From 487cb5c6e82ec41055a77132a2a83b0c6f255167 Mon Sep 17 00:00:00 2001 From: Rui Zhao Date: Tue, 1 Dec 2026 23:16:35 +0800 Subject: [PATCH v1] Skip TOAST fetches for dropped columns when forming a row value A heap tuple written before DROP COLUMN can still contain the column's TOAST pointer. When forming a row value, toast_build_flattened_tuple() follows that pointer and copies the dropped value. The fetch can fail if its chunks have already been removed. Mark dropped columns NULL so we skip the TOAST fetch and leave their bytes out of the row value. --- src/backend/access/heap/heaptoast.c | 16 +++++++--------- src/test/regress/expected/rowtypes.out | 9 +++++++++ src/test/regress/sql/rowtypes.sql | 5 +++++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/backend/access/heap/heaptoast.c b/src/backend/access/heap/heaptoast.c index e329662b683..39bb0c5d8b1 100644 --- a/src/backend/access/heap/heaptoast.c +++ b/src/backend/access/heap/heaptoast.c @@ -584,22 +584,20 @@ toast_build_flattened_tuple(TupleDesc tupleDesc, int num_to_free; int i; Datum new_values[MaxTupleAttributeNumber]; + bool new_isnull[MaxTupleAttributeNumber]; void *freeable_values[MaxTupleAttributeNumber]; - /* - * We can pass the caller's isnull array directly to heap_form_tuple, but - * we potentially need to modify the values array. - */ Assert(numAttrs <= MaxTupleAttributeNumber); memcpy(new_values, values, numAttrs * sizeof(Datum)); num_to_free = 0; for (i = 0; i < numAttrs; i++) { - /* - * Look at non-null varlena attributes - */ - if (!isnull[i] && TupleDescCompactAttr(tupleDesc, i)->attlen == -1) + CompactAttribute *att = TupleDescCompactAttr(tupleDesc, i); + + /* Don't fetch an old TOAST value for a dropped column. */ + new_isnull[i] = isnull[i] || att->attisdropped; + if (!new_isnull[i] && att->attlen == -1) { varlena *new_value; @@ -616,7 +614,7 @@ toast_build_flattened_tuple(TupleDesc tupleDesc, /* * Form the reconfigured tuple. */ - new_tuple = heap_form_tuple(tupleDesc, new_values, isnull); + new_tuple = heap_form_tuple(tupleDesc, new_values, new_isnull); /* * Free allocated temp values diff --git a/src/test/regress/expected/rowtypes.out b/src/test/regress/expected/rowtypes.out index c402160d488..bb605c5183e 100644 --- a/src/test/regress/expected/rowtypes.out +++ b/src/test/regress/expected/rowtypes.out @@ -185,6 +185,15 @@ select (fn).first, substr((fn).last, 1, 20), length((fn).last) from people; Jack | abcdefghijklabcdefgh | 1200000 (2 rows) +-- After DROP COLUMN, old and new rows should form values of the same size. +alter table pp drop column f1; +insert into pp default values; +select count(distinct pg_column_size(pp)) = 1 as same_size from pp; + same_size +----------- + t +(1 row) + -- Test row comparison semantics. Prior to PG 8.2 we did this in a totally -- non-spec-compliant way. select ROW(1,2) < ROW(1,3) as true; diff --git a/src/test/regress/sql/rowtypes.sql b/src/test/regress/sql/rowtypes.sql index 5aef9f5a3c3..04d566b7591 100644 --- a/src/test/regress/sql/rowtypes.sql +++ b/src/test/regress/sql/rowtypes.sql @@ -92,6 +92,11 @@ update people set fn.first = 'Jack'; select (fn).first, substr((fn).last, 1, 20), length((fn).last) from people; +-- After DROP COLUMN, old and new rows should form values of the same size. +alter table pp drop column f1; +insert into pp default values; +select count(distinct pg_column_size(pp)) = 1 as same_size from pp; + -- Test row comparison semantics. Prior to PG 8.2 we did this in a totally -- non-spec-compliant way. -- 2.43.7