Postgres and Indexes on Foreign Keys and Primary Keys

前端 未结 7 1720
青春惊慌失措
青春惊慌失措 2020-12-02 03:34

Does Postgres automatically put indexes on Foreign Keys and Primary Keys? How can I tell? Is there a command that will return all indexes on a table?

7条回答
  •  猫巷女王i
    2020-12-02 04:20

    If you want to list the indexes of all the tables in your schema(s) from your program, all the information is on hand in the catalog:

    select
         n.nspname  as "Schema"
        ,t.relname  as "Table"
        ,c.relname  as "Index"
    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
    

    If you want to delve further (such as columns and ordering), you need to look at pg_catalog.pg_index. Using psql -E [dbname] comes in handy for figuring out how to query the catalog.

提交回复
热议问题