Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Name five practical differences between MySQL InnoDB and PostgreSQL that matter to a Java developer writing queries and migrations.

Five differences matter most day to day: MySQL defaults to REPEATABLE READ with gap locking while PostgreSQL defaults to READ COMMITTED; InnoDB clusters rows by primary key with undo logs while PostgreSQL keeps rows in a heap and cleans old versions with VACUUM; PostgreSQL DDL is transactional while MySQL DDL commits implicitly; PostgreSQL has partial indexes, jsonb with GIN, arrays, ranges and extensions while MySQL indexes JSON through generated columns or multi-valued indexes; and PostgreSQL has INSERT ... ON CONFLICT ... RETURNING while MySQL has INSERT ... ON DUPLICATE KEY UPDATE and no RETURNING.

Why this question is asked

Many Java developers move between MySQL and PostgreSQL during their careers, and JPA makes the two look alike until a migration fails halfway or a query that was fine on one engine deadlocks on the other. Interviewers ask for practical differences to see whether we have written real queries and migrations on both, not just read a feature list. In this answer, we will go through five differences one at a time, each with what it means for our code, and then look at what non-transactional DDL means for Flyway migrations on MySQL.

1Difference 1: Default Isolation and Locking

MySQL InnoDB defaults to REPEATABLE READ. A plain SELECT reads from a snapshot taken at the transaction's first read, and locking reads and updates take next-key and gap locks on the ranges they scan, so they can block inserts into those ranges. PostgreSQL defaults to READ COMMITTED: each statement sees data committed before it began, and SELECT ... FOR UPDATE locks only rows that exist.

SQL
-- the same code, run concurrently by two requests
SELECT * FROM slots WHERE room = 1 AND day = '2026-11-05' FOR UPDATE; -- no row
INSERT INTO slots (room, day) VALUES (1, '2026-11-05');
-- MySQL (REPEATABLE READ): both get gap locks, one INSERT fails with a deadlock (1213)
-- PostgreSQL (READ COMMITTED): no locks on the empty result, both INSERTs succeed (double booking)

Notice that the same check-then-insert code fails differently on each engine, and is wrong on both. The portable answer is a unique constraint, which gives a duplicate key error on either engine.

2Difference 2: How Rows Are Stored

InnoDB stores rows inside the primary key index, the clustered index, and keeps old versions in a separate undo log, cleaned by background purge threads. Secondary indexes point to rows by primary key value. So the primary key choice shapes the table: random UUID keys scatter inserts, and wide keys widen every index.

PostgreSQL stores rows in a heap, an unordered table file, and every index points to a physical row location. An UPDATE writes a new row version into the heap and leaves the old one as a dead tuple until VACUUM, run by autovacuum, reclaims it. So in PostgreSQL we watch table bloat and autovacuum health, and frequent updates to indexed columns are more expensive; in MySQL we watch purge lag and the history list length.

3Difference 3: Transactional DDL

In PostgreSQL, most DDL can run inside a transaction. If a migration creates a table, adds a column and then fails on an index, the whole migration rolls back and the schema is exactly as before. In MySQL, each DDL statement causes an implicit commit, before and after it. Since 8.0, a single DDL statement is atomic, so it either fully happens or not at all, but a script of several statements cannot be rolled back as a unit.

SQL
-- MySQL
START TRANSACTION;
ALTER TABLE customer ADD COLUMN tier TINYINT NOT NULL DEFAULT 0; -- implicit commit: done for good
CREATE INDEX ix_tier ON customer (tierr); -- ERROR 1072: Key column 'tierr' doesn't exist
ROLLBACK; -- the new column stays
-- PostgreSQL
BEGIN;
ALTER TABLE customer ADD COLUMN tier smallint NOT NULL DEFAULT 0;
CREATE INDEX ix_tier ON customer (tierr); -- ERROR: column "tierr" does not exist
ROLLBACK; -- no column, no index

One exception on the PostgreSQL side: CREATE INDEX CONCURRENTLY cannot run inside a transaction block, so migrations that build large indexes online run it on its own.

4Difference 4: Indexes and Data Types

PostgreSQL has a richer toolbox. Partial indexes index only rows matching a condition, such as WHERE status = 'PENDING', which keeps an index on a small hot subset tiny. jsonb stores parsed JSON, and a GIN index can answer containment queries on any key. Arrays, range types with exclusion constraints, and extensions such as PostGIS and pg_trgm cover needs that would otherwise require other systems.

MySQL has a JSON type too. To index a value inside it, we index a generated column or use a functional index, and since 8.0.17 a multi-valued index can index the elements of a JSON array. MySQL has no partial indexes; the usual workaround is a generated column that is NULL for rows we do not want, because NULLs are cheap in the index.

Five differences at a glance
AreaMySQL InnoDBPostgreSQL
Default isolationREPEATABLE READ, gap locksREAD COMMITTED
Storageclustered by PK, undo log, purgeheap, dead tuples, VACUUM
DDLimplicit commit per statementtransactional
Indexing JSON and subsetsgenerated column, multi-valued indexGIN on jsonb, partial indexes
Upsert and returningON DUPLICATE KEY UPDATE, no RETURNINGON CONFLICT, RETURNING

5Difference 5: Upserts and RETURNING

Both engines can insert or update in one statement, with different syntax and semantics. PostgreSQL's ON CONFLICT names the constraint or columns it reacts to, and RETURNING gives back the resulting row, including generated values. MySQL's ON DUPLICATE KEY UPDATE reacts to any unique key, and since there is no RETURNING, we read generated keys through JDBC or LAST_INSERT_ID().

SQL
-- PostgreSQL
INSERT INTO stock (sku, qty) VALUES ('A1', 5)
ON CONFLICT (sku) DO UPDATE SET qty = stock.qty + EXCLUDED.qty
RETURNING qty; -- 12
-- MySQL 8.0.19 and later (row alias instead of the deprecated VALUES())
INSERT INTO stock (sku, qty) VALUES ('A1', 5) AS new
ON DUPLICATE KEY UPDATE qty = stock.qty + new.qty; -- 2 rows affected means an update happened
SELECT qty FROM stock WHERE sku = 'A1'; -- 12

Notice the MySQL rows-affected value: 1 for an insert, 2 for an update of an existing row, 0 if nothing changed. Code that relied on RETURNING needs an extra read on MySQL.

6Follow-up questions

What does non-transactional DDL mean for Flyway migrations on MySQL?

If a versioned migration with several DDL statements fails in the middle, the statements before the failure stay applied, and Flyway records the migration as failed. We then have to inspect the schema, undo or complete the partial changes by hand, and run flyway repair before migrating again. So on MySQL we keep one DDL change per migration file, make statements idempotent where possible, and test migrations against a copy of production first. On PostgreSQL, Flyway wraps each migration in a transaction and a failure leaves no trace.

Is MySQL's REPEATABLE READ the same as PostgreSQL's?

No. Both give a snapshot for plain reads, but MySQL's locking reads and updates work on the latest committed data and take gap locks, while PostgreSQL's REPEATABLE READ raises a serialization error when we try to update a row someone else changed after our snapshot.

Which difference most often surprises Java developers?

In practice, the DDL behaviour during a failed migration and the different locking behaviour under concurrency, because both stay invisible in tests that run one request at a time.

7Conclusion

In this answer, we went through five practical differences. MySQL defaults to REPEATABLE READ with gap locks and PostgreSQL to READ COMMITTED. InnoDB clusters rows by primary key with undo and purge, while PostgreSQL uses heap tables with dead tuples cleaned by VACUUM. PostgreSQL DDL is transactional, while MySQL commits around each DDL statement, so a failed Flyway migration can leave partial changes. PostgreSQL offers partial indexes, jsonb with GIN, arrays, ranges and extensions, while MySQL uses generated columns and multi-valued indexes. And upserts use ON CONFLICT with RETURNING in PostgreSQL but ON DUPLICATE KEY UPDATE without RETURNING in MySQL.

What to say in the interviewAbout 80 seconds aloud

Open with

The first difference is isolation. MySQL defaults to REPEATABLE READ, and its locking reads take next-key and gap locks, so an empty FOR UPDATE can block inserts and cause deadlocks, while PostgreSQL defaults to READ COMMITTED and only locks existing rows.

Then, if they want more

The second is storage. InnoDB clusters rows by primary key and keeps old versions in undo, cleaned by purge, so the primary key choice matters a lot. PostgreSQL uses heap tables where updates create new row versions, and VACUUM cleans the dead ones, so I watch bloat and autovacuum there.

Third, DDL. In PostgreSQL DDL is transactional, so a failed migration rolls back entirely. In MySQL every DDL statement commits implicitly, so a Flyway migration with several statements can fail halfway and leave partial changes that I have to fix by hand and then run flyway repair. That is why I keep one DDL per migration on MySQL. Fourth, features.

PostgreSQL has partial indexes, jsonb with GIN indexes, arrays, ranges and extensions like PostGIS. MySQL has JSON, but I index it with generated columns, functional indexes or multi-valued indexes, and it has no partial indexes. Fifth, upserts.

PostgreSQL has INSERT ON CONFLICT with RETURNING, and MySQL has INSERT ON DUPLICATE KEY UPDATE without RETURNING, so I read generated keys through JDBC.

About this answer

Last updated
12 November 2026
Versions it names
MySQL 8.0
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
MySQL reference manual
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Name five practical differences between MySQL InnoDB and PostgreSQL that matter to a...": https://scrivio-interview-library.netlify.app/databases/mysql/2de488add8/