Sub topic 12
How PostgreSQL, MySQL, SQL Server and Oracle Differ
This chapter assumes basic SELECT and UPDATE statements, explicit transactions with BEGIN and COMMIT, and the idea that several clients can use a database at once. The SQL standard names four isolation levels, but database products implement those names differently. Always check the product version and configuration before relying on a guarantee.
PostgreSQL
PostgreSQL uses multiversion concurrency control (MVCC). A statement normally reads a snapshot of committed rows instead of waiting for readers or writers.
READ UNCOMMITTEDbehaves likeREAD COMMITTED. PostgreSQL never exposes another transaction's uncommitted data.READ COMMITTEDis the default. Each statement gets a fresh snapshot, so two statements in one transaction can see different committed values.REPEATABLE READuses one transaction-wide snapshot. Concurrent changes that would make the snapshot inconsistent can cause a serialization failure.SERIALIZABLEadds Serializable Snapshot Isolation (SSI). It detects dangerous read and write patterns and may abort a transaction with SQLSTATE40001.
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
SELECT balance FROM accounts WHERE id = 1;
UPDATE accounts SET balance = balance + 10 WHERE id = 1;
COMMIT;The application must retry a transaction that fails with a serialization error.
MySQL InnoDB
MySQL's behavior depends on the storage engine, but InnoDB is the normal transactional engine. Its default is REPEATABLE READ.
Consistent, nonlocking reads use an MVCC snapshot. Under REPEATABLE READ, repeated ordinary queries usually see the same snapshot. Locking reads such as SELECT ... FOR UPDATE read current rows and take locks. InnoDB also uses gap locks and next-key locks, especially under REPEATABLE READ, to protect index ranges from inserts.
READ COMMITTED creates a newer snapshot for each statement and reduces gap locking. READ UNCOMMITTED can expose uncommitted changes. SERIALIZABLE turns ordinary reads into stronger locking reads.
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
START TRANSACTION;
SELECT balance FROM accounts WHERE id = 1 FOR UPDATE;
UPDATE accounts SET balance = balance + 10 WHERE id = 1;
COMMIT;The setting applies to the next transaction in the session. Connection pools must set it deliberately because sessions are reused.
SQL Server
SQL Server's traditional default is locking READ COMMITTED. A reader can wait for a writer, and a writer can wait for a reader. An administrator can enable READ_COMMITTED_SNAPSHOT, which changes READ COMMITTED to use row versions instead.
SQL Server also provides:
SNAPSHOT, an explicit transaction-wide row-versioned snapshot.REPEATABLE READ, which keeps shared locks on rows read.SERIALIZABLE, which adds key-range locks to prevent matching rows from appearing.
SET TRANSACTION ISOLATION LEVEL SNAPSHOT;
BEGIN TRANSACTION;
SELECT balance FROM dbo.accounts WHERE id = 1;
COMMIT TRANSACTION;SNAPSHOT must be enabled for the database before sessions can use it. Row versioning reduces reader-writer blocking, but it consumes version-store space.
Oracle
Oracle uses MVCC and defaults to statement-level READ COMMITTED. A query sees a consistent view from the time that query begins. A later query in the same transaction can see newer committed data.
Oracle does not provide dirty reads. Its SERIALIZABLE mode uses a transaction-level snapshot and can reject a conflicting update with ORA-08177: cannot serialize access for this transaction. Oracle also supports READ ONLY transactions.
Comparison and choice
| Product | Default | Snapshot behavior | Common conflict result |
|---|---|---|---|
| PostgreSQL | READ COMMITTED | New snapshot per statement | Serialization failure |
| MySQL InnoDB | REPEATABLE READ | Transaction snapshot for ordinary reads | Deadlock or lock wait |
| SQL Server | Locking READ COMMITTED | Depends on database row-versioning setting | Blocking or deadlock |
| Oracle | READ COMMITTED | New snapshot per statement | ORA-08177 in serializable mode |
Exercise
A SQL Server application expects readers not to block writers. Which setting or isolation mode should you investigate?
Show answer
Investigate READCOMMITTEDSNAPSHOT for database-wide behavior or SNAPSHOT for explicit transaction-wide snapshots. Confirm that version-store capacity and retry handling are appropriate.