pg_chainsync
pg_chainsync · Access blockchain blocks, events, and tasks directly inside PostgreSQL using background workers.
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 100805 | pg_chainsync | pg_chainsync | 0.0.0 | ETL | MIT | Rust |
PostgreSQL · 17
Usage
Sources:
pg_chainsync is a proof-of-concept PostgreSQL background worker for fetching blockchain blocks, events, and scheduled tasks and passing them to user-defined SQL handlers. The reviewed upstream README warns that breaking changes should be expected, so use it for controlled experimentation rather than assuming a stable ingestion contract.
Preload and Lifecycle
The worker requires pg_chainsync.so in shared_preload_libraries and a PostgreSQL restart. Set chainsync.database to the database in which the worker should operate, then create the extension there:
chainsync.restart() reloads registered jobs into the worker; chainsync.stop() stops their execution.
Register a Block Handler
A handler receives a typed chain value plus the job’s jsonb metadata. For an EVM block watcher:
The README also documents chainsync.EvmLog event handlers, one-shot work, cron tasks, preloaded tasks, and a block-await hook used to coordinate event processing with block availability. Confirm the installed chainsync.register signature and job JSON schema because this project is explicitly unstable.
Configuration and Reliability
chainsync.evm_ws_permitsbounds concurrent tasks sharing an EVM WebSocket provider.chainsync.evm_blocktick_resetcontrols when reduced block ranges are reset.chainsync.svm_rpc_permitsbounds concurrent SVM RPC fetches within a task.chainsync.svm_signatures_bufferlimits buffered SVM signatures.- Provider rate limits, disconnects, historical range limits, chain finality, and reorganizations are external failure modes. Persist checkpoints and chain identifiers, and design handlers to be idempotent under retries and replay.
- A successful callback is not by itself proof of finality. Define a confirmation policy and a reorg correction path before using synchronized data for irreversible decisions.
- Handler functions execute database writes under the worker’s privileges. Constrain ownership and
search_path, schema-qualify objects, validate job JSON, protect provider credentials, and test transaction/error behavior.