# SQL storage schema Reference for the tables `kuatia-storage-sql` creates. This is the composed end-state after replaying every migration in `crates/kuatia-storage-sql/src/migrations/` (`001_init` through `013_value_date`) plus the `_migrations` bookkeeping table created in `crates/kuatia-storage-sql/src/migrate.rs`. It is a living reference: update it when a migration lands. For *why* the schema looks this way, see the ADRs it links to, not this file. Ground rules that keep the catalog terse: - **The store is a dumb record-keeper, so no foreign keys are declared.** Every cross-table relationship below is logical, enforced in Rust, not by the database. A posting's identity is `transfer_id + idx`; `account_head` points at the current `accounts` version; the account/transfer link is an explicit index table. See [ADR-0003](adr/0003-dumb-storage-saga-recovery.md). - **Every payload and hash column is `TEXT`.** Content-addressed ids and opaque saga bytes are lower-case hex; JSON payloads are their `TEXT` serialization. The store never does arithmetic on these. - **Money columns are backend-native, and only they may be summed in SQL.** An amount is `NUMERIC(39,0)` on PostgreSQL and a 16-byte `BLOB` on SQLite, always at full `i128` width whatever the `i128` cargo feature selects. Both forms are ordered and compared numerically, and both sum exactly: plain `SUM()` on Postgres, the registered `kuatia_i128_sum` aggregate on SQLite, which raises rather than wrapping. Everything else is still computed in Rust. `AMOUNT` in the diagram below stands for this pair of types. See [ADR-0024](adr/0024-native-money-columns.md). - **All ids are Rust-minted `BIGINT`.** No `AUTOINCREMENT` / `SERIAL`; snowflake ids come from `AutoId`. See [ADR-0015](adr/0015-fixed-width-account-code.md). - The DDL is identical on SQLite and PostgreSQL (`sqlx::Any`) except for the money columns, whose native types differ; that divergence lives in the `Dialect` seam and in the per-backend halves of `011_native_amounts`. Design rationale lives in [ADR-0016 (immutable postings + index tables)](adr/0016-immutable-postings-index-tables.md), [ADR-0017 (append-only hot indexes)](adr/0017-correctness-first-append-only-hot-indexes.md), [ADR-0022 (merged live-postings hot index)](adr/0022-merged-live-postings-hot-index.md), [ADR-0019 (cached balance projection)](adr/0019-cached-balance-projection.md), and [ADR-0008 (conformance-tested storage)](adr/0008-conformance-tested-storage.md). ## Entity relationships Edges are **logical only** (no `FOREIGN KEY` constraint exists); the labels name how the ledger relates the rows in Rust. ```mermaid erDiagram accounts { BIGINT id PK BIGINT subaccount PK BIGINT version PK INTEGER flags BIGINT book TEXT metadata } account_head { BIGINT id PK BIGINT subaccount PK BIGINT version } postings { TEXT transfer_id PK SMALLINT idx PK BIGINT owner BIGINT subaccount INTEGER asset AMOUNT value } live_postings { TEXT transfer_id PK SMALLINT idx PK BIGINT owner BIGINT subaccount INTEGER asset AMOUNT value } transfers { TEXT id PK TEXT transfer TEXT receipt BIGINT created_at BIGINT book BIGINT value_date } transfer_accounts { TEXT transfer_id PK BIGINT account_id PK BIGINT subaccount PK } books { BIGINT id PK TEXT name TEXT data } balance_projection { BIGINT id PK BIGINT account BIGINT subaccount INTEGER asset AMOUNT balance BIGINT watermark } events { BIGINT seq PK BIGINT timestamp TEXT kind TEXT data TEXT dedup_key UK } migrations { TEXT name PK } accounts ||--|| account_head : "head -> current version" accounts }o--|| books : "book id" transfers ||--o{ transfer_accounts : "id = transfer_id" accounts ||--o{ transfer_accounts : "account_id + subaccount" transfers ||--o{ postings : "id = transfer_id" postings ||--o| live_postings : "live index copy" accounts ||--o{ balance_projection : "account + subaccount" ``` (The `_migrations` table is shown as `migrations`; Mermaid entity names cannot start with an underscore.) ## Accounts Append-only, versioned accounts with a head pointer. Owned by `account.rs` (`AccountStore`). See [ADR-0012 (subaccounts)](adr/0012-subaccounts.md) and [ADR-0020 (account transition recovery)](adr/0020-account-transition-recovery.md). ### `accounts` Every account version is an immutable row; a new version is appended, never updated in place. `metadata` is JSON. | Column | Type | Key | Purpose | |---|---|---|---| | `id` | `BIGINT` | PK | Base account id. | | `subaccount` | `BIGINT` | PK | Subaccount code (`0` = base). | | `version` | `BIGINT` | PK | Monotonic version; the chain is gap-free. | | `flags` | `INTEGER` | | `AccountFlags` bitfield (frozen/closed/inflight, `DEBIT_MUST_NOT_EXCEED_CREDIT`). | | `book` | `BIGINT` | | Owning book id. | | `metadata` | `TEXT` | | JSON key/value metadata. | - **Primary key**: `(id, subaccount, version)`. ### `account_head` One row per account pointing at its current version, so a lookup is a single indexed join instead of scanning the version chain. Maintained by delete+insert, never `UPDATE`. | Column | Type | Key | Purpose | |---|---|---|---| | `id` | `BIGINT` | PK | Base account id. | | `subaccount` | `BIGINT` | PK | Subaccount code. | | `version` | `BIGINT` | | The current version in `accounts`. | - **Primary key**: `(id, subaccount)`. ## Postings A posting is a signed amount of one asset owned by one (sub)account, identified by `(transfer_id, idx)`. The immutable `postings` record is the historical source of truth; one `live_postings` hot table carries a full row copy of the unspent set, so spendable reads never merge back to history. State is *derived* from `live_postings` membership: present = Live, in `postings` only = Spent, absent = Missing. Owned by `posting.rs` (`PostingStore`). See [ADR-0016 (immutable postings + index tables)](adr/0016-immutable-postings-index-tables.md), [ADR-0017 (full-row hot copies)](adr/0017-correctness-first-append-only-hot-indexes.md), [ADR-0022 (merged hot index)](adr/0022-merged-live-postings-hot-index.md), [ADR-0025 (one live set)](adr/0025-one-live-set-no-reservations.md). **Why one hot table with full-row copies?** The index originally held only ids in two tables ([ADR-0016](adr/0016-immutable-postings-index-tables.md)); [ADR-0017](adr/0017-correctness-first-append-only-hot-indexes.md) switched to full-row copies, [ADR-0022](adr/0022-merged-live-postings-hot-index.md) merged the active and reserved tables into one `live_postings`, and [ADR-0025](adr/0025-one-live-set-no-reservations.md) dropped the `reservation` column that had distinguished them. The hot read is "what can this account spend in this asset" (`get_postings_by_account`, `query_postings`, and the balance sum): carrying the data columns lets `live_postings` hold `idx_live_owner(owner, subaccount, asset)`, so a live read is one index scan on a small table with no join back to history and no `UNION` (the index *is* the table). The duplication is safe because the copied columns are immutable: a `live_postings` row is inserted and deleted but never updated, and `postings` rows never change, so a copy can never drift from its value row. And it is bounded and disposable: only the live set is duplicated (spent postings live in `postings` alone), and `live_postings` is rebuildable from `postings` and the transfer log, so a corrupt hot table is a drop-and-rebuild, not data loss. ### `postings` The immutable record. A row here that is absent from `live_postings` is Spent. | Column | Type | Key | Purpose | |---|---|---|---| | `transfer_id` | `TEXT` | PK | Creating transfer's id (hex). | | `idx` | `SMALLINT` | PK | Position within that transfer. | | `owner` | `BIGINT` | | Owning base account id. | | `subaccount` | `BIGINT` | | Owning subaccount code. | | `asset` | `INTEGER` | | Asset id. | | `value` | `NUMERIC(39,0)` / `BLOB` | | Signed `Cent`, stored natively: `NUMERIC(39,0)` on PostgreSQL, the 16-byte order-preserving encoding on SQLite. Read a SQLite one with `kuatia_i128_to_text(value)`. | - **Primary key**: `(transfer_id, idx)`. - **Index**: `idx_postings_owner (owner, subaccount, asset)`. ### `live_postings` The live-set hot copy. A posting is here while it is spendable, and an atomic commit deletes the consumed row directly (→ Spent), so membership is the whole of its state. Rebuildable from `postings` and the transfer log. | Column | Type | Key | Purpose | |---|---|---|---| | `transfer_id` | `TEXT` | PK | Posting id (hex). | | `idx` | `SMALLINT` | PK | Position within the transfer. | | `owner` | `BIGINT` | | Owning base account id. | | `subaccount` | `BIGINT` | | Owning subaccount code. | | `asset` | `INTEGER` | | Asset id. | | `value` | `NUMERIC(39,0)` / `BLOB` | | Signed `Cent`, stored natively: `NUMERIC(39,0)` on PostgreSQL, the 16-byte order-preserving encoding on SQLite. Read a SQLite one with `kuatia_i128_to_text(value)`. | - **Primary key**: `(transfer_id, idx)`. - **Index**: `idx_live_owner (owner, subaccount, asset)`. ## Transfers Committed envelope records and the account index that finds them. Owned by `transfer.rs` (`TransferStore`). ### `transfers` | Column | Type | Key | Purpose | |---|---|---|---| | `id` | `TEXT` | PK | Content-addressed envelope id (hex). | | `transfer` | `TEXT` | | The `Envelope` as JSON. | | `receipt` | `TEXT` | | The `Receipt` as JSON. | | `created_at` | `BIGINT` | | Unix millis when the ledger committed this entry (default `0`). Never caller-supplied: the balance cache folds its tail against it (ADR-0019). | | `book` | `BIGINT` | | Owning book id (default `0`). | | `value_date` | `BIGINT` | | Days since 1970-01-01 for the day this entry takes effect; `NULL` means the day it was committed. A filterable projection of `Envelope.value_date`, written at commit and never read back (ADR-0027). | - **Primary key**: `id`. - **Indexes**: `idx_transfers_created_at (created_at)`, `idx_transfers_book (book)`, `idx_transfers_value_date (value_date)`. ### `transfer_accounts` The account -> transfer index; the caller supplies the involved set, the store does no computation. | Column | Type | Key | Purpose | |---|---|---|---| | `transfer_id` | `TEXT` | PK | The transfer id (hex). | | `account_id` | `BIGINT` | PK | An involved base account id. | | `subaccount` | `BIGINT` | PK | The involved subaccount code. | - **Primary key**: `(transfer_id, account_id, subaccount)`. - **Index**: `idx_xfer_acct (account_id, subaccount)`. ## Ledger plumbing The `sagas` write-ahead table was dropped in migration `010_drop_sagas`: a commit and an account transition are now single atomic store transactions (ADR-0023, superseding ADR-0003), so there is no half-applied state for a write-ahead record to recover. ### `events` The append-only ledger event log, idempotent on the dedup key. Owned by `event.rs` (`EventStore`). See [ADR-0010 (event stream vs transfer log)](adr/0010-event-stream-vs-transfer-log.md). | Column | Type | Key | Purpose | |---|---|---|---| | `seq` | `BIGINT` | PK | Monotonic sequence number. | | `timestamp` | `BIGINT` | | Unix millis of the event. | | `kind` | `TEXT` | | Event kind tag (JSON). | | `data` | `TEXT` | | The full `LedgerEvent` as JSON. | | `dedup_key` | `TEXT` | UNIQUE | Stable key; a replayed event returns the existing `seq`. | - **Primary key**: `seq`. **Unique**: `dedup_key`. ### `books` Book definitions (asset/account/flag policy) as JSON. Owned by `book.rs` (`BookStore`). See [ADR-0013 (journaling model)](adr/0013-journaling-model.md). | Column | Type | Key | Purpose | |---|---|---|---| | `id` | `BIGINT` | PK | Book id. | | `name` | `TEXT` | | Human-readable name. | | `data` | `TEXT` | | The `Book` (with its `BookPolicy`) as JSON. | - **Primary key**: `id`. ### `balance_projection` Append-only balance cache points: each row snapshots one `(account, subaccount, asset)` balance at a commit-time watermark. Rows are only inserted; a read picks the highest-id row at or before a watermark. A derived, rebuildable accelerator, never authoritative. Owned by `projection.rs` (`BalanceProjectionStore`). See [ADR-0019](adr/0019-cached-balance-projection.md). | Column | Type | Key | Purpose | |---|---|---|---| | `id` | `BIGINT` | PK | Rust-minted monotonic id (tie-breaker for equal watermarks). | | `account` | `BIGINT` | | Base account id. | | `subaccount` | `BIGINT` | | Subaccount code. | | `asset` | `INTEGER` | | Asset id. | | `balance` | `NUMERIC(39,0)` / `BLOB` | | The cached `Cent`, stored natively (see `postings.value`). | | `watermark` | `BIGINT` | | Commit-time watermark (unix millis) this snapshot covers. | - **Primary key**: `id`. - **Index**: `idx_balance_projection_closest (account, subaccount, asset, watermark, id)`. ### `_migrations` The applied-migration ledger. Created in `migrate.rs` (not a `.sql` file); a migration whose `name` is present is skipped, making `migrate()` idempotent. | Column | Type | Key | Purpose | |---|---|---|---| | `name` | `TEXT` | PK | The migration name (e.g. `011_native_amounts`). | - **Primary key**: `name`. ## SQLite money functions SQLite has no 128-bit integer, so an amount is a 16-byte `BLOB` and three registered functions do the work the type system would otherwise do. They are installed by `kuatia_storage_sql::install_drivers()`, which callers use in place of `sqlx::any::install_default_drivers()` and **must call before connecting a pool**: registration goes through `sqlite3_auto_extension`, so a connection opened earlier does not have them. `migrate()` checks for them on SQLite and returns `StoreError::Misconfigured` rather than a bare "no such function". | Function | Signature | Purpose | |---|---|---| | `kuatia_i128_sum` | `BLOB` → `BLOB` (aggregate) | Exact sum of an amount column. Folds with `i128::checked_add` and raises on overflow or on an argument that is not a 16-byte amount; an empty group sums to zero, not `NULL`. The PostgreSQL equivalent is plain `SUM()`. | | `kuatia_i128_from_text` | `TEXT` → `BLOB` | Decimal string to the stored encoding. Used by `011_native_amounts` to convert the old text columns. | | `kuatia_i128_to_text` | `BLOB` → `TEXT` | The stored encoding back to a decimal string, for tooling built on this crate. | The encoding is two's-complement big-endian with the sign bit flipped, so SQLite's byte-wise `BLOB` comparison orders amounts numerically: `i128::MIN` is all zeros, `0` is `0x80` followed by zeros, `i128::MAX` is all `0xFF`. It is deliberately not the content-addressed hash encoding (`Cent::to_canonical_bytes`), so the two cannot be confused. These functions live in the process that registers them, so a **plain `sqlite3` CLI does not have them**: it sees a blob. To read one by hand there, take `hex(value)`, subtract `0x80` from the leading byte, and read the result as a 128-bit big-endian two's-complement integer. This is the one place the schema gives up on being legible without tooling, and it is the price of an amount column the database can sum and order (ADR-0024).