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

From: Konstantin Knizhnik <knizhnik(at)garret(dot)ru>
To: Robert Haas <robertmhaas(at)gmail(dot)com>, Konstantin Knizhnik <konstantin(dot)knizhnik(at)databricks(dot)com>
Cc: Rui Zhao <zhaorui126(at)gmail(dot)com>, Matthias van de Meent <boekewurm+postgres(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 15:05:29
Message-ID: 3eb2379c-af00-4f2e-b9cb-bb82cf3537c4@garret.ru
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On 30/11/2026 11:59 PM, Robert Haas wrote:

> On Thu, Nov 12, 2026 at 12:24 PM Konstantin Knizhnik
> <konstantin(dot)knizhnik(at)databricks(dot)com> wrote:
>> [new patch version]
> Hi,
>
> In general terms, I think this is probably the right fix.
>
> Suppose we have a tuple M in the main table and a corresponding tuple
> T in the toast table. Once a tuple is invisible to all transactions in
> the current database, either M or T can be removed at any time, in
> either order. So by the time a table-rewriting operation starts,
> either M or T might be gone while the other remains. In particular, M
> might remain while T is gone. Even in that case, everything is still
> OK as long as the rewrite sees M as dead, because then it isn't
> copied. But if the rewrite thinks that M is only recently dead, then
> it will try to copy it, which means trying to access T, which leads to
> the reported error.
>
> So in the originally reported test case and in the one reported in
> https://www.postgresql.org/message-id/flat/1779078055(dot)630771314(at)fmail2(dot)qdit
> the transaction in another database causes the rewrite to have an xmin
> that's at least as old as that other database transaction, which is
> what lets the rewrite think that M is only recently dead, while the
> vacuum that removed T thought it was just plain dead. So the only
> options for fixing this are (1) make sure that the table rewrite never
> sees something as only recently dead that a previous vacuum might have
> seen as fully dead or (2) tolerate the TOAST tuples being missing.
>
> I'm not very optimistic about (1). Many people have been unhappy over
> the years about the possibility of xmin horizons moving backward, but
> nobody has come up with a way to prevent it, so I'm not very
> optimistic about us coming up with a way to fix it now, either,
> especially one that is back-patchable. So I suspect that (2), which is
> what this patch implements, is the right idea.
>
> That said, there's a huge amount of action at a distance in this patch
> about which it is hard to feel good. We're ignoring errors in very
> low-level functions and basically just hoping that the cause is this
> problem rather than something else. I don't know what to do about
> that, but it doesn't seem great. The fact that the patch breaks ABI
> compatibility is a direct result of this problem: we have to feed the
> failure indicator up through multiple levels of the call stack.
>
> + /*
> + * The tuples are written out only after the
> sort, by which time
> + * the TOAST data of a recently-dead tuple may
> be gone, so fetch
> + * it now. If it is already gone, the tuple
> is dead to everyone
> + * who could still read it, so skip it.
> + */
>
> I think this comment is wrong. By the time we're running this code, we
> should have AccessExclusiveLock on both tables, so nothing can
> disappear any more. What is true is that the TOAST tuple might already
> be gone, from before the current command even started. By the time the
> tuple comes out of the tuplesort, we won't know any more whether we
> thought it was recently dead, so this is our last chance to tolerate a
> non-detoastable attribute only in recently-dead tuples. So in that
> sense this logic is needed, but it's not free: it means that we insert
> the detoasted tuple, which might be much larger, into the tuplesort,
> instead of the toasted version. If the recently dead tuple is wider
> than 1GB untoasted, that will fail where master succeeds. And even if
> that doesn't happen, the tuplesort could end up gigantic. My LLM
> claims to have observed a 726MB tupelstore where master didn't even
> spill to disk. Possible alternatives:
>
> - Check that every TOAST chunk is accessible at this point, and drop
> it if not. If it is, nothing should disappear later.
>
> -or-
>
> - Push the recently_dead flag through the sort.
>
> I'm inclined to think that the first of those options may be better,
> but I am not certain.
>
> + /*
> + * The TOAST data of a RECENTLY_DEAD tuple can be
> removed while we
> + * look at it; see heap_inline_recently_dead_toast().
> Fetch what the
> + * index needs up front, and skip the tuple if it is
> already gone.
> + */
>
> This comment is correct, because here we don't hold
> AccessExclusiveLock, or any lock that would prevent VACUUM from
> running concurrently, on the TOAST table. If we don't do this here,
> even the ExecQual that immediately follows is unsafe, although I would
> argue that the comment could be clearer about that. But (1) we can
> also pass the !tupleIsAlive test for a delete-in-progress tuple, where
> nothing should be missing and (2) this forcibly detoasts every
> attribute, even if the index predicates and expressions wouldn't have
> needed to do so, e.g. "where foo is null," and (3)
> ExecForceStoreHeapTuple() is expensive; we're about to need to deform
> the tuple again.
>
> Aside from the specific review points, this is just a scary patch.
> It's not clear that there's any clean fix that doesn't introduce some
> disadvantages elsewhere. I'm not sure what to do about that, but I
> certainly understand if anyone is looking at this and hesitating to
> commit. It's not that hard to imagine something breaking of which we
> have not thought.

Hi Robert,

Thank you for the review. Attached is v9, which follows your suggestions
and is a rewrite of the code part of the patch; the test is unchanged
apart from a comment.

- There is no more TOAST_MISSING_OK flag, so nothing is passed through
  toast_helper.c, heap_toast_insert_or_update(), detoast.c or
  rewriteheap.c, and the signature of relation_fetch_toast_slice in
  TableAmRoutine is unchanged.  Everything is now in heapam_handler.c,
  plus two heap-only functions in heaptoast.c,
  heap_toast_value_is_complete() and heap_toast_fetch_value_if_complete(),
  which share the chunk loop of heap_fetch_toast_slice() and report
  missing chunks instead of raising an error.

- Rewrite (CLUSTER, VACUUM FULL, REPACK): as you said, we hold
  AccessExclusiveLock on the TOAST relation, so nothing can disappear
  once the rewrite has started.  The TOAST data of a RECENTLY_DEAD tuple
  is checked at scan time, and if it is gone, the tuple is treated as
  dead and passed to rewrite_heap_dead_tuple(), as for any other dead
  tuple.  That also takes care of update chains stashed in
  rewriteheap.c, with no extra bookkeeping.  The sort path no longer
  inlines anything into the tuplesort.

- Index build: nothing keeps VACUUM or pruning of the TOAST relation
  from removing the data while the index is built, so it still has to be
  fetched up front.  This is now done only for RECENTLY_DEAD tuples,
  only for the varlena attributes referenced by the index columns,
  expressions and predicate (all of them for a whole-row reference), into
  a virtual slot in per-tuple memory.  ExecForceStoreHeapTuple() is gone.

- The TOAST table of a user_catalog_table doesn't inherit that option,
  so its VACUUM ignores catalog_xmin while the table itself respects it.
  Skipping a tuple there could silently lose a row that logical decoding
  needs, so such relations keep raising the error.

The dblink isolation test covers CLUSTER (index scan and sort), VACUUM
FULL, REPACK, CREATE INDEX on a column and on a whole-row expression,
and update chains whose older versions lose their TOAST data.  It fails
on master and passes with the patch; make check and the core isolation
tests pass as well.

Attachment Content-Type Size
v9-0001-Fix-missing-chunk-errors-during-heap-rewrites-and.patch text/plain 42.7 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2026-12-01 15:14:53 Re: BUG #19963: INNER JOIN ON qual evaluated as scan filter even when the inner input is empty
Previous Message Michael Banck 2026-12-01 14:37:36 Re: reltuples kept decreasing with each autovacuum run Re: BUG #18773