pgsql: Fix regression with location calculation of nested statements - Mailing list pgsql-committers

From Michael Paquier
Subject pgsql: Fix regression with location calculation of nested statements
Date
Msg-id E1uHYCf-0002tf-2G@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Fix regression with location calculation of nested statements

The statement location calculated for some nested query cases was wrong
when multiple queries are sent as a single string, these being separated
by semicolons.  As pointed by Sami Imseih, the location calculation was
incorrect when the last query of nested statement with multiple queries
does **NOT** finish with a semicolon for the last statement.  In this
case, the statement length tracked by RawStmt is 0, which is equivalent
to say that the string should be used until its end.  The code
previously discarded this case entirely, causing the location to remain
at 0, the same as pointing at the beginning of the string.  This caused
pg_stat_statements to store incorrect query strings.

This issue has been introduced in 499edb09741b.  I have looked at the
diffs generated by pgaudit back then, and noticed the difference
generated for this nested query case, but I have missed the point that
it was an actual regression with an existing case.  A test case is added
in pg_stat_statements to provide some coverage, restoring the pre-17
behavior for the calculation of the query locations.  Special thanks to
David Steele, who, through an analysis of the test diffs generated by
pgaudit with the new v18 logic, has poked me about the fact that my
original analysis of the matter was wrong.

The test output of pg_overexplain is updated to reflect the new logic,
as the new locations refer to the beginning of the argument passed to
the function explain_filter().  When the module was introduced in
8d5ceb113e3f, which was after 499edb09741b (for the new calculation
method), the locations of the test were not actually right: the plan
generated for the query string given in input of the function pointed to
the top-level query, not the nested one.

Reported-by: David Steele <david@pgbackrest.org>
Author: Michael Paquier <michael@paquier.xyz>
Reviewed-by: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
Reviewed-by: Jian He <jian.universality@gmail.com>
Reviewed-by: Sami Imseih <samimseih@gmail.com>
Reviewed-by: David Steele <david@pgbackrest.org>
Discussion: https://postgr.es/m/844a3b38-bbf1-4fb2-9fd6-f58c35c09917@pgbackrest.org

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/06450c7b8c70120cebe91e4b6ed3cec9d7051d0a

Modified Files
--------------
contrib/pg_overexplain/expected/pg_overexplain.out |  8 ++--
.../pg_stat_statements/expected/level_tracking.out | 51 ++++++++++++++++++++++
contrib/pg_stat_statements/sql/level_tracking.sql  | 26 +++++++++++
src/backend/parser/analyze.c                       | 17 +++-----
4 files changed, 87 insertions(+), 15 deletions(-)


pgsql-committers by date:

Previous
From: Nathan Bossart
Date:
Subject: pgsql: pg_dump: Fix array literals in fetchAttributeStats().
Next
From: Fujii Masao
Date:
Subject: pgsql: Fix incorrect WAL description for PREPARE TRANSACTION record.