| From: | Bernd Reiß <bd_reiss(at)gmx(dot)at> |
|---|---|
| To: | Lukas Fittl <lukas(at)fittl(dot)com>, Andres Freund <andres(at)anarazel(dot)de>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Cc: | 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 18:42:43 |
| Message-ID: | 268c1223-7622-4a5c-9390-c93f8387c838@gmx.at |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi Lukas,
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.
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.
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.
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.
Thanks for continuing to work on this. The approach looks very promising.
Regards,
Ahmed Gouda, Bernd Reiß, and Miłosz Bieniek
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Masahiko Sawada | 2026-11-30 18:44:55 | Re: Fix race between copying and dropping a replication slot |
| Previous Message | Melanie Plageman | 2026-11-30 18:35:10 | Re: Why clearing the VM doesn't require registering vm buffer in wal record |