Skip to content

🐛 Bug Report: Distance queries never use the spatial index #14177

Closed

Description

@JoshiJoshiJoshi

👟 Reproduction steps

  1. Table with a required point column loc and 200,000 rows spread over a ~700 × 900 km area.
  2. Optionally add a spatial index on loc (creates USING GIST (loc)).
  3. 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) (for meters: ST_DWithin(col::geography, geom::geography, :d) together with a functional GIST index on (col::geography), or keep geometry and use ST_DWithin on a geography index created by the spatial index type).
  • distanceGreaterThan / distanceEqual can keep ST_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

  1. added
    product / databases
    product / functions
    sdk / cli
    on Oct 6, 2026
  2. added 2 commits that reference this issue on Oct 7, 2026
    0faf8e4
    c504cc3
  3. added a commit that references this issue on Oct 7, 2026
    adbad1e
  4. added 7 commits that reference this issue on Oct 7, 2026
    604268c
    f97aa38
    43e9a42
    0507214
    f94986f
    be44085
    9376e93
  5. HarshMN2345 commented on Oct 30, 2026

    @HarshMN2345
    Member

    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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    product / databasesproduct / functionssdk / cli

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions