Skip to content

Database Schema v1

Documentation · Source: migrations/0001_ledger_schema.up.sql · Step: 1.08

The database is the last line of defence. Rust checks every rule first (crates/core), but a bug in the engine, a manual SQL session or future code can bypass Rust. So the database enforces the core invariants again, on its own, with constraints, triggers and role privileges.

Table One row per Notes
currencies ISO 4217 currency The same 11 currencies as crates/core/src/currency.rs, with their exponent
accounts Account Chart code and sub-account, type, currency, status, allow_negative, running totals (debits_total, credits_total), version
journal_entries Posted entry sequence (posting order), client ID and idempotency key, reverses_entry_id, metadata, event_time, recorded_time
journal_lines Line of an entry Account, side, amount, currency, and the account’s totals right after the line (debits_after, credits_after)
idempotency_records Successfully posted request Client, idempotency key, SHA-256 fingerprint of the request, the entry it created (migration 0002, step 1.11)
reason_codes Reason a correction may give Code, the kind of entry it is for (reversal or adjustment), description; the same list as REASON_CODES in crates/core (migration 0003, step 1.12)
verification_runs Verification run Scope (full or incremental), the entries it covered (from_sequence < sequence ≤ to_sequence), duration, lines checked, result and findings (migration 0004, step 1.13)
verification_checkpoints Account at a clean run’s checkpoint Each account’s totals as of that run’s to_sequence, the starting point for the next incremental run
holds Hold The account it holds from, its own held account, amount and currency, captured, status (active, captured, released, expired), expires_at, the entry that placed it and the entry that ended it (migration 0005, step 1.14)
periods Accounting period Label, status (open, closing, closed), the entry range it holds (after_sequence < sequence ≤ last_sequence), when it opened, stopped taking postings and closed (migration 0007, step 2.05)
period_snapshots Account at the end of a closed period The account’s totals from every line up to the period’s last_sequence; append-only
refund_tallies Account of a refunded entry What the entry’s refunds have moved back on that account (refunded) and the most they may (limit_amount); it only grows (migration 0006, step 2.04)
adjustment_requests Adjustment asked for through the API Client and idempotency key, status (pending, approving, approved, rejected), reason code, the requested lines, who asked and when, who decided and when, and the entry an approval posted (migration 0008, step 2.07)
clients Client platform Name, status (active, disabled), rate limit (rate_per_second, burst); a disabled client never becomes active again (migration 0009, step 2.08)
client_credentials Credential of a client Its ID is the OAuth client_id; the actor, the roles, the Argon2id hash of the secret, when it was revoked. Only revoking it ever changes it
access_tokens Access token issued The SHA-256 of the token, its credential, its scopes and when it expires; deleted once expired
entry_chain Entry, in the hash chain Its position (commit order), the previous hash, its own hash, and the receipt: the key ID and the Ed25519 signature (migration 0010, step 2.09)
audit_events Audit event When, the client and actor, the action (METHOD /route or a named action), the target, the request ID, the outcome, the details, and the chain’s previous hash and own hash
checkpoints Checkpoint at a period’s close Both chains’ heads (position and hash), when, and the signature
signing_keys Public signing key The key ID and the Ed25519 public key; retired keys stay
chain_heads Chain Its last position and hash; one row each for entries and audit, the only rows that change
recon_runs Statement import or reconciliation run Its kind (import, external, internal, offline), the account or client it covered, its clock (as_of), what it found (items, matched, pending, breaks new, cleared and still open) and its runtime; recorded once, when it ends (migration 0014, step 2.15)
recon_items Bank statement line The account, the bank’s line ID (unique per account), booking date, signed amount, currency, reference and SHA-256 of the line; the ledger line it matched, by which rule and run, set once
recon_breaks Disagreement found Its kind, a subject stable across runs (one open break per subject), the statement line, ledger line, device or voucher it is about, the other record’s amount, the ledger’s and the difference; its status (open, cleared, resolved, explained) and how it closed

Since migration 0003, every journal entry also has a kind (standard, reversal, adjustment or, since migration 0006, refund), and corrections record reason_code, requested_by and approved_by. A refund records the entry it refunds in refunds_entry_id. Since migration 0007, every new entry records its accounting period in period_id. Since migration 0008, an account opened through the API records the client that opened it (opened_by) and the idempotency key it used (open_key), unique together, so a retried open returns the same account; neither ever changes (MZ005).

Amounts are BIGINT minor units (ADR-0005). IDs are UUID (ADR-0006). Status, type and side are TEXT with a CHECK, not PostgreSQL enums, because adding a value to an enum later is harder to migrate.

Constraints and the invariants they protect

Section titled “Constraints and the invariants they protect”

This is the step 1.08 exit gate: every constraint names its invariant (invariant catalogue).

Constraint Kind Protects Test (crates/engine/tests/schema.rs)
journal_lines.amount > 0 CHECK INV-1: amounts are positive line_amounts_must_be_positive
journal_entries_balanced, journal_lines_balanced → mezgeb_check_entry_balanced() Deferred constraint trigger INV-1: at least 2 lines, debits = credits per currency, checked at commit. INV-4: an entry without lines cannot commit unbalanced_entries_fail_at_commit
journal_lines.entry_id → journal_entries Foreign key INV-4: no line without its entry lines_reference_their_entry_and_account_currency
journal_lines_account_currency_fk → accounts (id, currency) Foreign key INV-3: a line is in its account’s currency lines_reference_their_entry_and_account_currency
accounts_not_negative CHECK INV-8: no balance below zero on the normal side unless allow_negative a_wallet_cannot_go_negative
accounts.debits_total >= 0, credits_total >= 0 CHECK INV-5, INV-8: totals stay in range a_wallet_cannot_go_negative
accounts_guard → mezgeb_accounts_guard(): totals never decrease Trigger INV-5, INV-9: totals only ever add posted lines totals_only_increase
journal_entries_append_only, journal_lines_append_only, *_no_truncate → mezgeb_reject_change() Triggers INV-9: history is never updated, deleted or truncated, even by the owner journal_history_cannot_change
Role privileges: mezgeb_app has no UPDATE, DELETE or TRUNCATE on the journal Privileges INV-9, second layer journal_history_cannot_change
journal_entries_idempotency_unique (client_id, idempotency_key) UNIQUE INV-13: an idempotency key is used once per client idempotency_keys_are_unique_per_client
idempotency_records primary key (client_id, idempotency_key) PRIMARY KEY INV-13: claimed before any account is locked; a concurrent request with the same key waits on it fifty_concurrent_repeats_post_once (tests/idempotency.rs)
idempotency_records_entry_fk → journal_entries (id, client_id, idempotency_key), deferred Foreign key INV-13: a record and its entry always share client and key ten_sequential_repeats_post_once
idempotency_records_append_only → mezgeb_reject_change(); mezgeb_app has no UPDATE or DELETE Trigger, privileges INV-9, INV-13: a record never changes idempotency_records_are_append_only
journal_entries_authorization CHECK INV-14, INV-25: a standard entry has no reason or actors; a reversal has a reason and a requester (and an approver, if any, is someone else); an adjustment has a reason, a requester and a different approver adjustments_need_two_different_people (tests/corrections.rs)
journal_entries_reason_fk → reason_codes (code, entry_kind) Foreign key A reason exists and is meant for this kind of entry adjustments_need_two_different_people
journal_entries.reverses_entry_id UNIQUE UNIQUE INV-14: an entry is reversed at most once reversal_rules
journal_entries_reversal_target → mezgeb_check_reversal_target() Trigger INV-14: a reversal is never reversed reversal_rules
journal_entries_reversal_mirror → mezgeb_check_reversal_mirror() Deferred constraint trigger INV-14: a reversal mirrors its original exactly reversal_rules
journal_lines_account_open → mezgeb_check_line_account_open() Trigger INV-24: lines post only to open accounts account_status_rules
accounts_closed_at_zero CHECK Account lifecycle: an account closes only at zero balance account_status_rules
accounts_guard: a closed account never reopens; identity columns never change Trigger Account lifecycle; INV-8 (allow_negative cannot be switched on later) account_status_rules, the_app_role_cannot_change_account_identity
Role privileges: mezgeb_app may update only totals, version and status Column privileges Account identity cannot be changed by the service the_app_role_cannot_change_account_identity
journal_entries.sequence identity Identity column INV-12: posting order a_valid_posting_commits
accounts_reference_unique (code, sub_account), NULLS NOT DISTINCT UNIQUE One account per chart reference covered by the account helpers
Format CHECKs on codes, sub-accounts, idempotency keys, metadata CHECK Validation repeated from crates/core idempotency_keys_are_unique_per_client
holds_guard → mezgeb_holds_guard() Trigger INV-16: a hold’s status changes only from active, once; only status, captured and the closing entry may change the_database_guards_holds (tests/holds.rs)
holds_closed_by_entry: active ⇔ no closing entry CHECK INV-16: every ended hold names the entry that ended it the_database_guards_holds
holds.captured between 0 and amount; holds_captured_only_when_captured CHECK INV-17: a capture never exceeds the hold the_database_guards_holds
holds.held_account_id UNIQUE; holds_distinct_accounts UNIQUE, CHECK INV-18: each hold has its own held account, never the account it holds from hold_operations_are_idempotent, held_accounts_are_protected_on_postgresql
holds.place_entry_id, close_entry_id UNIQUE → journal_entries UNIQUE, foreign key A hold’s entries are real, and each belongs to one hold the_database_guards_holds
holds_no_delete → mezgeb_reject_change(); mezgeb_app may update only status, captured and close_entry_id Trigger, column privileges INV-9, INV-16: holds are never deleted, and their terms never change the_database_guards_holds

Invariants marked “DB” that later steps add

Section titled “Invariants marked “DB” that later steps add”

Their tables do not exist yet. Each step adds the constraint and its test.

Invariant What the database will enforce Step
INV-10 No entry in a closed accounting period 2.05
INV-12 recorded_time strictly increasing under concurrency: measured in step 1.10 (no tie or inversion in 1,000 parallel postings). Since step 2.09 the hash chain gives every entry a single commit order (entry_chain.position); recorded_time itself is still assigned at insert, before the chain’s lock, so it is measured, not guaranteed 3.03
INV-19, INV-20 Voucher escrow cap; one settlement per (device_id, counter) 2.12

How a posting uses its key (ADR-0008, step 1.11):

  1. In the posting’s transaction, before any account is locked, the key is claimed with INSERT … ON CONFLICT DO NOTHING on idempotency_records.
  2. If the key was free, the posting goes ahead; the record commits with the entry, or both roll back. A refused posting therefore never uses up its key.
  3. If the key is taken, the stored fingerprint is compared with the request’s: the same fingerprint returns the original entry, a different one is refused (IdempotencyKeyReused). Nothing is written either way.
  4. If another transaction holds the key and has not finished, PostgreSQL makes this one wait on the primary key, then applies step 3.

The response is rebuilt from the stored entry. Entries are immutable (INV-9), so it is always identical; no separate copy is kept that could drift.

Retention. Records are kept as long as their entry, which is forever: a key stays unique per client permanently, so a late retry, however late, can never post twice. That costs about 100 bytes per entry. Archival of old records, if storage ever requires it, is decided with the journal’s own archival (step 3.04), and must keep keys unique for at least the longest client retry window.

mezgeb-cli verify (or mezgeb_engine::verify) proves that the stored balances equal the journal (step 1.13, ADR-0012). In one consistent snapshot it checks:

Check Invariant Reported with
Each entry balances per currency INV-1 the entry
Each entry has at least two lines; no line lacks its entry INV-1, INV-4 the entry, or the entry and line
Each line’s stored totals continue its account’s chain INV-6 the account, and the first line where the chain breaks
Each account’s totals equal its checkpoint plus every line after it INV-5 the account, stored and expected totals
All debits equal all credits per currency INV-2 the currency
Each held account holds its active hold’s amount, or nothing once the hold has ended INV-18 the hold and its held account

Full and incremental. A full run checks every entry. An incremental run checks the first three only for entries after the last clean run’s checkpoint, and the last two for every account. A clean run stores each account’s totals at its checkpoint; a dirty run does not, so the next run starts at the same place. An incremental run does not re-check lines it has already verified: tampering with an old line is found by a full run (run one regularly) and, from step 2.09, by the hash chain.

No entry is skipped. A run checkpoints only entries recorded at least 60 seconds before it starts (--settle-seconds), and the service role mezgeb_app has transaction_timeout = 30s. An entry’s sequence number is taken when it is inserted, so any entry with a lower sequence was inserted earlier, and its transaction has finished, committed or rolled back, by the time the checkpoint is taken. No entry can commit late below a checkpoint.

Recorded. Every run is a row in verification_runs, with its findings; both verification tables are append-only.

A hold reserves money by a real posting into its own held account, such as 2020:wd-0001 (ADR-0004, step 1.14). It then ends exactly once:

Operation Entry Status
Place account → held account active
Capture (full or partial) held account → destination, and the remainder back to the account, in one entry captured
Release held account → account released
Expire (once expires_at has passed) held account → account expired

Each operation is one transaction that locks the hold’s row (SELECT … FOR UPDATE), is idempotent like any posting, and tags its entry with the metadata hold and hold_action. Ordinary postings, adjustments and reversals that touch a held account are refused (AccountIsHeld): only the hold’s own entries move money in and out of it.

The expiry worker (expire_due_holds) takes due holds oldest first from the partial index holds_due_idx, claiming each with FOR UPDATE SKIP LOCKED and expiring it in its own transaction under the idempotency key hold-expire:<hold id>.

  • Several workers can run at once. A hold that a capture or release has locked is skipped, not waited for. Once that commits the hold is no longer active; if it rolls back, the hold is due again on the next run.
  • A capture racing an expiry has exactly one winner. Whichever locks the row first ends the hold; the other finds it no longer active.
  • A worker that dies mid-batch is simply run again. What it committed is done, and the rest is still due. The key, the entry and the status change commit together, so no hold is expired twice.

A refund names the standard entry it refunds (refunds_entry_id) and is capped by it, account by account (INV-15, ADR-0015, step 2.04):

  1. The engine locks the original’s tallies. On a refund, it creates the original’s refund_tallies rows if needed (INSERT … ON CONFLICT DO NOTHING, one per account, with the limit taken from the original’s lines) and locks them, in account order, with FOR UPDATE. Concurrent refunds of one entry wait here, so each sees what the one before it gave back. Refunds of different entries do not touch each other’s rows.
  2. It checks the refund with RefundLimits, the same rule the reference ledger uses, after re-reading whether the original was reversed while it waited.
  3. It posts the refund and updates the tallies in the same transaction.

A reversal takes the same tally locks before it checks that the entry has no refunds, so a refund and a reversal of one entry never both succeed.

The database guards the rule on its own: refund_tallies refuses a refunded above limit_amount (23514), a tally that shrinks or changes anything else (MZ007), and any delete. At commit, the deferred trigger journal_entries_refund_rules checks every refund again from the journal alone (it names an unreversed standard entry, its lines move each account the allowed way, all refunds of the original stay within it), and refuses a reversal of a refund or of a refunded entry (MZ007).

Every entry belongs to the period that was open when it was posted (INV-10, ADR-0016, step 2.05). The operator’s procedure is the close runbook.

  • One open period. A partial unique index allows one open and one closing period. A period only moves open → closing → closed, and nothing else about it changes (MZ008); periods are never deleted.
  • The switch. Each posting takes pg_advisory_xact_lock_shared on the period lock, then reads the open period, and holds the lock until it commits. begin_close takes the lock exclusively: it waits for postings under way, records the period’s last_sequence, marks it closing and opens the next one, in one transaction.
  • Into an open period only. A trigger refuses an entry or a line whose period is not open when it is written, and a deferred trigger checks the entry again at commit (MZ008). Entries posted before migration 0007 have no period.
  • Snapshots. finish_close verifies the closing period in one REPEATABLE READ transaction, then writes period_snapshots and marks the period closed. Snapshots are append-only.

Who may call the ledger (ADR-0017, migration 0009, step 2.08; the operator’s procedure is the clients and credentials runbook).

  • Secrets and tokens are never stored. A credential keeps the Argon2id hash of its secret; a token, its SHA-256.
  • A token is valid while it has not expired and its credential is not revoked and its client not disabled: the service checks all three on every request, so revoking or disabling takes effect at once.
  • Separation of duties. A CHECK refuses a credential with both the operator and approver roles, and one with a role outside the six of the permission matrix.
  • Only forward, never deleted. A credential changes only by being revoked, once; a client only by its status and rate limit, and a disabled client stays disabled (MZ010). Neither is ever deleted.
  • Privilege separation. Only mezgeb_admin creates clients and credentials or revokes them. The service (mezgeb_app) reads them, to check a secret or a token, and inserts and deletes tokens: a compromised service cannot mint itself a credential.

Silent changes by someone with full database access are made detectable (ADR-0018, migration 0010, step 2.09; the operator’s procedure is the tamper evidence runbook).

  • The entry chain (INV-11). Every posting ends, in its own transaction, by locking the entries head, hashing the entry, its lines and the previous hash (mezgeb-core::chain), and appending the link to entry_chain. Chain order is commit order. Changing any entry, line or link breaks every hash after it.
  • Receipts (INV-26). Each link is signed with the ledger’s Ed25519 key: that signature is the entry’s receipt, verifiable with the public key in signing_keys alone. A foreign key ensures every signature names a published key.
  • The audit chain. Audit events are chained the same way, on the audit head.
  • Checkpoints. At each close, both heads are signed into checkpoints and exported to files kept outside the database. A deleted or rewritten tail disagrees with a checkpoint someone else holds.
  • Append-only. The chains, checkpoints and keys are never changed or deleted (mezgeb_reject_change); only chain_heads moves.
  • Before the chain. Entries posted before migration 0010 have no link; verify --chain counts them, and any entry after the chain began that has no link is a finding.

The cost: postings serialise on the entries head from their last step to their commit (see What one posting costs).

Payer devices and the vouchers that reserve their offline spending (ADR-0019, migration 0011, step 2.11; the protocol is offline protocol v1, the operator’s procedure the offline runbook).

  • One escrow per voucher. Issuing a voucher opens its own 2050:vch-… account and posts E16 into it, in the same transaction as the signed voucher row. vouchers.escrow_account_id and entry_id are unique.
  • One open voucher per device (ADR-0019). A partial unique index on vouchers (device_id) over active, grace and frozen.
  • Limits only lowered. offline_tier_limits holds a client’s own limits per tier; a CHECK keeps them at or below the protocol’s maxima. Without a row, tiers A and B have the maxima and tier C is disabled.
  • Keys. A device’s compressed public key and its key ID are unique. offline_issuer_keys holds the public half of each issuer key, append-only; the secret key is never in the database.
  • Serialised where it matters. Issuing locks the device row (one issue at a time per device), then the owner’s wallet (one at a time per customer, so the 5,000 ETB cap holds across their devices).
  • Only states move. The service may update a device’s status and next counter, and a voucher’s status, and nothing else; mezgeb_admin may change a device’s status and a client’s tier limits.

The invariant, each escrow holds exactly its voucher’s unspent value, is checked by mezgeb-cli offline check.

Each uploaded receipt and what became of it (migration 0012, step 2.12; protocol §9).

  • One record per (device_id, counter), written the first time the receipt arrives, whatever its outcome. A later upload with the same digest is a DUPLICATE; with another digest, a CONFLICT (INV-20, INV-21). Refusals (BAD_*, UNKNOWN_*) are never stored.
  • The evidence. A second, different receipt for a recorded counter goes to offline_receipt_conflicts, append-only.
  • Paid exactly when settled. A CHECK ties a settled outcome to its entry and settlement time, and the escrow, wallet and loss-account parts to the amount.
  • Only the outcome moves. A trigger (MZ011) refuses any change to a settled receipt or to a signed field, and every delete.
  • One device at a time. Ingestion locks the device row first, so the uploads of one device queue, and the same receipt uploaded many times at once settles once. The settlement entry’s idempotency key is offline:<device_id>:<counter>.
  • The fee. offline_settings.fee_bps is a client’s merchant fee on offline payments; without a row, none.

Offline expiry, late receipts and disputes

Section titled “Offline expiry, late receipts and disputes”

The rest of a voucher’s life (migration 0013, step 2.13; protocol §7, §11, §12, §14).

  • The return is recorded. A voucher’s returned_at, return_entry_id (E18, E25 or E27) and ended_at; CHECKs tie them to its status.
  • Shortfalls. offline_shortfalls keeps what a late receipt’s payer could not pay, paid by 5030 (E29), until it is repaid (E30). An index finds a wallet’s unpaid ones: any unpaid shortfall blocks the owner’s next voucher.
  • Disputes. A freeze opens one row in offline_disputes; a partial unique index allows one open case per voucher. Resolving it sets the resolution, who decided and when.
  • Conflicting copies paid by a dispute are recorded in offline_dispute_payments, append-only, since they have no row of their own in offline_receipts.
  • The late deadline. offline_settings.late_deadline_days, 1 to 365, 90 by default.
  • One device at a time, again. The tick and a dispute’s resolution take the device’s and the voucher’s row locks in the same order as receipt ingestion, so a receipt racing the expiry has one outcome.

Reconciliation reads the ledger and writes only its own three tables (migration 0014, step 2.15, ADR-0020). The service and mezgeb-cli recon cannot change a journal row through them.

  • History never changes. A run is inserted once, when its counts are known, and never updated or deleted. A statement line’s content never changes; only its match is set, once. A break’s facts never change; only an open break’s status moves, once, to cleared, resolved or explained (MZ001 otherwise, for every role, including the owner).
  • One open break per subject. A partial unique index on subject where status = 'open'; a run inserts with ON CONFLICT DO NOTHING, so the same disagreement is never raised twice.
  • A ledger line matches once. UNIQUE (matched_entry_id, matched_line_no); an entry resolves at most one break (resolution_entry_id is unique). Keys point at journal_entries, not journal_lines, so the journal’s own guard is what refuses a TRUNCATE of the lines.
  • A run’s rows refer to it before it exists. The foreign keys to recon_runs are checked at commit (DEFERRABLE INITIALLY DEFERRED): the run is inserted last, in the same transaction.
  • One run of a scope at a time. Imports and runs of an account take a transaction-scoped advisory lock on it; offline runs and each client’s cache comparisons take their own.
  • What a device reported. vouchers.reported_last_counter keeps the last counter the device gave when it asked for the voucher, for the offline reconciliation; NULL for vouchers issued before step 2.15.

An adjustment is requested by one person and approved by another (INV-25, step 1.12). Through the API these are two calls, so the request waits in adjustment_requests (migration 0008, step 2.07).

  • Two people. A CHECK refuses a decision by the person who asked (23514).
  • Along its states only. A request moves pending → approving → approved, or pending → rejected, and only its decision columns change (MZ009). Requests are never deleted.
  • Approval survives a crash. Approving marks the request approving under a row lock, posts the adjustment with the idempotency key adjustment-{id}, then marks it approved with the entry. If the service stops in between, the next approval finishes it, with the checker who started it, and posts nothing new: the key returns the entry already posted.
  • A refused approval posts nothing. If the ledger refuses the lines (an account would go negative, or is no longer open), the request goes back from approving to pending, undecided, to be approved later or rejected.

Every write is one transaction (step 1.15). What a crash leaves behind depends only on whether COMMIT returned:

  • Before COMMIT: nothing. PostgreSQL rolls the transaction back, whether the process died, the connection dropped or the server was killed. Lines, totals, the idempotency record and any hold change are gone together.
  • After COMMIT: everything. The commit is in the WAL on disk, and crash recovery replays it on the next start.
  • In between, the client may not know which. The commit can succeed and the answer still be lost. The client retries with the same idempotency key: if the first attempt committed, it gets the original entry back; if not, the retry posts it once.

This was tested at four points inside every write (FaultPoint, with the engine’s test-only test-hooks feature), by killing PostgreSQL with SIGKILL under load 20 times (scripts/chaos.sh), and with the simulator (mezgeb-sim). See the 1.15 evidence.

Measured with pg_stat_statements during the step 1.16 baseline (mezgeb-bench --statements). An entry of n lines on a accounts runs, in one transaction:

Statements What
1 Lock the accounts: SELECT … FOR NO KEY UPDATE, in account-id order
a Update each account’s totals
1 Insert the entry
1 Insert the lines, all in one statement
1 Insert the idempotency record
— PostgreSQL’s own foreign-key checks: each line’s entry and account, and the idempotency record’s entry
n + 1, × 2 At COMMIT: the deferred balance check (journal_entries_balanced, journal_lines_balanced)

The deferred balance check runs once per row, for the entry and for each line, and each run checks the whole entry: 3 times for a 2-line entry, 5 times for a 4-line one. Each run is cheap (about 0.03 ms), but the work grows with the number of lines. Checking once per entry is a possible optimisation for step 3.01.

On a hot account, the time is in the lock queue, not the work. With 16 connections posting to one account, the lock SELECT took 40–45 ms on average while the rest of the posting took under 2 ms.

Since step 2.09, every posting also takes the chain head (ADR-0018): one row lock for the whole ledger, held from the posting’s last step to its commit, so commits no longer share a disk flush. Measured on the development machine (Docker Desktop on Windows), 2-line entries fell from 135 to 71 per second between uniform accounts and from 52 to 41 on one hot account. The slow disk flush of that machine makes the serial step long; a server with fast disks loses less. Step 3.03 takes it up.

Triggers raise Mezgeb’s own SQLSTATE codes so callers and tests can tell exactly which rule fired. Standard codes are used for standard constraints.

SQLSTATE Meaning
MZ001 The journal is append-only (INV-9)
MZ002 Entry unbalanced or with fewer than 2 lines (INV-1, INV-4)
MZ003 Invalid reversal (INV-14)
MZ004 Line posted to an account that is not open (INV-24)
MZ005 Forbidden account change: identity column, reopening, totals going down
MZ006 Forbidden hold change: an ended hold changed, or a change other than ending it (INV-16)
MZ007 Invalid refund, a refund tally changed other than by growing, or a reversal of a refund or of a refunded entry (INV-15)
MZ008 Posting into a period that is not open, or a forbidden period change (INV-10)
MZ009 Forbidden adjustment request change: a field other than the decision, or a step backwards (INV-25)
MZ010 Forbidden client or credential change: anything but revoking a credential, or enabling a disabled client
MZ011 Forbidden offline receipt change: a settled receipt, a signed field, or a delete (INV-20)
23514 A CHECK constraint failed
23505 A UNIQUE constraint failed
23503 A foreign key failed
42501 The role lacks the privilege

Roles are created once per PostgreSQL cluster, before migrations, by db/init/01-roles.sql.

Role Used by Privileges
mezgeb_owner Migrations only Owns the schema. Never used by the running service.
mezgeb_app The ledger service SELECT and INSERT on accounts and the journal; UPDATE on accounts.debits_total, credits_total, version and status, on refund_tallies.refunded, on a period’s status and end, and on an adjustment request’s decision, only; SELECT on clients and credentials, and SELECT, INSERT and DELETE on access tokens; SELECT and INSERT on the chains, checkpoints and signing keys, and UPDATE on the chain heads; SELECT and INSERT on devices, vouchers, issuer keys, offline receipts and their conflicts, UPDATE on their status columns, a device’s next counter and a receipt’s outcome and payment, and SELECT on tier limits and offline settings; SELECT and INSERT on the reconciliation tables, UPDATE on a statement line’s match and a break’s status. No DELETE on anything else.
mezgeb_readonly Reports, reconciliation, support SELECT only
mezgeb_verifier mezgeb-cli verify, mezgeb-cli recon SELECT on the ledger; SELECT and INSERT on the verification and reconciliation tables, and UPDATE on a statement line’s match and a break’s status; nothing else (separation of duties, threat TM-15)
mezgeb_admin mezgeb-cli client, mezgeb-cli credential Creates clients and credentials, disables and revokes them, deletes tokens, sets clients’ offline tier limits, fee and devices’ status, and records those changes as audit events; nothing in the ledger (steps 2.08, 2.11). Created by db/init/03-admin.sql; a database created before step 2.08 needs that file run once

mezgeb_app also has transaction_timeout = 30s, which verification relies on (see Verification). Both are set in db/init/02-verifier.sql; a database created before step 1.13 needs that file run once.

The triggers apply to every role, including the owner. Privileges are a second, independent layer for the service.

Terminal window
docker compose up -d # PostgreSQL 18 on port 55432, roles created on first start
MEZGEB_TEST_DATABASE_URL=postgres://postgres:postgres@localhost:55432/postgres \
cargo test -p mezgeb-engine
docker compose down # stop and delete the data

Each database test creates its own throwaway database, owned by mezgeb_owner, runs the migrations as mezgeb_owner, and then acts as mezgeb_app. The superuser URL is used only to create and drop those databases. Without the variable the tests are skipped locally; on CI they must run.

Migrations are SQL files in migrations/, applied with SQLx. Each has an up and a down file, and a test runs them up, down and up again on an empty database. Once a migration has been applied anywhere outside development, it is never edited: a change is a new migration.