pg_stat_statements: Fix normalization of + signs for FETCH and MOVE

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Cc: Michael Paquier <michael(dot)paquier(at)gmail(dot)com>, Sami Imseih <samimseih(at)gmail(dot)com>
Subject: pg_stat_statements: Fix normalization of + signs for FETCH and MOVE
Date: 2026-06-01 05:33:40
Message-ID: DFFF817B-7DFB-43D8-AC6F-7874716DB375@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

While revisiting feature “Show sizes of FETCH queries as constants in pg_stat_statements”, I found another issue where “+” is not properly handled.

See this repro:
```
evantest=*# fetch + 1 c;
g
---
(0 rows)

evantest=*# select calls, query from pg_stat_statements where query like 'fetch%';
calls | query
-------+--------------
1 | fetch $1 1 c
(1 row)

evantest=*# SELECT pg_stat_statements_reset();
pg_stat_statements_reset
-------------------------------
2026-06-01 08:59:19.538839+08
(1 row)

evantest=*# fetch +1 c;
g
---
(0 rows)

evantest=*# select calls, query from pg_stat_statements where query like 'fetch%';
calls | query
-------+-------------
1 | fetch $11 c
(1 row)
```

As shown above, the "+" sign is replaced separately from the integer. However, a "-" sign is handled correctly:
```
evantest=*# SELECT pg_stat_statements_reset();
pg_stat_statements_reset
-------------------------------
2026-06-01 09:09:48.776385+08
(1 row)

evantest=*# fetch - 1 c;
g
----
10
(1 row)

evantest=*# select calls, query from pg_stat_statements where query like 'fetch%';
calls | query
-------+------------
1 | fetch $1 c
(1 row)
```

The same issue exists for “MOVE” as well:
```
evantest=*# move relative + 2 c;
MOVE 0
evantest=*# select calls, query from pg_stat_statements where query like 'move%';
calls | query
-------+----------------------
1 | move relative $1 2 c
(1 row)
```

The fix seems simple, as the "-" sign is already handled:
```
/*
* We should find the token position exactly, but if we somehow
* run past it, work with that.
*/
if (yylloc >= loc)
{
if (query[loc] == '-')
{
/*
* It's a negative value - this is the one and only case
* where we replace more than a single token.
*
* Do not compensate for the special-case adjustment of
* location to that of the leading '-' operator in the
* event of a negative constant (see doNegate() in
* gram.y). It is also useful for our purposes to start
* from the minus symbol. In this way, queries like
* "select * from foo where bar = 1" and "select * from
* foo where bar = -2" can be treated similarly.
*/
```

We just need to handle "+" in the same way. See the attached patch for details.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachment Content-Type Size
v1-0001-pg_stat_statements-Fix-normalization-of-signs-for.patch application/octet-stream 5.5 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Henson Choi 2026-06-01 05:35:24 Re: Row pattern recognition
Previous Message Michael Paquier 2026-06-01 05:17:16 Re: [PATCH] Remove obsolete tupDesc assignment in extended statistics