Skip to content

Instantly share code, notes, and snippets.

@barbietunnie
Last active November 2, 2023 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barbietunnie/16e02ef6e756d6d10082b3927aa9a217 to your computer and use it in GitHub Desktop.
Save barbietunnie/16e02ef6e756d6d10082b3927aa9a217 to your computer and use it in GitHub Desktop.
Find dependent objects for a table or view in Postgresql or Redshift

Find dependent objects for a table or view in Postgresql or Redshift

SELECT dependent_ns.nspname as dependent_schema
, dependent_view.relname as dependent_view 
, source_ns.nspname as source_schema
, source_table.relname as source_table
, pg_attribute.attname as column_name
FROM pg_depend 
JOIN pg_rewrite ON pg_depend.objid = pg_rewrite.oid 
JOIN pg_class as dependent_view ON pg_rewrite.ev_class = dependent_view.oid 
JOIN pg_class as source_table ON pg_depend.refobjid = source_table.oid 
JOIN pg_attribute ON pg_depend.refobjid = pg_attribute.attrelid 
    AND pg_depend.refobjsubid = pg_attribute.attnum 
JOIN pg_namespace dependent_ns ON dependent_ns.oid = dependent_view.relnamespace
JOIN pg_namespace source_ns ON source_ns.oid = source_table.relnamespace
WHERE 
source_ns.nspname = 'my_schema'
AND source_table.relname = 'my_table'
AND pg_attribute.attnum > 0 
-- AND pg_attribute.attname = 'my_column'
ORDER BY 1,2;

Source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment