Skip to content

Have PostGIS write each feature in PostgreSQLGeoJSONProvider - #84

Closed
dblodgett-usgs wants to merge 2 commits into
cgs-earth:masterfrom
dblodgett-usgs:feature-text
Closed

dblodgett-usgs wants to merge 2 commits into
cgs-earth:masterfrom
dblodgett-usgs:feature-text

Conversation

@dblodgett-usgs

Copy link
Copy Markdown
Contributor

Assisted by Claude Opus 5.5 (implementation and testing) and Fable 5.1 (planning)

PostgreSQLGeoJSONProvider already has PostGIS write the geometry, but it still loads each row through the ORM and
builds a feature dict per row. This change has PostGIS write the whole feature, and the JSON formatter puts those
strings into the response without parsing them.

What changes

  • PostgreSQLGeoJSONProvider.query runs ST_AsGeoJSON(t.*, geom, max_decimal_digits, false, id) over a subquery that
    applies the core provider's filters (property, CQL, bbox, datetime), sort, offset and limit. Each row comes back as
    one string, wrapped in a GeoJSONFeature.
  • The id column is selected twice, once under an alias that ST_AsGeoJSON moves to the feature id, so id also
    stays in properties as it does with the core provider.
  • GeoJSONGeometry's lazy parsing moves to a base class, _LazyJSON, shared with the new GeoJSONFeature. A feature
    is parsed only when something reads it (HTML, JSON-LD, JSON-FG with a time field), and then serializes from its
    contents.
  • JSONFormatter writes an unread GeoJSONFeature into the collection as an orjson.Fragment.
  • Requests with a CRS transform or skipGeometry fall back to the core ORM path, so they still work but don't get the
    speedup. get() is unchanged.
  • numberMatched comes from an exact count(*) with the same filters, run only when count is set or for
    resulttype=hits, as in the core provider.

Results

100,000 flowlines from a national hydrofabric table, served by uvicorn with one worker. Median of three runs on a
fresh server:

features core provider before this change this change direct psycopg2 fetch
10,000 0.53 s 0.19 s 0.083 s 0.065 s
100,000 7.28 s 2.11 s 1.04 s 0.81 s

Peak server memory at 100,000 features: 1,035 MB for the core provider, 555 MB before this change, 490 MB after it.
The direct fetch runs the same ST_AsGeoJSON query with psycopg2 and no pygeoapi, so it is the floor for this
approach.

Against the core provider's response at 100,000 features: same ids in the same order, equal properties, coordinates
within 5e-10 (rounded to 9 decimals by max_decimal_digits). Offset, sortby, property filter, bbox, CQL,
properties=, count and hits give the same results as the core provider on the same table.

Tests

tests/test_postgresql_provider.py, tests/test_json_formatter.py and tests/test_jsonfg_formatter.py pass. One
formatter test is added: a GeoJSONFeature is written out unchanged and unparsed, and parses to the same feature when
read as a dict.

Notes

  • Property values are written by PostgreSQL's JSON conversion, not Python's. They match for text and double precision.
    Types like real, numeric, timestamps with time zone and NaN may come out differently than from the core provider.
  • Sorting only on a column with repeated values gives an unstable order across pages. This is the same in the core
    provider, which adds ORDER BY id only when no sortby is given.
  • The response is still built in full before it is sent. Streaming would be a change to pygeoapi's API, not to the
    provider.
  • The new query() copies the core signature, so it adds four ruff B006 warnings (bbox=[] and similar), matching
    PseudoPostgreSQLProvider in the same file.

dblodgett-usgs and others added 2 commits November 7, 2026 07:57
PostgreSQLGeoJSONProvider.query now selects ST_AsGeoJSON(t.*, geom,
max_decimal_digits, false, id) over a subquery that applies the core
filters, sort, offset and limit, so PostGIS writes the whole feature as
text. The provider returns those strings as GeoJSONFeature objects
without parsing them, and JSONFormatter writes each one into the
collection verbatim as an orjson.Fragment. The id column is selected a
second time under an alias that ST_AsGeoJSON moves to the feature id,
so id stays in properties as with the core provider.

Requests with a CRS transform or skipGeometry take the core provider's
ORM path. get() is unchanged.

GeoJSONGeometry's lazy parsing moves to a base class, _LazyJSON, shared
with the new GeoJSONFeature. A feature that is read (HTML, JSON-LD,
JSON-FG with a time field) parses on first access and serializes from
its contents, as GeoJSONGeometry already did.

At 100,000 flowlines from a national hydrofabric table, uvicorn with
one worker, the response takes 1.04 s against 2.11 s before this change
and 7.28 s for the core PostgreSQL provider, 1.28 times a direct
psycopg2 fetch of the same ST_AsGeoJSON rows. Peak server memory is
490 MB against 555 MB and 1,035 MB. The response matches the core
provider's ids, order and properties, with coordinates rounded to 9
decimals.

Assisted-by: Claude Opus-5.5
@webb-ben

Copy link
Copy Markdown
Member

Superseded by #85, which includes these commits plus the PostGIS 3.5 migration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants