Skip to content

Use ONLY for CTID snapshot queries to exclude inherited child rows#909

Merged
kvch merged 4 commits into
mainfrom
fix/snapshot-only-table-pages
Jun 29, 2026
Merged

Use ONLY for CTID snapshot queries to exclude inherited child rows#909
kvch merged 4 commits into
mainfrom
fix/snapshot-only-table-pages

Conversation

@kvch

@kvch kvch commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

Problem

The CTID page-range data snapshot uses SELECT * FROM <table> WHERE ctid BETWEEN ... and SELECT MAX(ctid) FROM <table>. For a legacy-inheritance parent table (CREATE TABLE ... INHERITS), these implicitly span the parent plus all child heaps, so CTIDs aren't unique to the parent's storage: page ranges overlap and child rows get copied through the parent (and again when the child is snapshotted on its own).

Fix

Add ONLY to the page-range and max-CTID queries so each snapshot is confined to the selected table's own heap, matching pgstream's per-table snapshot model.

Note: this is a no-op for plain tables and leaf partitions, and does not affect declaratively partitioned parents (relkind='p'), which are already excluded from this path by the relkind='r' filter in getTableInfo.

Testing

  • Unit: TestSnapshotQueriesDoNotIncludeInheritedRows
  • Integration: Test_SnapshotToPostgres_SelectedParentTableDoesNotCopyInheritedRows
  • go test ./pkg/snapshot/generator/postgres/data/...

Cherry-picked from @danddanddand (fix/snapshot-only-table-pages).

@kvch
kvch marked this pull request as ready for review June 23, 2026 18:10
@kvch
kvch requested a review from tsg June 23, 2026 18:10
@kvch
kvch enabled auto-merge (squash) June 29, 2026 14:37
@kvch
kvch merged commit bce8ea8 into main Jun 29, 2026
8 checks passed
@kvch
kvch deleted the fix/snapshot-only-table-pages branch June 29, 2026 14:38
@github-actions

Copy link
Copy Markdown

Merging this branch will not change overall coverage

Impacted Packages Coverage Δ 🤖
github.com/xataio/pgstream/pkg/snapshot/generator/postgres/data 78.48% (ø)
github.com/xataio/pgstream/pkg/stream/integration 0.00% (ø)

Coverage by file

Changed files (no unit tests)

Changed File Coverage Δ Total Covered Missed 🤖
github.com/xataio/pgstream/pkg/snapshot/generator/postgres/data/pg_snapshot_generator.go 84.47% (ø) 206 174 32

Please note that the "Total", "Covered", and "Missed" counts above refer to code statements instead of lines of code. The value in brackets refers to the test coverage of that file in the old version of the code.

Changed unit test files

  • github.com/xataio/pgstream/pkg/snapshot/generator/postgres/data/pg_snapshot_generator_test.go
  • github.com/xataio/pgstream/pkg/stream/integration/snapshot_pg_integration_test.go

kvch added a commit that referenced this pull request Jul 6, 2026
…) (#966)

Backports the following snapshot/restore fixes from `main` to the
`v1.1.x` release branch.

| PR | Commit | Fix |
|----|--------|-----|
| #906 | 2cf967b | Skip legacy public PL/pgSQL handler functions on
snapshot restore |
| #908 | 5f1d6e3 | Refresh materialized views after data restore |
| #909 | bce8ea8 | Use `ONLY` for CTID snapshot queries to exclude
inherited child rows |
| #907 | 1b090aa | Restore `CLUSTER ON` statements after their
referenced indexes |
| #910 | 6b3bb8a | Type `int4range`/`int8range` values before
insert/COPY |

---------

Co-authored-by: dand <daniel.dent@borderconnect.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
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