Say I have a table like this
WITH conds(cond) AS (
SELECT '[3, 5)'::int4range
UNION
SELECT '[6, 8)'::int4range
UNION
SELECT '[9, 20)'::int4range
)
SELECT cond FROM conds;
For a given input range, I want to break it into homogeneous sub-ranges which either are entirely contained in some row in conds, or do not overlap with any row in conds. There should be an additional column indicating whether each sub-range is covered by conds.
More concretely, for an input period of '[1, 11)'::int4range, the expected output is
?column? | ?column?
-----------+----------
[1,3) | f
[3,5) | t
[5,6) | f
[6,8) | t
[8,9) | f
[9,11) | t
(6 rows)
Every two rows in conds are guaranteed to be disjoint, but conds may also be empty (in which case the output is just the input range and f), and each cond may overlap with the bound of the input range (as shown in the example above).
Which query can achieve this? This answer tells me how to handle the case where cond only has one row, but it may contain multiple rows for me.