ST_Union
What is ST_Union?
ST_Union returns the set-theoretic union of geometries — all the space covered by any of the inputs, with overlaps dissolved. It exists as both a two-argument function and as an aggregate that combines many rows. PostGIS implements the aggregate via a cascaded union algorithm that is dramatically faster than pairwise union on large sets.
1ST_Union(geometry g1, geometry g2) → geometry
2ST_Union(geometry g1, geometry g2, float gridSize) → geometry
3ST_Union(geometry set g1field) → geometry
4ST_Union(geometry[] g1_array) → geometryThe optional gridSize (PostGIS 3.1+) snaps inputs to a precision grid for robust overlay.
When would you use ST_Union?
Use ST_Union whenever you need to dissolve many features into one: merging parcels by owner, dissolving census tracts by region, combining storm buffers into a single impacted area, or aggregating service polygons by provider. It is the standard "dissolve" operation in spatial analysis. Combined with GROUP BY, it produces one union geometry per category.
1-- Dissolve parcels by landuse
2SELECT landuse, ST_Union(geom) AS geom
3FROM parcels
4GROUP BY landuse;FAQs
Is the aggregate form fast?
Yes. PostGIS uses cascaded union, which builds the result in a balanced way rather than unioning one feature at a time. It scales well to hundreds of thousands of features. For memory-constrained environments use ST_MemUnion instead — slower but lighter on RAM.
How does it differ from ST_Collect?
ST_Collect just wraps geometries into a MULTI* collection without merging overlaps — very fast but leaves duplicated area. ST_Union dissolves overlaps into a single clean geometry.
What is the gridSize parameter for?
It snaps inputs to a fixed precision grid before overlay, which improves robustness against tiny vertex mismatches (slivers). Typical value for metre-based data is 0.001. Available from PostGIS 3.1.
Can I union geographies?
Not directly — ST_Union accepts geometry only. For lat/lon data either reproject to a projected CRS first, or cast to geometry, union, then cast back with ::geography.