From 3ef2f72689d30bf78c32c297f277c8370a3b0a94 Mon Sep 17 00:00:00 2001
From: alterego665 <824662526@qq.com>
Date: Mon, 9 Jun 2025 11:24:08 +0800
Subject: [PATCH v2] Add WAIT_EVENT_XACT_DONE to XactLockTableWait functions

XactLockTableWait and ConditionalXactLockTableWait previously lacked a
specific wait event, making backend states less transparent. This commit
introduces WAIT_EVENT_XACT_DONE, which both functions now report to
pg_stat_activity during their sleep phases, enhancing visibility.
---
 src/backend/storage/lmgr/lmgr.c                 | 4 ++++
 src/backend/utils/activity/wait_event_names.txt | 3 ++-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/backend/storage/lmgr/lmgr.c b/src/backend/storage/lmgr/lmgr.c
index 3f6bf70bd3c..a35de2c497a 100644
--- a/src/backend/storage/lmgr/lmgr.c
+++ b/src/backend/storage/lmgr/lmgr.c
@@ -719,7 +719,9 @@ XactLockTableWait(TransactionId xid, Relation rel, ItemPointer ctid,
 		if (!first)
 		{
 			CHECK_FOR_INTERRUPTS();
+			pgstat_report_wait_start(WAIT_EVENT_XACT_DONE);
 			pg_usleep(1000L);
+			pgstat_report_wait_end();
 		}
 		first = false;
 		xid = SubTransGetTopmostTransaction(xid);
@@ -762,7 +764,9 @@ ConditionalXactLockTableWait(TransactionId xid, bool logLockFailure)
 		if (!first)
 		{
 			CHECK_FOR_INTERRUPTS();
+			pgstat_report_wait_start(WAIT_EVENT_XACT_DONE);
 			pg_usleep(1000L);
+			pgstat_report_wait_end();
 		}
 		first = false;
 		xid = SubTransGetTopmostTransaction(xid);
diff --git a/src/backend/utils/activity/wait_event_names.txt b/src/backend/utils/activity/wait_event_names.txt
index 4da68312b5f..9be14973535 100644
--- a/src/backend/utils/activity/wait_event_names.txt
+++ b/src/backend/utils/activity/wait_event_names.txt
@@ -160,6 +160,7 @@ WAL_BUFFER_INIT	"Waiting on WAL buffer to be initialized."
 WAL_RECEIVER_EXIT	"Waiting for the WAL receiver to exit."
 WAL_RECEIVER_WAIT_START	"Waiting for startup process to send initial data for streaming replication."
 WAL_SUMMARY_READY	"Waiting for a new WAL summary to be generated."
+XACT_DONE	"Waiting for a transaction to commit or abort."
 XACT_GROUP_UPDATE	"Waiting for the group leader to update transaction status at transaction end."
 
 ABI_compatibility:
@@ -419,7 +420,7 @@ extend	"Waiting to extend a relation."
 frozenid	"Waiting to update <structname>pg_database</structname>.<structfield>datfrozenxid</structfield> and <structname>pg_database</structname>.<structfield>datminmxid</structfield>."
 page	"Waiting to acquire a lock on a page of a relation."
 tuple	"Waiting to acquire a lock on a tuple."
-transactionid	"Waiting for a transaction to finish."
+transactionid	"Waiting to acquire a transaction ID lock; see <xref linkend='transaction-id'/>."
 virtualxid	"Waiting to acquire a virtual transaction ID lock; see <xref linkend="transaction-id"/>."
 spectoken	"Waiting to acquire a speculative insertion lock."
 object	"Waiting to acquire a lock on a non-relation database object."
-- 
2.48.1

