]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Refactor pg_stat_get_lock() to use a helper function
authorMichael Paquier <michael@paquier.xyz>
Tue, 30 Jun 2026 07:24:34 +0000 (16:24 +0900)
committerMichael Paquier <michael@paquier.xyz>
Tue, 30 Jun 2026 07:24:34 +0000 (16:24 +0900)
This commit extracts the tuple-building logic from pg_stat_get_lock()
into a new static helper pg_stat_lock_build_tuples().  This is in
preparation for a follow-up patch, to add support for backend-level lock
stats, which will reuse the same helper.

This change follows the pattern established by pg_stat_io_build_tuples()
for IO stats and pg_stat_wal_build_tuple() for WAL stats.

Author: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Reviewed-by: Tatsuya Kawata <kawatatatsuya0913@gmail.com>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Reviewed-by: Tristan Partin <tristan@partin.io>
Reviewed-by: Rui Zhao <zhaorui126@gmail.com>
Discussion: https://postgr.es/m/aiAzEY+cMQb/W8yu@bdtpg

src/backend/utils/adt/pgstatfuncs.c

index 0c59df17901a5873ee52a4f51d21d6697e1d4993..1f9165f1616054cc68c696b77cd3bd54a8b22afc 100644 (file)
@@ -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++] = Float8GetDatum(pg_stat_us_to_ms(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;
 }