Re: pg_stat_statements: Fix nested tracking for implicitly closed cursors

From: Sami Imseih <samimseih(at)gmail(dot)com>
To: Michael Paquier <michael(at)paquier(dot)xyz>
Cc: Martin Huang <jjja5555(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: pg_stat_statements: Fix nested tracking for implicitly closed cursors
Date: 2026-01-17 00:12:16
Message-ID: CAA5RZ0t59qp6jAMzir7CXpJZ+EtGQj-52eCPxQsu1MWSCe=2Ew@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> Couldn't it be possible that we reach this stack of execution with
> is_txn_end = true but we don't intend it to be so? For example,
> imagine first that we reach pgss_ProcessUtility() for a COMMIT
> TransactionStmt, second we error without resetting is_txn_end, third a
> different portal is executed a with the same backend: we could exit
> the executor with is_txn_end set and nesting_level increased but we
> did not intend these events to happen.

This is the key point to this fix. So, in v2, is_txn_end is reset at the start
of pgss_planner, pgss_post_parse_analyze and pgss_ProcessUtility. But now
I realized that is_txn_end should also be reset at
pgss_ExecutorStart/Run and should
always be reset at the end of pgss_ExecutorEnd.

So in your example above the new portal will go through one of the hooks which
will reset is_txn_end at the start. Basically, we have to go through
one of pgss_planner,
pgss_post_parse_analyze and pgss_ProcessUtility, pgss_ExecutorRun or
pgss_ExecutorStart
for any command, so resetting this value at the start of these hooks
should ensure that
is_txn_end = true does not incorrectly persist across statements. right?

( oops, I just realized my example at the start of the thread doe not indicate
that this issue only occurs with pg_stat_statements.track="all" )

--
Sami Imseih
Amazon Web Services (AWS)

Attachment Content-Type Size
v3-0001-pg_stat_statements-Fix-nested-tracking-for-implic.patch application/octet-stream 15.7 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2026-01-17 00:13:53 Re: Extended Statistics set/restore/clear functions.
Previous Message Andrei Lepikhov 2026-01-16 23:51:50 Add rows removed by hash join clause to instrumentation