Logging slow queries on Google Cloud SQL PostgreSQL instances

前端 未结 4 882
死守一世寂寞
死守一世寂寞 2021-02-13 10:40

The company I work for uses Google Cloud SQL to manage their SQL databases in production.

We\'re having performance issues and I thought it\'d be a good idea (among othe

4条回答
  •  萌比男神i
    2021-02-13 11:04

    Not ideal by any measure, but what we do is run something like this on a cron once a minute and log out the result:

    SELECT EXTRACT(EPOCH FROM now() - query_start) AS seconds, query
     FROM  pg_stat_activity 
     WHERE state = 'active' AND now() - query_start > interval '1 seconds' AND query NOT LIKE '%pg_stat_activity%'
     ORDER BY seconds DESC LIMIT 20
    

    You'd need to fiddle with the query to get millisecond granularity, and even then it'll only catch queries that overlap with your cron frequency, but probably better than nothing?

提交回复
热议问题