Skip to content
Huseyin Babal
Go back

PostgreSQL 19 vs 18: testing the foreign key claim, NOT IN, REPACK and parallel autovacuum

The PostgreSQL 19 announcement says the release “improves the performance of foreign key checks, which in testing made bulk inserts nearly 2x faster”. I wanted to know how that holds up outside the project’s own testing, so I ran Postgres 18.7 and 19.0 side by side on one server and measured that claim plus four other changes from the release notes.

Everything below comes from one run of the scripts linked at the end. Medians are shown; the raw per-run values are in the results archive.

Setup

PartValue
ServerDigitalOcean dedicated droplet c-8: 8 vCPU, 16 GB RAM
Imagespostgres:18.7 and postgres:19.0 (official Docker images)
SettingsSame on both: shared_buffers=2GB, maintenance_work_mem=1GB, max_wal_size=16GB, checkpoint_timeout=30min, jit=off
AutovacuumOff during the tests (set with ALTER SYSTEM), on only for the autovacuum test
RunsInserts and queries 5 runs, maintenance tests 3 runs, median reported

JIT is off on both because Postgres 19 turns it off by default. Leaving it on for 18 would have mixed a second change into every query.

One gotcha while building this: passing -c autovacuum=off on the command line overrides anything you later set with ALTER SYSTEM, so autovacuum could never be switched back on for the autovacuum test. The script sets it through ALTER SYSTEM instead.

1. Foreign key checks

A foreign key from child.parent_id to parent.id means every inserted child row needs a lookup in the parent’s primary key index, plus a FOR KEY SHARE row lock on the parent row so it can’t be deleted mid-insert. That is one extra index lookup per row.

The test: a parent table with 1,000,000 rows and a child table that receives 5,000,000 rows, loaded two ways.

-- INSERT … SELECT
insert into child(parent_id, payload)
select (i % 1000000) + 1, md5(i::text) from generate_series(1, 5000000) i;

-- COPY from a CSV with the same rows
copy child(parent_id, payload) from '/tmp/child.csv' (format csv);
Load (5M rows)18.719.0Speed-up
COPY, with FK31.45 s15.19 s2.07×
INSERT … SELECT, with FK37.62 s21.45 s1.75×
COPY, no FK3.67 s3.35 s1.09×
INSERT … SELECT, no FK9.99 s9.69 s1.03×

The claim holds for COPY into a table with a foreign key. Without the foreign key the two versions are within 3–9 %, so the gain really is in the FK check. On 18.7, the foreign key turned a 3.7 s COPY into 31.5 s; 19.0 removes more than half of that extra time.

Single-row inserts

Most applications don’t bulk load; they insert one row per request. pgbench with 16 clients ran this script for 60 seconds against the table with the foreign key:

\set p random(1, 1000000)
insert into child(parent_id, payload) values (:p, md5(:p::text));
pgbench, 16 clients, 60 s18.719.0
With FK30,375 tps32,940 tps (+8 %)
Without FK35,474 tps36,082 tps (+2 %)

Here the gain is 8 %, not 2×.

2. NOT IN becomes an anti join

Release note: “Allow NOT IN clauses to be converted to more efficient ANTI JOINs when NULLs are not present.”

create table a(x int not null);   -- 5,000,000 rows
create table b(y int not null);   -- 1,000,000 rows (and a second case with 100,000)
select count(*) from a where x not in (select y from b);

An anti join hashes b once and probes it with each row of a. Postgres 18 never rewrites NOT IN that way, because a single NULL in b makes NOT IN return no rows at all, and an anti join would return rows. Postgres 18 can still hash the subquery when it fits in work_mem; when it doesn’t, it falls back to a materialized subplan and rescans it for every row of a.

The plans from the test server:

-- 18.7, b = 1,000,000 rows
Filter: (NOT (ANY (x = (SubPlan 1).col1)))
  SubPlan 1
    ->  Materialize
          ->  Seq Scan on b

-- 19.0, same query
->  Parallel Hash Anti Join
      Hash Cond: (a.x = b.y)
NOT IN18.719.0
b = 1,000,000 rowsdid not finish in 120 s (5 of 5 runs)0.54 s
b = 100,000 rows343 ms280 ms

With the small b, 18.7 already hashes the subquery and the difference is small. With the large one, 18.7 hit the 120 s statement_timeout in every run. Both columns are declared NOT NULL, which is what lets 19 do the rewrite. If the columns are nullable, writing the query as NOT EXISTS is still the way to get an anti join.

3. Bloat: VACUUM FULL, REPACK and REPACK CONCURRENTLY

Postgres never updates a row in place; an update writes a new version and the old one stays until no running query can see it. VACUUM marks that space as reusable but doesn’t shrink the file. To give space back to the operating system the table has to be rewritten.

The test table: 5,000,000 rows with a 200-byte note, then 80 % deleted and vacuumed. It still took 1.35 GB. While each rewrite ran, pgbench sent about 18,000 transactions per second of single-row updates and reads (8 clients, -P 1 for per-second numbers).

CommandVersionTimeSize afterLowest 1-second tps during the command
VACUUM FULL18.72.35 s279 MB0
VACUUM FULL19.01.72 s279 MB460
REPACK19.01.77 s279 MB380
REPACK (CONCURRENTLY)19.02.16 s280 MB12,533

VACUUM FULL and plain REPACK hold an ACCESS EXCLUSIVE lock for the whole rewrite, so traffic stops on both versions. The lowest one-second value depends on how the roughly two-second lock lines up with pgbench’s one-second buckets, so I don’t read the 0 vs 460 as a real difference.

REPACK (CONCURRENTLY) copies the table while reads and writes continue, captures the changes made during the copy with logical decoding (the same machinery logical replication uses), applies them to the new copy, and takes the exclusive lock only for the short file swap at the end. The table needs a primary key. In this test it took 0.4 s longer than VACUUM FULL, and the slowest second still did 12,533 transactions, about two thirds of normal.

4. Parallel autovacuum

Postgres 19 lets autovacuum vacuum indexes with parallel workers, controlled by autovacuum_max_parallel_workers and the per-table storage parameter autovacuum_parallel_workers.

The test table has 5,000,000 rows, 8 indexes and half its rows deleted. Elapsed time comes from the autovacuum log line (log_autovacuum_min_duration = 0):

automatic vacuum of table "postgres.public.wide": index scans: 1
	parallel workers: index vacuum: 4 planned, 4 launched in total
	system usage: CPU: user: 0.80 s, system: 0.12 s, elapsed: 33.90 s
Autovacuum, 8 indexesMedian
18.736.3 s
19.0, no parallel workers35.7 s
19.0, 4 parallel workers33.9 s

Four workers saved about 5 % on this table. Only the index vacuuming runs in parallel.

5. TOAST: lz4 by default

default_toast_compression now defaults to lz4 instead of pglz. 50,000 rows with a 200-object jsonb document each:

18.719.0
pg_column_compression(body)pglz (all rows)lz4 (all rows)
pg_total_relation_size('docs')416,899,072 bytes416,899,072 bytes

Both versions came out at exactly the same size on this data. I didn’t measure compression or decompression speed.

New SQL

Two additions from the release notes that are easy to try:

insert into u values (2, 'a@x') on conflict (email) do select returning *;
copy (select * from u) to stdout (format json);

ON CONFLICT DO SELECT returns the conflicting row instead of doing nothing, and COPY TO can write JSON. Neither syntax exists in 18.

Before you upgrade

Two changes in the “Migration to Version 19” section can break an existing setup:

What this test doesn’t cover

One server size, one run of the suite, Docker on a single droplet. I didn’t test replication, pg_plan_advice, other NOT IN shapes, compression speed, or a bigger machine.

Scripts and raw results

Everything is also in the postgres-18-vs-19/ folder of the db-wars repo.

Sources


Share this post:

Previous Post
Kafka on Kubernetes with Strimzi: partitions, rebalancing, failover, commits and a DLQ in one lab