ST_MemUnion

更新时间:
复制 MD 格式

An aggregate function that unions a set of geometry objects in a memory-efficient but slower way. ST_MemUnion produces the same result as ST_Union, but uses less memory at the cost of more processing time.

Syntax

geometry ST_MemUnion(geometry set geomfield);

Parameters

ParameterDescription
geomfieldThe geometry field in the input geometry set.

When to use ST_MemUnion

ST_MemUnion unions geometries incrementally rather than accumulating them into an array first. This makes it less memory-intensive than ST_Union, but slower — the incremental approach requires more processing passes.

Use ST_MemUnion when the dataset is large enough that ST_Union exhausts available memory. For smaller datasets where performance matters more than memory usage, use ST_Union instead.

Examples

Aggregate geometries by group

The following example merges all geometries in a table into a single geometry per group. This is the typical use case for ST_MemUnion — dissolving boundaries across rows that share a common ID.

SELECT id, ST_MemUnion(geom) AS merged_geom
FROM spatial_table
GROUP BY id;

Self-contained example with inline data

The following example constructs two adjacent polygons inline and returns their union.

SELECT ST_AsText(ST_MemUnion(g))
FROM (
    SELECT unnest(ARRAY[
        'POLYGON((0 0,1 0,1 2,0 2,0 0))'::geometry,
        'POLYGON((1 0,3 0,3 1,1 1,1 0))'::geometry
    ]) AS g
) AS t;

Expected output:

                st_astext
--------------------------------------------
 POLYGON((1 0,0 0,0 2,1 2,1 1,3 1,3 0,1 0))
(1 row)

See also

  • ST_Union — the faster, higher-memory alternative for geometry union aggregation