| 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:01:20 |
| Message-ID: | 7ae5efa5-f6fc-47f8-a832-34f38c592a10@garret.ru |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
On 01/12/2026 6:48 PM, Matthias van de Meent wrote:
> On Tue, 1 Dec 2026 at 16:05, Konstantin Knizhnik<knizhnik(at)garret(dot)ru> wrote:
>> - Rewrite (CLUSTER, VACUUM FULL, REPACK): as you said, we hold
>> AccessExclusiveLock on the TOAST relation, so nothing can disappear
>> once the rewrite has started.
> 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.
>
> See heapam_index_heap_fetch(); it calls into heap_page_prune_opt()
> whenever the heap page holding the tuple corresponding to the index's
> result is first loaded into memory.
>
> 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.
>
>> 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.
> That's causing a regression from earlier versions of this patch, as
> those toast tuples may still be removed concurrently. See above for
> why.
>
> -Matthias
Agreed. I think this splits the RECENTLY_DEAD tuples into two groups.
1.
Update XID at or after MyProc->xmin. Our own xmin holds back every
horizon that can prune the old TOAST relation, including the one our
backend uses for on-access pruning. Other backends cannot access
that relation at all while we hold the lock. So the chunks cannot go
away during the command. They can only be gone already, when some
horizon was higher than our xmin before we started, which is the
case you mention. A check at scan time is enough for these tuples.
This is the case from the original reports: the transaction in
another database holds back our xmin, so the deleter is newer than it.
2.
Update XID before MyProc->xmin. These tuples are RECENTLY_DEAD only
because some other xmin holds OldestXmin back, for example:
* a read-only snapshot in the same database,
* a replication slot,
* hot_standby_feedback.
That xmin can go away at any moment. For these tuples I would go
back to what v8 did in the sort path, but for both paths: fetch the
referenced TOAST data at scan time, tolerating missing chunks. Then
treat the tuple as dead if the data is gone, or replace it with a
copy that has the values inlined, so that nothing in the old TOAST
relation is accessed for it again.
This keeps Robert's point about avoiding action at a distance.
Everything stays in heapam_handler.c and the two heaptoast.c functions,
and TableAmRoutine, detoast.c, toast_helper.c and rewriteheap.c stay
unchanged. It also limits inlining to the tuples that are actually at
risk. The common case from the bug reports keeps the cheaper check-only
path.
The index build part of v9 already fetches up front with missing_ok and
does not go back to the TOAST relation afterwards, so I think it is not
affected.
To cover the second group in the test, I would add an injection point
between the scan and the write. A same-database snapshot that holds
OldestXmin back would be released there, and then a TOAST fetch would
prune the page.
Unless you see a problem with this split, I will post v10 along these lines.
-- Konstantin
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Konstantin Knizhnik | 2026-12-01 19:31:26 | Re: BUG #19519: REPACK can fail due to missing chunk for toast value |
| Previous Message | surya poondla | 2026-12-01 18:15:40 | Re: BUG #19688: pg_dump --schema scans all sequences in PostgreSQL 18, causing severe performance regression |