Repository navigation
Ledger
kingres_ledger (version 0.3) puts ordinary PostgreSQL tables under ledger control. Every change to such a table is hashed and recorded in a hash chain of blocks, so that tampering can be detected later. This page describes the extension inside the database. Signing keys, publication and anchoring are on Ledger Signing and Publication and Ledger Anchoring; the independent checker is on The Verifier.
The ledger makes tampering detectable, not impossible. Anyone who can modify the database files, restore an old backup or replace the server binary can change data. What they cannot do is change it without the change being found by a verification against a digest that was stored outside the database.
-
The digest stored outside the database is the root of trust. Nothing inside the database,
ledger_verifyincluded, can prove integrity on its own. An attacker who rewrites the whole chain consistently passesledger_verifyand is caught only byledger_verify_digestwith a digest exported earlier. -
There is a window. A committed transaction is covered by a digest only after it is placed in a block and a digest of that block has left the database. The background worker builds a block about every
kingres_ledger.block_interval(10 seconds by default). - The digests that
ledger_generate_digestrecords inkingres.ledger_digestare not outside the database and are not a root of trust. - The ledger does not prove what a row version meant, who is allowed to change a row, or that a change was authorised. The user name of a transaction is the role that ran the statement.
A table is put under control with kingres.ledger_enable. There are two modes:
- Append-only (the default): rows can only be inserted.
-
Updatable (
ledger_enable(table, 'updatable')): rows can be inserted, updated and deleted, and every old version stays provable. A history table<table>_ledger_historykeeps each removed version.
Enabling is permanent, and so is the mode. There is no function that switches it off, and a table cannot change mode. Once a table is under control, DROP TABLE of it is rejected, and so are DROP SCHEMA ... CASCADE of its schema and DROP OWNED BY its owner. DROP EXTENSION kingres_ledger is rejected while any ledger table exists. ledger_enable is therefore not executable by PUBLIC.
Limits of this version: ordinary permanent heap tables in a user schema only (a table with the access method columnar is refused with 0A000), no generated columns, no partitioned or inheriting tables, no rules, no temporary or unlogged tables, and TRUNCATE is rejected in both modes. There is no conversion between the two modes for an existing table.
In a cluster. There is one chain per shard group, and an aggregate digest over the chains (phase 7). A ledger table cannot be distributed or made a reference table: create_distributed_table and create_reference_table refuse it with SQLSTATE KS016, and check_cluster reports one that the catalog distributes. A distributed schema that holds a ledger table is pinned to its shard group and cannot move (Sharding). A multi-node ledger with consensus is not built (phase 8, in design, not built).
These calls are taken from the README of the extension:
SELECT kingres.ledger_enable('public.accounts'); -- existing rows are entered too
SELECT kingres.ledger_enable('public.inventory', 'updatable');
SELECT kingres.ledger_build_block(); -- normally called on an interval
SELECT kingres.ledger_generate_digest(); -- store the result OUTSIDE the database
SELECT * FROM kingres.ledger_verify(); -- no rows when consistent
SELECT kingres.ledger_verify_digest('{...}'); -- a digest exported earlier
SELECT kingres.ledger_inclusion_proof('accounts', kingres.ledger_row_hash(a.*))
FROM accounts a WHERE id = 1;
SELECT kingres.ledger_verify_proof(proof, digest); -- uses only its two argumentsWrite the row as a.*, not a. ledger_enable and the block builder need the READ COMMITTED isolation level.
For an updatable table, an UPDATE writes a D entry for the old version and an I entry for the new one. A DELETE writes a D entry. The old version goes to the history table.
SELECT kingres.ledger_enable('public.inventory', 'updatable');
UPDATE inventory SET qty = qty - 1 WHERE id = 7; -- D of the old version, I of the new one
DELETE FROM inventory WHERE id = 8; -- D of the old version
SELECT * FROM inventory_ledger_history ORDER BY ledger_txid, ledger_seq; -- every removed versionThe history table has the columns of the table plus ledger_txid, ledger_seq and ledger_op. Only the extension owner and the owner of the ledger table (and roles that inherit it) can read it, and it bypasses row security and column privileges of the table. ledger_enable is not executable by PUBLIC; an administrator grants it: GRANT EXECUTE ON FUNCTION kingres.ledger_enable(regclass, text) TO table_admin;.
The library must be loaded at server start, then the extension is created as a superuser in each database:
shared_preload_libraries = 'kingres_ledger'
CREATE EXTENSION kingres_ledger;The role that creates the extension owns its functions and tables and must stay a superuser. Without the library preloaded, ledger_enable refuses to run (SQLSTATE KL003). The extension lives in schema kingres. To upgrade from 0.2: ALTER EXTENSION kingres_ledger UPDATE TO '0.3' after loading the new library. In a cluster that kingresd runs, the control plane preloads kingres_ledger (with kingres_shard, and kingres_columnar and pg_stat_statements when their libraries exist) on every instance it creates.
Settings named in the README: kingres_ledger.block_interval, max_block_transactions, worker_timeout, lock_hold_limit, max_clock_skew, enable_batch_delay, enable_credit_limit and allow_manual_write (superuser only, see the guard below).
- A trigger records an entry for every changed row version (transaction id, sequence number, row hash). The transaction is queued as pending.
-
ledger_build_blockcomputes each transaction's Merkle root and writes a block that links to the previous block by hash. Order inside a block is by transaction id, not commit order. - Block-building workers (a launcher plus short-lived workers, started when recovery has finished, so a standby has none) call the builder on the interval.
block_interval = 0switches them off. A call toledger_build_block()orledger_generate_digest()builds a block by hand at any time. - The hash format is version 1: digests, proofs and block hashes made by 0.1 still verify. The format is written out in the README section "Hash format version 1".
- A hot standby can verify the ledger, check digests and build and check proofs; it cannot enable tables or build blocks.
| Function | What it does |
|---|---|
ledger_verify() |
reads every ledger table and entry and returns one row per finding; none means consistent. It is meant for scheduled use. |
ledger_verify_digest(digest) |
checks a digest exported earlier against the chain |
ledger_verify_recorded |
a verification that is recorded in ledger_verification
|
ledger_inclusion_proof, ledger_verify_proof
|
prove that one row version is in the ledger; the proof is checked with its digest alone |
ledger_head, ledger_blocks_entries, ledger_block_preimage
|
read-only functions that the publisher in kingresd uses to build tile logs |
ledger_status (a view, not a function) |
table counts, latest block, pending transactions and the age of the oldest, the last digest and verification, worker state and the roles that can drop the database |
ledger_head and ledger_blocks_entries are not executable by PUBLIC. Grant EXECUTE to the service role of kingresd. They detect changed fields, wrong links or hashes, gaps, and rows that point above the head. They do not detect blocks removed from the end together with the rows that refer to them; a digest or signed checkpoint kept elsewhere does.
From kingres_ledger--0.3.sql, schema kingres. Pure helpers of the hash format (ledger_leaf_hash, ledger_node_hash, ledger_merkle_root, ledger_merkle_path, ledger_tx_leaf, ledger_block_tx_leaf, ledger_block_hash, ledger_genesis_hash, ledger_row_hash, ledger_history_row_hash, ledger_timestamptz_us) are in the same schema; the hash format is in the README, "Hash format version 1". Internal functions and triggers are not listed.
| Function | Arguments | Returns and purpose |
|---|---|---|
ledger_enable |
relation regclass, mode text DEFAULT 'append_only' |
Puts a table under ledger control ('append_only' or 'updatable'); existing rows are entered. |
ledger_build_block |
none | Builds a block from the pending transactions by hand. |
ledger_generate_digest |
none | jsonb: chain identity, height and block hash of the latest block (closes a block if needed and records the digest); NULL when the ledger is empty. |
ledger_verify |
from_height bigint DEFAULT 0, to_height bigint DEFAULT NULL |
One row per finding (check_name, object, detail); none means consistent. |
ledger_verify_recorded |
same | Like ledger_verify and records the result in ledger_verification. |
ledger_verify_digest |
digest jsonb |
boolean: does the chain match a digest exported earlier. |
ledger_inclusion_proof |
relation regclass, row_hash bytea [, op "char"] |
jsonb proof that a row version is in the ledger. |
ledger_verify_proof |
proof jsonb, digest jsonb [, expected_table_id int, expected_row_hash bytea [, expected_op "char"]] |
boolean; uses only its arguments. |
ledger_head, ledger_blocks_entries
|
ledger_blocks_entries(from_height bigint, max_count int) |
Read functions for the publisher; not executable by PUBLIC. |
ledger_block_preimage |
hash_version smallint, height bigint, prev_hash bytea, tx_merkle_root bytea, tx_count int, closed_at timestamptz |
The bytes a block hash covers. |
ledger_remove_stray |
reason text |
Superuser with allow_manual_write on: takes out a row that stops ledger_head; a copy is kept in ledger_removed. |
ledger_client_key_register |
public_key bytea, role_name name, label text |
Registers a client Ed25519 key. |
ledger_client_key_revoke |
key_id bytea, reason text |
Revokes a client key. |
ledger_attest |
relation regclass, row_hash bytea, op "char", key_id bytea, nonce bytea, signature bytea |
Records a client signature of an entry written in this transaction. |
ledger_attestation_message |
chain_id bytea, table_id int, op "char", row_hash bytea, key_id bytea, nonce bytea |
The message a client signs. |
ledger_set_signature_policy |
relation regclass, policy text |
optional to required, never back. |
ledger_worker_state, ledger_hook_active
|
none | State of the block worker; whether the protection hook is installed. |
Tables: ledger_table, ledger_chain, ledger_entry, ledger_pending, ledger_tx, ledger_blocks, ledger_digest, ledger_verification, ledger_removed, and for client signatures ledger_client_key, ledger_client_key_revocation, ledger_attestation. The model is on Data Model Ledger.
| SQLSTATE | Meaning |
|---|---|
KL001 |
modification of an append-only table |
KL002 |
statement rejected by the protection hook or event trigger |
KL003 |
the library is not preloaded |
KL004 |
the previous block is closed ahead of the clock (max_clock_skew); the builder refuses to build and transactions stay pending |
KL005 |
the SQL of the extension is older than the library |
KL006 |
a call out of the order of the locks |
KL010 |
a transaction leaves an entry of a table with signature policy required without an attestation (at COMMIT or PREPARE TRANSACTION) |
KL011 |
ledger_attest finds no such entry in this transaction, or every one is attested |
KL012 |
the key is unknown, revoked or not allowed for the role |
KL013 |
the signature does not verify |
KL014 |
the nonce was used with the key before |
The short descriptions are taken from the README text around each code; the README is the reference. See Troubleshooting.
The ledger tables (the nine kingres.ledger_* tables, plus the three of client signatures) can be written only by the code of the extension. The rule in the README is: no statement that a role can issue writes a ledger table, whatever the role inherits or owns, except through the public functions doing what they are documented to do, and except a superuser acting as itself with kingres_ledger.allow_manual_write set, which leaves a record. Members of pg_write_all_data and pg_maintain cannot write or lock them either. A role that is not a superuser may call a lock-taking function of the ledger only as a statement of its own, not in a transaction block.
Protections against switching the ledger off (event triggers and a hook in the library) stop mistakes and ordinary privileged use. They are not a security boundary against someone who edits catalogs directly, disables event triggers, removes the library from the configuration or changes files. The README lists every known way. One is stated plainly: a database can be dropped together with its ledger by every role that has the privileges of its owner or can SET ROLE to it, and nothing inside the database prevents that. ledger_status lists such roles.
A client can sign the rows it writes, so that the entry shows that the holder of a key wrote it. An attestation is an Ed25519 signature over one ledger entry of one row version in one chain. The server never signs for a client and never sees a private key.
Objects, all ledger tables of the extension itself: ledger_client_key, ledger_client_key_revocation, ledger_attestation. Functions: ledger_client_key_register(public_key, role_name, label), ledger_client_key_revoke(key_id, reason), ledger_attest(relation, row_hash, op, key_id, nonce, signature), ledger_attestation_message(...) and ledger_set_signature_policy(relation, policy).
Workflow: read chain_id and table_id, compute the row hash from the data the client holds (with kingres-verify row-hash or its own implementation), write the row, and in the same transaction call ledger_attest. An UPDATE of an updatable table needs two attestations.
Policy: a table can be switched from optional to required, never back. With required, a transaction that leaves any entry without an attestation fails at COMMIT or PREPARE TRANSACTION with KL010. The switch waits for the open writers of the table. The policy is not covered by a hash: a superuser who edits the catalog can switch it back, and what was written meanwhile is not found by ledger_verify. The client must recompute the row hash itself and must not sign a hash that the server returned without doing so.
- Phase 6 (the ledger) and phase 7 (signing, anchoring, publication, aggregate digest; design
ledger-phase7.md) are built. Phase 7 step 24 (an external signer command, publication mirrors and the anchor probe from another server) is not in the first release. The extension's work in 0.3 is the read functions and the client signatures. -
ledger_verifydoes not verify client signatures; the standalone verifier does. - Server signatures, anchoring and the aggregate digest are in
kingresd, not in the extension (Ledger Signing and Publication, Ledger Anchoring). - A database export and import carry a ledger table as data and start a new chain, which is verified (ADR 0028).
- After a restore of the system database the aggregate log and the events cannot be re-derived: the operator accepts the loss, and the logs fork against what was published (ADR 0025).
- The guard and the client signatures were reviewed in several rounds; the review files are in the repository.
See also Security Model and Configuration.
- https://github.com/KingresDB/Kingres/blob/kingres-18/contrib/kingres_ledger/README.md
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/docs/design/ledger-mvp.md
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/docs/design/ledger-0.2.md
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/docs/design/ledger-phase7.md
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/docs/adr/0013-ledger-tables-and-anchoring-first.md
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/README.md
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/docs/guide/limitations.md
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/docs/reviews/2026-11-18-kingres-ledger-0.3-guard-seventh-and-client-signatures.md
- https://github.com/KingresDB/Kingres/blob/kingres-18/kingres/docs/reviews/2026-11-18-kingres-ledger-0.3-client-signatures-second.md
KingresDB's own code is licensed under AGPL-3.0-only. PostgreSQL code keeps the PostgreSQL License. Copyright of the project's own code: RooyeKhat Media Co. KingresDB is derived from PostgreSQL and is not endorsed by the PostgreSQL project. Repository | Licence | COPYRIGHT
KingresDB
1. Product
- Product Overview and Features
- Status and Roadmap
- User Guide
- Administrator Guide
- REST API Reference
- Web Console
- Command Line Client
- Extensions
- Limitations and Known Issues
- Release Notes
Features in detail
- Sharding
- Sharding Query Pushdown
- Sharding Table Lifecycle
- High Availability
- Ledger
- Ledger Signing and Publication
- Ledger Anchoring
- The Verifier
- Analytics Extensions
Administration
- Cluster Setup
- Servers Databases and Roles
- Rebalancing
- Configuration
- Memory and Warm-up
- Control Plane and REST API
- Upgrades
2. Architecture (arc42)
- Architecture
- Architecture Introduction and Goals
- Architecture Constraints
- Architecture System Context
- Architecture Containers
- Architecture Components
- Architecture Runtime Views Sharding
- Architecture Runtime Views Availability and Ledger
- Architecture Deployment
- Architecture Crosscutting Concepts
- Security Model
- Architecture Decisions
- Architecture Quality Requirements
- Architecture Risks and Technical Debt
- Enterprise Architecture
3. Data architecture
- Data Architecture
- Data Model Sharding
- Data Model Ledger
- Data Model System Database
- Data Model Analytics
- Data Lifecycle
- Backup Restore and PITR
- Data Security
- Data Quality and Integrity
4. Development
- Building from Source
- Development and Testing
- Repository Layout
- Coding Standards
- Branching and Synchronisation
- Testing Strategy
- Review Process
- Release Process
- Contributing
5. Operations