patternsqlMinor
Get all recursive dependencies of a single database object
Viewed 0 times
alldatabaserecursivegetsingleobjectdependencies
Problem
I've written a SELECT statement that creates a List of all objects that depend on a single object, so that if I wanted to DROP that object I could DROP all referenced objects first without using CASCADE.
It's a long script and I'm not sure if it's gonna work in every situation, so I just wanted to ask if I've missed something or if there is a way to optimize / shorten this script.
It's a long script and I'm not sure if it's gonna work in every situation, so I just wanted to ask if I've missed something or if there is a way to optimize / shorten this script.
WITH RECURSIVE dep_recursive AS (
-- Recursion: Initial Query
SELECT
0 AS "level",
'enter_object_name' AS "dep_name", -- 0 -- ignore the initial object (level 0)
GROUP BY dep_name -- ignore multiple references to dependent objects, dropping them once is enough
ORDER BY level desc, dep_name; -- level descending: deepest dependency firstSolution
Looks good! Seemed to work for me, except in postgres 9.3 (harmless in previous) add:
in the classType subquery.
WHEN 'm' THEN 'MATERIALIZED VIEW'::textin the classType subquery.
Code Snippets
WHEN 'm' THEN 'MATERIALIZED VIEW'::textContext
StackExchange Code Review Q#23181, answer score: 2
Revisions (0)
No revisions yet.