9 ms·
postgres can collect stats across all queries against the database[1]. GCP Cloud SQL has this enabled by default. You can do `select * from pg_stat_user_tables
by devmunchies 5y ago
postgres can collect stats across all queries against the database[1]. GCP Cloud SQL has this enabled by default.
You can do `select * from pg_stat_user_tables` to see how many table have had (1) a full sequential scan, (2) how many records have been traversed by sequential scans, (3) how many index scans, and (4) how many records have been scanned using indexes.
You can also do `select * from pg_stat_user_indexes` to see (1) which indexes have or have not been used, (2) how many times they've been used, and (3) how many records have been crawled using each index.
Using this information, you would deduce which indexes to add/remove, and but you would still need to figure out which queries are not hitting indexes (an exercise for the reader).
If you save these stats once a day (e.g. using `pg_cron` to copy to a new table with a timestamp), you will be able to monitor over time whether an index should be added/removed
[1]: https://www.postgresql.org/docs/14/monitoring-stats.html https://www.postgresql.org/docs/14/monitoring-stats.html