Subject: [PATCH v1 1/2] Refactor pg_stat_get_lock() to use a helper function

From: Bertrand Drouvot <[email-masked]>
Date: Wed, 3 Jun 2026 13:04:26 +0000

Extract the tuple-building logic from pg_stat_get_lock() into a new
static helper pg_stat_lock_build_tuples().  This is in preparation for
pg_stat_get_backend_lock() which will reuse the same helper, following
the pattern established by pg_stat_io_build_tuples() for IO stats and
pg_stat_wal_build_tuple() for WAL stats.

Author: Bertrand Drouvot <[email-masked]>
Reviewed-by:
Discussion:
---
 src/backend/utils/adt/pgstatfuncs.c | 47 +++++++++++++++++++----------
 1 file changed, 31 insertions(+), 16 deletions(-)
 100.0% src/backend/utils/adt/

diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c
index 6f9c9c72de5..353607954ad 100644
--- a/src/backend/utils/adt/pgstatfuncs.c
+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -1737,38 +1737,53 @@ pg_stat_get_wal(PG_FUNCTION_ARGS)
 									wal_stats->stat_reset_timestamp));
 }
 
-Datum
-pg_stat_get_lock(PG_FUNCTION_ARGS)
+/*
+ * pg_stat_lock_build_tuples
+ *
+ * Helper routine for pg_stat_get_lock(), filling a result tuplestore with one
+ * tuple for each lock type.
+ */
+static void
+pg_stat_lock_build_tuples(ReturnSetInfo *rsinfo,
+						  PgStat_LockEntry *lock_stats,
+						  TimestampTz stat_reset_timestamp)
 {
 #define PG_STAT_LOCK_COLS	5
-	ReturnSetInfo *rsinfo;
-	PgStat_Lock *lock_stats;
-
-	InitMaterializedSRF(fcinfo, 0);
-	rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
-
-	lock_stats = pgstat_fetch_stat_lock();
-
 	for (int lcktype = 0; lcktype <= LOCKTAG_LAST_TYPE; lcktype++)
 	{
-		const char *locktypename;
 		Datum		values[PG_STAT_LOCK_COLS] = {0};
 		bool		nulls[PG_STAT_LOCK_COLS] = {0};
-		PgStat_LockEntry *lck_stats = &lock_stats->stats[lcktype];
+		PgStat_LockEntry *lck_stats = &lock_stats[lcktype];
 		int			i = 0;
 
-		locktypename = LockTagTypeNames[lcktype];
-
-		values[i++] = CStringGetTextDatum(locktypename);
+		values[i++] = CStringGetTextDatum(LockTagTypeNames[lcktype]);
 		values[i++] = Int64GetDatum(lck_stats->waits);
 		values[i++] = Int64GetDatum(lck_stats->wait_time);
 		values[i++] = Int64GetDatum(lck_stats->fastpath_exceeded);
-		values[i] = TimestampTzGetDatum(lock_stats->stat_reset_timestamp);
+		if (stat_reset_timestamp != 0)
+			values[i] = TimestampTzGetDatum(stat_reset_timestamp);
+		else
+			nulls[i] = true;
 
 		Assert(i + 1 == PG_STAT_LOCK_COLS);
 
 		tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
 	}
+}
+
+Datum
+pg_stat_get_lock(PG_FUNCTION_ARGS)
+{
+	ReturnSetInfo *rsinfo;
+	PgStat_Lock *lock_stats;
+
+	InitMaterializedSRF(fcinfo, 0);
+	rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
+
+	lock_stats = pgstat_fetch_stat_lock();
+
+	pg_stat_lock_build_tuples(rsinfo, lock_stats->stats,
+							  lock_stats->stat_reset_timestamp);
 
 	return (Datum) 0;
 }
-- 
2.34.1

--caEBMJLxMU8EntZL
Content-Type: text/x-diff; charset=us-ascii
Content-Disposition: attachment;
	filename="v1-0002-Add-per-backend-lock-statistics.patch"