Home > Database > Mysql Tutorial > How to Retrieve Indexed Columns in PostgreSQL?

How to Retrieve Indexed Columns in PostgreSQL?

Barbara Streisand
Release: 2024-12-28 11:33:46
Original
597 people have browsed it

How to Retrieve Indexed Columns in PostgreSQL?

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';
Copy after login

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;
Copy after login

Additional Resources for PostgreSQL Meta-Information Extraction:

  • [pg_index](https://www.postgresql.org/docs/current/static/view-pg-index.html)
  • ["Extracting META information from PostgreSQL"](https://www.depesz.com/2007/08/12/extracting-meta-information-from-postgresql/)

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template