EDIT:
In order to bring this into a single column, Let's COALESCE
, and whenever the new function returns NULL
, we will insert an array containing the result of the pg_get_indexdef
, and prefer ARRAY_AGG
to JSON_AGG
. All together:
SELECT COALESCE((SELECT ARRAY_AGG(columns.column_name)
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = i.indrelid::regclass::text
AND ordinal_position IN (SELECT matches[1]::INTEGER
FROM regexp_matches(i.indexprs::TEXT,
'varattno (\d)',
'g') as matches)),
ARRAY[(pg_get_indexdef(att.attrelid, att.attnum, true))]) AS argument_columns
, i.indrelid::regclass::text AS table
, c.relname AS index_nameq
, att.attname as column_names
FROM pg_catalog.pg_namespace n
JOIN pg_catalog.pg_class c ON c.relnamespace = n.oid
JOIN pg_catalog.pg_attribute att ON att.attrelid = c.oid
JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid
WHERE n.nspname !~ '^pg_'
AND c.relkind IN ('r', 't', 'i');
This produces:
argument_columns | table | index_nameq | column_names |
---|---|---|---|
{file_id} | file_lookup_4k | date_index | file_id |
{file_id} | file_lookup_4k | date2_index | abs |
{sequence_no} | file_lookup_4k | date3_index | round |
{sequence_no} | file_lookup_4k | date4_index | round |
{content} | file_lookup_4k | date5_index | substring |
{file_id,sequence_no} | file_lookup_4k | date6_index | abs |