pg_stat_monitor/regression/expected
Diego Fronza c921d483a8
PG -156: replace query placeholders with actual arguments for prepared statements (#481)
* Denormalize prepared statement queries

Added support for extracting query arguments for prepared statements
when `pg_stat_monitor.pgsm_normalized_query` is off.

Previously pg_stat_monitor was unable to extract the arguments for
prepared statements, thus leaving queries with placeholders $1
.. $N instead of the actual arguments.

* Optmize query denormalization

Instead of copying original query text byte by byte, copy data between
query placeholders in chunks, example:

`INSERT INTO foo(a, b, c) VALUES('test', 100, 'test again)'`

Would result in normalized query:

`INSERT INTO foo(a, b, c) VALUES($1, $2, $3)`

The original patch would copy the parts between placeholders byte by
byte, e.g. `INSERT INTO foo(a, b, c) VALUES(`, instead we can copy this
whole block at once, 1 function call and maybe 1 buffer re-allocation
per call.

Also make use of `appendBinaryStringInfo` to avoid calculating string
length as we have this info already.

* Optmize query denormalization(2)

Avoid allocating an array of strings for extracting query argument
values, instead append the current parameter value directly in the
buffer used to store the denormalized query.

This avoids not only unnecessary memory allocations, but also copying
data between temporary memory and the buffer.

* Store denormalized query only under certain constraints

This commit introduces a little optimization along with a feature, it
stores the query in denormalized form only under the circumstances
below:

- The psgm_normalized_query GUC is disabled (off).
- The query is seem for the first time, or the query total
  execution time exceeds the mean execution time calculated for
  the previous queries.

Having the query which took most execution time along with it's
arguments could help users in further investigating performance issues.

* Fix regression tests

When query normalization is disabled utility queries like SELECT 10+20
are now stored as is, instead of SELECT $1+$2.

Also when functions or sub queries are created the arguments used
internally by the function or subqueries will be replaced by NULL instead
of $1..$N. The actual arguments will be displayed when the function or
subquery is actually invoked.

* Add query denormalization regression test for prepared statements

Ensures that the denormalization of prepared statements is working, also
ensure that a query which takes more time to execute replaces the
previous denormalized query.

* Updated pgsm_query_id regression tests

With the query dernomalization feature, having integer literals used in
sql like 1, or 2 could create some confusion on whether those are
placeholders or constant values, thus this commit updates the
pgsm_query_id regression test to use different integer literals to avoid
confusion.

* Improve query denormalization regression test

Add a new test case:

1. Execute a prepared statement with larger execution time first.
2. Execute the same prepared statement with cheap execution time.
3. Ensures that the denormalized heavy query is not replaced by the
   cheaper.

* Format source using pgindent

* Fix top query regression tests on PG 12,13

On PG 12, 13, the internal return instruction in the following function:
```
CREATE OR REPLACE FUNCTION add(int, int) RETURNS INTEGER AS
  $$
  BEGIN
     return (select $1 + $2);
  END; $$ language plpgsql;
```

Is stored as SELECT (select expr1 + expr2)

On PG 14 onward it's stored just as SELECT (expr1 + expr2)
2024-11-01 19:28:16 -03:00
..
application_name.out [PG-644] Add option to disable application name tracking (#469) 2024-07-23 18:49:33 +02:00
application_name_unique.out PG-563: Update TAP testcases and output due to changes by DEV. 2022-12-27 18:14:32 +05:00
application_name_unique_1.out PG-563: Update TAP testcases and output due to changes by DEV. 2022-12-27 18:14:32 +05:00
basic.out PG-320: Removing the query state code from the view. 2022-11-15 16:31:37 +00:00
cmd_type.out PG-588: Some queries are not being normalised. 2023-02-22 19:31:52 +05:00
cmd_type_1.out PG-544: Regression cleanup. 2022-11-16 21:31:16 +00:00
counters.out PG-570: Fix counters test case. 2023-01-03 18:50:53 +05:00
database.out PG-588: Some queries are not being normalised. 2023-02-22 19:31:52 +05:00
denorm_prepared_statements.out PG -156: replace query placeholders with actual arguments for prepared statements (#481) 2024-11-01 19:28:16 -03:00
different_parent_queries.out PG-592: Treat queries with different parent queries as separate entries (#403) 2024-08-06 23:43:48 +02:00
error.out PG-320: Removing the query state code from the view. 2022-11-15 16:31:37 +00:00
error_1.out PG-554: Remove redundant files and fix regression. (#319) 2022-11-23 02:20:43 +05:00
error_2.out PG-588: Some queries are not being normalised. 2023-02-22 19:31:52 +05:00
error_insert.out PG-563: Update TAP testcases and output due to changes by DEV. 2022-12-27 18:14:32 +05:00
error_insert_1.out PG-563: Update TAP testcases and output due to changes by DEV. 2022-12-27 18:14:32 +05:00
functions.out [PG-810] PG-17 Support (#463) 2024-07-18 14:59:57 +02:00
functions_1.out [PG-810] PG-17 Support (#463) 2024-07-18 14:59:57 +02:00
guc.out [PG-644] Add option to disable application name tracking (#469) 2024-07-23 18:49:33 +02:00
guc_1.out [PG-644] Add option to disable application name tracking (#469) 2024-07-23 18:49:33 +02:00
guc_2.out [PG-644] Add option to disable application name tracking (#469) 2024-07-23 18:49:33 +02:00
histogram.out PG-544: Regression cleanup. 2022-11-16 19:47:07 +00:00
histogram_1.out PG-544: Regression cleanup. 2022-11-16 19:47:07 +00:00
histogram_2.out PG-544: Regression cleanup. 2022-11-16 19:47:07 +00:00
level_tracking.out [PG-810] PG-17 Support (#463) 2024-07-18 14:59:57 +02:00
level_tracking_1.out [PG-810] PG-17 Support (#463) 2024-07-18 14:59:57 +02:00
level_tracking_2.out [PG-810] PG-17 Support (#463) 2024-07-18 14:59:57 +02:00
pgsm_query_id.out PG -156: replace query placeholders with actual arguments for prepared statements (#481) 2024-11-01 19:28:16 -03:00
pgsm_query_id_1.out PG -156: replace query placeholders with actual arguments for prepared statements (#481) 2024-11-01 19:28:16 -03:00
relations.out PG-563: Update TAP testcases and output due to changes by DEV. 2022-12-27 18:14:32 +05:00
relations_1.out PostgreSQL 16 support for PGSM 2023-09-12 12:45:58 +05:00
rows.out PG-588: Some queries are not being normalised. 2023-02-22 19:31:52 +05:00
tags.out PG-320: Removing the query state code from the view. 2022-11-15 16:31:37 +00:00
top_query.out PG -156: replace query placeholders with actual arguments for prepared statements (#481) 2024-11-01 19:28:16 -03:00
top_query_1.out PG -156: replace query placeholders with actual arguments for prepared statements (#481) 2024-11-01 19:28:16 -03:00
user.out PG-588: Some queries are not being normalised. 2023-02-22 19:31:52 +05:00
version.out [PG-810] PG-17 Support (#463) 2024-07-18 14:59:57 +02:00