Change stat_lock.wait_time to double precision

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
This commit is contained in:
Michael Paquier 2026-06-30 12:47:57 +09:00
parent 7e5a19a16c
commit ff6f6e0470
7 changed files with 14 additions and 12 deletions

View 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.

View 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)
{

View 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;
}

View 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);

View file

@ -57,6 +57,6 @@
*/
/* yyyymmddN */
#define CATALOG_VERSION_NO 202606281
#define CATALOG_VERSION_NO 202606301
#endif

View file

@ -6064,7 +6064,7 @@
{ 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' },

View 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);
/*