Skip to content

fix(catalog): sort backups with a consistent order - #344

Open
augustoribeiro wants to merge 1 commit into
cloudnative-pg:mainfrom
augustoribeiro:fix/catalog-sort
Open

augustoribeiro wants to merge 1 commit into
cloudnative-pg:mainfrom
augustoribeiro:fix/catalog-sort

Conversation

@augustoribeiro

Copy link
Copy Markdown

fix(catalog): sort backups with a consistent order

Catalog.Less is not a strict weak ordering:

  • a backup with no end time is "less" than every other backup, including other backups with no end time;
  • a backup with no begin time is neither less nor greater than anything, so it counts as equal to every backup;
  • two completed backups are compared by i.BeginTime against j.EndTime.

Once the catalog holds a backup that failed before barman wrote its begin time (begin_time=None, status=FAILED), sort.Sort can leave completed backups out of order. We hit this in production on a catalog with 26 entries: 7 completed, one FAILED with no begin time, and the rest orphaned STARTED runs.

  • GetLastSuccessfulBackupTime returned the end of a backup from 8 days earlier, older than FirstRecoverabilityPoint.
  • The plugin's last_available_backup_timestamp metric froze and alerted, even though a backup had completed every day since.
  • FindBackupInfo sees the same sorted list, so a restore to "latest" would have started from that old base backup.

Removing the single entry with no begin time made the output correct again.

This change orders completed backups first, then the incomplete ones, each by begin time and then by ID. The order is total, and the result no longer depends on the order barman listed the backups in. Every caller already skips incomplete backups through isBackupDone, so moving them to the end changes no result.

The new test uses the shape of the production catalog. On the current code it fails on the latest backup, the first recoverability point and FindBackupInfo, and with input in shuffled order; with this change it passes.

🤖 Generated with Claude Code

Less compared one backup's begin time with the other's end time, treated
every backup with no end time as smaller than any other, and a backup with
no begin time as equal to all of them. That is not a strict weak ordering,
so once the catalog holds a backup that failed before it began, sort.Sort
can leave completed backups out of order. LatestBackupInfo and
FirstRecoverabilityPoint then report the wrong backups, and FindBackupInfo
can restore from an old base backup instead of the latest one.

Order completed backups first, then the incomplete ones, each by begin
time and then by ID.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Augusto Ribeiro <augusto.mcc@gmail.com>
@augustoribeiro
augustoribeiro requested a review from a team as a code owner November 25, 2026 10:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant