Database reference
This is a low-level reference designed to assist with advanced workflows. To learn how to run basic queries for indexed data, visit the query the database guide.
This reference describes how Ponder interacts with the database, which may be useful for advanced workflows around zero-downtime deployments, crash recovery, and database administration.
Instance lifecycle
When a Ponder instance starts up, it runs the following logic (simplified for clarity).
- Start returning
200
from the/health
endpoint. Return503
from the/ready
endpoint. - Generate a new
instance_id
(a random four-character string). - For each table defined in
ponder.schema.ts
, create a new table in the database named{instance_id}__{table_name}
. If the user has specified a custom database schema inponder.schema.ts
, use it. - Begin the historical backfill (starting from scratch) and write data to the tables created in the previous step.
- When the historical backfill is complete, for each table create a view named
{table_name}
asSELECT * FROM {instance_id}__{table_name}
. If a view with that name already exists, drop the old one first. - Start returning
200
from the/ready
endpoint. - On shutdown (e.g. due to a process exit signal), do not drop the tables or views.
The following sections describe possible deviations from this standard lifecycle.
Live views
Instances running ponder dev
create the live views immediately. Instances running ponder start
create the live views as described above, just before the instance begins responding as ready.
Build ID and crash recovery
During the build step, each instance generates a content hash of the entire app (config, schema, and indexing function file contents). This is called the build_id
.
Then, each instance checks the _ponder_meta
registry table for any instances with the same build_id
that are no longer running. If it finds one, the current instance will adopt the prior instance's instance_id
and resume indexing where the prior instance left off.
Crash recovery is disabled when running ponder dev
.
Stale table cleanup
Shortly after startup, each instance checks the _ponder_meta
registry table and drops any tables belonging to instances that are not currently running, aside from the 3 most recent instances.
Tables belonging to non-running ponder dev
instances will always get dropped. They do not count towards the limit.
RPC request cache
Ponder caches RPC request data to improve reindexing performance on hot reloads and redeployments.
- The RPC request cache is located in the
ponder_sync
schema. - The cache is durable, persists across instance restarts, and does not store unfinalized data.
- The cache query patterns are designed to be lock-free. It's safe for multiple instances to run in the same Postgres database at the same time (reading and writing to the same
ponder_sync
schema).