Home >Database >Mysql Tutorial >How to Retrieve Columns Associated with Indexes in PostgreSQL?
In PostgreSQL, retrieving the columns corresponding to an index differs from MySQL's SHOW INDEXES command.
To obtain the desired information in PostgreSQL, 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' and t.relname like 'test%';
This query retrieves the table and index names along with the associated column names. For further insights, the query can be modified to aggregate the column names:
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' and t.relname like 'test%' group by t.relname, i.relname order by t.relname, i.relname;
The above is the detailed content of How to Retrieve Columns Associated with Indexes in PostgreSQL?. For more information, please follow other related articles on the PHP Chinese website!