From: | "David E(dot) Wheeler" <david(at)justatheory(dot)com> |
---|---|
To: | PostgreSQL-development Hackers <pgsql-hackers(at)postgresql(dot)org> |
Subject: | List of Index Columns & Expressions |
Date: | 2013-01-10 21:50:26 |
Message-ID: | C8E5E473-1093-4E93-B1DB-F6A9C0052A58@justatheory.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-hackers |
Hackers,
I'm trying to write a query to give me a list of the columns and/or expressions in an index. For example, given this table:
david=# \d foo
Table "public.foo"
Column | Type | Modifiers
---------+-----------+-----------
id | integer |
bar_ids | integer[] |
Indexes:
"idx_foo_stuff" btree (id, abs(id), (bar_ids[1]))
I'd like to write a query to emit:
i | coalesce
---+---------------------
0 | id
1 | abs(id)
2 | bar_ids[1]
However, it looks as if I can only get multiple expressions as a single string. The query I've come up with is:
SELECT s.i, COALESCE(a.attname, pg_catalog.pg_get_expr( x.indexprs, ct.oid ))
FROM pg_catalog.pg_index x
JOIN pg_catalog.pg_class ct ON ct.oid = x.indrelid
JOIN pg_catalog.pg_class ci ON ci.oid = x.indexrelid
JOIN pg_catalog.pg_namespace n ON n.oid = ct.relnamespace
JOIN generate_series(0, current_setting('max_index_keys')::int - 1) s(i)
ON x.indkey[s.i] IS NOT NULL
LEFT JOIN pg_catalog.pg_attribute a
ON ct.oid = a.attrelid
AND a.attnum = x.indkey[s.i]
WHERE ct.relname = 'foo'
AND ci.relname = 'idx_foo_stuff'
AND n.nspname = 'public'
ORDER BY s.i;
Which emits:
i | coalesce
---+---------------------
0 | id
1 | abs(id), bar_ids[1]
2 | abs(id), bar_ids[1]
Is there some way to only get the relevant index expression from indexprs, rather than the whole expression?
Thanks,
David
PS: I need this to work all the way back to 8.1, if possible.
From | Date | Subject | |
---|---|---|---|
Next Message | Peter Eisentraut | 2013-01-10 22:06:01 | Re: PL/perl should fail on configure, not make |
Previous Message | Tom Lane | 2013-01-10 21:48:28 | Re: PL/perl should fail on configure, not make |