Hash joins, planner statistics and index upkeep on PostgreSQL 18
A second sitting of small measurements this week, mostly about what the planner believes and what indexes cost to keep. One result went the opposite way from what I expected.
Setup
PostgreSQL 18.6, built from source, default settings (work_mem 4MB, shared_buffers 128MB, maintenance_work_mem 64MB, 2 parallel workers per gather, 2 parallel maintenance workers). The database collation is C. MacBook Air M5, 10 cores, 24 GB RAM, internal SSD. Same machine as my previous posts.
Query times are EXPLAIN (ANALYZE, BUFFERS, TIMING OFF) execution time, median of 3 runs after one warm-up run. Buffers are from the top plan node. Index build times are wall clock from psql, median of 3, and include connecting. Sizes are decimal MB. Where I turned parallel workers off, I say so. The script and the raw results are linked at the bottom.
work_mem and a hash join
Two tables of 2,000,000 rows each, joined on an unindexed int column. Parallel workers off.
| work_mem | Batches | Hash memory | Temp blocks written | Time |
|---|---|---|---|---|
| 4MB | 16 | 6,938 kB | 11,917 | 419.7 ms |
| 64MB | 1 | 94,509 kB | 0 | 513.9 ms |
| 256MB | 1 | 94,509 kB | 0 | 515.0 ms |
I expected the single-batch join to win. It was about 90 ms slower, every run. The 4MB join spilled to temp files on an SSD and still came out ahead. My guess is that the smaller hash table fits the CPU caches better, but I didn't test that.
Statistics target on a skewed column
2,000,000 rows, 20,000 distinct values, heavily skewed. I picked values by how often they occur and compared the planner's row estimate with the real count. ANALYZE samples rows, so I ran it three times for each setting.
| Value | Actual rows | Estimate, target 100 | Estimate, target 1000 |
|---|---|---|---|
| 50th most common | 1,850 | 2,133 / 2,333 / 109 | 1,913 / 1,793 / 1,680 |
| 250th most common | 624 | 110 / 110 / 111 | 687 / 700 / 627 |
| 2000th most common | 155 | 110 / 110 / 109 | 66 / 66 / 66 |
With the default target of 100, the 50th value sometimes fell out of the most-common list and the estimate dropped to 109. Target 1000 kept a 1,000-value list and fixed the middle of the range. For the rare value it made the estimate worse, 66 against 155.
Correlated columns
1,000,000 rows where the zip code decides the city (10,000 zips, 100 cities). WHERE city = 5 AND zip = 523 returns 100 rows.
| Estimate | |
|---|---|
| Default statistics | 1 |
After CREATE STATISTICS ... (dependencies) | 100 |
The planner multiplies the two selectivities unless it knows the columns move together. Grouping by both columns had the same problem: estimated 100,000 groups, 10,008 after (ndistinct) statistics, 10,000 real.
Before the first ANALYZE
2,000,000 rows inserted into an empty table with autovacuum off. WHERE v = 3 matches 200,000 rows.
| Row estimate | Time | |
|---|---|---|
| No statistics yet | 11,650 | 30.0 ms |
| After ANALYZE | 197,067 | 28.4 ms |
The estimate was off by 17 times, but the time barely moved here. On a join, a wrong estimate like that can pick a much worse plan. I didn't test a join.
HOT and indexed columns
1,000,000 rows, fillfactor 80, an index on b, autovacuum off. I updated every 10th row, 100,000 rows.
| Column updated | HOT updates | WAL | Time |
|---|---|---|---|
c, not indexed | 100,000 | 7.3 MB | 0.067 s |
b, indexed | 0 | 25.4 MB | 0.191 s |
Changing an indexed column means new entries in every index on the table, so none of those updates were HOT.
Index bloat after deletes
An index on a random double column, 2,000,000 rows. I deleted 90% of the rows and ran VACUUM.
| Index size | Range query buffers | |
|---|---|---|
| Before delete | 44.9 MB | 60 |
| After delete and VACUUM | 44.9 MB | 60 |
| After REINDEX | 4.5 MB | 9 |
VACUUM didn't shrink the index, and the range query read the same number of pages. REINDEX took 0.051 s on what was left.
Building indexes
5,000,000 random bigints, an index on that column (112.3 MB).
| Command | Time |
|---|---|
CREATE INDEX (plus DROP) | 1.183 s |
CREATE INDEX CONCURRENTLY (plus DROP) | 1.464 s |
REINDEX INDEX | 0.985 s |
REINDEX INDEX CONCURRENTLY | 1.503 s |
On an idle table CONCURRENTLY cost about 0.3 to 0.5 s more. The plain versions block writes for the whole build. I didn't test with writes running.
The same CREATE INDEX with different maintenance_work_mem:
| maintenance_work_mem | 0 workers | 2 workers |
|---|---|---|
| 1MB | 1.321 s | 1.300 s |
| 64MB | 1.136 s | 0.912 s |
| 1GB | 1.064 s | 0.732 s |
With 1MB, the parallel workers didn't help.
A boolean index
2,000,000 rows, an index on each of two boolean columns. One is true for half the rows, the other for 1%. Both indexes were 13.9 MB. Parallel workers off for the timings.
For the 50% column the planner didn't use the index at all and chose a sequential scan. For the 1% column it used an index scan: 30.5 ms, against 47.0 ms after I dropped the index. Those 20,000 rows are spread over the whole table, so the index scan still read 20,020 buffers.
Three ways to index jsonb
1,000,000 documents like {"user": 42, "kind": "a", "tags": [...], "n": 1}, 134.3 MB. 100 documents have user 42. Parallel workers off.
| Index | Size | Build | doc @> '{"user": 42}' | doc->>'user' = '42' |
|---|---|---|---|---|
| none | 69.0 ms | |||
GIN jsonb_ops | 70.9 MB | 1.946 s | 1.227 ms, 307 buffers | 56.9 ms |
GIN jsonb_path_ops | 67.3 MB | 1.196 s | 0.105 ms, 104 buffers | 58.7 ms |
B-tree on (doc->>'user') | 6.9 MB | 0.167 s | 65.9 ms | 0.104 ms |
Neither GIN index helped the ->> comparison. The B-tree helped only that one. jsonb_path_ops came out barely smaller than jsonb_ops here, but more than 10 times faster for this containment query. It can't answer key-exists queries like ?, which I didn't measure.
Files
- run.py: every test above, in order. It also has a prepared-statement test on skewed data that I left out, because my rare value matched zero rows.
- results.json: raw output, including each run