1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

Fix query jumbling to account for NULL nodes

Previously NULL nodes were ignored.  This could cause issues where the
computed query ID could match for queries where fields that are next to
each other in their Node struct where one field was NULL and the other
non-NULL.  For example, the Query struct had distinctClause and sortClause
next to each other.  If someone wrote;

SELECT DISTINCT c1 FROM t;

and then;

SELECT c1 FROM t ORDER BY c1;

these would produce the same query ID since, in the first query, we
ignored the NULL sortClause and appended the jumble bytes for the
distictClause.  In the latter query, since we did nothing for the NULL
distinctClause then jumble the non-NULL sortClause, and since the node
representation stored is the same in both cases, the query IDs were
identical.

Here we fix this by always accounting for NULL nodes by recording that
we saw a NULL in the jumble buffer.  This fixes the issue as the order that
the NULL is recorded isn't the same in the above two queries.

Author: Bykov Ivan <i.bykov@modernsys.ru>
Author: Michael Paquier <michael@paquier.xyz>
Author: David Rowley <dgrowleyml@gmail.com>
Discussion: https://postgr.es/m/aafce7966e234372b2ba876c0193f1e9%40localhost.localdomain
This commit is contained in:
David Rowley
2025-03-27 18:23:00 +13:00
parent 44fe6ceb51
commit f31aad9b07
4 changed files with 238 additions and 21 deletions

View File

@ -19,6 +19,86 @@ SELECT 1 AS "int";
1
(1 row)
-- LIMIT and OFFSET patterns
-- Try some query permutations which once produced identical query IDs
SELECT 1 AS "int" LIMIT 1;
int
-----
1
(1 row)
SELECT 1 AS "int" LIMIT 2;
int
-----
1
(1 row)
SELECT 1 AS "int" OFFSET 1;
int
-----
(0 rows)
SELECT 1 AS "int" OFFSET 2;
int
-----
(0 rows)
SELECT 1 AS "int" OFFSET 1 LIMIT 1;
int
-----
(0 rows)
SELECT 1 AS "int" OFFSET 2 LIMIT 2;
int
-----
(0 rows)
SELECT 1 AS "int" LIMIT 1 OFFSET 1;
int
-----
(0 rows)
SELECT 1 AS "int" LIMIT 3 OFFSET 3;
int
-----
(0 rows)
SELECT 1 AS "int" OFFSET 1 FETCH FIRST 2 ROW ONLY;
int
-----
(0 rows)
SELECT 1 AS "int" OFFSET 2 FETCH FIRST 3 ROW ONLY;
int
-----
(0 rows)
-- DISTINCT and ORDER BY patterns
-- Try some query permutations which once produced identical query IDs
SELECT DISTINCT 1 AS "int";
int
-----
1
(1 row)
SELECT DISTINCT 2 AS "int";
int
-----
2
(1 row)
SELECT 1 AS "int" ORDER BY 1;
int
-----
1
(1 row)
SELECT 2 AS "int" ORDER BY 1;
int
-----
2
(1 row)
/* this comment should not appear in the output */
SELECT 'hello'
-- but this one will appear
@ -135,9 +215,14 @@ SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C";
3 | 3 | SELECT $1 + $2 + $3 AS "add"
1 | 1 | SELECT $1 AS "float"
2 | 2 | SELECT $1 AS "int"
2 | 2 | SELECT $1 AS "int" LIMIT $2
2 | 0 | SELECT $1 AS "int" OFFSET $2
6 | 0 | SELECT $1 AS "int" OFFSET $2 LIMIT $3
2 | 2 | SELECT $1 AS "int" ORDER BY 1
1 | 2 | SELECT $1 AS i UNION SELECT $2 ORDER BY i
1 | 1 | SELECT $1 || $2
1 | 1 | SELECT $1, $2 LIMIT $3
2 | 2 | SELECT DISTINCT $1 AS "int"
0 | 0 | SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C"
1 | 1 | SELECT pg_stat_statements_reset() IS NOT NULL AS t
1 | 2 | WITH t(f) AS ( +
@ -145,7 +230,7 @@ SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C";
| | ) +
| | SELECT f FROM t ORDER BY f
1 | 1 | select $1::jsonb ? $2
(12 rows)
(17 rows)
SELECT pg_stat_statements_reset() IS NOT NULL AS t;
t

View File

@ -12,6 +12,26 @@ SELECT pg_stat_statements_reset() IS NOT NULL AS t;
--
SELECT 1 AS "int";
-- LIMIT and OFFSET patterns
-- Try some query permutations which once produced identical query IDs
SELECT 1 AS "int" LIMIT 1;
SELECT 1 AS "int" LIMIT 2;
SELECT 1 AS "int" OFFSET 1;
SELECT 1 AS "int" OFFSET 2;
SELECT 1 AS "int" OFFSET 1 LIMIT 1;
SELECT 1 AS "int" OFFSET 2 LIMIT 2;
SELECT 1 AS "int" LIMIT 1 OFFSET 1;
SELECT 1 AS "int" LIMIT 3 OFFSET 3;
SELECT 1 AS "int" OFFSET 1 FETCH FIRST 2 ROW ONLY;
SELECT 1 AS "int" OFFSET 2 FETCH FIRST 3 ROW ONLY;
-- DISTINCT and ORDER BY patterns
-- Try some query permutations which once produced identical query IDs
SELECT DISTINCT 1 AS "int";
SELECT DISTINCT 2 AS "int";
SELECT 1 AS "int" ORDER BY 1;
SELECT 2 AS "int" ORDER BY 1;
/* this comment should not appear in the output */
SELECT 'hello'
-- but this one will appear