This property ensures that a transaction is treated as a single, indivisible unit of work, which either completes in its entirety or not at all. If any part of the transaction fails, the entire transaction fails, and the database is left unchanged.
Example:
Let’s consider a banking system where you are transferring money from your account to a friend’s account. This operation involves two steps: deducting the amount from your account and adding it to your friend’s account.
Atomicity ensures that both these operations succeed or fail as a whole. If there is a failure after deducting the amount from your account but before adding it to your friend’s, atomicity rolls back the transaction, ensuring that the amount is not deducted from your account.
Atomicity in a database transaction, such as in your PostgreSQL example, is typically achieved through the use of transactions.
In a transactional database system like PostgreSQL, you can group multiple SQL commands into a single transaction. A transaction begins with the first executable SQL statement and ends when it is committed or rolled back.
Here’s a simplified example of how you might use a transaction to ensure atomicity in your service:
BEGIN; -- Start the transaction
-- Step 1: Debit from one account
UPDATE accounts SET balance = balance - 100 WHERE account_id = 1;
-- Step 2: Credit to another account
UPDATE accounts SET balance = balance + 100 WHERE account_id = 2;
COMMIT; -- Commit the transaction
In this example, the BEGIN
statement starts a new transaction. The two UPDATE
statements are part of this transaction. If both UPDATE
statements execute successfully, the COMMIT
statement will end the transaction and make the changes permanent.
However, if there’s a problem with either of the UPDATE
statements (for example, if the first UPDATE
statement succeeds but the second fails), you can issue a ROLLBACK
command instead of COMMIT
. This will undo any changes made in the current transaction, ensuring atomicity.
So, in this way, either both the debit and credit operations will succeed, or if there’s a failure, both operations will be rolled back. This is how atomicity is achieved in a database transaction.
Will atomicity in a relational database be compromised if queries are not enclosed between BEGIN and COMMIT?
Yes. In a relational database like PostgreSQL, the use of BEGIN
and COMMIT
(or ROLLBACK
in case of errors) is critical to ensuring atomicity.
When you enclose a set of SQL statements between BEGIN
and COMMIT
, you're defining them as a single transaction. The database management system will ensure that either all of these operations are successfully completed, or, if any of them fail, none of them will take effect.
Without these transaction boundaries (BEGIN
and COMMIT
), each SQL statement is treated as a separate transaction. So, if you have a sequence of operations that need to be performed as a single unit of work to maintain data integrity, and you don't use BEGIN
and COMMIT
, you won't achieve atomicity. If an error or failure occurs during the sequence, some operations may have been executed while others have not, leading to potential inconsistencies in your data.