postgresql/src/backend
Andres Freund 0022622c93 instrumentation: Standardize ticks to nanosecond conversion method
The timing infrastructure (INSTR_* macros) measures time elapsed using
clock_gettime() on POSIX systems, which returns the time as nanoseconds,
and QueryPerformanceCounter() on Windows, which is a specialized timing
clock source that returns a tick counter that needs to be converted to
nanoseconds using the result of QueryPerformanceFrequency().

This conversion currently happens ad-hoc on Windows, e.g. when calling
INSTR_TIME_GET_NANOSEC, which calls QueryPerformanceFrequency() on every
invocation, despite the frequency being stable after program start,
incurring unnecessary overhead. It also causes a fractured implementation
where macros are defined differently between platforms.

To ease code readability, and prepare for a future change that intends
to use a ticks-to-nanosecond conversion on x86-64 for TSC use, introduce
new pg_ticks_to_ns() / pg_ns_to_ticks() functions that get called from
INSTR_* macros on all platforms.

These functions rely on a separately initialized ticks_per_ns_scaled
value, that represents the conversion ratio. This value is initialized
from QueryPerformanceFrequency() on Windows, and set to zero on x86-64
POSIX systems, which results in the ticks being treated as nanoseconds.
Other architectures always directly return the original ticks.

To support this, pg_initialize_timing() is introduced, and is now
mandatory for both the backend and any frontend programs to call before
utilizing INSTR_* macros.

In passing, fix variable names in comment documenting INSTR_TIME_ADD_NANOSEC().

Author: Lukas Fittl <lukas@fittl.com>
Author: David Geier <geidav.pg@gmail.com>
Author: Andres Freund <andres@anarazel.de>
Reviewed-by: Andres Freund <andres@anarazel.de>
Reviewed-by: David Geier <geidav.pg@gmail.com>
Reviewed-by: Lukas Fittl <lukas@fittl.com>
Reviewed-by: Zsolt Parragi <zsolt.parragi@percona.com>
Discussion: https://www.postgresql.org/message-id/flat/20200612232810.f46nbqkdhbutzqdg%40alap3.anarazel.de
2026-04-07 13:00:24 -04:00
..
access Fix harmless leftover in _hash_kill_items() 2026-04-07 17:38:11 +03:00
archive Don't include wait_event.h in pgstat.h 2026-03-06 16:24:58 +01:00
backup Online enabling and disabling of data checksums 2026-04-03 22:58:51 +02:00
bootstrap Use PG_DATA_CHECKSUM_OFF instead of hardcoded value 2026-04-06 22:11:53 +02:00
catalog Add pg_stat_autovacuum_scores system view. 2026-04-06 16:56:33 -05:00
commands Reserve replication slots specifically for REPACK 2026-04-07 16:55:29 +02:00
executor Use add_size/mul_size for index instrumentation size calculations 2026-04-07 12:47:28 +02:00
foreign Remove bits* typedefs. 2026-03-30 16:12:08 -05:00
jit jit: No backport::SectionMemoryManager for LLVM 22. 2026-04-03 14:55:11 +13:00
lib dshash: Make it possible to suppress out of memory errors 2026-03-19 11:51:17 -04:00
libpq oauth: Allow validators to register custom HBA options 2026-04-07 08:15:19 -07:00
main Update copyright for 2026 2026-01-01 13:24:10 -05:00
nodes Mark JumbleState as a const in the post_parse_analyze hook 2026-04-07 15:22:49 +09:00
optimizer Fix volatile function evaluation in eager aggregation 2026-04-06 11:54:08 +09:00
parser Support more object types within CREATE SCHEMA. 2026-04-06 15:16:25 -04:00
partitioning Thread flags through begin-scan APIs 2026-03-30 12:27:24 -04:00
po Update copyright for 2026 2026-01-01 13:24:10 -05:00
port Use the new shmem allocation functions in a few core subsystems 2026-04-06 02:12:59 +03:00
postmaster instrumentation: Standardize ticks to nanosecond conversion method 2026-04-07 13:00:24 -04:00
regex Use fallthrough attribute instead of comment 2026-02-19 08:51:12 +01:00
replication Reserve replication slots specifically for REPACK 2026-04-07 16:55:29 +02:00
rewrite Add UPDATE/DELETE FOR PORTION OF 2026-04-01 19:06:03 +02:00
snowball Fix meson build of snowball code. 2026-01-05 16:51:36 -05:00
statistics Add support for extended statistics on virtual generated columns. 2026-04-01 17:02:24 +01:00
storage Allow logical replication snapshots to be database-specific 2026-04-07 12:31:18 +02:00
tcop Add errdetail() with PID and UID about source of termination signal. 2026-04-07 10:22:33 -04:00
tsearch Require superuser to install a non-built-in selectivity estimator. 2026-02-09 10:07:31 -05:00
utils Reserve replication slots specifically for REPACK 2026-04-07 16:55:29 +02:00
.gitignore
common.mk Blind attempt to fix LLVM dependency in the backend 2022-09-15 10:53:48 +07:00
Makefile ssl: Serverside SNI support for libpq 2026-03-18 12:37:11 +01:00
meson.build Add CONCURRENTLY option to REPACK 2026-04-06 21:55:08 +02:00
nls.mk Create a separate file listing backend types 2025-09-26 15:21:49 +02:00