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:
transfer_id + idx; account_head points at
the current accounts version; the account/transfer link is an explicit index
table. See ADR-0003.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.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.BIGINT. No AUTOINCREMENT / SERIAL; snowflake
ids come from AutoId. See ADR-0015.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-0017 (append-only hot indexes), ADR-0022 (merged live-postings hot index), ADR-0019 (cached balance projection), and ADR-0008 (conformance-tested storage).
Edges are logical only (no FOREIGN KEY constraint exists); the labels name
how the ledger relates the rows in Rust.
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.)
Append-only, versioned accounts with a head pointer. Owned by account.rs
(AccountStore). See ADR-0012 (subaccounts) and
ADR-0020 (account transition recovery).
accountsEvery 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. |
(id, subaccount, version).account_headOne 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. |
(id, subaccount).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-0017 (full-row hot copies),
ADR-0022 (merged hot index),
ADR-0025 (one live set).
Why one hot table with full-row copies? The index originally held only ids in
two tables (ADR-0016);
ADR-0017 switched to
full-row copies, ADR-0022 merged the
active and reserved tables into one live_postings, and
ADR-0025 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.
postingsThe 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). |
(transfer_id, idx).idx_postings_owner (owner, subaccount, asset).live_postingsThe 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). |
(transfer_id, idx).idx_live_owner (owner, subaccount, asset).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). |
id.idx_transfers_created_at (created_at), idx_transfers_book (book), idx_transfers_value_date (value_date).transfer_accountsThe 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. |
(transfer_id, account_id, subaccount).idx_xfer_acct (account_id, subaccount).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.
eventsThe append-only ledger event log, idempotent on the dedup key. Owned by
event.rs (EventStore). See ADR-0010 (event stream vs transfer
log).
| 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. |
seq. Unique: dedup_key.booksBook definitions (asset/account/flag policy) as JSON. Owned by book.rs
(BookStore). See ADR-0013 (journaling model).
| Column | Type | Key | Purpose |
|---|---|---|---|
id |
BIGINT |
PK | Book id. |
name |
TEXT |
Human-readable name. | |
data |
TEXT |
The Book (with its BookPolicy) as JSON. |
id.balance_projectionAppend-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.
| 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. |
id.idx_balance_projection_closest (account, subaccount, asset, watermark, id)._migrationsThe 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). |
name.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).