Repository navigation
fix(postgres): let distanceLessThan use the spatial index - #1001
Merged
Merged
Conversation
ST_Distance(a, b) < d cannot be served by a GIST index, so radius queries filtered every row while walking the primary key. Without meters, distanceLessThan now leads with ST_DWithin on the geometry column. With meters on a point column and a point value, it leads with a degree box (&& ST_Expand) that holds every point within range on the WGS84 spheroid. Lines and polygons, and boxes that would reach a pole or the antimeridian, keep the exact geography comparison only. The exact ST_Distance check stays in every case, so the boundary remains exclusive.
Contributor
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 18 minutes. View limit detailsLimit details: You’ve used the included review currently available. Review configuration: ⚙️ Run configuration
📒 Files selected for processing (1)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
2 tasks done
ArnabChatterjee20k
approved these changes
Oct 7, 2026
This was referenced Oct 7, 2026
Closed
abnegate
added a commit
that referenced
this pull request
Oct 8, 2026
… into ws-api-w7 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
distanceLessThancompiled toST_Distance(...) < :distance. PostGIS cannot answer that from a GIST index, so a radius query filtered every row while walking the primary key, and got slower the fewer rows matched (appwrite/appwrite#14177).ST_DWithin(col, geom, :distance) AND ST_Distance(col, geom) < :distance.ST_DWithinuses the index;ST_Distancekeeps the boundary exclusive.col && ST_Expand(geom, :lon, :lat) AND <unchanged geography ST_Distance>. The box holds every point within range on the WGS84 spheroid: a degree of latitude spans at least 110,574 m, and a degree of longitude at least 111,319 m × cos of the most poleward latitude in range.distanceGreaterThan,distanceEqualanddistanceNotEqualare unchanged; they cannot use an index.Test Plan
200,000 points in the issue's layout (~700 × 900 km around
[10, 51]) on PostgreSQL 18 + PostGIS 3.6 with the spatial index,limit 25, average of 20 runs:Each query returns the same rows as before.
Results were also compared with the old SQL on 100,000 points packed near both poles and the antimeridian. 800 random meter queries (10 m to 1,000 km, half of them centered near a pole or the antimeridian) returned identical rows. Cases that drop rows under a naive degree radius also match: 1 m around
[0, 89.999], a point 20 km from the geodesic arc of[[-60, 60], [60, 60]], and a stored line within 20 km of a point.tests/e2e/Adapter/PostgresTest.phpandtests/e2e/Adapter/SharedTables/PostgresTest.phppass. No new test: which plan Postgres picks is not observable through the adapter API, and the existing spatial e2e tests cover the results.Related