List columns with indexes in PostgreSQL

后端 未结 23 1931
我在风中等你
我在风中等你 2020-11-27 09:14

I would like to get the columns that an index is on in PostgreSQL.

In MySQL you can use SHOW INDEXES FOR table and look at the Column_name

23条回答
  •  一整个雨季
    2020-11-27 09:34

    Combined with others code and created a view:

    CREATE OR REPLACE VIEW view_index AS 
    SELECT
         n.nspname  as "schema"
        ,t.relname  as "table"
        ,c.relname  as "index"
        ,pg_get_indexdef(indexrelid) as "def"
    FROM pg_catalog.pg_class c
        JOIN pg_catalog.pg_namespace n ON n.oid        = c.relnamespace
        JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid
        JOIN pg_catalog.pg_class t ON i.indrelid   = t.oid
    WHERE c.relkind = 'i'
        and n.nspname not in ('pg_catalog', 'pg_toast')
        and pg_catalog.pg_table_is_visible(c.oid)
    ORDER BY
         n.nspname
        ,t.relname
        ,c.relname;
    

提交回复
热议问题