List all index names, column names and its table name of a PostgreSQL database

后端 未结 6 1666
走了就别回头了
走了就别回头了 2020-12-12 13:40

What is the query to get the list all index names, its column name and its table name of a postgresql database?

I have tried to get the list of all indexes in a db b

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-12 14:36

    More human friendly version of @Denis solution:

    SELECT
      U.usename                AS user_name,
      ns.nspname               AS schema_name,
      idx.indrelid :: REGCLASS AS table_name,
      i.relname                AS index_name,
      idx.indisunique          AS is_unique,
      idx.indisprimary         AS is_primary,
      am.amname                AS index_type,
      idx.indkey,
           ARRAY(
               SELECT pg_get_indexdef(idx.indexrelid, k + 1, TRUE)
               FROM
                 generate_subscripts(idx.indkey, 1) AS k
               ORDER BY k
           ) AS index_keys,
      (idx.indexprs IS NOT NULL) OR (idx.indkey::int[] @> array[0]) AS is_functional,
      idx.indpred IS NOT NULL AS is_partial
    FROM pg_index AS idx
      JOIN pg_class AS i
        ON i.oid = idx.indexrelid
      JOIN pg_am AS am
        ON i.relam = am.oid
      JOIN pg_namespace AS NS ON i.relnamespace = NS.OID
      JOIN pg_user AS U ON i.relowner = U.usesysid
    WHERE NOT nspname LIKE 'pg%'; -- Excluding system tables
    

提交回复
热议问题