Hello!

>> Variant B is not acceptable IMO as it adds a whole bunch of 
>> null-terminators unnecessarily. For example, in a simple "select 1", 
>> (expr == NULL) is true 19 times, so that is an extra 19 bytes.

> Variant B is not acceptable here.

Could we improve Variant B?

I was thinking about adding a count of NULL pointers encountered by the query
jumble walker since the last scalar or non-null field visited.
This way, we can traverse the query as follows:
====
QueryA->subNodeOne = &(Value X);
/* JumbleState->Count = 0;
   QueryA_ID = hash_any_extended(&(Value X), sizeof(Value X), QueryA_ID) */
QueryA->subNodeTwo = NULL;
/* JumbleState->Count = 1; */
QueryA->subNodeThee = NULL;
/* JumbleState->Count = 2; */
QueryA->subNodeFour = NULL;
/* JumbleState->Count = 3; */
QueryA->scalar = Value Z;
/* QueryA_ID = hash_any_extended(&(JumbleState->Count),
    sizeof(JumbleState->Count), QueryA_ID)
   JumbleState->Count = 0;
   QueryA_ID = hash_any_extended(&(Value Y), sizeof(Value Y), QueryA_ID) */
====

Variant A improvement
---------------------

I have attached the updated Variant A patch with the following changes:
- Implemented a pg_stat_statements regression test (see similar test results
  on REL_17_3 in Query-ID-collision-at_pg_stat_statements-1ea6e890b22.txt).
- Added extra description about the Query ID collision
  in src/include/nodes/parsenodes.h.



-- End of contrib/pg_stat_statements/expected/select.out for REL_17_3

CREATE TABLE pgss_c AS
  SELECT id FROM generate_series(1,2) AS id;
SELECT pg_stat_statements_reset() IS NOT NULL AS t;
 t 
---
 t
(1 row)

SELECT DISTINCT id FROM pgss_c;
 id 
----
  2
  1
(2 rows)

SELECT id FROM pgss_c ORDER BY id;
 id 
----
  1
  2
(2 rows)

SELECT id FROM pgss_c LIMIT 1;
 id 
----
  1
(1 row)

SELECT id FROM pgss_c OFFSET 1;
 id 
----
  2
(1 row)

SELECT query FROM pg_stat_statements
  WHERE query LIKE '%pgss_c%'
  ORDER BY query COLLATE "C";
             query              
--------------------------------
 SELECT DISTINCT id FROM pgss_c
 SELECT id FROM pgss_c LIMIT $1
(2 rows)

SELECT pg_stat_statements_reset() IS NOT NULL AS t;
 t 
---
 t
(1 row)

SELECT id FROM pgss_c ORDER BY id;
 id 
----
  1
  2
(2 rows)

SELECT DISTINCT id FROM pgss_c;
 id 
----
  2
  1
(2 rows)

SELECT id FROM pgss_c OFFSET 1;
 id 
----
  2
(1 row)

SELECT id FROM pgss_c LIMIT 1;
 id 
----
  1
(1 row)

SELECT query FROM pg_stat_statements
  WHERE query LIKE '%pgss_c%'
  ORDER BY query COLLATE "C";
               query               
-----------------------------------
 SELECT id FROM pgss_c OFFSET $1
 SELECT id FROM pgss_c ORDER BY id
(2 rows)

SELECT pg_stat_statements_reset() IS NOT NULL AS t;
 t 
---
 t
(1 row)

DROP TABLE pgss_c;

Attachment: v2-0001-Query-ID-Calculation-Fix-Variant-A.patch
Description: v2-0001-Query-ID-Calculation-Fix-Variant-A.patch

Reply via email to