Machine Room

Postgres vs MySQL

Both are free, open source, and you talk to both in SQL. They differ in where a row lives, what you can add, and what an update costs. The one-page notes from the reel.

Download the PDF

One-page notes, Postgres vs MySQL. Both are free, open source, and you talk to both in SQL. They differ in where a row lives. 1, Same job: tables that link. A shop, a customers table (id, name, city: 12 Ana Lisbon, 42 Ben Leeds, 58 Mei Austin) and an orders table (id, customer_id, total, status: 1001 42 49.00 paid, 1002 12 18.00 pending, 1003 42 85.00 shipped), with arrows from each customer_id to its customer. Both are relational: your data lives in tables that link to each other. For an ordinary app, like a shop with customers and orders, either one does the job. 2, MySQL: the row lives inside the index. SELECT * FROM customers WHERE id = 42; goes into a tree labelled primary key index: a root page 35 | 63 over three leaf pages holding whole rows (12 Ana Lisbon, 27 Ravi Pune, 31 Lea Lyon; 35 Omar Cairo, 42 Ben Leeds, 58 Mei Austin; 63 Sara Oslo, 71 Tom Perth, 88 Yui Osaka); the search ends on 42 Ben Leeds with a tick. The table is its primary key index: the bottom pages hold the whole rows, sorted by id. Once the search finds the id, the whole row is right there. No second trip. MySQL's manual: "the index search leads directly to the page that contains the row data." Other MySQL indexes (say, on city) hold the row's primary key, then search this index for the row. 3, Postgres: same lookup, two steps. The same query goes into the primary key index, whose entry holds only an address: 42 → page 7. A dashed red arrow, second trip, goes to table (heap) · page 7, holding 88 Yui Osaka, 42 Ben Leeds, 12 Ana Lisbon. Rows sit in their own table, the heap. Every index holds only the row's address. So a lookup reads the index, then makes a second trip to fetch the row. Postgres docs: "in an ordinary index scan, each row retrieval requires fetching data from both the index and the heap." The exception is an index-only scan, which "can answer queries from an index alone", when the index holds every column the query asks for. 4, Postgres: the full package. Postgres docs: "a powerful, open source object-relational database system". PostGIS: CREATE EXTENSION postgis; a street map with pins North, Harbor and Old Town; maps and locations; location POINT(-9.137 38.716). pgvector: CREATE EXTENSION vector; a products table (id, name, embedding: 7 Rain jacket [0.12, -0.40, …], 9 Trail shoes [-0.33, 0.25, …]); "Store your vectors with the rest of your data." (pgvector). Arrays: tags text[] holding {sale,outdoor} and {winter}; a column can hold a list. JSON: details jsonb; CREATE INDEX ON products USING GIN (details); SELECT name FROM products WHERE details @> '{"color": "red"}'; Result: Rain jacket, Trail shoes; search inside the documents you store. Fair to know: MySQL has a JSON type too, and MySQL 9.7 has a VECTOR type. 5, The price of an update. UPDATE orders SET status = 'shipped' WHERE id = 1002; Postgres: the orders table keeps the old copy, 1002 12 18.00 pending, greyed, and adds a new copy, 1002 12 18.00 shipped; index on id gets + (1002, …) and index on status gets + (shipped, …); a broom labelled VACUUM. It writes a whole new copy of the row, and often new index entries too. The old copy stays until VACUUM removes it. Postgres docs: "MVCC has some downsides for update queries." MySQL: the row 1002 12 18.00 has pending crossed out and shipped written in place; an undo log holds pending. It changes the row where it sits: "updated in-place". Earlier versions can be rebuilt from the undo log. 6, Which one? A bar chart, Databases developers used: PostgreSQL 57.9%, SQLite 42.1%, MySQL 35.1% (Stack Overflow Developer Survey 2026 · 12,061 answered). MySQL: mostly simple lookups by id, a lot of updates, easy to run; GitHub, Dec 2023: 1,200+ MySQL hosts, 5.5 million queries per second. Postgres: more than plain rows, maps, AI search, searching inside JSON. Starting fresh? No reason either way → go with Postgres. It's what most developers use. MySQL: the row lives inside its primary key index. Postgres: rows sit in their own table, and extensions add maps, vectors and more.

On a phone, press and hold the image to save it.