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: v1-0001-pg_stat_statements-Fix-normalization-of-signs-for.patch
Description: Binary data

Reply via email to