pghex
pghex · Adds a hexagonal-grid coordinate type with tile arithmetic, neighbor, range, line-drawing, and distance functions.
Overview
PostgreSQL · 12, 13, 14, 15, 16, 17
Usage
Sources:
pghex adds a hex coordinate type and common algorithms for hexagonal grids. It is useful for board games, map cells, field-of-view calculations, and other applications that need neighbor, distance, line, ring, range, or spiral operations inside SQL.
Core Workflow
Coordinates are written as two-axis values [q, r]; the implementation derives the third cube coordinate as s = -q - r.
The +, -, and = operators add, subtract, and compare hex values.
Main Functions
neighbors(hex)anddiagonals(hex)return the six adjacent or diagonal cells.hex_distance(hex, hex)returns cube-grid distance.linedraw(hex, hex)returns the cells along a line, including its endpoints.hexes_in_range(hex, integer)returns every cell up to the requested distance.ring_path(hex, integer)returns a single ring;spiral_path(hex, integer)returns the center and successive rings.
The path and neighborhood functions are set-returning functions, so use them in FROM or with LATERAL when applying them per table row.
Caveats
The reviewed package reports version 0.0.0; upstream describes development-environment installation and does not present a stable compatibility guarantee. Its Cargo features cover PostgreSQL 12 through 17. The extension is non-relocatable and its control file requires superuser installation. Radius and distance arguments should be validated by the application, and large ranges grow quadratically in row count. Store one consistent coordinate convention across the application; no preload or restart is required.