[COMMITTERS] pgsql: Use clock_gettime(), if available, in instr_time measurements. - Mailing list pgsql-committers

From Tom Lane
Subject [COMMITTERS] pgsql: Use clock_gettime(), if available, in instr_time measurements.
Date
Msg-id E1cO7fR-0003y0-9E@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Use clock_gettime(), if available, in instr_time measurements.

The advantage of clock_gettime() is that the API allows the result to
be precise to nanoseconds, not just microseconds as in gettimeofday().
Now that it's routinely possible to do tens of plan node executions
in 1us, we really need more precision than gettimeofday() can offer
for EXPLAIN ANALYZE to accumulate statistics with.

Some research shows that clock_gettime() is available on pretty nearly
every modern Unix-ish platform, and as far as I have been able to test,
it has about the same execution time as gettimeofday(), so there's no
loss in switching over.  (By the same token, this doesn't do anything
to fix the fact that we really wish clock readings were faster.  But
there's enough win here to justify changing anyway.)

A small side benefit is that on most platforms, we can use CLOCK_MONOTONIC
instead of CLOCK_REALTIME and thereby render EXPLAIN impervious to
concurrent resets of the system clock.  (This means that code must not
assume that the contents of struct instr_time have any well-defined
interpretation as timestamps, but really that was true before.)

Some platforms offer nonstandard clock IDs that might be of interest.
This patch knows we should use CLOCK_MONOTONIC_RAW on macOS, because it
provides more precision and is faster to read than their CLOCK_MONOTONIC.
If there turn out to be many more cases where we need special rules, it
might be appropriate to handle the selection of clock ID in configure,
but for the moment that doesn't seem worth the trouble.

Discussion: https://postgr.es/m/31856.1400021891@sss.pgh.pa.us

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/1d63f7d2d180c8708bc12710254eb7b45823440f

Modified Files
--------------
configure                            |  58 +++++++++++++++++++-
configure.in                         |   3 +-
src/include/pg_config.h.in           |   3 ++
src/include/pg_config.h.win32        |   3 ++
src/include/portability/instr_time.h | 102 +++++++++++++++++++++++++++++++++--
5 files changed, 163 insertions(+), 6 deletions(-)


pgsql-committers by date:

Previous
From: Tom Lane
Date:
Subject: [COMMITTERS] pgsql: In pgbench logging,avoid assuming that instr_times match Unix t
Next
From: Joe Conway
Date:
Subject: [COMMITTERS] pgsql: Silence Bison deprecation warnings