Skip to content

Commit d01fb17

Browse files
committed
Reduce cardinality of pg_stat_statements
Make the example queries.yaml `pg_stat_statements` query safer. * Select the top 10% of queries by total query time. * Only expose the top 100 queries by total query time. * Keep only the most useful metrics. Fixes: #549 Signed-off-by: SuperQ <superq@gmail.com>
1 parent d273f97 commit d01fb17

File tree

1 file changed

+31
-54
lines changed

1 file changed

+31
-54
lines changed

queries.yaml

+31-54
Original file line numberDiff line numberDiff line change
@@ -148,75 +148,52 @@ pg_statio_user_tables:
148148

149149
# WARNING: This set of metrics can be very expensive on a busy server as every unique query executed will create an additional time series
150150
pg_stat_statements:
151-
query: "SELECT t2.rolname, t3.datname, queryid, calls, total_time / 1000 as total_time_seconds, min_time / 1000 as min_time_seconds, max_time / 1000 as max_time_seconds, mean_time / 1000 as mean_time_seconds, stddev_time / 1000 as stddev_time_seconds, rows, shared_blks_hit, shared_blks_read, shared_blks_dirtied, shared_blks_written, local_blks_hit, local_blks_read, local_blks_dirtied, local_blks_written, temp_blks_read, temp_blks_written, blk_read_time / 1000 as blk_read_time_seconds, blk_write_time / 1000 as blk_write_time_seconds FROM pg_stat_statements t1 JOIN pg_roles t2 ON (t1.userid=t2.oid) JOIN pg_database t3 ON (t1.dbid=t3.oid) WHERE t2.rolname != 'rdsadmin'"
152-
master: true
151+
query: |
152+
SELECT
153+
pg_get_userbyid(userid) as user,
154+
pg_database.datname,
155+
pg_stat_statements.queryid,
156+
pg_stat_statements.calls,
157+
pg_stat_statements.total_time / 1000.0 as seconds_total,
158+
pg_stat_statements.rows,
159+
pg_stat_statements.blk_read_time / 1000.0 as block_read_seconds_total,
160+
pg_stat_statements.blk_write_time / 1000.0 as block_write_seconds_total
161+
FROM pg_stat_statements
162+
JOIN pg_database
163+
ON pg_database.oid = pg_stat_statements.dbid
164+
WHERE
165+
total_time > (
166+
SELECT percentile_cont(0.1)
167+
WITHIN GROUP (ORDER BY total_time)
168+
FROM pg_stat_statements
169+
)
170+
ORDER BY seconds_total DESC
171+
LIMIT 100
153172
metrics:
154-
- rolname:
173+
- user:
155174
usage: "LABEL"
156-
description: "Name of user"
175+
description: "The user who executed the statement"
157176
- datname:
158177
usage: "LABEL"
159-
description: "Name of database"
178+
description: "The database in which the statement was executed"
160179
- queryid:
161180
usage: "LABEL"
162-
description: "Query ID"
181+
description: "Internal hash code, computed from the statement's parse tree"
163182
- calls:
164183
usage: "COUNTER"
165184
description: "Number of times executed"
166-
- total_time_seconds:
185+
- seconds_total:
167186
usage: "COUNTER"
168-
description: "Total time spent in the statement, in milliseconds"
169-
- min_time_seconds:
170-
usage: "GAUGE"
171-
description: "Minimum time spent in the statement, in milliseconds"
172-
- max_time_seconds:
173-
usage: "GAUGE"
174-
description: "Maximum time spent in the statement, in milliseconds"
175-
- mean_time_seconds:
176-
usage: "GAUGE"
177-
description: "Mean time spent in the statement, in milliseconds"
178-
- stddev_time_seconds:
179-
usage: "GAUGE"
180-
description: "Population standard deviation of time spent in the statement, in milliseconds"
187+
description: "Total time spent in the statement, in seconds"
181188
- rows:
182189
usage: "COUNTER"
183190
description: "Total number of rows retrieved or affected by the statement"
184-
- shared_blks_hit:
185-
usage: "COUNTER"
186-
description: "Total number of shared block cache hits by the statement"
187-
- shared_blks_read:
188-
usage: "COUNTER"
189-
description: "Total number of shared blocks read by the statement"
190-
- shared_blks_dirtied:
191-
usage: "COUNTER"
192-
description: "Total number of shared blocks dirtied by the statement"
193-
- shared_blks_written:
194-
usage: "COUNTER"
195-
description: "Total number of shared blocks written by the statement"
196-
- local_blks_hit:
197-
usage: "COUNTER"
198-
description: "Total number of local block cache hits by the statement"
199-
- local_blks_read:
200-
usage: "COUNTER"
201-
description: "Total number of local blocks read by the statement"
202-
- local_blks_dirtied:
203-
usage: "COUNTER"
204-
description: "Total number of local blocks dirtied by the statement"
205-
- local_blks_written:
206-
usage: "COUNTER"
207-
description: "Total number of local blocks written by the statement"
208-
- temp_blks_read:
209-
usage: "COUNTER"
210-
description: "Total number of temp blocks read by the statement"
211-
- temp_blks_written:
212-
usage: "COUNTER"
213-
description: "Total number of temp blocks written by the statement"
214-
- blk_read_time_seconds:
191+
- block_read_seconds_total:
215192
usage: "COUNTER"
216-
description: "Total time the statement spent reading blocks, in milliseconds (if track_io_timing is enabled, otherwise zero)"
217-
- blk_write_time_seconds:
193+
description: "Total time the statement spent reading blocks, in seconds"
194+
- block_write_seconds_total:
218195
usage: "COUNTER"
219-
description: "Total time the statement spent writing blocks, in milliseconds (if track_io_timing is enabled, otherwise zero)"
196+
description: "Total time the statement spent writing blocks, in seconds"
220197

221198
pg_process_idle:
222199
query: |

0 commit comments

Comments
 (0)