pg_s2
pg_s2 · Adds an S2 CellID type, hierarchy and covering functions, distance helpers, casts, operators, and a B-tree operator class.
Overview
PostgreSQL · 14, 15, 16, 17
Usage
Sources:
- Upstream README and API examples
- Extension control file
- Cargo package metadata
- Rust SQL implementation
- API specification
pg_s2 exposes a compact S2 CellID API using PostgreSQL built-in spatial types, without requiring PostGIS. Its order-preserving s2cellid type supports B-tree indexes, token and bigint conversion, hierarchy and neighbour traversal, cap/rectangle coverings, boundaries, and great-circle distance.
Create and inspect cells
PostgreSQL point values use longitude as X and latitude as Y:
For indexed spatial searches, store the cell, use s2_cover_cap_ranges() or s2_cover_rect_ranges() as a coarse B-tree prefilter, then apply an exact predicate such as s2_great_circle_distance(). A covering is deliberately an over-approximation and can include false positives. User-settable defaults include pg_s2.default_level, pg_s2.default_cover_level, and pg_s2.earth_radius_m.
Version 0.0.6 is described upstream as an early MVP/testing release. The README states PostgreSQL 14 through 17 support, while Cargo also exposes build features for 13 and 18; those extra features are not the same as a support guarantee. Invalid tokens, coordinates, or levels raise errors, and longitude/latitude order must be kept consistent throughout the application.