Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xK7gu-00000000dtX-3Bef for pgsql-bugs@arkaria.postgresql.org; Fri, 23 Oct 2026 05:19:45 +0000 Received: from localhost ([127.0.0.1]:42562 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xK7gt-0000000ESp8-1nXo for pgsql-bugs@arkaria.postgresql.org; Fri, 23 Oct 2026 05:19:43 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]:59182) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xK7gs-0000000ESow-0pJg for pgsql-bugs@lists.postgresql.org; Fri, 23 Oct 2026 05:19:43 +0000 Received: from fout-b5-smtp.messagingengine.com ([202.12.124.148]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xK7gp-00000000VyG-0OOK for pgsql-bugs@lists.postgresql.org; Fri, 23 Oct 2026 05:19:41 +0000 Received: from phl-compute-04.internal (phl-compute-04.internal [10.202.2.44]) by mailfout.stl.internal (Postfix) with ESMTP id 5801C1D001E1 for ; Fri, 23 Oct 2026 01:19:37 -0400 (EDT) Received: from phl-frontend-04 ([10.202.2.163]) by phl-compute-04.internal (MEProxy); Fri, 23 Oct 2026 01:19:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= cc:cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1792732776; x=1792819176; bh=gLmfYN3zcy vzwAzqX+3QqRtYcJWz/DUX3JH/dgwR1l4=; b=pJF+aQd+VWdGpkWCSiG6GkjdfH dEgcpzQNVnuOtABP+fpuyr5hqzeAL6mD6VItYLnDbIvWbjxuVdXH3W8WNv5xaFnz yLxHM6H8zmK0eYWy909G0V4XeDFvv8JIzXQAc6X8mulP8IkJN3mpEOhjTcWx3TKY HfPbOfopWcMQ2IJAp08ZTSvdT+YNryjIMHyaegpD0eliMoEYJXy+hMJCj5x0dafT GVHIcPpvCyi8AF+2lj3UkQwMn3n5hwbyB4y7NVPfUeJvLCd7YKZWyRUTFiCNoaTH 3J8yvVNP1/avIktv0WE4klYK0cVYa0LnkP6YtZ8V5joT9CfAr29psGrwXWWw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1792732776; x=1792819176; bh=gLmfYN3zcyvzwAzqX+3QqRtYcJWz/DUX3JH /dgwR1l4=; b=G2OSwcKeuImHPrQ1F1Z+icQquXjxbvl903z0eRMHNgG/rgyyK4w /S1j9D8E+ULzb+vz0YH699SHtIBLIwho4tuX3H3R5KVnYLCwyD3ty8of1pZ1UxR8 G5zIrH/7eJKU4nxpcRmQ2Q98Qo0XZPhKA8gj66+nDtRw4wl10EStzivDwRxtvnM7 jjAtqEfFCViH8KMUnjefnEkv9U2fvqYLEJuKJe6rrIKSKs80ispoiZQ9zsBnR3MS 8saL7rLJD+f45KXKT9DuMw5YY7p16tcP0G8RNWAayh8xRCrSMAW9I69PgsLslhFO ZwrfGPy5nDBKy0omosqkBedO57pF28ZaHRQ== X-DKIM2-Info: draft=ietf-dkim-dkim2-spec-07; repo=github.com/dkim2wg/interop; date=2026-10-18; sw=lmtpprox; action=sign d=paquier.xyz a=rsa-sha256; DKIM2-Signature: i=1; m=1; t=1792732776; d=paquier.xyz; mf=PG1pY2hhZWxAcGFxdWllci54eXo+; rt=PHBnc3FsLWJ1Z3NAbGlzdHMucG9zdGdyZXNxbC5vcmc+; s=fm2:rsa-sha256:FjTEbCbD64BjwUFxdmBoP8gwVudt5GGOmaBLAOkl1QTwm/A berKDQYvqNRv7Lhp76rPHHUbtCbJZjZrbuCNfke3pqDjgTkrVNZXNpKT/ocpXWdv bBSY8qMr7+fyOeSiTkcwQ9GxXDquDlP9x+QzaglbEKVuR2371QjJWbswXmjB53a6 qKS2RETMWVRwuXxxuWvY0RVUSY+KpiNv08wr9BGIp1ehMKMk4BF9sHb6niRE1suI vqs1Ac3a4scIKteQFx0AkJvr6xhPg+Qn4pyfwUnPFR8QnR5S92CZm0E1UXCN42fY W40RtlmjwY3KnWuZFkz2kkdVv0W4E1lb0NY+kRw==; X-DKIM2-Info: draft=ietf-dkim-dkim2-spec-07; repo=github.com/dkim2wg/interop; date=2026-10-18; sw=lmtpprox; action=mi-m=1; hc=12; hn=cc,content-disposition,content-type,date,feedback-id,from, in-reply-to,message-id,mime-version,references,subject,to; Message-Instance: m=1; h=sha256:LfMwfSRtyddL6ve7hNP90w+vo7uGCeKAD/Bo8bWMjOE=:utpeMUyY5n/Z2e19D3BHs5FcM5cudL/4kIim/dwzwqQ=; X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: dmFkZTExBBUsz0n7Vw+HVUiZ1ZJcolJl9mmZeneJ/p6eOiquGk3fb6wP+qobJ8lI2oS6Gx HbIP9t2ATsrZ3ForfjSu21RRCJ119R22WXFb1eLyVPmNWzgJZlwZm+OnjDzt5PUH8zwjKw NYeUXJHXx0NEo3pMd8Q/STpUQ5pCYtof9EJLuFxrih0i+NCQOsEQLW6OSCNBUqM2oVy36n z1B5ziuyPn5kDFfoGi9JMA/UvUmCU3EtSBZ6SdBDsIQ/iMBmnOfUcVW4JDWfagQ0VdKKdI jfeESfx9R+ERAgpTHKXixx5+yOGQ+u16LoeXSVArciZWpACZdqM84OERnbGnozND6O5AIU 3rC0IxdfNHg/WmqTo8VnlIde9kBY5CqVoV88lZaj1zna68UYi0j0TLUqiFxsAaIP/hZGlw 6yN8u4Ecl32jwCsMsNoA9pS2QhtpvHB0BurMdGuEqDWBpg86m1Je8MCygkEv22lgFhp0SX CTy9wX0jvTqutDSv07PaAoE5D6fn6QhRHOHmw9BguBg1qmnFw36r1uanu4+MV2Agp9hYAe 2ykns7VBJThhgdBqC8rCfQ2Iw8+kw/6shmY3ARLLBESCFSDZv7AnHaj9oYQiDf63eMCy7u febPHunFDRnb8JWUxXUBV6vqBtLghcPpPqDgIBU4geXIW6cYTsNzFLMz+wzw X-ME-Proxy: Feedback-ID: i0fe9450f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 23 Oct 2026 01:19:35 -0400 (EDT) Date: Fri, 23 Oct 2026 14:19:32 +0900 From: Michael Paquier To: Gaurav Singh Cc: Sami Imseih , pgsql-bugs@lists.postgresql.org Subject: Re: pg_stat_statements stores WITH HOLD cursor query with literals Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="npAhn+8oVo44hwfX" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --npAhn+8oVo44hwfX Content-Type: multipart/mixed; boundary="SaldLe3M/oFMfPZx" Content-Disposition: inline --SaldLe3M/oFMfPZx Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Thu, Oct 22, 2026 at 12:09:56PM +0900, Michael Paquier wrote: > We have a similar concept for ExplainState with its extension_state > indexed by GetExplainExtensionId() based on an extension name. > Perhaps we should do something similar for QueryDesc? That would take > care of the conflicting states across extensions at least, giving > extensions way to store in the QueryDesc what they want. Here, that's > the nesting_level that you want to track *only* in PGSS and have no > other extensions meddle with it. This was itching me, so I have looked at what it would look like, finishing with the attached set of two patches: - One to introduce a backend-side facility to give for extensions a way to plug in custom data into QueryDesc. - The second patch is a refactored v2, that makes use of the new infra. That feels rather neat. This facility could be extended to other things, like auto_explain perhaps? -- Michael --SaldLe3M/oFMfPZx Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v3-0001-Add-custom-extension-states-to-QueryDesc.patch Content-Transfer-Encoding: quoted-printable =46rom 416354866d1379793da8d95e754b676c933228e9 Mon Sep 17 00:00:00 2001 =46rom: Michael Paquier Date: Fri, 23 Oct 2026 14:05:23 +0900 Subject: [PATCH v3 1/2] Add custom extension states to QueryDesc Extensions sometimes need to carry private data from one executor hook to another for a given query. ExecutorEnd() can run in a different context than ExecutorStart(), so backend-wide variables cannot be used for that. This commit adds a way for loadable modules to attach private state to a QueryDesc, modeled on the extension state of PlannerGlobal, PlannerInfo, RelOptInfo and ExplainState. GetQueryDescExtensionId() maps an extension name to a backend-local ID. SetQueryDescExtensionState() and GetQueryDescExtensionState() store and fetch an opaque pointer for that ID. The array of pointers is allocated in the memory context of the QueryDesc, and is freed by FreeQueryDesc(). What the pointers refer to is owned by the extensions. There are no users of this facility yet. A follow-up commit will make use of it. Author: Michael Paquier Discussion: https://postgr.es/m/ --- src/include/executor/execdesc.h | 29 ++++++++++ src/backend/tcop/pquery.c | 95 ++++++++++++++++++++++++++++++++- 2 files changed, 123 insertions(+), 1 deletion(-) diff --git a/src/include/executor/execdesc.h b/src/include/executor/execdes= c.h index 37c2576e4bc9..b83efbd3315a 100644 --- a/src/include/executor/execdesc.h +++ b/src/include/executor/execdesc.h @@ -55,6 +55,10 @@ typedef struct QueryDesc =20 /* This field is allocated by ExecutorStart if needed */ struct Instrumentation *query_instr; /* query level instrumentation */ + + /* Private state of extensions; see GetQueryDescExtensionId() */ + void **extension_state; + int extension_state_allocated; } QueryDesc; =20 /* in pquery.c */ @@ -69,4 +73,29 @@ extern QueryDesc *CreateQueryDesc(PlannedStmt *plannedst= mt, =20 extern void FreeQueryDesc(QueryDesc *qdesc); =20 +/* + * Loadable modules can attach private state to a QueryDesc. Get an integ= er + * ID for the module with GetQueryDescExtensionId(), which returns the sam= e ID + * for the same name within a backend, and use it with the functions below. + * The state lives as long as the QueryDesc, and is not touched by core + * otherwise, so the module is responsible for what it points to. + */ +extern int GetQueryDescExtensionId(const char *extension_name); +extern void SetQueryDescExtensionState(QueryDesc *qdesc, int extension_id, + void *opaque); + +/* + * Get extension-specific state from a QueryDesc, or NULL if none was set. + */ +static inline void * +GetQueryDescExtensionState(QueryDesc *qdesc, int extension_id) +{ + Assert(extension_id >=3D 0); + + if (extension_id >=3D qdesc->extension_state_allocated) + return NULL; + + return qdesc->extension_state[extension_id]; +} + #endif /* EXECDESC_H */ diff --git a/src/backend/tcop/pquery.c b/src/backend/tcop/pquery.c index fa2ef20f178a..a94550544cec 100644 --- a/src/backend/tcop/pquery.c +++ b/src/backend/tcop/pquery.c @@ -23,6 +23,7 @@ #include "executor/tstoreReceiver.h" #include "miscadmin.h" #include "pg_trace.h" +#include "port/pg_bitutils.h" #include "tcop/pquery.h" #include "tcop/utility.h" #include "utils/memutils.h" @@ -97,6 +98,10 @@ CreateQueryDesc(PlannedStmt *plannedstmt, /* not yet executed */ qd->already_executed =3D false; =20 + /* no extension state yet */ + qd->extension_state =3D NULL; + qd->extension_state_allocated =3D 0; + return qd; } =20 @@ -113,10 +118,98 @@ FreeQueryDesc(QueryDesc *qdesc) UnregisterSnapshot(qdesc->snapshot); UnregisterSnapshot(qdesc->crosscheck_snapshot); =20 - /* Only the QueryDesc itself need be freed */ + /* + * Free the array of extension state pointers. What these point to is + * owned by the extensions. + */ + if (qdesc->extension_state !=3D NULL) + pfree(qdesc->extension_state); + + /* Free the QueryDesc itself */ pfree(qdesc); } =20 +static const char **QueryDescExtensionNameArray =3D NULL; +static int QueryDescExtensionNamesAssigned =3D 0; +static int QueryDescExtensionNamesAllocated =3D 0; + +/* + * Map the name of an extension to an integer ID, to be used with + * SetQueryDescExtensionState() and GetQueryDescExtensionState(). + * + * Within the lifetime of a particular backend, the same name will be mapp= ed + * to the same ID every time. IDs are not stable across backends. + */ +int +GetQueryDescExtensionId(const char *extension_name) +{ + /* Search for an existing extension by this name; if found, return ID. */ + for (int i =3D 0; i < QueryDescExtensionNamesAssigned; ++i) + if (strcmp(QueryDescExtensionNameArray[i], extension_name) =3D=3D 0) + return i; + + /* If there is no array yet, create one. */ + if (QueryDescExtensionNameArray =3D=3D NULL) + { + QueryDescExtensionNamesAllocated =3D 16; + QueryDescExtensionNameArray =3D (const char **) + MemoryContextAlloc(TopMemoryContext, + QueryDescExtensionNamesAllocated + * sizeof(char *)); + } + + /* If there's an array but it's currently full, expand it. */ + if (QueryDescExtensionNamesAssigned >=3D QueryDescExtensionNamesAllocated) + { + int i =3D pg_nextpower2_32(QueryDescExtensionNamesAssigned + 1); + + QueryDescExtensionNameArray =3D + repalloc_array(QueryDescExtensionNameArray, const char *, i); + QueryDescExtensionNamesAllocated =3D i; + } + + /* Assign and return new ID. The name is copied, to be on the safe side.= */ + QueryDescExtensionNameArray[QueryDescExtensionNamesAssigned] =3D + MemoryContextStrdup(TopMemoryContext, extension_name); + return QueryDescExtensionNamesAssigned++; +} + +/* + * Store extension-specific state into a QueryDesc. + * + * The array of pointers is allocated in the memory context of the QueryDe= sc, + * and is freed by FreeQueryDesc(). + */ +void +SetQueryDescExtensionState(QueryDesc *qdesc, int extension_id, void *opaqu= e) +{ + Assert(extension_id >=3D 0); + + /* If there is no array yet, create one. */ + if (qdesc->extension_state =3D=3D NULL) + { + qdesc->extension_state_allocated =3D + Max(4, pg_nextpower2_32(extension_id + 1)); + qdesc->extension_state =3D + MemoryContextAllocZero(GetMemoryChunkContext(qdesc), + qdesc->extension_state_allocated + * sizeof(void *)); + } + + /* If there's an array but it's currently full, expand it. */ + if (extension_id >=3D qdesc->extension_state_allocated) + { + int i =3D pg_nextpower2_32(extension_id + 1); + + qdesc->extension_state =3D + repalloc0_array(qdesc->extension_state, void *, + qdesc->extension_state_allocated, i); + qdesc->extension_state_allocated =3D i; + } + + qdesc->extension_state[extension_id] =3D opaque; +} + =20 /* * ProcessQuery --=20 2.56.0 --SaldLe3M/oFMfPZx Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v3-0002-pg_stat_statements-Fix-toplevel-tracking-across-n.patch Content-Transfer-Encoding: quoted-printable =46rom 715a60ffd8e849da453afe9830d187f2f8ac3c32 Mon Sep 17 00:00:00 2001 =46rom: Michael Paquier Date: Fri, 23 Oct 2026 14:17:39 +0900 Subject: [PATCH v3 2/2] 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 =3D false, so ExecutorEnd misses it and stores a duplicate with the raw query text and toplevel =3D true. The same happens with track =3D 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. No backpatch is done, due to the ABI changes required. Reported-by: Gaurav Singh Author: Sami Imseih Author: Michael Paquier Discussion: https://postgr.es/m/CAEcQ1bZrgwKsauC=3D03otebkLk77Zc_hYNjhTP3OU= dpXs++u+BA@mail.gmail.com Discussion: https://postgr.es/m/CAA5RZ0v281uwSudcFrY8BymSRDM4J6PAHfzqg_9R+i= 7zWbTb2A@mail.gmail.com --- .../expected/level_tracking.out | 25 +++++++++++ .../pg_stat_statements/pg_stat_statements.c | 43 ++++++++++++++++++- .../pg_stat_statements/sql/level_tracking.sql | 11 +++++ src/tools/pgindent/typedefs.list | 1 + 4 files changed, 78 insertions(+), 2 deletions(-) diff --git a/contrib/pg_stat_statements/expected/level_tracking.out b/contr= ib/pg_stat_statements/expected/level_tracking.out index 832d65e97cad..ccebf8b29604 100644 --- a/contrib/pg_stat_statements/expected/level_tracking.out +++ b/contrib/pg_stat_statements/expected/level_tracking.out @@ -1151,6 +1151,31 @@ SELECT toplevel, calls, query FROM pg_stat_statements t | 1 | SELECT pg_stat_statements_reset() IS NOT NULL AS t (7 rows) =20 +-- DECLARE CURSOR, all-level tracking with implicit close. +SET pg_stat_statements.track =3D 'all'; +SELECT pg_stat_statements_reset() IS NOT NULL AS t; + t=20 +--- + t +(1 row) + +BEGIN; +DECLARE FOOCUR CURSOR FOR SELECT * FROM stats_track_tab WHERE x =3D 1; +FETCH FORWARD 1 FROM foocur; + x=20 +--- +(0 rows) + +COMMIT; +SELECT toplevel, calls, query FROM pg_stat_statements + WHERE query LIKE 'DECLARE FOOCUR%' + ORDER BY query COLLATE "C", toplevel; + toplevel | calls | query = =20 +----------+-------+-------------------------------------------------------= ---------------- + t | 1 | DECLARE FOOCUR CURSOR FOR SELECT * FROM stats_track_ta= b WHERE x =3D $1 + f | 1 | DECLARE FOOCUR CURSOR FOR SELECT * FROM stats_track_ta= b WHERE x =3D $1; +(2 rows) + -- DECLARE CURSOR, top-level tracking. SET pg_stat_statements.track =3D 'top'; SELECT pg_stat_statements_reset() IS NOT NULL AS t; diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_s= tat_statements/pg_stat_statements.c index 4562222c9fcf..8db4b3d6e24b 100644 --- a/contrib/pg_stat_statements/pg_stat_statements.c +++ b/contrib/pg_stat_statements/pg_stat_statements.c @@ -274,6 +274,19 @@ static const ShmemCallbacks pgss_shmem_callbacks =3D { /* Current nesting depth of planner/ExecutorRun/ProcessUtility calls */ static int nesting_level =3D 0; =20 +/* + * State saved in a QueryDesc at ExecutorStart, for use at ExecutorEnd. A + * query may end at a different nesting level than the one it started at, + * for example a cursor declared in a transaction and closed at COMMIT. + */ +typedef struct pgssQueryState +{ + int nesting_level; /* nesting level at ExecutorStart */ +} pgssQueryState; + +/* ID of pg_stat_statements for QueryDesc extension state */ +static int pgss_queryDesc_id =3D -1; + /* Saved hook values */ static post_parse_analyze_hook_type prev_post_parse_analyze_hook =3D NULL; static planner_hook_type prev_planner_hook =3D NULL; @@ -356,6 +369,7 @@ static void pgss_ProcessUtility(PlannedStmt *pstmt, con= st char *queryString, DestReceiver *dest, QueryCompletion *qc); static void pgss_store(const char *query, int64 queryId, int query_location, int query_len, + bool toplevel, pgssStoreKind kind, double total_time, uint64 rows, const BufferUsage *bufusage, @@ -474,6 +488,11 @@ _PG_init(void) */ RegisterShmemCallbacks(&pgss_shmem_callbacks); =20 + /* + * Get an ID to store state in QueryDesc. + */ + pgss_queryDesc_id =3D GetQueryDescExtensionId("pg_stat_statements"); + /* * Install hooks. */ @@ -866,6 +885,7 @@ pgss_post_parse_analyze(ParseState *pstate, Query *quer= y, const JumbleState *jst query->queryId, query->stmt_location, query->stmt_len, + nesting_level =3D=3D 0, PGSS_INVALID, 0, 0, @@ -948,6 +968,7 @@ pgss_planner(Query *parse, parse->queryId, parse->stmt_location, parse->stmt_len, + nesting_level =3D=3D 0, PGSS_PLAN, INSTR_TIME_GET_MILLISEC(duration), 0, @@ -992,6 +1013,8 @@ pgss_planner(Query *parse, static void pgss_ExecutorStart(QueryDesc *queryDesc, int eflags) { + pgssQueryState *qstate; + /* * If query has queryId zero, don't track it. This prevents double * counting of optimizable statements that are directly contained in @@ -1001,6 +1024,12 @@ pgss_ExecutorStart(QueryDesc *queryDesc, int eflags) { /* Request all summary instrumentation, i.e. timing, buffers and WAL */ queryDesc->query_instr_options |=3D INSTRUMENT_ALL; + + /* Remember the nesting level for ExecutorEnd */ + qstate =3D MemoryContextAlloc(GetMemoryChunkContext(queryDesc), + sizeof(pgssQueryState)); + qstate->nesting_level =3D nesting_level; + SetQueryDescExtensionState(queryDesc, pgss_queryDesc_id, qstate); } =20 if (prev_ExecutorStart) @@ -1058,14 +1087,22 @@ static void pgss_ExecutorEnd(QueryDesc *queryDesc) { int64 queryId =3D queryDesc->plannedstmt->queryId; + pgssQueryState *qstate =3D GetQueryDescExtensionState(queryDesc, + pgss_queryDesc_id); =20 + /* + * Use the nesting level saved at ExecutorStart, if available, since the + * query may end at a different level (e.g. a cursor closed at COMMIT). + */ if (queryId !=3D INT64CONST(0) && queryDesc->query_instr && - pgss_enabled(nesting_level)) + qstate !=3D NULL && + pgss_enabled(qstate->nesting_level)) { pgss_store(queryDesc->sourceText, queryId, queryDesc->plannedstmt->stmt_location, queryDesc->plannedstmt->stmt_len, + qstate->nesting_level =3D=3D 0, PGSS_EXEC, INSTR_TIME_GET_MILLISEC(queryDesc->query_instr->total), queryDesc->estate->es_total_processed, @@ -1202,6 +1239,7 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *q= ueryString, saved_queryId, saved_stmt_location, saved_stmt_len, + nesting_level =3D=3D 0, PGSS_EXEC, INSTR_TIME_GET_MILLISEC(duration), rows, @@ -1267,6 +1305,7 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *q= ueryString, static void pgss_store(const char *query, int64 queryId, int query_location, int query_len, + bool toplevel, pgssStoreKind kind, double total_time, uint64 rows, const BufferUsage *bufusage, @@ -1310,7 +1349,7 @@ pgss_store(const char *query, int64 queryId, key.userid =3D GetUserId(); key.dbid =3D MyDatabaseId; key.queryid =3D queryId; - key.toplevel =3D (nesting_level =3D=3D 0); + key.toplevel =3D toplevel; =20 /* Lookup the hash table entry with shared lock. */ LWLockAcquire(&pgss->lock.lock, LW_SHARED); diff --git a/contrib/pg_stat_statements/sql/level_tracking.sql b/contrib/pg= _stat_statements/sql/level_tracking.sql index 003efb8184b1..cc14a9808dd5 100644 --- a/contrib/pg_stat_statements/sql/level_tracking.sql +++ b/contrib/pg_stat_statements/sql/level_tracking.sql @@ -262,6 +262,17 @@ COMMIT; SELECT toplevel, calls, query FROM pg_stat_statements ORDER BY query COLLATE "C"; =20 +-- DECLARE CURSOR, all-level tracking with implicit close. +SET pg_stat_statements.track =3D 'all'; +SELECT pg_stat_statements_reset() IS NOT NULL AS t; +BEGIN; +DECLARE FOOCUR CURSOR FOR SELECT * FROM stats_track_tab WHERE x =3D 1; +FETCH FORWARD 1 FROM foocur; +COMMIT; +SELECT toplevel, calls, query FROM pg_stat_statements + WHERE query LIKE 'DECLARE FOOCUR%' + ORDER BY query COLLATE "C", toplevel; + -- DECLARE CURSOR, top-level tracking. SET pg_stat_statements.track =3D 'top'; SELECT pg_stat_statements_reset() IS NOT NULL AS t; diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs= =2Elist index a7a8dbdd23f7..cfef572df80f 100644 --- a/src/tools/pgindent/typedefs.list +++ b/src/tools/pgindent/typedefs.list @@ -4124,6 +4124,7 @@ pgsql_thing_t pgssEntry pgssGlobalStats pgssHashKey +pgssQueryState pgssSharedState pgssStoreKind pgssVersion --=20 2.56.0 --SaldLe3M/oFMfPZx-- --npAhn+8oVo44hwfX Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmra7mQACgkQnvQgOdby QH0DYw/7BDkLCuT1n3d1MM4KDj6vzno86wmx25c7qh1r0cCkK3GqjI0OP3ne24Aa vINKAx+0HA0JF/jE4XuklHe5O1w2gGu3ETsh9PQaMvhV1BBQYbIPHDs4tIDxGrSx nmSqDLBSzZkrpJxpVl7y0W7epiReCJtrysOZ6+O6gUxkqhHG0AeK2M7lo0Vauh+V G0bCyuPzBJ1O73JVDesySQHuRgSnqGFFzt5KPWf3WsG30YP0NhLVFG3FHxNuKdR+ vWbCBBufoMvxqrfyxslI+FtsjgSQy2mCVz9LQ1CfWHNowsG0ZJecgvw7RDN3rYDE KBT3YCqYIllZjwnEenFHhYQM9HUouiGjl8kRNcoYJKUUyoEYXwJFvyF9gNBsJUaD I7GMYWtc6vCv+fD83BkskcwSF6YbQ1Hl3bw955FFifXWcIliJJY0XXCCDaqS3H72 QrOozA7IitlWPIJn/tzbSDCn+ybaUQo7QvZTRa0SB8MzzVBg9emX9CzF3JIX/ddn puIvBIbZZb5z1/clpnLcCcrM0thlp5FSgZYoEq+JlFzntbdFnEg5PSFloZh9iMaF MAEopWqINI8dadUTUdGk/1NfSZ7OxU5vAn5iwh9vrYeV79PDk2g188nZAQmVGkRd iK/djQVkJJTYtY7rohH5cx1j9x2Tl3ppS6qsnoNwFnbE2GAsT9I= =SFU1 -----END PGP SIGNATURE----- --npAhn+8oVo44hwfX--