ST_MaxDistance
What is ST_MaxDistance?
ST_MaxDistance returns the 2D maximum distance between two geometries — the length of the longest possible line connecting a point on g1 to a point on g2. It is the numeric counterpart of ST_LongestLine.
ST_MaxDistance(geometry g1, geometry g2) → floatThe result is in the units of the input CRS. If called with the same geometry for both arguments, the result is the geometry's diameter — the largest distance between any two of its points.
When would you use ST_MaxDistance?
Use ST_MaxDistance to measure worst-case extent or separation: the diameter of a lake polygon for symbology, the maximum span between two zones in a spacing audit, or the farthest-point distance for a bounding heuristic. It is also used in generalisation QA to flag overly long polygons that should be split.
1-- Polygons whose diameter exceeds 10 km
2SELECT id, ST_MaxDistance(geom, geom) AS diameter_m
3FROM parcels
4WHERE ST_MaxDistance(geom, geom) > 10000;FAQs
What units does it return?
The units of the input CRS — degrees for EPSG:4326, metres for projected CRSs. There is no geography overload; reproject first for real-world metres on lat/lon data.
Is there a 3D version?
Yes — ST_3DMaxDistance uses Z coordinates. For 3D worst-case spans, prefer it over ST_MaxDistance.
Is it symmetric?
Yes. ST_MaxDistance(g1, g2) = ST_MaxDistance(g2, g1).
How does it differ from ST_LongestLine?
ST_LongestLine returns the LINESTRING geometry; ST_MaxDistance returns its length. Use ST_MaxDistance when you only need the scalar and want to avoid materialising a geometry.