Quantcast
Channel: CodeFari
Viewing all articles
Browse latest Browse all 265

How to List columns with indexes in PostgreSQL?

$
0
0

Problem: I have a table of the customer below, and I want to list column with indexes in PostgreSQL, Basically purpose of this question is an analysis of the performance.

 

 

CREATETABLECustomer

(

    IdserialNOTNULL,

    FNametextCOLLATEpg_catalog.default,

    LNametextCOLLATEpg_catalog.default,

    UserNametextCOLLATEpg_catalog.default,

    PasswordtextCOLLATEpg_catalog.default,

    ContacttextCOLLATEpg_catalog.default,

    AddresstextCOLLATEpg_catalog.default,

    CONSTRAINTpk_Customer_IdPRIMARYKEY (Id)

)

 

 

Solution: Using pg_index we can list the column with index

 

select

    t.relnameastable,

    i.relnameasindex,

    a.attnameascolumn

from

    pg_classt,

    pg_classi,

    pg_indexix,

    pg_attributea

where

    t.oid=ix.indrelid

    andi.oid=ix.indexrelid

    anda.attrelid=t.oid

    anda.attnum=ANY(ix.indkey)

    andt.relkind='r'

    andt.relnamelike'Customer%'

orderby

    t.relname,

    i.relname;

 


Using command \di

\di command is the easiest way to list all indexes in the current database.



As we know, command \d is used to list all the relations in the current database. \di is used to list the database indexes.

Note: But it will not show the column on which the index is created. Also, it will show all indexes, but not for a particular table.




Viewing all articles
Browse latest Browse all 265

Trending Articles