All Products
Search
Document Center

ApsaraDB RDS:ST_DWithin

Last Updated:Mar 28, 2026

Returns true if two spatial objects are within a specified distance of each other, and false otherwise. Unlike ST_Distance, ST_DWithin automatically uses available spatial indexes, making it the standard choice for proximity queries.

Syntax

boolean ST_DWithin(geometry g1, geometry g2, double precision distanceOfSrid);
boolean ST_DWithin(geography gg1, geography gg2, double precision distanceMeters);
boolean ST_DWithin(geography gg1, geography gg2, double precision distanceMeters, boolean useSpheroid);

Parameters

ParameterTypeDescription
g1geometryThe first geometry object.
g2geometryThe second geometry object.
distanceOfSriddouble precisionThe threshold distance, in the unit defined by the spatial reference system (SRS) of the geometry objects.
gg1geographyThe first geography object.
gg2geographyThe second geography object.
distanceMetersdouble precisionThe threshold distance, in meters.
useSpheroidbooleanSpecifies whether to use an ellipsoid reference system. If you use an ellipsoid reference system, this function returns a more accurate result but at a slightly lower speed.

Usage notes

  • When both arguments are geometry, they must share the same spatial reference identifier (SRID).

  • When both arguments are geography, distance is measured in meters by default.

  • The function compares bounding boxes using all available spatial indexes, so proximity checks are index-accelerated by design.

  • For 3D proximity checks, use ST_3DDWithin instead.

Examples

Difference between ST_DWithin and ST_DFullyWithin

ST_DWithin returns true if the shortest distance between the two objects is within the threshold. ST_DFullyWithin is stricter: every point on both objects must be within the threshold.

SELECT ST_DFullyWithin(g1, g2, 2), ST_DWithin(g1, g2, 2)
FROM (
  SELECT 'LINESTRING(0 1,1 1)'::geometry AS g1,
         'LINESTRING(0 0,0 -1)'::geometry AS g2
) AS test;

Expected output:

 st_dfullywithin | st_dwithin
-----------------+------------
 f               | t
(1 row)

What's next

  • ST_DFullyWithin: checks whether all points on two objects are within a specified distance

  • ST_3DDWithin: proximity check for 3D objects in a projected coordinate system