Collects geometry objects into a MULTI\* or GeometryCollection, depending on whether the input geometries share the same type (homogeneous) or have different types (heterogeneous).
Syntax
geometry ST_Collect(geometry g1, geometry g2);
geometry ST_Collect(geometry[] g1Array);
geometry ST_Collect(geometry set g1Field);Parameters
| Parameter | Description |
|---|---|
g1 | The first geometry object. |
g2 | The second geometry object. |
g1Array | An array of geometry objects. |
g1Field | A field of geometry objects from a dataset, used when calling ST_Collect as an aggregate function. |
Usage notes
ST_Collect has three variants:
Variant 1 — two-input (
g1,g2): Combines two geometry objects. Returns a MULTI\* type if both inputs share the same geometry type, or a GeometryCollection if the types differ.Variant 2 — array (
g1Array): Combines an array of geometry objects into a single collection.Variant 3 — aggregate (
g1Field): Acts as an aggregate function, collecting all geometry objects from a rowset into a single geometry.
ST_Collect supports CircularString and Curve objects, but may not return the expected MultiCurve or MULTI\* type in all cases.
If any input is itself a collection type (MULTI\* or GeometryCollection),ST_Collectreturns a GeometryCollection rather than a flat MULTI\* type. To prevent this, useST_Dumpin a subquery to expand the input collections to their atomic elements before callingST_Collect.
ST_Collect vs. ST_Union
Both functions combine geometry objects, but behave differently:
| Behavior | ST_Collect | ST_Union |
|---|---|---|
| Dissolves boundaries | No | Yes |
| LineString handling | Returns a MultiLineString | Splits at node intersections |
| Return type | MULTI\* or GeometryCollection | May return a simple geometry |
| Performance | Faster | Slower |
Use ST_Collect to group geometries without merging their boundaries. Use ST_Union when you need the geometries dissolved into a single unified geometry.
Examples
Variant 1: Two-input
Collect two points of the same type — returns a MULTIPOINT:
SELECT ST_AsText(ST_Collect('POINT(0 0)'::geometry, 'POINT(0 1)'::geometry));Result:
st_astext
---------------------
MULTIPOINT(0 0,0 1)
(1 row)Collect geometries of different types — returns a GeometryCollection:
SELECT ST_AsText(ST_Collect('POINT(0 0)'::geometry, 'LINESTRING(0 2,0 3)'::geometry));Result:
st_astext
----------------------------------------------------
GEOMETRYCOLLECTION(POINT(0 0),LINESTRING(0 2,0 3))
(1 row)Variant 3: Aggregate
Collect all points from a subquery into a single MULTIPOINT:
SELECT ST_AsText(ST_Collect(t.geom))
FROM (
SELECT (ST_DumpPoints(st_buffer('POINT(0 0)'::geometry, 1, 'quad_segs=2'))).geom AS geom
) AS t;Result:
st_astext
----------------------------------------------------------------
MULTIPOINT(1 0,0.707106781186548 -0.707106781186547,0 -1,-0.70
7106781186546 -0.707106781186549,-1 0,-0.70710678118655 0.70710
6781186545,0 1,0.707106781186544 0.707106781186551,1 0)
(1 row)