Re: BUG #19519: REPACK can fail due to missing chunk for toast value

From: Konstantin Knizhnik <knizhnik(at)garret(dot)ru>
To: Matthias van de Meent <boekewurm+postgres(at)gmail(dot)com>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Konstantin Knizhnik <konstantin(dot)knizhnik(at)databricks(dot)com>, Rui Zhao <zhaorui126(at)gmail(dot)com>, Michael Paquier <michael(at)paquier(dot)xyz>, Andrey Borodin <x4mmm(at)yandex-team(dot)ru>, Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, Srinath Reddy Sadipiralla <srinath2133(at)gmail(dot)com>, Imran Zaheer <imran(dot)zhir(at)gmail(dot)com>, Alexander Lakhin <exclusion(at)gmail(dot)com>, PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #19519: REPACK can fail due to missing chunk for toast value
Date: 2026-12-01 19:31:26
Message-ID: 846c59ca-5dff-4627-8112-9f84c0b93a37@garret.ru
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Tue, 1 Dec 2026 at 18:xx, Matthias van de Meent
<boekewurm+postgres(at)gmail(dot)com> wrote:
> How sure are you about that?  On-access pruning happens
> (opportunistically) for every heap page accessed, and if the
> GlobalVisHorizons for that toast table change at any point while we're
> still accessing it, then we might still prune away tuples in the toast
> table next time we try to access that tuple.

You are right, and my claim in the v9 mail was wrong. AccessExclusiveLock
on the TOAST relation keeps VACUUM and other backends out of it, but not
the rewrite itself: every TOAST fetch is an index scan on the TOAST
relation, which calls heap_page_prune_opt() when it first pins a TOAST
heap page.  In v9 the window between the check and the fetch lasted for
the whole sort, and for update-chain members stashed in rewriteheap.c.

I could reproduce it.  v10, attached, adds an injection point between
the scan and the sort of a rewrite, and an isolation test in
src/test/modules/injection_points that uses it:

- s1 holds a snapshot, which makes the old version of an updated row
  RECENTLY_DEAD;
- s2 holds an XID that follows the update, which is what holds back the
  xmin of the CLUSTER;
- CLUSTER waits at the injection point after the scan; s1 and s2 commit,
  and s2 sends invalidations;
- the sort compares enum values that need a pg_enum lookup, which takes
  a new catalog snapshot, so the next TOAST fetch recomputes the horizon
  and prunes the chunks of the old version.

With v9 this fails with "missing chunk number 0 for toast value ...".

> The only valid xmin horizon we can rely on here is one that's
> protected by our own PROC entry's xmin; every other xmin might
> disappear and cause the GlobalVisHorizons to change, and prune away
> old toast tuples.  And even that xmin is not necessarily correct,
> because visibility horizons for a relation we're accessing could have
> been higher than our xmin.

Agreed, and v10 relies on exactly that, per TOAST chunk.  The caller
holds the active snapshot for the whole rewrite; let stable_xmin be its
xmin.  Our proc's xmin can rise during the command, but not past
stable_xmin, so no horizon our own pruning uses can pass it either. A
chunk whose xmax is invalid, lock-only, or at or after stable_xmin
therefore cannot be pruned before we are done.  It may already be gone,
for the reason you give, but the scan-time check from v9 catches that.
A chunk deleted (or locked by a multixact) by a transaction that
precedes stable_xmin may go away at any time.

The update XID of the tuple is not a good enough proxy for this: if an
UPDATE leaves a toasted column alone, both versions share its chunks,
and a later update in the chain that replaces the column deletes them
with its own XID, which can be older than the first updater's.

The only change from v9 is in the rewrite.  For a RECENTLY_DEAD tuple
with on-disk external values, the scan-time check now also reports
whether any chunk precedes stable_xmin:

- if chunks are missing, the tuple is treated as dead, as in v9;
- if all are there and stable, nothing more is done, as in v9.  That
  still covers the cases from the original reports, where a transaction
  in another database holds back our xmin and the deleter is newer;
- otherwise, the values are fetched right away, again tolerating
  missing chunks, and the tuple is replaced by a copy with the values
  inlined, keeping its identity and visibility information for
  rewriteheap.c.  The old TOAST relation is not accessed for the tuple
  after that.

So inlining is back, but only for tuples whose TOAST data can actually
disappear before they are written out.  The index build part is
unchanged, as it already fetches up front, tolerating missing chunks,
and does not go back to the TOAST relation afterwards.

--
Konstantin

Attachment Content-Type Size
v10-0001-Fix-missing-chunk-errors-during-heap-rewrites-an.patch text/plain 54.4 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Nathan Bossart 2026-12-01 20:39:19 Re: BUG #19891: pg_restore ignores an explicitly empty -F argument
Previous Message Konstantin Knizhnik 2026-12-01 19:01:20 Re: BUG #19519: REPACK can fail due to missing chunk for toast value