pg_blkchain
pg_blkchain · C extension providing Bitcoin blockchain parsing and helper functions for PostgreSQL.
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 100003 | pg_blkchain | pg_blkchain | 0.0.1 | FUNC | MIT | C |
Usage
Sources:
pg_blkchain is an experimental C extension for decoding and constructing legacy Bitcoin blocks, transactions, inputs, outputs, and scripts inside PostgreSQL. Version 0.0.1 is explicitly work in progress and the reviewed source stopped in 2017; do not treat its parsing or signature verification as a current consensus or security implementation.
Core Workflow
Install the extension, keep raw serialized transactions in bytea, and expand their outputs for analysis:
Scripts can be expanded into opcode records:
The second example assumes transaction_output_script is a bytea value supplied by the caller.
Important Objects
get_block(bytea)returnsCBlock;get_tx(bytea)returnsCTx.get_vin(bytea)andget_vout(bytea)return sets ofCTxInandCTxOutrecords.parse_script(bytea)returnsCScriptOprecords containing the symbolic opcode, numeric opcode, and pushed data.verify_sig(txTo bytea, txFrom bytea, int)attempts to verify one transaction input signature.build_vin(...)andbuild_vout(...)are aggregates that serialize input and output components.get_vin_outpt_arr(bytea),get_vin_outpt_jsonb(bytea), andget_vin_outpt_bytea(bytea)expose previous-output references in several representations.
Operational Caveats
The upstream warning says to use the project at your own risk. The code predates major Bitcoin and PostgreSQL changes, parses untrusted binary data in the database backend, and provides no maintained compatibility or consensus-validation guarantee. A malformed input or C bug can terminate a backend. The README’s signature example also needs pgcrypto only for its double-SHA256 lookup query; it is not declared as an extension dependency. Use an actively maintained Bitcoin library outside PostgreSQL for validation, and keep this extension limited to isolated historical-data experiments.