Skip to content

Instantly share code, notes, and snippets.

@manics
Created August 2, 2017 07:58
Show Gist options
  • Save manics/a85d39ec85c26df3d31a16a34017ee84 to your computer and use it in GitHub Desktop.
Save manics/a85d39ec85c26df3d31a16a34017ee84 to your computer and use it in GitHub Desktop.
Get PostgreSQL pg_stat_statements as a CSV. Requires pg_stat_statements extension on the server
#!/bin/bash
# Requires pg_stat_statements extension on the server
# https://www.postgresql.org/docs/9.6/static/pgstatstatements.html
if [ $# -ne 1 ]; then
echo Usage: $(basename "$0") dbname
exit 1
fi
sudo -u postgres psql "$1" -c '
COPY (
SELECT
r.rolname,
d.datname,
queryid,
query,
calls,
total_time,
min_time,
max_time,
mean_time,
stddev_time,
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,
blk_write_time
FROM pg_stat_statements s
LEFT JOIN pg_roles r on (s.userid = r.oid)
LEFT JOIN pg_database d on (s.dbid = d.oid)
)
TO STDOUT WITH CSV HEADER
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment