4.6 DBMS Comparison
The concept of a transaction is the same across the four products, but how it starts and the defaults differ.
| Item | PostgreSQL | MySQL (InnoDB) | Oracle | SQL Server / Azure SQL |
|---|---|---|---|---|
| Server default autocommit | On | On | No per-statement autocommit. The first change statement implicitly starts a transaction | On |
| Explicit start | BEGIN / START TRANSACTION | START TRANSACTION / BEGIN | No separate start statement (SET TRANSACTION sets attributes) | BEGIN TRANSACTION |
| DDL and transactions | Most DDL can be rolled back inside a transaction | DDL implicitly commits the preceding transaction | DDL implicitly commits the preceding transaction | Most DDL can be rolled back inside a transaction |
| Savepoint | Supported | Supported | Supported | SAVE TRANSACTION |
Oracle's approach is a little unintuitive. It does not commit automatically per statement. The first update statement opens a transaction, and the transaction stays open until the user issues COMMIT. Unless you commit explicitly, the change is not finalized and is not visible to other sessions. A developer used to PostgreSQL or MySQL who runs only an UPDATE in Oracle and closes the session loses the change.
As seen earlier, in MySQL and Oracle, DDL cannot be placed inside a transaction. If the database meets an ALTER TABLE midway through a transaction, it first commits the changes up to that point and commits the DDL separately. From the next statement on, a new transaction starts. This behavior is called an implicit commit. The user expressed it as one transaction, but the database splits the transaction at the DDL.
| Step | Statement executed | MySQL / Oracle | PostgreSQL / SQL Server |
|---|---|---|---|
| 1 | BEGIN | Transaction starts | Transaction starts |
| 2 | INSERT INTO log ... | Inside the transaction | Inside the transaction |
| 3 | ALTER TABLE account ADD COLUMN ... | Step 2 is auto-committed first. The ALTER is also committed immediately | Inside the transaction |
| 4 | INSERT INTO account ... → fails | Fails in a new transaction | Fails inside the transaction |
| 5 | ROLLBACK | Only step 4 is cancelled. Steps 2 and 3 are already finalized and cannot be undone | Steps 2, 3, and 4 are all cancelled |
Implicit commit around DDL (MySQL, Oracle)
sequenceDiagram
participant App as Application
participant DB as MySQL or Oracle
App->>DB: 1. BEGIN
Note over DB: Transaction T1 starts
App->>DB: 2. INSERT INTO log
Note over DB: Runs inside T1
App->>DB: 3. ALTER TABLE account ADD COLUMN
Note over DB: T1 implicitly committed - step 2 finalized
Note over DB: ALTER runs and commits immediately - step 3 finalized
App->>DB: 4. INSERT INTO account
Note over DB: Runs in new transaction T2
DB-->>App: Step 4 fails
App->>DB: 5. ROLLBACK
Note over DB: Only T2 cancelled - only step 4 undone
Note over App,DB: Steps 2 and 3 are already finalized and cannot be undone
The diagram draws the MySQL / Oracle column of the table above in time order. The application intended everything from step 1 BEGIN to step 5 ROLLBACK as one transaction, but inside the database the transaction is split at the DDL.
Steps 1 and 2 run inside transaction T1. When step 3, ALTER TABLE, arrives, the database commits T1 before executing the DDL, so the INSERT of step 2 is finalized at this point. It then executes the ALTER and commits that immediately too. The INSERT of step 4 runs in the newly started transaction T2 and fails. All the step 5 ROLLBACK can cancel is T2, that is, step 4 only. Steps 2 and 3 have already been committed and cannot be undone.
In PostgreSQL and SQL Server, the same statements run inside a single transaction from start to finish, so the step 5 ROLLBACK cancels steps 2, 3, and 4 all together.
Although there are differences between databases, note that, as in the example above, how far you can roll back when an update after a DDL fails inside one transaction differs by product. It is recommended to run DDL on its own rather than mixing it with other changes in the same transaction.