pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedStreaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641)
18+ messages / 6 participants
[nested] [flat]
* Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641)
@ 2026-09-30 13:59 Jiří Kavalík <jiri.kavalik@comgate.cz>
0 siblings, 0 replies; 18+ messages in thread
From: Jiří Kavalík @ 2026-09-30 13:59 UTC (permalink / raw)
To: pgsql-bugs@lists.postgresql.org
Version and platform
PostgreSQL 18.3 (Debian 18.3-1.pgdg13+1) on x86_64-pc-linux-gnu, compiled
by gcc (Debian 14.2.0-19) 14.2.0, 64-bit Official postgres:18 Docker image:
Debian GNU/Linux 13 (trixie), glibc 2.41, Linux 6.17 x86_64. Seen in
production on the same version with pgoutput / built-in logical replication
(subscriptions with streaming = parallel). In production, the table with
the TOASTed conbin (followed by 34 more pg_constraint rows) is modified by
the streamed transaction but is not a member of the subscribed publication.
The same holds in the reproducer: with pgoutput and a publication
containing only "filler", decoding still fails, because the relcache entry
is built for every changed relation.
Non-default settings:
wal_level = logical
logical_decoding_work_mem = 64kB
Steps to reproduce
The attached repro.sh runs these steps with psql against an empty database:
CREATE TABLE t (id int PRIMARY KEY, v text,
CONSTRAINT a_big CHECK (v <> ALL (ARRAY[ <400 md5()
literals> ])));
-- the conbin of a_big is stored out of line:
-- pg_column_toast_chunk_id(conbin) IS NOT NULL
CREATE TABLE filler (id int, pad text);
SELECT pg_create_logical_replication_slot('repro', 'test_decoding');
-- session 1, kept open:
BEGIN;
INSERT INTO t VALUES (1, 'y');
INSERT INTO filler SELECT i, repeat('x', 200) FROM generate_series(1, 10000) i;
SELECT pg_sleep(10);
COMMIT;
-- session 2, a new backend, while session 1 is in pg_sleep:
\set VERBOSITY verbose
SELECT count(*) FROM pg_logical_slot_peek_changes('repro', NULL, NULL,
'stream-changes', '1');
-- after session 1 has committed:
SELECT count(*) FROM pg_logical_slot_peek_changes('repro', NULL, NULL,
'stream-changes', '1');
Actual output
Session 2, while session 1 is still open (3 out of 3 runs on a fresh
container):
ERROR: XX000: unexpected table_index_fetch_tuple call during logical decoding
LOCATION: table_index_fetch_tuple, tableam.h:1218
The same peek after session 1 has committed: 10106 rows, no error.
The same error occurs with pgoutput (proto_version '4', streaming
'parallel').
Expected output
The in-progress transaction is streamed without error, as it is when the
relation has no TOASTed catalog data.
Variations (same procedure, different table definitions)
pg_constraint rows of the table, in conname order Result
a_big (TOASTed), b_small, t_id_not_null, t_pkey ERROR
a_big (TOASTed), t_only_id_not_null, t_only_pkey ERROR
a_small, t_last_id_not_null, t_last_pkey, z_big (TOASTed) no error
no TOASTed conbin no error
The error occurs only when the TOASTed row is not the last one the scan
returns. Because NOT NULL constraints are pg_constraint rows in PG18, most
tables have rows after any given CHECK constraint.
Backtrace
Captured with backtrace_functions = 'table_index_fetch_tuple'. The server
has no debug symbols, so static functions show as offsets:
(+0xebb28)
index_getnext_slot+0x45
systable_getnext+0x38
(+0x66feae)
RelationIdGetRelation+0x8d
(+0x4a304e)
(+0x4a42b5)
ReorderBufferQueueChange+0x301
heap_decode+0x1df
LogicalDecodingProcessRecord+0x76
(+0x49afa5)
pg_logical_slot_peek_changes+0x11
Possible cause (a suggestion only)
bsysscan is a plain bool, set and cleared in
systable_{begin,end}scan[_ordered]. Since 8175a7d11 (the fix for BUG
#18641), the TOAST fetch that detoasts conbin inside the outer
pg_constraint scan clears bsysscan on its end-scan. The outer scan's next
systable_getnext() then reaches table_index_fetch_tuple() while
CheckXidAlive is still valid. That would also explain why a TOASTed row in
the last position does not fail. Restoring the previous value of bsysscan,
or counting nesting depth, instead of clearing it might fix this.
Attachments:
repro.sh (application/x-shellscript, 1.9K) download
^ permalink raw reply [nested|flat] 18+ messages in thread
* Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641)
@ 2026-10-02 07:33 Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-02 08:29 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Jiří Kavalík <jiri.kavalik@comgate.cz>
2026-10-07 10:08 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Zhijie Hou <houzhijie22@gmail.com>
0 siblings, 2 replies; 18+ messages in thread
From: Hayato Kuroda (Fujitsu) @ 2026-10-02 07:33 UTC (permalink / raw)
To: 'jiri.kavalik@comgate.cz' <jiri.kavalik@comgate.cz>; +Cc: pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Hi,
This is the reply for [1]. My mailer could not receive the original post due to
the company's policy, so I will put as the normal post. Sorry for inconvenience.
I confirmed this could happen on PG18, PG17 and PG14. Not tested, but expecting
for PG15 and 166 as well. This could not happen on PG19/HEAD because the
elog(ERROR) was removed by 87f7b824f20, but possible for all branches.
I think your analysis is correct. bsysscan tries to indicate that whether we're
scanning a system table, which was turned on at systable_beginscan* and turned
off at systable_endscan*. But if the systable scan is nested (i.e., pg_constraint.conbin),
the flag can be wrong reset. In PG18- the state is checked for every getnextslot,
which raised the ERROR. In PG19+ the check is unified at the beginning thus the
ERROR does not happen, but I guess the flag can be still wrong.
One idea is to track the depth of scans. Attached patch is for PG18, and I tried not
to modify the header as much as possible. It also had a test code based on your
reproducer. Can you see it's same as your expectation?
[1]: https://www.postgresql.org/message-id/CAF7a2M-OF%2BTjYUBvkSV2oyZNYuPm%3DEqTApcMDOC%2BaevBFPuWrg%40ma...
Best regards,
Hayato Kuroda
FUJITSU LIMITED
Attachments:
v1-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 8.3K) download
From a4e85b6b6d648d248402ffbf6d8f1122a90ce50f Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
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
^ permalink raw reply [nested|flat] 18+ messages in thread
* Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641)
2026-10-02 07:33 Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
@ 2026-10-02 08:29 ` Jiří Kavalík <jiri.kavalik@comgate.cz>
2026-10-06 09:21 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
1 sibling, 1 reply; 18+ messages in thread
From: Jiří Kavalík @ 2026-10-02 08:29 UTC (permalink / raw)
To: Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; +Cc: pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Hi Kuroda-san,
Thank you for the quick analysis and the patch.
> Can you see it's same as your expectation?
Yes. I tested v1 on REL_18_STABLE (37bbf5bba0, 18.6 plus later commits),
with
two builds from the same tree differing only by the patch, both configured
with --enable-debug --enable-cassert.
In each case below, a fresh backend peeks at a slot while a transaction that
first inserts one row into the test table, then 10000 rows into a filler
table, is still open (logical_decoding_work_mem = 64kB, CHECKPOINT before
the
peek):
1. TOASTed conbin first, three more constraints after it:
- test_decoding: unpatched ERROR, patched OK
- pgoutput, table published: unpatched ERROR, patched OK
- pgoutput, table NOT published: unpatched ERROR, patched OK
2. TOASTed conbin first, only the NOT NULL and PK constraints after it:
- test_decoding: unpatched ERROR, patched OK
- pgoutput, table published: unpatched ERROR, patched OK
- pgoutput, table NOT published: unpatched ERROR, patched OK
3. TOASTed conbin last: OK on both builds, both plugins.
4. No TOASTed conbin: OK on both builds, both plugins.
"OK" means the whole transaction was decoded (10098 rows from test_decoding,
10100 from pgoutput). There were no assertion failures in either server log.
Your new stream.sql test fails on the unpatched build with the same ERROR
and
passes with the patch. With the patch, contrib/test_decoding "make check"
(19 regression + 13 isolation tests) and the core "make check" (231 tests)
pass.
The "table NOT published" rows may be worth noting: the relation that hit
the
error in our production case is not in the subscribed publication, it is
only
modified by the streamed transaction. The relcache entry is still built, so
the bug is reachable for any table the transaction touches.
One question while reading the patch, not a problem I could trigger: the
depth
is incremented in systable_beginscan* and decremented in systable_endscan*
only
if CheckXidAlive is valid, and that is evaluated separately at each end. If
CheckXidAlive changed while a scan was open, the counter would be off by
one.
I could not find a path where that happens. Error paths look fine, since
AbortTransaction/AbortSubTransaction call ResetLogicalStreamingState(). If a
SysScanDesc field is acceptable despite the header concern, remembering in
the
scan whether it was counted would make the pairing explicit.
Regards,
Jiří Kavalík
pá 2. 10. 2026 v 9:33 odesílatel Hayato Kuroda (Fujitsu) <
kuroda.hayato@fujitsu.com> napsal:
> Hi,
>
> This is the reply for [1]. My mailer could not receive the original post
> due to
> the company's policy, so I will put as the normal post. Sorry for
> inconvenience.
>
> I confirmed this could happen on PG18, PG17 and PG14. Not tested, but
> expecting
> for PG15 and 166 as well. This could not happen on PG19/HEAD because the
> elog(ERROR) was removed by 87f7b824f20, but possible for all branches.
>
> I think your analysis is correct. bsysscan tries to indicate that whether
> we're
> scanning a system table, which was turned on at systable_beginscan* and
> turned
> off at systable_endscan*. But if the systable scan is nested (i.e.,
> pg_constraint.conbin),
> the flag can be wrong reset. In PG18- the state is checked for every
> getnextslot,
> which raised the ERROR. In PG19+ the check is unified at the beginning
> thus the
> ERROR does not happen, but I guess the flag can be still wrong.
>
> One idea is to track the depth of scans. Attached patch is for PG18, and I
> tried not
> to modify the header as much as possible. It also had a test code based on
> your
> reproducer. Can you see it's same as your expectation?
>
> [1]:
> https://www.postgresql.org/message-id/CAF7a2M-OF%2BTjYUBvkSV2oyZNYuPm%3DEqTApcMDOC%2BaevBFPuWrg%40ma...
>
> Best regards,
> Hayato Kuroda
> FUJITSU LIMITED
>
>
--
S pozdravem
Jiří Kavalík
jiri.kavalik@comgate.cz
Comgate a.s.
Gočárova třída 1754/48b, 500 02 Hradec Králové
www.comgate.cz
^ permalink raw reply [nested|flat] 18+ messages in thread
* RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641)
2026-10-02 07:33 Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-02 08:29 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Jiří Kavalík <jiri.kavalik@comgate.cz>
@ 2026-10-06 09:21 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-06 09:33 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Jiří Kavalík <jiri.kavalik@comgate.cz>
0 siblings, 1 reply; 18+ messages in thread
From: Hayato Kuroda (Fujitsu) @ 2026-10-06 09:21 UTC (permalink / raw)
To: 'Jiří Kavalík' <jiri.kavalik@comgate.cz>; +Cc: pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Hi Jiří,
Thanks for the test. I understood that no issues were found for now.
> One question while reading the patch, not a problem I could trigger: the depth
> is incremented in systable_beginscan* and decremented in systable_endscan* only
> if CheckXidAlive is valid, and that is evaluated separately at each end. If
> CheckXidAlive changed while a scan was open, the counter would be off by one.
> I could not find a path where that happens. Error paths look fine, since
> AbortTransaction/AbortSubTransaction call ResetLogicalStreamingState(). If a
> SysScanDesc field is acceptable despite the header concern, remembering in the
> scan whether it was counted would make the pairing explicit.
Hmm, I'm not excited to modify the exposed data structure yet, unless there is a
real issue. Per my analysis, SysScanDescData only contains pointers (8 bytes),
it does not have any paddings. This meant we need to modify a size of the data
structure, it might cause failures somewhere.
Also, the existing code has the same possibility while turning on/off bsysscan,
right? So I feel it's already accepted.
(Of course, we must fix if it causes a real failure)
Best regards,
Hayato Kuroda
FUJITSU LIMITED
^ permalink raw reply [nested|flat] 18+ messages in thread
* Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641)
2026-10-02 07:33 Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-02 08:29 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Jiří Kavalík <jiri.kavalik@comgate.cz>
2026-10-06 09:21 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
@ 2026-10-06 09:33 ` Jiří Kavalík <jiri.kavalik@comgate.cz>
0 siblings, 0 replies; 18+ messages in thread
From: Jiří Kavalík @ 2026-10-06 09:33 UTC (permalink / raw)
To: Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; +Cc: pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Hi Kuroda-san,
> Hmm, I'm not excited to modify the exposed data structure yet, unless
there is a
> real issue.
Agreed. It was only a readability suggestion, not a fix for a real failure.
I checked again. CheckXidAlive is set only at the start of each change in
ReorderBufferProcessTXN(), before that change's catalog access. It is
cleared
only after the change loop, or on abort, where ResetLogicalStreamingState()
also resets the new depth counter. So it does not change while a scan is
open,
except on the error path, which v1 already handles. v1 looks complete to me.
Thanks for the patch.
Regards,
Jiří Kavalík
út 6. 10. 2026 v 11:22 odesílatel Hayato Kuroda (Fujitsu) <
kuroda.hayato@fujitsu.com> napsal:
> Hi Jiří,
>
> Thanks for the test. I understood that no issues were found for now.
>
> > One question while reading the patch, not a problem I could trigger: the
> depth
> > is incremented in systable_beginscan* and decremented in
> systable_endscan* only
> > if CheckXidAlive is valid, and that is evaluated separately at each end.
> If
> > CheckXidAlive changed while a scan was open, the counter would be off by
> one.
> > I could not find a path where that happens. Error paths look fine, since
> > AbortTransaction/AbortSubTransaction call ResetLogicalStreamingState().
> If a
> > SysScanDesc field is acceptable despite the header concern, remembering
> in the
> > scan whether it was counted would make the pairing explicit.
>
> Hmm, I'm not excited to modify the exposed data structure yet, unless
> there is a
> real issue. Per my analysis, SysScanDescData only contains pointers (8
> bytes),
> it does not have any paddings. This meant we need to modify a size of the
> data
> structure, it might cause failures somewhere.
> Also, the existing code has the same possibility while turning on/off
> bsysscan,
> right? So I feel it's already accepted.
> (Of course, we must fix if it causes a real failure)
>
> Best regards,
> Hayato Kuroda
> FUJITSU LIMITED
>
>
--
S pozdravem
Jiří Kavalík
jiri.kavalik@comgate.cz
Comgate a.s.
Gočárova třída 1754/48b, 500 02 Hradec Králové
www.comgate.cz
^ permalink raw reply [nested|flat] 18+ messages in thread
* Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641)
2026-10-02 07:33 Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
@ 2026-10-07 10:08 ` Zhijie Hou <houzhijie22@gmail.com>
2026-10-08 02:36 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
1 sibling, 1 reply; 18+ messages in thread
From: Zhijie Hou @ 2026-10-07 10:08 UTC (permalink / raw)
To: Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; +Cc: jiri.kavalik@comgate.cz <jiri.kavalik@comgate.cz>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Hi,
On Fri, Oct 2, 2026 at 3:33 PM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:
>
> Hi,
>
> This is the reply for [1]. My mailer could not receive the original post due to
> the company's policy, so I will put as the normal post. Sorry for inconvenience.
>
> I confirmed this could happen on PG18, PG17 and PG14. Not tested, but expecting
> for PG15 and 166 as well. This could not happen on PG19/HEAD because the
> elog(ERROR) was removed by 87f7b824f20, but possible for all branches.
>
> I think your analysis is correct. bsysscan tries to indicate that whether we're
> scanning a system table, which was turned on at systable_beginscan* and turned
> off at systable_endscan*. But if the systable scan is nested (i.e., pg_constraint.conbin),
> the flag can be wrong reset. In PG18- the state is checked for every getnextslot,
> which raised the ERROR. In PG19+ the check is unified at the beginning thus the
> ERROR does not happen, but I guess the flag can be still wrong.
>
> One idea is to track the depth of scans. Attached patch is for PG18, and I tried not
> to modify the header as much as possible. It also had a test code based on your
> reproducer. Can you see it's same as your expectation?
I think using a counter to fix this is OK. But I'm wondering whether it's
necessary to keep both the boolean and the counter, could we merge them into a
single parameter to simplify things?
Best Regards,
Zhijie Hou
^ permalink raw reply [nested|flat] 18+ messages in thread
* RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641)
2026-10-02 07:33 Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-07 10:08 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Zhijie Hou <houzhijie22@gmail.com>
@ 2026-10-08 02:36 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-08 05:35 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Jiří Kavalík <jiri.kavalik@comgate.cz>
0 siblings, 1 reply; 18+ messages in thread
From: Hayato Kuroda (Fujitsu) @ 2026-10-08 02:36 UTC (permalink / raw)
To: Zhijie Hou <houzhijie22@gmail.com>; +Cc: jiri.kavalik@comgate.cz <jiri.kavalik@comgate.cz>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Dear Hou,
Thanks for the review!
> I think using a counter to fix this is OK. But I'm wondering whether it's
> necessary to keep both the boolean and the counter, could we merge them into
> a
> single parameter to simplify things?
Actually it's intentional because the initially attached patch was for PG18.
bsysscan was exported and functions in tableam.h referred it. I did not want to
modify the header as much as possible thus they were retained.
For HEAD, yes, the boolean can be removed from everywhere, see attached.
I will create patched for back branches when they are stabilized.
Best regards,
Hayato Kuroda
FUJITSU LIMITED
Attachments:
v2-HEAD-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 10.9K) download
From 672c68c05cdea4d46624dc7ceb01b1203c45d121 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v2-HEAD] 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 | 62 +++++++++++++----------
src/backend/replication/logical/logical.c | 3 +-
src/include/access/genam.h | 2 +
src/include/access/tableam.h | 8 +--
src/include/access/xact.h | 2 +-
7 files changed, 122 insertions(+), 34 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 98ac5bc79c9..05f2d43d939 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -37,6 +37,38 @@
#include "utils/ruleutils.h"
#include "utils/snapmgr.h"
+/* Number of nested systable scans while decoding in-progress transactions. */
+int sysscan_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 the flag
+ * while an outer scan is still active.
+ */
+static inline void
+IncrementSysScanDepth(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ sysscan_depth++;
+}
+
+static inline void
+DecrementSysScanDepth(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(sysscan_depth > 0);
+ sysscan_depth--;
+}
+
+void
+ResetSysScanDepth(void)
+{
+ sysscan_depth = 0;
+}
/* ----------------------------------------------------------------
* general access method routines
@@ -432,13 +464,7 @@ systable_beginscan(Relation heapRelation,
sysscan->snapshot = 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();
if (irel)
{
@@ -633,12 +659,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);
}
@@ -721,13 +742,7 @@ systable_beginscan_ordered(Relation heapRelation,
elog(ERROR, "column is not in index");
}
- /*
- * 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();
sysscan->iscan = index_beginscan(heapRelation, indexRelation, false,
snapshot, NULL, nkeys, 0,
@@ -782,12 +797,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 98e5f1dd8f9..421d7d0d6b5 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"
@@ -2030,7 +2031,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSysScanDepth();
}
/*
diff --git a/src/include/access/genam.h b/src/include/access/genam.h
index 1bfdc1ef6b1..c5b9a457fb2 100644
--- a/src/include/access/genam.h
+++ b/src/include/access/genam.h
@@ -248,4 +248,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 */
diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h
index ea3f2a6be99..692f70adc90 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -927,7 +927,7 @@ table_beginscan_common(Relation rel, Snapshot snapshot, int nkeys,
* via systable_beginscan() et al. See detailed comments in xact.c where
* these variables are declared.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) && !sysscan_depth))
elog(ERROR, "scan started during logical decoding");
return rel->rd_tableam->scan_begin(rel, snapshot, nkeys, key, pscan, flags);
@@ -1251,7 +1251,7 @@ table_index_scan_begin(IndexScanDesc scan, uint32 flags)
* via systable_beginscan() et al. See detailed comments in xact.c where
* these variables are declared.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) && !sysscan_depth))
elog(ERROR, "scan started during logical decoding");
scan->heapRelation->rd_tableam->index_scan_begin(scan, flags);
@@ -1344,7 +1344,7 @@ table_fetch_tid(Relation rel,
* CheckXidAlive for catalog or regular tables. See detailed comments in
* xact.c where these variables are declared.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) && !sysscan_depth))
elog(ERROR, "unexpected table_fetch_tid call during logical decoding");
return rel->rd_tableam->fetch_tid(rel, tid, snapshot, all_dead);
@@ -1369,7 +1369,7 @@ table_tuple_fetch_row_version(Relation rel,
* valid CheckXidAlive for catalog or regular tables. See detailed
* comments in xact.c where these variables are declared.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) && !sysscan_depth))
elog(ERROR, "unexpected table_tuple_fetch_row_version call during logical decoding");
return rel->rd_tableam->tuple_fetch_row_version(rel, tid, snapshot, slot);
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index a8cbdf247c8..b28fe967589 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -85,7 +85,7 @@ extern PGDLLIMPORT int synchronous_commit;
/* used during logical streaming of a transaction */
extern PGDLLIMPORT TransactionId CheckXidAlive;
-extern PGDLLIMPORT bool bsysscan;
+extern PGDLLIMPORT int sysscan_depth;
/*
* Miscellaneous flag bits to record events which occur on the top level
--
2.52.0
^ permalink raw reply [nested|flat] 18+ messages in thread
* Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641)
2026-10-02 07:33 Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-07 10:08 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Zhijie Hou <houzhijie22@gmail.com>
2026-10-08 02:36 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
@ 2026-10-08 05:35 ` Jiří Kavalík <jiri.kavalik@comgate.cz>
2026-10-09 02:56 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
0 siblings, 1 reply; 18+ messages in thread
From: Jiří Kavalík @ 2026-10-08 05:35 UTC (permalink / raw)
To: Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; +Cc: Zhijie Hou <houzhijie22@gmail.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Hi Kuroda-san,
> For HEAD, yes, the boolean can be removed from everywhere, see attached.
One small thing in v2-HEAD: the definition in xact.c is left behind.
With the patch applied on master (061065e), "git grep -w bsysscan" still
finds
src/backend/access/transam/xact.c:103:bool bsysscan = false;
and since the extern is gone from xact.h, gcc 14 warns:
xact.c:103:17: warning: no previous declaration for 'bsysscan'
[-Wmissing-variable-declarations]
Removing that line should be enough. The comment above it in xact.c may also
want a word about the counter.
Regards,
Jiří Kavalík
čt 8. 10. 2026 v 4:36 odesílatel Hayato Kuroda (Fujitsu) <
kuroda.hayato@fujitsu.com> napsal:
> Dear Hou,
>
> Thanks for the review!
>
> > I think using a counter to fix this is OK. But I'm wondering whether it's
> > necessary to keep both the boolean and the counter, could we merge them
> into
> > a
> > single parameter to simplify things?
>
> Actually it's intentional because the initially attached patch was for
> PG18.
> bsysscan was exported and functions in tableam.h referred it. I did not
> want to
> modify the header as much as possible thus they were retained.
>
> For HEAD, yes, the boolean can be removed from everywhere, see attached.
> I will create patched for back branches when they are stabilized.
>
> Best regards,
> Hayato Kuroda
> FUJITSU LIMITED
>
>
--
S pozdravem
Jiří Kavalík
jiri.kavalik@comgate.cz
Comgate a.s.
Gočárova třída 1754/48b, 500 02 Hradec Králové
www.comgate.cz
^ permalink raw reply [nested|flat] 18+ messages in thread
* RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641)
2026-10-02 07:33 Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-07 10:08 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Zhijie Hou <houzhijie22@gmail.com>
2026-10-08 02:36 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-08 05:35 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Jiří Kavalík <jiri.kavalik@comgate.cz>
@ 2026-10-09 02:56 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-26 13:25 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
0 siblings, 1 reply; 18+ messages in thread
From: Hayato Kuroda (Fujitsu) @ 2026-10-09 02:56 UTC (permalink / raw)
To: 'Jiří Kavalík' <jiri.kavalik@comgate.cz>; +Cc: Zhijie Hou <houzhijie22@gmail.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Dear Jiří
Good catch. I accidentally removed the meson option " --werror" so that
I could not recognize. PSA fixed version.
Best regards,
Hayato Kuroda
FUJITSU LIMITED
Attachments:
v3-HEAD-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 11.4K) download
From aab5684280084a618d41111e3220259cefc9c270 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v3-HEAD] 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 | 62 +++++++++++++----------
src/backend/access/transam/xact.c | 1 -
src/backend/replication/logical/logical.c | 3 +-
src/include/access/genam.h | 2 +
src/include/access/tableam.h | 8 +--
src/include/access/xact.h | 2 +-
8 files changed, 122 insertions(+), 35 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 98ac5bc79c9..05f2d43d939 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -37,6 +37,38 @@
#include "utils/ruleutils.h"
#include "utils/snapmgr.h"
+/* Number of nested systable scans while decoding in-progress transactions. */
+int sysscan_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 the flag
+ * while an outer scan is still active.
+ */
+static inline void
+IncrementSysScanDepth(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ sysscan_depth++;
+}
+
+static inline void
+DecrementSysScanDepth(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(sysscan_depth > 0);
+ sysscan_depth--;
+}
+
+void
+ResetSysScanDepth(void)
+{
+ sysscan_depth = 0;
+}
/* ----------------------------------------------------------------
* general access method routines
@@ -432,13 +464,7 @@ systable_beginscan(Relation heapRelation,
sysscan->snapshot = 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();
if (irel)
{
@@ -633,12 +659,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);
}
@@ -721,13 +742,7 @@ systable_beginscan_ordered(Relation heapRelation,
elog(ERROR, "column is not in index");
}
- /*
- * 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();
sysscan->iscan = index_beginscan(heapRelation, indexRelation, false,
snapshot, NULL, nkeys, 0,
@@ -782,12 +797,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/access/transam/xact.c b/src/backend/access/transam/xact.c
index cab99f25e1e..072bf8a39a1 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -100,7 +100,6 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
-bool bsysscan = false;
/*
* When running as a parallel worker, we place only a single
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index 98e5f1dd8f9..421d7d0d6b5 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"
@@ -2030,7 +2031,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSysScanDepth();
}
/*
diff --git a/src/include/access/genam.h b/src/include/access/genam.h
index 1bfdc1ef6b1..c5b9a457fb2 100644
--- a/src/include/access/genam.h
+++ b/src/include/access/genam.h
@@ -248,4 +248,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 */
diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h
index ea3f2a6be99..692f70adc90 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -927,7 +927,7 @@ table_beginscan_common(Relation rel, Snapshot snapshot, int nkeys,
* via systable_beginscan() et al. See detailed comments in xact.c where
* these variables are declared.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) && !sysscan_depth))
elog(ERROR, "scan started during logical decoding");
return rel->rd_tableam->scan_begin(rel, snapshot, nkeys, key, pscan, flags);
@@ -1251,7 +1251,7 @@ table_index_scan_begin(IndexScanDesc scan, uint32 flags)
* via systable_beginscan() et al. See detailed comments in xact.c where
* these variables are declared.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) && !sysscan_depth))
elog(ERROR, "scan started during logical decoding");
scan->heapRelation->rd_tableam->index_scan_begin(scan, flags);
@@ -1344,7 +1344,7 @@ table_fetch_tid(Relation rel,
* CheckXidAlive for catalog or regular tables. See detailed comments in
* xact.c where these variables are declared.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) && !sysscan_depth))
elog(ERROR, "unexpected table_fetch_tid call during logical decoding");
return rel->rd_tableam->fetch_tid(rel, tid, snapshot, all_dead);
@@ -1369,7 +1369,7 @@ table_tuple_fetch_row_version(Relation rel,
* valid CheckXidAlive for catalog or regular tables. See detailed
* comments in xact.c where these variables are declared.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) && !sysscan_depth))
elog(ERROR, "unexpected table_tuple_fetch_row_version call during logical decoding");
return rel->rd_tableam->tuple_fetch_row_version(rel, tid, snapshot, slot);
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index a8cbdf247c8..b28fe967589 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -85,7 +85,7 @@ extern PGDLLIMPORT int synchronous_commit;
/* used during logical streaming of a transaction */
extern PGDLLIMPORT TransactionId CheckXidAlive;
-extern PGDLLIMPORT bool bsysscan;
+extern PGDLLIMPORT int sysscan_depth;
/*
* Miscellaneous flag bits to record events which occur on the top level
--
2.52.0
^ permalink raw reply [nested|flat] 18+ messages in thread
* RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641)
2026-10-02 07:33 Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-07 10:08 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Zhijie Hou <houzhijie22@gmail.com>
2026-10-08 02:36 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-08 05:35 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Jiří Kavalík <jiri.kavalik@comgate.cz>
2026-10-09 02:56 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
@ 2026-10-26 13:25 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-27 03:36 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Peter Smith <smithpb2250@gmail.com>
0 siblings, 1 reply; 18+ messages in thread
From: Hayato Kuroda (Fujitsu) @ 2026-10-26 13:25 UTC (permalink / raw)
To: 'Jiří Kavalík' <jiri.kavalik@comgate.cz>; +Cc: Zhijie Hou <houzhijie22@gmail.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>; Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
Hi members,
I made a patch set for supported branches. I confirmed the error can happen
and the patch can address the issue on PG14-PG18.
I refactored the test code for the readability. Wondering PG19/HEAD should contain
the test, because the reported ERROR could not happen in the first place.
Best regards,
Hayato Kuroda
FUJITSU LIMITED
Attachments: download all (6 files, .tar.gz)
nocfbot-v4-PG14-PG15-0001-Track-the-depth-of-scans-of-system-tabl.patch (application/octet-stream, 8.0K) download
From c8ea4d347a7f8571df623211998aa13a9aefa9ac Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v4-PG14-PG15] Track the depth of scans of system tables
---
contrib/test_decoding/expected/stream.out | 43 ++++++++++++++-
contrib/test_decoding/sql/stream.sql | 32 ++++++++++-
src/backend/access/index/genam.c | 66 ++++++++++++++---------
src/backend/replication/logical/logical.c | 3 +-
src/include/access/genam.h | 2 +
5 files changed, 117 insertions(+), 29 deletions(-)
diff --git a/contrib/test_decoding/expected/stream.out b/contrib/test_decoding/expected/stream.out
index 0f21dcb8e0e..0cfcf716ea0 100644
--- a/contrib/test_decoding/expected/stream.out
+++ b/contrib/test_decoding/expected/stream.out
@@ -106,7 +106,48 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL,NULL, 'incl
committing streamed transaction
(17 rows)
-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);
+ ALTER TABLE nested_sys_scan_test ALTER COLUMN checked_value set storage external;
+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.
+BEGIN;
+INSERT INTO nested_sys_scan_test SELECT repeat('x', 6000) FROM generate_series(1, 10);
+CHECKPOINT;
+SELECT count(*) > 0 AS streamed FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+ streamed
+----------
+ t
+(1 row)
+
+COMMIT;
+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 4feec62972a..815900e707d 100644
--- a/contrib/test_decoding/sql/stream.sql
+++ b/contrib/test_decoding/sql/stream.sql
@@ -44,5 +44,35 @@ toasted-123456789012345678901234567890123456789012345678901234567890123456789012
SELECT data 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);
+ ALTER TABLE nested_sys_scan_test ALTER COLUMN checked_value set storage external;
+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.
+BEGIN;
+INSERT INTO nested_sys_scan_test SELECT repeat('x', 6000) FROM generate_series(1, 10);
+CHECKPOINT;
+SELECT count(*) > 0 AS streamed FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+COMMIT;
+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 6dd05e01be8..c0e4d611555 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -39,6 +39,42 @@
#include "utils/snapmgr.h"
#include "utils/syscache.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
@@ -460,13 +496,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;
}
@@ -615,12 +645,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);
}
@@ -703,13 +728,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;
}
@@ -756,12 +775,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 d796abbde78..754e8981780 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 "fmgr.h"
@@ -1931,7 +1932,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSysScanDepth();
}
/*
diff --git a/src/include/access/genam.h b/src/include/access/genam.h
index 26436abdf2a..0f80ab1c6fd 100644
--- a/src/include/access/genam.h
+++ b/src/include/access/genam.h
@@ -238,4 +238,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
v4-HEAD-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 11.4K) download
From 7a77c3091ef0382ffa2115afaf9e5f0591b28c56 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v4-HEAD] 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 | 62 +++++++++++++----------
src/backend/access/transam/xact.c | 1 -
src/backend/replication/logical/logical.c | 3 +-
src/include/access/genam.h | 2 +
src/include/access/tableam.h | 8 +--
src/include/access/xact.h | 2 +-
8 files changed, 120 insertions(+), 35 deletions(-)
diff --git a/contrib/test_decoding/expected/stream.out b/contrib/test_decoding/expected/stream.out
index 0ec5c933610..5192d8c9579 100644
--- a/contrib/test_decoding/expected/stream.out
+++ b/contrib/test_decoding/expected/stream.out
@@ -134,6 +134,48 @@ 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 (
+ 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)
+
-- bug #19616
--
-- An aborted top-level transaction that is discarded at eviction must not
@@ -157,7 +199,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..9649b133111 100644
--- a/contrib/test_decoding/sql/stream.sql
+++ b/contrib/test_decoding/sql/stream.sql
@@ -65,6 +65,37 @@ 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 (
+ 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');
+
-- bug #19616
--
-- An aborted top-level transaction that is discarded at eviction must not
@@ -82,5 +113,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 98ac5bc79c9..05f2d43d939 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -37,6 +37,38 @@
#include "utils/ruleutils.h"
#include "utils/snapmgr.h"
+/* Number of nested systable scans while decoding in-progress transactions. */
+int sysscan_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 the flag
+ * while an outer scan is still active.
+ */
+static inline void
+IncrementSysScanDepth(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ sysscan_depth++;
+}
+
+static inline void
+DecrementSysScanDepth(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(sysscan_depth > 0);
+ sysscan_depth--;
+}
+
+void
+ResetSysScanDepth(void)
+{
+ sysscan_depth = 0;
+}
/* ----------------------------------------------------------------
* general access method routines
@@ -432,13 +464,7 @@ systable_beginscan(Relation heapRelation,
sysscan->snapshot = 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();
if (irel)
{
@@ -633,12 +659,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);
}
@@ -721,13 +742,7 @@ systable_beginscan_ordered(Relation heapRelation,
elog(ERROR, "column is not in index");
}
- /*
- * 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();
sysscan->iscan = index_beginscan(heapRelation, indexRelation, false,
snapshot, NULL, nkeys, 0,
@@ -782,12 +797,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/access/transam/xact.c b/src/backend/access/transam/xact.c
index 0121a0c8fa0..79aace30ab4 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -100,7 +100,6 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
-bool bsysscan = false;
/*
* When running as a parallel worker, we place only a single
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index 98e5f1dd8f9..421d7d0d6b5 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"
@@ -2030,7 +2031,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSysScanDepth();
}
/*
diff --git a/src/include/access/genam.h b/src/include/access/genam.h
index 1bfdc1ef6b1..c5b9a457fb2 100644
--- a/src/include/access/genam.h
+++ b/src/include/access/genam.h
@@ -248,4 +248,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 */
diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h
index ea3f2a6be99..692f70adc90 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -927,7 +927,7 @@ table_beginscan_common(Relation rel, Snapshot snapshot, int nkeys,
* via systable_beginscan() et al. See detailed comments in xact.c where
* these variables are declared.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) && !sysscan_depth))
elog(ERROR, "scan started during logical decoding");
return rel->rd_tableam->scan_begin(rel, snapshot, nkeys, key, pscan, flags);
@@ -1251,7 +1251,7 @@ table_index_scan_begin(IndexScanDesc scan, uint32 flags)
* via systable_beginscan() et al. See detailed comments in xact.c where
* these variables are declared.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) && !sysscan_depth))
elog(ERROR, "scan started during logical decoding");
scan->heapRelation->rd_tableam->index_scan_begin(scan, flags);
@@ -1344,7 +1344,7 @@ table_fetch_tid(Relation rel,
* CheckXidAlive for catalog or regular tables. See detailed comments in
* xact.c where these variables are declared.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) && !sysscan_depth))
elog(ERROR, "unexpected table_fetch_tid call during logical decoding");
return rel->rd_tableam->fetch_tid(rel, tid, snapshot, all_dead);
@@ -1369,7 +1369,7 @@ table_tuple_fetch_row_version(Relation rel,
* valid CheckXidAlive for catalog or regular tables. See detailed
* comments in xact.c where these variables are declared.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) && !sysscan_depth))
elog(ERROR, "unexpected table_tuple_fetch_row_version call during logical decoding");
return rel->rd_tableam->tuple_fetch_row_version(rel, tid, snapshot, slot);
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index a8cbdf247c8..b28fe967589 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -85,7 +85,7 @@ extern PGDLLIMPORT int synchronous_commit;
/* used during logical streaming of a transaction */
extern PGDLLIMPORT TransactionId CheckXidAlive;
-extern PGDLLIMPORT bool bsysscan;
+extern PGDLLIMPORT int sysscan_depth;
/*
* Miscellaneous flag bits to record events which occur on the top level
--
2.52.0
nocfbot-v4-PG19-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 10.9K) download
From f7c0fdc64d55728e3a0198d072363c3b8becacf8 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v4-PG19] 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 | 62 +++++++++++++----------
src/backend/access/transam/xact.c | 1 -
src/backend/replication/logical/logical.c | 3 +-
src/include/access/genam.h | 2 +
src/include/access/tableam.h | 6 +--
src/include/access/xact.h | 2 +-
8 files changed, 119 insertions(+), 34 deletions(-)
diff --git a/contrib/test_decoding/expected/stream.out b/contrib/test_decoding/expected/stream.out
index 0ec5c933610..5192d8c9579 100644
--- a/contrib/test_decoding/expected/stream.out
+++ b/contrib/test_decoding/expected/stream.out
@@ -134,6 +134,48 @@ 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 (
+ 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)
+
-- bug #19616
--
-- An aborted top-level transaction that is discarded at eviction must not
@@ -157,7 +199,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..9649b133111 100644
--- a/contrib/test_decoding/sql/stream.sql
+++ b/contrib/test_decoding/sql/stream.sql
@@ -65,6 +65,37 @@ 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 (
+ 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');
+
-- bug #19616
--
-- An aborted top-level transaction that is discarded at eviction must not
@@ -82,5 +113,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 1408989c568..5dba2185b1d 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -37,6 +37,38 @@
#include "utils/ruleutils.h"
#include "utils/snapmgr.h"
+/* Number of nested systable scans while decoding in-progress transactions. */
+int sysscan_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 the flag
+ * while an outer scan is still active.
+ */
+static inline void
+IncrementSysScanDepth(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ sysscan_depth++;
+}
+
+static inline void
+DecrementSysScanDepth(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(sysscan_depth > 0);
+ sysscan_depth--;
+}
+
+void
+ResetSysScanDepth(void)
+{
+ sysscan_depth = 0;
+}
/* ----------------------------------------------------------------
* general access method routines
@@ -420,13 +452,7 @@ systable_beginscan(Relation heapRelation,
sysscan->snapshot = 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();
if (irel)
{
@@ -620,12 +646,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);
}
@@ -708,13 +729,7 @@ systable_beginscan_ordered(Relation heapRelation,
elog(ERROR, "column is not in index");
}
- /*
- * 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();
sysscan->iscan = index_beginscan(heapRelation, indexRelation,
snapshot, NULL, nkeys, 0,
@@ -769,12 +784,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/access/transam/xact.c b/src/backend/access/transam/xact.c
index 92319290cf7..e329f36b476 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -100,7 +100,6 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
-bool bsysscan = false;
/*
* When running as a parallel worker, we place only a single
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index 98e5f1dd8f9..421d7d0d6b5 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"
@@ -2030,7 +2031,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSysScanDepth();
}
/*
diff --git a/src/include/access/genam.h b/src/include/access/genam.h
index 3e33c1d4ae4..6eba666b7d0 100644
--- a/src/include/access/genam.h
+++ b/src/include/access/genam.h
@@ -251,4 +251,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 */
diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h
index ff03a2b816f..dae2834d74d 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -927,7 +927,7 @@ table_beginscan_common(Relation rel, Snapshot snapshot, int nkeys,
* via systable_beginscan() et al. See detailed comments in xact.c where
* these variables are declared.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) && !sysscan_depth))
elog(ERROR, "scan started during logical decoding");
return rel->rd_tableam->scan_begin(rel, snapshot, nkeys, key, pscan, flags);
@@ -1252,7 +1252,7 @@ table_index_fetch_begin(Relation rel, uint32 flags)
* via systable_beginscan() et al. See detailed comments in xact.c where
* these variables are declared.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) && !sysscan_depth))
elog(ERROR, "scan started during logical decoding");
return rel->rd_tableam->index_fetch_begin(rel, flags);
@@ -1351,7 +1351,7 @@ table_tuple_fetch_row_version(Relation rel,
* valid CheckXidAlive for catalog or regular tables. See detailed
* comments in xact.c where these variables are declared.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) && !sysscan_depth))
elog(ERROR, "unexpected table_tuple_fetch_row_version call during logical decoding");
return rel->rd_tableam->tuple_fetch_row_version(rel, tid, snapshot, slot);
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index a8cbdf247c8..b28fe967589 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -85,7 +85,7 @@ extern PGDLLIMPORT int synchronous_commit;
/* used during logical streaming of a transaction */
extern PGDLLIMPORT TransactionId CheckXidAlive;
-extern PGDLLIMPORT bool bsysscan;
+extern PGDLLIMPORT int sysscan_depth;
/*
* Miscellaneous flag bits to record events which occur on the top level
--
2.52.0
nocfbot-v4-PG16-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 7.9K) download
From dbb030ce15c7327e1a4885c77342bc7b74a78335 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v4-PG16] 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 0f21dcb8e0e..c5880508299 100644
--- a/contrib/test_decoding/expected/stream.out
+++ b/contrib/test_decoding/expected/stream.out
@@ -106,7 +106,49 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL,NULL, 'incl
committing streamed transaction
(17 rows)
-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 4feec62972a..f42412a5e78 100644
--- a/contrib/test_decoding/sql/stream.sql
+++ b/contrib/test_decoding/sql/stream.sql
@@ -44,5 +44,36 @@ toasted-123456789012345678901234567890123456789012345678901234567890123456789012
SELECT data 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 67246406cff..6e6d595b0cc 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -38,6 +38,42 @@
#include "utils/snapmgr.h"
#include "utils/syscache.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
@@ -460,13 +496,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;
}
@@ -615,12 +645,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);
}
@@ -703,13 +728,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;
}
@@ -756,12 +775,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 ce7f7143070..ef7bd18fae3 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 "fmgr.h"
@@ -2008,7 +2009,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSysScanDepth();
}
/*
diff --git a/src/include/access/genam.h b/src/include/access/genam.h
index 7188d42894e..516c7b7ed32 100644
--- a/src/include/access/genam.h
+++ b/src/include/access/genam.h
@@ -240,4 +240,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
nocfbot-v4-PG17-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 7.8K) download
From ff6ba5df9e5fbf2508f6e931b2e63c5801a9e966 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
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
nocfbot-v4-PG18-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 8.3K) download
From da1a2bdb111bb313e2b1cbb486afd65c5b22ba35 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v4-PG18] 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 0ec5c933610..5192d8c9579 100644
--- a/contrib/test_decoding/expected/stream.out
+++ b/contrib/test_decoding/expected/stream.out
@@ -134,6 +134,48 @@ 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 (
+ 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)
+
-- bug #19616
--
-- An aborted top-level transaction that is discarded at eviction must not
@@ -157,7 +199,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..9649b133111 100644
--- a/contrib/test_decoding/sql/stream.sql
+++ b/contrib/test_decoding/sql/stream.sql
@@ -65,6 +65,37 @@ 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 (
+ 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');
+
-- bug #19616
--
-- An aborted top-level transaction that is discarded at eviction must not
@@ -82,5 +113,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
^ permalink raw reply [nested|flat] 18+ messages in thread
* Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641)
2026-10-02 07:33 Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-07 10:08 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Zhijie Hou <houzhijie22@gmail.com>
2026-10-08 02:36 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-08 05:35 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Jiří Kavalík <jiri.kavalik@comgate.cz>
2026-10-09 02:56 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-26 13:25 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
@ 2026-10-27 03:36 ` Peter Smith <smithpb2250@gmail.com>
2026-10-27 06:42 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
0 siblings, 1 reply; 18+ messages in thread
From: Peter Smith @ 2026-10-27 03:36 UTC (permalink / raw)
To: Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; +Cc: Jiří Kavalík <jiri.kavalik@comgate.cz>; Zhijie Hou <houzhijie22@gmail.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
On Tue, Oct 27, 2026 at 12:25 AM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:
>
> Hi members,
>
> I made a patch set for supported branches. I confirmed the error can happen
> and the patch can address the issue on PG14-PG18.
>
> I refactored the test code for the readability. Wondering PG19/HEAD should contain
> the test, because the reported ERROR could not happen in the first place.
>
Hi Kuroda-San,
Since you are refactoring this flag anyway, would it be better to try
to eliminate the global-variable completely? e.g. expose the same
information by some accessor function.
-extern PGDLLIMPORT bool bsysscan;
+extern PGDLLIMPORT int sysscan_depth;
======
Kind Regards,
Peter Smith.
Fujitsu Australia.
^ permalink raw reply [nested|flat] 18+ messages in thread
* RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641)
2026-10-02 07:33 Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-07 10:08 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Zhijie Hou <houzhijie22@gmail.com>
2026-10-08 02:36 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-08 05:35 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Jiří Kavalík <jiri.kavalik@comgate.cz>
2026-10-09 02:56 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-26 13:25 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-27 03:36 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Peter Smith <smithpb2250@gmail.com>
@ 2026-10-27 06:42 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-28 08:41 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Chao Li <li.evan.chao@gmail.com>
0 siblings, 1 reply; 18+ messages in thread
From: Hayato Kuroda (Fujitsu) @ 2026-10-27 06:42 UTC (permalink / raw)
To: Peter Smith <smithpb2250@gmail.com>; +Cc: Jiří Kavalík <jiri.kavalik@comgate.cz>; Zhijie Hou <houzhijie22@gmail.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Hi Peter,
> Since you are refactoring this flag anyway, would it be better to try
> to eliminate the global-variable completely? e.g. expose the same
> information by some accessor function.
>
> -extern PGDLLIMPORT bool bsysscan;
> +extern PGDLLIMPORT int sysscan_depth;
Good point, it can be removed by providing few functions. v5 did the same.
We need to keep bsysscan anyway for back branches, so the patch shape for them
is slightly different, see v5-PG18.
Best regards,
Hayato Kuroda
FUJITSU LIMITED
Attachments: download all (3 files, .tar.gz)
v5-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 7.9K) download
From 4d8131c9ad26a939c170d74b7e9a2df2edd73a8b Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v5] Track the depth of scans of system tables
---
src/backend/access/index/genam.c | 30 ++-----------
src/backend/access/transam/xact.c | 51 ++++++++++++++++++++++-
src/backend/replication/logical/logical.c | 2 +-
src/include/access/tableam.h | 28 +++++++------
src/include/access/xact.h | 6 ++-
5 files changed, 76 insertions(+), 41 deletions(-)
diff --git a/src/backend/access/index/genam.c b/src/backend/access/index/genam.c
index 98ac5bc79c9..35e73ce9513 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -432,13 +432,7 @@ systable_beginscan(Relation heapRelation,
sysscan->snapshot = 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;
+ BeginSystemTableScan();
if (irel)
{
@@ -633,12 +627,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;
+ EndSystemTableScan();
pfree(sysscan);
}
@@ -721,13 +710,7 @@ systable_beginscan_ordered(Relation heapRelation,
elog(ERROR, "column is not in index");
}
- /*
- * 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;
+ BeginSystemTableScan();
sysscan->iscan = index_beginscan(heapRelation, indexRelation, false,
snapshot, NULL, nkeys, 0,
@@ -782,12 +765,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;
+ EndSystemTableScan();
pfree(sysscan);
}
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 0121a0c8fa0..45af6cce946 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -100,7 +100,13 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
-bool bsysscan = false;
+
+/*
+ * Number of nested systable scans while CheckXidAlive is valid. A counter is
+ * needed because systable scans can be nested. Transaction abort resets it,
+ * since ERROR can bypass the matching systable_endscan().
+ */
+static int sysscan_depth = 0;
/*
* When running as a parallel worker, we place only a single
@@ -6531,3 +6537,46 @@ xact_redo(XLogReaderState *record)
else
elog(PANIC, "xact_redo: unknown op code %u", info);
}
+
+/*
+ * Record entry into a system table scan while CheckXidAlive is valid.
+ */
+void
+BeginSystemTableScan(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ sysscan_depth++;
+}
+
+/*
+ * Record normal exit from a tracked system table scan.
+ */
+void
+EndSystemTableScan(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(IsSystemTableScanInProgress());
+ sysscan_depth--;
+}
+
+/*
+ * Reset the system table scan state during transaction abort.
+ */
+void
+ResetSystemTableScanState(void)
+{
+ sysscan_depth = 0;
+}
+
+/*
+ * Return whether any tracked system table scan is in progress.
+ */
+bool
+IsSystemTableScanInProgress(void)
+{
+ return sysscan_depth > 0;
+}
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index 98e5f1dd8f9..f579fe934c7 100644
--- a/src/backend/replication/logical/logical.c
+++ b/src/backend/replication/logical/logical.c
@@ -2030,7 +2030,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSystemTableScanState();
}
/*
diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h
index ea3f2a6be99..f66faaacf9b 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -924,10 +924,11 @@ table_beginscan_common(Relation rel, Snapshot snapshot, int nkeys,
/*
* We don't allow scans to be started while CheckXidAlive is set, except
- * via systable_beginscan() et al. See detailed comments in xact.c where
- * these variables are declared.
+ * via systable_beginscan() et al. See comments for CheckXidAlive in
+ * xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "scan started during logical decoding");
return rel->rd_tableam->scan_begin(rel, snapshot, nkeys, key, pscan, flags);
@@ -1248,10 +1249,11 @@ table_index_scan_begin(IndexScanDesc scan, uint32 flags)
/*
* We don't allow scans to be started while CheckXidAlive is set, except
- * via systable_beginscan() et al. See detailed comments in xact.c where
- * these variables are declared.
+ * via systable_beginscan() et al. See comments for CheckXidAlive in
+ * xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "scan started during logical decoding");
scan->heapRelation->rd_tableam->index_scan_begin(scan, flags);
@@ -1341,10 +1343,11 @@ table_fetch_tid(Relation rel,
{
/*
* We don't expect direct calls to table_fetch_tid with valid
- * CheckXidAlive for catalog or regular tables. See detailed comments in
- * xact.c where these variables are declared.
+ * CheckXidAlive for catalog or regular tables. See comments for
+ * CheckXidAlive in xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "unexpected table_fetch_tid call during logical decoding");
return rel->rd_tableam->fetch_tid(rel, tid, snapshot, all_dead);
@@ -1366,10 +1369,11 @@ table_tuple_fetch_row_version(Relation rel,
{
/*
* We don't expect direct calls to table_tuple_fetch_row_version with
- * valid CheckXidAlive for catalog or regular tables. See detailed
- * comments in xact.c where these variables are declared.
+ * valid CheckXidAlive for catalog or regular tables. See comments for
+ * CheckXidAlive in xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "unexpected table_tuple_fetch_row_version call during logical decoding");
return rel->rd_tableam->tuple_fetch_row_version(rel, tid, snapshot, slot);
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index a8cbdf247c8..cc91388a798 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -85,7 +85,6 @@ extern PGDLLIMPORT int synchronous_commit;
/* used during logical streaming of a transaction */
extern PGDLLIMPORT TransactionId CheckXidAlive;
-extern PGDLLIMPORT bool bsysscan;
/*
* Miscellaneous flag bits to record events which occur on the top level
@@ -535,4 +534,9 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
+extern void BeginSystemTableScan(void);
+extern void EndSystemTableScan(void);
+extern void ResetSystemTableScanState(void);
+extern bool IsSystemTableScanInProgress(void);
+
#endif /* XACT_H */
--
2.52.0
nocfbot-v5-PG18-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 9.1K) download
From 45555458e4672bbd440756eff57a5e1316fd4bc3 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v5-PG18] 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 | 31 ++------------
src/backend/access/transam/xact.c | 49 +++++++++++++++++++++++
src/backend/replication/logical/logical.c | 3 +-
src/include/access/xact.h | 4 ++
6 files changed, 134 insertions(+), 30 deletions(-)
diff --git a/contrib/test_decoding/expected/stream.out b/contrib/test_decoding/expected/stream.out
index 0ec5c933610..ce64e361c26 100644
--- a/contrib/test_decoding/expected/stream.out
+++ b/contrib/test_decoding/expected/stream.out
@@ -134,6 +134,48 @@ 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 (
+ 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 consumed FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+ consumed
+----------
+ t
+(1 row)
+
-- bug #19616
--
-- An aborted top-level transaction that is discarded at eviction must not
@@ -157,7 +199,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..48b54eee1aa 100644
--- a/contrib/test_decoding/sql/stream.sql
+++ b/contrib/test_decoding/sql/stream.sql
@@ -65,6 +65,37 @@ 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 (
+ 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 consumed 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 +113,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..622ef9fb5db 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -37,7 +37,6 @@
#include "utils/ruleutils.h"
#include "utils/snapmgr.h"
-
/* ----------------------------------------------------------------
* general access method routines
*
@@ -468,13 +467,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;
+ BeginSystemTableScan();
return sysscan;
}
@@ -619,12 +612,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;
+ EndSystemTableScan();
pfree(sysscan);
}
@@ -714,13 +702,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;
+ BeginSystemTableScan();
return sysscan;
}
@@ -767,12 +749,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;
+ EndSystemTableScan();
pfree(sysscan);
}
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index d81e749b031..632b582af58 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -98,6 +98,18 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
+
+/*
+ * Number of nested systable scans while CheckXidAlive is valid. A counter is
+ * needed because systable scans can be nested. Transaction abort resets it,
+ * since ERROR can bypass the matching systable_endscan().
+ */
+static int bsysscan_depth = 0;
+
+/*
+ * Indicates whether we are currently in a system table scan. Retained for the
+ * backwards-compatibility.
+ */
bool bsysscan = false;
/*
@@ -6462,3 +6474,40 @@ xact_redo(XLogReaderState *record)
else
elog(PANIC, "xact_redo: unknown op code %u", info);
}
+
+/*
+ * Record entry into a system table scan while CheckXidAlive is valid.
+ */
+void
+BeginSystemTableScan(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ bsysscan_depth++;
+ bsysscan = true;
+}
+
+/*
+ * Record normal exit from a tracked system table scan.
+ */
+void
+EndSystemTableScan(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(bsysscan_depth > 0);
+ bsysscan_depth--;
+ bsysscan = (bsysscan_depth > 0);
+}
+
+/*
+ * Reset the system table scan state during transaction abort.
+ */
+void
+ResetSystemTableScanState(void)
+{
+ bsysscan_depth = 0;
+ bsysscan = false;
+}
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index 4d3a39f4cd7..5ff58acbdda 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;
+ ResetSystemTableScanState();
}
/*
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index b2bc10ee041..d2d4f9bb027 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -533,4 +533,8 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
+extern void BeginSystemTableScan(void);
+extern void EndSystemTableScan(void);
+extern void ResetSystemTableScanState(void);
+
#endif /* XACT_H */
--
2.52.0
nocfbot-v5-PG19-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 7.2K) download
From 672d27ad3ac9820d47d4ed0cba8365118d80caea Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v5-PG19] Track the depth of scans of system tables
---
src/backend/access/index/genam.c | 30 ++-----------
src/backend/access/transam/xact.c | 51 ++++++++++++++++++++++-
src/backend/replication/logical/logical.c | 2 +-
src/include/access/tableam.h | 21 ++++++----
src/include/access/xact.h | 6 ++-
5 files changed, 72 insertions(+), 38 deletions(-)
diff --git a/src/backend/access/index/genam.c b/src/backend/access/index/genam.c
index 1408989c568..0fed65ab7bf 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -420,13 +420,7 @@ systable_beginscan(Relation heapRelation,
sysscan->snapshot = 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;
+ BeginSystemTableScan();
if (irel)
{
@@ -620,12 +614,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;
+ EndSystemTableScan();
pfree(sysscan);
}
@@ -708,13 +697,7 @@ systable_beginscan_ordered(Relation heapRelation,
elog(ERROR, "column is not in index");
}
- /*
- * 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;
+ BeginSystemTableScan();
sysscan->iscan = index_beginscan(heapRelation, indexRelation,
snapshot, NULL, nkeys, 0,
@@ -769,12 +752,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;
+ EndSystemTableScan();
pfree(sysscan);
}
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 92319290cf7..70e4b4b371e 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -100,7 +100,13 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
-bool bsysscan = false;
+
+/*
+ * Number of nested systable scans while CheckXidAlive is valid. A counter is
+ * needed because systable scans can be nested. Transaction abort resets it,
+ * since ERROR can bypass the matching systable_endscan().
+ */
+static int sysscan_depth = 0;
/*
* When running as a parallel worker, we place only a single
@@ -6532,3 +6538,46 @@ xact_redo(XLogReaderState *record)
else
elog(PANIC, "xact_redo: unknown op code %u", info);
}
+
+/*
+ * Record entry into a system table scan while CheckXidAlive is valid.
+ */
+void
+BeginSystemTableScan(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ sysscan_depth++;
+}
+
+/*
+ * Record normal exit from a tracked system table scan.
+ */
+void
+EndSystemTableScan(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(IsSystemTableScanInProgress());
+ sysscan_depth--;
+}
+
+/*
+ * Reset the system table scan state during transaction abort.
+ */
+void
+ResetSystemTableScanState(void)
+{
+ sysscan_depth = 0;
+}
+
+/*
+ * Return whether any tracked system table scan is in progress.
+ */
+bool
+IsSystemTableScanInProgress(void)
+{
+ return sysscan_depth > 0;
+}
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index 98e5f1dd8f9..f579fe934c7 100644
--- a/src/backend/replication/logical/logical.c
+++ b/src/backend/replication/logical/logical.c
@@ -2030,7 +2030,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSystemTableScanState();
}
/*
diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h
index ff03a2b816f..376def9461c 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -924,10 +924,11 @@ table_beginscan_common(Relation rel, Snapshot snapshot, int nkeys,
/*
* We don't allow scans to be started while CheckXidAlive is set, except
- * via systable_beginscan() et al. See detailed comments in xact.c where
- * these variables are declared.
+ * via systable_beginscan() et al. See comments for CheckXidAlive in
+ * xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "scan started during logical decoding");
return rel->rd_tableam->scan_begin(rel, snapshot, nkeys, key, pscan, flags);
@@ -1249,10 +1250,11 @@ table_index_fetch_begin(Relation rel, uint32 flags)
/*
* We don't allow scans to be started while CheckXidAlive is set, except
- * via systable_beginscan() et al. See detailed comments in xact.c where
- * these variables are declared.
+ * via systable_beginscan() et al. See comments for CheckXidAlive in
+ * xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "scan started during logical decoding");
return rel->rd_tableam->index_fetch_begin(rel, flags);
@@ -1348,10 +1350,11 @@ table_tuple_fetch_row_version(Relation rel,
{
/*
* We don't expect direct calls to table_tuple_fetch_row_version with
- * valid CheckXidAlive for catalog or regular tables. See detailed
- * comments in xact.c where these variables are declared.
+ * valid CheckXidAlive for catalog or regular tables. See comments for
+ * CheckXidAlive in xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "unexpected table_tuple_fetch_row_version call during logical decoding");
return rel->rd_tableam->tuple_fetch_row_version(rel, tid, snapshot, slot);
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index a8cbdf247c8..cc91388a798 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -85,7 +85,6 @@ extern PGDLLIMPORT int synchronous_commit;
/* used during logical streaming of a transaction */
extern PGDLLIMPORT TransactionId CheckXidAlive;
-extern PGDLLIMPORT bool bsysscan;
/*
* Miscellaneous flag bits to record events which occur on the top level
@@ -535,4 +534,9 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
+extern void BeginSystemTableScan(void);
+extern void EndSystemTableScan(void);
+extern void ResetSystemTableScanState(void);
+extern bool IsSystemTableScanInProgress(void);
+
#endif /* XACT_H */
--
2.52.0
^ permalink raw reply [nested|flat] 18+ messages in thread
* Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641)
2026-10-02 07:33 Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-07 10:08 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Zhijie Hou <houzhijie22@gmail.com>
2026-10-08 02:36 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-08 05:35 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Jiří Kavalík <jiri.kavalik@comgate.cz>
2026-10-09 02:56 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-26 13:25 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-27 03:36 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Peter Smith <smithpb2250@gmail.com>
2026-10-27 06:42 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
@ 2026-10-28 08:41 ` Chao Li <li.evan.chao@gmail.com>
2026-10-29 04:52 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
0 siblings, 1 reply; 18+ messages in thread
From: Chao Li @ 2026-10-28 08:41 UTC (permalink / raw)
To: Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; +Cc: Peter Smith <smithpb2250@gmail.com>; Jiří Kavalík <jiri.kavalik@comgate.cz>; Zhijie Hou <houzhijie22@gmail.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
> On Oct 27, 2026, at 14:42, Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com> wrote:
>
> Hi Peter,
>
>> Since you are refactoring this flag anyway, would it be better to try
>> to eliminate the global-variable completely? e.g. expose the same
>> information by some accessor function.
>>
>> -extern PGDLLIMPORT bool bsysscan;
>> +extern PGDLLIMPORT int sysscan_depth;
>
> Good point, it can be removed by providing few functions. v5 did the same.
> We need to keep bsysscan anyway for back branches, so the patch shape for them
> is slightly different, see v5-PG18.
>
> Best regards,
> Hayato Kuroda
> FUJITSU LIMITED
>
> <v5-0001-Track-the-depth-of-scans-of-system-tables.patch><nocfbot-v5-PG18-0001-Track-the-depth-of-scans-of-system-tables.patch><nocfbot-v5-PG19-0001-Track-the-depth-of-scans-of-system-tables.patch>
Hi Hayato,
I agree with the solution. However, I have a minor concern about the naming. BeginSystemTableScan() and EndSystemTableScan() sound like functions that actually start and end system table scans, whereas they only track sysscan_depth. How about renaming them to TrackSystemTableScanStart() and TrackSystemTableScanEnd() to better reflect their purpose?
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
^ permalink raw reply [nested|flat] 18+ messages in thread
* RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641)
2026-10-02 07:33 Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-07 10:08 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Zhijie Hou <houzhijie22@gmail.com>
2026-10-08 02:36 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-08 05:35 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Jiří Kavalík <jiri.kavalik@comgate.cz>
2026-10-09 02:56 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-26 13:25 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-27 03:36 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Peter Smith <smithpb2250@gmail.com>
2026-10-27 06:42 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-28 08:41 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Chao Li <li.evan.chao@gmail.com>
@ 2026-10-29 04:52 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-11-04 12:38 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Shlok Kyal <shlok.kyal.oss@gmail.com>
0 siblings, 1 reply; 18+ messages in thread
From: Hayato Kuroda (Fujitsu) @ 2026-10-29 04:52 UTC (permalink / raw)
To: Chao Li <li.evan.chao@gmail.com>; +Cc: Peter Smith <smithpb2250@gmail.com>; Jiří Kavalík <jiri.kavalik@comgate.cz>; Zhijie Hou <houzhijie22@gmail.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Hi Chao,
> I agree with the solution. However, I have a minor concern about the naming.
> BeginSystemTableScan() and EndSystemTableScan() sound like functions that
> actually start and end system table scans, whereas they only track sysscan_depth.
> How about renaming them to TrackSystemTableScanStart() and
> TrackSystemTableScanEnd() to better reflect their purpose?
+1, PSA new version.
Best regards,
Hayato Kuroda
FUJITSU LIMITED
Attachments: download all (7 files, .tar.gz)
nocfbot-v6-PG14-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 8.6K) download
From 274d4ad93a143dd4db6a8c173c7bf45fb73ddc39 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v6-PG14] Track the depth of scans of system tables
---
contrib/test_decoding/expected/stream.out | 43 +++++++++++++++++++-
contrib/test_decoding/sql/stream.sql | 33 ++++++++++++++-
src/backend/access/index/genam.c | 30 ++------------
src/backend/access/transam/xact.c | 49 +++++++++++++++++++++++
src/backend/replication/logical/logical.c | 3 +-
src/include/access/xact.h | 4 ++
6 files changed, 133 insertions(+), 29 deletions(-)
diff --git a/contrib/test_decoding/expected/stream.out b/contrib/test_decoding/expected/stream.out
index 0f21dcb8e0e..d14dd7524b7 100644
--- a/contrib/test_decoding/expected/stream.out
+++ b/contrib/test_decoding/expected/stream.out
@@ -106,7 +106,48 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL,NULL, 'incl
committing streamed transaction
(17 rows)
-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);
+ ALTER TABLE nested_sys_scan_test ALTER COLUMN checked_value set storage external;
+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.
+BEGIN;
+INSERT INTO nested_sys_scan_test SELECT repeat('x', 6000) FROM generate_series(1, 10);
+CHECKPOINT;
+SELECT count(*) > 0 AS streamed FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+ streamed
+----------
+ t
+(1 row)
+
+COMMIT;
+SELECT count(*) > 0 AS consumed FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+ consumed
+----------
+ 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 4feec62972a..93c72f12cf6 100644
--- a/contrib/test_decoding/sql/stream.sql
+++ b/contrib/test_decoding/sql/stream.sql
@@ -44,5 +44,36 @@ toasted-123456789012345678901234567890123456789012345678901234567890123456789012
SELECT data 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);
+ ALTER TABLE nested_sys_scan_test ALTER COLUMN checked_value set storage external;
+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.
+BEGIN;
+INSERT INTO nested_sys_scan_test SELECT repeat('x', 6000) FROM generate_series(1, 10);
+CHECKPOINT;
+SELECT count(*) > 0 AS streamed FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+COMMIT;
+SELECT count(*) > 0 AS consumed 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 e3d0fd95f7d..ee9fa95273f 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -457,13 +457,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -612,12 +606,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
@@ -700,13 +689,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -753,12 +736,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index ef0fe7a5361..b4ba9d51f77 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -94,6 +94,18 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
+
+/*
+ * Number of nested systable scans while CheckXidAlive is valid. A counter is
+ * needed because systable scans can be nested. Transaction abort resets it,
+ * since ERROR can bypass the matching systable_endscan().
+ */
+static int bsysscan_depth = 0;
+
+/*
+ * Indicates whether we are currently in a system table scan. Retained for the
+ * backwards-compatibility.
+ */
bool bsysscan = false;
/*
@@ -6150,6 +6162,43 @@ xact_redo(XLogReaderState *record)
elog(PANIC, "xact_redo: unknown op code %u", info);
}
+/*
+ * Record entry into a system table scan while CheckXidAlive is valid.
+ */
+void
+TrackSystemTableScanStart(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ bsysscan_depth++;
+ bsysscan = true;
+}
+
+/*
+ * Record normal exit from a tracked system table scan.
+ */
+void
+TrackSystemTableScanEnd(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(bsysscan_depth > 0);
+ bsysscan_depth--;
+ bsysscan = (bsysscan_depth > 0);
+}
+
+/*
+ * Reset the system table scan state during transaction abort.
+ */
+void
+ResetSystemTableScanState(void)
+{
+ bsysscan_depth = 0;
+ bsysscan = false;
+}
+
/*
* IsSubTransactionAssignmentPending
*
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index a7efca27512..5dd01140e37 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 "fmgr.h"
@@ -1896,7 +1897,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSystemTableScanState();
}
/*
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index 5af78bd0dc7..c513efd353f 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -480,4 +480,8 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
+extern void TrackSystemTableScanStart(void);
+extern void TrackSystemTableScanEnd(void);
+extern void ResetSystemTableScanState(void);
+
#endif /* XACT_H */
--
2.52.0
nocfbot-v6-PG15-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 8.6K) download
From 5aa4835f7f077147e8ff81b62b902c5966e3045d Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v6-PG15] Track the depth of scans of system tables
---
contrib/test_decoding/expected/stream.out | 43 +++++++++++++++++++-
contrib/test_decoding/sql/stream.sql | 33 ++++++++++++++-
src/backend/access/index/genam.c | 30 ++------------
src/backend/access/transam/xact.c | 49 +++++++++++++++++++++++
src/backend/replication/logical/logical.c | 3 +-
src/include/access/xact.h | 4 ++
6 files changed, 133 insertions(+), 29 deletions(-)
diff --git a/contrib/test_decoding/expected/stream.out b/contrib/test_decoding/expected/stream.out
index 0f21dcb8e0e..d14dd7524b7 100644
--- a/contrib/test_decoding/expected/stream.out
+++ b/contrib/test_decoding/expected/stream.out
@@ -106,7 +106,48 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL,NULL, 'incl
committing streamed transaction
(17 rows)
-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);
+ ALTER TABLE nested_sys_scan_test ALTER COLUMN checked_value set storage external;
+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.
+BEGIN;
+INSERT INTO nested_sys_scan_test SELECT repeat('x', 6000) FROM generate_series(1, 10);
+CHECKPOINT;
+SELECT count(*) > 0 AS streamed FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+ streamed
+----------
+ t
+(1 row)
+
+COMMIT;
+SELECT count(*) > 0 AS consumed FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+ consumed
+----------
+ 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 4feec62972a..93c72f12cf6 100644
--- a/contrib/test_decoding/sql/stream.sql
+++ b/contrib/test_decoding/sql/stream.sql
@@ -44,5 +44,36 @@ toasted-123456789012345678901234567890123456789012345678901234567890123456789012
SELECT data 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);
+ ALTER TABLE nested_sys_scan_test ALTER COLUMN checked_value set storage external;
+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.
+BEGIN;
+INSERT INTO nested_sys_scan_test SELECT repeat('x', 6000) FROM generate_series(1, 10);
+CHECKPOINT;
+SELECT count(*) > 0 AS streamed FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+COMMIT;
+SELECT count(*) > 0 AS consumed 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 6dd05e01be8..90d5bb636e4 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -460,13 +460,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -615,12 +609,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
@@ -703,13 +692,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -756,12 +739,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index ad5d06f4499..921c6117a38 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -96,6 +96,18 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
+
+/*
+ * Number of nested systable scans while CheckXidAlive is valid. A counter is
+ * needed because systable scans can be nested. Transaction abort resets it,
+ * since ERROR can bypass the matching systable_endscan().
+ */
+static int bsysscan_depth = 0;
+
+/*
+ * Indicates whether we are currently in a system table scan. Retained for the
+ * backwards-compatibility.
+ */
bool bsysscan = false;
/*
@@ -6260,3 +6272,40 @@ xact_redo(XLogReaderState *record)
else
elog(PANIC, "xact_redo: unknown op code %u", info);
}
+
+/*
+ * Record entry into a system table scan while CheckXidAlive is valid.
+ */
+void
+TrackSystemTableScanStart(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ bsysscan_depth++;
+ bsysscan = true;
+}
+
+/*
+ * Record normal exit from a tracked system table scan.
+ */
+void
+TrackSystemTableScanEnd(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(bsysscan_depth > 0);
+ bsysscan_depth--;
+ bsysscan = (bsysscan_depth > 0);
+}
+
+/*
+ * Reset the system table scan state during transaction abort.
+ */
+void
+ResetSystemTableScanState(void)
+{
+ bsysscan_depth = 0;
+ bsysscan = false;
+}
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index d796abbde78..9c57b596c3b 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 "fmgr.h"
@@ -1931,7 +1932,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSystemTableScanState();
}
/*
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index 8d46a781bbd..85d0c89919e 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -527,4 +527,8 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
+extern void TrackSystemTableScanStart(void);
+extern void TrackSystemTableScanEnd(void);
+extern void ResetSystemTableScanState(void);
+
#endif /* XACT_H */
--
2.52.0
nocfbot-v6-PG16-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 8.5K) download
From 5a0987fe26b43ba7737dc59a9d4eb12a0a71efb7 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v6-PG16] Track the depth of scans of system tables
---
contrib/test_decoding/expected/stream.out | 44 +++++++++++++++++++-
contrib/test_decoding/sql/stream.sql | 34 +++++++++++++++-
src/backend/access/index/genam.c | 30 ++------------
src/backend/access/transam/xact.c | 49 +++++++++++++++++++++++
src/backend/replication/logical/logical.c | 3 +-
src/include/access/xact.h | 4 ++
6 files changed, 135 insertions(+), 29 deletions(-)
diff --git a/contrib/test_decoding/expected/stream.out b/contrib/test_decoding/expected/stream.out
index 0f21dcb8e0e..899c4878195 100644
--- a/contrib/test_decoding/expected/stream.out
+++ b/contrib/test_decoding/expected/stream.out
@@ -106,7 +106,49 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL,NULL, 'incl
committing streamed transaction
(17 rows)
-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 consumed FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+ consumed
+----------
+ 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 4feec62972a..1e8a34d43fb 100644
--- a/contrib/test_decoding/sql/stream.sql
+++ b/contrib/test_decoding/sql/stream.sql
@@ -44,5 +44,37 @@ toasted-123456789012345678901234567890123456789012345678901234567890123456789012
SELECT data 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 consumed 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 67246406cff..40cd418d5ca 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -460,13 +460,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -615,12 +609,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
@@ -703,13 +692,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -756,12 +739,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index e876adf71ce..ff1d7219bda 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -97,6 +97,18 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
+
+/*
+ * Number of nested systable scans while CheckXidAlive is valid. A counter is
+ * needed because systable scans can be nested. Transaction abort resets it,
+ * since ERROR can bypass the matching systable_endscan().
+ */
+static int bsysscan_depth = 0;
+
+/*
+ * Indicates whether we are currently in a system table scan. Retained for the
+ * backwards-compatibility.
+ */
bool bsysscan = false;
/*
@@ -6279,3 +6291,40 @@ xact_redo(XLogReaderState *record)
else
elog(PANIC, "xact_redo: unknown op code %u", info);
}
+
+/*
+ * Record entry into a system table scan while CheckXidAlive is valid.
+ */
+void
+TrackSystemTableScanStart(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ bsysscan_depth++;
+ bsysscan = true;
+}
+
+/*
+ * Record normal exit from a tracked system table scan.
+ */
+void
+TrackSystemTableScanEnd(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(bsysscan_depth > 0);
+ bsysscan_depth--;
+ bsysscan = (bsysscan_depth > 0);
+}
+
+/*
+ * Reset the system table scan state during transaction abort.
+ */
+void
+ResetSystemTableScanState(void)
+{
+ bsysscan_depth = 0;
+ bsysscan = false;
+}
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index ce7f7143070..ca7c2cd5ffe 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 "fmgr.h"
@@ -2008,7 +2009,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSystemTableScanState();
}
/*
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index 7d3b9446e62..9a3645ecddc 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -527,4 +527,8 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
+extern void TrackSystemTableScanStart(void);
+extern void TrackSystemTableScanEnd(void);
+extern void ResetSystemTableScanState(void);
+
#endif /* XACT_H */
--
2.52.0
nocfbot-v6-PG17-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 8.6K) download
From f70754fe071efd8ce490b0dd012d9a32a214f296 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v6-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 | 31 ++------------
src/backend/access/transam/xact.c | 49 +++++++++++++++++++++++
src/backend/replication/logical/logical.c | 3 +-
src/include/access/xact.h | 4 ++
6 files changed, 134 insertions(+), 30 deletions(-)
diff --git a/contrib/test_decoding/expected/stream.out b/contrib/test_decoding/expected/stream.out
index a76f77601e2..95dd8b1b68f 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 consumed FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+ consumed
+----------
+ 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..0958d95e41e 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 consumed 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..ca35f0163be 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -37,7 +37,6 @@
#include "utils/ruleutils.h"
#include "utils/snapmgr.h"
-
/* ----------------------------------------------------------------
* general access method routines
*
@@ -459,13 +458,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -614,12 +607,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
@@ -702,13 +690,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -755,12 +737,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 96b9ff52728..441ba1b7b86 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -96,6 +96,18 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
+
+/*
+ * Number of nested systable scans while CheckXidAlive is valid. A counter is
+ * needed because systable scans can be nested. Transaction abort resets it,
+ * since ERROR can bypass the matching systable_endscan().
+ */
+static int bsysscan_depth = 0;
+
+/*
+ * Indicates whether we are currently in a system table scan. Retained for the
+ * backwards-compatibility.
+ */
bool bsysscan = false;
/*
@@ -6401,3 +6413,40 @@ xact_redo(XLogReaderState *record)
else
elog(PANIC, "xact_redo: unknown op code %u", info);
}
+
+/*
+ * Record entry into a system table scan while CheckXidAlive is valid.
+ */
+void
+TrackSystemTableScanStart(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ bsysscan_depth++;
+ bsysscan = true;
+}
+
+/*
+ * Record normal exit from a tracked system table scan.
+ */
+void
+TrackSystemTableScanEnd(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(bsysscan_depth > 0);
+ bsysscan_depth--;
+ bsysscan = (bsysscan_depth > 0);
+}
+
+/*
+ * Reset the system table scan state during transaction abort.
+ */
+void
+ResetSystemTableScanState(void)
+{
+ bsysscan_depth = 0;
+ bsysscan = false;
+}
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index f79557c7261..40406dca4b5 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;
+ ResetSystemTableScanState();
}
/*
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index 6d4439f0524..2035d5e4338 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -527,4 +527,8 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
+extern void TrackSystemTableScanStart(void);
+extern void TrackSystemTableScanEnd(void);
+extern void ResetSystemTableScanState(void);
+
#endif /* XACT_H */
--
2.52.0
nocfbot-v6-PG18-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 9.1K) download
From f63e2423c016f769b262fe68c9532f014db30e61 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v6-PG18] 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 | 31 ++------------
src/backend/access/transam/xact.c | 49 +++++++++++++++++++++++
src/backend/replication/logical/logical.c | 3 +-
src/include/access/xact.h | 4 ++
6 files changed, 134 insertions(+), 30 deletions(-)
diff --git a/contrib/test_decoding/expected/stream.out b/contrib/test_decoding/expected/stream.out
index 0ec5c933610..ce64e361c26 100644
--- a/contrib/test_decoding/expected/stream.out
+++ b/contrib/test_decoding/expected/stream.out
@@ -134,6 +134,48 @@ 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 (
+ 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 consumed FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+ consumed
+----------
+ t
+(1 row)
+
-- bug #19616
--
-- An aborted top-level transaction that is discarded at eviction must not
@@ -157,7 +199,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..48b54eee1aa 100644
--- a/contrib/test_decoding/sql/stream.sql
+++ b/contrib/test_decoding/sql/stream.sql
@@ -65,6 +65,37 @@ 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 (
+ 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 consumed 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 +113,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..4927dac389e 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -37,7 +37,6 @@
#include "utils/ruleutils.h"
#include "utils/snapmgr.h"
-
/* ----------------------------------------------------------------
* general access method routines
*
@@ -468,13 +467,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -619,12 +612,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
@@ -714,13 +702,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -767,12 +749,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index d81e749b031..2e025ac5780 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -98,6 +98,18 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
+
+/*
+ * Number of nested systable scans while CheckXidAlive is valid. A counter is
+ * needed because systable scans can be nested. Transaction abort resets it,
+ * since ERROR can bypass the matching systable_endscan().
+ */
+static int bsysscan_depth = 0;
+
+/*
+ * Indicates whether we are currently in a system table scan. Retained for the
+ * backwards-compatibility.
+ */
bool bsysscan = false;
/*
@@ -6462,3 +6474,40 @@ xact_redo(XLogReaderState *record)
else
elog(PANIC, "xact_redo: unknown op code %u", info);
}
+
+/*
+ * Record entry into a system table scan while CheckXidAlive is valid.
+ */
+void
+TrackSystemTableScanStart(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ bsysscan_depth++;
+ bsysscan = true;
+}
+
+/*
+ * Record normal exit from a tracked system table scan.
+ */
+void
+TrackSystemTableScanEnd(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(bsysscan_depth > 0);
+ bsysscan_depth--;
+ bsysscan = (bsysscan_depth > 0);
+}
+
+/*
+ * Reset the system table scan state during transaction abort.
+ */
+void
+ResetSystemTableScanState(void)
+{
+ bsysscan_depth = 0;
+ bsysscan = false;
+}
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index 4d3a39f4cd7..5ff58acbdda 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;
+ ResetSystemTableScanState();
}
/*
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index b2bc10ee041..0a299b583d9 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -533,4 +533,8 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
+extern void TrackSystemTableScanStart(void);
+extern void TrackSystemTableScanEnd(void);
+extern void ResetSystemTableScanState(void);
+
#endif /* XACT_H */
--
2.52.0
nocfbot-v6-PG19-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 7.3K) download
From 188c9337c9f150e364656ceb474df833b39a40b5 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v6^PG19] Track the depth of scans of system tables
---
src/backend/access/index/genam.c | 30 ++-----------
src/backend/access/transam/xact.c | 51 ++++++++++++++++++++++-
src/backend/replication/logical/logical.c | 2 +-
src/include/access/tableam.h | 21 ++++++----
src/include/access/xact.h | 6 ++-
5 files changed, 72 insertions(+), 38 deletions(-)
diff --git a/src/backend/access/index/genam.c b/src/backend/access/index/genam.c
index 1408989c568..556b3aedc41 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -420,13 +420,7 @@ systable_beginscan(Relation heapRelation,
sysscan->snapshot = 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;
+ TrackSystemTableScanStart();
if (irel)
{
@@ -620,12 +614,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
@@ -708,13 +697,7 @@ systable_beginscan_ordered(Relation heapRelation,
elog(ERROR, "column is not in index");
}
- /*
- * 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;
+ TrackSystemTableScanStart();
sysscan->iscan = index_beginscan(heapRelation, indexRelation,
snapshot, NULL, nkeys, 0,
@@ -769,12 +752,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 92319290cf7..69d55651b92 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -100,7 +100,13 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
-bool bsysscan = false;
+
+/*
+ * Number of nested systable scans while CheckXidAlive is valid. A counter is
+ * needed because systable scans can be nested. Transaction abort resets it,
+ * since ERROR can bypass the matching systable_endscan().
+ */
+static int sysscan_depth = 0;
/*
* When running as a parallel worker, we place only a single
@@ -6532,3 +6538,46 @@ xact_redo(XLogReaderState *record)
else
elog(PANIC, "xact_redo: unknown op code %u", info);
}
+
+/*
+ * Record entry into a system table scan while CheckXidAlive is valid.
+ */
+void
+TrackSystemTableScanStart(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ sysscan_depth++;
+}
+
+/*
+ * Record normal exit from a tracked system table scan.
+ */
+void
+TrackSystemTableScanEnd(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(IsSystemTableScanInProgress());
+ sysscan_depth--;
+}
+
+/*
+ * Reset the system table scan state during transaction abort.
+ */
+void
+ResetSystemTableScanState(void)
+{
+ sysscan_depth = 0;
+}
+
+/*
+ * Return whether any tracked system table scan is in progress.
+ */
+bool
+IsSystemTableScanInProgress(void)
+{
+ return sysscan_depth > 0;
+}
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index 98e5f1dd8f9..f579fe934c7 100644
--- a/src/backend/replication/logical/logical.c
+++ b/src/backend/replication/logical/logical.c
@@ -2030,7 +2030,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSystemTableScanState();
}
/*
diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h
index ff03a2b816f..376def9461c 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -924,10 +924,11 @@ table_beginscan_common(Relation rel, Snapshot snapshot, int nkeys,
/*
* We don't allow scans to be started while CheckXidAlive is set, except
- * via systable_beginscan() et al. See detailed comments in xact.c where
- * these variables are declared.
+ * via systable_beginscan() et al. See comments for CheckXidAlive in
+ * xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "scan started during logical decoding");
return rel->rd_tableam->scan_begin(rel, snapshot, nkeys, key, pscan, flags);
@@ -1249,10 +1250,11 @@ table_index_fetch_begin(Relation rel, uint32 flags)
/*
* We don't allow scans to be started while CheckXidAlive is set, except
- * via systable_beginscan() et al. See detailed comments in xact.c where
- * these variables are declared.
+ * via systable_beginscan() et al. See comments for CheckXidAlive in
+ * xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "scan started during logical decoding");
return rel->rd_tableam->index_fetch_begin(rel, flags);
@@ -1348,10 +1350,11 @@ table_tuple_fetch_row_version(Relation rel,
{
/*
* We don't expect direct calls to table_tuple_fetch_row_version with
- * valid CheckXidAlive for catalog or regular tables. See detailed
- * comments in xact.c where these variables are declared.
+ * valid CheckXidAlive for catalog or regular tables. See comments for
+ * CheckXidAlive in xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "unexpected table_tuple_fetch_row_version call during logical decoding");
return rel->rd_tableam->tuple_fetch_row_version(rel, tid, snapshot, slot);
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index a8cbdf247c8..43166bd28b8 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -85,7 +85,6 @@ extern PGDLLIMPORT int synchronous_commit;
/* used during logical streaming of a transaction */
extern PGDLLIMPORT TransactionId CheckXidAlive;
-extern PGDLLIMPORT bool bsysscan;
/*
* Miscellaneous flag bits to record events which occur on the top level
@@ -535,4 +534,9 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
+extern void TrackSystemTableScanStart(void);
+extern void TrackSystemTableScanEnd(void);
+extern void ResetSystemTableScanState(void);
+extern bool IsSystemTableScanInProgress(void);
+
#endif /* XACT_H */
--
2.52.0
v6-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 7.9K) download
From b8d077bf13277b2b1c528468644ba82cd5747802 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v6] Track the depth of scans of system tables
---
src/backend/access/index/genam.c | 30 ++-----------
src/backend/access/transam/xact.c | 51 ++++++++++++++++++++++-
src/backend/replication/logical/logical.c | 2 +-
src/include/access/tableam.h | 28 +++++++------
src/include/access/xact.h | 6 ++-
5 files changed, 76 insertions(+), 41 deletions(-)
diff --git a/src/backend/access/index/genam.c b/src/backend/access/index/genam.c
index 98ac5bc79c9..dff54d30c1c 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -432,13 +432,7 @@ systable_beginscan(Relation heapRelation,
sysscan->snapshot = 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;
+ TrackSystemTableScanStart();
if (irel)
{
@@ -633,12 +627,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
@@ -721,13 +710,7 @@ systable_beginscan_ordered(Relation heapRelation,
elog(ERROR, "column is not in index");
}
- /*
- * 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;
+ TrackSystemTableScanStart();
sysscan->iscan = index_beginscan(heapRelation, indexRelation, false,
snapshot, NULL, nkeys, 0,
@@ -782,12 +765,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 0121a0c8fa0..cae52f77afb 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -100,7 +100,13 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
-bool bsysscan = false;
+
+/*
+ * Number of nested systable scans while CheckXidAlive is valid. A counter is
+ * needed because systable scans can be nested. Transaction abort resets it,
+ * since ERROR can bypass the matching systable_endscan().
+ */
+static int sysscan_depth = 0;
/*
* When running as a parallel worker, we place only a single
@@ -6531,3 +6537,46 @@ xact_redo(XLogReaderState *record)
else
elog(PANIC, "xact_redo: unknown op code %u", info);
}
+
+/*
+ * Record entry into a system table scan while CheckXidAlive is valid.
+ */
+void
+TrackSystemTableScanStart(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ sysscan_depth++;
+}
+
+/*
+ * Record normal exit from a tracked system table scan.
+ */
+void
+TrackSystemTableScanEnd(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(IsSystemTableScanInProgress());
+ sysscan_depth--;
+}
+
+/*
+ * Reset the system table scan state during transaction abort.
+ */
+void
+ResetSystemTableScanState(void)
+{
+ sysscan_depth = 0;
+}
+
+/*
+ * Return whether any tracked system table scan is in progress.
+ */
+bool
+IsSystemTableScanInProgress(void)
+{
+ return sysscan_depth > 0;
+}
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index 98e5f1dd8f9..f579fe934c7 100644
--- a/src/backend/replication/logical/logical.c
+++ b/src/backend/replication/logical/logical.c
@@ -2030,7 +2030,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSystemTableScanState();
}
/*
diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h
index ea3f2a6be99..f66faaacf9b 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -924,10 +924,11 @@ table_beginscan_common(Relation rel, Snapshot snapshot, int nkeys,
/*
* We don't allow scans to be started while CheckXidAlive is set, except
- * via systable_beginscan() et al. See detailed comments in xact.c where
- * these variables are declared.
+ * via systable_beginscan() et al. See comments for CheckXidAlive in
+ * xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "scan started during logical decoding");
return rel->rd_tableam->scan_begin(rel, snapshot, nkeys, key, pscan, flags);
@@ -1248,10 +1249,11 @@ table_index_scan_begin(IndexScanDesc scan, uint32 flags)
/*
* We don't allow scans to be started while CheckXidAlive is set, except
- * via systable_beginscan() et al. See detailed comments in xact.c where
- * these variables are declared.
+ * via systable_beginscan() et al. See comments for CheckXidAlive in
+ * xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "scan started during logical decoding");
scan->heapRelation->rd_tableam->index_scan_begin(scan, flags);
@@ -1341,10 +1343,11 @@ table_fetch_tid(Relation rel,
{
/*
* We don't expect direct calls to table_fetch_tid with valid
- * CheckXidAlive for catalog or regular tables. See detailed comments in
- * xact.c where these variables are declared.
+ * CheckXidAlive for catalog or regular tables. See comments for
+ * CheckXidAlive in xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "unexpected table_fetch_tid call during logical decoding");
return rel->rd_tableam->fetch_tid(rel, tid, snapshot, all_dead);
@@ -1366,10 +1369,11 @@ table_tuple_fetch_row_version(Relation rel,
{
/*
* We don't expect direct calls to table_tuple_fetch_row_version with
- * valid CheckXidAlive for catalog or regular tables. See detailed
- * comments in xact.c where these variables are declared.
+ * valid CheckXidAlive for catalog or regular tables. See comments for
+ * CheckXidAlive in xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "unexpected table_tuple_fetch_row_version call during logical decoding");
return rel->rd_tableam->tuple_fetch_row_version(rel, tid, snapshot, slot);
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index a8cbdf247c8..43166bd28b8 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -85,7 +85,6 @@ extern PGDLLIMPORT int synchronous_commit;
/* used during logical streaming of a transaction */
extern PGDLLIMPORT TransactionId CheckXidAlive;
-extern PGDLLIMPORT bool bsysscan;
/*
* Miscellaneous flag bits to record events which occur on the top level
@@ -535,4 +534,9 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
+extern void TrackSystemTableScanStart(void);
+extern void TrackSystemTableScanEnd(void);
+extern void ResetSystemTableScanState(void);
+extern bool IsSystemTableScanInProgress(void);
+
#endif /* XACT_H */
--
2.52.0
^ permalink raw reply [nested|flat] 18+ messages in thread
* Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641)
2026-10-02 07:33 Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-07 10:08 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Zhijie Hou <houzhijie22@gmail.com>
2026-10-08 02:36 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-08 05:35 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Jiří Kavalík <jiri.kavalik@comgate.cz>
2026-10-09 02:56 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-26 13:25 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-27 03:36 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Peter Smith <smithpb2250@gmail.com>
2026-10-27 06:42 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-28 08:41 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Chao Li <li.evan.chao@gmail.com>
2026-10-29 04:52 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
@ 2026-11-04 12:38 ` Shlok Kyal <shlok.kyal.oss@gmail.com>
2026-11-04 13:24 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
0 siblings, 1 reply; 18+ messages in thread
From: Shlok Kyal @ 2026-11-04 12:38 UTC (permalink / raw)
To: Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; +Cc: Chao Li <li.evan.chao@gmail.com>; Peter Smith <smithpb2250@gmail.com>; Jiří Kavalík <jiri.kavalik@comgate.cz>; Zhijie Hou <houzhijie22@gmail.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
On Thu, 29 Oct 2026 at 10:22, Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:
>
> Hi Chao,
>
> > I agree with the solution. However, I have a minor concern about the naming.
> > BeginSystemTableScan() and EndSystemTableScan() sound like functions that
> > actually start and end system table scans, whereas they only track sysscan_depth.
> > How about renaming them to TrackSystemTableScanStart() and
> > TrackSystemTableScanEnd() to better reflect their purpose?
>
> +1, PSA new version.
>
Hi Kuroda-san,
I have reviewed and tested the patches.
I am able to reproduce the issue in PG14 to PG18 using the test added
in stream.sql and confirm that the corresponding patches address the
issue.
Patches is applying cleanly on all branches and passed 'make check-world'
I have some minor comments:
1. For all patches: Most of the comments for function in the file
'xact.c' is like of format:
/*
* Function name
*
* Function Description
*/
Maybe we should follow the same for the functions added by these patches?
2. For patches for PG14 to PG18: In logical.c we can remove:
+#include "access/genam.h"
#include "access/xact.h"
In logical.c we only introduced "ResetSystemTableScanState();" and
this function which was declared 'access/xact.h'
logical.c already includes access/xact.h
3. For patches for PG 18 and PG 17: I noticed a unrelated line deletion change:
#include "utils/snapmgr.h"
-
/* ----------------------------------------------------------------
Was it intentional?
4. For patches for PG 14 and PG 15:
+ ALTER TABLE nested_sys_scan_test ALTER COLUMN checked_value set
storage external;
I think we should capitalize 'set storage external;'
Thanks,
Shlok Kyal
^ permalink raw reply [nested|flat] 18+ messages in thread
* RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641)
2026-10-02 07:33 Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-07 10:08 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Zhijie Hou <houzhijie22@gmail.com>
2026-10-08 02:36 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-08 05:35 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Jiří Kavalík <jiri.kavalik@comgate.cz>
2026-10-09 02:56 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-26 13:25 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-27 03:36 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Peter Smith <smithpb2250@gmail.com>
2026-10-27 06:42 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-28 08:41 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Chao Li <li.evan.chao@gmail.com>
2026-10-29 04:52 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-11-04 12:38 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Shlok Kyal <shlok.kyal.oss@gmail.com>
@ 2026-11-04 13:24 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-11-05 10:43 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Shlok Kyal <shlok.kyal.oss@gmail.com>
0 siblings, 1 reply; 18+ messages in thread
From: Hayato Kuroda (Fujitsu) @ 2026-11-04 13:24 UTC (permalink / raw)
To: Shlok Kyal <shlok.kyal.oss@gmail.com>; +Cc: Chao Li <li.evan.chao@gmail.com>; Peter Smith <smithpb2250@gmail.com>; Jiří Kavalík <jiri.kavalik@comgate.cz>; Zhijie Hou <houzhijie22@gmail.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Hi Shlok,
Thanks for the review!
> 1. For all patches: Most of the comments for function in the file
> 'xact.c' is like of format:
> /*
> * Function name
> *
> * Function Description
> */
> Maybe we should follow the same for the functions added by these patches?
I do not have strong opinions for it, but added accordingly.
> 2. For patches for PG14 to PG18: In logical.c we can remove:
> +#include "access/genam.h"
> #include "access/xact.h"
>
> In logical.c we only introduced "ResetSystemTableScanState();" and
> this function which was declared 'access/xact.h'
> logical.c already includes access/xact.h
Fixed. It was included because functions are initially declared in genam.h.
> 3. For patches for PG 18 and PG 17: I noticed a unrelated line deletion change:
> #include "utils/snapmgr.h"
>
> -
> /* ----------------------------------------------------------------
>
> Was it intentional?
No, removed.
> 4. For patches for PG 14 and PG 15:
> + ALTER TABLE nested_sys_scan_test ALTER COLUMN checked_value set
> storage external;
> I think we should capitalize 'set storage external;'
+1. Fixed.
PSA new patch set.
Best regards,
Hayato Kuroda
FUJITSU LIMITED
Attachments: download all (7 files, .tar.gz)
nocfbot-v7-PG14-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 8.6K) download
From 02efa07ed503ac01accd5ec1c47aebb832b68f8d Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v7-PG14] Track the depth of scans of system tables
---
contrib/test_decoding/expected/stream.out | 43 +++++++++++++++++-
contrib/test_decoding/sql/stream.sql | 33 +++++++++++++-
src/backend/access/index/genam.c | 30 ++-----------
src/backend/access/transam/xact.c | 55 +++++++++++++++++++++++
src/backend/replication/logical/logical.c | 2 +-
src/include/access/xact.h | 4 ++
6 files changed, 138 insertions(+), 29 deletions(-)
diff --git a/contrib/test_decoding/expected/stream.out b/contrib/test_decoding/expected/stream.out
index 0f21dcb8e0e..62b3237eb8d 100644
--- a/contrib/test_decoding/expected/stream.out
+++ b/contrib/test_decoding/expected/stream.out
@@ -106,7 +106,48 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL,NULL, 'incl
committing streamed transaction
(17 rows)
-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);
+ ALTER TABLE nested_sys_scan_test ALTER COLUMN checked_value SET STORAGE EXTERNAL;
+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.
+BEGIN;
+INSERT INTO nested_sys_scan_test SELECT repeat('x', 6000) FROM generate_series(1, 10);
+CHECKPOINT;
+SELECT count(*) > 0 AS streamed FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+ streamed
+----------
+ t
+(1 row)
+
+COMMIT;
+SELECT count(*) > 0 AS consumed FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+ consumed
+----------
+ 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 4feec62972a..1f050a585d8 100644
--- a/contrib/test_decoding/sql/stream.sql
+++ b/contrib/test_decoding/sql/stream.sql
@@ -44,5 +44,36 @@ toasted-123456789012345678901234567890123456789012345678901234567890123456789012
SELECT data 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);
+ ALTER TABLE nested_sys_scan_test ALTER COLUMN checked_value SET STORAGE EXTERNAL;
+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.
+BEGIN;
+INSERT INTO nested_sys_scan_test SELECT repeat('x', 6000) FROM generate_series(1, 10);
+CHECKPOINT;
+SELECT count(*) > 0 AS streamed FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+COMMIT;
+SELECT count(*) > 0 AS consumed 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 e3d0fd95f7d..ee9fa95273f 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -457,13 +457,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -612,12 +606,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
@@ -700,13 +689,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -753,12 +736,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 35ac4e65438..d90b51beb21 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -94,6 +94,18 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
+
+/*
+ * Number of nested systable scans while CheckXidAlive is valid. A counter is
+ * needed because systable scans can be nested. Transaction abort resets it,
+ * since ERROR can bypass the matching systable_endscan().
+ */
+static int bsysscan_depth = 0;
+
+/*
+ * Indicates whether we are currently in a system table scan. Retained for the
+ * backwards-compatibility.
+ */
bool bsysscan = false;
/*
@@ -6153,6 +6165,49 @@ xact_redo(XLogReaderState *record)
elog(PANIC, "xact_redo: unknown op code %u", info);
}
+/*
+ * TrackSystemTableScanStart
+ *
+ * Record entry into a system table scan while CheckXidAlive is valid.
+ */
+void
+TrackSystemTableScanStart(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ bsysscan_depth++;
+ bsysscan = true;
+}
+
+/*
+ * TrackSystemTableScanEnd
+ *
+ * Record normal exit from a tracked system table scan.
+ */
+void
+TrackSystemTableScanEnd(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(bsysscan_depth > 0);
+ bsysscan_depth--;
+ bsysscan = (bsysscan_depth > 0);
+}
+
+/*
+ * ResetSystemTableScanState
+ *
+ * Reset the system table scan state during transaction abort.
+ */
+void
+ResetSystemTableScanState(void)
+{
+ bsysscan_depth = 0;
+ bsysscan = false;
+}
+
/*
* IsSubTransactionAssignmentPending
*
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index a7efca27512..f8a2d4c3a40 100644
--- a/src/backend/replication/logical/logical.c
+++ b/src/backend/replication/logical/logical.c
@@ -1896,7 +1896,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSystemTableScanState();
}
/*
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index 5af78bd0dc7..c513efd353f 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -480,4 +480,8 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
+extern void TrackSystemTableScanStart(void);
+extern void TrackSystemTableScanEnd(void);
+extern void ResetSystemTableScanState(void);
+
#endif /* XACT_H */
--
2.52.0
nocfbot-v7-PG15-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 8.5K) download
From d3e5a26a7647643729eec82de043f5dd29daa9e4 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v7-PG15] Track the depth of scans of system tables
---
contrib/test_decoding/expected/stream.out | 43 +++++++++++++++++-
contrib/test_decoding/sql/stream.sql | 33 +++++++++++++-
src/backend/access/index/genam.c | 30 ++-----------
src/backend/access/transam/xact.c | 55 +++++++++++++++++++++++
src/backend/replication/logical/logical.c | 2 +-
src/include/access/xact.h | 4 ++
6 files changed, 138 insertions(+), 29 deletions(-)
diff --git a/contrib/test_decoding/expected/stream.out b/contrib/test_decoding/expected/stream.out
index 0f21dcb8e0e..62b3237eb8d 100644
--- a/contrib/test_decoding/expected/stream.out
+++ b/contrib/test_decoding/expected/stream.out
@@ -106,7 +106,48 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL,NULL, 'incl
committing streamed transaction
(17 rows)
-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);
+ ALTER TABLE nested_sys_scan_test ALTER COLUMN checked_value SET STORAGE EXTERNAL;
+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.
+BEGIN;
+INSERT INTO nested_sys_scan_test SELECT repeat('x', 6000) FROM generate_series(1, 10);
+CHECKPOINT;
+SELECT count(*) > 0 AS streamed FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+ streamed
+----------
+ t
+(1 row)
+
+COMMIT;
+SELECT count(*) > 0 AS consumed FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+ consumed
+----------
+ 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 4feec62972a..1f050a585d8 100644
--- a/contrib/test_decoding/sql/stream.sql
+++ b/contrib/test_decoding/sql/stream.sql
@@ -44,5 +44,36 @@ toasted-123456789012345678901234567890123456789012345678901234567890123456789012
SELECT data 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);
+ ALTER TABLE nested_sys_scan_test ALTER COLUMN checked_value SET STORAGE EXTERNAL;
+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.
+BEGIN;
+INSERT INTO nested_sys_scan_test SELECT repeat('x', 6000) FROM generate_series(1, 10);
+CHECKPOINT;
+SELECT count(*) > 0 AS streamed FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+COMMIT;
+SELECT count(*) > 0 AS consumed 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 6dd05e01be8..90d5bb636e4 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -460,13 +460,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -615,12 +609,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
@@ -703,13 +692,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -756,12 +739,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index bf9b36b9a60..3a42e25cddc 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -96,6 +96,18 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
+
+/*
+ * Number of nested systable scans while CheckXidAlive is valid. A counter is
+ * needed because systable scans can be nested. Transaction abort resets it,
+ * since ERROR can bypass the matching systable_endscan().
+ */
+static int bsysscan_depth = 0;
+
+/*
+ * Indicates whether we are currently in a system table scan. Retained for the
+ * backwards-compatibility.
+ */
bool bsysscan = false;
/*
@@ -6263,3 +6275,46 @@ xact_redo(XLogReaderState *record)
else
elog(PANIC, "xact_redo: unknown op code %u", info);
}
+
+/*
+ * TrackSystemTableScanStart
+ *
+ * Record entry into a system table scan while CheckXidAlive is valid.
+ */
+void
+TrackSystemTableScanStart(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ bsysscan_depth++;
+ bsysscan = true;
+}
+
+/*
+ * TrackSystemTableScanEnd
+ *
+ * Record normal exit from a tracked system table scan.
+ */
+void
+TrackSystemTableScanEnd(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(bsysscan_depth > 0);
+ bsysscan_depth--;
+ bsysscan = (bsysscan_depth > 0);
+}
+
+/*
+ * ResetSystemTableScanState
+ *
+ * Reset the system table scan state during transaction abort.
+ */
+void
+ResetSystemTableScanState(void)
+{
+ bsysscan_depth = 0;
+ bsysscan = false;
+}
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index d796abbde78..f8083fed3cf 100644
--- a/src/backend/replication/logical/logical.c
+++ b/src/backend/replication/logical/logical.c
@@ -1931,7 +1931,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSystemTableScanState();
}
/*
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index 8d46a781bbd..85d0c89919e 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -527,4 +527,8 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
+extern void TrackSystemTableScanStart(void);
+extern void TrackSystemTableScanEnd(void);
+extern void ResetSystemTableScanState(void);
+
#endif /* XACT_H */
--
2.52.0
nocfbot-v7-PG16-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 8.5K) download
From c1d9692b39f23fe81d076a990c0e766233bf8361 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v7-PG16] Track the depth of scans of system tables
---
contrib/test_decoding/expected/stream.out | 44 +++++++++++++++++-
contrib/test_decoding/sql/stream.sql | 34 +++++++++++++-
src/backend/access/index/genam.c | 30 ++-----------
src/backend/access/transam/xact.c | 55 +++++++++++++++++++++++
src/backend/replication/logical/logical.c | 2 +-
src/include/access/xact.h | 4 ++
6 files changed, 140 insertions(+), 29 deletions(-)
diff --git a/contrib/test_decoding/expected/stream.out b/contrib/test_decoding/expected/stream.out
index 0f21dcb8e0e..899c4878195 100644
--- a/contrib/test_decoding/expected/stream.out
+++ b/contrib/test_decoding/expected/stream.out
@@ -106,7 +106,49 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL,NULL, 'incl
committing streamed transaction
(17 rows)
-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 consumed FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+ consumed
+----------
+ 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 4feec62972a..1e8a34d43fb 100644
--- a/contrib/test_decoding/sql/stream.sql
+++ b/contrib/test_decoding/sql/stream.sql
@@ -44,5 +44,37 @@ toasted-123456789012345678901234567890123456789012345678901234567890123456789012
SELECT data 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 consumed 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 67246406cff..40cd418d5ca 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -460,13 +460,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -615,12 +609,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
@@ -703,13 +692,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -756,12 +739,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 662078daf91..f3bf1473491 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -97,6 +97,18 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
+
+/*
+ * Number of nested systable scans while CheckXidAlive is valid. A counter is
+ * needed because systable scans can be nested. Transaction abort resets it,
+ * since ERROR can bypass the matching systable_endscan().
+ */
+static int bsysscan_depth = 0;
+
+/*
+ * Indicates whether we are currently in a system table scan. Retained for the
+ * backwards-compatibility.
+ */
bool bsysscan = false;
/*
@@ -6282,3 +6294,46 @@ xact_redo(XLogReaderState *record)
else
elog(PANIC, "xact_redo: unknown op code %u", info);
}
+
+/*
+ * TrackSystemTableScanStart
+ *
+ * Record entry into a system table scan while CheckXidAlive is valid.
+ */
+void
+TrackSystemTableScanStart(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ bsysscan_depth++;
+ bsysscan = true;
+}
+
+/*
+ * TrackSystemTableScanEnd
+ *
+ * Record normal exit from a tracked system table scan.
+ */
+void
+TrackSystemTableScanEnd(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(bsysscan_depth > 0);
+ bsysscan_depth--;
+ bsysscan = (bsysscan_depth > 0);
+}
+
+/*
+ * ResetSystemTableScanState
+ *
+ * Reset the system table scan state during transaction abort.
+ */
+void
+ResetSystemTableScanState(void)
+{
+ bsysscan_depth = 0;
+ bsysscan = false;
+}
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index ce7f7143070..1c44a2d06d3 100644
--- a/src/backend/replication/logical/logical.c
+++ b/src/backend/replication/logical/logical.c
@@ -2008,7 +2008,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSystemTableScanState();
}
/*
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index 7d3b9446e62..9a3645ecddc 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -527,4 +527,8 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
+extern void TrackSystemTableScanStart(void);
+extern void TrackSystemTableScanEnd(void);
+extern void ResetSystemTableScanState(void);
+
#endif /* XACT_H */
--
2.52.0
nocfbot-v7-PG17-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 8.4K) download
From cfe52fb9eb3040400b1edd7bea5042e33bf26b48 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v7-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 | 30 ++-----------
src/backend/access/transam/xact.c | 55 +++++++++++++++++++++++
src/backend/replication/logical/logical.c | 2 +-
src/include/access/xact.h | 4 ++
6 files changed, 139 insertions(+), 29 deletions(-)
diff --git a/contrib/test_decoding/expected/stream.out b/contrib/test_decoding/expected/stream.out
index a76f77601e2..95dd8b1b68f 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 consumed FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+ consumed
+----------
+ 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..0958d95e41e 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 consumed 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..fcec6f1c668 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -459,13 +459,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -614,12 +608,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
@@ -702,13 +691,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -755,12 +738,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 71646d46c83..6fd05ece48e 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -96,6 +96,18 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
+
+/*
+ * Number of nested systable scans while CheckXidAlive is valid. A counter is
+ * needed because systable scans can be nested. Transaction abort resets it,
+ * since ERROR can bypass the matching systable_endscan().
+ */
+static int bsysscan_depth = 0;
+
+/*
+ * Indicates whether we are currently in a system table scan. Retained for the
+ * backwards-compatibility.
+ */
bool bsysscan = false;
/*
@@ -6404,3 +6416,46 @@ xact_redo(XLogReaderState *record)
else
elog(PANIC, "xact_redo: unknown op code %u", info);
}
+
+/*
+ * TrackSystemTableScanStart
+ *
+ * Record entry into a system table scan while CheckXidAlive is valid.
+ */
+void
+TrackSystemTableScanStart(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ bsysscan_depth++;
+ bsysscan = true;
+}
+
+/*
+ * TrackSystemTableScanEnd
+ *
+ * Record normal exit from a tracked system table scan.
+ */
+void
+TrackSystemTableScanEnd(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(bsysscan_depth > 0);
+ bsysscan_depth--;
+ bsysscan = (bsysscan_depth > 0);
+}
+
+/*
+ * ResetSystemTableScanState
+ *
+ * Reset the system table scan state during transaction abort.
+ */
+void
+ResetSystemTableScanState(void)
+{
+ bsysscan_depth = 0;
+ bsysscan = false;
+}
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index f79557c7261..00fd01b5f98 100644
--- a/src/backend/replication/logical/logical.c
+++ b/src/backend/replication/logical/logical.c
@@ -2040,7 +2040,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSystemTableScanState();
}
/*
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index 6d4439f0524..2035d5e4338 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -527,4 +527,8 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
+extern void TrackSystemTableScanStart(void);
+extern void TrackSystemTableScanEnd(void);
+extern void ResetSystemTableScanState(void);
+
#endif /* XACT_H */
--
2.52.0
nocfbot-v7-PG18-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 8.9K) download
From f69bff07a6e62f3c6c447332400adec491a73c8a Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v7-PG18] 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 | 30 ++-----------
src/backend/access/transam/xact.c | 55 +++++++++++++++++++++++
src/backend/replication/logical/logical.c | 2 +-
src/include/access/xact.h | 4 ++
6 files changed, 139 insertions(+), 29 deletions(-)
diff --git a/contrib/test_decoding/expected/stream.out b/contrib/test_decoding/expected/stream.out
index 0ec5c933610..ce64e361c26 100644
--- a/contrib/test_decoding/expected/stream.out
+++ b/contrib/test_decoding/expected/stream.out
@@ -134,6 +134,48 @@ 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 (
+ 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 consumed FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+ consumed
+----------
+ t
+(1 row)
+
-- bug #19616
--
-- An aborted top-level transaction that is discarded at eviction must not
@@ -157,7 +199,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..48b54eee1aa 100644
--- a/contrib/test_decoding/sql/stream.sql
+++ b/contrib/test_decoding/sql/stream.sql
@@ -65,6 +65,37 @@ 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 (
+ 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 consumed 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 +113,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..35ab4db7546 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -468,13 +468,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -619,12 +613,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
@@ -714,13 +703,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -767,12 +750,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 56f4320abfd..25a785a52ab 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -98,6 +98,18 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
+
+/*
+ * Number of nested systable scans while CheckXidAlive is valid. A counter is
+ * needed because systable scans can be nested. Transaction abort resets it,
+ * since ERROR can bypass the matching systable_endscan().
+ */
+static int bsysscan_depth = 0;
+
+/*
+ * Indicates whether we are currently in a system table scan. Retained for the
+ * backwards-compatibility.
+ */
bool bsysscan = false;
/*
@@ -6465,3 +6477,46 @@ xact_redo(XLogReaderState *record)
else
elog(PANIC, "xact_redo: unknown op code %u", info);
}
+
+/*
+ * TrackSystemTableScanStart
+ *
+ * Record entry into a system table scan while CheckXidAlive is valid.
+ */
+void
+TrackSystemTableScanStart(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ bsysscan_depth++;
+ bsysscan = true;
+}
+
+/*
+ * TrackSystemTableScanEnd
+ *
+ * Record normal exit from a tracked system table scan.
+ */
+void
+TrackSystemTableScanEnd(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(bsysscan_depth > 0);
+ bsysscan_depth--;
+ bsysscan = (bsysscan_depth > 0);
+}
+
+/*
+ * ResetSystemTableScanState
+ *
+ * Reset the system table scan state during transaction abort.
+ */
+void
+ResetSystemTableScanState(void)
+{
+ bsysscan_depth = 0;
+ bsysscan = false;
+}
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index 4d3a39f4cd7..c6d2ac8c07c 100644
--- a/src/backend/replication/logical/logical.c
+++ b/src/backend/replication/logical/logical.c
@@ -2013,7 +2013,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSystemTableScanState();
}
/*
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index b2bc10ee041..0a299b583d9 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -533,4 +533,8 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
+extern void TrackSystemTableScanStart(void);
+extern void TrackSystemTableScanEnd(void);
+extern void ResetSystemTableScanState(void);
+
#endif /* XACT_H */
--
2.52.0
nocfbot-v7-PG19-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 7.4K) download
From 8ac461ede766841f3596e9019f0ad01a114ae29b Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v7-PG19] Track the depth of scans of system tables
---
src/backend/access/index/genam.c | 30 ++----------
src/backend/access/transam/xact.c | 59 ++++++++++++++++++++++-
src/backend/replication/logical/logical.c | 2 +-
src/include/access/tableam.h | 21 ++++----
src/include/access/xact.h | 6 ++-
5 files changed, 80 insertions(+), 38 deletions(-)
diff --git a/src/backend/access/index/genam.c b/src/backend/access/index/genam.c
index 1408989c568..556b3aedc41 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -420,13 +420,7 @@ systable_beginscan(Relation heapRelation,
sysscan->snapshot = 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;
+ TrackSystemTableScanStart();
if (irel)
{
@@ -620,12 +614,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
@@ -708,13 +697,7 @@ systable_beginscan_ordered(Relation heapRelation,
elog(ERROR, "column is not in index");
}
- /*
- * 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;
+ TrackSystemTableScanStart();
sysscan->iscan = index_beginscan(heapRelation, indexRelation,
snapshot, NULL, nkeys, 0,
@@ -769,12 +752,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 92319290cf7..d6895e15234 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -100,7 +100,13 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
-bool bsysscan = false;
+
+/*
+ * Number of nested systable scans while CheckXidAlive is valid. A counter is
+ * needed because systable scans can be nested. Transaction abort resets it,
+ * since ERROR can bypass the matching systable_endscan().
+ */
+static int sysscan_depth = 0;
/*
* When running as a parallel worker, we place only a single
@@ -6532,3 +6538,54 @@ xact_redo(XLogReaderState *record)
else
elog(PANIC, "xact_redo: unknown op code %u", info);
}
+
+/*
+ * TrackSystemTableScanStart
+ *
+ * Record entry into a system table scan while CheckXidAlive is valid.
+ */
+void
+TrackSystemTableScanStart(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ sysscan_depth++;
+}
+
+/*
+ * TrackSystemTableScanEnd
+ *
+ * Record normal exit from a tracked system table scan.
+ */
+void
+TrackSystemTableScanEnd(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(IsSystemTableScanInProgress());
+ sysscan_depth--;
+}
+
+/*
+ * ResetSystemTableScanState
+ *
+ * Reset the system table scan state during transaction abort.
+ */
+void
+ResetSystemTableScanState(void)
+{
+ sysscan_depth = 0;
+}
+
+/*
+ * IsSystemTableScanInProgress
+ *
+ * Return whether any tracked system table scan is in progress.
+ */
+bool
+IsSystemTableScanInProgress(void)
+{
+ return sysscan_depth > 0;
+}
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index 98e5f1dd8f9..f579fe934c7 100644
--- a/src/backend/replication/logical/logical.c
+++ b/src/backend/replication/logical/logical.c
@@ -2030,7 +2030,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSystemTableScanState();
}
/*
diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h
index ff03a2b816f..376def9461c 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -924,10 +924,11 @@ table_beginscan_common(Relation rel, Snapshot snapshot, int nkeys,
/*
* We don't allow scans to be started while CheckXidAlive is set, except
- * via systable_beginscan() et al. See detailed comments in xact.c where
- * these variables are declared.
+ * via systable_beginscan() et al. See comments for CheckXidAlive in
+ * xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "scan started during logical decoding");
return rel->rd_tableam->scan_begin(rel, snapshot, nkeys, key, pscan, flags);
@@ -1249,10 +1250,11 @@ table_index_fetch_begin(Relation rel, uint32 flags)
/*
* We don't allow scans to be started while CheckXidAlive is set, except
- * via systable_beginscan() et al. See detailed comments in xact.c where
- * these variables are declared.
+ * via systable_beginscan() et al. See comments for CheckXidAlive in
+ * xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "scan started during logical decoding");
return rel->rd_tableam->index_fetch_begin(rel, flags);
@@ -1348,10 +1350,11 @@ table_tuple_fetch_row_version(Relation rel,
{
/*
* We don't expect direct calls to table_tuple_fetch_row_version with
- * valid CheckXidAlive for catalog or regular tables. See detailed
- * comments in xact.c where these variables are declared.
+ * valid CheckXidAlive for catalog or regular tables. See comments for
+ * CheckXidAlive in xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "unexpected table_tuple_fetch_row_version call during logical decoding");
return rel->rd_tableam->tuple_fetch_row_version(rel, tid, snapshot, slot);
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index a8cbdf247c8..43166bd28b8 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -85,7 +85,6 @@ extern PGDLLIMPORT int synchronous_commit;
/* used during logical streaming of a transaction */
extern PGDLLIMPORT TransactionId CheckXidAlive;
-extern PGDLLIMPORT bool bsysscan;
/*
* Miscellaneous flag bits to record events which occur on the top level
@@ -535,4 +534,9 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
+extern void TrackSystemTableScanStart(void);
+extern void TrackSystemTableScanEnd(void);
+extern void ResetSystemTableScanState(void);
+extern bool IsSystemTableScanInProgress(void);
+
#endif /* XACT_H */
--
2.52.0
v7-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 8.0K) download
From a6002fcbc8f1e3741cb3a9720a5c1409492df54d Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v7] Track the depth of scans of system tables
---
src/backend/access/index/genam.c | 30 ++----------
src/backend/access/transam/xact.c | 59 ++++++++++++++++++++++-
src/backend/replication/logical/logical.c | 2 +-
src/include/access/tableam.h | 28 ++++++-----
src/include/access/xact.h | 6 ++-
5 files changed, 84 insertions(+), 41 deletions(-)
diff --git a/src/backend/access/index/genam.c b/src/backend/access/index/genam.c
index 98ac5bc79c9..dff54d30c1c 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -432,13 +432,7 @@ systable_beginscan(Relation heapRelation,
sysscan->snapshot = 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;
+ TrackSystemTableScanStart();
if (irel)
{
@@ -633,12 +627,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
@@ -721,13 +710,7 @@ systable_beginscan_ordered(Relation heapRelation,
elog(ERROR, "column is not in index");
}
- /*
- * 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;
+ TrackSystemTableScanStart();
sysscan->iscan = index_beginscan(heapRelation, indexRelation, false,
snapshot, NULL, nkeys, 0,
@@ -782,12 +765,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 49e64939667..d9262b6e936 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -100,7 +100,13 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
-bool bsysscan = false;
+
+/*
+ * Number of nested systable scans while CheckXidAlive is valid. A counter is
+ * needed because systable scans can be nested. Transaction abort resets it,
+ * since ERROR can bypass the matching systable_endscan().
+ */
+static int sysscan_depth = 0;
/*
* When running as a parallel worker, we place only a single
@@ -6531,3 +6537,54 @@ xact_redo(XLogReaderState *record)
else
elog(PANIC, "xact_redo: unknown op code %u", info);
}
+
+/*
+ * TrackSystemTableScanStart
+ *
+ * Record entry into a system table scan while CheckXidAlive is valid.
+ */
+void
+TrackSystemTableScanStart(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ sysscan_depth++;
+}
+
+/*
+ * TrackSystemTableScanEnd
+ *
+ * Record normal exit from a tracked system table scan.
+ */
+void
+TrackSystemTableScanEnd(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(IsSystemTableScanInProgress());
+ sysscan_depth--;
+}
+
+/*
+ * ResetSystemTableScanState
+ *
+ * Reset the system table scan state during transaction abort.
+ */
+void
+ResetSystemTableScanState(void)
+{
+ sysscan_depth = 0;
+}
+
+/*
+ * IsSystemTableScanInProgress
+ *
+ * Return whether any tracked system table scan is in progress.
+ */
+bool
+IsSystemTableScanInProgress(void)
+{
+ return sysscan_depth > 0;
+}
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index 98e5f1dd8f9..f579fe934c7 100644
--- a/src/backend/replication/logical/logical.c
+++ b/src/backend/replication/logical/logical.c
@@ -2030,7 +2030,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSystemTableScanState();
}
/*
diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h
index ea3f2a6be99..f66faaacf9b 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -924,10 +924,11 @@ table_beginscan_common(Relation rel, Snapshot snapshot, int nkeys,
/*
* We don't allow scans to be started while CheckXidAlive is set, except
- * via systable_beginscan() et al. See detailed comments in xact.c where
- * these variables are declared.
+ * via systable_beginscan() et al. See comments for CheckXidAlive in
+ * xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "scan started during logical decoding");
return rel->rd_tableam->scan_begin(rel, snapshot, nkeys, key, pscan, flags);
@@ -1248,10 +1249,11 @@ table_index_scan_begin(IndexScanDesc scan, uint32 flags)
/*
* We don't allow scans to be started while CheckXidAlive is set, except
- * via systable_beginscan() et al. See detailed comments in xact.c where
- * these variables are declared.
+ * via systable_beginscan() et al. See comments for CheckXidAlive in
+ * xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "scan started during logical decoding");
scan->heapRelation->rd_tableam->index_scan_begin(scan, flags);
@@ -1341,10 +1343,11 @@ table_fetch_tid(Relation rel,
{
/*
* We don't expect direct calls to table_fetch_tid with valid
- * CheckXidAlive for catalog or regular tables. See detailed comments in
- * xact.c where these variables are declared.
+ * CheckXidAlive for catalog or regular tables. See comments for
+ * CheckXidAlive in xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "unexpected table_fetch_tid call during logical decoding");
return rel->rd_tableam->fetch_tid(rel, tid, snapshot, all_dead);
@@ -1366,10 +1369,11 @@ table_tuple_fetch_row_version(Relation rel,
{
/*
* We don't expect direct calls to table_tuple_fetch_row_version with
- * valid CheckXidAlive for catalog or regular tables. See detailed
- * comments in xact.c where these variables are declared.
+ * valid CheckXidAlive for catalog or regular tables. See comments for
+ * CheckXidAlive in xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "unexpected table_tuple_fetch_row_version call during logical decoding");
return rel->rd_tableam->tuple_fetch_row_version(rel, tid, snapshot, slot);
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index a8cbdf247c8..43166bd28b8 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -85,7 +85,6 @@ extern PGDLLIMPORT int synchronous_commit;
/* used during logical streaming of a transaction */
extern PGDLLIMPORT TransactionId CheckXidAlive;
-extern PGDLLIMPORT bool bsysscan;
/*
* Miscellaneous flag bits to record events which occur on the top level
@@ -535,4 +534,9 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
+extern void TrackSystemTableScanStart(void);
+extern void TrackSystemTableScanEnd(void);
+extern void ResetSystemTableScanState(void);
+extern bool IsSystemTableScanInProgress(void);
+
#endif /* XACT_H */
--
2.52.0
^ permalink raw reply [nested|flat] 18+ messages in thread
* Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641)
2026-10-02 07:33 Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-07 10:08 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Zhijie Hou <houzhijie22@gmail.com>
2026-10-08 02:36 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-08 05:35 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Jiří Kavalík <jiri.kavalik@comgate.cz>
2026-10-09 02:56 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-26 13:25 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-27 03:36 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Peter Smith <smithpb2250@gmail.com>
2026-10-27 06:42 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-28 08:41 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Chao Li <li.evan.chao@gmail.com>
2026-10-29 04:52 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-11-04 12:38 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Shlok Kyal <shlok.kyal.oss@gmail.com>
2026-11-04 13:24 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
@ 2026-11-05 10:43 ` Shlok Kyal <shlok.kyal.oss@gmail.com>
2026-11-18 02:06 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
0 siblings, 1 reply; 18+ messages in thread
From: Shlok Kyal @ 2026-11-05 10:43 UTC (permalink / raw)
To: Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>; +Cc: Chao Li <li.evan.chao@gmail.com>; Peter Smith <smithpb2250@gmail.com>; Jiří Kavalík <jiri.kavalik@comgate.cz>; Zhijie Hou <houzhijie22@gmail.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
>
> PSA new patch set.
>
Hi Kuroda-san,
I have reviewed the patches and it looks good to me.
Thanks,
Shlok Kyal
^ permalink raw reply [nested|flat] 18+ messages in thread
* RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641)
2026-10-02 07:33 Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-07 10:08 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Zhijie Hou <houzhijie22@gmail.com>
2026-10-08 02:36 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-08 05:35 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Jiří Kavalík <jiri.kavalik@comgate.cz>
2026-10-09 02:56 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-26 13:25 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-27 03:36 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Peter Smith <smithpb2250@gmail.com>
2026-10-27 06:42 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-28 08:41 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Chao Li <li.evan.chao@gmail.com>
2026-10-29 04:52 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-11-04 12:38 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Shlok Kyal <shlok.kyal.oss@gmail.com>
2026-11-04 13:24 ` RE: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-11-05 10:43 ` Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Shlok Kyal <shlok.kyal.oss@gmail.com>
@ 2026-11-18 02:06 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
0 siblings, 0 replies; 18+ messages in thread
From: Hayato Kuroda (Fujitsu) @ 2026-11-18 02:06 UTC (permalink / raw)
To: Shlok Kyal <shlok.kyal.oss@gmail.com>; +Cc: Chao Li <li.evan.chao@gmail.com>; Peter Smith <smithpb2250@gmail.com>; Jiří Kavalík <jiri.kavalik@comgate.cz>; Zhijie Hou <houzhijie22@gmail.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Hi,
Since PG19 is now releasing, it's not a good idea to modify the header.
An exported variable bsysscan is retained in this version.
Patches for other versions are unchanged.
Best regards,
Hayato Kuroda
FUJITSU LIMITED
Attachments: download all (6 files, .tar.gz)
v8-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 8.0K) download
From 3f9d51483e08655995f0ea521e0aabe982e7be33 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v8] Track the depth of scans of system tables
---
src/backend/access/index/genam.c | 30 ++----------
src/backend/access/transam/xact.c | 59 ++++++++++++++++++++++-
src/backend/replication/logical/logical.c | 2 +-
src/include/access/tableam.h | 28 ++++++-----
src/include/access/xact.h | 6 ++-
5 files changed, 84 insertions(+), 41 deletions(-)
diff --git a/src/backend/access/index/genam.c b/src/backend/access/index/genam.c
index 98ac5bc79c9..dff54d30c1c 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -432,13 +432,7 @@ systable_beginscan(Relation heapRelation,
sysscan->snapshot = 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;
+ TrackSystemTableScanStart();
if (irel)
{
@@ -633,12 +627,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
@@ -721,13 +710,7 @@ systable_beginscan_ordered(Relation heapRelation,
elog(ERROR, "column is not in index");
}
- /*
- * 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;
+ TrackSystemTableScanStart();
sysscan->iscan = index_beginscan(heapRelation, indexRelation, false,
snapshot, NULL, nkeys, 0,
@@ -782,12 +765,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 1227e3fbe24..bb6f8e0f332 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -100,7 +100,13 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
-bool bsysscan = false;
+
+/*
+ * Number of nested systable scans while CheckXidAlive is valid. A counter is
+ * needed because systable scans can be nested. Transaction abort resets it,
+ * since ERROR can bypass the matching systable_endscan().
+ */
+static int sysscan_depth = 0;
/*
* When running as a parallel worker, we place only a single
@@ -6549,3 +6555,54 @@ xact_redo(XLogReaderState *record)
else
elog(PANIC, "xact_redo: unknown op code %u", info);
}
+
+/*
+ * TrackSystemTableScanStart
+ *
+ * Record entry into a system table scan while CheckXidAlive is valid.
+ */
+void
+TrackSystemTableScanStart(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ sysscan_depth++;
+}
+
+/*
+ * TrackSystemTableScanEnd
+ *
+ * Record normal exit from a tracked system table scan.
+ */
+void
+TrackSystemTableScanEnd(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(IsSystemTableScanInProgress());
+ sysscan_depth--;
+}
+
+/*
+ * ResetSystemTableScanState
+ *
+ * Reset the system table scan state during transaction abort.
+ */
+void
+ResetSystemTableScanState(void)
+{
+ sysscan_depth = 0;
+}
+
+/*
+ * IsSystemTableScanInProgress
+ *
+ * Return whether any tracked system table scan is in progress.
+ */
+bool
+IsSystemTableScanInProgress(void)
+{
+ return sysscan_depth > 0;
+}
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index 8fe255fa960..8314686271b 100644
--- a/src/backend/replication/logical/logical.c
+++ b/src/backend/replication/logical/logical.c
@@ -2056,7 +2056,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSystemTableScanState();
}
/*
diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h
index 9d2fc7f9acb..0939c54b568 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -924,10 +924,11 @@ table_beginscan_common(Relation rel, Snapshot snapshot, int nkeys,
/*
* We don't allow scans to be started while CheckXidAlive is set, except
- * via systable_beginscan() et al. See detailed comments in xact.c where
- * these variables are declared.
+ * via systable_beginscan() et al. See comments for CheckXidAlive in
+ * xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "scan started during logical decoding");
return rel->rd_tableam->scan_begin(rel, snapshot, nkeys, key, pscan, flags);
@@ -1248,10 +1249,11 @@ table_index_scan_begin(IndexScanDesc scan, uint32 flags)
/*
* We don't allow scans to be started while CheckXidAlive is set, except
- * via systable_beginscan() et al. See detailed comments in xact.c where
- * these variables are declared.
+ * via systable_beginscan() et al. See comments for CheckXidAlive in
+ * xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "scan started during logical decoding");
scan->heapRelation->rd_tableam->index_scan_begin(scan, flags);
@@ -1341,10 +1343,11 @@ table_fetch_tid(Relation rel,
{
/*
* We don't expect direct calls to table_fetch_tid with valid
- * CheckXidAlive for catalog or regular tables. See detailed comments in
- * xact.c where these variables are declared.
+ * CheckXidAlive for catalog or regular tables. See comments for
+ * CheckXidAlive in xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "unexpected table_fetch_tid call during logical decoding");
return rel->rd_tableam->fetch_tid(rel, tid, snapshot, all_dead);
@@ -1366,10 +1369,11 @@ table_tuple_fetch_row_version(Relation rel,
{
/*
* We don't expect direct calls to table_tuple_fetch_row_version with
- * valid CheckXidAlive for catalog or regular tables. See detailed
- * comments in xact.c where these variables are declared.
+ * valid CheckXidAlive for catalog or regular tables. See comments for
+ * CheckXidAlive in xact.c.
*/
- if (unlikely(TransactionIdIsValid(CheckXidAlive) && !bsysscan))
+ if (unlikely(TransactionIdIsValid(CheckXidAlive) &&
+ !IsSystemTableScanInProgress()))
elog(ERROR, "unexpected table_tuple_fetch_row_version call during logical decoding");
return rel->rd_tableam->tuple_fetch_row_version(rel, tid, snapshot, slot);
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index a8cbdf247c8..43166bd28b8 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -85,7 +85,6 @@ extern PGDLLIMPORT int synchronous_commit;
/* used during logical streaming of a transaction */
extern PGDLLIMPORT TransactionId CheckXidAlive;
-extern PGDLLIMPORT bool bsysscan;
/*
* Miscellaneous flag bits to record events which occur on the top level
@@ -535,4 +534,9 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
+extern void TrackSystemTableScanStart(void);
+extern void TrackSystemTableScanEnd(void);
+extern void ResetSystemTableScanState(void);
+extern bool IsSystemTableScanInProgress(void);
+
#endif /* XACT_H */
--
2.52.0
nocfbot-v8-PG15-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 8.5K) download
From c08af8e9fb6c39b7c7ed851ce1e6adc13370ea27 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v8-PG15] Track the depth of scans of system tables
---
contrib/test_decoding/expected/stream.out | 43 +++++++++++++++++-
contrib/test_decoding/sql/stream.sql | 33 +++++++++++++-
src/backend/access/index/genam.c | 30 ++-----------
src/backend/access/transam/xact.c | 55 +++++++++++++++++++++++
src/backend/replication/logical/logical.c | 2 +-
src/include/access/xact.h | 4 ++
6 files changed, 138 insertions(+), 29 deletions(-)
diff --git a/contrib/test_decoding/expected/stream.out b/contrib/test_decoding/expected/stream.out
index 0f21dcb8e0e..62b3237eb8d 100644
--- a/contrib/test_decoding/expected/stream.out
+++ b/contrib/test_decoding/expected/stream.out
@@ -106,7 +106,48 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL,NULL, 'incl
committing streamed transaction
(17 rows)
-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);
+ ALTER TABLE nested_sys_scan_test ALTER COLUMN checked_value SET STORAGE EXTERNAL;
+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.
+BEGIN;
+INSERT INTO nested_sys_scan_test SELECT repeat('x', 6000) FROM generate_series(1, 10);
+CHECKPOINT;
+SELECT count(*) > 0 AS streamed FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+ streamed
+----------
+ t
+(1 row)
+
+COMMIT;
+SELECT count(*) > 0 AS consumed FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+ consumed
+----------
+ 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 4feec62972a..1f050a585d8 100644
--- a/contrib/test_decoding/sql/stream.sql
+++ b/contrib/test_decoding/sql/stream.sql
@@ -44,5 +44,36 @@ toasted-123456789012345678901234567890123456789012345678901234567890123456789012
SELECT data 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);
+ ALTER TABLE nested_sys_scan_test ALTER COLUMN checked_value SET STORAGE EXTERNAL;
+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.
+BEGIN;
+INSERT INTO nested_sys_scan_test SELECT repeat('x', 6000) FROM generate_series(1, 10);
+CHECKPOINT;
+SELECT count(*) > 0 AS streamed FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+COMMIT;
+SELECT count(*) > 0 AS consumed 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 6dd05e01be8..90d5bb636e4 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -460,13 +460,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -615,12 +609,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
@@ -703,13 +692,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -756,12 +739,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 8382fe94082..b0c092efac3 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -96,6 +96,18 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
+
+/*
+ * Number of nested systable scans while CheckXidAlive is valid. A counter is
+ * needed because systable scans can be nested. Transaction abort resets it,
+ * since ERROR can bypass the matching systable_endscan().
+ */
+static int bsysscan_depth = 0;
+
+/*
+ * Indicates whether we are currently in a system table scan. Retained for the
+ * backwards-compatibility.
+ */
bool bsysscan = false;
/*
@@ -6281,3 +6293,46 @@ xact_redo(XLogReaderState *record)
else
elog(PANIC, "xact_redo: unknown op code %u", info);
}
+
+/*
+ * TrackSystemTableScanStart
+ *
+ * Record entry into a system table scan while CheckXidAlive is valid.
+ */
+void
+TrackSystemTableScanStart(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ bsysscan_depth++;
+ bsysscan = true;
+}
+
+/*
+ * TrackSystemTableScanEnd
+ *
+ * Record normal exit from a tracked system table scan.
+ */
+void
+TrackSystemTableScanEnd(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(bsysscan_depth > 0);
+ bsysscan_depth--;
+ bsysscan = (bsysscan_depth > 0);
+}
+
+/*
+ * ResetSystemTableScanState
+ *
+ * Reset the system table scan state during transaction abort.
+ */
+void
+ResetSystemTableScanState(void)
+{
+ bsysscan_depth = 0;
+ bsysscan = false;
+}
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index db67437ab46..1f01cdafbfc 100644
--- a/src/backend/replication/logical/logical.c
+++ b/src/backend/replication/logical/logical.c
@@ -1923,7 +1923,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSystemTableScanState();
}
/*
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index 8d46a781bbd..85d0c89919e 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -527,4 +527,8 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
+extern void TrackSystemTableScanStart(void);
+extern void TrackSystemTableScanEnd(void);
+extern void ResetSystemTableScanState(void);
+
#endif /* XACT_H */
--
2.52.0
nocfbot-v8-PG16-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 8.5K) download
From 4d1cdcc03c2599f6fba9c6d39336c65c008fbaf7 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v8-PG16] Track the depth of scans of system tables
---
contrib/test_decoding/expected/stream.out | 44 +++++++++++++++++-
contrib/test_decoding/sql/stream.sql | 34 +++++++++++++-
src/backend/access/index/genam.c | 30 ++-----------
src/backend/access/transam/xact.c | 55 +++++++++++++++++++++++
src/backend/replication/logical/logical.c | 2 +-
src/include/access/xact.h | 4 ++
6 files changed, 140 insertions(+), 29 deletions(-)
diff --git a/contrib/test_decoding/expected/stream.out b/contrib/test_decoding/expected/stream.out
index 0f21dcb8e0e..899c4878195 100644
--- a/contrib/test_decoding/expected/stream.out
+++ b/contrib/test_decoding/expected/stream.out
@@ -106,7 +106,49 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL,NULL, 'incl
committing streamed transaction
(17 rows)
-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 consumed FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+ consumed
+----------
+ 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 4feec62972a..1e8a34d43fb 100644
--- a/contrib/test_decoding/sql/stream.sql
+++ b/contrib/test_decoding/sql/stream.sql
@@ -44,5 +44,37 @@ toasted-123456789012345678901234567890123456789012345678901234567890123456789012
SELECT data 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 consumed 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 67246406cff..40cd418d5ca 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -460,13 +460,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -615,12 +609,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
@@ -703,13 +692,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -756,12 +739,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index d3254f85f77..eec49c285be 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -97,6 +97,18 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
+
+/*
+ * Number of nested systable scans while CheckXidAlive is valid. A counter is
+ * needed because systable scans can be nested. Transaction abort resets it,
+ * since ERROR can bypass the matching systable_endscan().
+ */
+static int bsysscan_depth = 0;
+
+/*
+ * Indicates whether we are currently in a system table scan. Retained for the
+ * backwards-compatibility.
+ */
bool bsysscan = false;
/*
@@ -6300,3 +6312,46 @@ xact_redo(XLogReaderState *record)
else
elog(PANIC, "xact_redo: unknown op code %u", info);
}
+
+/*
+ * TrackSystemTableScanStart
+ *
+ * Record entry into a system table scan while CheckXidAlive is valid.
+ */
+void
+TrackSystemTableScanStart(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ bsysscan_depth++;
+ bsysscan = true;
+}
+
+/*
+ * TrackSystemTableScanEnd
+ *
+ * Record normal exit from a tracked system table scan.
+ */
+void
+TrackSystemTableScanEnd(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(bsysscan_depth > 0);
+ bsysscan_depth--;
+ bsysscan = (bsysscan_depth > 0);
+}
+
+/*
+ * ResetSystemTableScanState
+ *
+ * Reset the system table scan state during transaction abort.
+ */
+void
+ResetSystemTableScanState(void)
+{
+ bsysscan_depth = 0;
+ bsysscan = false;
+}
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index 0767999eafc..04fcd8d3a30 100644
--- a/src/backend/replication/logical/logical.c
+++ b/src/backend/replication/logical/logical.c
@@ -2034,7 +2034,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSystemTableScanState();
}
/*
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index 7d3b9446e62..9a3645ecddc 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -527,4 +527,8 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
+extern void TrackSystemTableScanStart(void);
+extern void TrackSystemTableScanEnd(void);
+extern void ResetSystemTableScanState(void);
+
#endif /* XACT_H */
--
2.52.0
nocfbot-v8-PG17-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 8.4K) download
From 845dd84e80dae7c472b6ed6405957de5c3c5c416 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v8-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 | 30 ++-----------
src/backend/access/transam/xact.c | 55 +++++++++++++++++++++++
src/backend/replication/logical/logical.c | 2 +-
src/include/access/xact.h | 4 ++
6 files changed, 139 insertions(+), 29 deletions(-)
diff --git a/contrib/test_decoding/expected/stream.out b/contrib/test_decoding/expected/stream.out
index a76f77601e2..95dd8b1b68f 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 consumed FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+ consumed
+----------
+ 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..0958d95e41e 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 consumed 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..fcec6f1c668 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -459,13 +459,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -614,12 +608,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
@@ -702,13 +691,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -755,12 +738,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 9e73a225f0d..cbdd2b8bf3d 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -96,6 +96,18 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
+
+/*
+ * Number of nested systable scans while CheckXidAlive is valid. A counter is
+ * needed because systable scans can be nested. Transaction abort resets it,
+ * since ERROR can bypass the matching systable_endscan().
+ */
+static int bsysscan_depth = 0;
+
+/*
+ * Indicates whether we are currently in a system table scan. Retained for the
+ * backwards-compatibility.
+ */
bool bsysscan = false;
/*
@@ -6422,3 +6434,46 @@ xact_redo(XLogReaderState *record)
else
elog(PANIC, "xact_redo: unknown op code %u", info);
}
+
+/*
+ * TrackSystemTableScanStart
+ *
+ * Record entry into a system table scan while CheckXidAlive is valid.
+ */
+void
+TrackSystemTableScanStart(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ bsysscan_depth++;
+ bsysscan = true;
+}
+
+/*
+ * TrackSystemTableScanEnd
+ *
+ * Record normal exit from a tracked system table scan.
+ */
+void
+TrackSystemTableScanEnd(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(bsysscan_depth > 0);
+ bsysscan_depth--;
+ bsysscan = (bsysscan_depth > 0);
+}
+
+/*
+ * ResetSystemTableScanState
+ *
+ * Reset the system table scan state during transaction abort.
+ */
+void
+ResetSystemTableScanState(void)
+{
+ bsysscan_depth = 0;
+ bsysscan = false;
+}
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index 08ce2d6b26e..521f5409e5b 100644
--- a/src/backend/replication/logical/logical.c
+++ b/src/backend/replication/logical/logical.c
@@ -2066,7 +2066,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSystemTableScanState();
}
/*
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index 6d4439f0524..2035d5e4338 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -527,4 +527,8 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
+extern void TrackSystemTableScanStart(void);
+extern void TrackSystemTableScanEnd(void);
+extern void ResetSystemTableScanState(void);
+
#endif /* XACT_H */
--
2.52.0
nocfbot-v8-PG18-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 8.9K) download
From 281c75f1242ecf443ae9b17219ca2f7b304228b8 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v8] 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 | 30 ++-----------
src/backend/access/transam/xact.c | 55 +++++++++++++++++++++++
src/backend/replication/logical/logical.c | 2 +-
src/include/access/xact.h | 4 ++
6 files changed, 139 insertions(+), 29 deletions(-)
diff --git a/contrib/test_decoding/expected/stream.out b/contrib/test_decoding/expected/stream.out
index 0ec5c933610..ce64e361c26 100644
--- a/contrib/test_decoding/expected/stream.out
+++ b/contrib/test_decoding/expected/stream.out
@@ -134,6 +134,48 @@ 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 (
+ 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 consumed FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+ consumed
+----------
+ t
+(1 row)
+
-- bug #19616
--
-- An aborted top-level transaction that is discarded at eviction must not
@@ -157,7 +199,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..48b54eee1aa 100644
--- a/contrib/test_decoding/sql/stream.sql
+++ b/contrib/test_decoding/sql/stream.sql
@@ -65,6 +65,37 @@ 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 (
+ 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 consumed 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 +113,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..35ab4db7546 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -468,13 +468,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -619,12 +613,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
@@ -714,13 +703,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;
+ TrackSystemTableScanStart();
return sysscan;
}
@@ -767,12 +750,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 181cffc26ca..8af360d3a5d 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -98,6 +98,18 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
+
+/*
+ * Number of nested systable scans while CheckXidAlive is valid. A counter is
+ * needed because systable scans can be nested. Transaction abort resets it,
+ * since ERROR can bypass the matching systable_endscan().
+ */
+static int bsysscan_depth = 0;
+
+/*
+ * Indicates whether we are currently in a system table scan. Retained for the
+ * backwards-compatibility.
+ */
bool bsysscan = false;
/*
@@ -6483,3 +6495,46 @@ xact_redo(XLogReaderState *record)
else
elog(PANIC, "xact_redo: unknown op code %u", info);
}
+
+/*
+ * TrackSystemTableScanStart
+ *
+ * Record entry into a system table scan while CheckXidAlive is valid.
+ */
+void
+TrackSystemTableScanStart(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ bsysscan_depth++;
+ bsysscan = true;
+}
+
+/*
+ * TrackSystemTableScanEnd
+ *
+ * Record normal exit from a tracked system table scan.
+ */
+void
+TrackSystemTableScanEnd(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(bsysscan_depth > 0);
+ bsysscan_depth--;
+ bsysscan = (bsysscan_depth > 0);
+}
+
+/*
+ * ResetSystemTableScanState
+ *
+ * Reset the system table scan state during transaction abort.
+ */
+void
+ResetSystemTableScanState(void)
+{
+ bsysscan_depth = 0;
+ bsysscan = false;
+}
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index 09d8b0fa2c5..6bbd2835b19 100644
--- a/src/backend/replication/logical/logical.c
+++ b/src/backend/replication/logical/logical.c
@@ -2039,7 +2039,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSystemTableScanState();
}
/*
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index b2bc10ee041..0a299b583d9 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -533,4 +533,8 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
+extern void TrackSystemTableScanStart(void);
+extern void TrackSystemTableScanEnd(void);
+extern void ResetSystemTableScanState(void);
+
#endif /* XACT_H */
--
2.52.0
nocfbot-v8-PG19-0001-Track-the-depth-of-scans-of-system-tables.patch (application/octet-stream, 9.0K) download
From 94098133141a225711107fd93771a22387a6bc5a Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Thu, 1 Oct 2026 16:59:00 +0900
Subject: [PATCH v8] 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 | 30 ++-----------
src/backend/access/transam/xact.c | 55 +++++++++++++++++++++++
src/backend/replication/logical/logical.c | 2 +-
src/include/access/xact.h | 4 ++
6 files changed, 139 insertions(+), 29 deletions(-)
diff --git a/contrib/test_decoding/expected/stream.out b/contrib/test_decoding/expected/stream.out
index 0ec5c933610..ce64e361c26 100644
--- a/contrib/test_decoding/expected/stream.out
+++ b/contrib/test_decoding/expected/stream.out
@@ -134,6 +134,48 @@ 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 (
+ 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 consumed FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'stream-changes', '1');
+ consumed
+----------
+ t
+(1 row)
+
-- bug #19616
--
-- An aborted top-level transaction that is discarded at eviction must not
@@ -157,7 +199,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..48b54eee1aa 100644
--- a/contrib/test_decoding/sql/stream.sql
+++ b/contrib/test_decoding/sql/stream.sql
@@ -65,6 +65,37 @@ 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 (
+ 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 consumed 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 +113,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 1408989c568..556b3aedc41 100644
--- a/src/backend/access/index/genam.c
+++ b/src/backend/access/index/genam.c
@@ -420,13 +420,7 @@ systable_beginscan(Relation heapRelation,
sysscan->snapshot = 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;
+ TrackSystemTableScanStart();
if (irel)
{
@@ -620,12 +614,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
@@ -708,13 +697,7 @@ systable_beginscan_ordered(Relation heapRelation,
elog(ERROR, "column is not in index");
}
- /*
- * 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;
+ TrackSystemTableScanStart();
sysscan->iscan = index_beginscan(heapRelation, indexRelation,
snapshot, NULL, nkeys, 0,
@@ -769,12 +752,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;
+ TrackSystemTableScanEnd();
pfree(sysscan);
}
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 575845ee1f9..079e561c37d 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -100,6 +100,18 @@ int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
* concurrent aborts only in systable_* APIs.
*/
TransactionId CheckXidAlive = InvalidTransactionId;
+
+/*
+ * Number of nested systable scans while CheckXidAlive is valid. A counter is
+ * needed because systable scans can be nested. Transaction abort resets it,
+ * since ERROR can bypass the matching systable_endscan().
+ */
+static int bsysscan_depth = 0;
+
+/*
+ * Indicates whether we are currently in a system table scan. Retained for the
+ * backwards-compatibility.
+ */
bool bsysscan = false;
/*
@@ -6550,3 +6562,46 @@ xact_redo(XLogReaderState *record)
else
elog(PANIC, "xact_redo: unknown op code %u", info);
}
+
+/*
+ * TrackSystemTableScanStart
+ *
+ * Record entry into a system table scan while CheckXidAlive is valid.
+ */
+void
+TrackSystemTableScanStart(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ bsysscan_depth++;
+ bsysscan = true;
+}
+
+/*
+ * TrackSystemTableScanEnd
+ *
+ * Record normal exit from a tracked system table scan.
+ */
+void
+TrackSystemTableScanEnd(void)
+{
+ if (!TransactionIdIsValid(CheckXidAlive))
+ return;
+
+ Assert(bsysscan_depth > 0);
+ bsysscan_depth--;
+ bsysscan = (bsysscan_depth > 0);
+}
+
+/*
+ * ResetSystemTableScanState
+ *
+ * Reset the system table scan state during transaction abort.
+ */
+void
+ResetSystemTableScanState(void)
+{
+ bsysscan_depth = 0;
+ bsysscan = false;
+}
diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index 8fe255fa960..8314686271b 100644
--- a/src/backend/replication/logical/logical.c
+++ b/src/backend/replication/logical/logical.c
@@ -2056,7 +2056,7 @@ void
ResetLogicalStreamingState(void)
{
CheckXidAlive = InvalidTransactionId;
- bsysscan = false;
+ ResetSystemTableScanState();
}
/*
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index a8cbdf247c8..15bf9be2ca8 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -535,4 +535,8 @@ extern void EnterParallelMode(void);
extern void ExitParallelMode(void);
extern bool IsInParallelMode(void);
+extern void TrackSystemTableScanStart(void);
+extern void TrackSystemTableScanEnd(void);
+extern void ResetSystemTableScanState(void);
+
#endif /* XACT_H */
--
2.52.0
^ permalink raw reply [nested|flat] 18+ messages in thread
end of thread, other threads:[~2026-11-18 02:06 UTC | newest]
Thread overview: 18+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-09-30 13:59 Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Jiří Kavalík <jiri.kavalik@comgate.cz>
2026-10-02 07:33 Re: Streaming decoding fails with "unexpected table_index_fetch_tuple call during logical decoding" when a relation has a TOASTed conbin (follow-up to BUG #18641) Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-02 08:29 ` Jiří Kavalík <jiri.kavalik@comgate.cz>
2026-10-06 09:21 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-06 09:33 ` Jiří Kavalík <jiri.kavalik@comgate.cz>
2026-10-07 10:08 ` Zhijie Hou <houzhijie22@gmail.com>
2026-10-08 02:36 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-08 05:35 ` Jiří Kavalík <jiri.kavalik@comgate.cz>
2026-10-09 02:56 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-26 13:25 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-27 03:36 ` Peter Smith <smithpb2250@gmail.com>
2026-10-27 06:42 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-10-28 08:41 ` Chao Li <li.evan.chao@gmail.com>
2026-10-29 04:52 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-11-04 12:38 ` Shlok Kyal <shlok.kyal.oss@gmail.com>
2026-11-04 13:24 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
2026-11-05 10:43 ` Shlok Kyal <shlok.kyal.oss@gmail.com>
2026-11-18 02:06 ` Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox