| From: | Michael Paquier <michael(at)paquier(dot)xyz> |
|---|---|
| To: | Xnerv Wang <xnervwang(at)gmail(dot)com> |
| Cc: | pgsql-hackers(at)lists(dot)postgresql(dot)org, tgl(at)sss(dot)pgh(dot)pa(dot)us, rahilasyed90(at)gmail(dot)com, Andres Freund <andres(at)anarazel(dot)de> |
| Subject: | Re: GetNewOidWithIndex() can reuse a TOAST value ID while its chunks are still in the heap |
| Date: | 2026-11-30 05:36:22 |
| Message-ID: | aw0LVlr3P38Mzw2q@paquier.xyz |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
On Fri, Nov 27, 2026 at 06:35:49PM -0800, Xnerv Wang wrote:
> Hi hackers,
Also adding Andres here, as he's commented on this stuff and the
LP_DEAD markups in the past.
> 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
Ugh.
> I ran CHECKPOINT, stopped the primary, and set the next OID with:
>
> pg_resetwal -o <the old value ID>
Just to reproduce an OID wraparound. Why not.
> 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.
>
> 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?
I cannot get excited about putting a clear of ignore_killed_tuples in
index_beginscan_internal(), because this solution would just cause
more heap fetches, for what I get leads to the same result under
a CLUSTER/REPACK. Something among the same lines is a clear in
systable_beginscan(): less efficiency, more reads. And both of these
do not cover assumptions that could be hold on out-of-core code that
uses these routines. For all these reasons, these two pproaches are
not appealing to me.
+ /*
+ * Also check index entries that are marked LP_DEAD, since the rows
+ * they point to may not have been pruned yet. The mark is only a
+ * hint: a crash can lose it, and a standby ignores it during recovery
+ * and may not have it after promotion. Wherever it is missing or
+ * ignored, reusing the OID of a deleted TOAST value whose chunks are
+ * still in the heap would make SnapshotToast return the chunks of
+ * both values.
+ */
+ if (scan->iscan)
+ scan->iscan->ignore_killed_tuples = false;
The top of GetNewOidWithIndex() has a special note about SnapshotAny.
I'd suggest to amend the comment block there with an extra paragraph,
like:
"For the same reason, the scan must check index entries marked as
LP_DEAD, as their heap tuples may not have been pruned yet. Other
scans may still find these tuples, as LP_DEAD marks are not WAL-logged
and are ignored during recovery."
And then add a second comment: "See notes above about LP_DEAD."
I'd be OK with a test here, but I'm not sure about the location.
Perhaps test_misc?
Something that you are not mentioning is a new SO_* flag in
ScanOptions, that could be applied on a per-caller basis, which is
what I guess we are looking at here. Not something that I would
backpatch of course, but something that could be done on HEAD so as it
becomes possible to grep which caller wishes to keep dead tuples.
Hence that's a new SO_KEEP_KILLED? That would be after an agreement
for the initial patch is reached, of course.
Any thoughts and/or comments from others?
--
Michael
| From | Date | Subject | |
|---|---|---|---|
| Next Message | shihao zhong | 2026-11-30 05:38:21 | citext gives wrong results with a nondeterministic collation |
| Previous Message | Shinya Kato | 2026-11-30 05:21:04 | Re: Speed up binary COPY TO by not allocating 1 kB per datum |