219

Could you tell me how to check what indexes are created for some table in postgresql ?

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47

5 Answers5

294

The view pg_indexes provides access to useful information about each index in the database, eg.

select *
from pg_indexes
where tablename not like 'pg%';
klin
  • 112,967
  • 15
  • 204
  • 232
142

if you're in psql, then:

\d tablename

show Indexes, Foreign Keys and references...

dwilkins
  • 1,923
  • 1
  • 10
  • 9
40

You can use this query:

select tablename,indexname,tablespace,indexdef from pg_indexes where tablename = 'your_table_name';

where has tablename is a field in pg_indexes ,you an get an accurate indices by matching user defined table at 'your_table_name' at WHERE clause . This will give you the desired details.

Thomas Richter
  • 833
  • 8
  • 20
b.vishnu Prasad
  • 501
  • 4
  • 3
8

You can find all the index related information inside the pg_indexes view. Sometimes, a table may be part of some schema ("owner") or might have had a different name in the past (see: PostgreSQL Rename Table).

So first find out what is the schema ("owner") of the table:

SELECT schemaname, tablename FROM pg_tables WHERE tablename='table_name';

and then query indexes on the table with either of these queries:

SELECT tablename, indexname FROM pg_indexes WHERE tablename='table_name';
-- or 
SELECT * FROM pg_indexes WHERE tablename='schema_name.table_name';

As an alternative to all the above, you can also use \d:

\d table_name;
tanius
  • 14,003
  • 3
  • 51
  • 63
santosh tiwary
  • 598
  • 6
  • 4
5

The command

\di

will list all indexes for the current schema.

Acceptance
  • 110
  • 1
  • 5