Re: BUG #18947: TRAP: failed Assert("len_to_wrt >= 0") in pg_stat_statements

Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>

From: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
To: Michael Paquier <michael@paquier.xyz>
Cc: Fujii Masao <masao.fujii@oss.nttdata.com>, Dilip Kumar <dilipbalaut@gmail.com>, a.kozhemyakin@postgrespro.ru, pgsql-bugs@lists.postgresql.org
Date: 2025-06-11T09:04:36Z
Lists: pgsql-bugs

Attachments

Here's a v2 for the attempted fix. This is using the mentioned approach:
- SelectStmt's location is set to Select's position when the node if built
- If there are surrounding parentheses, we include them and move the
start of the statement to the outermost '('

The Statement length is only set for:
- COPY: We use the PreparableStatement's surrounding '()'  to set both
location and length (setting location should be redundant here though)
- CTAS: we set the statement's length if there's a tailing 'WITH (NO) DATA'.
- Other than those, the statement length will fallback to use the top
RawStmt's remaining length.

I haven't done it for Views and JSON_ARRAY. Their nested queries
aren't currently tracked and reported by pgss so there's no way to
test those. This could be done but there's no available way to test
this is correct AFAIK?