SELECT
    f.attnum AS number,
    c.relname AS table_name,
    f.attname AS column_name,
FROM pg_attribute f
    JOIN pg_class c ON c.oid = f.attrelid
    JOIN pg_type t ON t.oid = f.atttypid
    LEFT JOIN pg_attrdef d ON d.adrelid = c.oid AND d.adnum = f.attnum
    LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
    LEFT JOIN pg_constraint p ON p.conrelid = c.oid AND f.attnum = ANY (p.conkey)
    LEFT JOIN pg_class AS g ON p.confrelid = g.oid
WHERE c.relkind = 'r'::char
    AND n.nspname = $1    -- schema name
    AND c.relname = $2 -- table name
    AND p.contype = 'p'  -- primary key
    AND f.attnum > 0
ORDER BY number;