我目前正在审查复杂 PostgreSQL 数据库中索引的使用情况。看起来有用的查询之一是
SELECT idstat.schemaname AS schema_name, idstat.relname AS table_name,
indexrelname AS index_name,
idstat.idx_scan AS times_used,
pg_size_pretty(pg_relation_size(idstat.relid)) AS table_size, pg_size_pretty(pg_relation_size(indexrelid)) AS index_size,
n_tup_upd + n_tup_ins + n_tup_del as num_writes,
indexdef AS definition
FROM pg_stat_user_indexes AS idstat JOIN pg_indexes ON (indexrelname = indexname AND idstat.schemaname = pg_indexes.schemaname)
JOIN pg_stat_user_tables AS tabstat ON idstat.relid = tabstat.relid
WHERE idstat.idx_scan < 200
AND indexdef !~* 'unique'
ORDER BY idstat.relname, indexrelname;
它告诉我索引的使用频率、它使用了多少空间等。
然而:
我从客户端站点获取数据库备份。当我恢复数据库时,查询为 times_used 返回零。我怀疑所有索引都在恢复时重建。
最后,提问:
从 pg_catalog 捕获(备份)数据以便恢复和分析有关索引使用的实际客户端数据的最简单方法是什么?