Another sitting of small measurements. Two of them changed how I write queries. The rest mostly confirmed things I had only read.
Setup
PostgreSQL 18.6, built from source, default settings (work_mem 4MB, shared_buffers 128MB, 2 parallel workers per gather). The database collation is C. MacBook Air M5, 10 cores, 24 GB RAM, internal SSD. Same machine as my previous post.
The main table has 2,000,000 rows. Query times are EXPLAIN (ANALYZE, BUFFERS, TIMING OFF) execution time, median of 5 runs after one warm-up run (3 runs for the slower ones). Buffers are from the top plan node. Sizes are decimal MB. Where I turned parallel workers off, I say so. The script and the raw results are linked at the bottom.
Joins that ask "is there a match"
200,000 customers and 2,000,000 orders, with an index on orders.c_id. 150,000 customers have orders. Parallel workers off.
| Query | Time | Buffers |
|---|---|---|
WHERE EXISTS (SELECT 1 FROM o ...) | 60.6 ms | 2,510 |
WHERE id IN (SELECT c_id FROM o) | 60.2 ms | 2,510 |
SELECT DISTINCT c.id FROM c JOIN o ... | 305.9 ms | 11,892 |
EXISTS and IN became the same semi-join. The JOIN version built all 2M joined rows and then removed the duplicates.
The other direction, the 50,000 customers with no orders:
| Query | Time | Plan |
|---|---|---|
NOT EXISTS (...) | 61.5 ms | Merge Anti Join |
LEFT JOIN o ... WHERE o.id IS NULL | 213.7 ms | Hash Right Join, then a filter |
I expected the planner to turn the LEFT JOIN into an anti-join too. It didn't, with the IS NULL test on o.id. I didn't test the version that checks the join column o.c_id instead.
When an index stops being used
An index on created_at, one day of rows:
| Filter | Time | Buffers |
|---|---|---|
date(created_at AT TIME ZONE 'UTC') = '2026-03-01' | 47.6 ms | 24,692 |
created_at >= '2026-03-01 00:00+00' AND created_at < '2026-03-02 00:00+00' | 0.34 ms | 28 |
The function version read the whole table. The index is on created_at, not on what the function returns.
The same thing happens with a literal of the wrong type. WHERE id = 1234567 on the bigint primary key took 0.036 ms. WHERE id = 1234567.0 took 32.8 ms and a parallel sequential scan. The numeric literal makes PostgreSQL compare as numeric, so the column gets cast on every row and the bigint index can't help.
TOAST
20,000 rows, each with one text value of random hex. Values over roughly 2 kB get moved out of the row into the table's TOAST table.
| Value size | Main table | TOAST table | sum(id) | sum(length(body)) |
|---|---|---|---|---|
| 1 kB | 23.4 MB | 0 MB | 2.69 ms | 16.9 ms |
| 3 kB | 1.0 MB | 81.9 MB | 0.59 ms | 63.8 ms |
| 10 kB | 1.0 MB | 245.8 MB | 0.57 ms | 208.9 ms |
Queries that don't touch the big column got faster once it moved out. The 1 kB values stayed inline, so even sum(id) had to read 23.4 MB.
Types
2,000,000 short codes like code42, stored three ways, parallel workers off:
| Type | Table | WHERE v = 'code42' |
|---|---|---|
char(20) | 104.4 MB | 49.4 ms |
varchar(20) | 87.0 MB | 41.5 ms |
text | 87.0 MB | 39.7 ms |
char(20) pads every value with spaces to 20 characters and stores the padding. varchar and text came out the same.
Timestamps stored as text instead ('2026-03-01 00:00:10'):
| timestamptz | text | |
|---|---|---|
| Bytes per value | 8 | 20 |
| Index | 44.9 MB | 81.2 MB |
| One week range | 2.08 ms, 170 buffers | 2.22 ms, 302 buffers |
The text range only works because this format sorts the same way as the dates, and the database collation is C.
sum() over 2M values, parallel workers off: numeric 98.5 ms, bigint 77.2 ms, double precision 80.2 ms.
Counting and estimating
count(*) on the 2M rows took 21.4 ms. Reading pg_class.reltuples took 0.015 ms and said 2,000,000. After inserting 200,000 more rows without ANALYZE, reltuples still said 2,000,000, but EXPLAIN SELECT * FROM t estimated 2,151,385. The planner scales the stored density by the table's current size, so its guess moves even when reltuples doesn't.
The pgbench protocols
Single-row lookups by random primary key, one client, 10 seconds, median of 3:
-M | Transactions per second |
|---|---|
| simple | 52,680 |
| extended | 47,337 |
| prepared | 69,168 |
Extended without prepared statements was the slowest. It sends a parse step every time. Prepared parses and plans once.
OR against UNION
With an index on each of a and b, WHERE a = 42 OR b = 7 took 2.24 ms. The planner combined both indexes with a BitmapOr. Rewriting it as a UNION of two queries took 2.62 ms and read the same 4,010 buffers. This one came out even.
Files
- run.py: every test above, in order. It also has three tests I left out: a LIKE prefix test that shows nothing on a C-collation database, and two composite-index tests whose data matched zero rows.
- results.json: raw output, including each run