Bug: Whole-row var in indexes corrupts indexes after DDL

From: Matthias van de Meent <boekewurm(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Peter Eisentraut <peter(at)eisentraut(dot)org>
Subject: Bug: Whole-row var in indexes corrupts indexes after DDL
Date: 2026-07-29 19:45:44
Message-ID: CAEze2WjDaDyvztdXh3Cb2J=11CMVRp4NWAW0E6dBRKa1T9w5ag@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

The attached SQL script shows a clear index corruption case caused by
incomplete tracking of whole-row vars in index expressions and the
invalidation of those when the table's layout is modified by DDL. It
has similar effects as the bug reported by Ayush Tiwari and fixed with
a4639d64e2; the index wasn't built with new data after the table's
whole-row expression changed for existing rows through DDL.

I've attached a patch which adds explicit tracking for whole-row
expressions in pg_depend, and use it to make sure we're rebuilding
indexes and revalidating constraints where and when required, without
O(N) dependency scans.

(This was triggered by some traffic on the list and in the commit log
on the topic of whole-row support, apparently without noticing this
pre-existing issue that I've known existed but haven't had the time to
address or advocate for.)

There is one item that I haven't touched in this patch that might also
affect whole-row expressions, and that is column renaming:
Technically, changes in names can affect a function's output (see e.g.
row_to_json), but I haven't yet found an immutable function that has a
`record` argument and exposes column names in output, nor have I
worked up the courage to wire up index rebuilds into
ExecRenameStmt/renameatt().

Kind regards,

Matthias van de Meent
Databricks (https://www.databricks.com)

Attachment Content-Type Size
whole-row-var-index-corruptions.sql application/sql 864 bytes
v1-0001-Track-whole-row-Var-dependencies-in-pg_depend.patch application/octet-stream 21.2 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Sami Imseih 2026-07-29 19:57:39 Re: Increase repalloc_array() usage in buffile.c
Previous Message Tristan Partin 2026-07-29 19:27:33 Increase repalloc_array() usage in buffile.c