PostgreSQL में अनुक्रमित के साथ कॉलम सूचीबद्ध करें


233

मैं कॉलम प्राप्त करना चाहूंगा कि एक सूचकांक PostgreSQL में है।

MySQL में आप कॉलम का उपयोग SHOW INDEXES FOR tableऔर देख सकते हैं Column_name

mysql> show indexes from foos;

+-------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name            | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| foos  |          0 | PRIMARY             |            1 | id          | A         |       19710 |     NULL | NULL   |      | BTREE      |         | 
| foos  |          0 | index_foos_on_email |            1 | email       | A         |       19710 |     NULL | NULL   | YES  | BTREE      |         | 
| foos  |          1 | index_foos_on_name  |            1 | name        | A         |       19710 |     NULL | NULL   |      | BTREE      |         | 
+-------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+

क्या PostgreSQL के लिए ऐसा कुछ मौजूद है?

मैंने कमांड प्रॉम्प्ट पर ( एसक्यूएल दिखाने के विकल्प के साथ ) कोशिश की है \d, लेकिन यह उस जानकारी को नहीं दिखाता है जिसकी मुझे तलाश है।psql-E

अपडेट: उन सभी को धन्यवाद जिन्होंने अपने उत्तर जोड़े। ad360 ने मुझे ठीक वही दिया जो मैं ढूंढ रहा था, लेकिन कई लोगों ने बहुत उपयोगी लिंक के साथ धोखा दिया। भविष्य में संदर्भ के लिए, के लिए दस्तावेज़ीकरण देखें pg_index (के माध्यम से Milen ए Radev ) और बहुत उपयोगी लेख PostgreSQL निकालने मेटा जानकारी (के माध्यम से मीकल निकलस )।


बस स्पष्ट करने के लिए: आप चाहते हैं कि आपका प्रोग्राम रनटाइम पर, यह पता लगाने में सक्षम हो कि कौन से कॉलम इंडेक्स किए गए हैं, है ना? जैसा कि आप जानते हुए भी प्रोग्रामिंग का विरोध करते हैं।
वेन कॉनराड

हाँ सही। आदर्श रूप से मैं एक एसक्यूएल स्टेटमेंट चाहता हूं जो केवल उन कॉलमों को सूचीबद्ध करता है जो सूचकांक पर है। लेकिन मुझे पता है कि PostgreSQL MySQL की तुलना में अधिक जटिल है और सूचकांक एक फ़ंक्शन पर हो सकता है, आदि
ल्यूक फ्रेंकल

जवाबों:


261

कुछ परीक्षण डेटा बनाएं ...

create table test (a int, b int, c int, constraint pk_test primary key(a, b));
create table test2 (a int, b int, c int, constraint uk_test2 unique (b, c));
create table test3 (a int, b int, c int, constraint uk_test3b unique (b), constraint uk_test3c unique (c),constraint uk_test3ab unique (a, b));

सूची अनुक्रमित और स्तंभों को अनुक्रमित:

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'
    and t.relname like 'test%'
order by
    t.relname,
    i.relname;

 table_name | index_name | column_name
------------+------------+-------------
 test       | pk_test    | a
 test       | pk_test    | b
 test2      | uk_test2   | b
 test2      | uk_test2   | c
 test3      | uk_test3ab | a
 test3      | uk_test3ab | b
 test3      | uk_test3b  | b
 test3      | uk_test3c  | c

स्तंभ नामों को रोल अप करें:

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'
    and t.relname like 'test%'
group by
    t.relname,
    i.relname
order by
    t.relname,
    i.relname;

 table_name | index_name | column_names
------------+------------+--------------
 test       | pk_test    | a, b
 test2      | uk_test2   | b, c
 test3      | uk_test3ab | a, b
 test3      | uk_test3b  | b
 test3      | uk_test3c  | c

24
आबादी वाले डेटाबेस में अनुक्रमित खोजने की कोशिश करने वाले किसी के लिए: यह क्वेरी बहुत बढ़िया काम करती है, लेकिन and t.relname like 'test%'आप जिस तालिका को चाहते हैं, उस पंक्ति को तालिका में बदल दें या अपने db में सभी अनुक्रमितों को खोजने के लिए उस पंक्ति को पूरी तरह से मिटा दें।
एरिक जे

1
क्या कोई relkind='r'इसका मतलब समझा सकता है?
क्वर्टी

5
@ प्रश्न, pg_class के लिए दस्तावेज़ देखें r = ordinary table, i = index, S = sequence, v = view, c = composite type, t = TOAST table
१३:०६

1
वहाँ भी कुंजी की विशिष्टता के बारे में बताने के लिए एक रास्ता है?
एंड्रयू

2
सूचकांक विशिष्टता देखने के लिए भी चयन करेंix.indisunique
जना

177

PostgreSQL ( pg_indexes ):

SELECT * FROM pg_indexes WHERE tablename = 'mytable';

MySQL ( SHOW INDEX ):

SHOW INDEX FROM mytable;

3
यह सबसे सीधा जवाब है, और सवाल का जवाब देने के मामले में सबसे दिलचस्प है "क्या मेरा कॉलम अनुक्रमित है?" PostgreSQL: SELECT COUNT(indexname) AS indexcount FROM pg_indexes WHERE tablename='mytablename' AND indexdef LIKE '%mycolumnname%' ;और सत्यापित करें indexcount>0। mySQL: SHOW INDEX FROM mytablename WHERE Column_name='mycolumnname' ;और सत्यापन परिणाम सेट खाली नहीं है।
zerobandwidth

2
यद्यपि यह अनुक्रमणिकाओं के बारे में त्वरित जानकारी प्राप्त करने के मामले में एक बहुत ही उपयोगी उत्तर है, यह मूल प्रश्न का उत्तर नहीं देता है क्योंकि pg_indexesदृश्य कॉलम नाम प्रदान नहीं करता है। postgresql.org/docs/current/view-pg-indexes.html
akagixxer

146

\d table_nameइस जानकारी को दिखाता है psql, लेकिन यदि आप SQL का उपयोग करके डेटाबेस से ऐसी जानकारी प्राप्त करना चाहते हैं, तो PostgreSQL से META जानकारी निकालने पर एक नज़र डालें ।

मैं परीक्षण और उत्पादन वातावरण में PostgreSQL डेटाबेस की तुलना करने के लिए db स्कीमा से कुछ जानकारी की रिपोर्ट करने के लिए अपनी उपयोगिता में इस तरह की जानकारी का उपयोग करता हूं ।


Postgres से मेटा जानकारी निकालने पर आपका लिंक वही है जो मैं ढूंढ रहा था! इस धागे में युक्तियों का उपयोग करना और कुछ खुदाई में मुझे उस पोस्ट में उपयोग की जाने वाली क्वेरी के बहुत करीब मिला, लेकिन यह अच्छा है कि यह सब उस तरह से निर्धारित किया गया है।
ल्यूक फ्रेंकल

1
मैं AWS RDS PostgreSQL 9.6.5 का उपयोग कर रहा हूं और \d tableकोई इंडेक्स नहीं दिखाता है, हालांकि \diसभी इंडेक्स दिखाता है।
हेंडी इरावन

@ हेंडी इरावन यह स्पष्ट रूप से अन्य सेटिंग्स से प्रभावित हो सकता है। जैसे मुझे आश्चर्य है कि अगर आपके पास "टुपल्स ओनली" मोड ऑन (टॉगल करके \t) है। "टुपल्स ओनली" के साथ, मुझे इंडेक्स नहीं मिलते \d, "टुपल्स ओनली" से, मैं करता हूँ। यह psql (PostgreSQL) 9.6.15 के साथ है।
झामुमो

77

बस करो: \d table_name

लेकिन मुझे यकीन नहीं है कि आपका क्या मतलब है कि कॉलम के बारे में जानकारी नहीं है।

उदाहरण के लिए:

# \d pg_class
       Table "pg_catalog.pg_class"
     Column      |   Type    | Modifiers
-----------------+-----------+-----------
 relname         | name      | not null
 relnamespace    | oid       | not null
 reltype         | oid       | not null
 reloftype       | oid       | not null
 relowner        | oid       | not null
 relam           | oid       | not null
 relfilenode     | oid       | not null
 reltablespace   | oid       | not null
 relpages        | integer   | not null
 reltuples       | real      | not null
 reltoastrelid   | oid       | not null
 reltoastidxid   | oid       | not null
 relhasindex     | boolean   | not null
 relisshared     | boolean   | not null
 relistemp       | boolean   | not null
 relkind         | "char"    | not null
 relnatts        | smallint  | not null
 relchecks       | smallint  | not null
 relhasoids      | boolean   | not null
 relhaspkey      | boolean   | not null
 relhasexclusion | boolean   | not null
 relhasrules     | boolean   | not null
 relhastriggers  | boolean   | not null
 relhassubclass  | boolean   | not null
 relfrozenxid    | xid       | not null
 relacl          | aclitem[] |
 reloptions      | text[]    |
Indexes:
    "pg_class_oid_index" UNIQUE, btree (oid)
    "pg_class_relname_nsp_index" UNIQUE, btree (relname, relnamespace)

यह स्पष्ट रूप से दिखाता है कि इस तालिका में कौन से कॉलम दिए गए हैं।


मैं एक ऐसी चीज की उम्मीद कर रहा था, जो मुझे एक टेबल पर सभी अनुक्रमित करने देगी लेकिन आपको सही \d index_nameजानकारी है। इसलिए मैं एक टेबल पर अनुक्रमित को देख सकता हूं, फिर विवरण देख सकता हूं। कॉलम नहीं दिखाने से मेरा मतलब है कि मैंने \d tableनाम से उत्पन्न SQL को देखा और यह मेरे लिए स्पष्ट नहीं है कि कॉलम सूची कहां से आ रही है। मुझे लगता है कि इसे इंडेक्स परिभाषा से बाहर रखा गया है, जिसे मैं नहीं करना पसंद करूंगा।
ल्यूक फ्रेंकल

मैं AWS RDS PostgreSQL 9.6.5 का उपयोग कर रहा हूं और \d tableकोई इंडेक्स नहीं दिखाता है, हालांकि \diसभी इंडेक्स दिखाता है।
हेंडी इरावन

37

# \di

सुगमता और सबसे छोटा तरीका है \di, जो वर्तमान डेटाबेस में सभी अनुक्रमितों को सूचीबद्ध करेगा।

$ \di
                      List of relations
 Schema |            Name             | Type  |  Owner   |     Table     
--------+-----------------------------+-------+----------+---------------
 public | part_delivery_index         | index | shipper  | part_delivery
 public | part_delivery_pkey          | index | shipper  | part_delivery
 public | shipment_by_mandator        | index | shipper  | shipment_info
 public | shipment_by_number_and_size | index | shipper  | shipment_info
 public | shipment_info_pkey          | index | shipper  | shipment_info
(5 rows)

\di\dकमांड का "छोटा भाई" है जो वर्तमान डी एटबेस के सभी संबंधों को सूचीबद्ध करेगा । इस प्रकार \diनिश्चित रूप से "मुझे यह दिखाने के लिए खड़े हो जाओ डी डेटाबेस मैं ndexes"।

टाइपिंग \diSसभी अनुक्रमित प्रयुक्त सिस्टम लिस्ट को सूचीबद्ध करेगा, जिसका अर्थ है कि आपको सभी pg_catalog इंडेक्स भी मिलते हैं।

$ \diS
                                      List of relations
   Schema   |                   Name                    | Type  |  Owner   |          Table
------------+-------------------------------------------+-------+----------+-------------------------
 pg_catalog | pg_aggregate_fnoid_index                  | index | postgres | pg_aggregate
 pg_catalog | pg_am_name_index                          | index | postgres | pg_am
 pg_catalog | pg_am_oid_index                           | index | postgres | pg_am
 pg_catalog | pg_amop_fam_strat_index                   | index | postgres | pg_amop
 pg_catalog | pg_amop_oid_index                         | index | postgres | pg_amop
 pg_catalog | pg_amop_opr_fam_index                     | index | postgres | pg_amop
 pg_catalog | pg_amproc_fam_proc_index                  | index | postgres | pg_amproc
 pg_catalog | pg_amproc_oid_index                       | index | postgres | pg_amproc
 pg_catalog | pg_attrdef_adrelid_adnum_index            | index | postgres | pg_attrdef
--More-- 

इन दोनों आदेशों के साथ आप +इसके बाद और भी अधिक जानकारी प्राप्त कर सकते हैं जैसे कि डिस्क स्थान का आकार सूचकांक की जरूरत है और यदि उपलब्ध हो तो विवरण।

$ \di+
                                 List of relations
 Schema |            Name             | Type  |  Owner   |     Table     | Size  | Description 
--------+-----------------------------+-------+----------+---------------+-------+-------------
 public | part_delivery_index         | index | shipper  | part_delivery | 16 kB | 
 public | part_delivery_pkey          | index | shipper  | part_delivery | 16 kB | 
 public | shipment_by_mandator        | index | shipper  | shipment_info | 19 MB | 
 public | shipment_by_number_and_size | index | shipper  | shipment_info | 19 MB | 
 public | shipment_info_pkey          | index | shipper  | shipment_info | 53 MB | 
(5 rows)

Psql में आप आसानी से कमांड टाइपिंग के बारे में मदद पा सकते हैं \?


2
लेकिन यह उन स्तंभ नामों को नहीं दिखाता है जिन पर अनुक्रमित बनाए जाते हैं। कंपोजिट प्राइमरी कीज़ इंडेक्स में कई कॉलम होते हैं और जिन्हें देखा नहीं जा सकता है।
विग्नेश राजा

18

दूसरों के कोड के साथ संयुक्त और एक दृश्य बनाया:

CREATE OR REPLACE VIEW view_index AS 
SELECT
     n.nspname  as "schema"
    ,t.relname  as "table"
    ,c.relname  as "index"
    ,pg_get_indexdef(indexrelid) as "def"
FROM pg_catalog.pg_class c
    JOIN pg_catalog.pg_namespace n ON n.oid        = c.relnamespace
    JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid
    JOIN pg_catalog.pg_class t ON i.indrelid   = t.oid
WHERE c.relkind = 'i'
    and n.nspname not in ('pg_catalog', 'pg_toast')
    and pg_catalog.pg_table_is_visible(c.oid)
ORDER BY
     n.nspname
    ,t.relname
    ,c.relname;

12

कुछ सैंपल डेटा ...

create table test (a int, b int, c int, constraint pk_test primary key(a, b));
create table test2 (a int, b int, c int, constraint uk_test2 unique (b, c));
create table test3 (a int, b int, c int, constraint uk_test3b unique (b), constraint uk_test3c unique (c), constraint uk_test3ab unique (a, b));

उपयोग pg_get_indexdefसमारोह:

select pg_get_indexdef(indexrelid) from pg_index where indrelid = 'test'::regclass;

                    pg_get_indexdef
--------------------------------------------------------
 CREATE UNIQUE INDEX pk_test ON test USING btree (a, b)
(1 row)


select pg_get_indexdef(indexrelid) from pg_index where indrelid = 'test2'::regclass;
                     pg_get_indexdef
----------------------------------------------------------
 CREATE UNIQUE INDEX uk_test2 ON test2 USING btree (b, c)
(1 row)


select pg_get_indexdef(indexrelid) from pg_index where indrelid ='test3'::regclass;
                      pg_get_indexdef
------------------------------------------------------------
 CREATE UNIQUE INDEX uk_test3b ON test3 USING btree (b)
 CREATE UNIQUE INDEX uk_test3c ON test3 USING btree (c)
 CREATE UNIQUE INDEX uk_test3ab ON test3 USING btree (a, b)
(3 rows)

सरल और प्रभावी!
डेविड

बस कमाल। मैं भाग्यशाली हूं कि मैंने इस उत्तर को स्क्रॉल किया है।
महानवगृह

8

यह आदेश तालिका चर, अनुक्रमित और बाधाओं का दृश्य भी दिखाता है

=# \d table_name;

उदाहरण:

testannie=# \d dv.l_customer_account;

7

\d tablename संस्करण 8.3.8 पर मेरे लिए कॉलम नाम दिखाता है।

 "username_idx" UNIQUE, btree (username), tablespace "alldata1"

7

परिणाम का परिणाम:

table |     column     |          type          | notnull |  index_name  | is_index | primarykey | uniquekey | default
-------+----------------+------------------------+---------+--------------+----------+-   -----------+-----------+---------
 nodes | dns_datacenter | character varying(255) | f       |              | f        | f          | f         |
 nodes | dns_name       | character varying(255) | f       | dns_name_idx | t        | f          | f         |
 nodes | id             | uuid                   | t       | nodes_pkey   | t        | t          | t         |
(3 rows)

: QUERY

SELECT  
c.relname AS table,
f.attname AS column,  
pg_catalog.format_type(f.atttypid,f.atttypmod) AS type,
f.attnotnull AS notnull,  
i.relname as index_name,
CASE  
    WHEN i.oid<>0 THEN 't'  
    ELSE 'f'  
END AS is_index,  
CASE  
    WHEN p.contype = 'p' THEN 't'  
    ELSE 'f'  
END AS primarykey,  
CASE  
    WHEN p.contype = 'u' THEN 't' 
    WHEN p.contype = 'p' THEN 't' 
    ELSE 'f'
END AS uniquekey,
CASE
    WHEN f.atthasdef = 't' THEN d.adsrc
END AS default  FROM pg_attribute f  
JOIN pg_class c ON c.oid = f.attrelid  
JOIN pg_type t ON t.oid = f.atttypid  
LEFT JOIN pg_attrdef d ON d.adrelid = c.oid AND d.adnum = f.attnum  
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace  
LEFT JOIN pg_constraint p ON p.conrelid = c.oid AND f.attnum = ANY (p.conkey)  
LEFT JOIN pg_class AS g ON p.confrelid = g.oid
LEFT JOIN pg_index AS ix ON f.attnum = ANY(ix.indkey) and c.oid = f.attrelid and c.oid = ix.indrelid 
LEFT JOIN pg_class AS i ON ix.indexrelid = i.oid 

WHERE c.relkind = 'r'::char  
AND n.nspname = 'public'  -- Replace with Schema name 
--AND c.relname = 'nodes'  -- Replace with table name, or Comment this for get all tables
AND f.attnum > 0
ORDER BY c.relname,f.attname;

अच्छा है, हालांकि एक कॉलम के लिए "कॉलम" नाम एक आरक्षित शब्द है। स्कीमा के लिए IDEM,
parisni

5

कच्ची जानकारी pg_index में है


दिलचस्प। विशेष रूप से indkey: "यह इंडनेट्स मानों की एक सरणी है जो इंगित करता है कि यह सूचकांक सूचकांक किस तालिका स्तंभों को दर्शाता है। उदाहरण के लिए 1 3 के मान का मतलब होगा कि पहली और तीसरी तालिका कॉलम सूचकांक कुंजी बनाते हैं। इस सरणी में एक शून्य इंगित करता है कि। संबंधित सूचकांक विशेषता एक साधारण स्तंभ संदर्भ के बजाय टेबल कॉलम पर एक अभिव्यक्ति है "
ल्यूक फ्रेंकल

2

यदि आप सूचकांक में स्तंभ क्रम को संरक्षित करना चाहते हैं, तो ऐसा करने का तरीका (बहुत ही बदसूरत) तरीका है:

select table_name,
    index_name,
    array_agg(column_name)
from (
    select
        t.relname as table_name,
        i.relname as index_name,
        a.attname as column_name,
        unnest(ix.indkey) as unn,
        a.attnum
    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'
        and t.relnamespace = <oid of the schema you're interested in>
    order by
        t.relname,
        i.relname,
        generate_subscripts(ix.indkey,1)) sb
where unn = attnum
group by table_name, index_name

स्तंभ क्रम pg_index.indkey कॉलम में संग्रहीत किया जाता है, इसलिए मैंने उस सरणी के ग्राहकों द्वारा आदेश दिया।


2

इंडेक्स के साथ खेलने के दौरान इंडेक्स में जो कॉलम बनाए जाते हैं उसका क्रम उतना ही महत्वपूर्ण है जितना कि खुद कॉलम।

निम्न क्वेरी किसी दिए गए तालिका के लिए सभी अनुक्रमित और उनके सभी स्तंभों को क्रमबद्ध तरीके से सूचीबद्ध करती है।

SELECT
  table_name,
  index_name,
  string_agg(column_name, ',')
FROM (
       SELECT
         t.relname AS table_name,
         i.relname AS index_name,
         a.attname AS column_name,
         (SELECT i
          FROM (SELECT
                  *,
                  row_number()
                  OVER () i
                FROM unnest(indkey) WITH ORDINALITY AS a(v)) a
          WHERE v = attnum)
       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'
         AND t.relname LIKE 'tablename'
       ORDER BY table_name, index_name, i
     ) raw
GROUP BY table_name, index_name

2
ओपी को "यह प्रयास" क्यों करना चाहिए? एक अच्छे उत्तर में हमेशा एक स्पष्टीकरण होगा कि क्या किया गया था और यह इस तरह से क्यों किया गया था, न केवल ओपी के लिए बल्कि भविष्य के आगंतुकों के लिए एसओ जो इस प्रश्न को ढूंढ सकते हैं और आपके उत्तर को पढ़ रहे होंगे।
मैक्सिमिलियन एस्ट

iसंबंध प्रकार के लिए बहुत चालाक है। यह सुनिश्चित करता है कि कॉलम सही क्रम में बताए गए हैं।
क्रॉक

यह एकमात्र उत्तर था जिसने मेरे लिए काम किया। स्तंभ क्रम महत्वपूर्ण है। (यदि आप मुझ पर विश्वास नहीं करते हैं, तो फोनबुक में पहले नाम फ्रैंक वाले सभी लोगों की तलाश करें।)
जुराज

1

कृपया आवश्यक इंडेक्स के लिए नीचे ड्रिल करने के लिए क्वेरी की कोशिश करें

नीचे दिए गए प्रश्न - मैंने इसे व्यक्तिगत रूप से आज़माया है और इसे अक्सर उपयोग करता हूं।

SELECT n.nspname as "Schema",
  c.relname as "Name",
  CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' 
THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' END as "Type",
  u.usename as "Owner",
 c2.relname as "Table"
FROM pg_catalog.pg_class c
     JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid
     JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid
     LEFT JOIN pg_catalog.pg_user u ON u.usesysid = c.relowner
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('i','')
      AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
      AND pg_catalog.pg_table_is_visible(c.oid)
      AND c2.relname like '%agg_transaction%' --table name
      AND nspname = 'edjus' -- schema name 
ORDER BY 1,2;

1

स्वीकृत उत्तर के समान लेकिन pg_attribute पर सामान्य जुड़ने या pg_attribute के साथ क्वेरी के रूप में बाईं ओर जुड़ने से कोई संकेत नहीं मिलता है जो इस प्रकार हैं:
create unique index unique_user_name_index on users (lower(name))

select 
    row_number() over (order by c.relname),
    c.relname as index, 
    t.relname as table, 
    array_to_string(array_agg(a.attname), ', ') as column_names 
from pg_class c
join pg_index i on c.oid = i.indexrelid and c.relkind='i' and c.relname not like 'pg_%' 
join pg_class t on t.oid = i.indrelid
left join pg_attribute a on a.attrelid = t.oid and a.attnum = ANY(i.indkey) 
group by t.relname, c.relname order by c.relname;

अच्छी बात है, लेकिन उस "लोअर (
कॉलम_नाम

1

यहाँ एक ऐसा कार्य है जो ad360 के उत्तर को लपेटता है:

CREATE OR REPLACE FUNCTION getIndices(_table_name varchar)
  RETURNS TABLE(table_name varchar, index_name varchar, column_name varchar) AS $$
  BEGIN
    RETURN QUERY
    select
    t.relname::varchar as table_name,
    i.relname::varchar as index_name,
    a.attname::varchar 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'
    and t.relname = _table_name
order by
    t.relname,
    i.relname;
  END;
  $$ LANGUAGE plpgsql;

उपयोग:

select * from getIndices('<my_table>')

मेरी अनुक्रमणिका के उन हिस्सों को सूचीबद्ध नहीं किया जो फ़ंक्शंस का उपयोग करते हैं (उदाहरण के लिए "ऊपरी (फ़ील्ड_नाम)")।
जॉनमुद

0

कैसे एक सरल समाधान के बारे में:

SELECT 
  t.relname table_name,
  ix.relname index_name,
  indisunique,
  indisprimary, 
  regexp_replace(pg_get_indexdef(indexrelid), '.*\((.*)\)', '\1') columns
FROM pg_index i
JOIN pg_class t ON t.oid = i.indrelid
JOIN pg_class ix ON ix.oid = i.indexrelid
WHERE t.relname LIKE 'test%'

`


इस घोल से प्यार करें। दुर्भाग्य से यह अनुक्रमणिका के साथ विफल होता है जहां क्लॉस होता है। (या अन्य कोष्ठक)
kbrock

मैंने शुरुआत में पार्न्स को छोड़ना नहीं छोड़ा, और बीच में पार्न को कैप्चर नहीं किया, और उसके बाद सब कुछ छोड़ दिया। '^[^\)]*\(([^\)]*)\).*$'
क्रॉक

0

@ ad360 का उत्कृष्ट उत्तर, सिंटैक्स में शामिल होने के लिए परिवर्तित किया गया।

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
join pg_index ix
on t.oid = ix.indrelid
join pg_class i
on i.oid = ix.indexrelid
join pg_attribute a
on a.attrelid = t.oid
and a.attnum = ANY(ix.indkey)
where t.relkind = 'r'
and t.relname like 'test%'
group by t.relname
       , i.relname
order by t.relname
       , i.relname
;

0

मुझे नहीं लगता कि यह संस्करण अभी तक इस थ्रेड पर मौजूद है: यह इंडेक्स के लिए ddl के साथ कॉलम नामों की सूची प्रदान करता है।

CREATE OR REPLACE VIEW V_TABLE_INDEXES AS

SELECT
     n.nspname  as "schema"
    ,t.relname  as "table"
    ,c.relname  as "index"
    ,i.indisunique AS "is_unique"
    ,array_to_string(array_agg(a.attname), ', ') as "columns"
    ,pg_get_indexdef(i.indexrelid) as "ddl"
FROM pg_catalog.pg_class c
    JOIN pg_catalog.pg_namespace n ON n.oid        = c.relnamespace
    JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid
    JOIN pg_catalog.pg_class t ON i.indrelid   = t.oid
    JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(i.indkey)
WHERE c.relkind = 'i'
      and n.nspname not in ('pg_catalog', 'pg_toast')
      and pg_catalog.pg_table_is_visible(c.oid)
GROUP BY
    n.nspname
    ,t.relname
    ,c.relname
    ,i.indisunique
    ,i.indexrelid
ORDER BY
    n.nspname
    ,t.relname
    ,c.relname;

मैंने पाया कि फ़ंक्शन का उपयोग करने वाले इंडेक्स कॉलम के नामों से लिंक नहीं करते हैं, इसलिए कभी-कभी आपको इंडेक्स लिस्टिंग मिलती है जैसे कि एक कॉलम नाम जब वास्तव में 3 का उपयोग होता है।

उदाहरण:

CREATE INDEX ui1 ON table1 (coalesce(col1,''),coalesce(col2,''),col3)

इंडेक्स पर कॉलम के रूप में क्वेरी केवल 'col3' लौटाती है, लेकिन DDL इंडेक्स में उपयोग किए गए कॉलम का पूरा सेट दिखाता है।


0

@ Cope360 के अच्छे उत्तर का विस्तार करें। निश्चित तालिका के लिए पाने के लिए (उनका नाम समान तालिका नाम है, लेकिन विभिन्न स्कीमा), बस तालिका OID का उपयोग कर।

select
     t.relname as table_name
    ,i.relname as index_name
    ,a.attname as column_name
    ,a.attrelid tableid

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'
    -- and t.relname like 'tbassettype'
    and a.attrelid = '"dbLegal".tbassettype'::regclass
order by
    t.relname,
    i.relname;

स्पष्ट करें: मेरे पास स्कीमा 'dbAsset' और 'dbLegal' दोनों में तालिका का नाम 'tbassettype' है। DbLegal पर केवल तालिका प्राप्त करने के लिए, बस a.attrelid = इसका OID दें।


0

@ Ad360 का थोड़ा संशोधित उत्तर:

create table test (a int, b int, c int, constraint pk_test primary key(c, a, b));
select i.relname as index_name,
       ix.indisunique as is_unique,
       a.attname as column_name,
from pg_class c
       inner join pg_index ix on c.oid=ix.indrelid
       inner join pg_class i on ix.indexrelid=i.oid
       inner join pg_attribute a on a.attrelid=c.oid and a.attnum=any(ix.indkey)
where c.oid='public.test'::regclass::oid
order by array_position(ix.indkey, a.attnum) asc;

यह सही क्रम में सूचकांक कॉलम दिखाएगा:

index_name      is_unique  column_name
pk_test         true       c
pk_test         true       a
pk_test         true       b

"लेफ्ट ज्वाइन pg_attribute" का उपयोग करते हुए, एक पूर्ण स्तंभ के साथ निश्चित रूप से गणना किए गए कॉलम पर अनुक्रमित दिखाएंगे।
पाओलो बोन्जिनी

0
select t.relname as table_name, 
       i.relname as index_name, 
       array_position(ix.indkey,a.attnum) pos, 
       a.attname as column_name
from pg_class t
join pg_index ix on t.oid = ix.indrelid
join pg_class i on i.oid = ix.indexrelid
join pg_attribute a on a.attrelid = t.oid and a.attnum = ANY(ix.indkey)
where t.relkind = 'r'
and t.relname like 'orders'
order by t.relname, i.relname, array_position(ix.indkey,a.attnum)

0

@ Ad360 द्वारा स्वीकृत उत्तर अच्छा है, लेकिन मैं ओरेकल के DBA_IND_COLUMNS, ALL_IND_COLUMNS और USER_IND_COLUMNS (उदाहरण के लिए, तालिका अनुक्रमणिका स्कीमा और एक बहुरंगी सूचकांक में सूचकांक की स्थिति) जैसे कुछ और चाहता था, इसलिए मैंने स्वीकार किया इस में उत्तर दें:

with
 ind_cols as (
select
    n.nspname as schema_name,
    t.relname as table_name,
    i.relname as index_name,
    a.attname as column_name,
    1 + array_position(ix.indkey, a.attnum) as column_position
from
     pg_catalog.pg_class t
join pg_catalog.pg_attribute a on t.oid    =      a.attrelid 
join pg_catalog.pg_index ix    on t.oid    =     ix.indrelid
join pg_catalog.pg_class i     on a.attnum = any(ix.indkey)
                              and i.oid    =     ix.indexrelid
join pg_catalog.pg_namespace n on n.oid    =      t.relnamespace
where t.relkind = 'r'
order by
    t.relname,
    i.relname,
    array_position(ix.indkey, a.attnum)
)
select * 
from ind_cols
where schema_name = 'test'
  and table_name  = 'indextest'
order by schema_name, table_name
;

यह एक आउटपुट देता है जैसे:

 schema_name | table_name | index_name | column_name | column_position 
-------------+------------+------------+-------------+-----------------
 test        | indextest  | testind1   | singleindex |               1
 test        | indextest  | testind2   | firstoftwo  |               1
 test        | indextest  | testind2   | secondoftwo |               2
(3 rows)
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.