Skip to content

Commit

Permalink
Update the PLPGSQL queries to resolve duplicate queries
Browse files Browse the repository at this point in the history
Update the PLPGSQL queries to resolve duplicate queries

Signed-off-by: VigneshViggu <vigneshkumar.venugopal@outlook.com>
  • Loading branch information
vigneshkumar2016 authored Mar 11, 2024
1 parent b126e62 commit cb73f19
Showing 1 changed file with 36 additions and 40 deletions.
76 changes: 36 additions & 40 deletions collector/pg_stat_statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,47 +71,43 @@ var (
prometheus.Labels{},
)

pgStatStatementsQuery = `SELECT
pg_get_userbyid(userid) as user,
pg_database.datname,
pg_stat_statements.queryid,
pg_stat_statements.calls as calls_total,
pg_stat_statements.total_time / 1000.0 as seconds_total,
pg_stat_statements.rows as rows_total,
pg_stat_statements.blk_read_time / 1000.0 as block_read_seconds_total,
pg_stat_statements.blk_write_time / 1000.0 as block_write_seconds_total
FROM pg_stat_statements
JOIN pg_database
ON pg_database.oid = pg_stat_statements.dbid
WHERE
total_time > (
SELECT percentile_cont(0.1)
WITHIN GROUP (ORDER BY total_time)
FROM pg_stat_statements
pgStatStatementsQuery = `WITH percentiles AS (
SELECT percentile_cont(0.1) WITHIN GROUP (ORDER BY total_time) AS percentile
FROM pg_stat_statements
)
ORDER BY seconds_total DESC
LIMIT 100;`

pgStatStatementsNewQuery = `SELECT
pg_get_userbyid(userid) as user,
pg_database.datname,
pg_stat_statements.queryid,
pg_stat_statements.calls as calls_total,
pg_stat_statements.total_exec_time / 1000.0 as seconds_total,
pg_stat_statements.rows as rows_total,
pg_stat_statements.blk_read_time / 1000.0 as block_read_seconds_total,
pg_stat_statements.blk_write_time / 1000.0 as block_write_seconds_total
FROM pg_stat_statements
JOIN pg_database
ON pg_database.oid = pg_stat_statements.dbid
WHERE
total_exec_time > (
SELECT percentile_cont(0.1)
WITHIN GROUP (ORDER BY total_exec_time)
FROM pg_stat_statements
)
ORDER BY seconds_total DESC
LIMIT 100;`
SELECT DISTINCT ON (pss.queryid, pg_get_userbyid(pss.userid), pg_database.datname)
pg_get_userbyid(pss.userid) as user,
pg_database.datname,
pss.queryid,
pss.calls as calls_total,
pss.total_time / 1000.0 as seconds_total,
pss.rows as rows_total,
pss.blk_read_time / 1000.0 as block_read_seconds_total,
pss.blk_write_time / 1000.0 as block_write_seconds_total
FROM pg_stat_statements pss
JOIN pg_database ON pg_database.oid = pss.dbid
CROSS JOIN percentiles
WHERE pss.total_time > (SELECT percentile FROM percentiles)
ORDER BY pss.queryid, pg_get_userbyid(pss.userid) DESC, pg_database.datname
LIMIT 100;`

pgStatStatementsNewQuery = `SELECT DISTINCT ON (pss.queryid, pg_get_userbyid(pss.userid), pg_database.datname)
pg_get_userbyid(pss.userid) AS user,
pg_database.datname AS database_name,
pss.queryid,
pss.calls AS calls_total,
pss.total_exec_time / 1000.0 AS seconds_total,
pss.rows AS rows_total,
pss.blk_read_time / 1000.0 AS block_read_seconds_total,
pss.blk_write_time / 1000.0 AS block_write_seconds_total
FROM pg_stat_statements pss
JOIN pg_database ON pg_database.oid = pss.dbid
JOIN (
SELECT percentile_cont(0.1) WITHIN GROUP (ORDER BY total_exec_time) AS percentile_val
FROM pg_stat_statements
) AS perc ON pss.total_exec_time > perc.percentile_val
ORDER BY pss.queryid, pg_get_userbyid(pss.userid) DESC, pg_database.datname
LIMIT 100;`
)

func (PGStatStatementsCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
Expand Down

0 comments on commit cb73f19

Please sign in to comment.