Re: pg_stat_statements: Fix nested tracking for implicitly closed cursors - Mailing list pgsql-hackers

From Michael Paquier
Subject Re: pg_stat_statements: Fix nested tracking for implicitly closed cursors
Date
Msg-id aWq34FtyC6Y4pdig@paquier.xyz
Whole thread Raw
In response to Re: pg_stat_statements: Fix nested tracking for implicitly closed cursors  (Sami Imseih <samimseih@gmail.com>)
Responses Re: pg_stat_statements: Fix nested tracking for implicitly closed cursors
List pgsql-hackers
On Tue, Jan 13, 2026 at 07:01:48PM -0600, Sami Imseih wrote:
> Correct.Thanks for spotting this oversight. I think the is_txn_end flag must
> be reset in all hook functions that call pgss_store, except in pgss_ExecutorEnd,
> where we just have to check is_txn_end and increment the nesting_level. Also,
> I added a PG_TRY/FINALLY inside pgss_ExecutorEnd to ensure we reset the
> nesting_level is reset.
>
> I added a test case for the query you mentioned above.

@@ -1079,35 +1095,47 @@ pgss_ExecutorEnd(QueryDesc *queryDesc)
 {
     int64        queryId = queryDesc->plannedstmt->queryId;

-    if (queryId != INT64CONST(0) && queryDesc->totaltime &&
-        pgss_enabled(nesting_level))
+    if (is_txn_end)
+        nesting_level++;

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.
--
Michael

Attachment

pgsql-hackers by date:

Previous
From: Michael Paquier
Date:
Subject: Re: [PATCH] remove incorrect comment in pg_resetwal.c
Next
From: Masahiko Sawada
Date:
Subject: Re: POC: Parallel processing of indexes in autovacuum