ST_BoundingDiagonal
What is ST_BoundingDiagonal?
ST_BoundingDiagonal is a PostGIS function that returns the diagonal of a geometry's bounding box as a two-point LINESTRING, running from the minimum corner to the maximum corner.
ST_BoundingDiagonal(geometry g, boolean fits = false) → geometryWhen fits is false (default), the cached bounding box is used (fast). When true, the diagonal is computed from actual vertex coordinates, giving a tighter but slower result. Dimensionality (Z, M) of the input is preserved — for a 3D geometry the diagonal is a 3D linestring.
When would you use ST_BoundingDiagonal?
Use ST_BoundingDiagonal when you need the bounding-box extents as a geometry for further calculation — computing diagonal length as a size heuristic, extracting min/max corners via ST_StartPoint/ST_EndPoint, or feeding the result into envelope-based operators:
1SELECT id,
2 ST_Length(ST_BoundingDiagonal(geom)) AS bbox_diagonal_m
3FROM parcels
4WHERE ST_IsValid(geom);For 3D data it is one of the few built-in ways to get the full x-y-z extent as a single geometry.
FAQs
What is the difference from ST_Envelope?
ST_Envelope returns the full bounding-box polygon (five vertices); ST_BoundingDiagonal returns just the diagonal linestring (two vertices). Use the diagonal when all you need is the corners or the diagonal length.
When should I set fits = true?
Set fits = true only if you need an exactly-tight diagonal and suspect the cached bounding box may have been inflated by prior transformations. For most analytic workloads the default is fine and much faster.
Does ST_BoundingDiagonal work in 3D?
Yes. For Z-aware geometries the returned linestring is 3D with minZ at the start and maxZ at the end.
What does it return for an empty geometry?
An empty LINESTRING in the same SRID. Test with ST_IsEmpty before downstream use.