Re: List of Index Columns & Expressions

From: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: PostgreSQL-development Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: List of Index Columns & Expressions
Date: 2013-01-10 22:27:48
Message-ID: 204ADCAA-853B-4B5A-A080-4DFA0470B790@justatheory.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Jan 10, 2013, at 2:16 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

>> Is there some way to only get the relevant index expression from indexprs, rather than the whole expression?
>
> pg_get_indexdef() is your friend. You really, really don't want to
> write any client-side code that inspects indexprs directly. It'll
> break.

Ah-hah, somehow I missed that. So this:

SELECT s.i, pg_catalog.pg_get_indexdef( ci.oid, s.i+1, false)
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
WHERE ct.relname = 'foo'
AND ci.relname = 'idx_foo_stuff'
AND n.nspname = 'public'
ORDER BY s.i

Returns:

i | pg_get_indexdef
---+-----------------
0 | id
1 | abs(id)
2 | (bar_ids[1])

Which is perfect. Thanks!

David

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2013-01-10 22:30:30 Re: PL/perl should fail on configure, not make
Previous Message Tom Lane 2013-01-10 22:16:28 Re: List of Index Columns & Expressions