Hi,

When working with the commit_ts module, I find the following issue:

After configure with --enable-cassert option, then initdb with:

initdb -D x2 -c track_commit_timestamp=on

Then we can get the following core dump:

 0 in raise of /lib/x86_64-linux-gnu/libc.so.6
 1 in abort of /lib/x86_64-linux-gnu/libc.so.6
 2 in ExceptionalCondition of assert.c:66
 3 in TransactionIdSetCommitTs of commit_ts.c:257
 4 in SetXidCommitTsInPage of commit_ts.c:236
 5 in TransactionTreeSetCommitTsData of commit_ts.c:192
 6 in RecordTransactionCommit of xact.c:1468
 7 in CommitTransaction of xact.c:2365
 8 in CommitTransactionCommandInternal of xact.c:3202
 9 in CommitTransactionCommand of xact.c:3163
10 in BootstrapModeMain of bootstrap.c:390
11 in main of main.c:210

The reason are TransactionIdSetCommitTs think the given xid must be
normal

static void
TransactionIdSetCommitTs(TransactionId xid, TimestampTz ts,
                                                 RepOriginId nodeid, int slotno)
{
    ...
        Assert(TransactionIdIsNormal(xid));
}

However this is not true in BootstrapMode, this failure is masked by
default because TransactionTreeSetCommitTsData returns fast when
track_commit_timestamp is off.

void
TransactionTreeSetCommitTsData(TransactionId xid, int nsubxids,
                                                           TransactionId 
*subxids, TimestampTz timestamp,
                                                           RepOriginId nodeid)
{

        /*
         * No-op if the module is not active.
         *
         */
        if (!commitTsShared->commitTsActive)
                return;

    ..        
}

I can't think out a meaningful reason to record the commit timestamp for a
BootstrapTransactionId or FrozenTransactionId, so I think bypass it in
TransactionTreeSetCommitTsData could be a solution. Another solution is
just removing the Assert in TransactionIdSetCommitTs, it works during
initdb test at least.

I include both fixes in the attachment, I think just one of them could
be adopted however.

-- 
Best Regards
Andy Fan

>From b5e921ef42763dbb1126d60313b25ae40f8ec140 Mon Sep 17 00:00:00 2001
From: Andy Fan <zhihuifan1...@163.com>
Date: Tue, 1 Jul 2025 23:50:37 +0000
Subject: [PATCH v1 1/1] Fix the Assert failure when initdb with
 track_commit_timestamp=on

the real commit message depends on which solution is adopted.
---
 src/backend/access/transam/commit_ts.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/backend/access/transam/commit_ts.c b/src/backend/access/transam/commit_ts.c
index 113fae1437a..da8bfb7167c 100644
--- a/src/backend/access/transam/commit_ts.c
+++ b/src/backend/access/transam/commit_ts.c
@@ -157,6 +157,13 @@ TransactionTreeSetCommitTsData(TransactionId xid, int nsubxids,
 	if (!commitTsShared->commitTsActive)
 		return;
 
+	/*
+	 * There is no point to record a commit_ts for BootStrapCommitTs or
+	 * FrozenTransactionId.
+	 */
+	if (unlikely(xid == BootstrapTransactionId || xid == FrozenTransactionId))
+		return;
+
 	/*
 	 * Figure out the latest Xid in this batch: either the last subxid if
 	 * there's any, otherwise the parent xid.
@@ -252,8 +259,6 @@ TransactionIdSetCommitTs(TransactionId xid, TimestampTz ts,
 	int			entryno = TransactionIdToCTsEntry(xid);
 	CommitTimestampEntry entry;
 
-	Assert(TransactionIdIsNormal(xid));
-
 	entry.time = ts;
 	entry.nodeid = nodeid;
 
-- 
2.45.1

Reply via email to