您可能需要的关于索引的一切。适用于 >=9.1 & <=9.5 的版本。
学分 - IRC 上的 RhodiumToad。:)
SELECT schemaname, tablename, indexname, amname, indisunique, indisprimary,
array_agg(attname ORDER BY ord) AS columns,
array_agg(coll ORDER BY ord) AS collations,
array_agg(opclass ORDER BY ord) AS opclasses,
array_agg(ordering ORDER BY ord) AS orderings,
array_agg(expression ORDER BY ord) AS expressions,
predicate
FROM (SELECT n.nspname AS schemaname,
ct.relname AS tablename,
c.relname AS indexname,
m.amname,
s.indisunique, s.indisprimary, s.ord,
a.attname,
CASE WHEN con.nspname is not null
THEN format('%I.%I',con.nspname,co.collname)
END AS coll,
CASE WHEN oc.opcname is not null
THEN format('%I.%I',ocn.nspname,oc.opcname)
END AS opclass,
CASE WHEN m.amcanorder
THEN format('%s NULLS %s',
CASE (option & 1) WHEN 1 THEN 'DESC' ELSE 'ASC' END,
CASE (option & 2) WHEN 2 THEN 'FIRST' ELSE 'LAST' END)
END AS ordering,
pg_get_expr(s.indpred, s.indrelid) AS predicate,
pg_get_indexdef(s.indexrelid, ord, false) AS expression
FROM (SELECT *,
generate_series(1,array_length(i.indkey,1)) AS ord,
unnest(i.indkey) AS key,
unnest(i.indcollation) AS coll,
unnest(i.indclass) AS class,
unnest(i.indoption) AS option
FROM pg_index i) s
JOIN pg_class c ON (c.oid=s.indexrelid)
JOIN pg_class ct ON (ct.oid=s.indrelid)
JOIN pg_namespace n ON (n.oid=c.relnamespace)
JOIN pg_am m ON (m.oid=c.relam)
LEFT JOIN pg_attribute a ON (a.attrelid=s.indrelid AND a.attnum=s.key)
LEFT JOIN pg_collation co ON (co.oid=s.coll)
LEFT JOIN pg_namespace con ON (con.oid=co.collnamespace)
LEFT JOIN pg_opclass oc ON (oc.oid=s.class)
LEFT JOIN pg_namespace ocn ON (ocn.oid=oc.opcnamespace)
) s2
WHERE tablename = 'your_table_name'
GROUP BY schemaname, tablename, indexname, amname, indisunique, indisprimary, predicate;
输出:
schemaname | tablename | indexname | amname | indisunique | indisprimary | columns | collations | opclasses | orderings | expressions | predicate
------------+----------------------------+---------------------------------+--------+-------------+--------------+---------+-----------------------------------------------------+-------------------------------------------+---------------------------------------+-------------+-----------
public | ticket26180_indexes_spamin | test26180_indexes_spamin_a_hsh | hash | f | f | {a} | {"pg_catalog.\"default\""} | {pg_catalog.text_ops} | {NULL} | {a} |
public | ticket26180_indexes_spamin | test26180_indexes_spamin_atpata | btree | f | f | {a,b} | {"pg_catalog.\"default\"","pg_catalog.\"default\""} | {pg_catalog.text_ops,pg_catalog.text_ops} | {"DESC NULLS FIRST","ASC NULLS LAST"} | {a,b} |
public | ticket26180_indexes_spamin | test26180_indexes_spamin_b_hsh | hash | f | f | {b} | {"pg_catalog.\"default\""} | {pg_catalog.text_ops} | {NULL} | {b} |
public | ticket26180_indexes_spamin | ticket26180_a_6fe9a5_idx | btree | f | f | {a,b} | {"pg_catalog.\"default\"","pg_catalog.\"default\""} | {pg_catalog.text_ops,pg_catalog.text_ops} | {"ASC NULLS LAST","ASC NULLS LAST"} | {a,b} |
public | ticket26180_indexes_spamin | ticket26180_indexes_spamin_pkey | btree | t | t | {id} | {NULL} | {pg_catalog.int4_ops} | {"ASC NULLS LAST"} | {id} |
想把它贴在这里,这样任何需要它的人就不必像我一样费力地寻找它。