pgexperts / pgx_scripts

A collection of useful little scripts for database analysis and administration, created by our team at PostgreSQL Experts.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect where clause on btree_index_atts and pg_attribute join

Abhishek1804 opened this issue · comments

This condition pg_attribute.attnum = ind_atts.attnum is filtering out majority of the bloated indexes.
Ref - JOIN btree_index_atts AS ind_atts ON pg_attribute.attrelid = ind_atts.indexrelid AND pg_attribute.attnum = ind_atts.attnum

This is because attnum of indexes in pg_attribute table will not always match with indkey of index in pg_index table. It will usually match for primary key or initial columns. Thus, pg_attribute.attnum = ind_atts.attnum condition has to be removed.

This condition : pg_stats.attname = pg_catalog.pg_get_indexdef(pg_attribute.attrelid, pg_attribute.attnum, TRUE) while joining with pg_stats will take care of the rest.