Query to retrieve all indexed columns - Mailing list pgsql-sql

From Bart van Houdt
Subject Query to retrieve all indexed columns
Date
Msg-id CECE69D480C32F49891F27ED3E49C38C04A703EF@nthvsexch02.interaccess.nl
Whole thread Raw
Responses Re: Query to retrieve all indexed columns
List pgsql-sql

Hi,

 

I am working on a query to retrieve all indexed columns and came up with the following query:

 

select pgc.relname as indexname

      ,pgc2.relname as tablename

      ,pga.attname as columnname

      ,pga.attnum as columnnumber

      ,replace(pgi.indkey::text, ' ', ',') as columnindex

  from pg_class pgc

  join pg_namespace pgn ON (pgn.oid = pgc.relnamespace

                            AND pgn.nspname = 'public')

  left join pg_index pgi ON (pgi.indexrelid = pgc.oid)

  left join pg_class pgc2 ON (pgc2.oid = pgi.indrelid)

  left join pg_attribute pga ON (pga.attrelid = pgc2.oid

                                 AND attnum::text IN (replace(pgi.indkey::text, ' ', ',')))

where pgc.relkind = 'i'

order by indexname, columnindex;

 

Tis query works for single column indexes, but with multiple column indexes I get incorrect results… I’m having a hard time figuring out how to join pg_attribute.indkey in this, could anyone help me out on this one?  

 

Thanks in advance,

 

Bart van Houdt

Syfact International B.V.

Database developer

pgsql-sql by date:

Previous
From: mahmoud ewiwi
Date:
Subject: Re: custom serial number
Next
From: "A. Kretschmer"
Date:
Subject: Re: custom serial number