range_agg
range_agg · range_agg: merge adjacent or overlapping PostgreSQL range values
Overview
Usage
Sources:
- Official README
- Official version 1.2.1 SQL
- Official PGXN release
- PostgreSQL built-in aggregate documentation
range_agg version 1.2.1 merges adjacent or overlapping PostgreSQL ranges and can reject gaps or overlaps. It predates PostgreSQL’s built-in multirange aggregate of the same name, so modern installations must account for name resolution and different return types.
Core Workflow
On a server where this extension is appropriate, merge contiguous reservations per room:
The one-argument form returns one range and raises an error if it encounters a gap or overlap. To coalesce ranges while permitting gaps, use the two-argument form and unnest the returned array:
Aggregate Forms
range_agg(anyrange)returns one range and rejects gaps and overlaps.range_agg(range, permit_gaps)returns an array, allows gaps when true, and still rejects overlaps.range_agg(range, permit_gaps, permit_overlaps)returns an array and independently controls both conditions.- The two- and three-argument forms are declared for
int4range,int8range,numrange,tsrange,tstzrange, anddaterange. Custom range types need matching transition/final functions and aggregate declarations.
Operational Caveats
PostgreSQL 14 and later provide pg_catalog.range_agg(anyrange) returning a multirange. That built-in has different semantics and normally wins unqualified name lookup, while this extension’s one-argument aggregate returns a single range. Verify the selected function with regprocedure, schema-qualify calls if coexistence is intentional, and prefer the maintained built-in on current PostgreSQL. Aggregation errors abort the statement when ranges overlap or contain forbidden gaps. Test empty groups, infinite bounds, discrete versus continuous ranges, and custom canonicalization before relying on coalescing results.