pg_stat_statements: Fix normalization of + signs for FETCH and MOVE - Mailing list pgsql-hackers

From Chao Li
Subject pg_stat_statements: Fix normalization of + signs for FETCH and MOVE
Date
Msg-id DFFF817B-7DFB-43D8-AC6F-7874716DB375@gmail.com
Whole thread
Responses Re: pg_stat_statements: Fix normalization of + signs for FETCH and MOVE
List 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

pgsql-hackers by date:

Previous
From: Kyotaro Horiguchi
Date:
Subject: Re: Improve pg_stat_statements scalability
Next
From: Henson Choi
Date:
Subject: Re: Row pattern recognition