Avoid logging dropped TOAST values with REPLICA IDENTITY FULL

From: Rui Zhao <zhaorui126(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Cc: Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>, Andres Freund <andres(at)anarazel(dot)de>
Subject: Avoid logging dropped TOAST values with REPLICA IDENTITY FULL
Date: 2026-12-04 17:28:28
Message-ID: CAHWVJhE8xMz5bK_5aUZCnan8a-Az4jnrYCrGTYo4pzDmu5HY6A@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I recently ran into a case where deleting old rows by date range
filled the disk with WAL. The table used REPLICA IDENTITY FULL, and a
large TOASTed column had just been dropped. What surprised me was that
the DELETE still wrote that column's value to WAL for each row it
removed.

I reproduced this on master (d637007e62a) with wal_level = logical.
The old rows still held TOAST pointers for the dropped column. To log
the full old row, PostgreSQL followed the pointer and fetched the
value from the TOAST table.

CREATE TABLE w (id int PRIMARY KEY, gone text, kept text,
created_at date);
ALTER TABLE w REPLICA IDENTITY FULL;
ALTER TABLE w ALTER COLUMN gone SET STORAGE EXTERNAL;
INSERT INTO w
SELECT i, repeat('x', 64000), 'keep',
date '2025-01-01' + (i - 1) / 1000
FROM generate_series(1, 21000) AS g(i);
ALTER TABLE w DROP COLUMN gone;
DELETE FROM w
WHERE created_at >= date '2025-01-01'
AND created_at < date '2025-01-21';
UPDATE w SET kept = 'next';
UPDATE w SET kept = 'last';

The DELETE removed 20,000 rows. I then updated kept twice on the
remaining 1,000 rows. On both builds I set full_page_writes = off to
keep full-page images out of the measurements. I ran CHECKPOINT before
each measured statement. EXPLAIN (ANALYZE, WAL) reported these WAL
sizes in bytes.

master patched
DELETE 20,000 rows by date 1,324,362,040 44,222,040
UPDATE remaining 1,000 rows 66,381,558 2,374,558
UPDATE same rows again 230,636 230,636

The DELETE wrote 1.19 GiB less WAL with the patch. pg_waldump showed
that the main table's DELETE records shrank by 64,007 bytes per row
on average. With physical standbys, that is also less WAL to stream.
The first UPDATE left NULL in the dropped column, so the second
UPDATE saved nothing.

I fixed this in ExtractReplicaIdentity(), where we build the old row
for REPLICA IDENTITY FULL. If a dropped column still has a TOAST
pointer, we now write NULL for that column in WAL without fetching
the value. The regression test in the attached patch drops a column
with a 100,000-byte value and checks how much WAL the DELETE writes.

One thing I wanted to check was whether writing NULL here would affect
logical decoding. ReorderBufferToastReplace() skips dropped columns
when rebuilding TOAST values for a new tuple. For the old row of a
DELETE, pgoutput skips dropped columns in
logicalrep_should_publish_column(), and test_decoding does the same
in tuple_to_stringinfo().

I tried this on master and with the patch using a table with just id,
gone and kept. I deleted one row, dropped gone, then deleted another
row before reading either replication slot. For the DELETE before the
DROP, pgoutput and test_decoding both showed id, gone and kept,
including all 64,000 bytes of gone. For the DELETE after the DROP,
both showed only id and kept.

Regards,
Rui

Attachment Content-Type Size
v1-0001-Avoid-logging-dropped-TOAST-values-with-REPLICA-IDENTITY-FULL.patch application/octet-stream 5.8 KB

Browse pgsql-hackers by date

  From Date Subject
Previous Message Rafia Sabih 2026-12-04 17:12:22 Re: Semi-join related bug in postgres_fdw