Skip to content

Instantly share code, notes, and snippets.

@bitdivine
Last active June 3, 2016 09:03
Show Gist options
  • Select an option

  • Save bitdivine/225fa1277074b5220891e4c4a39f2fee to your computer and use it in GitHub Desktop.

Select an option

Save bitdivine/225fa1277074b5220891e4c4a39f2fee to your computer and use it in GitHub Desktop.
List the tables that a view depends on.
-- List the tables that a view depends on.
-- Usage: select * from inf_view_dependencies(SOME_VIEW);
-- Thanks to Dave: http://stackoverflow.com/questions/4229468/getting-a-list-of-tables-that-a-view-table-depends-on-in-postgresql
create function inf_view_dependencies(v text)
returns table (kind text, name text) as $$
SELECT cl_d.relkind::text as kind
, cl_d.relname::text AS name
FROM pg_rewrite AS r
JOIN pg_class AS cl_r ON r.ev_class=cl_r.oid
JOIN pg_depend AS d ON r.oid=d.objid
JOIN pg_class AS cl_d ON d.refobjid=cl_d.oid
WHERE cl_d.relkind IN ('r','v','m') AND cl_r.relname=$1 and cl_d.relname!=$1
GROUP BY cl_d.relname, cl_d.relkind
ORDER BY cl_d.relname;
$$ language SQL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment