1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-04 20:11:56 +03:00

pg_stat_statements: Fix handling of duplicate constant locations

Two or more constants can have the same location.  We handled this
correctly for non squashed constants, but failed to do it if squashed
(resulting in out-of-bounds memory access), because the code structure
became broken by commit 0f65f3eec4: we failed to update 'last_loc'
correctly when skipping these squashed constants.

The simplest fix seems to be to get rid of 'last_loc' altogether -- in
hindsight, it's quite pointless.  Also, when ignoring a constant because
of this, make sure to fulfill fill_in_constant_lengths's duty of setting
its length to -1.

Lastly, we can use == instead of <= because the locations have been
sorted beforehand, so the < case cannot arise.

Co-authored-by: Sami Imseih <samimseih@gmail.com>
Co-authored-by: Dmitry Dolgov <9erthalion6@gmail.com>
Reported-by: Konstantin Knizhnik <knizhnik@garret.ru>
Backpatch-through: 18
Discussion: https://www.postgresql.org/message-id/2b91e358-0d99-43f7-be44-d2d4dbce37b3%40garret.ru
This commit is contained in:
Álvaro Herrera
2025-10-29 12:35:02 +01:00
parent a92bbffbc3
commit b1635c1666
3 changed files with 123 additions and 16 deletions

View File

@@ -291,6 +291,30 @@ select where '1' IN ('1'::int::text, '2'::int::text);
select where '1' = ANY (array['1'::int::text, '2'::int::text]);
SELECT query, calls FROM pg_stat_statements ORDER BY query COLLATE "C";
-- composite function with row expansion
create table test_composite(x integer);
CREATE FUNCTION composite_f(a integer[], out x integer, out y integer) returns
record as $$ begin
x = a[1];
y = a[2];
end;
$$ language plpgsql;
SELECT pg_stat_statements_reset() IS NOT NULL AS t;
SELECT ((composite_f(array[1, 2]))).* FROM test_composite;
SELECT ((composite_f(array[1, 2, 3]))).* FROM test_composite;
SELECT ((composite_f(array[1, 2, 3]))).*, 1, 2, 3, ((composite_f(array[1, 2, 3]))).*, 1, 2
FROM test_composite
WHERE x IN (1, 2, 3);
SELECT ((composite_f(array[1, $1, 3]))).*, 1 FROM test_composite \bind 1
;
-- ROW() expression with row expansion
SELECT (ROW(ARRAY[1,2])).*;
SELECT (ROW(ARRAY[1, 2], ARRAY[1, 2, 3])).*;
SELECT 1, 2, (ROW(ARRAY[1, 2], ARRAY[1, 2, 3])).*, 3, 4;
SELECT (ROW(ARRAY[1, 2], ARRAY[1, $1, 3])).*, 1 \bind 1
;
SELECT query, calls FROM pg_stat_statements ORDER BY query COLLATE "C";
--
-- cleanup
--
@@ -300,3 +324,5 @@ DROP TABLE test_squash_numeric;
DROP TABLE test_squash_bigint;
DROP TABLE test_squash_cast CASCADE;
DROP TABLE test_squash_jsonb;
DROP TABLE test_composite;
DROP FUNCTION composite_f;