PostgreSQL 18Dev & Eng
Content type: Concept
7 min read
PostgreSQL 18: Eleven Measurements Show How Joins, Casts, and TOAST Affect Your Queries
- Reading
- 7 min
- Tables
- 5
- Code
- 1
DBA Hollis Varden ran eleven small experiments on PostgreSQL 18.6 and found behaviors every developer should check before writing their next query.
Author Hollis Varden, of the blog Explain, Measured, published a new round of performance tests on PostgreSQL 18.6 on November 25, 2026. It's not a version announcement: it's a battery of small experiments, measured with EXPLAIN (ANALYZE, BUFFERS, TIMING OFF), seeking to confirm (or debunk) common assumptions about how the query planner behaves.
The environment was compiled from source, with default settings: work_mem at 4MB, shared_buffers at 128MB, and 2 parallel workers per gather. The database collation is C. The hardware was a MacBook Air M5 with 10 cores, 24GB of RAM, and internal SSD. Times are medians of 5 runs (3 for the slower ones), after a warm-up.
Two of them changed how I write queries. The rest mostly confirmed things I had only read.
Hollis Varden, author of the blog Explain, Measured
For those who write SQL every day, the value isn't in the overall verdict but in the specific numbers: they show where intuition about the Postgres optimizer matches reality and where it doesn't.
EXISTS and IN Become the Same Semi-Join, JOIN Doesn't
In the test with 200,000 customers and 2,000,000 orders (index on orders.c_id), Varden compared three ways to find customers with at least one order:
| 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 compiled to the same semi-join: the planner stops as soon as it finds the first match. The JOIN with DISTINCT, on the other hand, materialized the combined rows (nearly 2 million) only to remove duplicates afterward. In practice, replacing a JOIN ... DISTINCT with EXISTS isn't a style choice, it's five times less I/O work in the same plan.

The Anti-Join the Optimizer Doesn't Make on Its Own
In the reverse direction, 50,000 customers with no orders at all, the result wasn't symmetric:
| 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 + filter |
Varden's expectation was that the planner would rewrite the LEFT JOIN filtered by IS NULL as an anti-join, the same way it did with EXISTS/IN. That didn't happen when the null check falls on o.id. She didn't test the variant that checks o.c_id (the join column itself) instead of the primary key of the joined table, so that point remains open for anyone who wants to reproduce the experiment.
When the Index Disappears: Wrong Function, Wrong Type
Two classic cases of an ignored index appear side by side. With an index on created_at, filtering for a specific day in two different ways:
-- reads the entire table: 47.6 ms, 24,692 buffers
WHERE date(created_at AT TIME ZONE 'UTC') = '2026-03-01'
-- uses the index: 0.34 ms, 28 buffers
WHERE created_at >= '2026-03-01 00:00+00'
AND created_at < '2026-03-02 00:00+00'The index exists on created_at, not on the result of the function applied to the column. Wrapping the indexed column in a function is the most common way to waste an index without realizing it.
The second case is subtler: WHERE id = 1234567 on a bigint primary key took 0.036 ms. Replacing the literal with id = 1234567.0 took 32.8 ms, with a parallel sequential scan. The literal with a decimal point makes PostgreSQL compare as numeric, forcing an implicit cast on every row of the bigint column and rendering the index useless. An ORM that generates numeric literals carelessly can introduce this cost silently.
TOAST: What Leaves the Row Also Clears the Path
With 20,000 rows containing a random hexadecimal text value, values above approximately 2 kB are moved to the TOAST table. The effect on queries that don't touch that column was direct:
| 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 |
With 1 kB values (still stored inline), even a simple sum(id) needed to read 23.4 MB. As soon as the content was pushed to TOAST, the main table shrank and any query that ignores the large column got faster, even with no index change at all. It's a concrete argument against keeping large payloads (JSON, free text) in the same table as frequently queried columns.
char, varchar, and text: Same Storage, Different Cost
For 2,000,000 short codes like code42, stored in three ways:
| Type | Table size | 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 each value with spaces up to 20 characters and stores that padding, inflating the table by nearly 20%. varchar and text came out practically tied, reinforcing the well-known recommendation to avoid char(n) outside very specific cases.
Timestamps as Text Work, But Are Fragile
Storing '2026-03-01 00:00:10' as text instead of timestamptz costs more per row (20 bytes versus 8) and more for the index (81.2 MB versus 44.9 MB), but the one-week range query came out similar: 2.22 ms versus 2.08 ms. Varden is explicit about why: the text range only works because this format sorts the same way as real dates, and the database collation is C. Changing the collation, or formatting the date differently, breaks this coincidence without warning.
On numeric aggregation, sum() over 2 million values varied little between types: numeric at 98.5 ms, bigint at 77.2 ms, double precision at 80.2 ms. The difference exists, but it's small compared to the other results in the experiment.
The Driver Protocol Is Also a Cost
A test with pgbench compared the three communication protocols for random primary-key lookups, one client, 10 seconds:
Protocol (-M) | Transactions per second |
|---|---|
simple | 52,680 |
extended | 47,337 |
prepared | 69,168 |
The extended mode without prepared statements was the slowest: it sends a parse step on every execution. The prepared mode parses and plans only once. For anyone deciding between a pooler, driver, and whether to use prepared statements in the application, the nearly 32% difference between plain extended and prepared is relevant enough to enter the conversation about connection pool configuration.
OR with Two Indexes Versus UNION: A Tie
With an index on each of two columns, WHERE a = 42 OR b = 7 took 2.24 ms, combining the two indexes via a BitmapOr. Rewriting it as a UNION of two queries took 2.62 ms and read the same 4,010 buffers. Unlike the other cases, here the manual rewrite brought no gain: the optimizer already solves the OR on its own when there's an index on both sides.
Counting Is Expensive, Estimating Is Free
count(*) over the 2 million rows took 21.4 ms. Reading pg_class.reltuples took 0.015 ms and returned exactly 2,000,000. After inserting another 200,000 rows without running ANALYZE, reltuples still showed 2,000,000, but EXPLAIN SELECT * FROM t estimated 2,151,385 rows. The planner scales the stored density by the table's current size, so its estimate moves even when the raw statistic in reltuples hasn't been updated. It's a reminder that exact counting and planning estimates are two different things, with different costs and different guarantees.
What Was Left Out
The full script (run.py) and the raw results (results.json) are linked in the original post. Varden dropped three tests from the final article: a LIKE prefix test that shows nothing relevant on a database with C collation, and two composite index tests whose data didn't match any rows. The omission isn't a flaw, it's methodological honesty, rare enough in database benchmarks to deserve mention.
Translated from the Brazilian Portuguese original · Read the original
Data Egret checklist helps avoid downtime in PostgreSQL DDL and DML migrations
Data Egret, a consultancy, published a set of questions to assess, before running in production, the real risk of an ALTER TABLE, a constraint validation, or a bulk UPDATE in PostgreSQL.





