From 48bb42245c0fe3c846512b1ced30ba210b8d4617 Mon Sep 17 00:00:00 2001
From: Sami Imseih <simseih@amazon.com>
Date: Mon, 26 May 2025 21:41:17 -0500
Subject: [PATCH v5 1/4] Fix off-by-one error in query normalization
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

In situations where a location was recorded more than
once during query jumbling, query normalization would
still account for the duplicate location—which should
have been skipped—when generating the $n values. This
led to a situation where gaps in the $n values would
appear in the final normalized string. For example:

select where '1' IN ('1'::int, '2'::int::text)

would be normalized to:

select where $1 IN ($3, $4)

instead of the correct:

select where $1 IN ($2, $3)
---
 contrib/pg_stat_statements/pg_stat_statements.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index d8fdf42df79..c58f34e9f30 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -2818,9 +2818,7 @@ generate_normalized_query(JumbleState *jstate, const char *query,
 				last_off = 0,	/* Offset from start for previous tok */
 				last_tok_len = 0;	/* Length (in bytes) of that tok */
 	bool		in_squashed = false;	/* in a run of squashed consts? */
-	int			skipped_constants = 0;	/* Position adjustment of later
-										 * constants after squashed ones */
-
+	int			num_constants_replaced = 0;
 
 	/*
 	 * Get constants' lengths (core system only gives us locations).  Note
@@ -2878,7 +2876,7 @@ generate_normalized_query(JumbleState *jstate, const char *query,
 
 			/* ... and then a param symbol replacing the constant itself */
 			n_quer_loc += sprintf(norm_query + n_quer_loc, "$%d",
-								  i + 1 + jstate->highest_extern_param_id - skipped_constants);
+								  num_constants_replaced++ + 1 + jstate->highest_extern_param_id);
 
 			/* In case previous constants were merged away, stop doing that */
 			in_squashed = false;
@@ -2902,12 +2900,10 @@ generate_normalized_query(JumbleState *jstate, const char *query,
 
 			/* ... and then start a run of squashed constants */
 			n_quer_loc += sprintf(norm_query + n_quer_loc, "$%d /*, ... */",
-								  i + 1 + jstate->highest_extern_param_id - skipped_constants);
+								  num_constants_replaced++ + 1 + jstate->highest_extern_param_id);
 
 			/* The next location will match the block below, to end the run */
 			in_squashed = true;
-
-			skipped_constants++;
 		}
 		else
 		{
-- 
2.39.5 (Apple Git-154)

