Hollis Varden: Eleven small measurements on PostgreSQL 18: joins, casts, TOAST and types

2026-11-25来源 Planet PostgreSQLEnglish

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.

QueryTimeBuffers
WHERE EXISTS (SELECT 1 FROM o ...)60.6 ms2,510
WHERE id IN (SELECT c_id FROM o)60.2 ms2,510
SELECT DISTINCT c.id FROM c JOIN o ...305.9 ms11,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:

QueryTimePlan
NOT EXISTS (...)61.5 msMerge Anti Join
LEFT JOIN o ... WHERE o.id IS NULL213.7 msHash 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:

FilterTimeBuffers
date(created_at AT TIME ZONE 'UTC') = '2026-03-01'47.6 ms24,692
created_at >= '2026-03-01 00:00+00' AND created_at < '2026-03-02 00:00+00'0.34 ms28

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 sizeMain tableTOAST tablesum(id)sum(length(body))
1 kB23.4 MB0 MB2.69 ms16.9 ms
3 kB1.0 MB81.9 MB0.59 ms63.8 ms
10 kB1.0 MB245.8 MB0.57 ms208.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:

TypeTableWHERE v = 'code42'
char(20)104.4 MB49.4 ms
varchar(20)87.0 MB41.5 ms
text87.0 MB39.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'):

timestamptztext
Bytes per value820
Index44.9 MB81.2 MB
One week range2.08 ms, 170 buffers2.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:

-MTransactions per second
simple52,680
extended47,337
prepared69,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

原文:https://www.a8q9.cn/en/article/hollis-varden-eleven-small-measurements-on-postgresql-18-joins-casts-toast-and-t-9e533f.html

相关文章