From: ocean_li_996 <ocean_li_996@163.com>
To: cca5507 <cca5507@qq.com>
Cc: pgsql-hackers <pgsql-hackers@lists.postgresql.org>
Subject: Re:[BUG] Incorrect historic snapshot may be serialized to disk during fast-forwarding
Date: Sun, 23 Nov 2025 10:15:24 +0800 (CST)
Message-ID: <1f1c51f3.4ed.19aae7e9de0.Coremail.ocean_li_996@163.com> (raw)
In-Reply-To: <tencent_3A071B760AA1A38540B57F297332B7781C08@qq.com>
References: <tencent_3A071B760AA1A38540B57F297332B7781C08@qq.com>
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) downloaddiff --git a/src/backend/replication/logical/decode.c b/src/backend/replication/logical/decode.cindex 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.cindex 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,
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: ocean_li_996@163.com, cca5507@qq.com
Subject: Re: [BUG] Incorrect historic snapshot may be serialized to disk during fast-forwarding
In-Reply-To: <1f1c51f3.4ed.19aae7e9de0.Coremail.ocean_li_996@163.com>
References: <tencent_3A071B760AA1A38540B57F297332B7781C08@qq.com> <1f1c51f3.4ed.19aae7e9de0.Coremail.ocean_li_996@163.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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