Repository navigation
Description
👟 Reproduction steps
- Table with a required
pointcolumnlocand 200,000 rows spread over a ~700 × 900 km area. - Optionally add a
spatialindex onloc(createsUSING GIST (loc)). GET /v1/tablesdb/{databaseId}/tables/{tableId}/rows?queries[]={"method":"distanceLessThan","attribute":"loc","values":[[[10.0,51.0],1000,true]]}&queries[]={"method":"limit","values":[25]}&total=0(two rows within 1 km).
👍 Expected behavior
With a spatial index, a radius query with few matches is answered from the index in a few milliseconds.
👎 Actual Behavior
Throughput with 20 concurrent clients over two API nodes (two runs each):
| Query | without spatial index | with spatial index |
|---|---|---|
distanceLessThan 1 km, meters: true (2 matches) |
15 / 13 req/s, p50 1.5–1.7 s | 11 / 13 req/s, p50 1.9–2.3 s |
distanceLessThan 20 km, meters: true (stops after 25 matches) |
226 / 195 req/s | 178 / 191 req/s |
distanceLessThan 1 km as degrees, meters: false |
– | 80 / 79 req/s, p50 ~250 ms |
| plain list of 25 rows on the same table (reference) | ~1,100 req/s | ~1,100 req/s |
The index makes no difference, and the query gets slower the fewer rows match. EXPLAIN (ANALYZE, BUFFERS) of the generated SQL with the spatial index in place:
Limit (actual rows=2)
-> Index Scan using _1_database_2_collection_125_pkey on _1_database_2_collection_125 main (actual rows=2)
Filter: (st_distance((loc)::geography, '0101000020E6…'::geography, true) < '1000'::double precision)
Rows Removed by Filter: 199998
Buffers: shared hit=21314
Execution Time: 292.763 ms
The GIST index on loc is never considered; every row's distance is computed (the implicit ORDER BY "_id" makes Postgres walk the primary key). Without meters the plan is the same (51.5 ms).
Cause: the Postgres adapter builds the filter as ST_Distance(...) < :distance (Adapter/Postgres.php L1464–L1496):
- with
meters: true:ST_Distance((col::geography), ST_SetSRID(…)::geography) < :d(L1490–L1492) - otherwise:
ST_Distance(col, ST_GeomFromText(…)) < :d(L1496)
ST_Distance(a, b) < d is not index-assisted in PostGIS; the index-aware form is ST_DWithin(a, b, d). In the meters variant the column is additionally cast to geography, which a GIST index on the geometry column (L910) cannot serve. main still builds the same expressions.
🎲 Appwrite version
2.3.0 (utopia-php/database 7.3.11)
💻 Operating system
Linux (Debian 12), Docker
🧱 Your Environment
Self-hosted Appwrite 2.3.0, Postgres adapter (PostgreSQL 18, PostGIS), two API nodes, Postgres on a separate node.
💡 Suggested fix
distanceLessThan/distanceLessThanEqual:ST_DWithin(col, geom, :d)(formeters:ST_DWithin(col::geography, geom::geography, :d)together with a functional GIST index on(col::geography), or keepgeometryand useST_DWithinon a geography index created by the spatial index type).distanceGreaterThan/distanceEqualcan keepST_Distance, they cannot use an index anyway.
👀 Have you spent some time to check if this issue has been raised before?
- I checked and didn't find a similar issue
🏢 Have you read the Code of Conduct?
- I have read the Code of Conduct
Activity
fix(databases): do not let the spatial prefilter drop matches
fix(databases): skip the meter prefilter unless both sides are points
fix(databases): take the distanceLessThan index fix from utopia-php/d…
test(databases): lock the distance SQL from utopia-php/database
fix(databases): do not let the spatial prefilter drop matches
fix(databases): skip the meter prefilter unless both sides are points
fix(databases): take the distanceLessThan index fix from utopia-php/d…
test(databases): lock the distance SQL from utopia-php/database
test(databases): assert distance rows and the spatial query plan
test(databases): explain only this collection's spatial plan
HarshMN2345 commentedon Oct 30, 2026
Fixed in utopia-php/database 8.3.14 (utopia-php/database#1001), which main now uses: distance queries use the spatial index. Thanks for reporting it!