PostgreSQL Transactions and Concurrency

Transactions in PostgreSQL are a sequence of one or more SQL operations executed as a single unit. They ensure data consistency, integrity, and reliability by following the ACID properties:

1. ACID Properties

  • Atomicity: All operations in a transaction succeed or none do.
  • Consistency: Transactions move the database from one valid state to another.
  • Isolation: Transactions operate independently without interference.
  • Durability: Once committed, changes are permanent.

2. Basic Transaction Syntax

-- Start a transaction
            BEGIN;

            -- SQL operations
            INSERT INTO employees(name, department_id, salary) VALUES('John Doe', 1, 50000);
            UPDATE employees SET salary = salary * 1.10 WHERE department_id = 1;

            -- Commit changes
            COMMIT;

            -- Rollback in case of error
            ROLLBACK;

3. Savepoints

Savepoints allow partial rollback within a transaction without rolling back the entire transaction.

BEGIN;

            INSERT INTO employees(name, department_id, salary) VALUES('Alice', 2, 60000);

            -- Create a savepoint
            SAVEPOINT before_bonus;

            UPDATE employees SET salary = salary * 1.10 WHERE department_id = 2;

            -- Rollback to savepoint if needed
            ROLLBACK TO SAVEPOINT before_bonus;

            COMMIT;

4. Isolation Levels

PostgreSQL supports multiple isolation levels to control concurrency and avoid conflicts:

  • Read Uncommitted: Allows dirty reads (not commonly used in PostgreSQL).
  • Read Committed: Default level; each query sees only committed data.
  • Repeatable Read: Ensures consistent reads within a transaction.
  • Serializable: Highest isolation; ensures complete transaction isolation.
-- Set transaction isolation level
            BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;

            -- Perform operations
            COMMIT;

5. Concurrency Control

PostgreSQL uses MVCC (Multi-Version Concurrency Control) to allow multiple transactions to occur simultaneously without conflicts.

  • Readers do not block writers, and writers do not block readers.
  • Ensures data consistency for concurrent operations.
  • Helps prevent deadlocks and improves performance in multi-user environments.

6. Transaction Example

-- Transfer salary between employees safely
            BEGIN;

            UPDATE employees SET salary = salary - 5000 WHERE id = 101;
            UPDATE employees SET salary = salary + 5000 WHERE id = 102;

            -- Check if both updates are successful
            COMMIT;

7. Best Practices

  • Always use transactions for multiple dependent operations.
  • Use savepoints for partial rollback in long transactions.
  • Keep transactions short to reduce locking and improve performance.
  • Choose appropriate isolation levels based on your application needs.
  • Monitor for deadlocks and handle exceptions properly.

Conclusion

Transactions are a critical feature of PostgreSQL for maintaining data integrity and consistency in concurrent environments. By understanding ACID properties, isolation levels, savepoints, and concurrency control, you can design robust and reliable applications. In the next tutorial, we will explore Triggers, which allow automatic execution of actions in response to database events.