Which of 100 000 scenes cover my site in June?
GeoStack · background · question → rows
Abstract¶
Pictures first. A catalog answers “which files do I need?” before any pixel is read. This page covers what sits under one: tables, indexes, row and column storage, transactions, where a database lives, and what STAC and pgstac add. One question runs through all of it, with measured times.
The running example¶
A table¶
column: one attribute, one type
iddatetimebboxcloudhrefS2_0813012026-05-30T10:5671.2s3://…/081301/B12.tifS2_0813022026-06-02T10:4612.4s3://…/081302/B12.tifS2_0813032026-06-02T11:023.0s3://…/081303/B12.tifS2_0813042026-06-04T10:5655.9s3://…/081304/B12.tif
row: one sceneprimary key: id , unique, the name every other system uses for this row
A catalog is a table: one row per scene, a few typed columns, and a column that says where the data lives. The page’s table has 100 000 such rows.
A schema is the list of columns and their types. A primary key is a column whose value is unique per row; it is how a product record, a STAC item and a file on disk all refer to the same scene. Nothing else on this page works without one.
-- the catalog
CREATE TABLE scenes (
id TEXT PRIMARY KEY,
datetime TIMESTAMP NOT NULL,
geometry GEOMETRY(Polygon, 4326) NOT NULL, -- PostGIS type
cloud REAL,
href TEXT NOT NULL
);
CREATE INDEX scenes_time ON scenes (datetime); -- B-tree
CREATE INDEX scenes_geom ON scenes USING GIST (geometry); -- spatial
-- scenes over the site in June 2026
SELECT id, href FROM scenes
WHERE datetime >= '2026-06-01' AND datetime < '2026-07-01'
AND ST_Intersects(geometry, ST_SetSRID(ST_Point(-0.40, 39.45), 4326));Indexes: how a query skips rows¶
Without an index: look at every row
100 000 rows checkedWith a B-tree on datetime: the values are kept sorted, so halve the range each step
1234June 2026
20232027
about 17 steps to find where June starts, then read the 2 120 June rows in order
An index is a second, sorted copy of one column with pointers back to the rows. It makes reads on that column fast and every write a little slower, since the index must be updated too.
A spatial index: boxes inside boxes
The site is a point.Three of four groups cannot contain it: skipped whole.Inside the fourth, only the boxes that cover the point are kept.1 178 of 100 000 scenes cover the site
A B-tree sorts along one line, and a map has two. Spatial indexes (R-tree in SQLite, GiST in PostGIS) group nearby footprints into boxes, and boxes into bigger boxes, so a query discards whole regions at once. The index works on bounding boxes; the exact geometry test runs on the few rows that survive.
SQLite, no index (scan)
Python list scan
DuckDB over Parquet (row-group skip)
SQLite, R-tree on bbox
SQLite, B-tree on datetime
“Scenes over the site in June 2026” (23 hits) on 100 000 rows, best of five, one laptop-class core. PostgreSQL + PostGIS was not available in the sandbox and is not measured.
The time index wins here because June is the more selective filter (2 120 rows against 1 178). The database’s query planner makes that choice for you when both indexes exist. At 100 000 rows every method answers in milliseconds; the gap grows with the table, since a scan grows with the row count and an index lookup with its logarithm.
Row stores and column stores¶
Row store: each row’s values sit together
avg(cloud) walks every row and steps over the other four columnsColumn store: each column’s values sit together
avg(cloud) reads one stripe; same-typed neighbours also compress well
Same 100 000 rows, measured: mean cloud cover by month takes 30 ms in SQLite (rows) and 4.1 ms in DuckDB over Parquet (columns); the files are 24 MB and 5.2 MB. Fetching one scene by id takes 4 µs in the row store.
The two layouts suit two kinds of question. “Give me scene S2_081302” or “mark this scene processed” touches one row: a row store finds it through the primary key and changes it in place. “Mean cloud cover per month” touches one column of every row: a column store reads that column alone, and skips whole row groups whose min and max rule them out (the June query read 1 of 10).
import duckdb
# the same question, asked of a file; no server, no load step
hits = duckdb.sql("""
SELECT id, href FROM 'catalog.parquet'
WHERE datetime >= TIMESTAMP '2026-06-01' AND datetime < TIMESTAMP '2026-07-01'
AND x0 <= -0.40 AND x1 >= -0.40 AND y0 <= 39.45 AND y1 >= 39.45
""").fetchall() # → 23 rowsTransactions and upserts¶
A transaction: several changes, one outcome
beforescene row: old href
UPDATE scenes SET href …
COMMIT: both
INSERT INTO products …
crash: neither
Measured on the page’s SQLite file: an error raised between two writes left the first one undone. Compare the four PUTs on the object storage page, where the first two stay.
Databases promise four things about a transaction, known as ACID: it is atomic (all or nothing), leaves the data consistent with its rules (types, unique keys), is isolated from other transactions running at the same time, and is durable once committed. Atomic and isolated are the two the storage designs lean on.
-- insert, or update if the id is already there: safe to run twice
INSERT INTO scenes (id, datetime, geometry, cloud, href)
VALUES (:id, :datetime, :geometry, :cloud, :href)
ON CONFLICT (id) DO UPDATE
SET href = EXCLUDED.href, cloud = EXCLUDED.cloud;An upsert inserts a row or updates the one with the same key. Running it twice gives the same table as running it once, which is what a retried or restarted pipeline needs.
Where the database lives¶
ServerPostgreSQL + PostGISmany clients, many writersusers, roles, backupsyou run or rent it
In-processSQLite, DuckDBa library inside your programone file on diskone writer at a time
A fileGeoParquetno engine of its ownany reader, any languagechange = rewrite the file
The same table and the same SQL in three homes. They differ in who may write, and in what you have to operate.
| PostgreSQL + PostGIS | SQLite / DuckDB | GeoParquet file | |
|---|---|---|---|
| Writers at once | many | one | none: rewrite to change |
| Readers | anything with a driver, over the network | the process that opened the file | any Parquet reader, straight from a bucket |
| Spatial index | GiST | R-tree / spatial extension | bbox column statistics per row group |
| You operate | a server, users, migrations, backups | a file | a file |
| Best at | a live service many people write to | analysis on one machine | publishing and archiving a catalog |
SQL, drivers and ORMs¶
You reach a database through a driver that sends SQL. A toolkit such as SQLAlchemy lets you describe tables in Python and builds the SQL for you; its ORM layer goes further and maps rows to objects. Two rules keep that safe.
import sqlalchemy as sa
metadata = sa.MetaData()
# map only the columns this program uses; the table may have more
scenes = sa.Table(
"scenes", metadata,
sa.Column("id", sa.Text, primary_key=True),
sa.Column("datetime", sa.DateTime, nullable=False),
sa.Column("href", sa.Text, nullable=False),
)
# half-open: BETWEEN would stop at 30 June 00:00 and drop that day's scenes
query = sa.select(scenes.c.id, scenes.c.href).where(
scenes.c.datetime >= "2026-06-01",
scenes.c.datetime < "2026-07-01",
)A Python model may describe a subset of a table’s columns. Reads work as they are. Inserts work as long as every column the model leaves out is nullable or has a default.
One tool owns the table definitions. Either your migrations create and alter the tables, or another system does (pgstac owns its own schema). Two owners drift apart.
STAC and pgstac¶
STAC: an agreed shape for the rows
Cataloglinks to collections
Collectionone product: extent, bands
Itemone scene: geometry, datetime
Assethref to a file: the B12 COG
…and three ways to serve them
Static JSON filesfollow links
stac-geoparquetone table file
STAC APIsearch by place, time
pgstacitems stored in PostgreSQL
STAC is a vocabulary, not a database: it fixes the names (datetime, bbox, assets) so any client can search any catalog. A STAC API is usually stac-fastapi in front of pgstac, which is a schema and a set of SQL functions inside PostgreSQL.
A STAC item is one row of the catalog written as GeoJSON, with agreed names. The mapping to this page’s table is direct: id is the primary key, geometry and bbox the footprint, properties.datetime the time, and each asset an href to a file such as the B12 COG.
In your stack¶
Checked at geotoolz 640670c.
| Where | What it is, in this page’s terms |
|---|---|
| geocatalog catalogs | a table saved as GeoParquet and queried with DuckDB: a column store in a file. Reserved columns filepath, start_time, end_time, geometry, bbox |
build_raster_catalog | writes rows sorted by (start_time, geometry_hilbert), so row-group statistics can skip by time and by place |
GeoCatalog.query | the June question as a method: bbox and interval in, rows out |
| Product catalog design (F, K, L) | one record with three encodings (STAC item, SQL row, Parquet row); adapters for GeoParquet, PostGIS, pgstac and a STAC API; a sync into pgstac through its functions |
Where this goes next¶
| Idea here | Shows up as |
|---|---|
| Primary key | one product id across STAC, SQL and Parquet (product catalog, K) |
| Upsert | publish and sync that are safe to rerun (product catalog, F and L) |
| Row store against column store | PostGIS for the live product table, GeoParquet for the working catalog |
| A transaction | catalog rows committed with their chunks (Level 2 collections, B); next page: versions and identity |
Sources and status
Sources: the PostgreSQL and PostGIS documentation, the SQLite R*Tree documentation, the DuckDB and Apache Parquet documentation, the STAC specification, and the pgstac and stac-fastapi documentation, as I know them and not re-checked online for this page; the entries are in references.bib and cited from the page’s MyST stub. Every measured number comes from notebooks/background/databases.py, whose recorded output is docs/images/background_databases/results.json. Timings: 100 000 synthetic rows, SQLite 3.53, DuckDB 2.0, best of five on one machine; PostgreSQL was not measured. Stack rows read from geotoolz at 640670c.