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

From: Konstantin Knizhnik <knizhnik(at)garret(dot)ru>
To: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org, Rui Zhao <zhaorui126(at)gmail(dot)com>, Matthias van de Meent <boekewurm+postgres(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-03 11:37:56
Message-ID: b89f67e7-a1f7-461b-9231-819e41c26821@garret.ru
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

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.
- stable_xmin, the xmax checks on TOAST chunks and the inlining are
  gone.  The scan-time check is the one from v9, still including
  dropped columns as in v11.
- The injection point test from v10 is kept, as it fails without the
  pruning being disabled.  It now checks that the rewrite does not prune
  TOAST chunks that become removable while it runs.

With v12, your example uses the same memory as without the
REPEATABLE READ transaction, where the old versions are simply removed.

--
Konstantin

Attachment Content-Type Size
v12-0001-Fix-missing-chunk-errors-during-heap-rewrites-an.patch text/plain 58.5 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Jan Nidzwetzki 2026-12-03 11:40:25 Re: pg_rewind WAL segments deletion pitfall
Previous Message Fujii Masao 2026-12-03 10:56:01 Re: BUG #19967: ecpg crashes after "cursor does not exist" on FETCH or MOVE