From 9719223522fa3bf18adb5ba4342924dfac8fa18b Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Tue, 17 Nov 2026 00:18:16 +0200
Subject: [PATCH 2/3] Don't consider backend as fully idle when it's starting a
 transaction

Make IsTransactionOrTransactionBlock() to return true, when we're in
the middle of StartTransaction(), i.e. when we're in the TRANS_START
state.  This fixes an assertion failure in the following scenario:

1. Backend is completely idle, and receives a catchup interrupt.
   ProcessCatchupInterrupt() calls StartTransactionCommand() to start
   a new transaction

2. The "idle stats update" timeout occurs and sets
   IdleStatsUpdateTimeoutPending.

3. In StartTransaction(), the sinval messages are processed when
   AtStart_Cache() is called.  If any CHECK_FOR_INTERRUPTS() calls are
   made while processing the invalidations, it sees the pending
   IdleStatsUpdateTimeoutPending flag and calls pgstat_report_stat()

4. pgstat_report_stat() calls GetCurrentTransactionStopTimestamp()

5. GetCurrentTransactionStopTimestamp() trips the assertion because
   we're in TRANS_START state.

This commit fixes that scenario at step 3.  ProcessInterrupts() checks
!IsTransactionOrTransactionBlock() to determine if the backend is
fully idle, and with this commit, it's not considered fully idle in
the TRANS_START state anymore.

Expand the comment in IsTransactionOrTransactionBlock() about when the
backend is considered fully idle.
---
 src/backend/access/transam/xact.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 1227e3fbe24..1edda59ea32 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -5079,17 +5079,28 @@ IsTransactionBlock(void)
 
 /*
  * IsTransactionOrTransactionBlock --- are we within either a transaction
- * or a transaction block?	(The backend is only really "idle" when this
- * returns false.)
+ * or a transaction block?
  *
- * This should match up with IsTransactionBlock and IsTransactionState.
+ * The backend is only really "idle" when this returns false.  Note that the
+ * purpose of this function is different from that of IsTransactionState().
+ * IsTransactionState() is used to check if we're in a live transaction where
+ * it's safe to access the database, while !IsTransactionOrTransactionBlock()
+ * is used to check if we're completely out of a transaction.  When we're in
+ * the middle of starting or ending a transaction, we're neither in a live
+ * transaction nor completely out of a transaction.
  */
 bool
 IsTransactionOrTransactionBlock(void)
 {
 	TransactionState s = CurrentTransactionState;
 
-	if (s->blockState == TBLOCK_DEFAULT)
+	/*
+	 * blockState == TBLOCK_DEFAULT usually what indicates that there's no
+	 * transaction in progress, but also check the low-level state, in case
+	 * we're starting a transaction, or cleaning up a transaction that failed
+	 * to start.
+	 */
+	if (s->blockState == TBLOCK_DEFAULT && s->state == TRANS_DEFAULT)
 		return false;
 
 	return true;
-- 
2.47.3

