PostGIS & Spatial SQL
Purpose: correct-and-fast spatial SQL. The two recurring failure modes are
semantic (geometry vs geography, SRID mismatches → wrong answers) and
performance (missing index usage → hour-long joins); this skill guards
both.
When the database is the right tool
Move from files/GeoPandas to PostGIS when any of: features > a few
million, concurrent readers/writers, repeated ad-hoc querying, a serving
API on top, or transactional integrity needs. For single-shot analytical
scans over GeoParquet, DuckDB Spatial is often the fastest
zero-install path — same SQL mindset, no server.
When requirements are incomplete, do not turn this heuristic into a final
recommendation. First obtain current and forecast data volume, concurrency,
delivery and mutation pattern, latency/SLA, serving needs, and operational
ownership (including backup and recovery). Define representative ingestion,
join, and read queries for both viable backends; compare runtime and resource
use only after row counts, join cardinality, SRID, geometry validity, and sample
outputs agree. Include this benchmark and correctness plan in the current
response; do not merely offer to draft it later.
Schema fundamentals
This runnable example assumes the data is contained in UTM zone 33N. Replace
EPSG:32633 with a projected CRS verified for the actual area of interest.
CREATE TABLE parcels (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
parcel_no text NOT NULL,
landuse text,
area_m2 double precision, -- unit in the name, always
geom geometry(MultiPolygon, 32633) NOT NULL
);
CREATE INDEX parcels_geom_gix ON parcels USING gist (geom);
ANALYZE parcels;
Type the geometry column fully: geometry(MultiPolygon, SRID) — an
untyped geometry column happily accepts mixed garbage.
Promote to Multi* on load (ST_Multi) so Polygon/MultiPolygon mixing
never bites.
geometry vs geography: geometry in a projected SRID for regional
analysis (fast, full function set); geography (SRID 4326) when the
extent is global/cross-zone and you want meters without picking a
projection (slower, smaller function set). Never store in 4326 geometry
and call ST_Area expecting m² — that's square degrees.
Never use EPSG:3857/Web Mercator for area or length measurement. When the
analysis CRS is not yet known, either use 4326 geography for a geodesic
result or stop and select a verified local/equal-area CRS; do not present a
known-distorting CRS as a runnable measurement alternative.
Any stored geometry column you recommend must be typed with its SRID.
Advising a "second projected geometry column" for repeated measurement is
incomplete until it is written as geometry(<Type>, <SRID>) with the index
and the populating ST_Transform. An untyped column recommended as a fix
reintroduces the mixed-SRID problem it was meant to solve:
ALTER TABLE parcels ADD COLUMN geom_32633 geometry(MultiPolygon, 32633);
UPDATE parcels SET geom_32633 = ST_Transform(geom, 32633);
CREATE INDEX parcels_geom_32633_gix ON parcels USING gist (geom_32633);
GiST index on every geometry column, ANALYZE after bulk loads; BRIN
only for huge, spatially-ordered, append-only tables.
Load paths: ogr2ogr -f PostgreSQL, shp2pgsql, or GeoPandas
to_postgis (small/medium). COPY beats INSERT by orders of magnitude.
Correct spatial predicates
ST_Intersects for "touches at all", ST_Contains/ST_Within for
containment, ST_DWithin(a, b, dist) for proximity — never
ST_Distance(a,b) < dist (that form can't use the index).
- The classic point-in-polygon join:
SELECT p.id, a.district
FROM points p
JOIN admin a ON ST_Intersects(a.geom, p.geom); -- GiST on both sides
- KNN nearest-neighbor with the distance operator (index-assisted):
SELECT h.id, h.name
FROM hospitals h
ORDER BY h.geom <-> (SELECT geom FROM incident WHERE id = 42)
LIMIT 3;
<-> gives true-distance ordering on modern PostGIS for geometry; wrap
with ST_DWithin to bound the search when tables are huge.
Performance playbook
EXPLAIN (ANALYZE, BUFFERS) first — confirm the GiST index is used
(look for "Index Scan ... _gix"); a Seq Scan on a big spatial join
means a rewrite, not a bigger server.
- Same SRID on both sides of every predicate —
ST_Transform inside a
join predicate kills index use; store a transformed, indexed copy
instead.
- Big-polygon problem: country/basin-sized geometries make index bboxes
useless →
ST_Subdivide into a work table (typical 10-100× speedup on
joins against them).
The following example assumes countries(country_id, geom).
CREATE TABLE country_parts AS
SELECT c.country_id, part.geom
FROM countries AS c
CROSS JOIN LATERAL ST_Subdivide(c.geom, 256) AS part(geom);
CREATE INDEX country_parts_geom_gix ON country_parts USING gist (geom);
ANALYZE country_parts;
ST_Subdivide is a set-returning function; do not access its result as
(ST_Subdivide(...)).geom.
- Validity in-database:
ST_IsValid audit, ST_MakeValid repair, add a
CHECK (ST_IsValid(geom)) if writers are untrusted.
- Simplify for serving, not for analysis: keep full-resolution geometry;
generate
ST_SimplifyPreserveTopology copies or vector tiles
(ST_AsMVT) for the web tier.
- Batch updates in transactions;
VACUUM ANALYZE after churn.
Common analytical patterns
-- Area-weighted aggregation (e.g., population into custom zones)
SELECT z.zone_id,
SUM(b.pop * ST_Area(ST_Intersection(z.geom, b.geom)) / ST_Area(b.geom)) AS pop_est
FROM zones z JOIN blocks b ON ST_Intersects(z.geom, b.geom)
GROUP BY z.zone_id;
-- Dissolve with attribute
SELECT landuse, ST_Multi(ST_Union(geom))::geometry(MultiPolygon, 32633) AS geom
FROM parcels GROUP BY landuse;
Area-weighted interpolation assumes uniform density within source units —
state that assumption when reporting. Validity repair is ST_MakeValid,
never ST_Buffer(geom, 0).
DuckDB Spatial quick path
INSTALL spatial; LOAD spatial;
SELECT a.name, count(*)
FROM 'admin.parquet' a, 'points.parquet' p
WHERE ST_Intersects(a.geom, p.geom)
GROUP BY a.name;
Reads GeoParquet/Shapefile/GPKG directly, parallel by default — ideal for
one-off large joins and pipeline steps without a server. No GiST; it plans
its own joins — benchmark, don't assume.
Verification protocol
- Row-count accounting query after each join/overlay CTE.
SELECT DISTINCT ST_SRID(geom), GeometryType(geom) on every table
touched — one query kills two classic bug families.
- Sample 5 output features rendered over a basemap (QGIS connects
directly) — numbers can pass while geometries are garbage.
- Treat every
sql fence presented as runnable as a syntax and alias
boundary: it must execute top-to-bottom after stated schema assumptions.
Never put angle-bracket placeholders, ellipses, pseudocode, abandoned joins,
or incomplete aliases inside it. If a schema value such as an SRID is
unknown, ask for it or keep the template in a labeled text block.
Pitfalls checklist
ST_Area/ST_Length on 4326 geometry (square degrees).
- EPSG:3857/Web Mercator for area or length measurement (systematic distortion).
ST_Distance < x instead of ST_DWithin (no index).
ST_Transform in join predicates.
- Untyped geometry columns with mixed SRIDs.
- Country-sized polygons joined without
ST_Subdivide.
buffer(0) as validity repair (silent part loss) — ST_MakeValid.
- Serving full-resolution geometries to web clients.
Execution contract
- Workflow: inspect schema, SRID, geometry type, size, and query goal; choose predicates and indexes; write auditable CTEs; inspect the plan; reconcile results; operationalize safely.
- Decision rules: use PostGIS for concurrent, repeated, or transactional spatial workloads; use file pipelines or DuckDB Spatial for bounded one-off transformations when a server adds no value.
- Verification protocol: assert SRID and geometry invariants, account for rows at each join, compare indexed plans and timings, sample geometries on a map, and test boundary semantics.
- Failure modes: block release for mixed SRIDs, accidental many-to-many explosion, invalid geometries, non-indexable predicates, geography/geometry unit confusion, or unexplained plan regressions.
- Deliverables: self-contained parameterized SQL or migration with consistent CTE/table aliases, indexes and rationale, query plan evidence, row accounting, sample validation, expected schema, performance notes, and rollback guidance.
- Source freshness: consult the authoritative source registry for the deployed database and extension versions before selecting functions or plans.
1---2name: postgis-spatial-sql3description: Invoke whenever spatial SQL or its execution backend is the decision: PostGIS, DuckDB Spatial, SpatiaLite, ST_* functions, recurring spatial joins, concurrent/growing workloads, or large GeoParquet queries. Covers backend selection, schemas, GiST/BRIN indexes, KNN, geometry versus geography, correctness benchmarks, and EXPLAIN optimization. Use PostGIS for managed concurrent services and embedded engines for bounded local analytics when evidence supports that choice. Use geo-data-engineering for acquisition, conversion, and file-based ETL without spatial SQL.4license: MIT5---67# PostGIS & Spatial SQL89Purpose: correct-and-fast spatial SQL. The two recurring failure modes are10semantic (geometry vs geography, SRID mismatches → wrong answers) and11performance (missing index usage → hour-long joins); this skill guards12both.1314## When the database is the right tool1516Move from files/GeoPandas to PostGIS when any of: features > a few17million, concurrent readers/writers, repeated ad-hoc querying, a serving18API on top, or transactional integrity needs. For single-shot analytical19scans over GeoParquet, **DuckDB Spatial** is often the fastest20zero-install path — same SQL mindset, no server.2122When requirements are incomplete, do not turn this heuristic into a final23recommendation. First obtain current and forecast data volume, concurrency,24delivery and mutation pattern, latency/SLA, serving needs, and operational25ownership (including backup and recovery). Define representative ingestion,26join, and read queries for both viable backends; compare runtime and resource27use only after row counts, join cardinality, SRID, geometry validity, and sample28outputs agree. Include this benchmark and correctness plan in the current29response; do not merely offer to draft it later.3031## Schema fundamentals3233This runnable example assumes the data is contained in UTM zone 33N. Replace34EPSG:32633 with a projected CRS verified for the actual area of interest.3536```sql37CREATE TABLE parcels (38 id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,39 parcel_no text NOT NULL,40 landuse text,41 area_m2 double precision, -- unit in the name, always42 geom geometry(MultiPolygon, 32633) NOT NULL43);44CREATE INDEX parcels_geom_gix ON parcels USING gist (geom);45ANALYZE parcels;46```4748- **Type the geometry column fully**: `geometry(MultiPolygon, SRID)` — an49 untyped `geometry` column happily accepts mixed garbage.50- Promote to Multi* on load (`ST_Multi`) so Polygon/MultiPolygon mixing51 never bites.52- **geometry vs geography**: geometry in a projected SRID for regional53 analysis (fast, full function set); geography (SRID 4326) when the54 extent is global/cross-zone and you want meters without picking a55 projection (slower, smaller function set). Never store in 4326 geometry56 and call `ST_Area` expecting m² — that's square degrees.57- Never use EPSG:3857/Web Mercator for area or length measurement. When the58 analysis CRS is not yet known, either use 4326 geography for a geodesic59 result or stop and select a verified local/equal-area CRS; do not present a60 known-distorting CRS as a runnable measurement alternative.61- **Any stored geometry column you recommend must be typed with its SRID.**62 Advising a "second projected geometry column" for repeated measurement is63 incomplete until it is written as `geometry(<Type>, <SRID>)` with the index64 and the populating `ST_Transform`. An untyped column recommended as a fix65 reintroduces the mixed-SRID problem it was meant to solve:6667 ```sql68 ALTER TABLE parcels ADD COLUMN geom_32633 geometry(MultiPolygon, 32633);69 UPDATE parcels SET geom_32633 = ST_Transform(geom, 32633);70 CREATE INDEX parcels_geom_32633_gix ON parcels USING gist (geom_32633);71 ```72- GiST index on every geometry column, `ANALYZE` after bulk loads; BRIN73 only for huge, spatially-ordered, append-only tables.74- Load paths: `ogr2ogr -f PostgreSQL`, `shp2pgsql`, or GeoPandas75 `to_postgis` (small/medium). `COPY` beats INSERT by orders of magnitude.7677## Correct spatial predicates7879- `ST_Intersects` for "touches at all", `ST_Contains`/`ST_Within` for80 containment, `ST_DWithin(a, b, dist)` for proximity — **never**81 `ST_Distance(a,b) < dist` (that form can't use the index).82- The classic point-in-polygon join:8384```sql85SELECT p.id, a.district86FROM points p87JOIN admin a ON ST_Intersects(a.geom, p.geom); -- GiST on both sides88```8990- KNN nearest-neighbor with the distance operator (index-assisted):9192```sql93SELECT h.id, h.name94FROM hospitals h95ORDER BY h.geom <-> (SELECT geom FROM incident WHERE id = 42)96LIMIT 3;97```9899`<->` gives true-distance ordering on modern PostGIS for geometry; wrap100with `ST_DWithin` to bound the search when tables are huge.101102## Performance playbook1031041. `EXPLAIN (ANALYZE, BUFFERS)` first — confirm the GiST index is used105 (look for "Index Scan ... _gix"); a Seq Scan on a big spatial join106 means a rewrite, not a bigger server.1072. Same SRID on both sides of every predicate — `ST_Transform` inside a108 join predicate kills index use; store a transformed, indexed copy109 instead.1103. Big-polygon problem: country/basin-sized geometries make index bboxes111 useless → `ST_Subdivide` into a work table (typical 10-100× speedup on112 joins against them).113114The following example assumes `countries(country_id, geom)`.115116```sql117CREATE TABLE country_parts AS118SELECT c.country_id, part.geom119FROM countries AS c120CROSS JOIN LATERAL ST_Subdivide(c.geom, 256) AS part(geom);121122CREATE INDEX country_parts_geom_gix ON country_parts USING gist (geom);123ANALYZE country_parts;124```125126`ST_Subdivide` is a set-returning function; do not access its result as127`(ST_Subdivide(...)).geom`.1281294. Validity in-database: `ST_IsValid` audit, `ST_MakeValid` repair, add a130 `CHECK (ST_IsValid(geom))` if writers are untrusted.1315. Simplify for serving, not for analysis: keep full-resolution geometry;132 generate `ST_SimplifyPreserveTopology` copies or vector tiles133 (`ST_AsMVT`) for the web tier.1346. Batch updates in transactions; `VACUUM ANALYZE` after churn.135136## Common analytical patterns137138```sql139-- Area-weighted aggregation (e.g., population into custom zones)140SELECT z.zone_id,141 SUM(b.pop * ST_Area(ST_Intersection(z.geom, b.geom)) / ST_Area(b.geom)) AS pop_est142FROM zones z JOIN blocks b ON ST_Intersects(z.geom, b.geom)143GROUP BY z.zone_id;144145-- Dissolve with attribute146SELECT landuse, ST_Multi(ST_Union(geom))::geometry(MultiPolygon, 32633) AS geom147FROM parcels GROUP BY landuse;148```149150Area-weighted interpolation assumes uniform density within source units —151state that assumption when reporting. Validity repair is `ST_MakeValid`,152never `ST_Buffer(geom, 0)`.153154## DuckDB Spatial quick path155156```sql157INSTALL spatial; LOAD spatial;158SELECT a.name, count(*)159FROM 'admin.parquet' a, 'points.parquet' p160WHERE ST_Intersects(a.geom, p.geom)161GROUP BY a.name;162```163164Reads GeoParquet/Shapefile/GPKG directly, parallel by default — ideal for165one-off large joins and pipeline steps without a server. No GiST; it plans166its own joins — benchmark, don't assume.167168## Verification protocol1691701. Row-count accounting query after each join/overlay CTE.1712. `SELECT DISTINCT ST_SRID(geom), GeometryType(geom)` on every table172 touched — one query kills two classic bug families.1733. Sample 5 output features rendered over a basemap (QGIS connects174 directly) — numbers can pass while geometries are garbage.1754. Treat every `sql` fence presented as runnable as a syntax and alias176 boundary: it must execute top-to-bottom after stated schema assumptions.177 Never put angle-bracket placeholders, ellipses, pseudocode, abandoned joins,178 or incomplete aliases inside it. If a schema value such as an SRID is179 unknown, ask for it or keep the template in a labeled `text` block.180181## Pitfalls checklist182183- `ST_Area`/`ST_Length` on 4326 geometry (square degrees).184- EPSG:3857/Web Mercator for area or length measurement (systematic distortion).185- `ST_Distance < x` instead of `ST_DWithin` (no index).186- `ST_Transform` in join predicates.187- Untyped geometry columns with mixed SRIDs.188- Country-sized polygons joined without `ST_Subdivide`.189- `buffer(0)` as validity repair (silent part loss) — `ST_MakeValid`.190- Serving full-resolution geometries to web clients.191192## Execution contract193194- **Workflow:** inspect schema, SRID, geometry type, size, and query goal; choose predicates and indexes; write auditable CTEs; inspect the plan; reconcile results; operationalize safely.195- **Decision rules:** use PostGIS for concurrent, repeated, or transactional spatial workloads; use file pipelines or DuckDB Spatial for bounded one-off transformations when a server adds no value.196- **Verification protocol:** assert SRID and geometry invariants, account for rows at each join, compare indexed plans and timings, sample geometries on a map, and test boundary semantics.197- **Failure modes:** block release for mixed SRIDs, accidental many-to-many explosion, invalid geometries, non-indexable predicates, geography/geometry unit confusion, or unexplained plan regressions.198- **Deliverables:** self-contained parameterized SQL or migration with consistent CTE/table aliases, indexes and rationale, query plan evidence, row accounting, sample validation, expected schema, performance notes, and rollback guidance.199- **Source freshness:** consult [the authoritative source registry](references/authoritative-sources.md) for the deployed database and extension versions before selecting functions or plans.