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
There is a way to log slow queries through the pg_stat_statements
extension which is supported by Cloud SQL.
Since Cloud SQL doesn't grant superuser right to any of the users you need to use some workaround. First, you need to enable the extension with
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
then you can check slow queries with a query like
SELECT pd.datname,
us.usename,
pss.userid,
pss.query AS SQLQuery,
pss.rows AS TotalRowCount,
(pss.total_time / 1000) AS TotalSecond,
((pss.total_time / 1000) / calls) as TotalAverageSecond
FROM pg_stat_statements AS pss
INNER JOIN pg_database AS pd
ON pss.dbid = pd.oid
INNER JOIN pg_user AS us
ON pss.userid = us.usesysid
ORDER BY TotalAverageSecond DESC
LIMIT 10;
As postgres
user you can have a look on all slow queries, but since the user is not superuser you will see
on all other users' queries.
To get around this limitation you can install the extension on other databases too (normally only postgres
user has rigths to install extensions) and you can check the query texts with the owner of the db.