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

From: Konstantin Knizhnik <knizhnik(at)garret(dot)ru>
To: Rui Zhao <zhaorui126(at)gmail(dot)com>
Cc: 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>, 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-02 12:23:00
Message-ID: f3100f93-bc1a-4f95-af1d-e83139fc2518@garret.ru
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Wed, 2 Dec 2026 at xx:xx, <reporter> wrote:
> While testing v10, I noticed that PostgreSQL still fetches a dropped
> column's TOAST value when forming a row value. I could reproduce
> this on unpatched master with a plain SELECT:
> ...
> Both rows print as (), but the row inserted before the DROP still
> includes the bytes of f1. I posted a patch for master in a separate
> thread [1].

Thanks, that was a good catch, and it does matter for this patch.

The index build part of v10 was not affected: for a RECENTLY_DEAD
tuple, it nulls out dropped columns before evaluating the predicate and
the index expressions, so a whole-row reference does not fetch their
TOAST data.  The inlined copy made by the rewrite nulls them out too.

The scan-time check of the rewrite, however, skipped dropped columns,
on the grounds that reform_and_rewrite_tuple() throws them away. But
when CLUSTER sorts on an index with a whole-row expression, the sort
evaluates that expression on the original tuple, which fetches the
TOAST data of the dropped column as you describe.  With a deleted row
whose only TOAST data is in a dropped column, and that data vacuumed
away, v10 still fails:

  CREATE TABLE rewrite_drop (id int PRIMARY KEY, data text);
  ALTER TABLE rewrite_drop ALTER COLUMN data SET STORAGE EXTERNAL;
  INSERT INTO rewrite_drop VALUES (1, repeat('x', 2500)), (2,
repeat('y', 2500));
  ALTER TABLE rewrite_drop DROP COLUMN data;
  CREATE INDEX rewrite_drop_expr_idx ON rewrite_drop
      ((rewrite_drop IS NOT NULL), id);
  -- then the same sequence as in rewrite_stale_xmin.spec, with
  -- enable_indexscan = off:
  CLUSTER rewrite_drop USING rewrite_drop_expr_idx;
  ERROR:  missing chunk number 0 for toast value ... in ...

v11, attached, checks the TOAST data of dropped columns as well, so
such a tuple is treated as dead if it is gone, and inlined (with the
dropped column nulled out) if it may go away before the sort is done.
The dblink test gets two more permutations with TOAST data only in a
dropped column: CLUSTER sorting on a whole-row expression, which fails
with v10, and CREATE INDEX on a whole-row expression, which already
passed.

Your fix for master would make the sort skip dropped columns, but the
check here is cheap and keeps this patch independent of it.

--
Konstantin

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

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Amit Langote 2026-12-02 12:54:51 Re: BUG #19851: Adding a tautological CHECK(true) to a domain changes JSON_VALUE's result (silent NULL, value/type c
Previous Message ZizhuanLiu X-MAN 2026-12-02 12:09:20 Re: BUG #17545: Incorrect selectivity for IS NOT DISTINCT FROM and NULLs