Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for new columns in PG17 #3238

Merged
merged 1 commit into from
Feb 26, 2025
Merged

Conversation

troycoll
Copy link
Contributor

Adds support for new column names in pg_stat_statements in Postgres v17.

Testing notes:

  • yarn
  • yarn build
  • test on Postgres v16:
./bin/run pg:outliers HEROKU_POSTGRESQL_TCOLL_GRAY_URL -a tc-data-dev
 total_exec_time | prop_exec_time | ncalls |  sync_io_time   |                                                              query                                                               
-----------------+----------------+--------+-----------------+----------------------------------------------------------------------------------------------------------------------------------
 00:07:33.785423 | 97.9%          | 20,673 | 00:00:00        | SELECT queryid AS query_id, calls, total_time, blk_read_time, blk_write_time                                                    +
                 |                |        |                 |                 FROM (                                                                                                          +
                 |                |        |                 |                         SELECT queryid,                                                                                         +
                 |                |        |                 |                         SUM(calls) OVER (PARTITION BY queryid) AS calls,                                                        +
                 |                |        |                 |                         SUM(total_exec_time) OVER (PARTITION BY queryid) AS total_time,                                         +
                 |                |        |                 |                         SUM(blk_read_time) OVER (PARTITION BY queryid) AS blk_read_time,                                        +
                 |                |        |                 |                         SUM(blk_write_time) OVER (PARTITION BY queryid) AS blk_write_time,                                      +
                 |                |        |                 |                         (total_exec_time / SUM(total_exec_time) over ()) * $1 AS time_spent                                     +
                 |                |        |                 |                         FROM public.pg_stat_statements($2)                                                                      +
                 |                |        |                 |                         WHERE userid = (                                                                                        +
                 |                |        |                 |                                 SELECT usesysid                                                                                 +
                 |                |        |                 |                                 FROM pg_user                                                                                    +
                 |                |        |                 |                                 WHERE usename = current_user LIMIT $3                                                           +
                 |                |        |                 |                         )                                                                                                       +
                 |                |        |                 |                 ) ts                                                                                                            +
                 |                |        |                 |                 WHERE time_spent >= $4                                                                                          +
                 |                |        |                 |                 GROUP BY queryid, calls, total_time, blk_read_time, blk_write_time
 00:00:05.762465 | 1.2%           | 20,673 | 00:00:00.000027 | SELECT pid, state                                                                                                               +
                 |                |        |                 |                 FROM pg_stat_activity                                                                                           +
                 |                |        |                 |                 WHERE state = $1                                                                                                +
                 |                |        |                 |                 AND query like $2                                                                                               +
                 |                |        |                 |                 AND NOT query like $3
 00:00:01.244622 | 0.3%           | 20,673 | 00:00:00        | SELECT CASE                                                                                                                     +
                 |                |        |                 |                 WHEN $1 IN (SELECT extname FROM pg_extension) THEN $2                                                           +
                 |                |        |                 |                 WHEN pg_is_in_recovery() THEN $3                                                                                +
                 |                |        |                 |                 WHEN current_setting($4) LIKE $5 THEN $6                                                                        +
                 |                |        |                 |                 ELSE $7                                                                                                         +
                 |                |        |                 |                 END as status
 00:00:00.849656 | 0.2%           | 20,673 | 00:00:00        | SELECT quote_ident(nspname::text) FROM pg_extension x INNER JOIN pg_namespace ns ON ns.oid = x.extnamespace WHERE x.extname = $1
 00:00:00.674264 | 0.1%           | 20,673 | 00:00:00        | SELECT extversion::text AS version FROM pg_catalog.pg_extension WHERE extname = $1
 00:00:00.39777  | 0.1%           | 20,675 | 00:00:00        | SELECT current_setting($1)::numeric >= $2
 00:00:00.396962 | 0.1%           | 20,673 | 00:00:00        | SET statement_timeout = '10s'
 00:00:00.237397 | 0.1%           | 40,323 | 00:00:00        | SELECT $1
 00:00:00.026024 | 0.0%           | 1      | 00:00:00        | SELECT interval $1 * total_exec_time AS total_exec_time,                                                                        +
                 |                |        |                 | to_char((total_exec_time/sum(total_exec_time) OVER()) * $2, $3) || $4  AS prop_exec_time,                                       +
                 |                |        |                 | to_char(calls, $5) AS ncalls,                                                                                                   +
                 |                |        |                 | interval $6 * (blk_read_time + blk_write_time) AS sync_io_time,                                                                 +
                 |                |        |                 | query AS query                                                                                                                  +
                 |                |        |                 | FROM pg_stat_statements WHERE userid = (SELECT usesysid FROM pg_user WHERE usename = current_user LIMIT $7)                     +
                 |                |        |                 | ORDER BY calls DESC                                                                                                             +
                 |                |        |                 | LIMIT $8
 00:00:00.000932 | 0.0%           | 1      | 00:00:00        | SET application_name='Heroku Postgres - Automation - 52.4.86.217:18396'
(10 rows)

  • test on PG17:
./bin/run pg:outliers HEROKU_POSTGRESQL_TCOLL_MAUVE_URL -a tc-data-dev
 total_exec_time | prop_exec_time | ncalls | sync_io_time |                                                              query                                                               
-----------------+----------------+--------+--------------+----------------------------------------------------------------------------------------------------------------------------------
 00:00:00.188967 | 57.8%          | 648    | 00:00:00     | SELECT pid, state                                                                                                               +
                 |                |        |              |                 FROM pg_stat_activity                                                                                           +
                 |                |        |              |                 WHERE state = $1                                                                                                +
                 |                |        |              |                 AND query like $2                                                                                               +
                 |                |        |              |                 AND NOT query like $3
 00:00:00.036011 | 11.0%          | 648    | 00:00:00     | SELECT CASE                                                                                                                     +
                 |                |        |              |                 WHEN $1 IN (SELECT extname FROM pg_extension) THEN $2                                                           +
                 |                |        |              |                 WHEN pg_is_in_recovery() THEN $3                                                                                +
                 |                |        |              |                 WHEN current_setting($4) LIKE $5 THEN $6                                                                        +
                 |                |        |              |                 ELSE $7                                                                                                         +
                 |                |        |              |                 END as status
 00:00:00.024527 | 7.5%           | 648    | 00:00:00     | SELECT quote_ident(nspname::text) FROM pg_extension x INNER JOIN pg_namespace ns ON ns.oid = x.extnamespace WHERE x.extname = $1
 00:00:00.020892 | 6.4%           | 648    | 00:00:00     | SET statement_timeout = '10s'
 00:00:00.018881 | 5.8%           | 648    | 00:00:00     | SELECT extversion::text AS version FROM pg_catalog.pg_extension WHERE extname = $1
 00:00:00.015858 | 4.9%           | 5      | 00:00:00     | SELECT interval $1 * total_exec_time AS total_exec_time,                                                                        +
                 |                |        |              | to_char((total_exec_time/sum(total_exec_time) OVER()) * $2, $3) || $4  AS prop_exec_time,                                       +
                 |                |        |              | to_char(calls, $5) AS ncalls,                                                                                                   +
                 |                |        |              | interval $6 * (shared_blk_read_time + shared_blk_write_time ) AS sync_io_time,                                                  +
                 |                |        |              | query AS query                                                                                                                  +
                 |                |        |              | FROM pg_stat_statements WHERE userid = (SELECT usesysid FROM pg_user WHERE usename = current_user LIMIT $7)                     +
                 |                |        |              | ORDER BY calls DESC                                                                                                             +
                 |                |        |              | LIMIT $8
 00:00:00.008568 | 2.6%           | 661    | 00:00:00     | SELECT current_setting($1)::numeric >= $2
 00:00:00.006597 | 2.0%           | 1,237  | 00:00:00     | SELECT $1
 00:00:00.000442 | 0.1%           | 12     | 00:00:00     | SELECT exists(                                                                                                                  +
                 |                |        |              |   SELECT $1                                                                                                                     +
                 |                |        |              |   FROM pg_extension e                                                                                                           +
                 |                |        |              |     LEFT JOIN pg_namespace n ON n.oid = e.extnamespace                                                                          +
                 |                |        |              |   WHERE e.extname = $2 AND n.nspname IN ($3, $4)                                                                                +
                 |                |        |              | ) AS available
 00:00:00.000261 | 0.1%           | 1      | 00:00:00     | SET application_name='Heroku Postgres - Automation - 52.4.86.217:27736'
(10 rows)

@troycoll troycoll requested a review from a team as a code owner February 26, 2025 17:42
Copy link
Contributor

@sbosio sbosio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@sbosio sbosio merged commit e849e91 into main Feb 26, 2025
8 checks passed
@sbosio sbosio deleted the tc/feat_pg17_support branch February 26, 2025 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants