Re: GetNewOidWithIndex() can reuse a TOAST value ID while its chunks are still in the heap

From: xnervwang(at)gmail(dot)com
To: michael(at)paquier(dot)xyz
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org, tgl(at)sss(dot)pgh(dot)pa(dot)us, rahilasyed90(at)gmail(dot)com, 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 07:14:35
Message-ID: CAFEh72CUpr-cRacpuueeVETqOBxN1+WiDt1DV6o9GgLTSB_dgA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sun, Nov 29, 2026 at 9:36 PM Michael Paquier <michael(at)paquier(dot)xyz> wrote:
> 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.

I've dropped the index_beginscan_internal() variant from my first email.

> 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."

That comment block has your paragraph as written. At the call site, "see
notes above about LP_DEAD" follows the style of the "see notes above
about using SnapshotAny" line above it.

> I'd be OK with a test here, but I'm not sure about the location.
> Perhaps test_misc?

The test is now in src/test/modules/test_misc/t/017_toast_oid_reuse.pl.
I added contrib/pageinspect to EXTRA_INSTALL in the test_misc Makefile,
which the test needs to check the LP_DEAD marks.

> 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.

Once we've settled this fix, I can write a separate patch for HEAD.

The attached v2 patch is rebased on current master (03dd453a67c). The
fix itself is unchanged from v1. The new test fails without the fix;
with it, the test and check-world pass.

Thanks,
Xnerv Wang
Amazon Web Services

Attachment Content-Type Size
v2-0001-Check-LP_DEAD-index-entries-before-reusing-an-OID.patch text/x-patch 7.8 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Previous Message Ajit Awekar 2026-11-30 06:57:53 Re: Continuous re-validation of session credentials