Skip to content

PostgreSQL - Rollback

Understanding Transactions and ROLLBACK in PostgreSQL

Section titled “Understanding Transactions and ROLLBACK in PostgreSQL”

In PostgreSQL, a transaction is a sequence of operations performed as a single logical unit of work. The ROLLBACK command is used to discard all changes made within the current transaction, effectively undoing them and restoring the database to the state it was in before the transaction began.

The ACID Properties: The Role of ROLLBACK and COMMIT

Transactions in PostgreSQL adhere to the ACID properties (Atomicity, Consistency, Isolation, Durability). ROLLBACK and COMMIT are essential for enforcing these.

  • Atomicity: Ensures that all operations within a transaction are completed successfully. If any part fails, the entire transaction is rolled back. COMMIT confirms a successful transaction, while ROLLBACK enforces atomicity upon failure.
  • Consistency: Guarantees that a transaction brings the database from one valid state to another. ROLLBACK prevents transactions that violate database rules (like constraints) from being saved.
  • Isolation: Ensures that concurrent transactions do not interfere with each other. This is managed by PostgreSQL’s concurrency control system.
  • Durability: Once a transaction is committed with COMMIT, its changes are permanent and will survive system failures. ROLLBACK discards non-durable changes.

Imagine a financial transfer between two bank accounts. The process requires two steps: debiting money from the sender’s account and crediting it to the receiver’s account. Both must succeed. If the credit operation fails after the debit has succeeded, a ROLLBACK is crucial to reverse the debit, ensuring no money is lost.

  • Error Prevention: If any statement in a transaction fails, ROLLBACK discards all preceding changes, preventing data corruption.
  • Data Integrity: Ensures that only complete and valid transactions are saved to the database, maintaining data consistency.
  • Safe Testing: Developers can execute a series of changes within a transaction, inspect the results, and then use ROLLBACK to discard the changes without permanently altering the database.
  • Failure Recovery: If the application or database server crashes mid-transaction, PostgreSQL automatically rolls back any uncommitted transactions upon restart.

How ROLLBACK Works: A Step-by-Step Example

Section titled “How ROLLBACK Works: A Step-by-Step Example”

Let’s demonstrate ROLLBACK with a practical example. We’ll attempt to add a new employee to our employees table. First, let’s see the current state of the table.

-- Let's assume we have an 'employees' table
-- Check its current content
SELECT * FROM employees;

This would show the existing employees. Now, let’s start a transaction to add a new employee, but then decide to undo it.

-- Step 1: Start a new transaction
BEGIN;
-- Step 2: Insert a new employee record
INSERT INTO employees (id, name, position, salary, department, hire_date)
VALUES (6, 'Alice', 'Data Scientist', 95000.00, 'Analytics', '2024-05-20');
-- Step 3: Within the same transaction, you can see the change
-- This SELECT statement will show Alice in the table
SELECT * FROM employees WHERE id = 6;
-- Step 4: Now, decide to undo the operation
ROLLBACK;

After executing ROLLBACK, the INSERT operation is completely undone. If we check the table again, the new employee will not be there.

-- Verify that the transaction was rolled back
SELECT * FROM employees;

The output will show the original table content, without the record for ‘Alice’, proving the ROLLBACK was successful.

  • Forgetting BEGIN: If you execute an INSERT, UPDATE, or DELETE statement without first starting a transaction with BEGIN, it will be auto-committed and cannot be rolled back.
  • Accidental COMMIT: Be careful not to issue a COMMIT command when you intend to ROLLBACK.
  • Connection Loss: If your connection to the database is lost, any open transaction will be automatically rolled back by PostgreSQL.