Last active
February 26, 2024 02:45
-
-
Save adamantnz/fb7cc33b6bf25a93a3e7 to your computer and use it in GitHub Desktop.
Redshift - view table/schema dependencies
This file contains hidden or 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
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' */ | |
; |
Helpful, thank you!
Very helpful to identify dependency tables, especially when you rename the original table. Thank you!
is this only apply to views or tables too?
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
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