SELECT * FROM accounts;
Start by checking the current balances to know the initial state before performing any transfer. This gives a clear baseline to compare against after the transaction is completed. It helps confirm whether the changes actually persist later.
BEGIN;
UPDATE accounts
SET balance = balance - 200
WHERE name = 'Alice';
UPDATE accounts
SET balance = balance + 200
WHERE name = 'Bob';
COMMIT;
A proper transfer is done by grouping both debit and credit inside a single transaction. The commit ensures both operations are saved together as one unit. This avoids cases where money is deducted but not added.
SELECT * FROM accounts;
After committing, checking the balances confirms that the transfer has successfully taken place. Alice’s balance should decrease and Bob’s should increase. This verifies that the transaction was applied correctly.
Reconnect to the database after simulating a restart or crash.
SELECT * FROM accounts;
Querying again after reconnecting ensures the changes are still present. If the balances remain updated, it confirms that the data was permanently stored. This shows that committed transactions are not lost even after a restart.
Durability is ensured because once a transaction is committed, the database writes the changes to persistent storage. PostgreSQL uses mechanisms like Write-Ahead Logging (WAL) to make sure data can be recovered even if a crash happens. If a failure occurs before the commit, none of the changes are saved. If it happens after the commit, the system uses logs to restore the committed state, ensuring no data loss.
Top comments (0)