Re: [RFC PATCH v0 0/7] Add EXPLAIN ANALYZE wait event reporting

Поиск
Список
Период
Сортировка
Искать

Re: [RFC PATCH v0 0/7] Add EXPLAIN ANALYZE wait event reporting

От:
Andres Freund <andres@anarazel.de>
Дата:
Hi,

On 2026-06-22 17:34:23 -0400, Robert Haas wrote:
> > - Is the disabled hot-path overhead of checking an exported boolean in
> >   pgstat_report_wait_start/end acceptable?
>
> I'm extremely skeptical. I'm really not keen to add ANY cycles to
> pgstat_report_wait_start/end(). But the even bigger problem is that
> turning this on will result in measuring the time a LOT more times
> than we do at present, and that has significant overhead that can
> distort the results. That is a problem with EXPLAIN ANALYZE, and I
> think it could be a hugely crippling problem for EXPLAIN WAITS,
> because a single ExecProcNode call could result in many, many I/Os or
> LWLock operations.

+1.

I'm just about dead set adding even a single cycle to wait events. If anything
we need to work to make wait events cheaper, not the opposite.  Small
overheads really can show up, that's what lead me to optimize them before,
c.f.

commit 225a22b19ed2960acc8e9c0b7ae53e0e5b0eac87
Author: Andres Freund 
Date:   2021-04-03 11:44:47 -0700

    Improve efficiency of wait event reporting, remove proc.h dependency.


Several of the callers for wait events are register starved, adding more
register pressure by checking a GUC or whatnot can cause stack spills etc.


> I have always felt that any kind of wait event statistics should use a
> sampling approach.

+1.


> I'd expect a successful patch in this area to work by using a time interrupt
> for self-sampling.

I guess I was thinking of sampling all processes from one measuring process,
because that would allow you a higher sampling rate without distorting the
measured processes as much (the timer interrupt would cause some syscalls to
return with EAGAIN and such).  But I'm not sure it matters that much.



> I doubt that counting the number of times that a wait event occurs
> will tell us anything we want to know.

Matches my experience.


Greetings,

Andres Freund


Re: [RFC PATCH v0 0/7] Add EXPLAIN ANALYZE wait event reporting

От:
Ilmar Yunusov <tanswis42@gmail.com>
Дата:
Hi,

Thanks, this is useful feedback.

I agree that the current v3 exact-accounting approach has a serious problem:
it adds a branch to pgstat_report_wait_start/end(), and when enabled it measures
time at every reported wait boundary. That can distort exactly the wait-heavy
cases this feature is meant to make visible.

So I do not plan to keep pushing the current hot-path accounting model as-is.
For a next version, I think the collection model needs to avoid adding any work
to pgstat_report_wait_start/end() when the option is disabled, and also avoid
materially changing the workload being measured when the option is enabled.

I will look at a sampling-based design instead. My current thought is to narrow
the first version compared to v3: report where sampled wait time is spent during
EXPLAIN ANALYZE, and either omit wait counts or treat them as secondary. The v3
prototype was still useful to explore output shape, statement-level vs
plan-node reporting, parallel aggregation, and test stability, but the
collection model needs to change.

A few questions about the next shape:

1. Would it be better for a first version to aim at statement-level sampled
   wait event reporting only, and leave plan-node attribution for a later patch?

2. If plan-node attribution is included, does keeping track of the active
   PlanState only while EXPLAIN ANALYZE has this option enabled sound like a
   reasonable direction, provided it stays outside pgstat_report_wait_start/end()?

3. For sampling, should I focus on an external sampler rather than backend
   self-sampling with a timer, to avoid distorting the measured backend?

Regards,
Ilmar Yunusov

The new status of this patch is: Waiting on Author

Re: [RFC PATCH v0 0/7] Add EXPLAIN ANALYZE wait event reporting

От:
Robert Haas <robertmhaas@gmail.com>
Дата:
On Mon, Jul 6, 2026 at 2:48 AM Ilmar Yunusov  wrote:
> 1. Would it be better for a first version to aim at statement-level sampled
>    wait event reporting only, and leave plan-node attribution for a later patch?

I don't know.

> 2. If plan-node attribution is included, does keeping track of the active
>    PlanState only while EXPLAIN ANALYZE has this option enabled sound like a
>    reasonable direction, provided it stays outside pgstat_report_wait_start/end()?

I think yes, that sounds reasonable.

> 3. For sampling, should I focus on an external sampler rather than backend
>    self-sampling with a timer, to avoid distorting the measured backend?

I don't think the answer to this question is entirely clear. If we
self-sample, then the main challenge seems to be keeping the work done
in the interrupt handler to some acceptable small amount of stuff. If
we had only a small, fixed number of wait events, then you could
perhaps imagine a global variable that pointed to the set of counters
that we should update when the self-sampler trips, but the number of
distinct wait events is basically unbounded, we might need an
expandable array or some such thing to hold all the counters that we
want to maintain, and we definitely cannot re-palloc to enlarge that
allocation in an interrupt handler.

If, on the other hand, we use an external sampler, then that concern
largely goes away, because the code that's doing the sampling no
longer needs to run inside of an interrupt routine. But now we have
another problem: how does the external sampler know which plan node,
or even which query, is running at any given moment in time? With a
self-sampler, you can use a global variable that is updated every time
we enter or exit a plan node, or every time we enter or exit a query.
But with this design, the information has to be published someplace
where another process can see it. You could publish a PlannedStmt * or
PlanState * pointer in shared memory, I suppose, but another process
won't be able to make any sense of that value, since the object
doesn't exist in its address space.

There's probably more than one thing you could do about that. One idea
would be to let the external sampler work in terms of pointers it
can't interpret, and then have it somehow ship the resulting data back
to the process that can interpret those pointers. Another idea could
be to ship something else -- the plan_node_id instead of the PlanState
pointer? The query string instead of the PlannedStmt pointer? It's not
really clear to me. It seems like no matter what you do you need a way
to get the data back to the process that asked for profiling
originally, and I'm not clear what the mechanism should be. We could
spin up a DSA, get the profiler to attach it and write data there, and
then read the DSA after the profiler has detached, maybe? Or the
profiler could write a temp file and we could read it? There's
probably other options, too.

The currently-existing wait event sampling solutions that I know about
are not designed to be used with EXPLAIN but on a system-wide basis,
so they work per-query, not per-node, and the idea is to sample
everything and correlate samples to query text or a query ID. To me,
that seems like a more natural fit than this idea, but that's not to
say this idea can't be made to work. I just don't know exactly what
kind of design we would want.

--
Robert Haas
EDB: http://www.enterprisedb.com


FAQ