From a4e85b6b6d648d248402ffbf6d8f1122a90ce50f Mon Sep 17 00:00:00 2001 From: Hayato Kuroda Date: Thu, 1 Oct 2026 16:59:00 +0900 Subject: [PATCH v1] Track the depth of scans of system tables --- contrib/test_decoding/expected/stream.out | 45 +++++++++++++++- contrib/test_decoding/sql/stream.sql | 34 +++++++++++- src/backend/access/index/genam.c | 66 ++++++++++++++--------- src/backend/replication/logical/logical.c | 3 +- src/include/access/genam.h | 2 + 5 files changed, 121 insertions(+), 29 deletions(-) diff --git a/contrib/test_decoding/expected/stream.out b/contrib/test_decoding/expected/stream.out index 0ec5c933610..baf02ac6297 100644 --- a/contrib/test_decoding/expected/stream.out +++ b/contrib/test_decoding/expected/stream.out @@ -134,6 +134,49 @@ SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, (1 row) RESET debug_logical_replication_streaming; +-- Create a table with a large constraint whose conbin value is stored out of +-- line. +DO $$ +DECLARE + large_literal text; +BEGIN + SELECT string_agg(md5(i::text), '') INTO large_literal FROM generate_series(1, 100) i; + + EXECUTE format( + 'CREATE TABLE nested_sys_scan_test ( + v text, + pad text, + CONSTRAINT a_big CHECK (v <> %L), + CONSTRAINT z_after CHECK (true))', + large_literal); +END +$$; +-- consume DDL +SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); + data +------ +(0 rows) + +-- Verify that detoasting catalog data in a nested system table scan does not +-- prevent the outer scan from fetching subsequent tuples. +SET debug_logical_replication_streaming = immediate; +BEGIN; +INSERT INTO nested_sys_scan_test VALUES ('y', repeat('x', 200)); +CHECKPOINT; +SELECT count(*) > 0 AS streamed FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'stream-changes', '1'); + streamed +---------- + t +(1 row) + +COMMIT; +RESET debug_logical_replication_streaming; +SELECT count(*) > 0 AS streamed FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'stream-changes', '1'); + streamed +---------- + t +(1 row) + -- bug #19616 -- -- An aborted top-level transaction that is discarded at eviction must not @@ -157,7 +200,7 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'inc COMMIT (3 rows) -DROP TABLE stream_test; +DROP TABLE stream_test, nested_sys_scan_test; SELECT pg_drop_replication_slot('regression_slot'); pg_drop_replication_slot -------------------------- diff --git a/contrib/test_decoding/sql/stream.sql b/contrib/test_decoding/sql/stream.sql index 5e45a8e9b64..4e5a176aab0 100644 --- a/contrib/test_decoding/sql/stream.sql +++ b/contrib/test_decoding/sql/stream.sql @@ -65,6 +65,38 @@ COMMIT; SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1', 'stream-changes', '1'); RESET debug_logical_replication_streaming; +-- Create a table with a large constraint whose conbin value is stored out of +-- line. +DO $$ +DECLARE + large_literal text; +BEGIN + SELECT string_agg(md5(i::text), '') INTO large_literal FROM generate_series(1, 100) i; + + EXECUTE format( + 'CREATE TABLE nested_sys_scan_test ( + v text, + pad text, + CONSTRAINT a_big CHECK (v <> %L), + CONSTRAINT z_after CHECK (true))', + large_literal); +END +$$; + +-- consume DDL +SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); + +-- Verify that detoasting catalog data in a nested system table scan does not +-- prevent the outer scan from fetching subsequent tuples. +SET debug_logical_replication_streaming = immediate; +BEGIN; +INSERT INTO nested_sys_scan_test VALUES ('y', repeat('x', 200)); +CHECKPOINT; +SELECT count(*) > 0 AS streamed FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'stream-changes', '1'); +COMMIT; +RESET debug_logical_replication_streaming; +SELECT count(*) > 0 AS streamed FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'stream-changes', '1'); + -- bug #19616 -- -- An aborted top-level transaction that is discarded at eviction must not @@ -82,5 +114,5 @@ ROLLBACK; INSERT INTO stream_test VALUES ('after-abort'); SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); -DROP TABLE stream_test; +DROP TABLE stream_test, nested_sys_scan_test; SELECT pg_drop_replication_slot('regression_slot'); diff --git a/src/backend/access/index/genam.c b/src/backend/access/index/genam.c index 0cb27af1310..998c1b581d8 100644 --- a/src/backend/access/index/genam.c +++ b/src/backend/access/index/genam.c @@ -37,6 +37,42 @@ #include "utils/ruleutils.h" #include "utils/snapmgr.h" +/* Number of nested systable scans while decoding in-progress transactions. */ +static int bsysscan_depth = 0; + +/* + * System table scans can be nested while CheckXidAlive is valid. A simple + * boolean is insufficient because ending an inner scan must not clear bsysscan + * while an outer scan is still active. Track the nesting depth so bsysscan is + * cleared only when the outermost scan ends. + */ +static inline void +IncrementSysScanDepth(void) +{ + if (!TransactionIdIsValid(CheckXidAlive)) + return; + + bsysscan_depth++; + bsysscan = true; +} + +static inline void +DecrementSysScanDepth(void) +{ + if (!TransactionIdIsValid(CheckXidAlive)) + return; + + Assert(bsysscan_depth > 0); + bsysscan_depth--; + bsysscan = (bsysscan_depth > 0); +} + +void +ResetSysScanDepth(void) +{ + bsysscan_depth = 0; + bsysscan = false; +} /* ---------------------------------------------------------------- * general access method routines @@ -468,13 +504,7 @@ systable_beginscan(Relation heapRelation, sysscan->iscan = NULL; } - /* - * If CheckXidAlive is set then set a flag to indicate that system table - * scan is in-progress. See detailed comments in xact.c where these - * variables are declared. - */ - if (TransactionIdIsValid(CheckXidAlive)) - bsysscan = true; + IncrementSysScanDepth(); return sysscan; } @@ -619,12 +649,7 @@ systable_endscan(SysScanDesc sysscan) if (sysscan->snapshot) UnregisterSnapshot(sysscan->snapshot); - /* - * Reset the bsysscan flag at the end of the systable scan. See detailed - * comments in xact.c where these variables are declared. - */ - if (TransactionIdIsValid(CheckXidAlive)) - bsysscan = false; + DecrementSysScanDepth(); pfree(sysscan); } @@ -714,13 +739,7 @@ systable_beginscan_ordered(Relation heapRelation, pfree(idxkey); - /* - * If CheckXidAlive is set then set a flag to indicate that system table - * scan is in-progress. See detailed comments in xact.c where these - * variables are declared. - */ - if (TransactionIdIsValid(CheckXidAlive)) - bsysscan = true; + IncrementSysScanDepth(); return sysscan; } @@ -767,12 +786,7 @@ systable_endscan_ordered(SysScanDesc sysscan) if (sysscan->snapshot) UnregisterSnapshot(sysscan->snapshot); - /* - * Reset the bsysscan flag at the end of the systable scan. See detailed - * comments in xact.c where these variables are declared. - */ - if (TransactionIdIsValid(CheckXidAlive)) - bsysscan = false; + DecrementSysScanDepth(); pfree(sysscan); } diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c index 4d3a39f4cd7..3eb88496edc 100644 --- a/src/backend/replication/logical/logical.c +++ b/src/backend/replication/logical/logical.c @@ -28,6 +28,7 @@ #include "postgres.h" +#include "access/genam.h" #include "access/xact.h" #include "access/xlog_internal.h" #include "access/xlogutils.h" @@ -2013,7 +2014,7 @@ void ResetLogicalStreamingState(void) { CheckXidAlive = InvalidTransactionId; - bsysscan = false; + ResetSysScanDepth(); } /* diff --git a/src/include/access/genam.h b/src/include/access/genam.h index 5b2ab181b5f..0bf04b908de 100644 --- a/src/include/access/genam.h +++ b/src/include/access/genam.h @@ -275,4 +275,6 @@ extern void systable_inplace_update_begin(Relation relation, extern void systable_inplace_update_finish(void *state, HeapTuple tuple); extern void systable_inplace_update_cancel(void *state); +extern void ResetSysScanDepth(void); + #endif /* GENAM_H */ -- 2.52.0