Skip TOAST fetches for dropped columns when forming a row value

From: Rui Zhao <zhaorui126(at)gmail(dot)com>
To: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Skip TOAST fetches for dropped columns when forming a row value
Date: 2026-12-02 11:05:53
Message-ID: CAHWVJhHfqnmCbO3jdwoWBssuWzdZsD2Yvj2kAa4srDmS59rw5g@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

While testing CLUSTER for BUG #19519, I found that PostgreSQL still
fetches a dropped column's TOAST value when forming a row value.
A plain SELECT shows this on unpatched master (93f5e9d405f).

CREATE TEMP TABLE pp (f1 text);
INSERT INTO pp VALUES (repeat('abcdefghijkl', 100000));
SELECT pg_column_size(pp) FROM pp;
-- 13783

ALTER TABLE pp DROP COLUMN f1;
SELECT pp::text, pg_column_size(pp) FROM pp;
-- () | 13783

Both SELECTs read the same row. Before the DROP, the 13783 bytes
include f1, as expected. After the DROP, pp has no columns and prints
as (), yet the SELECT still copies the old value of f1 into the
row value. A row inserted after the DROP gives the expected 24 bytes.

INSERT INTO pp DEFAULT VALUES
RETURNING pp::text, pg_column_size(pp);
-- () | 24

The patch marks dropped fields NULL in toast_build_flattened_tuple().
This skips the TOAST fetch and tells heap_form_tuple() to omit the
field's bytes. After the DROP, the original row also gives () | 24.

Regards,
Rui

Attachment Content-Type Size
v1-0001-Skip-TOAST-fetches-for-dropped-columns-when-forming-a-row-value.patch application/octet-stream 3.6 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Nikhil Sontakke 2026-12-02 11:14:34 Streamed transactions lose replication origin when first change is not a row change
Previous Message Chengpeng Yan 2026-12-02 10:43:23 Re: memory leak in bounded sorts