From 54337f311316acea0ec02fae1890b45924e3e46b Mon Sep 17 00:00:00 2001 From: Rui Zhao Date: Thu, 3 Dec 2026 23:12:27 +0800 Subject: [PATCH v1] Avoid logging dropped TOAST values with REPLICA IDENTITY FULL UPDATE and DELETE can encounter an external value left by DROP COLUMN. When logging a full replica identity, mark that field NULL instead of fetching its bytes into the WAL record. Logical replication omits dropped columns when publishing the old row. Keep the existing flattening behavior for other callers and for live columns. Extend the TOAST decoding test to check the WAL from DELETE. --- contrib/test_decoding/expected/toast.out | 16 +++++++++++++++ contrib/test_decoding/sql/toast.sql | 17 ++++++++++++++++ src/backend/access/heap/heapam.c | 4 ++-- src/backend/access/heap/heaptoast.c | 26 ++++++++++++++++++++---- src/include/access/heaptoast.h | 2 ++ 5 files changed, 59 insertions(+), 6 deletions(-) diff --git a/contrib/test_decoding/expected/toast.out b/contrib/test_decoding/expected/toast.out index d2c49796b3c..b6b503ff1ae 100644 --- a/contrib/test_decoding/expected/toast.out +++ b/contrib/test_decoding/expected/toast.out @@ -324,6 +324,22 @@ SELECT regexp_replace(data, '^(.{100}).*(.{100})$', '\1..\2') FROM pg_logical_sl COMMIT (3 rows) +-- The dropped key alone would add 100000 bytes to the old row in WAL. +BEGIN; +ALTER TABLE toasted_several DROP COLUMN toasted_key; +DO $$ +DECLARE + plan json; +BEGIN + EXECUTE 'EXPLAIN (ANALYZE, WAL, FORMAT JSON) DELETE FROM toasted_several' + INTO plan; + IF (plan->0->'Plan'->>'WAL Bytes')::bigint - + (plan->0->'Plan'->>'WAL FPI Bytes')::bigint >= 100000 THEN + RAISE EXCEPTION 'DELETE logged the dropped value'; + END IF; +END +$$; +ROLLBACK; -- test update of a toasted key without changing it UPDATE toasted_several SET toasted_col1 = toasted_key; UPDATE toasted_several SET toasted_col2 = toasted_col1; diff --git a/contrib/test_decoding/sql/toast.sql b/contrib/test_decoding/sql/toast.sql index 8e7b47aef9d..38de971f3d2 100644 --- a/contrib/test_decoding/sql/toast.sql +++ b/contrib/test_decoding/sql/toast.sql @@ -289,6 +289,23 @@ SELECT pg_column_size(toasted_key) > 2^16 FROM toasted_several; SELECT regexp_replace(data, '^(.{100}).*(.{100})$', '\1..\2') FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); +-- The dropped key alone would add 100000 bytes to the old row in WAL. +BEGIN; +ALTER TABLE toasted_several DROP COLUMN toasted_key; +DO $$ +DECLARE + plan json; +BEGIN + EXECUTE 'EXPLAIN (ANALYZE, WAL, FORMAT JSON) DELETE FROM toasted_several' + INTO plan; + IF (plan->0->'Plan'->>'WAL Bytes')::bigint - + (plan->0->'Plan'->>'WAL FPI Bytes')::bigint >= 100000 THEN + RAISE EXCEPTION 'DELETE logged the dropped value'; + END IF; +END +$$; +ROLLBACK; + -- test update of a toasted key without changing it UPDATE toasted_several SET toasted_col1 = toasted_key; UPDATE toasted_several SET toasted_col2 = toasted_col1; diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index 67073fff6b7..cd61f8e0ac8 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -9393,12 +9393,12 @@ ExtractReplicaIdentity(Relation relation, HeapTuple tp, bool key_required, { /* * When logging the entire old tuple, it very well could contain - * toasted columns. If so, force them to be inlined. + * toasted columns. Inline those that are not dropped. */ if (HeapTupleHasExternal(tp)) { *copy = true; - tp = toast_flatten_tuple(tp, desc); + tp = toast_flatten_tuple_extended(tp, desc, true); /* see below */ Assert(!HeapTupleHeaderIsDatum(tp->t_data)); diff --git a/src/backend/access/heap/heaptoast.c b/src/backend/access/heap/heaptoast.c index e329662b683..6e924c1d567 100644 --- a/src/backend/access/heap/heaptoast.c +++ b/src/backend/access/heap/heaptoast.c @@ -353,6 +353,14 @@ heap_toast_insert_or_update(Relation rel, HeapTuple newtup, HeapTuple oldtup, */ HeapTuple toast_flatten_tuple(HeapTuple tup, TupleDesc tupleDesc) +{ + return toast_flatten_tuple_extended(tup, tupleDesc, false); +} + +/* A full replica identity can omit external values of dropped columns. */ +HeapTuple +toast_flatten_tuple_extended(HeapTuple tup, TupleDesc tupleDesc, + bool omit_dropped_external) { HeapTuple new_tuple; int numAttrs = tupleDesc->natts; @@ -371,16 +379,26 @@ toast_flatten_tuple(HeapTuple tup, TupleDesc tupleDesc) for (i = 0; i < numAttrs; i++) { - /* - * Look at non-null varlena attributes - */ - if (!toast_isnull[i] && TupleDescCompactAttr(tupleDesc, i)->attlen == -1) + CompactAttribute *att; + + /* Look at non-null varlena attributes. */ + if (toast_isnull[i]) + continue; + + att = TupleDescCompactAttr(tupleDesc, i); + if (att->attlen == -1) { varlena *new_value; new_value = (varlena *) DatumGetPointer(toast_values[i]); if (VARATT_IS_EXTERNAL(new_value)) { + if (omit_dropped_external && att->attisdropped) + { + toast_isnull[i] = true; + continue; + } + new_value = detoast_external_attr(new_value); toast_values[i] = PointerGetDatum(new_value); toast_free[i] = true; diff --git a/src/include/access/heaptoast.h b/src/include/access/heaptoast.h index 4c3fda8a5f9..1983e66dcad 100644 --- a/src/include/access/heaptoast.h +++ b/src/include/access/heaptoast.h @@ -128,6 +128,8 @@ extern void heap_toast_delete(Relation rel, HeapTuple oldtup, * ---------- */ extern HeapTuple toast_flatten_tuple(HeapTuple tup, TupleDesc tupleDesc); +extern HeapTuple toast_flatten_tuple_extended(HeapTuple tup, TupleDesc tupleDesc, + bool omit_dropped_external); /* ---------- * toast_flatten_tuple_to_datum - -- 2.43.7