Skip to content

1.09 Atomic Posting with SQLx: Exit Evidence

Evidence · Step: 1.09 Atomic Posting with SQLx

Step 1.09 Atomic Posting with SQLx
Date 2026-11-12
Database PostgreSQL 18.6 (postgres:18-alpine)
Result Passed
Deliverable File Contents
Posting crates/engine/src/post.rs post_entry(conn, PostRequest) and reverse_entry, each one transaction
Store crates/engine/src/store.rs PgStore, which implements the same Ledger trait as MemLedger
Rows crates/engine/src/rows.rs Rows to mezgeb-core types (a stored account that breaks the account rules is refused), and SQLSTATE mapping
Integrity crates/engine/src/verify.rs integrity_report: entries with fewer than 2 lines, unbalanced entries, wrong account totals, unbalanced currencies
Ledger trait crates/core/src/model/api.rs The operations both ledgers offer
Test harness crates/engine/tests/support/mod.rs A fresh database per test, or copied from a migrated template for speed
Tests crates/engine/tests/posting.rs, crates/engine/tests/differential.rs Posting tests and the differential test
Crash test crates/engine/examples/crash_writer.rs Posts until killed; checks integrity afterwards

In one transaction:

  1. Lock every account the entry touches, SELECT … FOR UPDATE in ascending ID order (ADR-0007).
  2. Validate with mezgeb_core::validate against those locked rows, so the balances it checks cannot change before commit.
  3. Insert the entry.
  4. Apply the entry’s net effect to each account, one UPDATE … RETURNING per account, and work out each line’s totals after it from the totals the database returns.
  5. Insert every line in one statement (UNNEST).
  6. Commit. The database checks the result again (docs/db-schema.md).

Differences from the step’s task list:

  • post_entry takes an EntryDraft, not a BalancedEntry, and validates it once, inside the transaction, against the locked rows. A BalancedEntry built before the locks were taken would have been checked against balances that may since have changed; validating under the lock is the stronger guarantee.
  • Compile-time checked queries (query!, with offline data for CI) and tracing are not used yet. They add a build step for every contributor; they are revisited when the API is built (step 2.07).
Requirement Test Result
All worked entries post, and totals equal the hand-computed trial balance the_worked_story_posts_on_postgresql Pass: OB and E01–E18 (24 journal tables) post through PgStore; every balance equals the trial balance; history and accounts are identical to MemLedger’s; integrity clean
The same random sequence on MemLedger and PgStore gives identical balances and history postgresql_matches_the_reference_ledger Pass: every operation’s result, the final accounts and the full history are compared

Other tests: metadata and event time round-trip; a refused posting writes nothing (including a reused idempotency key); account and reversal errors are identical on both ledgers; the same account on both sides of an entry posts.

On its first run, the differential test failed and shrank the failure to two operations:

step 1, Transfer { debit: 4, credit: 4, amount: 1 }
oracle: Ok(Posted(…))
postgresql: Err(Storage("… violates check constraint \"accounts_not_negative\""))
minimal sequence: [Open(4), Transfer { debit: 4, credit: 4, amount: 1 }]

An entry that debits and credits account 3010 (equity) by the same amount nets to zero and is valid. The first version of the engine updated totals line by line, so after the debit line the account was briefly negative, and PostgreSQL checks a CHECK constraint after every row update, not at commit. The engine now applies each account’s net effect in one update. Regression tests were added on both ledgers (the_same_account_on_both_sides_posts).

The differential test passes on 1,000 sequences.

MEZGEB_DIFF_CASES=1000 MEZGEB_TEST_DATABASE_URL=… \
cargo test -p mezgeb-engine --release --test differential
differential test: 1000 sequences matched
test result: ok. 1 passed; finished in 802.82s

Each sequence has 1 to 40 random operations (open accounts, transfers, split payments, arbitrary lines, reversals, status changes), runs in its own database copied from a migrated template, and ends with a clean integrity report. Pass. CI runs the same test with 16 sequences on every push.

Gate: killing the test process mid-run never leaves an entry without lines, verified by the integrity query after a forced abort.

crash_writer write posts three-line entries (Dr bank, Cr wallet, Cr fee revenue) continuously. It was killed with SIGKILL five times after a random 2–6 seconds, and crash_writer check ran the integrity queries after each kill:

round 1: killed after 5s (exit 137)
126 entries; IntegrityReport { entries_without_lines: 0, unbalanced_entries: 0, accounts_with_wrong_totals: 0, unbalanced_currencies: 0 }
round 2: killed after 2s (exit 137)
190 entries; IntegrityReport { entries_without_lines: 0, unbalanced_entries: 0, accounts_with_wrong_totals: 0, unbalanced_currencies: 0 }
round 3: killed after 2s (exit 137)
254 entries; IntegrityReport { entries_without_lines: 0, unbalanced_entries: 0, accounts_with_wrong_totals: 0, unbalanced_currencies: 0 }
round 4: killed after 4s (exit 137)
356 entries; IntegrityReport { entries_without_lines: 0, unbalanced_entries: 0, accounts_with_wrong_totals: 0, unbalanced_currencies: 0 }
round 5: killed after 4s (exit 137)
475 entries; IntegrityReport { entries_without_lines: 0, unbalanced_entries: 0, accounts_with_wrong_totals: 0, unbalanced_currencies: 0 }

The PostgreSQL log confirms that two of the kills landed inside an open posting transaction, which PostgreSQL rolled back:

LOG: unexpected EOF on client connection with an open transaction
LOG: unexpected EOF on client connection with an open transaction

Result: passed. No kill left an entry without lines, an unbalanced entry or a wrong account total.