Skip to content

Data Model Ledger

Mohammad Rasoul Kazemi edited this page Nov 28, 2026 · 3 revisions

Data Model Ledger

The data model of the ledger has two layers: the tables of the extension kingres_ledger (version 0.3) inside each database, and the logs that kingresd builds from them in the system database. The first layer is taken from kingres_ledger--0.3.sql, the second from migrations 0030 to 0034 of the system database.

Conceptual model

A table under ledger control produces entries: one per inserted row version, and for an updatable table one more per removed version. The entries of one transaction form a Merkle tree; the transactions of one block form a second tree; blocks are linked by hash into a chain. The chain identity is a random 32-byte chain_id written once when the extension is created. A digest names the head of the chain. kingresd turns the chain into a transparency log (an RFC 6962 tree published as tiles) and signs checkpoints of it; an aggregate log covers the chains of all shard groups of a database.

Tables, entries and blocks have no UPDATE, DELETE or TRUNCATE; the extension's code is the only writer (the guard, see Ledger). A ledger table cannot be distributed: create_distributed_table and create_reference_table refuse it (SQLSTATE KS016), because the multi-node ledger is phase 8, in design and not built (Status and Roadmap); each shard group keeps a chain of its own.

Tables of the extension

erDiagram
  LEDGER_TABLE ||--o{ LEDGER_ENTRY : "is recorded in"
  LEDGER_BLOCKS ||--o{ LEDGER_TX : "contains"
  LEDGER_TX ||--o{ LEDGER_ENTRY : "groups"
  LEDGER_ENTRY ||--o| LEDGER_ATTESTATION : "may be attested"
  LEDGER_CLIENT_KEY ||--o{ LEDGER_ATTESTATION : "signs"
  LEDGER_CLIENT_KEY ||--o| LEDGER_CLIENT_KEY_REVOCATION : "may be revoked"
  LEDGER_TABLE {
    int table_id PK
    regclass relid
    text mode
    regclass history_relid
    text signature_policy
    bigint signature_required_from
  }
  LEDGER_ENTRY {
    bigint ledger_txid PK
    int seq PK
    int table_id FK
    char op
    bytea row_hash
  }
  LEDGER_TX {
    bigint ledger_txid PK
    bigint block_height
    int tx_index
    int entry_count
    bytea merkle_root
    name user_name
  }
  LEDGER_BLOCKS {
    bigint height PK
    smallint hash_version
    bytea prev_hash
    bytea tx_merkle_root
    int tx_count
    timestamptz closed_at
    bytea block_hash
  }
  LEDGER_ATTESTATION {
    bigint ledger_txid PK
    int seq PK
    bytea key_id
    bytea nonce
    bytea signature
  }
  LEDGER_CLIENT_KEY {
    bytea key_id PK
    text algorithm
    bytea public_key
    name role_name
    text label
  }
  LEDGER_CLIENT_KEY_REVOCATION {
    bytea key_id PK
    text reason
    name revoked_by
    timestamptz revoked_at
  }
Loading
Table Content
ledger_table Tables under control. table_id is the stable identity used inside hashes (an OID is not stable across dump and restore). mode is append_only or updatable; history_relid is set exactly for updatable. signature_policy is optional or required; signature_required_from is set exactly when the policy is required. Neither policy column is covered by a hash.
ledger_chain One row: the 32-byte random chain_id. The prev_hash of block 0 is derived from it.
ledger_entry Key (ledger_txid, seq); table_id; op is I (inserted version) or D (removed version); row_hash is 32 bytes. An index on (table_id, row_hash) serves inclusion proofs.
ledger_pending A work queue of transactions that have entries but are not in a block yet (queued_at only lets ledger_status show the oldest wait). The block builder deletes from it. It is not part of the ledger.
ledger_tx One row per transaction: its block, index in the block, entry count, the Merkle root of its entries and the user name (the role that ran the first statement).
ledger_blocks Key height: hash_version (1), prev_hash, tx_merkle_root, tx_count, closed_at, block_hash.
ledger_digest Every digest this database generated, as JSON. A convenience record, not a root of trust: a digest proves something only when a copy was kept outside the database.
ledger_verification Every run of ledger_verify_recorded: state running, clean, problems or failed, counts, and the first problems as JSON.
ledger_removed The record of what an operator did with ledger_remove_stray: the rows taken out, as they were (action is removed or inserted), with who, when and why, and the source table. ledger_verify reports every row of it (operator_removal) for as long as it exists. Not readable by PUBLIC.
ledger_client_key, ledger_client_key_revocation, ledger_attestation Client signatures (0.3). key_id is the first 16 bytes of the SHA-256 of the Ed25519 public key (32 bytes); role_name NULL lets any role use the key; an attestation is (ledger_txid, seq) with the key id, a 16-byte nonce and a 64-byte signature, with (key_id, nonce) unique. They are append-only ledger tables themselves, registered by the extension and entered into the chain.
<table>_ledger_history For each updatable table: the removed versions, with the columns ledger_txid, ledger_seq and ledger_op added to the table's columns.

Views: ledger_status (one row for the console: table counts, latest block, pending count and the oldest pending age, last digest, last verification, worker state, and the roles that can drop the database; not readable by PUBLIC) and the read-only functions ledger_head, ledger_blocks_entries and ledger_block_preimage that kingresd uses.

Hashes and the Merkle tree (hash format version 1)

All hashes are SHA-256. The trees are RFC 6962: leaf_hash(d) = SHA256(0x00 || d), node_hash(l, r) = SHA256(0x01 || l || r). Every kind of hashed data starts with its own tag byte so one kind cannot be read as another:

Tag Kind
0x10 the canonical encoding of a row (columns in attribute order, NULL columns omitted, values as the type's binary send output, strings in UTF-8)
0x11 an entry in the tree of a transaction (sequence, table id, op, row hash; 42 bytes)
0x12 a transaction in the tree of a block (transaction id, entry count, Merkle root, user name)
0x13 the input of the block hash
0x14 the input of the hash that gives the prev_hash of block 0 from the chain_id

The full encoding is in the README of the extension; the standalone verifier reimplements it independently (The Verifier).

The published layer: the system database

kingresd stores the publication in kingres_cp (see Data Model System Database for all tables):

erDiagram
  LEDGER_LOG ||--o{ LEDGER_CHECKPOINT : "has"
  LEDGER_LOG ||--o{ LEDGER_TILE : "has"
  LEDGER_ANCHOR_TARGET ||--o{ LEDGER_ANCHOR : "receives"
  LEDGER_LOG {
    bigint log_id PK
    text kind
    text database_name
    int shard_group_id
    bytea chain_id
    int epoch
    text origin
    text state
    bigint size
    bytea root
  }
  LEDGER_CHECKPOINT {
    bigint log_id PK
    bigint size PK
    bytea root
    bytea note
    bool anchored
  }
  LEDGER_TILE {
    bigint log_id PK
    int level PK
    bigint idx PK
    int width PK
    bytea data
  }
  LEDGER_ANCHOR_TARGET {
    bigint id PK
    text kind
    text name
    bool enabled
  }
  LEDGER_ANCHOR {
    bigint id PK
    bigint target_id FK
    bigint log_id
    bigint size
    text state
  }
Loading
  • A log is of kind chain (one per chain and epoch) or aggregate (one per database). Its origin is unique and cannot change after the first checkpoint was signed. State is active, diverged or closed.
  • A tile is 256 hashes at most (width 1 to 256); level -1 holds entry bundles. A published tile is never replaced: the primary key makes a second write fail and a trigger refuses an update (SQLSTATE KC001). A superuser can still change a row; signatures, anchors and mirrors tell.
  • A checkpoint is the signed note for one size of one log ((log_id, size) is the key).
  • The aggregate leaf lists, for each chain of the database (at most 512), a flag byte, chain id, epoch, height, block hash and the root of the chain log.
  • An epoch starts a new log for the same chain after a restore or a loss at failover; the old log stays served for ever.

Sources

Clone this wiki locally