Search code examples
postgresqlindexinggoogle-cloud-sqldatabase-performancepostgresql-9.6

Is pg_stat_user_indexes replicated?


We are running Postgres 9.6.23 on Google Cloud SQL, with the Primary Instance being used for the (transactional) application and a Read Replica being used for analytical purposes.

Querying pg_stat_user_indexes gives a long list of results with 0 scans, but we are not sure how to interpret those stats regarding the replicas with enough confidence to drop some of the indexes as the transactional and analytical queries are very different.

How are the pg_stat_user_indexes stats replicated?

  • Will they be equal, even when reading from the replica? (i.e. replicated, so you only get statistics for the primary)
  • Can they be different in the primary and replica? (i.e. not replicated, but you need to query both instances to check for usage)

Solution

  • Statistics views like pg_stat_user_indexes don't show data persisted in tables, so these data are not replicated. Rather, they show data for the database usage on the standby when you query them on a standby.

    To determine if an index is unused or not, query the view on the primary and all standby servers.