SELECT idstat.schemaname AS schema, idstat.relname AS table_name, indexrelname AS index_name, idstat.idx_scan AS times_used, pg_size_pretty(pg_relation_size(quote_ident(idstat.schemaname) || '.' || quote_ident(idstat.relname))) AS table_size, pg_size_pretty(pg_relation_size(quote_ident(idstat.schemaname) || '.' || quote_ident(indexrelname))) 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 JOIN pg_stat_user_tables AS tabstat ON idstat.relname = tabstat.relname WHERE indexrelname = ' {YOUR QUERY NAME } ';
I've added some new indexes this week into my prod environment, and I used your query to see if they're being used or not.
Query:
SELECT idstat.schemaname AS schema, idstat.relname AS table_name, indexrelname AS index_name, idstat.idx_scan AS times_used, pg_size_pretty(pg_relation_size(quote_ident(idstat.schemaname) || '.' || quote_ident(idstat.relname))) AS table_size, pg_size_pretty(pg_relation_size(quote_ident(idstat.schemaname) || '.' || quote_ident(indexrelname))) 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 JOIN pg_stat_user_tables AS tabstat ON idstat.relname = tabstat.relname WHERE indexrelname = 'ix_ja_jobs_clientid_title_time_job';
Returns:
schema table_name index_name times_used table_size index_size num_writes definition ------ ---------- ---------------------------------- ---------- ---------- ---------- ---------- ------------------------------------------------------------------------------------------------------------------------------------------------------- public ja_jobs ix_ja_jobs_clientid_title_time_job 41536 3526 MB 484 MB 38266927 CREATE INDEX "ix_ja_jobs_clientid_title_time_job" ON "ja_jobs" USING "btree" ("clientid", "lower"(("title")::"text") "varchar_pattern_ops", "time_job")
That index has been added just 3 hours ago, and you can see that the times_used goes over 41000.... How is that possible??