BRIN may be missing HOT-updated rows on old snapshots

From: Tomas Vondra <tomas(at)vondra(dot)me>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Cc: Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>, Josef Šimánek <josef(dot)simanek(at)gmail(dot)com>
Subject: BRIN may be missing HOT-updated rows on old snapshots
Date: 2026-11-25 11:11:00
Message-ID: 4149ea14-bc53-4dd1-9512-4bbc742ef4e0@vondra.me
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

While fixing some HOT issues for BRIN (commit bae27968b76d), I've
noticed a separate issue with HOT and BRIN indexes, unrelated to the
indcheckxmin protection. I'm CCing Alvaro and Josef, who were involved
in 19d8e2308bc that relaxed the HOT handling for BRIN in PG16.

The problem is about "old" snapshots and index rebuilds (either from
REINDEX, or even just vacuum summarizing a page range).

Consider this example:

1) let's start with a table containing row (1), and a BRIN index

2) a REPEATABLE READ transaction starts, and gets a snapshot without
accessing the index yet (so no lock on it)

3) a different transaction updates 1 => 2

4) writer runs REINDEX, or re-summarizes the range in some way

5) the reader (with the REPEATABLE READ) snapshot reads the table
through the index, but does not find the "1" row

The reason is pretty simple - as coded, heapam_index_build_range_scan
only summarizes values from the root TID (in HOT chains). The other
values from that chain (on the same page) are skipped, but the index is
then used even for the old snapshots.

The bae27968b76d addresses the same issue for CREATE INDEX, by adding
the indcheckxmin handling. But that can't help with REINDEX or just
re-summarization of a range, because those don't do indcheckxmin (simply
because it's irrelevant for non-summarizing indexes, as those disable
HOT once there's an index on the updated column).

I think we need to fix heapam_index_build_range_scan(), so that it
indexes all values on the page, not just the root TID. Patch attached,
needs to be backpatched to 16 (where 19d8e2308bc landed). A reindex will
be necessary to fix existing indexes.

I wonder if we could do the same thing for CREATE INDEX, making
bae27968b76d mostly unnecessary. But I haven't tried that.

regards

--
Tomas Vondra

Attachment Content-Type Size
v1-0001-Add-an-isolation-reproducer-for-BRIN-misses-after.patch text/x-patch 8.6 KB
v1-0002-Add-a-READ-COMMITTED-reproducer-for-BRIN-misses-a.patch text/x-patch 6.5 KB
v1-0003-Include-still-visible-HOT-versions-in-BRIN-summar.patch text/x-patch 10.8 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2026-11-25 11:11:02 Re: Detect mismatched subscription and replication slot properties
Previous Message Adam Lee 2026-11-25 11:05:39 Re: Allow progress tracking of sub-commands