dbTalk Databases Forums  

Finding all indexes of a table, including columns

comp.databases.postgresql comp.databases.postgresql


Discuss Finding all indexes of a table, including columns in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Michael Schuerig
 
Posts: n/a

Default Finding all indexes of a table, including columns - 04-02-2009 , 02:23 AM







The PostgreSQL adapter of the Ruby on Rails database connector contains
the following statement to find all indexes on a table, including the
indexed columns.

SELECT distinct i.relname, d.indisunique, a.attname
FROM pg_class t, pg_class i, pg_index d, pg_attribute a
WHERE i.relkind = 'i'
AND d.indexrelid = i.oid
AND d.indisprimary = 'f'
AND t.oid = d.indrelid
AND t.relname = '#{table_name}'
AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname IN
(#{schemas}) )
AND a.attrelid = t.oid
AND ( d.indkey[0]=a.attnum OR d.indkey[1]=a.attnum
OR d.indkey[2]=a.attnum OR d.indkey[3]=a.attnum
OR d.indkey[4]=a.attnum OR d.indkey[5]=a.attnum
OR d.indkey[6]=a.attnum OR d.indkey[7]=a.attnum
OR d.indkey[8]=a.attnum OR d.indkey[9]=a.attnum )
ORDER BY i.relname

Admittedly, I haven't even tried to understand what's going on here.
However, I've looked at the code of pgadmin3 which is no less involved.

Anyway, the effect I've noticed is that the statement above does find
all the columns of each index, but it returns them in an arbitrary
order. And indeed, there's nothing obviously imposing an ordering on
a.attname.

Is there a fix for this, or is the statement beyond repair? I've looked
at the information schema, as implemented in PostgreSQL 8.3, and
apparently it is not possible to gather information on indexes from it.

Michael

--
Michael Schuerig
mailto:michael (AT) schuerig (DOT) de
http://www.schuerig.de/michael/

Reply With Quote
  #2  
Old   
Laurenz Albe
 
Posts: n/a

Default Re: Finding all indexes of a table, including columns - 04-02-2009 , 06:54 AM






Michael Schuerig wrote:
Quote:
The PostgreSQL adapter of the Ruby on Rails database connector contains
the following statement to find all indexes on a table, including the
indexed columns.

SELECT distinct i.relname, d.indisunique, a.attname
FROM pg_class t, pg_class i, pg_index d, pg_attribute a
WHERE i.relkind = 'i'
AND d.indexrelid = i.oid
AND d.indisprimary = 'f'
AND t.oid = d.indrelid
AND t.relname = '#{table_name}'
AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname IN
(#{schemas}) )
AND a.attrelid = t.oid
AND ( d.indkey[0]=a.attnum OR d.indkey[1]=a.attnum
OR d.indkey[2]=a.attnum OR d.indkey[3]=a.attnum
OR d.indkey[4]=a.attnum OR d.indkey[5]=a.attnum
OR d.indkey[6]=a.attnum OR d.indkey[7]=a.attnum
OR d.indkey[8]=a.attnum OR d.indkey[9]=a.attnum )
ORDER BY i.relname

Admittedly, I haven't even tried to understand what's going on here.
However, I've looked at the code of pgadmin3 which is no less involved.

Anyway, the effect I've noticed is that the statement above does find
all the columns of each index, but it returns them in an arbitrary
order. And indeed, there's nothing obviously imposing an ordering on
a.attname.

Is there a fix for this, or is the statement beyond repair? I've looked
at the information schema, as implemented in PostgreSQL 8.3, and
apparently it is not possible to gather information on indexes from it.
The statement above will also not find functional indexes.

But it's easy to fix it so that the columns come in the order of
the index definition:

SELECT t.oid, i.relname, d.indisunique, a.attname
FROM pg_class t, pg_class i, pg_index d, pg_attribute a,
generate_series(0, 9) AS c(i)
WHERE i.relkind = 'i'
AND d.indexrelid = i.oid
AND d.indisprimary = 'f'
AND t.oid = d.indrelid
AND t.relname = '#{table_name}'
AND i.relnamespace IN
(SELECT oid FROM pg_namespace WHERE nspname IN (#{schemas}) )
AND a.attrelid = t.oid
AND d.indkey[c.i]=a.attnum
ORDER BY i.relname, c.i

Yours,
Laurenz Albe




Reply With Quote
  #3  
Old   
Michael Schuerig
 
Posts: n/a

Default Re: Finding all indexes of a table, including columns - 04-02-2009 , 08:09 AM



Laurenz Albe wrote:

Quote:
Michael Schuerig wrote:
The PostgreSQL adapter of the Ruby on Rails database connector
contains the following statement to find all indexes on a table,
including the indexed columns.
[...]

Quote:
The statement above will also not find functional indexes.

But it's easy to fix it so that the columns come in the order of
the index definition:
Thanks a lot! Easy is relative, I reckon.

Michael

--
Michael Schuerig
mailto:michael (AT) schuerig (DOT) de
http://www.schuerig.de/michael/


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.