Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit130300a

Browse files
committed
pg_stat_statements: Fix parameter number gaps in normalized queries
pg_stat_statements anticipates that certain constant locations may berecorded multiple times and attempts to avoid calculating a length forthese locations in fill_in_constant_lengths().However, during generate_normalized_query() where normalized querystrings are generated, these locations are not excluded fromconsideration. This could increment the parameter number counter forevery recorded occurrence at such a location, leading to an incorrectnormalization in certain cases with gaps in the numbers reported.For example, take this query:SELECT WHERE '1' IN ('2'::int, '3'::int::text)Before this commit, it would be normalized like that, with gaps in theparameter numbers:SELECT WHERE $1 IN ($3::int, $4::int::text)However the correct, less confusing one should be like that:SELECT WHERE $1 IN ($2::int, $3::int::text)This commit fixes the computation of the parameter numbers to track thenumber of constants replaced with an $n by a separate counter instead ofthe iterator used to loop through the list of locations.The underlying query IDs are not changed, neither are the normalizedstrings for existing PGSS hash entries. New entries with freshnormalized queries would automatically get reshaped based on the newparameter numbering.Issue discovered while discussing a separate problem for HEAD, but thisaffects all the stable branches.Author: Sami Imseih <samimseih@gmail.com>Discussion:https://postgr.es/m/CAA5RZ0tzxvWXsacGyxrixdhy3tTTDfJQqxyFBRFh31nNHBQ5qA@mail.gmail.comBackpatch-through: 13
1 parent4dc642e commit130300a

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

‎contrib/pg_stat_statements/expected/pg_stat_statements.out

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,35 @@ SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
462462
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C" | 0 | 0
463463
(6 rows)
464464

465+
-- normalization of constants and parameters, with constant locations
466+
-- recorded one or more times.
467+
SELECT pg_stat_statements_reset() IS NOT NULL AS t;
468+
t
469+
---
470+
t
471+
(1 row)
472+
473+
SELECT WHERE '1' IN ('1'::int, '3'::int::text);
474+
--
475+
(1 row)
476+
477+
SELECT WHERE (1, 2) IN ((1, 2), (2, 3));
478+
--
479+
(1 row)
480+
481+
SELECT WHERE (3, 4) IN ((5, 6), (8, 7));
482+
--
483+
(0 rows)
484+
485+
SELECT query, calls FROM pg_stat_statements ORDER BY query COLLATE "C";
486+
query | calls
487+
------------------------------------------------------------------------+-------
488+
SELECT WHERE $1 IN ($2::int, $3::int::text) | 1
489+
SELECT WHERE ($1, $2) IN (($3, $4), ($5, $6)) | 2
490+
SELECT pg_stat_statements_reset() IS NOT NULL AS t | 1
491+
SELECT query, calls FROM pg_stat_statements ORDER BY query COLLATE "C" | 0
492+
(4 rows)
493+
465494
--
466495
-- queries with locking clauses
467496
--

‎contrib/pg_stat_statements/pg_stat_statements.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2674,6 +2674,7 @@ generate_normalized_query(JumbleState *jstate, const char *query,
26742674
n_quer_loc=0,/* Normalized query byte location */
26752675
last_off=0,/* Offset from start for previous tok */
26762676
last_tok_len=0;/* Length (in bytes) of that tok */
2677+
intnum_constants_replaced=0;
26772678

26782679
/*
26792680
* Get constants' lengths (core system only gives us locations). Note
@@ -2717,7 +2718,8 @@ generate_normalized_query(JumbleState *jstate, const char *query,
27172718

27182719
/* And insert a param symbol in place of the constant token */
27192720
n_quer_loc+=sprintf(norm_query+n_quer_loc,"$%d",
2720-
i+1+jstate->highest_extern_param_id);
2721+
num_constants_replaced+1+jstate->highest_extern_param_id);
2722+
num_constants_replaced++;
27212723

27222724
quer_loc=off+tok_len;
27232725
last_off=off;

‎contrib/pg_stat_statements/sql/pg_stat_statements.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,14 @@ SELECT PLUS_ONE(1);
223223

224224
SELECT query, calls, rowsFROM pg_stat_statementsORDER BY query COLLATE"C";
225225

226+
-- normalization of constants and parameters, with constant locations
227+
-- recorded one or more times.
228+
SELECT pg_stat_statements_reset()IS NOT NULLAS t;
229+
SELECTWHERE'1'IN ('1'::int,'3'::int::text);
230+
SELECTWHERE (1,2)IN ((1,2), (2,3));
231+
SELECTWHERE (3,4)IN ((5,6), (8,7));
232+
SELECT query, callsFROM pg_stat_statementsORDER BY query COLLATE"C";
233+
226234
--
227235
-- queries with locking clauses
228236
--

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp