Skip to content

Instantly share code, notes, and snippets.

@adamantnz
Last active February 26, 2024 02:45
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save adamantnz/fb7cc33b6bf25a93a3e7 to your computer and use it in GitHub Desktop.
Save adamantnz/fb7cc33b6bf25a93a3e7 to your computer and use it in GitHub Desktop.
Redshift - view table/schema dependencies
CREATE OR REPLACE VIEW dbo.vwdependencies
AS
SELECT DISTINCT c_p.oid AS tbloid
,n_p.nspname AS schemaname
,c_p.relname AS NAME
,n_c.nspname AS refbyschemaname
,c_c.relname AS refbyname
,c_c.oid AS viewoid
FROM pg_class c_p
JOIN pg_depend d_p ON c_p.relfilenode = d_p.refobjid
JOIN pg_depend d_c ON d_p.objid = d_c.objid
JOIN pg_class c_c ON d_c.refobjid = c_c.relfilenode
LEFT JOIN pg_namespace n_p ON c_p.relnamespace = n_p.oid
LEFT JOIN pg_namespace n_c ON c_c.relnamespace = n_c.oid
WHERE d_c.deptype = 'i'::"char"
AND c_c.relkind = 'v'::"char"
/* AND lower(name) = 'table_name' */
;
@mbarkhau
Copy link

There were some corner cases where this selected invalid values. This other query did work however: https://github.com/awslabs/amazon-redshift-utils/blob/master/src/AdminViews/v_view_dependency.sql

@bencodeorg
Copy link

Helpful, thank you!

@ChetanSolur
Copy link

Very helpful to identify dependency tables, especially when you rename the original table. Thank you!

@onsiri
Copy link

onsiri commented Jan 25, 2019

is this only apply to views or tables too?

@surajsharmagit
Copy link

above query will not list down views if views created with no schema binding. Any solution to this issue ?

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