pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kristo Kaiv <kristo.kaiv@skype.net>
To: Stefan Zweig <stefanzweig1881@web.de>
Cc: pgsql-sql@postgresql.org
Subject: Re: show index from [table]
Date: Mon, 11 Jun 2007 09:28:13 +0300
Message-ID: <DE38B02B-CC33-409E-88AD-6EB53F504E88@skype.net> (raw)
In-Reply-To: <112402456@web.de>
References: <112402456@web.de>


On 08.06.2007, at 18:25, Stefan Zweig wrote:
>
> but actually i would need the information from within a (postgres)  
> sql-query. is there a possibility to get information about the  
> indices which have been created on a table?
>
> if there is not, it might be sufficient for me to get the create  
> index strings, such like you get, when viewing a table in pgAdmin:


you can turn on echoing of psql commands sent to server with:
psql -E dbname
from there you can get the queries needed:

find the oid of table (unique object id)
********* QUERY **********
SELECT c.oid,
   n.nspname,
   c.relname
FROM pg_catalog.pg_class c
      LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relname ~ '^(miljon)$'
   AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 2, 3;

find table indices (replace the oid by the value found with your  
previous query)
********* QUERY **********
SELECT c2.relname, i.indisprimary, i.indisunique, i.indisclustered,  
i.indisvalid, pg_catalog.pg_get_indexdef(i.indexrelid, 0, true),  
c2.reltablespace
FROM pg_catalog.pg_class c, pg_catalog.pg_class c2,  
pg_catalog.pg_index i
WHERE c.oid = '16427' AND c.oid = i.indrelid AND i.indexrelid = c2.oid
ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname

here is my sample output:
    relname    | indisprimary | indisunique | indisclustered |  
indisvalid |                       
pg_get_indexdef                       | reltablespace
--------------+--------------+-------------+---------------- 
+------------ 
+------------------------------------------------------------ 
+---------------
miljon_pkey  | t            | t           | f              |  
t          | CREATE UNIQUE INDEX miljon_pkey ON miljon USING btree  
(id) |             0
idx_blahblah | f            | f           | f              |  
t          | CREATE INDEX idx_blahblah ON miljon USING btree  
(sisu)     |             0
(2 rows)

hope this helps

Kristo



view thread (4+ messages)

Message-ID: <DE38B02B-CC33-409E-88AD-6EB53F504E88@skype.net>
Permalink:  ../DE38B02B-CC33-409E-88AD-6EB53F504E88@skype.net/
Also on:    postgresql.org/message-id/DE38B02B-CC33-409E-88AD-6EB53F504E88@skype.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: kristo.kaiv@skype.net, stefanzweig1881@web.de
  Subject: Re: show index from [table]
  In-Reply-To: <DE38B02B-CC33-409E-88AD-6EB53F504E88@skype.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox