From 14191b284e79cabd8322220b3500909720381431 Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Mon, 23 Nov 2026 21:01:19 -0500 Subject: [PATCH] Don't report idle stats while starting a transaction ProcessInterrupts() reports pending stats when the idle stats timeout has fired and the backend is idle. It uses IsTransactionOrTransactionBlock() to check that, which looks only at the block state. When an idle backend gets a catchup interrupt, ProcessCatchupInterrupt() starts a transaction, and the block state is still TBLOCK_DEFAULT while StartTransaction() runs. If the timeout is pending at a CHECK_FOR_INTERRUPTS() in there, pgstat_report_stat() is called in TRANS_START state, and the assertion in GetCurrentTransactionStopTimestamp() fails. Fix by also checking the low-level transaction state in ProcessInterrupts(), with a new function IsTransactionStateDefault(). IsTransactionOrTransactionBlock() is not changed, because ProcessRecoveryConflictInterrupt() uses it to ignore conflicts when the backend is idle, and an idle backend that is processing a catchup interrupt should not be terminated by a recovery conflict. Discussion: https://postgr.es/m/42b2c4f9-511e-4b5c-8edd-b474019ddf29@iki.fi --- src/backend/access/transam/xact.c | 14 ++++++++++++++ src/backend/tcop/postgres.c | 6 ++++-- src/include/access/xact.h | 1 + 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c index d8d91bed427..e6abaf93cc1 100644 --- a/src/backend/access/transam/xact.c +++ b/src/backend/access/transam/xact.c @@ -5095,6 +5095,20 @@ IsTransactionOrTransactionBlock(void) return true; } +/* + * IsTransactionStateDefault --- are we completely out of a transaction? + * + * Unlike !IsTransactionOrTransactionBlock(), this is false while a + * transaction is being started. + */ +bool +IsTransactionStateDefault(void) +{ + TransactionState s = CurrentTransactionState; + + return (s->state == TRANS_DEFAULT); +} + /* * TransactionBlockStatusCode - return status code to send in ReadyForQuery */ diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index d6761aeea9f..95d08681d3c 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -3685,10 +3685,12 @@ ProcessInterrupts(void) /* * If there are pending stats updates and we currently are truly idle - * (matching the conditions in PostgresMain(), report stats now. + * (matching the conditions in PostgresMain(), report stats now. We are + * not idle while starting a transaction. */ if (IdleStatsUpdateTimeoutPending && - DoingCommandRead && !IsTransactionOrTransactionBlock()) + DoingCommandRead && !IsTransactionOrTransactionBlock() && + IsTransactionStateDefault()) { IdleStatsUpdateTimeoutPending = false; pgstat_report_stat(true); diff --git a/src/include/access/xact.h b/src/include/access/xact.h index a8cbdf247c8..13e54136a9c 100644 --- a/src/include/access/xact.h +++ b/src/include/access/xact.h @@ -486,6 +486,7 @@ extern void StartParallelWorkerTransaction(char *tstatespace); extern void EndParallelWorkerTransaction(void); extern bool IsTransactionBlock(void); extern bool IsTransactionOrTransactionBlock(void); +extern bool IsTransactionStateDefault(void); extern char TransactionBlockStatusCode(void); extern void AbortOutOfAnyTransaction(void); extern void PreventInTransactionBlock(bool isTopLevel, const char *stmtType); -- 2.37.1 (Apple Git-137.1)