Skip to main content
added 1751 characters in body
Source Link

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_columnstableindex_nameqcolumn_names
{file_id}file_lookup_4kdate_indexfile_id
{file_id}file_lookup_4kdate2_indexabs
{sequence_no}file_lookup_4kdate3_indexround
{sequence_no}file_lookup_4kdate4_indexround
{content}file_lookup_4kdate5_indexsubstring
{file_id,sequence_no}file_lookup_4kdate6_indexabs

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_columnstableindex_nameqcolumn_names
{file_id}file_lookup_4kdate_indexfile_id
{file_id}file_lookup_4kdate2_indexabs
{sequence_no}file_lookup_4kdate3_indexround
{sequence_no}file_lookup_4kdate4_indexround
{content}file_lookup_4kdate5_indexsubstring
{file_id,sequence_no}file_lookup_4kdate6_indexabs
added 54 characters in body
Source Link

Thinking on this for a moment, perhaps better than pg_get_indexdef is pulling column ordinals out of the indexprs column noted in the first answer, and specifically the varattno field.   

regexp_matches for thatover the 'indexprs' (and only thatconverting :pg_tree_node to text) for varattno) 

I threw together the following, going back to the definition in the columns table of information_schema:

Thinking on this for a moment, perhaps better than pg_get_indexdef is pulling column ordinals out of the indexprs column noted in the first answer, and specifically the varattno field.  regexp_matches for that (and only that :)) I threw together the following, going back to the definition in the columns table of information_schema:

Thinking on this for a moment, perhaps better than pg_get_indexdef is pulling column ordinals out of the indexprs column noted in the first answer, and specifically the varattno field. 

regexp_matches over the 'indexprs' (converting pg_tree_node to text) for varattno) 

I threw together the following, going back to the definition in the columns table of information_schema:

added 227 characters in body
Source Link
  • I joined only to the attname table, because that is really all I need
  • JSON_AGG(columns.column_name) can be replaced with JSON_AGG(columns.*) or the like to select the entire row for the columns.
  • ANY (ARRAY['r', 't', 'i'] was updated to IN ANY ('r', 't', 'i')
  • I joined only to the attname table, because that is really all I need
  • JSON_AGG(columns.column_name) can be replaced with JSON_AGG(columns.*) or the like to select the entire row for the columns.
  • I joined only to the attname table, because that is really all I need
  • JSON_AGG(columns.column_name) can be replaced with JSON_AGG(columns.*) or the like to select the entire row for the columns.
  • ANY (ARRAY['r', 't', 'i'] was updated to IN ANY ('r', 't', 'i')
added 227 characters in body
Source Link
Loading
Source Link
Loading