Skip to content

Instantly share code, notes, and snippets.

@vladget
Created July 6, 2017 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vladget/9b314d94941a149b94b0300291760eb0 to your computer and use it in GitHub Desktop.
Save vladget/9b314d94941a149b94b0300291760eb0 to your computer and use it in GitHub Desktop.
report_pg_stat_statements.sh
#!/bin/bash
# variables
database="database"
to="email@email.com"
cc="email2@rmail.com"
# export top 100 statements to temporary file
psql -U postgres $database -c "copy (select query,min_time,mean_time,max_time,total_time,calls from pg_stat_statements where calls>100 and mean_time > 30 and min_time >10order by mean_time desc limit 100) to '/tmp/pg_stat_statements_top100.csv' delimiter ';'"
# format csv and email to receivers
cat /tmp/pg_stat_statements_top100.csv | awk -F ';' '{printf("query: %s \n min_time: %s \n mean_time: %s \n max_time: %s \n total_time: %s \n calls: %s \n\n",$1,$2,$3,$4,$5,$6)}' | tr -d \\r | mail -B -s "Postgres tracking execution statistics on $(hostname) for $(date +%F)" -c $cc $to
# clean up & reset stats
rm /tmp/pg_stat_statements_top100.csv
psql -U postgres catalog_microservice -c "select pg_stat_statements_reset();"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment