]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Change stat_lock.wait_time to double precision
authorMichael Paquier <michael@paquier.xyz>
Tue, 30 Jun 2026 03:47:57 +0000 (12:47 +0900)
committerMichael Paquier <michael@paquier.xyz>
Tue, 30 Jun 2026 03:47:57 +0000 (12:47 +0900)
Other statistics views (pg_stat_io, pg_stat_database, etc.) use float8
for all measured-time columns, the new pg_stat_lock standing out as an
outlier by using bigint.

This commit aligns pg_stat_lock with the other stats views for
consistency.  Like pg_stat_io, the time is stored in microseconds, and
is displayed in milliseconds with a conversion done when the view is
queried.

While on it, replace a use of "long" by PgStat_Counter, the former could
overflow for large wait times where sizeof(long) is 4 bytes (aka WIN32).

Bump catalog version.

Author: Tatsuya Kawata <kawatatatsuya0913@gmail.com>
Reviewed-by: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Discussion: https://postgr.es/m/CAHza6qerEiQehrbW5xaXyxvR0qJe3KBX1R4kocDz1+7Ygu8x-g@mail.gmail.com
Backpatch-through: 19

doc/src/sgml/monitoring.sgml
src/backend/storage/lmgr/proc.c
src/backend/utils/activity/pgstat_lock.c
src/backend/utils/adt/pgstatfuncs.c
src/include/catalog/catversion.h
src/include/catalog/pg_proc.dat
src/include/pgstat.h

index 08d5b8245529f1f6db4d6ef566a6080f1ff4011b..6dcf05eb7020f97055e9031049eed356a2ba0daf 100644 (file)
@@ -3359,7 +3359,7 @@ description | Waiting for a newly initialized WAL file to reach durable storage
      <row>
       <entry role="catalog_table_entry">
        <para role="column_definition">
-        <structfield>wait_time</structfield> <type>bigint</type>
+        <structfield>wait_time</structfield> <type>double precision</type>
        </para>
        <para>
         Total time spent waiting for locks of this type, in milliseconds.
index 6fa9de33e1ca2a3355224c6ce771ee489c10a35b..7d01c981a1f68ae88029c46ce421da0e19118916 100644 (file)
@@ -1608,12 +1608,13 @@ ProcSleep(LOCALLOCK *locallock)
                        TimestampDifference(get_timeout_start_time(DEADLOCK_TIMEOUT),
                                                                GetCurrentTimestamp(),
                                                                &secs, &usecs);
-                       msecs = secs * 1000 + usecs / 1000;
-                       usecs = usecs % 1000;
-
                        /* Increment the lock statistics counters if done waiting. */
                        if (myWaitStatus == PROC_WAIT_STATUS_OK)
-                               pgstat_count_lock_waits(locallock->tag.lock.locktag_type, msecs);
+                               pgstat_count_lock_waits(locallock->tag.lock.locktag_type,
+                                                                               (PgStat_Counter) secs * 1000000 + usecs);
+
+                       msecs = secs * 1000 + usecs / 1000;
+                       usecs = usecs % 1000;
 
                        if (log_lock_waits)
                        {
index aec64f8fb4b63cf4ef67c0e1b1466274bafd5174..8910a15634dd13e43956fa47c02feed34d7eddc0 100644 (file)
@@ -140,11 +140,11 @@ pgstat_count_lock_fastpath_exceeded(uint8 locktag_type)
  * like lock acquisitions.
  */
 void
-pgstat_count_lock_waits(uint8 locktag_type, long msecs)
+pgstat_count_lock_waits(uint8 locktag_type, PgStat_Counter usecs)
 {
        Assert(locktag_type <= LOCKTAG_LAST_TYPE);
        PendingLockStats.stats[locktag_type].waits++;
-       PendingLockStats.stats[locktag_type].wait_time += (PgStat_Counter) msecs;
+       PendingLockStats.stats[locktag_type].wait_time += usecs;
        have_lockstats = true;
        pgstat_report_fixed = true;
 }
index 6f9c9c72de561698d8440d7f6d209e53860ef565..0c59df17901a5873ee52a4f51d21d6697e1d4993 100644 (file)
@@ -1761,7 +1761,7 @@ pg_stat_get_lock(PG_FUNCTION_ARGS)
 
                values[i++] = CStringGetTextDatum(locktypename);
                values[i++] = Int64GetDatum(lck_stats->waits);
-               values[i++] = Int64GetDatum(lck_stats->wait_time);
+               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);
 
index 635c0d9cb13e7d9526e2f17e89babcae08a9643f..875a147f7533f2d01c0c2e5c3e0daf78048798d7 100644 (file)
@@ -57,6 +57,6 @@
  */
 
 /*                                                     yyyymmddN */
-#define CATALOG_VERSION_NO     202606281
+#define CATALOG_VERSION_NO     202606301
 
 #endif
index 1a985becde32def7ba7a28bc2485952b052b23a1..efe13b7866a194b456aff4e3a2adf28e5c23b120 100644 (file)
 { oid => '6509', descr => 'statistics: per lock type statistics',
   proname => 'pg_stat_get_lock', prorows => '10', proretset => 't',
   provolatile => 'v', proparallel => 'r', prorettype => 'record',
-  proargtypes => '', proallargtypes => '{text,int8,int8,int8,timestamptz}',
+  proargtypes => '', proallargtypes => '{text,int8,float8,int8,timestamptz}',
   proargmodes => '{o,o,o,o,o}',
   proargnames => '{locktype,waits,wait_time,fastpath_exceeded,stats_reset}',
   prosrc => 'pg_stat_get_lock' },
index dfa2e8376382ab8c61d6b7f8a549dbb64945232d..724966959998f94e9fa3988421135b3d418f4ced 100644 (file)
@@ -349,7 +349,7 @@ typedef struct PgStat_IO
 typedef struct PgStat_LockEntry
 {
        PgStat_Counter waits;
-       PgStat_Counter wait_time;       /* time in milliseconds */
+       PgStat_Counter wait_time;       /* time in microseconds */
        PgStat_Counter fastpath_exceeded;
 } PgStat_LockEntry;
 
@@ -638,7 +638,8 @@ extern bool pgstat_tracks_io_op(BackendType bktype, IOObject io_object,
 
 extern void pgstat_lock_flush(bool nowait);
 extern void pgstat_count_lock_fastpath_exceeded(uint8 locktag_type);
-extern void pgstat_count_lock_waits(uint8 locktag_type, long msecs);
+extern void pgstat_count_lock_waits(uint8 locktag_type,
+                                                                       PgStat_Counter usecs);
 extern PgStat_Lock *pgstat_fetch_stat_lock(void);
 
 /*