Skip to content

Fix backup consistency after concurrent TRUNCATE. - #145

Open
woblerr wants to merge 1 commit into
apache:mainfrom
woblerr:snapshot_lock_retry
Open

woblerr wants to merge 1 commit into
apache:mainfrom
woblerr:snapshot_lock_retry

Conversation

@woblerr

@woblerr woblerr commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Problem

gpbackup takes its snapshot before acquiring table locks. Concurrent TRUNCATE can replace storage while it waits, so backup and restore report success but restore an empty table, hiding data loss.

Reproduction

  1. Create a distributed heap table containing {1,2,3} on Cloudberry.
  2. In another transaction, run TRUNCATE and insert {4,5,6} without committing.
  3. Start gpbackup, wait until pg_locks shows the backup waiting for AccessShareLock on this table, then commit the writer.
  4. Restore to another database: both commands exit 0, but the restored table is empty.

Fix

  • On Cloudberry, validate table storage and identity after locking, including descendant partitions.
  • Add --snapshot-attempts (default 3) to retry with a fresh shared snapshot and fail the backup when attempts are exhausted.
  • Re-resolve table filters on each retry and disable heap file hashes for that backup to avoid skipping heap data.

A concurrent TRUNCATE can replace table storage after the backup
snapshot but before table locks are acquired. Backup and restore can
then report success while restoring an empty table.

On Cloudberry, validate storage and table identity after locking,
including descendant partitions. Add --snapshot-attempts (default 3) to
retry with a fresh shared snapshot, rebuild filters, and discard heap
file hashes. Fail the backup when the limit is reached.
@tuhaihe
tuhaihe requested review from MisterRaindrop, lss602726449 and tuhaihe and removed request for lss602726449 November 23, 2026 06:39
@tuhaihe

tuhaihe commented Nov 23, 2026

Copy link
Copy Markdown
Member

Hi @talmacschen-arch @liang8283, welcome to help review when you're available. Thanks!

@tuhaihe

tuhaihe commented Nov 23, 2026

Copy link
Copy Markdown
Member

Reviewed by Claude Code. FYI.


pr-145-review.md

@liang8283

Copy link
Copy Markdown
Collaborator

Storage-change check also trips on MVCC-safe rewrites, and can fail backups.

CheckSnapshotTables treats any relfilenode change as a conflict, but for heap tables VACUUM FULL, CLUSTER and ALTER TABLE ... SET TABLESPACE also assign a new relfilenode while keeping every row visible to older snapshots.

On a Cloudberry cluster with 60 heap tables, running this branch's gpbackup while vacuumdb --full --jobs 8 was looping, one run exhausted the default 3 attempts and failed with:

could not obtain a consistent backup snapshot after 3 attempt(s): public.t12 (table storage changed after the snapshot); ...

Another run only succeeded on the last attempt. The same workload backed up by main succeeded on the first attempt and restored cleanly.

Besides failing backups whose data was fine, every false-positive retry also discards --heap-file-hash results, so that incremental and the next one carry all heap tables in full. Could the check skip rewrites that keep the data visible, or at least document this behavior?

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.

3 participants