Re: Unused indexes - PostgreSQL 9.2 - Mailing list pgsql-general

From Lucas Possamai
Subject Re: Unused indexes - PostgreSQL 9.2
Date
Msg-id CAE_gQfUYk4GxFyLxA_nB-jdJVVPceqiKKMfvQyp4EMA_sCRfvw@mail.gmail.com
Whole thread Raw
In response to Re: Unused indexes - PostgreSQL 9.2  (Melvin Davidson <melvin6925@gmail.com>)
Responses Re: Unused indexes - PostgreSQL 9.2
List pgsql-general
Hello Melvin, how are you doing?



And what happens if you run this 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 = ' {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??

Don't think the query is right.

Can you please check ?

Thank you. 

pgsql-general by date:

Previous
From: Lucas Possamai
Date:
Subject: Re: Increased I/O / Writes
Next
From: "dandl"
Date:
Subject: Re: Fatal error "stack empty" on ROLLBACK