From 38494be7d8dd33089b27af2e8b9458a62f1c9df4 Mon Sep 17 00:00:00 2001
From: Ubuntu <ubuntu@ip-172-31-46-230.ec2.internal>
Date: Thu, 18 Dec 2025 19:14:10 +0000
Subject: [PATCH v1 2/2] Make JumbleState a const pointer for plug-ins

post_parse_analyze_hook currently exposes JumbleState without
protecting it against modifications by extensions. Since JumbleState
can be shared by multiple hooks, it should be passed as a const pointer.
This signals read-only intent and ensures extensions cannot accidentally
modify it through the hook.

This change updates pg_stat_statements functions and the relevant headers
to take `const JumbleState *` where appropriate.

Discussion: https://postgr.es/m/202510281023.4u5aszccvsct%40alvherre.pgsql
---
 contrib/pg_stat_statements/pg_stat_statements.c | 8 ++++----
 src/include/nodes/queryjumble.h                 | 7 ++++++-
 src/include/parser/analyze.h                    | 2 +-
 3 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 3eb29dfbd47..508627833e2 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -332,7 +332,7 @@ static void pgss_shmem_request(void);
 static void pgss_shmem_startup(void);
 static void pgss_shmem_shutdown(int code, Datum arg);
 static void pgss_post_parse_analyze(ParseState *pstate, Query *query,
-									JumbleState *jstate);
+									const JumbleState *jstate);
 static PlannedStmt *pgss_planner(Query *parse,
 								 const char *query_string,
 								 int cursorOptions,
@@ -356,7 +356,7 @@ static void pgss_store(const char *query, int64 queryId,
 					   const BufferUsage *bufusage,
 					   const WalUsage *walusage,
 					   const struct JitInstrumentation *jitusage,
-					   JumbleState *jstate,
+					   const JumbleState *jstate,
 					   int parallel_workers_to_launch,
 					   int parallel_workers_launched,
 					   PlannedStmtOrigin planOrigin);
@@ -832,7 +832,7 @@ error:
  * Post-parse-analysis hook: mark query with a queryId
  */
 static void
-pgss_post_parse_analyze(ParseState *pstate, Query *query, JumbleState *jstate)
+pgss_post_parse_analyze(ParseState *pstate, Query *query, const JumbleState *jstate)
 {
 	if (prev_post_parse_analyze_hook)
 		prev_post_parse_analyze_hook(pstate, query, jstate);
@@ -1289,7 +1289,7 @@ pgss_store(const char *query, int64 queryId,
 		   const BufferUsage *bufusage,
 		   const WalUsage *walusage,
 		   const struct JitInstrumentation *jitusage,
-		   JumbleState *jstate,
+		   const JumbleState *jstate,
 		   int parallel_workers_to_launch,
 		   int parallel_workers_launched,
 		   PlannedStmtOrigin planOrigin)
diff --git a/src/include/nodes/queryjumble.h b/src/include/nodes/queryjumble.h
index 8207227e79e..5116896ec64 100644
--- a/src/include/nodes/queryjumble.h
+++ b/src/include/nodes/queryjumble.h
@@ -34,6 +34,11 @@ typedef struct LocationLen
 /*
  * Working state for computing a query jumble and producing a normalized
  * query string
+ *
+ * When passed to hooks (e.g., post_parse_analyze_hook), this state is
+ * intended to be read-only. Extensions should not modify the contents;
+ * hooks must receive it as a 'const JumbleState *' to enforce this
+ * intent.
  */
 typedef struct JumbleState
 {
@@ -91,7 +96,7 @@ extern PGDLLIMPORT int compute_query_id;
 
 
 extern const char *CleanQuerytext(const char *query, int *location, int *len);
-extern char *generate_normalized_query(JumbleState *jstate, const char *query,
+extern char *generate_normalized_query(const JumbleState *jstate, const char *query,
 									   int query_loc, int *query_len_p);
 extern JumbleState *JumbleQuery(Query *query);
 extern void EnableQueryId(void);
diff --git a/src/include/parser/analyze.h b/src/include/parser/analyze.h
index f29ed03b476..f80da72933c 100644
--- a/src/include/parser/analyze.h
+++ b/src/include/parser/analyze.h
@@ -21,7 +21,7 @@
 /* Hook for plugins to get control at end of parse analysis */
 typedef void (*post_parse_analyze_hook_type) (ParseState *pstate,
 											  Query *query,
-											  JumbleState *jstate);
+											  const JumbleState *jstate);
 extern PGDLLIMPORT post_parse_analyze_hook_type post_parse_analyze_hook;
 
 
-- 
2.43.0

