From: Michael Paquier <michael@paquier.xyz>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: pg_stat_statements: Fix toplevel tracking across nesting levels
Date: Tue, 27 Oct 2026 06:27:58 +0000
Message-ID: <E1xLaf8-00000000wON-2CZR@gemulon.postgresql.org> (raw)
pg_stat_statements: Fix toplevel tracking across nesting levels
pg_stat_statements decides whether a query is toplevel from the
nesting level at the time each hook runs. A QueryDesc can outlive the
level it started at. For example, a cursor's query starts at level 1,
inside DECLARE, but if the cursor is closed implicitly at COMMIT,
ExecutorEnd runs at level 0. The entry created for the query at parse
analysis has toplevel = false, so ExecutorEnd misses it and stores a
duplicate with the raw query text and toplevel = true.
The same happens with track = top when another extension such as
auto_explain requests query instrumentation. ExecutorEnd stores the
query even though ExecutorStart did not track it.
PGSS is upgraded to save the nesting level in the extension state of the
QueryDesc when ExecutorStart() tracks a query. ExecutorEnd() then uses
it in place of the current level and skips the query if no state was
saved.
For now, only the nesting level is tracked in the private extension
state attached to QueryDesc by PGSS. auto_explain is not touched
either. All these pieces are left as future improvements.
No backpatch is done, due to the ABI changes required in QueryDesc.
This relies on the work done previously in e5d5d1ad1e3a.
Reported-by: Gaurav Singh <gaurav.singh@yugabyte.com>
Author: Sami Imseih <samimseih@gmail.com>
Author: Michael Paquier <michael@paquier.xyz>
Discussion: https://postgr.es/m/CAEcQ1bZrgwKsauC=03otebkLk77Zc_hYNjhTP3OUdpXs++u+BA@mail.gmail.com
Discussion: https://postgr.es/m/CAA5RZ0v281uwSudcFrY8BymSRDM4J6PAHfzqg_9R+i7zWbTb2A@mail.gmail.com
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/23bf901fa5306b245e23ace52e5a6777ea08bed6
Modified Files
--------------
.../pg_stat_statements/expected/level_tracking.out | 25 +++++++++++++
contrib/pg_stat_statements/pg_stat_statements.c | 43 +++++++++++++++++++++-
contrib/pg_stat_statements/sql/level_tracking.sql | 11 ++++++
src/tools/pgindent/typedefs.list | 1 +
4 files changed, 78 insertions(+), 2 deletions(-)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: michael@paquier.xyz
Subject: Re: pgsql: pg_stat_statements: Fix toplevel tracking across nesting levels
In-Reply-To: <E1xLaf8-00000000wON-2CZR@gemulon.postgresql.org>
References: <E1xLaf8-00000000wON-2CZR@gemulon.postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox