-- -- timespan__overlaps_p/2 -- create or replace function timespan__overlaps_p( integer, integer ) returns bool as $$ declare overlaps_p__timespan_1_id alias for $1; overlaps_p__timespan_2_id alias for $2; v_result boolean; rec_timespan record; begin -- Loop over 2nd timespan, checking each interval against 1st for rec_timespan in select * from timespans where timespan_id = overlaps_p__timespan_2_id loop v_result := timespan__overlaps_interval_p( overlaps_p__timespan_1_id, rec_timespan.interval_id ); if v_result then return true; end if; end loop; return false; end;$$ language plpgsql; -- -- timespan__overlaps_p/3 -- create or replace function timespan__overlaps_p( integer, timestamp with time zone, timestamp with time zone ) returns bool as $$ declare overlaps_p__timespan_id alias for $1; overlaps_p__start_date alias for $2; -- default null, overlaps_p__end_date alias for $3; -- default null v_result boolean; rec_timespan record; begin -- Loop over each interval in timespan, checking against dates. for rec_timespan in select * from timespans where timespan_id = overlaps_p__timespan_id loop v_result := time_interval__overlaps_p( rec_timespan.interval_id, overlaps_p__start_date, overlaps_p__end_date ); if v_result then return true; end if; end loop; return false; end;$$ language plpgsql;