Re: Reduce timing overhead of EXPLAIN ANALYZE using rdtsc?

From: Andres Freund <andres(at)anarazel(dot)de>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: David Geier <geidav(dot)pg(at)gmail(dot)com>, vignesh C <vignesh21(at)gmail(dot)com>, Lukas Fittl <lukas(at)fittl(dot)com>, Michael Paquier <michael(at)paquier(dot)xyz>, Ibrar Ahmed <ibrar(dot)ahmad(at)gmail(dot)com>, Maciek Sakrejda <m(dot)sakrejda(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Reduce timing overhead of EXPLAIN ANALYZE using rdtsc?
Date: 2023-01-17 16:47:58
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On 2023-01-17 08:46:12 -0500, Robert Haas wrote:
> On Fri, Jan 13, 2023 at 2:56 PM Andres Freund <andres(at)anarazel(dot)de> wrote:
> > Does anybody see a reason to not move forward with this aspect? We do a fair
> > amount of INSTR_TIME_ACCUM_DIFF() etc, and that gets a good bit cheaper by
> > just using nanoseconds. We'd also save memory in BufferUsage (144-122 bytes),
> > Instrumentation (16 bytes saved in Instrumentation itself, 32 via
> > BufferUsage).

Here's an updated version of the move to representing instr_time as
nanoseconds. It's now split into a few patches:

0001) Add INSTR_TIME_SET_ZERO() calls where otherwise 0002 causes gcc to

Alternatively we can decide to deprecate INSTR_TIME_SET_ZERO() and
just allow to assign 0.

0002) Convert instr_time to uint64

This is the cleaned up version of the prior patch. The main change is
that it deduplicated a lot of the code between the architectures.


This is used in 0004. Just allows setting an instr_time to a time in
seconds, allowing for a cheaper loop exit condition in 0004.

0004) report nanoseconds in pg_test_timing

I also couldn't help and hacked a bit on the rdtsc pieces. I did figure out
how to do the cycles->nanosecond conversion with integer shift and multiply in
the common case, which does show a noticable speedup. But that's for another

I fought a bit with myself about whether to send those patches in this thread,
because it'll take over the CF entry. But decided that it's ok, given that
David's patches should be rebased over these anyway?

> I read through 0001 and it seems basically fine to me. Comments:
> 1. pg_clock_gettime_ns() doesn't follow pgindent conventions.


> 2. I'm not entirely sure that the new .?S_PER_.?S macros are
> worthwhile but maybe they are, and in any case I don't care very much.

There's now fewer. But those I'd like to keep. I just end up counting digits
manually way too many times.

> 3. I've always found 'struct timespec' to be pretty annoying
> notationally, so I like the fact that this patch would reduce use of
> it.



Andres Freund

Attachment Content-Type Size
v7-0001-Zero-initialize-instr_time-uses-causing-compiler-.patch text/x-diff 3.9 KB
v7-0002-Use-int64-to-represent-instr_time-on-all-platform.patch text/x-diff 6.6 KB
v7-0003-instr_time-Add-INSTR_TIME_SET_SECOND.patch text/x-diff 1.5 KB
v7-0004-wip-report-nanoseconds-in-pg_test_timing.patch text/x-diff 11.4 KB

In response to


Browse pgsql-hackers by date

  From Date Subject
Next Message Andres Freund 2023-01-17 17:02:40 Re: Sampling-based timing for EXPLAIN ANALYZE
Previous Message Tom Lane 2023-01-17 16:30:05 Re: Extracting cross-version-upgrade knowledge from buildfarm client