public inbox for [email protected]  
help / color / mirror / Atom feed
From: Chao Li <[email protected]>
To: PostgreSQL Hackers <[email protected]>
Subject: Re: Fix unsafe PlannedStmt access in pg_stat_statements
Date: Mon, 11 May 2026 16:11:41 +0800
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>



> On May 11, 2026, at 16:07, Chao Li <[email protected]> wrote:
> 
> Hi,
> 
> I spotted this small issue while working on [1].
> 
> In pgss_ProcessUtility(), there is this comment:
> ```
> /*
> * CAUTION: do not access the *pstmt data structure again below here.
> * If it was a ROLLBACK or similar, that data structure may have been
> * freed.  We must copy everything we still need into local variables,
> * which we did above.
> *
> * For the same reason, we can't risk restoring pstmt->queryId to its
> * former value, which'd otherwise be a good idea.
> */
> ```
> 
> However, commit 3357471cf9f5e470dfed0c7919bcf31c7efaf2b9 added a new access to pstmt after that point:
> ```
> pgss_store(queryString,
>   saved_queryId,
>   saved_stmt_location,
>   saved_stmt_len,
>   PGSS_EXEC,
>   INSTR_TIME_GET_MILLISEC(duration),
>   rows,
>   &bufusage,
>   &walusage,
>   NULL,
>   NULL,
>   0,
>   0,
>   pstmt->planOrigin);
> ```
> 
> The attached patch fixes this by saving pstmt->planOrigin, following the same pattern already used for queryId, stmt_location, and stmt_len.
> 
> [1] https://www.postgresql.org/message-id/8ED8C22D-54CD-4EC4-B53C-D39F935FA83D%40gmail.com
> 
> Best regards,
> --
> Chao Li (Evan)
> HighGo Software Co., Ltd.
> https://www.highgo.com/
> 

Oops! Forgot the attachment.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/






Attachments:

  [application/octet-stream] v1-0001-Fix-unsafe-PlannedStmt-access-in-pg_stat_statemen.patch (1.2K, 2-v1-0001-Fix-unsafe-PlannedStmt-access-in-pg_stat_statemen.patch)
  download | inline diff:
From 05da7b75b455b8d0e30d9a77d5fbe3ca4eccae8e Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <[email protected]>
Date: Mon, 11 May 2026 15:43:53 +0800
Subject: [PATCH v1] Fix unsafe PlannedStmt access in pg_stat_statements

Author: Chao Li <[email protected]>
---
 contrib/pg_stat_statements/pg_stat_statements.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 95a5411a39d..a2d3ab770cc 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -1099,6 +1099,7 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
 	int64		saved_queryId = pstmt->queryId;
 	int			saved_stmt_location = pstmt->stmt_location;
 	int			saved_stmt_len = pstmt->stmt_len;
+	PlannedStmtOrigin saved_planOrigin = pstmt->planOrigin;
 	bool		enabled = pgss_track_utility && pgss_enabled(nesting_level);
 
 	/*
@@ -1210,7 +1211,7 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
 				   NULL,
 				   0,
 				   0,
-				   pstmt->planOrigin);
+				   saved_planOrigin);
 	}
 	else
 	{
-- 
2.50.1 (Apple Git-155)



view thread (8+ messages)  latest in thread

reply

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 using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: Fix unsafe PlannedStmt access in pg_stat_statements
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox