ADR-030: Geographic fields: PostGIS, GeoJSON on the wire¶
Status: Accepted — design in the geo fields spec. Date: 2026-10-10
Builds on ADR-014 (group gating), ADR-025 (public scope) and ADR-015 (search filters).
Context¶
Records need places, and the database should answer the geographic questions: 1. sort / filter a list by distance ("contacts within 10 km, nearest first"); 2. the distance between two records (attendee to event); 3. the website's "events nearest to me"; 4. zones: "contacts inside our service zone", "which companies serve this point".
Zones rule out plain lat/lon columns or earthdistance; a framework feature must also stay declarative (a struct tag), tenant-safe and usable by any module.
Decision¶
- PostGIS,
geography(…, 4326). Distances are in meters on the globe with no projection choice for callers. The DB image ispostgis/postgis:18-3.7(dev, prod, CI); boot runsCREATE EXTENSION IF NOT EXISTS postgisunder the schema advisory lock, before any table DDL (boot,EnsureSchema,MigrateModules, and so dbmanage create/switch). After an image upgrade, the same pass runspostgis_extensions_upgrade()when an installed postgis extension lags the server's version, so existing databases follow the image without a manual step. - Two ORM types,
orm.GeoPoint(geography(Point,4326)) andorm.GeoShape(geography(Geometry,4326): Polygon, MultiPolygon, LineString). Declared as nullable pointers:Location *orm.GeoPoint `db:"geo_location,index=gist"`. - Text I/O, not a pgx codec. Values are read as EWKB hex and written as EWKT (
SRID=4326;POINT(lon lat)) throughScanner/Valuer. A codec would have to be registered on the pool's type map, which needs the extension to exist before the pool connects and re-registration after a dbmanage hot-swap. Text needs neither and no SQL rewriting. - GeoJSON geometry on the wire (what Leaflet and Nominatim speak). Generic CRUD recognises a geo column by its Go type, converts on read, validates and converts on write. Invalid input is a 422
VALIDATION_ERRORwith the field infields. - List params on
GET /api/v1/{table}:near[col]=lon,lat(nearest first, rows carry a read-only_distance_m),within[col]=lon,lat,meters(radius),covers[col]=lon,lat(zones containing a point),inside[col]=<table>:<id>:<shape col>(points inside another record's zone). Malformed values are 400; an unusable reference is 404. - References are authorized in the ORM.
insideand the distance endpoint resolvetable:id:colonly if the table is registered and notExcluded, its module is active (orm.SetTableActiveCheck), the caller can read it (access.CanRead, stamped by the permission middleware as<table>:<table>:read;core/ormnever importsauth), the column has the right geo kind and is not group-gated for the caller. Tenant and soft-delete scoping sit inside the subquery, so a missing or foreign record matches nothing and leaks nothing. References are refused (400) on public routes;near/within/coverswork there on published columns. GET /api/v1/geo/distance?from=&to=(internal/geo, permissiongeo:distance:read) returns{"meters": n|null}for point/zone pairs, via the same reference resolution.- Frontend: Leaflet with OSM tiles and a minimal own polygon editor (no API key);
geofield type (point,shapewidgets), computeddistancewidget, search-bar Near / Inside / Covers filters. The user's unit system (units.system, in/me/preferencesasunit_system) picks km or miles.
sequenceDiagram
participant C as Client
participant H as CRUD handler
participant P as Permission middleware
participant R as crud (resolveGeoRef)
participant DB as PostgreSQL
C->>H: GET /api/v1/contact?inside[geo_location]=company:ID:service_zone
H->>P: route contact:contact:read
P->>P: stamp access.CanRead (lazy, memoized)
P->>R: list with the inside param
R->>R: company registered, module active, readable, shape column, not gated
R->>DB: WHERE ST_Covers((SELECT service_zone FROM company z WHERE id=$1 AND tenant=$2 AND deleted_at IS NULL), geo_location)
DB-->>C: rows (an unknown id matches nothing) Consequences¶
- Image switch: the only deployment step; the data directory format is unchanged. On managed Postgres,
CREATE EXTENSIONneeds a privileged role (or the extension pre-created). - OSM tile policy: the default tile server is for light use; a heavy deployment must point at its own tile provider.
- Group gating, the public scope and tenant pinning apply to geo columns and params unchanged. The Redis read cache (ADR-023) serves
near/within/coversreads like any other (keys include the SQL and args), but never caches a read withinside: its result depends on the referenced record's table, whose writes only bump that table's generation, so a cached entry would outlive an edited zone.nearorders by distance then primary key, so equal distances (unlocated rows) page stably. - Not in v1: routing/travel time, drawing lines (stored and shown only), marker clustering, reverse geocoding.
Pitfalls¶
- GeoJSON and EWKT coordinates are
[lon, lat], the reverse of the habitual "lat, lon". - A point exactly on a zone's boundary is covered (
ST_Covers, notST_Contains). _distance_mis computed: never writable, absent withoutnear; at most onenearper request;?distinct=and?aggregate=ignore it.- A point column without
index=gistmakesnear/withinscan the table. insideis refused on/api/v1/public, as it would reference arbitrary records.- A shape with fewer than 3 vertices in the editor means "no zone" (
null). - Tree grids never render geo columns; keep geo fields in list descriptors for the search bar.
- Production: the extension is created at boot, but a role without the right cannot do it.