What Changes When You Upgrade from PostgreSQL 18 to 19
PostgreSQL 19 came out on November 19. If you’re already running 18, the real question isn’t what’s in the release announcement, it’s which changes you’ll actually feel in day-to-day work.
I spun up PostgreSQL 18.6 and 19.0 side by side to try out the upgrades I’d care about most on a real app. I wanted to see whether batch inserts into tables with foreign keys get noticeably faster without changing any SQL, how much of a pg_repack maintenance job 19’s new native command can replace, and how well a few quality-of-life additions hold up in practice.
Where PostgreSQL 19 saved time
I started by timing two common patterns against the exact same data on PostgreSQL 18.6 and 19.0. One inserts 100,000 rows into a table with a foreign key, and the other finds missing IDs using NOT IN. Here are the median execution times across seven runs (full environment details are at the bottom of the post):
- Insert 100,000 rows, no foreign key went from 47.3 ms on 18.6 to 45.4 ms on 19.0 (−3.9%).
- Insert the same 100,000 rows with a foreign key dropped from 202.0 ms to 119.0 ms (−41.1%).
- Count 250,000 missing IDs with `NOT IN` dropped from 51.1 ms to 38.6 ms (−24.5%).
Faster foreign-key inserts
If you’ve ever run a bulk import into a table with foreign keys, you’ve probably noticed how much slower it is than writing to a plain table. To isolate that difference, I inserted 100,000 child rows in one statement, where each row pointed to a different parent by primary key, and compared it against an identical table without the foreign key.
EXPLAIN (ANALYZE, TIMING OFF)
INSERT INTO benchmark.child_fk (id, parent_id)
SELECT n, n FROM generate_series(1, 100000) AS s(n);Total insert time dropped from 202.0 ms on 18.6 down to 119.0 ms on 19.0. If you subtract the no-foreign-key baseline (about 47 ms on 18.6 and 45 ms on 19.0), the extra overhead of the foreign-key check itself fell roughly in half, from around 155 ms to 74 ms. That’s an estimate of the added constraint cost rather than a direct timer inside the check, and in my test the parent keys were already warm in memory and inserted in order. I didn’t test what happens when PostgreSQL has to read those parent keys off disk.
Under the hood, PostgreSQL 19 can check the parent’s unique index directly for this kind of insert. That shortcut doesn’t apply to partitioned parents or range-based temporal foreign keys. Deleting a parent row is also a different story, since PostgreSQL still has to find any matching children, which is why it still pays to index the child’s foreign key.
Faster NOT IN queries
When you want to find rows in one table that don’t exist in another, NOT IN is usually the first query that comes to mind. For years, PostgreSQL folks have had to rewrite those queries as NOT EXISTS to avoid a slower plan. I tested the original NOT IN form by counting IDs from a 500,000-row table that were missing from a 250,000-row table.
EXPLAIN (ANALYZE, BUFFERS, TIMING OFF)
SELECT count(*)
FROM benchmark.anti_lhs AS lhs
WHERE lhs.id NOT IN (SELECT rhs.id FROM benchmark.anti_rhs AS rhs);Both versions returned the same 250,000 missing IDs, but they got there differently. PostgreSQL 18 ran a sequential scan and checked each row against a hashed subplan filter. PostgreSQL 19 turned the query into a Hash Anti Join, which is the same faster join strategy 18 already chose when you wrote NOT EXISTS instead.
Watch out for `NULL` values. Both ID columns in this test wereNOT NULLprimary keys, so PostgreSQL 19 could safely swap in the anti join. If either side can containNULL,NOT INbehaves differently thanNOT EXISTSbecause an expression like3 NOT IN (1, NULL)evaluates to unknown and drops the row. In those nullable cases, you won’t get the rewrite.
Reclaim disk space without a separate tool
Say you run a cleanup job and delete six months of old events. Ordinary VACUUM marks that space as reusable for future rows, and a lot of the time that’s all you need. If you actually want the disk space back, VACUUM FULL rewrites the table, but it blocks readers and writers until it finishes. On 18, getting an online rewrite meant setting up pg_repack, which required installing both the server extension and a matching client binary.
On 19, you can run `REPACK (CONCURRENTLY) events;` directly in SQL while reads and writes keep flowing. It works on any normal (logged) table that has a primary key or another index configured as its replica identity. If events is partitioned, point it at an eligible leaf partition rather than the parent table. You also need to run it outside a transaction block and leave enough free disk space for at least one extra copy of the table and its indexes.
Plan around long-running queries. Even withCONCURRENTLY, the final file swap still needs a brief exclusive lock. A long-running report that already readeventscan hold up that swap, and an already-open transaction that readseventsfor the first time after the swap can see an empty table. Check for slow reports before scheduling a repack.
To see it in action, I deleted most of a test table so 2,353 rows were left scattered across it, then ran REPACK (CONCURRENTLY, VERBOSE, ANALYZE) repack_demo and checked the total size before and after.
SELECT
count(*) AS live_rows,
pg_size_pretty(pg_total_relation_size('repack_demo')) AS table_and_indexes
FROM repack_demo;The table and its indexes shrank by more than half, from 1,504 kB (about 1.5 MB) down to 656 kB, with all 2,353 rows intact. That confirms the space reclamation, though it doesn’t tell you how long writers would wait for the final swap on a busy production database.
There’s still at least one good reason to keep `pg_repack --tablespace` around. If you want to move a busy table onto another tablespace while keeping it online, pg_repack can move the table as it rewrites it. PostgreSQL 19’s REPACK leaves the table where it is. If you have a maintenance window, `ALTER TABLE events SET TABLESPACE archive` will move the table natively, though reads and writes have to wait for the move to finish and indexes stay behind unless you move them separately.
Try a different query plan without touching your SQL
If you’ve ever stared at a slow query plan and wondered what would happen if PostgreSQL scanned a table differently or joined tables in another order, on 18 you usually reached for an extension like pg_hint_plan. PostgreSQL 19 ships a built-in module called `pg_plan_advice` that lets you inspect the planner’s decisions and nudge them during debugging.
You can load it for a single session and ask EXPLAIN to print the advice that describes its current plan.
LOAD 'pg_plan_advice';
EXPLAIN (COSTS OFF, PLAN_ADVICE)
SELECT f.id, d.label
FROM join_fact AS f
JOIN join_dim AS d ON d.id = f.dim_id
WHERE f.dim_id = 1;Right below the normal query plan, PostgreSQL 19.0 printed this summary of the choices it made.
Generated Plan Advice:
JOIN_ORDER(d f)
NESTED_LOOP_PLAIN(f)
SEQ_SCAN(d)
BITMAP_HEAP_SCAN(f)
NO_GATHER(f d)The line that jumps out here is BITMAP_HEAP_SCAN(f), which tells you PostgreSQL used the index on dim_id to find the matching rows in join_fact. If you want to test what happens when it scans the whole table instead, pass 'SEQ_SCAN(f)' into pg_plan_advice.advice and run EXPLAIN again.
SET pg_plan_advice.advice = 'SEQ_SCAN(f)';
EXPLAIN (COSTS OFF)
SELECT f.id, d.label
FROM join_fact AS f
JOIN join_dim AS d ON d.id = f.dim_id
WHERE f.dim_id = 1;Here’s the part of the plan that changed.
-> Seq Scan on join_fact f
Filter: (dim_id = 1)
Supplied Plan Advice:
SEQ_SCAN(f) /* matched */You can tell immediately that PostgreSQL followed the nudge because the node switched to Seq Scan on join_fact f and the advice block marked it /* matched */. From there, you’d time the query with and without the advice to see whether the alternate plan actually helps.
LOAD is all you need to experiment in one session. Once you find advice that fixes a real bottleneck and want to apply it in production without editing application code, PostgreSQL 19 also includes `pg_stash_advice` to attach advice by query ID. Load it in the sessions running that query, or add it to shared_preload_libraries (which requires a restart) to cover every session. New advice kicks in the next time PostgreSQL plans the query, so it won’t alter already-cached plans and it doesn’t read pg_hint_plan comment syntax.
Get the row back without a second query
A pattern I run into all the time is inserting a record if it doesn’t exist yet, or getting the existing ID back if it’s already there (Rails even has `create_or_find_by` for this). On PostgreSQL 18, INSERT ... RETURNING id works for brand-new rows, but ON CONFLICT DO NOTHING RETURNING id gives you nothing back when a row already exists. You end up running a second SELECT just to fetch the ID.
On 19, `ON CONFLICT DO SELECT` handles both paths in one round-trip.
INSERT INTO article (slug) VALUES ('pg19')
ON CONFLICT (slug) DO SELECT
RETURNING id;Run that statement a second time with the same slug and you get the existing ID right back without writing a dummy update to the row. If your app needs more columns for the response, you can ask for them directly with RETURNING id, slug. One small thing to keep in mind is that PostgreSQL still pulls the next ID from the sequence before checking for a conflict, so your IDs can end up with gaps.
Wait for a read replica to catch up
Once that insert returns an ID on your primary database, there’s another classic snag if the very next request tries to look up that article on a read replica. Because replication happens asynchronously, the replica might not have replayed the write yet, so the row looks missing.
If you can, the simplest fix is still to send that follow-up read to the primary. When you do need to use a replica, PostgreSQL 19 adds `WAIT FOR LSN`, which lets a replica pause until it has replayed up to a specific write-ahead log position (LSN).
After committing the write on the primary, grab its current log position.
SELECT pg_current_wal_insert_lsn();Then pass that LSN to the replica before you run your SELECT.
WAIT FOR LSN '0/0306EE20' WITH (TIMEOUT '100ms', NO_THROW);I tested this against a physical standby with replay paused on purpose. Right after the insert, the row wasn’t visible on the replica and WAIT FOR LSN returned timeout. As soon as I resumed replay, the wait returned success and the article showed up immediately. In production, only read from that replica when the wait returns success. If it returns timeout, fall back to the primary, and if it returns not in recovery, check which server is actually the primary before retrying.
What to check before scheduling an upgrade
If you’re thinking about upgrading, start with a restored copy of your database and read through the migration notes. Here’s the short checklist I’d run through before booking a maintenance window:
- Verify your extensions and run `pg_upgrade --check`. Make sure your database host or packages already ship PostgreSQL 19 builds for every extension you rely on, then run `pg_upgrade` with
--checkagainst your restored copy. - Replace `io_workers` if you customized it. That setting was removed in 19, so take it out of your config and switch to
io_min_workersandio_max_workersif you need explicit bounds. - Check long-running reports if they rely on JIT. PostgreSQL 19 disables JIT by default, so benchmark any heavy analytical queries with the setting you plan to run in production.
- Double custom `max_locks_per_transaction` values. PostgreSQL 19 raised the default from 64 to 128. If you left it at the default, there’s nothing to change, but if you set a custom value on 18, double it on 19 to keep the same lock capacity.
- Plan for sequences if you upgrade via logical replication. Native sequence synchronization requires a publisher running 19 or newer. If your publisher is still on 18, you’ll still need to pause writes, wait for the subscriber to catch up, and sync its sequences manually before letting it assign new IDs.
If foreign-key checks are slowing down a big import, try that workload on a restored copy running 19 to see how much time you actually get back. If native REPACK could retire a pg_repack cron job, test how long writers wait during the swap under realistic traffic. PostgreSQL 18 receives fixes through November 2030, so there’s no rush. If none of these changes solve a pain point you have today, I’d stay current on 18 until you’ve had time to test 19 against your own workload.
How I tested this
All of the numbers and outputs above came from disposable Docker containers running PostgreSQL 18.6 and 19.0 on the same Apple Silicon machine. These settings kept the comparison consistent rather than mirroring a production server:
- Container setup. Each container got 4 CPUs and 4 GiB of memory, with
shared_buffersat 512 MB andwork_memat 64 MB. I turned off JIT, parallel query, and autovacuum for the timing runs, and keptfsyncandsynchronous_commiton. - Query benchmarks. For the foreign-key test, I loaded 250,000 parent IDs with
generate_series, ranANALYZE, and tested two child tables with identicalbigintprimary keys andbigint NOT NULLparent IDs (onlychild_fkhad the foreign key). ForNOT IN, the left table held IDs 1 through 500,000 and the right held the even IDs, both withbigintprimary keys. Using one client, I warmed each query twice and recordedExecution Timeacross seven runs, alternating which version ran first (truncating the child table outside the timed statement). OneNOT INrun on 19.0 spiked to 50.4 ms, while the median was 38.6 ms. - Repack and plan advice. For
REPACK, I created 10,000 rows with a primary key and an index ongroup_id, ran some deletes and updates followed by a plain repack to start compact, then deleted every row whose ID wasn’t divisible by four before runningREPACK (CONCURRENTLY). Forpg_plan_advice, I joined 10,000join_factrows to 10join_dimrows with an index onjoin_fact.dim_idand ranANALYZEon both tables first. - Replica reads. I created
articlewith an identity primary key and a uniqueslug, built a physical standby withpg_basebackup -R, paused WAL replay on the standby, ran theON CONFLICT DO SELECTinsert twice on the primary, and verified thetimeoutandsuccesspaths withWAIT FOR LSN.

Comments
No comments yet