sql >> Database teknologi >  >> RDS >> PostgreSQL

Liste alle indeksnavne, kolonnenavne og dets tabelnavn for en PostgreSQL-database

Dette vil udlæse alle indekser med detaljer (udtrukket fra mine visningsdefinitioner):

SELECT i.relname as indname,
       i.relowner as indowner,
       idx.indrelid::regclass,
       am.amname as indam,
       idx.indkey,
       ARRAY(
       SELECT pg_get_indexdef(idx.indexrelid, k + 1, true)
       FROM generate_subscripts(idx.indkey, 1) as k
       ORDER BY k
       ) as indkey_names,
       idx.indexprs IS NOT NULL as indexprs,
       idx.indpred IS NOT NULL as indpred
FROM   pg_index as idx
JOIN   pg_class as i
ON     i.oid = idx.indexrelid
JOIN   pg_am as am
ON     i.relam = am.oid;

Tilføj eventuelt en ekstra join til slutningen for at trimme navneområderne:

SELECT i.relname as indname,
       i.relowner as indowner,
       idx.indrelid::regclass,
       am.amname as indam,
       idx.indkey,
       ARRAY(
       SELECT pg_get_indexdef(idx.indexrelid, k + 1, true)
       FROM generate_subscripts(idx.indkey, 1) as k
       ORDER BY k
       ) as indkey_names,
       idx.indexprs IS NOT NULL as indexprs,
       idx.indpred IS NOT NULL as indpred
FROM   pg_index as idx
JOIN   pg_class as i
ON     i.oid = idx.indexrelid
JOIN   pg_am as am
ON     i.relam = am.oid
JOIN   pg_namespace as ns
ON     ns.oid = i.relnamespace
AND    ns.nspname = ANY(current_schemas(false));


  1. SSL til PostgreSQL-forbindelsesnodejs

  2. Guide til designdatabase til quiz i MySQL

  3. 5 måder at returnere rækker, der indeholder små bogstaver i SQL Server

  4. SQL Vælg mellem to tabeller med indre sammenføjning og grænse