Skip to content

Instantly share code, notes, and snippets.

@toreriklinnerud
Created April 14, 2021 14:03
Show Gist options
  • Save toreriklinnerud/dbc34bef5556bbdb25f793c8c228c0cf to your computer and use it in GitHub Desktop.
Save toreriklinnerud/dbc34bef5556bbdb25f793c8c228c0cf to your computer and use it in GitHub Desktop.
Postgres: list columns in relation A that are not in relation B (works for tables, views, materialized views)
WITH relations AS (SELECT
a.attname,
pg_catalog.format_type(a.atttypid, a.atttypmod) AS type,
t.relname
FROM
pg_attribute a
JOIN pg_class t ON a.attrelid = t.oid
JOIN pg_namespace s ON t.relnamespace = s.oid
WHERE
a.attnum > 0
AND NOT a.attisdropped
AND s.nspname = 'public'
ORDER BY
a.attnum
)
SELECT attname, type FROM relations WHERE relname = 'A'
EXCEPT
SELECT attname, type FROM relations WHERE relname = 'B'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment