These scripts can be used to figure out how often different sets of column combinations are used to filter queries in PostgreSQL. It's most useful for getting a high-level view in order to design and refactor indexes (e.g. if a certain set of columns is rarely used to filter queries, it's possible using index-merging instead of composite indexes would improve overall performance).
- For each server in your database cluster, using credentials that can read
pg_stat_statements.queryid
(which is usually just superusers), download the contents of pg_stat_statements as CSV. This should be done separately for the primary and the replicas becausepg_stat_statements
tracks queries locally:
psql --csv -c 'SELECT * FROM pg_stat_statements' $DATABASE_URL > pg_stat_statements/$DATABASE_NAME.csv