Re: Stack-based tracking of per-node WAL/buffer usage

From: Lukas Fittl <lukas(at)fittl(dot)com>
To: Bernd Reiß <bd_reiss(at)gmx(dot)at>
Cc: Andres Freund <andres(at)anarazel(dot)de>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, Tomas Vondra <tomas(at)vondra(dot)me>, Peter Smith <smithpb2250(at)gmail(dot)com>, Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>, ahmed <gouda0x(at)gmail(dot)com>, bieniek(dot)milosz0(at)gmail(dot)com
Subject: Re: Stack-based tracking of per-node WAL/buffer usage
Date: 2026-11-30 20:03:29
Message-ID: CAP53PkxopeKdwQQbWZMMELYP3pJr6NCXMDW3_jfr4Sc0r_wT-A@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Bernd, Ahmed and Miłosz,

On Mon, Nov 30, 2026 at 10:42 AM Bernd Reiß <bd_reiss(at)gmx(dot)at> wrote:
> we've been testing v18 against master and found two places where the
> per-node EXPLAIN (ANALYZE, BUFFERS) output changes. In both, the query
> totals are probably still right, but the per-node numbers mislead.
> Reproducers are below. All runs use costs off, timing off, summary off.

Thanks for testing!

I've been noodling on a v19 version for a few weeks now, and both of
these are relevant :)

>
> 1. BEFORE ROW trigger buffer usage disappears from EXPLAIN
> -----------------------------------------------------------------
>
> create table big as select g as id, repeat('x',200) pad
> from generate_series(1,50000) g;
> create table tgt(id int);
> create function trg() returns trigger language plpgsql as $$
> begin perform count(*) from big; return new; end $$;
> create trigger t before insert on tgt
> for each row execute function trg();
> vacuum analyze big;
>
> explain (analyze, buffers, costs off, timing off, summary off)
> insert into tgt select 1;
>
> master:
>
> Insert on tgt (actual rows=0.00 loops=1)
> Buffers: shared hit=1493 read=8 dirtied=1 written=1
> -> Result (actual rows=1.00 loops=1)
> Trigger t: calls=1
>
> v18:
>
> Insert on tgt (actual rows=0.00 loops=1)
> Buffers: shared hit=3 dirtied=1 written=1
> -> Result (actual rows=1.00 loops=1)
> Trigger t: calls=1
>
> With v18, the trigger has its own stack entry which is not accumulated
> in its parent / caller, leading to EXPLAIN not printing per-trigger
> buffers. So about 1500 buffers that master attributes to the Insert node
> no longer appear anywhere in the output.

Yep, good catch - I think that's a good example of where today the
behavior of what activity gets included in the nodes is a bit
coincidental, and we have to be more intentional where we show the
activity.

The part that jumped out at me when I was digging into that issue is
that currently on master, because of when the Insert/ModifyTable node
starts/stops, BEFORE triggers get included on the node, but AFTER
triggers don't.

>
> In pg_stat_statements the numbers still show up. EXPLAIN users are
> affected though, since on master they rely on this to tell that an
> INSERT/UPDATE is slow because of its triggers.
>
> We see two ways to fix it:
>
> (a) Keep master's behaviour. When a trigger fires while a node entry is
> current, charge that firing to the node as well.
>
> (b) Accept the move off the node, but print Buffers/WAL on the "Trigger
> t:" line. This has already been suggested in [0] and we'd argue for this
> approach, since it is more informative.

Ack. We could choose either approach to fix this, but I think the more
consistent way is to do (b), and move buffer/WAL usage off the node
altogether and print it only with the trigger line for both BEFORE and
AFTER triggers.

Unless I hear other feedback until then the upcoming v19 will address
it that way.

>
> 2. SubPlan shared by several parents is charged entirely to the first
> -------------------------------------------------------------------
>
> create table s(i int);
> insert into s select g from generate_series(1,1000) g;
> create index s_i on s(i);
> create table pp(id int, v int) partition by range(id);
> create table pp1 partition of pp for values from (0) to (50000);
> create table pp2 partition of pp for values from (50000) to (100000);
> insert into pp select g, g % 1000 from generate_series(0,99999) g;
> vacuum analyze pp, s;
>
> explain (analyze, buffers, costs off, timing off, summary off)
> select count(*) from pp
> where v < (select max(i) from s where s.i = pp.v + 1);
>
> master:
>
> Aggregate (actual rows=1.00 loops=1)
> Buffers: shared hit=200445
> -> Append (actual rows=100000.00 loops=1)
> Buffers: shared hit=200445
> -> Seq Scan on pp1 pp_1 (actual rows=50000.00 loops=1)
> Filter: (v < (SubPlan expr_1))
> Buffers: shared hit=100223
> ...
> -> Index Only Scan using s_i on s (actual
> rows=1.00 loops=100000)
> ...
> Buffers: shared hit=200001
> -> Seq Scan on pp2 pp_2 (actual rows=50000.00 loops=1)
> Filter: (v < (SubPlan expr_1))
> Buffers: shared hit=100222
>
> v18:
>
> Aggregate (actual rows=1.00 loops=1)
> Buffers: shared hit=200445
> -> Append (actual rows=100000.00 loops=1)
> Buffers: shared hit=200445
> -> Seq Scan on pp1 pp_1 (actual rows=50000.00 loops=1)
> Filter: (v < (SubPlan expr_1))
> Buffers: shared hit=200223
> ...
> -> Index Only Scan using s_i on s (actual
> rows=1.00 loops=100000)
> ...
> Buffers: shared hit=200001
> -> Seq Scan on pp2 pp_2 (actual rows=50000.00 loops=1)
> Filter: (v < (SubPlan expr_1))
> Buffers: shared hit=222
>
> Buffers per node (shared hit):
>
> node master v18
> ------------------------------ ------ ------
> Aggregate / Append 200445 200445
> Seq Scan on pp1 100223 200223
> SubPlan expr_1 (loops=100000) 200001 200001
> Seq Scan on pp2 100222 222
>
> Each partition runs the subplan 50000 times at about 2 index buffers per
> call. Master shows exactly that. v18 charges all subplan executions to
> pp1, and pp2 looks as if it never ran the subplan.
>
> Our reading of the cause: each partition's qual contains its own
> SubPlanState, but both point to the same PlanState in es_subplanstates.
> planstate_tree_walker walks the subPlan list of each scan, so
> ExecFinalizeNodeInstrumentation_walker reaches the subplan twice. The
> "finalized" flag makes the second visit a no-op. Without that flag the
> subplan would be counted twice; with it, it is counted once but charged
> entirely to the first partition.
>
> We don't have any suggested fixes for this problem.

Yeah, that is a tricky one. I'm not a fan of the current behavior on
master, since it puts activity on subsequent parent nodes ("Seq Scan
on pp2 pp_2" in your example) without making it clear where it's
originating from. This is even more confusing for CTE scans, because
we show the CTE subplan underneath the top node of the query level
that defines the CTE, but attribute its activity to the CTE scan and
its parents (implicitly because of how the buffer handling works on
master).

The current output also makes it hard for tools that read EXPLAIN to
calculate "self" values (as e.g. the popular "pev2" and derivatives
do) because they can't just subtract the numbers of the child nodes -
or risk self numbers going negative (and being incorrect). pev2 solves
that today [0] by not subtracting buffer activity from subplans (only
regular child plan nodes), but I think that is effectively causing
people to misinterpret where activity happened.

I think what we should probably ultimately do is list subplans, CTEs
and InitPlans separately from the main plan tree in EXPLAIN. Then we
don't have the problem where we try to attribute it to one of multiple
potential parent nodes, and it matches what popular external tools do
in terms of display for CTEs at least. But maybe that is best
discussed separately.

For now, in v19, I intend to keep the output as it is for v18 and
document it as an intentional rule of "wherever the
subplan/CTE/InitPlan is shown in EXPLAIN, we attribute the buffer/WAL
usage to that parent". That works well in the stack-based world, and
also makes it easy for tool authors to interpret.

> Thanks for continuing to work on this. The approach looks very promising.

Thanks! Appreciate the time you all have spent testing, and more
feedback/testing always welcome.

Thanks,
Lukas

[0]: https://github.com/dalibo/pev2/blob/75fce9510dcd3646d0d68f2320f3f49ccc6efb9b/src/services/plan-service.ts#L1639

--
Lukas Fittl

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Alberto Piai 2026-11-30 20:34:46 Re: Adding a stored generated column without long-lived locks
Previous Message Antonin Houska 2026-11-30 19:48:55 Re: REPACK enhancements