Home›Practice›Guided tutorials
Tutorials · a real PostgreSQL in your browser

Guided tutorials

Can I see this happen myself, without installing anything?

You will learn: how an UPDATE creates a new row version, how many pages a scan reads against an index, and why the column order in an index matters.

Each tutorial runs a real PostgreSQL inside this page, using PGlite. Nothing is sent to a server. Every step asks a question first. Make a guess, run the step, then read the answer. I learn more from a wrong guess than from a right one, and I expect you do too.

1. An UPDATE writes a new row version

Watch the physical address of a row, its ctid, change when you update it. Each step asks you to guess first.

Not started.

Create a table with two rows.

CREATE TABLE accounts (id int PRIMARY KEY, balance int);
INSERT INTO accounts VALUES (1, 100), (2, 100);
Two rows, each stored as a tuple in the first 8 KB page of the table file.

ctid is (page, slot). Guess first: where do the two rows sit?

SELECT ctid, xmin, * FROM accounts ORDER BY id;
Both are on page 0, in slots 1 and 2. xmin is the ID of the transaction that created each version.

Guess first: does row 1 keep its ctid after an UPDATE?

UPDATE accounts SET balance = 150 WHERE id = 1;
SELECT ctid, xmin, * FROM accounts ORDER BY id;
No. PostgreSQL wrote a new version of row 1 in a new slot, and xmin is now the updating transaction. The old version is still in the page, dead, until VACUUM removes it, or until HOT pruning reclaims it on the next access to that page. That is MVCC, and it is why update-heavy tables need vacuum.

Guess first: will VACUUM move row 1 back to slot 1?

VACUUM accounts;
SELECT ctid, * FROM accounts ORDER BY id;
No. VACUUM marks the dead slot as free for future inserts, but live rows stay where they are. Only a rewrite, such as VACUUM FULL, moves them, and it locks the table while it does.

2. Count the pages: a scan against an index

The same lookup twice. Read the Buffers line in each plan: it is the number of 8 KB pages the query touched.

Not started.

Create 100,000 users.

CREATE TABLE users AS
  SELECT g AS id, 'user' || g AS username
  FROM generate_series(1, 100000) g;
ANALYZE users;
A table of about 100,000 rows, about 700 pages at this row width.

Guess first: how many pages does finding one username read, with no index?

EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM users WHERE username = 'user50000';
A Seq Scan, and the Buffers line shows every page of the table. Nothing tells PostgreSQL where the row is, so it reads all of them.

Guess first: and with an index on username?

CREATE INDEX users_username ON users (username);
EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM users WHERE username = 'user50000';
An Index Scan with a handful of buffers: the index root, a leaf page, and the table page that holds the row. At 100 times the rows, the index adds about one page. The scan reads 100 times more.

3. Column order in a two-column index

An index on (customer_id, day) is sorted by customer_id first. That order decides which queries it can help.

Not started.

Create 100,000 orders and one two-column index.

CREATE TABLE orders AS
  SELECT g AS id, 1 + g % 1000 AS customer_id,
         date '2025-01-01' + (g % 365) AS day
  FROM generate_series(1, 100000) g;
CREATE INDEX orders_cust_day ON orders (customer_id, day);
ANALYZE orders;
100,000 orders, 1,000 customers, one index with customer_id first.

Guess first: does a filter on both columns use the index?

EXPLAIN SELECT * FROM orders
WHERE customer_id = 42 AND day >= '2025-06-01';
Yes. The plan names orders_cust_day: the leading column narrows to one customer, then the day range is read in order.

Guess first: and a filter on day alone, the second column?

EXPLAIN SELECT * FROM orders WHERE day = '2025-06-01';
Usually a Seq Scan. The index is sorted by customer_id, so the rows for one day are spread across all of it. If you filter by day on its own, it needs its own index, or day first in this one. Check what your plan says: on some data the planner still scans the whole index.

The same SQL runs in any psql session. For MySQL and MongoDB, the same experiments are on the Notebooks page, run on real servers.

Part 2 of 4 · Practice · 9 pagesAll pages

Can I see it happen? Simulations, an index you build yourself, a real PostgreSQL in your browser, experiments run on real servers, and tools for your own database: a query coach, an EXPLAIN reader, a config checker and a health check.