Explain, Measured

· postgresql

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_memBatchesHash memoryTemp blocks writtenTime
4MB166,938 kB11,917419.7 ms
64MB194,509 kB0513.9 ms
256MB194,509 kB0515.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.

ValueActual rowsEstimate, target 100Estimate, target 1000
50th most common1,8502,133 / 2,333 / 1091,913 / 1,793 / 1,680
250th most common624110 / 110 / 111687 / 700 / 627
2000th most common155110 / 110 / 10966 / 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 statistics1
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 estimateTime
No statistics yet11,65030.0 ms
After ANALYZE197,06728.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 updatedHOT updatesWALTime
c, not indexed100,0007.3 MB0.067 s
b, indexed025.4 MB0.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 sizeRange query buffers
Before delete44.9 MB60
After delete and VACUUM44.9 MB60
After REINDEX4.5 MB9

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).

CommandTime
CREATE INDEX (plus DROP)1.183 s
CREATE INDEX CONCURRENTLY (plus DROP)1.464 s
REINDEX INDEX0.985 s
REINDEX INDEX CONCURRENTLY1.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_mem0 workers2 workers
1MB1.321 s1.300 s
64MB1.136 s0.912 s
1GB1.064 s0.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.

IndexSizeBuilddoc @> '{"user": 42}'doc->>'user' = '42'
none69.0 ms
GIN jsonb_ops70.9 MB1.946 s1.227 ms, 307 buffers56.9 ms
GIN jsonb_path_ops67.3 MB1.196 s0.105 ms, 104 buffers58.7 ms
B-tree on (doc->>'user')6.9 MB0.167 s65.9 ms0.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