Goal This query retrieves the ordinal positions of primary key columns for all base tables within the database, explicitly including those created via inheritance. It acts as an auditing tool to help database administrators and developers understand column layouts and verify schema design conventions across the entire table hierarchy.
Type General Overview of some aspect of the database.
License MIT (opens in new tab)
Data Source INFORMATION_SCHEMA+system catalog
SQL Query
WITH pk_columns AS (
    SELECT 
        target_table_oid, 
        target_schema AS table_schema, 
        target_table AS table_name, 
        a.attname AS column_name, 
        a.attnum AS ordinal_position
    FROM (
        SELECT 
            nc.nspname AS target_schema,
            c.relname AS target_table, 
            c.oid AS target_table_oid,
            unnest(o.conkey) AS target_col
        FROM pg_constraint o 
        INNER JOIN pg_class c ON c.oid = o.conrelid
        INNER JOIN pg_namespace AS nc ON nc.oid = c.relnamespace
        INNER JOIN pg_authid AS a ON nc.nspowner = a.oid
        WHERE (nc.nspname = 'public' OR rolname <> 'postgres')
          AND o.contype = 'p'
    ) t
    INNER JOIN pg_attribute a ON t.target_col = a.attnum 
        AND t.target_table_oid = a.attrelid 
        AND a.attisdropped = false
),
non_pk_columns AS (
    SELECT table_schema, table_name, column_name, ordinal_position
    FROM INFORMATION_SCHEMA.columns
    EXCEPT 
    SELECT table_schema, table_name, column_name, ordinal_position
    FROM pk_columns
),
parent_tables AS (
    SELECT 
        i.inhrelid AS child_oid,
        string_agg(n.nspname, ', ' ORDER BY n.nspname) AS parent_table_schema,
        string_agg(c.relname, ', ' ORDER BY c.relname) AS parent_table_name
    FROM pg_inherits i
    INNER JOIN pg_class c ON c.oid = i.inhparent
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    GROUP BY i.inhrelid
)
SELECT 
    p.table_schema, 
    p.table_name,    
    pt.parent_table_schema,
    pt.parent_table_name,
    string_agg(p.column_name || '(' || p.ordinal_position::text || ')', ',' ORDER BY p.ordinal_position) AS primary_key_columns
FROM pk_columns AS p
LEFT JOIN parent_tables pt ON p.target_table_oid = pt.child_oid
AND EXISTS (
    SELECT 1 
    FROM non_pk_columns AS n 
    WHERE p.table_schema = n.table_schema 
      AND p.table_name = n.table_name 
      AND p.ordinal_position > n.ordinal_position
)
GROUP BY 
    p.table_schema, 
    p.table_name,
    pt.parent_table_schema,
    pt.parent_table_name
ORDER BY 
    p.table_schema, 
    p.table_name;

Categories

This query is classified under the following categories:

Comfortability of data management

Queries of this category provide information about the means that have been used to make the use or management of database more comfortable and thus, more efficient.

Inconsistencies

Queries of this catergory provide information about inconsistencies of solving the same problem in different places.

NameDescription
Comfortability of data managementQueries of this category provide information about the means that have been used to make the use or management of database more comfortable and thus, more efficient.
InconsistenciesQueries of this catergory provide information about inconsistencies of solving the same problem in different places.