CREATE VIEW collectorQueryDependencyTree AS WITH RECURSIVE collectorQueryDependencyTree AS ( SELECT cq.id, aqc.id as queryId, aqc.type, aqc.requiredIds, aqc.activeVersion FROM fullActiveQueries as aqc JOIN activeCollectorsWithRequiredIDs as cq ON aqc.id = ANY(cq.queryIds) UNION ALL SELECT acq.id, q.id as queryId, q.type, q.requiredIds, q.activeVersion FROM fullActiveQueries as q JOIN collectorQueryDependencyTree as acq on q.id = ANY(acq.requiredIds) ) SELECT id as collectorId, queryId, type, activeVersion as queryVersion, requiredIds::uuid[] FROM collectorQueryDependencyTree;