| From: | Robert Haas <robertmhaas(at)gmail(dot)com> |
|---|---|
| To: | 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-11-30 21:59:04 |
| Message-ID: | CA+TgmoZo+Z2cs3p0qbtRuk0tLsZJ2hXMZOXuZZQsZF=YGF3GSw@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
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.
--
Robert Haas
Databricks
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Zsolt Parragi | 2026-11-30 22:27:45 | Re: BUG #19928: pg_upgrade fails on v18 schema with NOT NULL ... NO INHERIT and inheritance |
| Previous Message | Nathan Bossart | 2026-11-30 20:43:03 | Re: BUG #19891: pg_restore ignores an explicitly empty -F argument |