Retrieve Indexed Columns in PostgreSQL
To obtain the columns indexed within PostgreSQL, you can utilize the following query:
SELECT t.relname AS table_name, i.relname AS index_name, a.attname AS column_name FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND a.attnum = ANY(ix.indkey) AND t.relkind = 'r';
To further aggregate the results by index, use this query:
SELECT t.relname AS table_name, i.relname AS index_name, array_to_string(array_agg(a.attname), ', ') AS column_names FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND a.attnum = ANY(ix.indkey) AND t.relkind = 'r' GROUP BY t.relname, i.relname;
Additional Resources for PostgreSQL Meta-Information Extraction:
The above is the detailed content of How to Retrieve Indexed Columns in PostgreSQL?. For more information, please follow other related articles on the PHP Chinese website!