| From: | Konstantin Knizhnik <knizhnik(at)garret(dot)ru> |
|---|---|
| To: | Robert Haas <robertmhaas(at)gmail(dot)com>, Matthias van de Meent <boekewurm+postgres(at)gmail(dot)com> |
| Cc: | Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>, pgsql-bugs(at)lists(dot)postgresql(dot)org, Rui Zhao <zhaorui126(at)gmail(dot)com>, Konstantin Knizhnik <konstantin(dot)knizhnik(at)databricks(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> |
| Subject: | Re: BUG #19519: REPACK can fail due to missing chunk for toast value |
| Date: | 2026-12-05 20:08:12 |
| Message-ID: | 1fba6f25-82d2-43f9-b8a5-0f9b284631d5@garret.ru |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
On Fri, 4 Dec 2026 at 22:02, Robert Haas <robertmhaas(at)gmail(dot)com> wrote:
> Relying on that feels fragile. It feels unlikely that someone would
> ever manage to successfully change the semantics of SnapshotToast, but
> if they did try to do that, they probably wouldn't notice the effect
> on this.
Agreed; v12 should not have rested on that. With AccessExclusiveLock
the only backend that can mark the old TOAST index's entries LP_DEAD is
ours. Today that does not happen for xmax'd chunks, because
SnapshotToast ignores xmax and so heapam_index_kill_item() is not
reached for them. If that ever changed, the rewrite could lose the
index entries of chunks that are still on the heap, and report "missing
chunk" even with pruning disabled.
v13, attached, therefore also skips kill-bit marking for the relation
while pruning is disabled. The check is next to the existing recovery
guard in heapam_index_kill_item().
> I feel like AtEO(Sub)Xact callback would be the normal way to handle
> error cleanup here.
Done. The local-transaction-ID trick is gone. heap_page_prune_opt_disable()
registers an AtEOXact callback the first time it is used, and that
callback clears the setting on commit, abort and prepare. The rewrite
still clears it explicitly after end_heap_rewrite(), so in the success
path pruning is re-enabled as soon as the old TOAST relation is no
longer needed.
On Fri, 4 Dec 2026 at 22:20, Matthias van de Meent
<boekewurm+postgres(at)gmail(dot)com> wrote:
> Is there really no better approach available than this? I strongly
> dislike hidden magic global variables. Furthermore, blocking pruning
> on a toast table that was already targeted by REPACK (either directly
> or indirectly) does sound like it would not benefit the user.
I share the dislike of a backend-global switch. The toast relation is
opened and closed by OID on every fetch, though, so a flag on Relation
would not stick unless we kept it open for the whole rewrite, and
threading a "don't prune" flag through detoast / toast_helper /
tableam is the action-at-a-distance Robert asked us to avoid. A
backend-local exclusion for one OID, cleared by AtEOXact, seems the
least invasive way to say "leave this relation alone until I am done".
As to whether the user benefits from pruning it meanwhile: the old
TOAST relation is about to be dropped. Anything we prune there is
thrown away with the file, so skipping that work does not cost the
user anything useful. Robert made the same point.
> I'm starting to think that the current treatment of the TOAST tables
> by pruning/mvcc (as if it were a normal data table, with
> VISHORIZON_DATA) is just plainly incorrect, [...] Toast may need
> stronger guarantees; e.g. VISHORIZON_SHARED, [...]
I think that is worth a separate discussion, but it would not replace
this patch.
VISHORIZON_SHARED for TOAST would stop a transaction in another
database, and a replication slot, from letting TOAST vacuum run ahead
of the rewrite's OldestXmin. That covers the original reports. It
does not cover:
- the rewrite pruning the old TOAST relation itself once some other
xmin goes away mid-command, which is why we still need the pruning
switch (or inlining);
- chunks that are already gone when the rewrite starts, which is why
we still need the scan-time check that tolerates missing chunks;
- CREATE INDEX, which holds no lock that keeps VACUUM out of the TOAST
relation.
It also has a cost for multi-tenant clusters: a long transaction in
database A would hold back TOAST vacuum in database B, which
VISHORIZON_DATA deliberately does not do today. And as you note in the
aside, even SHARED horizons are not monotonic under hot_standby_feedback
and slot creation.
So I would keep this patch as the local fix for rewrites and index
builds, and treat a stronger TOAST horizon as a follow-up, if we can
agree on the trade-offs.
> Aside: We should probably start working on making the horizons' lack
> of monotonicity actually guaranteed monotonicity, [...]
Robert already summarised the history of that effort. I have nothing
to add, other than that this patch does not depend on it.
On Fri, 4 Dec 2026 at 23:08, Robert Haas <robertmhaas(at)gmail(dot)com> wrote:
> Disabling pruning doesn't actually sound that bad. Aren't we going to
> rewrite the toast table and drop all the dead tuples anyway?
Yes. That is why I prefer it to the inlining of v10/v11, which blew up
memory and sort spill for a common case, as Zsolt showed.
--
Konstantin
| Attachment | Content-Type | Size |
|---|---|---|
| v13-0001-Fix-missing-chunk-errors-during-heap-rewrites-an.patch | text/plain | 60.3 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | shihao zhong | 2026-12-05 20:26:11 | Re: BUG #19960: Phrase search matches when it should not: phrase positions wrap at 16383 |
| Previous Message | Tom Lane | 2026-12-05 18:43:59 | Re: btree_gist: <-> fails on same-sign infinite intervals |