DEV Community

Jarvish John
Jarvish John

Posted on

Durability

SELECT * FROM accounts;
Enter fullscreen mode Exit fullscreen mode

Begin by looking at the current balances so there is a clear idea of the starting point. This makes it easier to confirm later whether the transfer actually went through and stayed saved.


BEGIN;

UPDATE accounts 
SET balance = balance - 300 
WHERE name = 'Alice';

UPDATE accounts 
SET balance = balance + 300 
WHERE name = 'Bob';

COMMIT;
Enter fullscreen mode Exit fullscreen mode

Both updates are wrapped inside a transaction so they execute together. The commit finalizes the change, making it permanent instead of temporary.


SELECT * FROM accounts;
Enter fullscreen mode Exit fullscreen mode

Immediately checking after commit helps confirm that the transfer worked as expected. The balances should reflect the updated values without any mismatch.

Reconnect to the database session after simulating failure.


SELECT * FROM accounts;
Enter fullscreen mode Exit fullscreen mode

Running the same query again verifies whether the data still exists after reconnecting. If the balances remain updated, it proves the changes were stored safely.


Durability ensures that once a transaction is committed, its changes are permanently stored on disk and not just in memory. PostgreSQL uses Write-Ahead Logging (WAL) to record changes before they are applied, allowing recovery even after a crash. If a failure occurs before the commit, none of the changes are saved, but if it happens after the commit, the system uses these logs to restore the committed state, ensuring no data is lost.

Top comments (0)