| From: | Matthias van de Meent <boekewurm+postgres(at)gmail(dot)com> |
|---|---|
| To: | Konstantin Knizhnik <knizhnik(at)garret(dot)ru> |
| 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>, Robert Haas <robertmhaas(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-04 22:20:18 |
| Message-ID: | CAEze2WieM5Pvi4H8y+uq+ET3JSAz8m-t+06xhB9J12unQhu4Uw@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
On Thu, 3 Dec 2026 at 12:38, Konstantin Knizhnik <knizhnik(at)garret(dot)ru> wrote:
>
> On Thu, 3 Dec 2026 at xx:xx, <reviewer> wrote:
> > Isn't this a common case?
> > ...
> > This peaks at 250MB, already 640MB with 6000 rows, and so on.
>
> Yes, it is. A long-running read-only transaction in the same database
> is enough to make every old version RECENTLY_DEAD with TOAST data
> deleted before the xmin of the rewrite, so v10/v11 inlined all of them.
> It is even worse than holding one tuple at a time: an inlined old
> version whose successor has not been seen yet is stashed in
> rewriteheap.c's unresolved_tups until the successor shows up, so in your
> example all 2000 old versions were held in memory, with their 100kB
> values. Without inlining, the stashed tuples only carry TOAST pointers.
>
> > Do we need the inlining at all?
> >
> > We hold an AccessExclusiveLock on the old TOAST relation, taken before
> > OldestXmin is computed. During the rewrite only heap_page_prune_opt()
> > in our backend can remove chunks. Couldn't
> > heapam_relation_copy_for_cluster() disable on access pruning for that
> > one relation while it runs? The file is dropped at the end anyway.
> > Then the check during the scan is enough, and stable_xmin and the
> > second pass can go away.
>
> I agree, and v12, attached, does that. I also checked that nothing else
> in our backend can make chunks disappear: a TOAST fetch could in
> principle mark index entries as killed, so that later fetches skip
> them, but SnapshotToast ignores xmax, so every chunk tuple is visible
> and heap_hot_search_buffer() clears all_dead before it ever looks at
> whether the tuple is dead to everybody.
>
> The changes from v11:
>
> - pruneheap.c gets heap_page_prune_opt_disable(relid), which makes
> heap_page_prune_opt() leave that one relation alone in this backend.
> heapam_relation_copy_for_cluster() calls it for the old TOAST
> relation before the scan, unless it is REPACK CONCURRENTLY, and turns
> it off again after end_heap_rewrite(), which writes out the stashed
> update-chain members. The setting also records the local transaction
> ID, so if the rewrite fails, pruning comes back with the next
> transaction without needing PG_TRY; until then, our backend just
> doesn't prune that relation, which is harmless.
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'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, especially given the toast
snapshot's behaviour of looking at (and returning) data that's not
visible to the MVCC snapshots protected by the GlobalVisTests. Toast
may need stronger guarantees; e.g. VISHORIZON_SHARED, so that we have
strong(er) monotonicity guarantees about the cleanup of its data and
that any selected xmin for the main table's scan will definitely fall
within the horizon used in the pruning of the toast table, insofar we
can make such guarantees right now. At least for VISHORIZON_SHARED
the break in monotonicity is limited to replication slots, rather than
backends normal backends that connected to the same database.
Aside: We should probably start working on making the horizons' lack
of monotonicity actually guaranteed monotonicity, because this is not
the first time we're getting bitten by the horizons receding. Bug
#17257 comes to mind, but I seem to recall we've seen more than just
that bug.
Kind regards,
Matthias van de Meent
Databricks (https://www.databricks.com)
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Robert Haas | 2026-12-04 23:08:31 | Re: BUG #19519: REPACK can fail due to missing chunk for toast value |
| Previous Message | Robert Haas | 2026-12-04 22:02:26 | Re: BUG #19519: REPACK can fail due to missing chunk for toast value |