PostgreSQL Consistency Model: COMMIT or ROLLBACK
Last updated: July 18, 2025
PostgreSQL is an ACID-compliant relational database, which means it guarantees Atomicity, Consistency, Isolation, and Durability. At the heart of this model are transactions, and each transaction has only two possible outcomes:
✅ COMMIT
This means the transaction succeeded.
All changes made during the transaction are permanently written to the database.
PostgreSQL ensures durability: even in the event of a crash, committed changes are safe and will persist.
In the Formance Ledger, a successful transaction means:
Balances are updated.
Logs are inserted.
Any triggers (like hashing) are executed.
Any side effects (e.g., volumes, metadata updates) are finalized.
❌ ROLLBACK
This means the transaction failed or was explicitly canceled.
All operations done during the transaction are undone.
PostgreSQL discards any intermediate state, so no partial updates ever reach the database.
In Formance, if an error occurs (e.g., insufficient funds, validation failure, concurrency conflict), the ledger returns an error to the client, and no changes are committed.
🔢 PostgreSQL Sequences & Gaps in IDs
In PostgreSQL, sequences are used to generate unique identifiers (e.g., for transaction_id or log_id). These sequences are not transactional. That is:
When you request the next value from a sequence (
nextval('sequence_name')), PostgreSQL increments the sequence immediately, outside the current transaction.If the transaction later rolls back, that value is lost forever — it is not reused.
This is by design, because sequences are intended to produce unique, ever-increasing values efficiently without rollback overhead.
If the ledger operation fails and is rolled back, the sequence number is still consumed. The next successful transaction will get a higher ID, resulting in a gap in the sequence.
🧱 Why This Matters for the Ledger
Because Formance wraps every ledger operation inside a single PostgreSQL transaction, this gives you strong consistency guarantees out of the box:
Guarantee | What It Means in Practice |
Atomicity | Either the whole ledger write succeeds, or none of it does. No partial ledger state. |
Consistency | Ledger rules (e.g., double-entry, balance constraints) are always preserved. |
Isolation | Concurrent transactions do not interfere—thanks to PostgreSQL’s row-level locking and MVCC. |
Durability | Once committed, your ledger data is safe—even in case of crashes or restarts. |
Formance does not need log hashing or extra mechanisms to ensure consistency. That’s already guaranteed by PostgreSQL through its transactional model: either everything is applied, or nothing is.