pg.ddx.io pgsql-hackers@postgresql.org mailing list archive
help / color / mirror / Atom feed[BUG] Incorrect historic snapshot may be serialized to disk during fast-forwarding
10+ messages / 4 participants
[nested] [flat]
* [BUG] Incorrect historic snapshot may be serialized to disk during fast-forwarding
@ 2025-11-22 08:55 cca5507 <cca5507@qq.com>
0 siblings, 1 reply; 10+ messages in thread
From: cca5507 @ 2025-11-22 08:55 UTC (permalink / raw)
To: pgsql-hackers <pgsql-hackers@lists.postgresql.org>
Hi,
When working on another historic snapshot's bug in [1], I find the $subject.
Here is a test case, but we need to add some log in SnapBuildSerialize() first:
inline patch 1: src/backend/replication/logical/snapbuild.c (+0/-0 lines) download inline-1.patch
diff --git a/src/backend/replication/logical/snapbuild.c b/src/backend/replication/logical/snapbuild.c
index 6e18baa33cb..6d13b2d811b 100644
--- a/src/backend/replication/logical/snapbuild.c
+++ b/src/backend/replication/logical/snapbuild.c
@@ -1523,6 +1523,19 @@ SnapBuildSerialize(SnapBuild *builder, XLogRecPtr lsn)
/* consistent snapshots have no next phase */
Assert(builder->next_phase_at == InvalidTransactionId);
+ StringInfoData logbuf;
+ initStringInfo(&logbuf);
+ appendStringInfo(&logbuf, "SnapBuildSerialize: lsn: %X/%08X xmin: %u, xmax: %u, committed: ",
+ LSN_FORMAT_ARGS(lsn), builder->xmin, builder->xmax);
+ for (size_t i = 0; i < builder->committed.xcnt; i++)
+ {
+ if (i > 0)
+ appendStringInfoString(&logbuf, ", ");
+ appendStringInfo(&logbuf, "%u", builder->committed.xip[i]);
+ }
+ elog(LOG, "%s", logbuf.data);
+ pfree(logbuf.data);
+
/*
* We identify snapshots by the LSN they are valid for. We don't need to
* include timelines in the name as each LSN maps to exactly one timeline
1) create table t (id int) with (user_catalog_table = true);
2) select pg_create_logical_replication_slot('s1', 'test_decoding');
3) select pg_create_logical_replication_slot('s2', 'test_decoding');
4) insert into t values (1);
5) select pg_replication_slot_advance('s1', pg_current_wal_lsn());
6) select pg_logical_slot_get_changes('s2', pg_current_wal_lsn(), null);
Then we will find some log like this:
LOG: SnapBuildSerialize: lsn: 0/017D1318 xmin: 768, xmax: 768, committed:
STATEMENT: select pg_replication_slot_advance('s1', pg_current_wal_lsn());
LOG: SnapBuildSerialize: lsn: 0/017D1318 xmin: 768, xmax: 769, committed: 768
STATEMENT: select pg_logical_slot_get_changes('s2', pg_current_wal_lsn(), null);
At the same lsn, we get two different historic snapshots, and the first one (which is incorrect) is serialized to disk.
The main reason is that we don't handle XLOG_HEAP2_NEW_CID during fast-forwarding, so we don't consider the insert as having a catalog change.
Attach a patch to fix it.
Looking forward to your reply.
[1]
https://www.postgresql.org/message-id/tencent_21E152AD504A814C071EDF41A4DD7BA84D06%40qq.com
--
Regards,
ChangAo Chen
Patches and attachments: download all (2 files, .tar.gz)
v1-0001-Handle-XLOG_HEAP2_NEW_CID-in-heap2_decode-even-if.patch (application/octet-stream, 963B) download
From 377692b2eba99408a2adf6aeb7b7fa42affe73ad Mon Sep 17 00:00:00 2001
From: ChangAo Chen <cca5507@qq.com>
Date: Sat, 22 Nov 2025 16:51:13 +0800
Subject: [PATCH v1] Handle XLOG_HEAP2_NEW_CID in heap2_decode() even if
fast-forwarding.
---
src/backend/replication/logical/decode.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/src/backend/replication/logical/decode.c b/src/backend/replication/logical/decode.c
index cc03f0706e9..ffd4372917b 100644
--- a/src/backend/replication/logical/decode.c
+++ b/src/backend/replication/logical/decode.c
@@ -435,9 +435,11 @@ heap2_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
xlrec = (xl_heap_new_cid *) XLogRecGetData(buf->record);
SnapBuildProcessNewCid(builder, xid, buf->origptr, xlrec);
-
- break;
}
+ else
+ ReorderBufferXidSetCatalogChanges(ctx->reorder, xid,
+ buf->origptr);
+ break;
case XLOG_HEAP2_REWRITE:
/*
--
2.51.2
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re:[BUG] Incorrect historic snapshot may be serialized to disk during fast-forwarding
@ 2025-11-23 02:15 ocean_li_996 <ocean_li_996@163.com>
parent: cca5507 <cca5507@qq.com>
0 siblings, 1 reply; 10+ messages in thread
From: ocean_li_996 @ 2025-11-23 02:15 UTC (permalink / raw)
To: cca5507 <cca5507@qq.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>
Hi ChangAo,
Thanks for your analyze and report.
At 2025-11-23 00:07:59, "cca5507" <cca5507@qq.com> wrote:
>The main reason is that we don't handle XLOG_HEAP2_NEW_CID during fast-forwarding, so we don't consider the insert as having a catalog change.
Yeah. Refers to code below:
```
case XLOG_HEAP2_NEW_CID:
if (!ctx->fast_forward)
{
xl_heap_new_cid *xlrec;
xlrec = (xl_heap_new_cid *) XLogRecGetData(buf->record);
SnapBuildProcessNewCid(builder, xid, buf->origptr, xlrec);
break;
}
```
It is clear that transaction is skipped set has catalog change (in SnapBuildProcessNewCid) duraing fast forward.
However, in practice this does not cause any actual issues, because for the "real" system catalogs (those not
specified via user_catalog_table and thus critical for decoding), any HEAP2_NEW_CID changes are always accompanied
by some invalid messages. As a result, they are ultimately marked as catalog changes.
Still, from a code perspective, I would prefer fixing this behavior for logical consistency across the snapshot
builder, even if it doesn’t manifest as a runtime problem today. But we can not provide a test case.
>Attach a patch to fix it.
The patch in attachment is better for me. What do you think?
Bset regards,
Haiyang Li
Attachments:
v2-0001-Handle-XLOG_HEAP2_NEW_CID-in-fast-forward.patch (application/octet-stream, 1.5K) download
diff --git a/src/backend/replication/logical/decode.c b/src/backend/replication/logical/decode.c
index cc03f0706e9..5d0c5088201 100644
--- a/src/backend/replication/logical/decode.c
+++ b/src/backend/replication/logical/decode.c
@@ -429,6 +429,15 @@ heap2_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
DecodeMultiInsert(ctx, buf);
break;
case XLOG_HEAP2_NEW_CID:
+ /*
+ * we only log new_cid's if a catalog tuple was modified, so mark the
+ * transaction as containing catalog modifications.
+ *
+ * Note: we do this even in fast-forward mode, as we need to maintain
+ * the snapshot correctly.
+ */
+ ReorderBufferXidSetCatalogChanges(ctx->reorder, xid, buf->origptr);
+
if (!ctx->fast_forward)
{
xl_heap_new_cid *xlrec;
diff --git a/src/backend/replication/logical/snapbuild.c b/src/backend/replication/logical/snapbuild.c
index 6e18baa33cb..9f78a881444 100644
--- a/src/backend/replication/logical/snapbuild.c
+++ b/src/backend/replication/logical/snapbuild.c
@@ -691,12 +691,6 @@ SnapBuildProcessNewCid(SnapBuild *builder, TransactionId xid,
{
CommandId cid;
- /*
- * we only log new_cid's if a catalog tuple was modified, so mark the
- * transaction as containing catalog modifications
- */
- ReorderBufferXidSetCatalogChanges(builder->reorder, xid, lsn);
-
ReorderBufferAddNewTupleCids(builder->reorder, xlrec->top_xid, lsn,
xlrec->target_locator, xlrec->target_tid,
xlrec->cmin, xlrec->cmax,
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: [BUG] Incorrect historic snapshot may be serialized to disk during fast-forwarding
@ 2025-11-23 05:31 cca5507 <cca5507@qq.com>
parent: ocean_li_996 <ocean_li_996@163.com>
0 siblings, 1 reply; 10+ messages in thread
From: cca5507 @ 2025-11-23 05:31 UTC (permalink / raw)
To: ocean_li_996 <ocean_li_996@163.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>
Hi Haiyang,
> The patch in attachment is better for me. What do you think?
The v2-0001 LGTM.
A small suggestion:
We should move the 'break' out of the 'if', because we don't want it fall through to XLOG_HEAP2_REWRITE if we are fast-forwarding.
--
Regards,
ChangAo Chen
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re:Re: [BUG] Incorrect historic snapshot may be serialized to disk during fast-forwarding
@ 2025-11-23 06:33 ocean_li_996 <ocean_li_996@163.com>
parent: cca5507 <cca5507@qq.com>
0 siblings, 2 replies; 10+ messages in thread
From: ocean_li_996 @ 2025-11-23 06:33 UTC (permalink / raw)
To: cca5507 <cca5507@qq.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>
Hi ChangAo,
At 2025-11-23 13:31:49, "cca5507" <cca5507@qq.com> wrote:
>> The patch in attachment is better for me. What do you think?
>
>The v2-0001 LGTM.
>
>A small suggestion:
>
>We should move the 'break' out of the 'if', because we don't want it fall through to XLOG_HEAP2_REWRITE if we are fast-forwarding.
Fair. The patch updated is provided in attachment.
Best regards,
Haiyang Li
Attachments:
v3-0001-Handle-XLOG_HEAP2_NEW_CID-in-fast-forward.patch (application/octet-stream, 1.6K) download
diff --git a/src/backend/replication/logical/decode.c b/src/backend/replication/logical/decode.c
index cc03f0706e9..4f5704354a6 100644
--- a/src/backend/replication/logical/decode.c
+++ b/src/backend/replication/logical/decode.c
@@ -429,15 +429,23 @@ heap2_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
DecodeMultiInsert(ctx, buf);
break;
case XLOG_HEAP2_NEW_CID:
+ /*
+ * we only log new_cid's if a catalog tuple was modified, so mark the
+ * transaction as containing catalog modifications.
+ *
+ * Note: we do this even in fast-forward mode, as we need to maintain
+ * the snapshot correctly.
+ */
+ ReorderBufferXidSetCatalogChanges(ctx->reorder, xid, buf->origptr);
+
if (!ctx->fast_forward)
{
xl_heap_new_cid *xlrec;
xlrec = (xl_heap_new_cid *) XLogRecGetData(buf->record);
SnapBuildProcessNewCid(builder, xid, buf->origptr, xlrec);
-
- break;
}
+ break;
case XLOG_HEAP2_REWRITE:
/*
diff --git a/src/backend/replication/logical/snapbuild.c b/src/backend/replication/logical/snapbuild.c
index 6e18baa33cb..9f78a881444 100644
--- a/src/backend/replication/logical/snapbuild.c
+++ b/src/backend/replication/logical/snapbuild.c
@@ -691,12 +691,6 @@ SnapBuildProcessNewCid(SnapBuild *builder, TransactionId xid,
{
CommandId cid;
- /*
- * we only log new_cid's if a catalog tuple was modified, so mark the
- * transaction as containing catalog modifications
- */
- ReorderBufferXidSetCatalogChanges(builder->reorder, xid, lsn);
-
ReorderBufferAddNewTupleCids(builder->reorder, xlrec->top_xid, lsn,
xlrec->target_locator, xlrec->target_tid,
xlrec->cmin, xlrec->cmax,
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: [BUG] Incorrect historic snapshot may be serialized to disk during fast-forwarding
@ 2025-12-10 05:01 cca5507 <cca5507@qq.com>
parent: ocean_li_996 <ocean_li_996@163.com>
1 sibling, 0 replies; 10+ messages in thread
From: cca5507 @ 2025-12-10 05:01 UTC (permalink / raw)
To: ocean_li_996 <ocean_li_996@163.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>
Hi,
I add some commit message to the patch and create a CF entry:
https://commitfest.postgresql.org/patch/6304/
--
Regards,
ChangAo Chen
Attachments:
v4-0001-Handle-XLOG_HEAP2_NEW_CID-in-heap2_decode-even-if.patch (application/octet-stream, 2.8K) download
From 6f0a67ca2474498e22bfeefba68e1fd73edcc4bd Mon Sep 17 00:00:00 2001
From: ChangAo Chen <cca5507@qq.com>
Date: Wed, 10 Dec 2025 11:34:02 +0800
Subject: [PATCH v4] Handle XLOG_HEAP2_NEW_CID in heap2_decode() even if
fast-forwarding.
We need to mark the transaction as containing catalog modifications
during decoding XLOG_HEAP2_NEW_CID even if fast-forwarding to maintain
the snapshot correctly. Otherwise, an incorrect historic snapshot may
be serialized to disk during fast-forwarding because the snapshot only
track catalog modified transaction.
---
src/backend/replication/logical/decode.c | 12 ++++++++++--
src/backend/replication/logical/snapbuild.c | 9 ++-------
2 files changed, 12 insertions(+), 9 deletions(-)
diff --git a/src/backend/replication/logical/decode.c b/src/backend/replication/logical/decode.c
index cc03f0706e9..31d22c51290 100644
--- a/src/backend/replication/logical/decode.c
+++ b/src/backend/replication/logical/decode.c
@@ -429,15 +429,23 @@ heap2_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
DecodeMultiInsert(ctx, buf);
break;
case XLOG_HEAP2_NEW_CID:
+ /*
+ * We only log new_cid's if a catalog tuple was modified, so mark the
+ * transaction as containing catalog modifications.
+ *
+ * Note: we do this even in fast-forward mode, as we need to maintain
+ * the snapshot correctly.
+ */
+ ReorderBufferXidSetCatalogChanges(ctx->reorder, xid, buf->origptr);
+
if (!ctx->fast_forward)
{
xl_heap_new_cid *xlrec;
xlrec = (xl_heap_new_cid *) XLogRecGetData(buf->record);
SnapBuildProcessNewCid(builder, xid, buf->origptr, xlrec);
-
- break;
}
+ break;
case XLOG_HEAP2_REWRITE:
/*
diff --git a/src/backend/replication/logical/snapbuild.c b/src/backend/replication/logical/snapbuild.c
index d6ab1e017eb..ab5ca82ff9f 100644
--- a/src/backend/replication/logical/snapbuild.c
+++ b/src/backend/replication/logical/snapbuild.c
@@ -682,7 +682,8 @@ SnapBuildProcessChange(SnapBuild *builder, TransactionId xid, XLogRecPtr lsn)
/*
* Do CommandId/combo CID handling after reading an xl_heap_new_cid record.
* This implies that a transaction has done some form of write to system
- * catalogs.
+ * catalogs. Caller must mark the transaction as containing catalog
+ * modifications in the reorder buffer.
*/
void
SnapBuildProcessNewCid(SnapBuild *builder, TransactionId xid,
@@ -690,12 +691,6 @@ SnapBuildProcessNewCid(SnapBuild *builder, TransactionId xid,
{
CommandId cid;
- /*
- * we only log new_cid's if a catalog tuple was modified, so mark the
- * transaction as containing catalog modifications
- */
- ReorderBufferXidSetCatalogChanges(builder->reorder, xid, lsn);
-
ReorderBufferAddNewTupleCids(builder->reorder, xlrec->top_xid, lsn,
xlrec->target_locator, xlrec->target_tid,
xlrec->cmin, xlrec->cmax,
--
2.34.1
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Re: [BUG] Incorrect historic snapshot may be serialized to disk during fast-forwarding
@ 2025-12-30 01:10 Masahiko Sawada <sawada.mshk@gmail.com>
parent: ocean_li_996 <ocean_li_996@163.com>
1 sibling, 1 reply; 10+ messages in thread
From: Masahiko Sawada @ 2025-12-30 01:10 UTC (permalink / raw)
To: ocean_li_996 <ocean_li_996@163.com>; +Cc: cca5507 <cca5507@qq.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>
On Sat, Nov 22, 2025 at 10:33 PM ocean_li_996 <ocean_li_996@163.com> wrote:
>
> Hi ChangAo,
>
> At 2025-11-23 13:31:49, "cca5507" <cca5507@qq.com> wrote:
> >> The patch in attachment is better for me. What do you think?
> >
> >The v2-0001 LGTM.
> >
> >A small suggestion:
> >
> >We should move the 'break' out of the 'if', because we don't want it fall through to XLOG_HEAP2_REWRITE if we are fast-forwarding.
>
> Fair. The patch updated is provided in attachment.
>
While I agree with your analysis, I'm not sure what actual problems it
could lead to in practice. Have you had a chance to reproduce this
behavior by using DDLs instead of a user-catalog table? IIUC the
problem can occur if a transaction makes catalog changes and writes
only NEW_CID WAL records without INVALIDATION WAL records. However,
I'm not sure there are such transactions in practice. IIUC it would
not be a problem in terms of logical decoding even if we don't include
their XIDs to the snapshot if they change only user-catalog tables. It
might be more future proof to mark transactions as catalog-changed
even when fast-forwarding a NEW_CID record, as you proposed, but I'd
like to confirm the actual problems first.
Regards,
--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: [BUG] Incorrect historic snapshot may be serialized to diskduring fast-forwarding
@ 2025-12-30 02:57 cca5507 <cca5507@qq.com>
parent: Masahiko Sawada <sawada.mshk@gmail.com>
0 siblings, 1 reply; 10+ messages in thread
From: cca5507 @ 2025-12-30 02:57 UTC (permalink / raw)
To: Masahiko Sawada <sawada.mshk@gmail.com>; ocean_li_996 <ocean_li_996@163.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>
Hi,
> While I agree with your analysis, I'm not sure what actual problems it
> could lead to in practice. Have you had a chance to reproduce this
> behavior by using DDLs instead of a user-catalog table?
I'm not sure if this can be reproduced by using DDLs, but I will try.
> IIUC the problem can occur if a transaction makes catalog changes and writes
> only NEW_CID WAL records without INVALIDATION WAL records.
+1
> However, I'm not sure there are such transactions in practice.
Maybe currently not, but what about future, I'm not sure yet.
> IIUC it would not be a problem in terms of logical decoding even if we
> don't include their XIDs to the snapshot if they change only user-catalog
> tables. It might be more future proof to mark transactions as catalog-changed
> even when fast-forwarding a NEW_CID record, as you proposed, but I'd
> like to confirm the actual problems first.
Our current design of historic snapshot is to track all catalogs even if only a part
of them are useful for logical decoding. So I think this bug breaks our design even
if it's maybe ok in practice.
--
Regards,
ChangAo Chen
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: [BUG] Incorrect historic snapshot may be serialized to disk during fast-forwarding
@ 2026-01-17 15:56 cca5507 <cca5507@qq.com>
parent: cca5507 <cca5507@qq.com>
0 siblings, 1 reply; 10+ messages in thread
From: cca5507 @ 2026-01-17 15:56 UTC (permalink / raw)
To: Masahiko Sawada <sawada.mshk@gmail.com>; ocean_li_996 <ocean_li_996@163.com>; +Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>
Hi,
The commit message of 'user_catalog_table' says:
```
Replication solutions built around the logical decoding machinery
will likely need to set this operation for their configuration
tables; it might also be needed by extensions which perform table
access in their output functions.
```
So if there is an extension using the incorrect snapshot to scan user
catalog tables, it will get the wrong result, right?
--
Regards,
ChangAo Chen
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: [BUG] Incorrect historic snapshot may be serialized to disk during fast-forwarding
@ 2026-01-27 07:18 ocean_li_996 <ocean_li_996@163.com>
parent: cca5507 <cca5507@qq.com>
0 siblings, 1 reply; 10+ messages in thread
From: ocean_li_996 @ 2026-01-27 07:18 UTC (permalink / raw)
To: cca5507 <cca5507@qq.com>; +Cc: Masahiko Sawada <sawada.mshk@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>
Hi Masahiko, ChangAo,
Masahiko Sawada <sawada.mshk@gmail.com> 2025-12-30 01:10:01 wrote:
> It might be more future proof to mark transactions as catalog-changed
> even when fast-forwarding a NEW_CID record, as you proposed, but I'd
> like to confirm the actual problems first.
Yeah, I haven’t identified any actual issue in practice. My observations are
purely based on code-level logic analysis.
cca5507 <cca5507@qq.com> Sat, 17 Jan 2026 23:56:17 +0800 wrote:
> The commit message of 'user_catalog_table' says:
>
> ```
> Replication solutions built around the logical decoding machinery
> will likely need to set this operation for their configuration
> tables; it might also be needed by extensions which perform table
> access in their output functions.
> ```
>
> So if there is an extension using the incorrect snapshot to scan user
> catalog tables, it will get the wrong result, right?
I agree with your analysis. The introduction of user_catalog_table is intended to
ensure that the historical MVCC in logical replication can observe the historical
state of this table. Consequently, transactions on this table should be tracked by
the historical snapshot ― even while in the fast forward state.
Regards
Haiyang Li
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: [BUG] Incorrect historic snapshot may be serialized to disk during fast-forwarding
@ 2026-09-03 04:05 Yuya Shinde <yuya.shinde@gmail.com>
parent: ocean_li_996 <ocean_li_996@163.com>
0 siblings, 0 replies; 10+ messages in thread
From: Yuya Shinde @ 2026-09-03 04:05 UTC (permalink / raw)
To: ocean_li_996 <ocean_li_996@163.com>; +Cc: cca5507 <cca5507@qq.com>; Masahiko Sawada <sawada.mshk@gmail.com>; pgsql-hackers <pgsql-hackers@lists.postgresql.org>
Hi ChangAo, Haiyang, and Masahiko,
I reviewed v4 at the CommitFest branch d4014e6ed86. On the unpatched
parent, I reproduced both the incorrect serialized snapshot
and the resulting failure in an output plugin's catalog lookup. I verified
that v4 fixes both cases.
Following Masahiko's question about a DDL-only reproducer, I inspected the
WAL generated in my test environment by:
* CREATE TABLE
* ALTER TABLE ADD COLUMN
* CREATE INDEX
* COMMENT ON TABLE
* GRANT on a table
* DROP TABLE
Each tested case generated both XLOG_HEAP2_NEW_CID and
XLOG_XACT_INVALIDATIONS. The latter already causes
ReorderBufferXidSetCatalogChanges() to be called, including during
fast-forwarding. Therefore, these transactions were tracked as
catalog-changing even without v4, and I could not reproduce the bug with
these representative DDL statements.
I also attached a test-only patch that fast-forwards over a NEW_CID without
XLOG_XACT_INVALIDATIONS and verifies that the transaction's XID is retained
in the serialized snapshot. The test fails without v4 and passes with it.
The patch applies on top of v4-0001 and can be folded into a future
revision if the approach is acceptable.
Regards,
Yuya Shinde
On Mon, Jan 26, 2026 at 11:19 PM ocean_li_996 <ocean_li_996@163.com> wrote:
> Hi Masahiko, ChangAo,
>
> Masahiko Sawada <sawada.mshk@gmail.com> 2025-12-30 01:10:01 wrote:
> > It might be more future proof to mark transactions as catalog-changed
> > even when fast-forwarding a NEW_CID record, as you proposed, but I'd
> > like to confirm the actual problems first.
>
> Yeah, I haven’t identified any actual issue in practice. My observations
> are
> purely based on code-level logic analysis.
>
> cca5507 <cca5507@qq.com> Sat, 17 Jan 2026 23:56:17 +0800 wrote:
> > The commit message of 'user_catalog_table' says:
> >
> > ```
> > Replication solutions built around the logical decoding machinery
> > will likely need to set this operation for their configuration
> > tables; it might also be needed by extensions which perform table
> > access in their output functions.
> > ```
> >
> > So if there is an extension using the incorrect snapshot to scan user
> > catalog tables, it will get the wrong result, right?
>
> I agree with your analysis. The introduction of user_catalog_table is
> intended to
> ensure that the historical MVCC in logical replication can observe the
> historical
> state of this table. Consequently, transactions on this table should be
> tracked by
> the historical snapshot — even while in the fast forward state.
>
> Regards
> Haiyang Li
>
Attachments:
v4-0002-add-test.patch (application/octet-stream, 5.3K) download
From 02d90ab32cc88f12fb8cf3a60a001c115ffc7ae3 Mon Sep 17 00:00:00 2001
From: yuyash <yuya.shinde@gmail.com>
Date: Wed, 2 Sep 2026 20:27:19 -0700
Subject: [PATCH v4 2/2] Add test for catalog changes during fast-forward
Exercise fast-forwarding over a NEW_CID record generated by an insert
into a user catalog table. Keep an older transaction open and use
pg_logicalinspect to verify that the catalog-changing transaction's XID
is included in the serialized historic snapshot.
---
.../expected/logical_inspect.out | 30 +++++++++++++++++++
.../specs/logical_inspect.spec | 17 +++++++++--
2 files changed, 45 insertions(+), 2 deletions(-)
diff --git a/contrib/pg_logicalinspect/expected/logical_inspect.out b/contrib/pg_logicalinspect/expected/logical_inspect.out
index b343d3ad733..4a0370ae431 100644
--- a/contrib/pg_logicalinspect/expected/logical_inspect.out
+++ b/contrib/pg_logicalinspect/expected/logical_inspect.out
@@ -30,3 +30,33 @@ t
(1 row)
step s0_commit: COMMIT;
+
+starting permutation: s0_init s0_begin s0_get_xid s1_insert_user_catalog s1_checkpoint s1_advance_slot s1_check_fast_forward_snapshot s0_commit
+step s0_init: SELECT 'init' FROM pg_create_logical_replication_slot('isolation_slot', 'test_decoding');
+?column?
+--------
+init
+(1 row)
+
+step s0_begin: BEGIN;
+step s0_get_xid: SELECT pg_current_xact_id() IS NOT NULL AS xid_assigned;
+xid_assigned
+------------
+t
+(1 row)
+
+step s1_insert_user_catalog: INSERT INTO user_cat VALUES (1);
+step s1_checkpoint: CHECKPOINT;
+step s1_advance_slot: SELECT slot_name FROM pg_replication_slot_advance('isolation_slot', pg_current_wal_lsn());
+slot_name
+--------------
+isolation_slot
+(1 row)
+
+step s1_check_fast_forward_snapshot: SELECT count(*) > 0 AS catalog_xid_tracked FROM pg_ls_logicalsnapdir() files, pg_get_logical_snapshot_info(files.name) AS info, user_cat WHERE user_cat.xmin = ANY(info.committed_xip);
+catalog_xid_tracked
+-------------------
+t
+(1 row)
+
+step s0_commit: COMMIT;
diff --git a/contrib/pg_logicalinspect/specs/logical_inspect.spec b/contrib/pg_logicalinspect/specs/logical_inspect.spec
index 26b2db10f3e..3ee8bcb0db5 100644
--- a/contrib/pg_logicalinspect/specs/logical_inspect.spec
+++ b/contrib/pg_logicalinspect/specs/logical_inspect.spec
@@ -6,13 +6,15 @@ setup
DROP TABLE IF EXISTS tbl1;
DROP TABLE IF EXISTS tbl2;
CREATE TABLE tbl1 (val1 integer, val2 integer);
+ CREATE TABLE user_cat (val1 integer) WITH (user_catalog_table = true);
CREATE EXTENSION pg_logicalinspect;
}
teardown
{
- DROP TABLE tbl1;
- DROP TABLE tbl2;
+ DROP TABLE IF EXISTS tbl1;
+ DROP TABLE IF EXISTS tbl2;
+ DROP TABLE IF EXISTS user_cat;
SELECT 'stop' FROM pg_drop_replication_slot('isolation_slot');
DROP EXTENSION pg_logicalinspect;
}
@@ -21,6 +23,7 @@ session "s0"
setup { SET synchronous_commit=on; }
step "s0_init" { SELECT 'init' FROM pg_create_logical_replication_slot('isolation_slot', 'test_decoding'); }
step "s0_begin" { BEGIN; }
+step "s0_get_xid" { SELECT pg_current_xact_id() IS NOT NULL AS xid_assigned; }
step "s0_savepoint" { SAVEPOINT sp1; }
step "s0_truncate" { TRUNCATE tbl1; }
step "s0_commit" { COMMIT; }
@@ -29,9 +32,12 @@ session "s1"
setup { SET synchronous_commit=on; }
step "s1_checkpoint" { CHECKPOINT; }
step "s1_create_table" { CREATE TABLE tbl2 (val1 integer, val2 integer); }
+step "s1_insert_user_catalog" { INSERT INTO user_cat VALUES (1); }
+step "s1_advance_slot" { SELECT slot_name FROM pg_replication_slot_advance('isolation_slot', pg_current_wal_lsn()); }
step "s1_get_changes" { SELECT data FROM pg_logical_slot_get_changes('isolation_slot', NULL, NULL, 'skip-empty-xacts', '1', 'include-xids', '0'); }
step "s1_check_snapshot_meta" { SELECT count(meta.*) > 0 AS has_meta from pg_ls_logicalsnapdir(), pg_get_logical_snapshot_meta(name) as meta; }
step "s1_check_snapshot_info" { SELECT count(*) > 0 as has_info FROM pg_ls_logicalsnapdir(), pg_get_logical_snapshot_info(name) AS info where info.catchange_count >= 2 and array_length(info.catchange_xip,1) >= 2 and info.committed_count >= 1 and array_length(info.committed_xip,1) >= 1; }
+step "s1_check_fast_forward_snapshot" { SELECT count(*) > 0 AS catalog_xid_tracked FROM pg_ls_logicalsnapdir() files, pg_get_logical_snapshot_info(files.name) AS info, user_cat WHERE user_cat.xmin = ANY(info.committed_xip); }
# Both s0 and s1 execute catalog-change transactions. When "s1_get_changes" is
@@ -44,3 +50,10 @@ step "s1_check_snapshot_info" { SELECT count(*) > 0 as has_info FROM pg_ls_logic
# least the expected number of transactions, accounting for potential
# additional catalog changes that may occur due to concurrent autoanalyze.
permutation "s0_init" "s0_begin" "s0_savepoint" "s0_truncate" "s1_create_table" "s1_checkpoint" "s1_get_changes" "s1_check_snapshot_info" "s1_check_snapshot_meta" "s0_commit"
+
+# An INSERT into a user catalog table emits NEW_CID without a cache
+# invalidation. Keep an older transaction open so that the INSERT's XID
+# remains in the historic snapshot's interesting range. Advancing the slot
+# fast-forwards over the NEW_CID and serializes a snapshot at the checkpoint.
+# The committed XID must be present in that saved snapshot.
+permutation "s0_init" "s0_begin" "s0_get_xid" "s1_insert_user_catalog" "s1_checkpoint" "s1_advance_slot" "s1_check_fast_forward_snapshot" "s0_commit"
--
2.54.0 (Apple Git-157)
^ permalink raw reply [nested|flat] 10+ messages in thread
end of thread, other threads:[~2026-09-03 04:05 UTC | newest]
Thread overview: 10+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-11-22 08:55 [BUG] Incorrect historic snapshot may be serialized to disk during fast-forwarding cca5507 <cca5507@qq.com>
2025-11-23 02:15 ` ocean_li_996 <ocean_li_996@163.com>
2025-11-23 05:31 ` cca5507 <cca5507@qq.com>
2025-11-23 06:33 ` ocean_li_996 <ocean_li_996@163.com>
2025-12-10 05:01 ` cca5507 <cca5507@qq.com>
2025-12-30 01:10 ` Masahiko Sawada <sawada.mshk@gmail.com>
2025-12-30 02:57 ` Re: [BUG] Incorrect historic snapshot may be serialized to diskduring fast-forwarding cca5507 <cca5507@qq.com>
2026-01-17 15:56 ` cca5507 <cca5507@qq.com>
2026-01-27 07:18 ` ocean_li_996 <ocean_li_996@163.com>
2026-09-03 04:05 ` Yuya Shinde <yuya.shinde@gmail.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