Transaction Processing in PostgreSQL DBMS

Outline

Transaction scope

  • A transaction starts from BEGIN or START TRANSACTION statement and it ends with COMMIT or ROLLBACK statement
  • SQL statements outside of an explicit transaction automatically become a single-statement transaction
  • PostgreSQL supports transactional DDL
  • It means that all DDL statemehts except CREATE/DROP/ALTER DATABASE, TABLESPACE, CLUSTER are reversible
  • If an error occurs inside a transaction then it is automatically rolledback
  • However, still a statement to complete the current transaction (COMMIT, ROLLBACK, ABORT) is required
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

Transaction Processing in PostgreSQL DBMS

Outline

Isolation levels in PostgreSQL

  • At READ COMMITED isolation level a transaction may exhibit:
    • non-repeatable read phenomenon,
    • phantom phenomenon
  • At REPEATABLE READ isolation level (SI) a transaction may exhibit:
    • other phonemena (write skew)
  • At SERIALIZABLE isolation level (SSI)a transaction may exhibit:
    • none of the phenomena
    • 	      
      Level           Dirty Read    Nonrepeatable Read  Phantom       Other (Write skew)
      ----------------------------------------------------------------------------------	      
      READ COMMITTED  Not possible  Possible            Possible      Possible
      
      REPEATABLE READ Not possible  Not possible        Not possible  Possible
      
      SERIALIZABLE    Not possible  Not possible        Not possible  Not possible
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

Transaction Processing in PostgreSQL DBMS

Outline

READ COMMITTED isolation level

  • We consider a database created through processing of the following CREATE TABLE statements
           
      CREATE TABLE DEPARTMENT(
       name               VARCHAR(50)        NOT NULL,
       code               CHAR(5)            NOT NULL,
       total_staff_number DECIMAL(2)         NOT NULL,
       chair              VARCHAR(50)            NULL,
       budget             DECIMAL(9,1)       NOT NULL,
        CONSTRAINT dept_pkey PRIMARY KEY(name),
        CONSTRAINT dept_ckey1 UNIQUE(code),
        CONSTRAINT dept_ckey2 UNIQUE(chair),
        CONSTRAINT dept_check1 CHECK (total_staff_number BETWEEN 1 AND 50) );
           
      CREATE TABLE COURSE(
       cnum               CHAR(7)           NOT NULL,
       title              VARCHAR(200)      NOT NULL,
       credits            DECIMAL(2)        NOT NULL,
       offered_by         VARCHAR(50)           NULL,
        CONSTRAINT course_pkey PRIMARY KEY(cnum),
        CONSTRAINT course_check1 CHECK (credits IN (6, 12)),
        CONSTRAINT course_fkey1 FOREIGN KEY(offered_by)
                                REFERENCES DEPARTMENT(name) ON DELETE CASCADE );
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

READ COMMITTED isolation level

  • Dirty read is not possible
           
      BEGIN;
      SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
      SELECT title FROM COURSE WHERE cnum='CSCI111';
       title
      -------
       C++
           
                                  BEGIN;
                                  SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
                                  UPDATE COURSE SET title = 'Java' WHERE cnum='CSCI111';
           
      SELECT title FROM COURSE WHERE cnum='CSCI111';
       title
      -------
      C++
           
                                  COMMIT;
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

READ COMMITTED isolation level

  • Lost update is not possible
           
      BEGIN;
      SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
      SELECT budget FROM DEPARTMENT
      WHERE name = 'Computer Science';
        budget  
      ----------
       223426.9
      UPDATE DEPARTMENT SET budget = budget + 100
      WHERE name = 'Computer Science';
           
                                  BEGIN;
                                  SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
                                  UPDATE DEPARTMENT SET budget = budget + 200
                                  WHERE name = 'Computer Science';
                                  WAIT !!!
  • Continued on the next slide ...
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

READ COMMITTED isolation level

  • Continuation from the previous slide ...
           
      COMMIT;
           
                                   COMMIT;
      SELECT budget from DEPARTMENT
      WHERE name = 'Computer Science';
        budget  
      ----------
       223726.9
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

READ COMMITTED isolation level

  • Non-repeatable read phenomenon is possible
      BEGIN;
      SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
      SELECT budget FROM DEPARTMENT
      WHERE name = 'Computer Science';
        budget  
      ----------
       223426.9
           
                                  BEGIN;
                                  SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
                                  UPDATE DEPARTMENT SET budget = budget + 200
                                  WHERE name = 'Computer Science';
                                  COMMIT;
      SELECT budget FROM DEPARTMENT
      WHERE name = 'Computer Science';
        budget  
      ----------
       223626.9
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

READ COMMITTED isolation level

  • Phantom phenomenon is possible
      BEGIN;
      SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
      SELECT count(*) FROM COURSE;
       count
      -------
          15
           
                                  BEGIN;
                                  SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
                                  DELETE FROM COURSE
                                  WHERE offered_by = 'Mathematics';
                                  COMMIT;
      SELECT count(*) FROM COURSE;
       count
      -------
          14
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

READ COMMITTED isolation level

  • Corruption of a database is possible
  • We would like to set a budget of Computer Science department equal to 2*budget of Mathematics department
      BEGIN;
      SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
      SELECT name,budget FROM DEPARTMENT
      WHERE name IN ('Computer Science','Mathematics');
             name       |  budget  
      ------------------+----------
       Computer Science | 223426.9
       Mathematics      | 133456.9
        
      UPDATE DEPARTMENT SET budget =
             (SELECT budget FROM DEPARTMENT
              WHERE name = 'Mathematics')
      WHERE name = 'Computer Science';
  • Continued on the next slide ...
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

READ COMMITTED isolation level

  • Continued from the previous slide ...
           
                                  BEGIN;
                                  SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
      			    UPDATE DEPARTMENT SET BUDGET = 1000
      			    WHERE name = 'Mathematics';
                                  COMMIT;
        
      UPDATE DEPARTMENT SET budget =  budget +
             (SELECT budget FROM DEPARTMENT
              WHERE name = 'Mathematics')
      WHERE name = 'Computer Science';
      SELECT name,budget FROM DEPARTMENT
      WHERE name IN ('Computer Science','Mathematics');
             name       |  budget  
      ------------------+----------
       Mathematics      |   1000.0
       Computer Science | 134456.9
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

READ COMMITTED isolation level

  • Write skew is possible
  • We would like to remove one of the courses, either ENGG100 or ENGG103
      BEGIN;
      SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
      SELECT cnum, title FROM COURSE WHERE cnum LIKE 'ENGG%';
        cnum   |               title                
      ---------+------------------------------------
       ENGG103 | Materials in Design
       ENGG100 | Engineering Computing and Analysis
       ENGG111 | C++
        
      DELETE FROM COURSE WHERE cnum = 'ENGG103';
  • Continued on the next slide ...
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

READ COMMITTED isolation level

  • Continued from the previous slide ...
           
                                  BEGIN;
                                  SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
      			    SELECT cnum, title FROM COURSE WHERE cnum LIKE 'ENGG%';
                                    cnum   |               title                
                                  ---------+------------------------------------
                                   ENGG103 | Materials in Design
                                   ENGG100 | Engineering Computing and Analysis
                                   ENGG111 | C++
        
      	                     DELETE FROM COURSE WHERE cnum = 'ENGG100';
      COMMIT;
                                   COMMIT;
      SELECT cnum, title FROM COURSE WHERE cnum LIKE 'ENGG%';
        cnum   |               title                
      ---------+------------------------------------
       ENGG111 | C++
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

Transaction Processing in PostgreSQL DBMS

Outline

REPEATABLE READ isolation level (SI)

  • Non-repeatable read phenomenon is not possible
      BEGIN;
      SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
      SELECT budget FROM DEPARTMENT
      WHERE name = 'Computer Science';
        budget  
      ----------
       223426.9
           
                                  BEGIN;
                                  SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
                                  UPDATE DEPARTMENT SET budget = budget + 200
                                  WHERE name = 'Computer Science';
                                  COMMIT;
      SELECT budget FROM DEPARTMENT
      WHERE name = 'Computer Science';
        budget  
      ----------
       223426.9
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

REPEATABLE READ isolation level (SI)

  • Phantom phenomenon is not possible
      BEGIN;
      SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
      SELECT count(*) FROM COURSE;
       count
      -------
          15
           
                                  BEGIN;
                                  SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
                                  DELETE FROM COURSE
                                  WHERE offered_by = 'Mathematics';
                                  COMMIT;
      SELECT count(*) FROM COURSE;
       count
      -------
          15
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

REPEATABLE READ isolation level (SI)

  • Corruption of a database is not possible
  • We would like to set a budget of Computer Science department equal to 2*budget of Mathematics department
      BEGIN;
      SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
      SELECT name,budget FROM DEPARTMENT
      WHERE name IN ('Computer Science','Mathematics');
             name       |  budget  
      ------------------+----------
       Computer Science | 223426.9
       Mathematics      | 133456.9
        
      UPDATE DEPARTMENT SET budget =
             (SELECT budget FROM DEPARTMENT
              WHERE name = 'Mathematics')
      WHERE name = 'Computer Science';
  • Continued on the next slide ...
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

REPEATABLE READ isolation level

  • Continued from the previous slide ...
           
                                  BEGIN;
                                  SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
      			    UPDATE DEPARTMENT SET BUDGET = 1000
      			    WHERE name = 'Mathematics';
                                  COMMIT;
        
      UPDATE DEPARTMENT SET budget =  budget +
             (SELECT budget FROM DEPARTMENT
              WHERE name = 'Mathematics')
      WHERE name = 'Computer Science';
      SELECT name,budget FROM DEPARTMENT
      WHERE name IN ('Computer Science','Mathematics');
             name       |  budget  
      ------------------+----------
       Mathematics      |   1000.0
       Computer Science | 266913.8
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

REPEATABLE READ isolation level

  • Lost update is not possible (first committer wins)
           
      BEGIN;
      SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
      SELECT budget FROM DEPARTMENT
      WHERE name = 'Computer Science';
        budget  
      ----------
       223426.9
      UPDATE DEPARTMENT SET budget = budget + 100
      WHERE name = 'Computer Science';
           
                                  BEGIN;
                                  SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
                                  UPDATE DEPARTMENT SET budget = budget + 200
                                  WHERE name = 'Computer Science';
                                  WAIT !!!
  • Continued on the next slide ...
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

REPEATABLE READ isolation level

  • Continuation from the previous slide ...
           
      COMMIT;
           
                            ERROR:  could not serialize access due to concurrent update
      SELECT budget from DEPARTMENT
      WHERE name = 'Computer Science';
        budget  
      ----------
       223526.9
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

REPEATABLE READ isolation level

  • Write skew is possible
  • We would like to remove one of the courses, either ENGG100 or ENGG103
      BEGIN;
      SET TRANSACTION ISOLATION LEVEL REPATABLE READ;
      SELECT cnum, title FROM COURSE WHERE cnum LIKE 'ENGG%';
        cnum   |               title                
      ---------+------------------------------------
       ENGG103 | Materials in Design
       ENGG100 | Engineering Computing and Analysis
       ENGG111 | C++
        
      DELETE FROM COURSE WHERE cnum = 'ENGG103';
  • Continued on the next slide ...
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

REPEATABLE READ isolation level

  • Continued from the previous slide ...
           
                                  BEGIN;
                                  SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
      			    SELECT cnum, title FROM COURSE WHERE cnum LIKE 'ENGG%';
                                    cnum   |               title                
                                  ---------+------------------------------------
                                   ENGG103 | Materials in Design
                                   ENGG100 | Engineering Computing and Analysis
                                   ENGG111 | C++
        
      	                     DELETE FROM COURSE WHERE cnum = 'ENGG100';
      COMMIT;
                                   COMMIT;
      SELECT cnum, title FROM COURSE WHERE cnum LIKE 'ENGG%';
        cnum   |               title                
      ---------+------------------------------------
       ENGG111 | C++
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

Transaction Processing in PostgreSQL DBMS

Outline

SERIALIZABLE isolation level

  • Write skew is not possible
  • We would like to remove one of the courses, either ENGG100 or ENGG103
      BEGIN;
      SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
      SELECT cnum, title FROM COURSE WHERE cnum LIKE 'ENGG%';
        cnum   |               title                
      ---------+------------------------------------
       ENGG103 | Materials in Design
       ENGG100 | Engineering Computing and Analysis
       ENGG111 | C++
        
      DELETE FROM COURSE WHERE cnum = 'ENGG103';
  • Continued on the next slide ...
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

SERIALIZABLE isolation level

  • Continued from the previous slide ...
           
                                  BEGIN;
                                  SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
      			    SELECT cnum, title FROM COURSE WHERE cnum LIKE 'ENGG%';
                                    cnum   |               title                
                                  ---------+------------------------------------
                                   ENGG103 | Materials in Design
                                   ENGG100 | Engineering Computing and Analysis
                                   ENGG111 | C++
        
      	                     DELETE FROM COURSE WHERE cnum = 'ENGG100';
                                   COMMIT;
      COMMIT;
         
      ERROR:  could not serialize access due to read/write dependencies among transactions
      DETAIL:  Reason code: Canceled on identification as a pivot, during commit attempt.
In HTML view press 'p' to see the lecture notes
TOP              Created by Janusz R. Getta, CSCI235 Database Systems, Autumn 2026

References