PostGISOverlay

ST_MemUnion

What is ST_MemUnion?

ST_MemUnion is an aggregate function that unions a set of geometries one at a time, keeping memory usage low at the cost of slower execution compared with ST_Union. It is useful when unioning very large result sets that would otherwise exhaust memory in the default cascaded-union approach.

SQL
ST_MemUnion(geometry set g1field)geometry

Functionally it produces the same output as ST_Union, but iteratively rather than building a large intermediate collection.

When would you use ST_MemUnion?

Use ST_MemUnion when unioning millions of small polygons or lines that would otherwise blow up PostgreSQL's work_mem — merging a country's worth of parcel polygons, dissolving raster-to-vector output, or combining footprint datasets in a memory-constrained environment. The trade-off is runtime: ST_MemUnion is noticeably slower than ST_Union on small-to-medium sets because it cannot use cascaded-union optimisations.

SQL
1-- Dissolve parcels by landuse with low memory footprint
2SELECT landuse, ST_MemUnion(geom) AS geom
3FROM parcels
4GROUP BY landuse;

FAQs

When should I use ST_MemUnion vs ST_Union?

Use ST_Union by default — it is much faster thanks to cascaded union. Switch to ST_MemUnion only when you run out of memory with ST_Union or when you need to union an extremely large set in a memory-constrained environment.

Does it produce the same output as ST_Union?

Yes, the geometric result is the same. Only the memory/runtime trade-off differs.

Is it parallelisable?

It is an aggregate like other aggregates and can be parallelised by the planner. The inner implementation is sequential per worker; each worker unions its own partition before the final combine.

What units / CRS does it preserve?

The output CRS matches the inputs (which must all share the same SRID). Units are those of the input CRS; ST_MemUnion does no reprojection.