From ff6ba5df9e5fbf2508f6e931b2e63c5801a9e966 Mon Sep 17 00:00:00 2001 From: Hayato Kuroda Date: Thu, 1 Oct 2026 16:59:00 +0900 Subject: [PATCH v4-PG17] Track the depth of scans of system tables --- contrib/test_decoding/expected/stream.out | 44 ++++++++++++++- contrib/test_decoding/sql/stream.sql | 33 +++++++++++- src/backend/access/index/genam.c | 66 ++++++++++++++--------- src/backend/replication/logical/logical.c | 3 +- src/include/access/genam.h | 2 + 5 files changed, 119 insertions(+), 29 deletions(-) diff --git a/contrib/test_decoding/expected/stream.out b/contrib/test_decoding/expected/stream.out index a76f77601e2..ec4d48c75b0 100644 --- a/contrib/test_decoding/expected/stream.out +++ b/contrib/test_decoding/expected/stream.out @@ -128,7 +128,49 @@ SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 5 (1 row) -DROP TABLE stream_test; +-- 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 ( + checked_value text, + CONSTRAINT a_toasted_check CHECK (checked_value <> %L), + CONSTRAINT z_following_check 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 ('valid'); +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) + +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 7f43f0c2ab7..a64924a6c72 100644 --- a/contrib/test_decoding/sql/stream.sql +++ b/contrib/test_decoding/sql/stream.sql @@ -59,5 +59,36 @@ ROLLBACK TO s1; COMMIT; SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1', 'stream-changes', '1'); -DROP TABLE stream_test; +-- 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 ( + checked_value text, + CONSTRAINT a_toasted_check CHECK (checked_value <> %L), + CONSTRAINT z_following_check 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 ('valid'); +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'); + +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 b123acc5a60..42eb8b0dd88 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 @@ -459,13 +495,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; } @@ -614,12 +644,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); } @@ -702,13 +727,7 @@ systable_beginscan_ordered(Relation heapRelation, index_rescan(sysscan->iscan, key, nkeys, NULL, 0); sysscan->scan = 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; } @@ -755,12 +774,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 f79557c7261..b5743674c8f 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" @@ -2040,7 +2041,7 @@ void ResetLogicalStreamingState(void) { CheckXidAlive = InvalidTransactionId; - bsysscan = false; + ResetSysScanDepth(); } /* diff --git a/src/include/access/genam.h b/src/include/access/genam.h index c25f5d11b53..b902b0d1400 100644 --- a/src/include/access/genam.h +++ b/src/include/access/genam.h @@ -243,4 +243,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