Created
April 14, 2021 14:03
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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