pgsql: Fix query jumbling to account for NULL nodes

From: David Rowley <drowley(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix query jumbling to account for NULL nodes
Date: 2025-03-27 05:23:24
Message-ID: E1txfi8-001CxO-1c@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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(dot)bykov(at)modernsys(dot)ru>
Author: Michael Paquier <michael(at)paquier(dot)xyz>
Author: David Rowley <dgrowleyml(at)gmail(dot)com>
Discussion: https://postgr.es/m/aafce7966e234372b2ba876c0193f1e9%40localhost.localdomain

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/f31aad9b07b7a6ef693d7059830bd2f1504976ae

Modified Files
--------------
contrib/pg_stat_statements/expected/select.out | 87 ++++++++++++++-
contrib/pg_stat_statements/sql/select.sql | 20 ++++
src/backend/nodes/queryjumblefuncs.c | 140 +++++++++++++++++++++----
src/include/nodes/queryjumble.h | 12 +++
4 files changed, 238 insertions(+), 21 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message David Rowley 2025-03-27 05:35:20 pgsql: Optimize Query jumble
Previous Message Michael Paquier 2025-03-27 01:21:17 pgsql: doc: Correct description of values used in FSM for indexes