Functions / PostGIS / ST_Extent
PostGISBounding Box

ST_Extent

What is ST_Extent?

ST_Extent is a PostGIS aggregate function that returns the 2D bounding box (box2d) covering every geometry in the input set. It is the exact, row-scanning counterpart to ST_EstimatedExtent.

SQL
ST_Extent(geometry set geoms) → box2d

It is a normal PostgreSQL aggregate, so it respects GROUP BY, WHERE, and can be used with windows.

When would you use ST_Extent?

Use ST_Extent to compute the exact extent of a layer or a filtered subset — for zoom-to-layer behaviour in mapping apps, for generating bounding-box metadata during ETL, or for producing one-row-per-group extents in thematic reports.

SQL
1SELECT country, ST_Extent(geom) AS bbox
2FROM cities
3GROUP BY country;

FAQs

How is ST_Extent different from ST_Envelope?

ST_Envelope(geom) is a per-row function that returns the bounding polygon of a single geometry. ST_Extent is an aggregate across many rows. Both describe rectangles, but ST_Extent returns the cheap box2d type while ST_Envelope returns a full geometry polygon.

What does ST_Extent return for an empty set?

If the input set is empty or all inputs are empty geometries, ST_Extent returns NULL. Protect reporting SQL with COALESCE or explicit null checks.

Is ST_Extent 3D-aware?

No — it returns a 2D box2d and ignores Z. Use ST_3DExtent when you need a 3D aggregate bounding box that includes Z min/max.

Can I cast the result to a geometry?

Yes. ST_Extent(geom)::geometry casts the returned box2d to a POLYGON geometry, which is useful when you need to store or render the bounding rectangle. Note that the resulting polygon is unsrided; set the SRID with ST_SetSRID if needed.