I'm using postgres 12.5. I've been researching answers on how to get the names of the columns that comprise a unique index. After combining a few answer on StackOverflow and elsewhere I came up with this solution
select i.relname as index_name,
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.accounts'::regclass::oid
and ix.indisunique is true
order by array_position(ix.indkey, a.attnum) asc
That solution worked great for unique indexes on traditional column types but didn't work for unique indexes on fields within a jsonb column type. See the example in this SQLFiddle
http://sqlfiddle.com/#!17/22f6d/4
In that SQLFiddle you can see how the table is created and two different unique indexes are created. You can see how the above query gets all regular column names that comprise a unique index but doesn't get the jsonb field name. But you can also see that the unique index on the jsonb field actually does its job.
How would I modify (or replace) the above query to return the names of the columns that comprise a compound unique index including jsonb fields?
p.s. Please don't suggest I move the field out of jsonb to a dedicated column. This is a legacy schema that I have inherited and need to make it work as-is