| From: | Xnerv Wang <xnervwang(at)gmail(dot)com> |
|---|---|
| To: | pgsql-hackers(at)lists(dot)postgresql(dot)org |
| Cc: | tgl(at)sss(dot)pgh(dot)pa(dot)us, michael(at)paquier(dot)xyz, rahilasyed90(at)gmail(dot)com |
| Subject: | GetNewOidWithIndex() can reuse a TOAST value ID while its chunks are still in the heap |
| Date: | 2026-11-28 02:35:49 |
| Message-ID: | CAFEh72CNz0qxQfCatatbuAUJBuoYFk0kxfL_5nccShcdgkWsyQ@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi hackers,
In our tests, a new TOAST value reused a deleted value's ID. When we
tried to read the new value on a read replica, the read failed:
ERROR: unexpected chunk number 0 (expected 1) for toast value N in
pg_toast_M
I reproduced this on community PostgreSQL 18.6 with a hot standby. The
table had autovacuum disabled and column v set to STORAGE EXTERNAL.
Inserting a 3000-byte value produced two chunks. I read the value's ID
with pg_column_toast_chunk_id(v) before deleting it. Once no snapshot
was older than the delete, I looked up the chunks in the TOAST table
with enable_seqscan and enable_bitmapscan off. The index lookup marked
both TOAST index entries LP_DEAD while both heap chunks were still
LP_NORMAL.
I ran CHECKPOINT, stopped the primary, and set the next OID with:
pg_resetwal -o <the old value ID>
After restarting the primary, I inserted a different 3000-byte value. It
got the old ID. The primary returned the new value, but SELECT on the
hot standby failed with the error above.
My reading is that since commit 0408e1ed599 (2018), GetNewOidWithIndex()
has probed the index with SnapshotAny so that deleted rows count as
collisions until they are pruned. Outside recovery,
RelationGetIndexScan() sets ignore_killed_tuples, so nbtree skips
LP_DEAD entries and the probe misses the old chunks. During recovery the
standby follows those entries as well as the new ones, and
HeapTupleSatisfiesToast() accepts chunk 0 of both values because it
checks only xmin.
In GetNewOidWithIndex(), the attached patch clears ignore_killed_tuples
on the index scan before the first systable_getnext(). It adds the
reproducer as a TAP test. The commit message explains why a standby
cannot rely on the LP_DEAD marks.
In 2016, Andres Freund wrote that to make GetNewOidWithIndex() safe,
"we'd at least make hint bit writes by the scan in GetNewOidWithIndex()
durable, and likely also disable the killtuples optimization". I think
the hint bit writes need not be durable because, with the attached
patch, the probe checks the heap for each matching index entry whether
or not it is marked LP_DEAD.
https://www.postgresql.org/message-id/20160510161916.23wwzxn26y5ou2ez@alap3.anarazel.de
The first email in "Bugs in TOAST handling, OID assignment and redo
recovery", the thread behind 0408e1ed599, lists killing the index
pointer as one way the window closes for readers. Does that reasoning
also cover a pointer that is already killed when the OID probe runs?
That's when I saw the ID get reused.
I left toastrel_valueid_exists() alone. It makes the same SnapshotAny
probe, but runs only during CLUSTER and VACUUM FULL. I couldn't find a
case where an LP_DEAD entry there would cause a collision.
The attached .txt makes the change in index_beginscan_internal(), so any
index scan using SnapshotAny keeps LP_DEAD entries. I kept the change in
GetNewOidWithIndex() because the broader version also affects the
index-ordered copy in CLUSTER/REPACK, logical replication's
update_deleted conflict check, and extension index scans that use
SnapshotAny. Would index_beginscan_internal() be a better place for
this?
The new test fails without the fix. With either change, both the new
test and check-world pass.
Thanks,
Xnerv Wang
Amazon Web Services
| Attachment | Content-Type | Size |
|---|---|---|
| nocfbot-snapshotany-index-scans.txt | text/plain | 707 bytes |
| v1-0001-Check-LP_DEAD-index-entries-before-reusing-an-OID.patch | text/x-patch | 7.4 KB |
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | Greg Burd | 2026-11-28 01:38:39 | Re: nbtree: opt-in leaf key prefix compression |