PostgreSQL tile sources
PostgreSQL tile sources connect Martin to PostGIS data and turn database rows or database-produced tile bytes into map tiles. The source lifecycle covers connection checks, catalog discovery, tile SQL, feature decoding, and optional rendering or MLT encoding. For the wider request path, see tile serving; compare other source types in file and remote tile sources and raster, terrain, and transcoding.
Source lifecycle
PostgreSQL sources follow two related paths. A table normally lets PostGIS make the tile, while a table with configured rendering layers can give Martin features for its own renderer. A function source is trusted to return tile bytes and does not expose its internal features.
flowchart LR
C[Connection settings] --> P[Pool and PostGIS checks]
P --> D[Catalog discovery]
D --> T[Table source]
D --> F[Function source]
T --> Q[Tile SQL or feature scan]
F --> QF[Function tile query]
Q --> O[Feature rows or MVT bytes]
QF --> O
O --> E[Response encoding and cache]
Connection management
Connection pools
Each configured PostgreSQL connection becomes a shared pool for its discovered sources. The pool selects TLS or plain transport from the connection settings, checks the PostgreSQL and PostGIS versions, and keeps the database identity used for source IDs and logs. PostgreSQL 15 or newer and PostGIS 3.5 or newer are required. martin-core/…/pool.rs
The pool also owns the active-query registry. Each tile or feature query is registered while it runs, so a dropped request can cancel its database work instead of leaving the query active until the database finishes it. martin-core/…/pool.rs
References: martin-core/…/pool.rs• L35-98 martin-core/…/pool.rs• L184-265 martin-core/…/pool.rs• L357-446
Startup retries
The retry policy applies to the first connection during source startup. The default is 30 seconds. 0s fails at once, while infinite waits until the database answers. Martin retries connection states that indicate a starting, restarting, full, or unreachable database. It does not retry bad credentials, an unknown database, or a failed TLS handshake. martin-core/…/retry_timeout.rs martin-core/…/pool.rs
References: martin-core/…/retry_timeout.rs• L10-35 martin-core/…/pool.rs• L157-181 martin-core/…/pool.rs• L268-293
Catalog discovery
Table and function sources
Discovery reads the PostgreSQL catalog for spatial tables and for functions that accept z, x, and y, with an optional JSON query argument. Explicit configuration narrows or enriches catalog results. Automatic publishing can include tables, functions, or both. It can also limit discovery to selected schemas. martin/…/builder.rs martin/…/auto_publish.rs martin/…/query_tables.rs
Configured entries merge with catalog metadata before they become sources. A configured table or function takes precedence over an automatically published entry for the same database object, while different tile grids can produce separate sources. Name matching accepts an exact name or one unambiguous case-insensitive match. A schema that exists but has no usable tile content is reported differently from a schema that does not exist. martin/…/builder.rs martin/…/builder.rs martin/…/utils.rs
Source settings include the tile grid, spatial reference, bounds, feature limit, ordering, filter, layer name, and property choices. The connection defaults apply unless a source supplies its own value. Table defaults use extent 4096, buffer 64, and geometry clipping enabled. martin/…/config.rs martin/…/config_table.rs martin/…/query_tables.rs
Discovery resolves the source CRS against the selected tile grid. It checks that PostGIS can perform required transforms and skips auto-published tables that have no usable SRID or cannot be transformed. For tables, it can compute TileJSON bounds during source creation. For functions, the function is trusted to produce tiles in its declared grid and its SQL comment can provide TileJSON and content type data. martin/…/auto_publish.rs martin/…/builder.rs martin/…/query_tables.rs martin/…/config_table.rs
References: martin/…/builder.rs• L235-292 martin/…/builder.rs• L295-368 martin/…/builder.rs• L421-481 martin/…/auto_publish.rs• L202-357 martin/…/query_tables.rs• L40-141 martin/…/config_table.rs• L224-275 martin/…/utils.rs• L8-95
Runtime source refresh
The PostgreSQL reloader publishes the initial catalog before serving starts. It then polls because PostgreSQL does not provide Martin with a source-change signal. Each poll compares resolved source specifications and applies added, changed, and removed sources. The default poll interval is 10 minutes, and 0s disables polling. martin/…/postgres.rs martin/…/postgres.rs martin/…/config.rs
Each resolved source has a fingerprint made from fields that can change served tile bytes or metadata. A catalog refresh therefore avoids rebuilding an unchanged source, while a change to its SQL, grid, geometry, properties, layer metadata, or function signature creates a new source version. Fields that do not affect served output, such as cache settings, do not trigger this source replacement. martin/…/spec.rs
References: martin/…/postgres.rs• L21-101 martin/…/config.rs• L19-35 martin/…/spec.rs• L10-79 martin/…/reload.rs• L27-55
Table scanning
Table scanning produces a row-per-feature view of a table for bulk generation and for Martin-managed layer rendering. A scan reads geometry, an optional integer feature ID, and selected properties in a stable column order. It streams rows into batches so the renderer can process large tables without loading the full table into memory. martin-core/…/scan.rs martin-core/…/scan.rs
Scans use a read-only repeatable-read transaction. Multiple readers can share one exported snapshot, which keeps all partitions consistent while they run in parallel. Plain tables and materialized views can split by heap blocks. Other sources use an integer-ID split when possible or one unbounded partition. martin-core/…/scan.rs martin/…/postgres.rs martin/…/postgres.rs
The scan query keeps WGS84 and Web Mercator geometry in its source CRS for the generator to project on workers. Other source CRSs are projected to WGS84 in PostgreSQL. It applies the configured row filter and any generation condition or bounding box before it streams rows. martin/…/query_tables.rs
References: martin-core/…/scan.rs• L19-30 martin-core/…/scan.rs• L46-112 martin-core/…/scan.rs• L134-228 martin/…/postgres.rs• L69-117 martin/…/postgres.rs• L135-190 martin/…/query_tables.rs• L569-636
Tile query execution
Table tile queries
The normal table path asks PostGIS for one tile-shaped result. The query selects features that intersect the tile search area, applies the source filter, transforms and clips geometry into tile space, and returns either one MVT blob or one row per feature. Tile extent, buffer, clipping, feature limits, ordering, and the layer name define the tile contract. The tile envelope is converted to the box type expected by PostGIS once per tile, which avoids repeating that conversion for every feature. martin/…/query_tables.rs martin/…/query_tables.rs
The search area follows the source tile grid and the table CRS. When the CRSs differ, the query densifies and transforms the search envelope so curved edges stay covered. It also widens searches for coordinate systems that cut the world at the antimeridian. This keeps index filtering broad while PostGIS performs the final tile clipping. martin/…/query_tables.rs
The row query preserves measured geometry when a downstream MLT encoder needs M ordinates. For a measured polygon, the query scales, clips, snaps, and orients the polygon in tile space before it returns WKB. Other geometry uses the normal tile geometry path. martin/…/query_tables.rs
References: martin/…/query_tables.rs• L201-300 martin/…/query_tables.rs• L320-484 martin/…/query_tables.rs• L430-525 martin/…/query_tables.rs• L712-823
Function tile queries
Function sources call a catalog-discovered function with tile coordinates. A function may return a tile blob directly or a record with the tile and an ETag. A query-aware variant receives the request query as JSON. When both query-aware and queryless variants exist, Martin chooses the queryless variant for requests without query parameters. martin/…/query_functions.rs martin/…/query_functions.rs martin-core/…/source.rs
At request time, Martin prepares the selected SQL, binds z, x, and y, and binds the query JSON only when the function declares that input. A missing row or null tile becomes an empty tile. A valid returned ETag is used directly. Otherwise Martin derives one from the tile bytes. martin-core/…/source.rs martin-core/…/source.rs
References: martin/…/query_functions.rs• L167-205 martin-core/…/source.rs• L168-201 martin-core/…/source.rs• L233-346 martin-core/…/source.rs• L374-455
Query cancellation
Tile queries hold a cancellation guard for their full database lifetime. When Martin cancels all active PostgreSQL work, the registry sends cancellation requests concurrently and removes the queries from its registry. This protects shutdown and request cancellation from turning into abandoned database work. martin-core/…/source.rs martin-core/…/pool.rs
References: martin-core/…/source.rs• L286-332 martin-core/…/pool.rs• L364-446
Feature encoding
Row decoding
The row-per-feature path turns PostGIS WKB into tile-coordinate geometry and keeps the feature ID when it is a non-negative integer. It maps common PostgreSQL scalar types to typed tile values. JSONB values remain documents until the target tile format decides how to represent them. martin-core/…/features.rs martin-core/…/features.rs
The table query casts unsupported property types to text so the row form matches the properties that PostGIS writes into MVT. Null values have no tile property. A source with an unsupported type that reaches decoding without a compatible cast fails instead of silently changing the value. martin/…/query_tables.rs martin-core/…/features.rs
Configured property names can map to database columns by exact name or by one case-insensitive match. An ambiguous case-insensitive match is rejected so the source does not silently read the wrong column. martin/…/config_table.rs martin/…/utils.rs
References: martin-core/…/features.rs• L16-35 martin-core/…/features.rs• L55-115 martin-core/…/features.rs• L139-184 martin/…/query_tables.rs• L167-199 martin/…/config_table.rs• L350-357 martin/…/utils.rs• L8-22
Tile formats
A PostgreSQL source can return MVT or another format declared by the source. For database-produced compressed bytes, Martin detects the actual encoding before it creates response metadata. Table row features provide the input needed when a caller must produce a format that PostGIS does not produce itself. martin-core/…/source.rs martin-core/…/source.rs martin/…/config_function.rs
References: martin-core/…/source.rs• L83-110 martin-core/…/source.rs• L168-201 martin-core/…/source.rs• L225-231 martin/…/config_function.rs• L141-162
MLT-specific paths
Direct MLT encoding
The MLT path reads table features from the row query and encodes them directly as one-layer MLT. It avoids taking an MVT blob apart first. Typed values become MLT property columns, empty feature sets become empty MLT tiles, and property column types widen when rows contain mixed numeric values. martin-core/…/mlt_encoder.rs martin-core/…/mlt_encoder.rs
Function sources do not have this row form because their SQL owns the tile blob. They therefore follow the function output path and any configured format conversion outside the PostgreSQL row encoder. martin-core/…/source.rs martin-core/…/source.rs
References: martin-core/…/mlt_encoder.rs• L26-45 martin-core/…/mlt_encoder.rs• L49-116 martin-core/…/mlt_encoder.rs• L215-290 martin-core/…/source.rs• L83-110 martin-core/…/source.rs• L393-410
MLT v1 and v2 values
MLT v2 can keep PostgreSQL M ordinates in a vertex value column and can keep JSONB documents as nested columns. MLT v1 cannot keep either shape in the same way: it drops measures and exposes supported top-level JSONB fields as ordinary properties, matching the MVT behavior. Nested data has a depth limit. Data that cannot fit the inferred shape is kept as text. martin-core/…/mlt_encoder.rs martin-core/…/mlt_encoder.rs martin-core/…/nested.rs martin-core/…/nested.rs
The encoder tells the table query whether it must preserve measures. That keeps the database query and the selected MLT wire format aligned. martin-core/…/source.rs martin-core/…/mlt_encoder.rs
References: martin-core/…/mlt_encoder.rs• L26-31 martin-core/…/mlt_encoder.rs• L134-173 martin-core/…/nested.rs• L12-14 martin-core/…/nested.rs• L128-181 martin-core/…/source.rs• L89-110
Engine-backed table sources
Layer rendering
An engine-backed table source reads features from PostgreSQL and lets Martin render configured layers into MVT. The database query is selected by zoom and returns geometry, IDs, and properties for the feature plan. Martin then renders the data in a blocking worker and reports skipped feature slices or failed expressions without stopping the whole tile when the renderer can continue. martin-core/…/engine.rs martin-core/…/engine.rs martin-core/…/engine.rs
This path is limited to Web Mercator and can prefetch an aligned block of tiles after a cache miss. The block shares one expanded query area, including a second envelope when it crosses the antimeridian. martin/…/query_tables.rs martin-core/…/engine.rs martin-core/…/engine.rs
The engine path is selected during source instantiation. A table with configured layers cannot use a non-Web-Mercator grid, while a normal table remains on the PostGIS tile-query path. martin/…/query_tables.rs martin/…/config_table.rs
References: martin/…/query_tables.rs• L266-300 martin/…/query_tables.rs• L639-672 martin-core/…/engine.rs• L15-30 martin-core/…/engine.rs• L46-118 martin-core/…/engine.rs• L129-186
Per-tile layer operations
Per-tile layer operations shape the features that remain in one engine-rendered tile after layer selection and sorting. limit keeps the first configured number of features, while thinning reduces point density with a grid. Each operation can use one setting or separate settings for non-overlapping zoom ranges. martin/…/per_tile.rs martin/…/per_tile.rs martin/…/per_tile.rs
These operations belong inside a named table layer under layers. The table source rejects the same keys at its top level because they act on a layer's features rather than on the source as a whole. martin/…/config_table.rs martin/…/error.rs
References: martin/…/per_tile.rs• L15-26 martin/…/per_tile.rs• L64-105 martin/…/per_tile.rs• L137-168 martin/…/config_table.rs• L158-185 martin/…/error.rs• L44-47
Related pages
- Tile serving describes the shared source, cache, and response flow.
- File and remote tile sources describes non-PostgreSQL vector tile inputs.
- Raster, terrain, and transcoding describes related raster and format conversion paths.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the table row query column order aligned with
PostgresRowQuery,ScanLayout, and both row decoders. - Keep measured geometry SQL aligned with the MLT encoder's
keeps_measuresdecision. - Keep auto-publish rules aligned with configured-source merging, case-insensitive name resolution, SRID fallback, and source ID formats.
- Keep function catalog output shapes aligned with ETag handling, queryless routing, and TileJSON content type detection.
- Keep source fingerprints aligned with every table or function field that can change served bytes or metadata.
- Keep engine scan columns aligned with the feature plan and the renderer's layer definitions.
- Keep per-tile layer operations under
layersand keep their zoom ranges non-overlapping. - Keep snapshot-based scan readers within the pool capacity after changing pool or scan concurrency rules.
- Keep reload interval behavior aligned with startup publication and the add, update, and remove source diff.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| PostgreSQL is older than 15 or PostGIS is older than 3.5 | The pool rejects the source during startup. | Version checks run when the pool connects. |
| The first connection fails with a non-transient error | Startup stops without waiting for the retry timeout. | Use valid credentials, database names, and TLS certificates. |
| A table has no usable SRID or PostGIS cannot transform it to the tile grid | Discovery omits the table source. | Set the correct table or default SRID and verify the CRS in PostGIS. |
| A configured schema exists but has no geometry tables or tile-serving functions | The configured source is reported as unavailable instead of as a missing schema. | Configure a schema with usable tile content. |
| A property or ID name has several case-insensitive matches | Source merge cannot choose a safe database column. | Use the exact column name. |
| A feature property reaches the row decoder with an unsupported PostgreSQL type | The tile query fails during feature decoding. | Configure a supported property type or expose the value as text. |
| A per-tile operation is placed directly on a table source | Configuration is rejected because the operation has no layer scope. | Put limit or thinning under a named entry in layers. |
| A configured MLT table needs measures but the query does not keep them | M values are absent from the encoded tile. | Use the format-aware measured row query. |
| A source refresh changes a served field that its fingerprint does not include | The old source can remain live after catalog polling. | Include every served-byte or metadata field in SourceSpec::fingerprint. |
| An engine or bulk scan uses a pool with no reader slot left | Rendering or scanning cannot open a reader. | Leave pool capacity for the snapshot connection. |
Key files
| File | Why to read it |
|---|---|
| martin/…/config.rs | Defines connection defaults, auto-publish controls, reload polling, and format settings. |
| martin-core/…/pool.rs | Defines connection checks, pool behavior, startup retries, and query cancellation. |
| martin/…/builder.rs | Shows how catalog results and configuration become resolved source specs and live sources. |
| martin/…/auto_publish.rs | Explains catalog scope, auto-publish decisions, source IDs, and SRID filtering. |
| martin/…/config_table.rs | Defines table-source settings and the merge from catalog metadata to configured source. |
| martin/…/per_tile.rs | Defines layer operations that shape engine-rendered features within each tile. |
| martin/…/query_tables.rs | Explains table discovery, bounds, CRS-aware tile SQL, row scans, and engine queries. |
| martin/…/postgres.rs | Defines initial publication and periodic catalog refresh. |
| martin/…/spec.rs | Defines resolved source fingerprints used to detect meaningful refresh changes. |
| martin/…/query_functions.rs | Explains function signatures, query variants, record outputs, and ETags. |
| martin-core/…/source.rs | Connects requests to table, function, row-feature, and engine paths. |
| martin-core/…/features.rs | Defines the common feature and property model used after row decoding. |
| martin-core/…/scan.rs | Defines consistent snapshots and streaming table scans. |
| martin-core/…/mlt_encoder.rs | Defines direct PostgreSQL feature encoding into MLT. |
| martin-core/…/engine.rs | Defines Martin-rendered layers and block prefetch for table sources. |