| From: | Tomas Vondra <tomas(at)vondra(dot)me> |
|---|---|
| To: | PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | BRIN re-summarization is not properly logging snapshotConflictHorizon / wrong results on hot standby |
| Date: | 2026-12-06 15:11:59 |
| Message-ID: | f2d04d02-c11b-4d4a-9faf-de4f361f074c@vondra.me |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
While investigating an issue with incorrect index scan results with old
snapshots on a hot standby (more about that in a separate thread), I've
realized there's a BRIN-specific gap. Unlike the other index types, BRIN
is not properly WAL-logging the snapshotConflictHorizon when summarizing
ranges. With old snapshot on standby (and with hot_standby_feedback=off,
which is not that uncommon), this can produce incorrect query results.
Consider this:
0) primary + hot standby with hot_standby_feedback=off
1) primary: There's a BRIN index, with a page range withoug a summary.
2) standby: A session acquires a snapshot (could be REPEATABLE READ, or
even READ COMMITTED).
3) primary: DELETE a bunch of rows from the table. Those are still
visible on the standby, with the "old" snapshot.
4) standby: Run a query using the BRIN index (lookup of the rows deleted
on primary). Everything works fine, because the summary does not have a
summary (so it matches all queries). Or maybe there's a summary, but it
still has the deleted rows.
5) primary: Summarize the range, which writes the summary to the index.
The summary however skips the deleted tuples - no session on the primary
does not see them, and there's no hot-standby feedback.
6) standby: The BRIN summary gets applied from WAL.
7) standby: Run the query again, using the old snapshot. Unfortunately,
this time it uses the new summary, and it won't see the rows :-(
The other index types don't have this issue, because they WAL-log so
called snapshotConflictHorizon, which allows the standby to either delay
the WAL replay for a bit, or cancels the conflicting queries. See for
example of that are xl_btree_delete or xl_hash_vacuum_one_page.
But BRIN doesn't have anything like that. Hence the issue.
Attached is a patch series adding a TAP test demonstrating the issue,
and then fixing it by adding XLOG_BRIN_SUMMARIZE_CONFLICT adding this.
It's a bit more complex - it also addes ii_DeadTuplesHorizon to
IndexInfo, and updates it in heapam_index_build_range_scan. I'm not
entirely sure it has to work like this, maybe there's a better way to
determine the horizon?
But overall, I think WAL-logging the snapshotConflictHorizon is the
right way to fix this. The problem is this only works for master - it
requires a new WAL record, so it can't be backpatched.
So what can ve do about this on backbranches? I have two rough ideas
that I think would be acceptable:
a) For summarizing indexes, summarize even the DEAD tuples.
That is not great, as the index will match more rows, but it's also
fairly cheap as the summaries are cheap (much cheaper than regular index
entries). If needed, people could still vacuum the table first, which
deletes the rows (and does proper WAL-logging of the horizon). After
that, there are no dead tuples, and the summary won't include them.
b) Piggy-back on ii_BrokenHotChain / indcheckxmin somehow?
We do have a somewhat related capability during index (re)builds, using
indcheckxmin and ii_BrokenHotChain. Maybe we could piggy-back on that
somehow. But not sure if that's feasible.
regards
--
Tomas Vondra
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-Add-a-standby-reproducer-for-wrong-results-after-.patch | text/x-patch | 5.5 KB |
| v1-0002-Log-a-snapshot-conflict-when-BRIN-summarization-o.patch | text/x-patch | 18.3 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Tomas Vondra | 2026-12-06 15:21:49 | Re: BRIN re-summarization is not properly logging snapshotConflictHorizon / wrong results on hot standby |
| Previous Message | ZizhuanLiu X-MAN | 2026-12-06 14:55:20 | [PROPOSAL] Canonicalize BooleanTest(NOT x, t) to BooleanTest(x, flip(t)) |