PostGIS orientation
Overview
PostGIS extends PostgreSQL with geometry, geography, spatial functions and spatial indexes. The local SQLite R*Tree comparison teaches a candidate-and-refine workflow using bounding boxes; it does not implement PostGIS. Create extensions only in authorized disposable environments and check installed versions.
First example
SELECT id FROM sr_box WHERE xmin<=5 AND xmax>=-5 AND ymin<=5 AND ymax>=-5 ORDER BY id;Observed SQLite result (rows in JSON):
[ [ 1 ], [ 2 ], [ 3 ], [ 5 ]]How it works
Use a bounding box to collect candidate points, then evaluate the exact radius test against original coordinates. Boxes may include false positives. The fixture uses rtree_i32 with integer coordinates and explicitly populated static points; a production index must be kept in sync with base rows. For PostGIS, ST_DWithin can exploit a spatial index, ST_Covers includes boundary points, and GiST is a common access method. Geometry validity and correct SRIDs matter. Do not equate candidate counts with latency.
Practical example
SELECT p.id FROM sr_box b JOIN sr_point p ON p.id=b.id WHERE b.xmin<=5 AND b.xmax>=-5 AND b.ymin<=5 AND b.ymax>=-5 AND p.x*p.x+p.y*p.y<=25 ORDER BY p.id;Observed SQLite result (rows in JSON):
[ [ 1 ], [ 2 ], [ 5 ]]Practice and mistakes
Practice: refine the R*Tree candidates with radius 3. Hint 1: use bounding limits -3 and 3. Hint 2: squared distance <=9. Only point 1 matches. For production, inspect query plans and benchmark with realistic data.
PostgreSQL 17 example — NOT RUN
-- Requires PostGIS 3.5.x.WITH pts(id,g) AS (VALUES (1,ST_MakePoint(0,0)),(2,ST_MakePoint(3,4)),(3,ST_MakePoint(4,4)))SELECT id FROM pts WHERE ST_DWithin(g,ST_MakePoint(0,0),5) ORDER BY id;Expected result, not executed: [[1], [2]]