Don't report idle stats while starting a transaction ProcessInterrupts() reports pending stats when the idle stats update timeout has fired and the backend is idle, waiting for a command. It used !IsTransactionOrTransactionBlock() to check that we're not in a transaction, but that looks only at the transaction block state. When an idle backend receives a sinval catchup or notify interrupt, ProcessClientReadInterrupt() starts a transaction to process it, and the block state is still TBLOCK_DEFAULT while StartTransaction() runs. If the timeout fired and CHECK_FOR_INTERRUPTS() was called during StartTransaction(), for example while processing invalidation messages in AtStart_Cache(), pgstat_report_stat() was called in TRANS_START state, and the assertion in GetCurrentTransactionStopTimestamp() failed.
To fix, add a new function IsCompletelyOutOfTransaction(), which also checks the low-level transaction state, and use it in ProcessInterrupts(). Also use it in the sanity check assertion in pgstat_report_stat(), so that we catch such bugs earlier. When the stats report is skipped because we're in the middle of processing a catchup or notify interrupt, ProcessInterrupts() has already cleared InterruptPending, so the deferred report would not happen until something else woke up the backend. To fix that, re-arm InterruptPending and check for interrupts again in ProcessClientReadInterrupt() after processing those interrupts. That's an independent issue, but this fix makes the window for it to happen wider, so fix that too. Without assertions, the consequence is only that stats are reported in the middle of starting a transaction, which is harmless, so no backpatching. Reviewed-by: Shihao Zhong <[email protected]> Reviewed-by: Ayush Tiwari <[email protected]> Discussion: https://postgr.es/m/[email protected] Branch ------ master Details ------- https://git.postgresql.org/pg/commitdiff/ad33a140c76f53d66f37330edfa47c5d96e2d6ce Modified Files -------------- src/backend/access/transam/xact.c | 31 +++++++++++++++++++++++++++++-- src/backend/tcop/postgres.c | 22 ++++++++++++++++++++-- src/backend/utils/activity/pgstat.c | 2 +- src/include/access/xact.h | 1 + 4 files changed, 51 insertions(+), 5 deletions(-)
