Skip to content

PostgreSQL 19 came out on November 19. This post covers four changes a small team will notice first, REPACK (CONCURRENTLY), the pg_plan_advice and pg_stash_advice extensions, WAIT FOR LSN, and INSERT ... ON CONFLICT DO SELECT, plus a table of compatibility changes to check before upgrading.

Published

The PostgreSQL Global Development Group released PostgreSQL 19 on November 19. The announcement sums up the release as simpler administration and simpler development. This post picks four changes from the announcement and the release notes that a small team will notice first.

  • REPACK (CONCURRENTLY) reclaims unused table space without blocking reads or writes.
  • pg_plan_advice and pg_stash_advice let you fix a query whose plan changed and got slower, without changing the application.
  • WAIT FOR LSN makes a standby wait until it can read the data you just wrote.
  • INSERT ... ON CONFLICT DO SELECT does "get or create" in one statement.

Image: devs7udio, four changes picked from the PostgreSQL 19 announcement

Tables that get many updates and deletes build up unused space. The announcement calls it bloat and says it can also hurt query performance significantly. Until now the tools were VACUUM FULL and CLUSTER, and both blocked access to the table until they finished.

REPACK in PostgreSQL 19 reclaims that space and can reorder a table's data to match an index. REPACK (CONCURRENTLY) does the same without blocking reads or writes. REPACK replaces VACUUM FULL and CLUSTER, and both stay available for compatibility. The release notes say the two commands did similar things under confusing names, so they were unified.

Source codesql
-- Rewrite the table and reclaim the unused space
REPACK employees;

-- Reorder the rows to match an index
REPACK employees USING INDEX employees_ind;

-- Follow the index used before, without blocking reads or writes
REPACK (CONCURRENTLY) employees USING INDEX;

The REPACK documentation also lists how CONCURRENTLY works and where it cannot be used.

  • Most of the work runs under a SHARE UPDATE EXCLUSIVE lock. An ACCESS EXCLUSIVE lock is taken only in the final phase that swaps the files. Changes made in the meantime are captured with logical decoding and applied.
  • It cannot be used on partitioned tables, UNLOGGED tables or materialized views. The same goes for a table with no primary key and no index-based replica identity.
  • It cannot run inside a transaction block, and it needs room for one more replication slot. The new max_repack_replication_slots setting sets that limit.
  • It might fail to complete if other transactions run DDL on the table.

Progress shows up in the new pg_stat_progress_repack system view.

The PostgreSQL query planner usually picks an efficient plan, but the plan can change as the data changes, and sometimes the new plan is slower. PostgreSQL 19 adds two extensions for this.

  • pg_plan_advice stabilizes plan choices and lets you try different plans with a small "plan advice" language. The PLAN_ADVICE option of EXPLAIN shows the chosen plan as an advice string, and putting that string in the pg_plan_advice.advice setting makes the planner take the same choices again.
  • pg_stash_advice stores advice and applies it automatically to queries with a matching query identifier. This is the part behind the announcement's "fix a slow plan without changing your application".
The data distribution changes
The planner picks another plan
The query gets slower
EXPLAIN (PLAN_ADVICE)read the advice string of the good plan
pg_stash_advicestore the query identifier and the advice
Applied automatically to that query
  • The data distribution changes → The planner picks another plan
  • The planner picks another plan → The query gets slower
  • The query gets slower → EXPLAIN (PLAN_ADVICE)
  • EXPLAIN (PLAN_ADVICE) → pg_stash_advice
  • pg_stash_advice → Applied automatically to that query
Source: drawn from the PostgreSQL 19 announcement and the pg_plan_advice and pg_stash_advice documentation.

Both documents are direct about the limits.

  • The planner adjusting plans as data changes is normally an advantage. If advice prevents that, a very poor plan may be chosen, so the documentation says to use it only where and as much as needed.
  • Advice only chooses among plans the planner considers viable. It cannot force a plan the planner refused to consider.
  • Aggregation and set operations such as UNION cannot be controlled with advice yet.
  • Advice is consulted only when a query is planned. It does not affect plans that are already cached.
  • pg_stash_advice has no sophisticated security model yet. The documentation says to assume that the contents of stashed advice strings may become visible to non-privileged users.

Applications that send reads to standbys share an old problem. A read on the standby right after a write on the primary may not see that write yet. WAIT FOR LSN in PostgreSQL 19 lets a session on a standby wait until the WAL has been replayed up to a specific LSN before it runs a query. An LSN is a position in the WAL.

Image: devs7udio, the order of steps with WAIT FOR LSN

Source codesql
-- Primary: write, then get the LSN
UPDATE movie SET genre = 'Dramatic' WHERE genre = 'Drama';
SELECT pg_current_wal_insert_lsn();

-- Standby: wait until that LSN is replayed, then read
WAIT FOR LSN '0/0306EE20';
SELECT * FROM movie WHERE genre = 'Drama';

-- With a timeout, and a status string instead of an error
WAIT FOR LSN '0/0306EE20' WITH (MODE 'standby_write', TIMEOUT '100ms', NO_THROW);

According to the WAIT documentation, the command waits indefinitely when no timeout is given. The default mode is standby_replay, which waits for replay, and other modes wait only for the WAL to be written or flushed to disk. With NO_THROW, a timeout comes back as a status string, not an error.

Logical replication is also easier to start. Enabling it often required a server restart before. When wal_level is replica, PostgreSQL 19 raises the effective WAL level to logical automatically once you create the first logical replication slot. The new effective_wal_level setting shows the level in use. Sequence values can now be replicated logically too, published with FOR ALL SEQUENCES in CREATE PUBLICATION.

"Get it if it exists, create it if it does not" is a common pattern. It used to take a separate INSERT and SELECT, or a DO UPDATE that changes nothing. INSERT ... ON CONFLICT DO SELECT in PostgreSQL 19 returns the existing conflicting row through RETURNING. The announcement says it saves a network round trip.

Source codesql
-- Insert if possible, otherwise return the existing row
INSERT INTO distributors (did, dname) VALUES (11, 'Global Electronics')
    ON CONFLICT (did) DO SELECT
    RETURNING *;

The INSERT documentation sets three conditions. A RETURNING clause is required, a conflict target (conflict_target) must be given, and you need SELECT privilege on the table. Adding FOR UPDATE or FOR SHARE also locks the returned row.

The announcement lists smaller additions as well.

  • COPY can output JSON. With the FORCE_ARRAY option the output is a single JSON array.
  • The SQL/JSON path language gains string methods such as lower(), upper(), replace(), split_part() and btrim().
  • The window functions lead(), lag(), first_value(), last_value() and nth_value() can skip NULL values with IGNORE NULLS.
  • random() can also generate dates and timestamps within a range.

Some workloads get faster with no change to the application. The announcement says the performance of foreign key checks improved, which in testing made bulk inserts nearly 2x faster.

  • Bulk inserts with foreign keys
    2x

    A testing result from the PostgreSQL Global Development Group, nearly 2x

Source: the PostgreSQL 19 announcement. It does not give the test conditions.

Other improvements from the same paragraph and the release notes:

  • NOT IN clauses are converted to more efficient anti-joins when NULL values are not present.
  • Aggregation can run before joins, which reduces the number of rows to process.
  • When io_method is worker, the default, the number of I/O workers adjusts automatically between io_min_workers and io_max_workers.
  • Autovacuum can vacuum indexes with parallel workers and uses a new scoring system to decide which tables go first.
  • Queries that read a table can mark pages as all-visible during execution, which leaves less work for later vacuums.
  • LISTEN and NOTIFY scale better in multi-channel workloads. Per the release notes, NOTIFY now wakes only the backends listening to that notification.

"Migration to Version 19" in the release notes collects the changes that may break compatibility with earlier releases. Moving data from any previous release takes a dump and restore with pg_dumpall, or pg_upgrade, or logical replication. The table below carries the items a small team is most likely to hit.

데이터 표
JIT disabled by defaultThe cost estimates that decided when to use JIT proved unreliableEnable it yourself if you run many large analytical queries
RADIUS authentication removedIt was supported only over UDP, which the notes call unfixably insecureMove to another authentication method
Warning after a successful MD5 loginMD5 was deprecated in PostgreSQL 18 and will be removed in a future releaseMove to SCRAM. md5_password_warnings turns the warning off
io_workers setting removedFour new settings replace itRemove io_workers from your configuration
max_locks_per_transaction default from 64 to 128Lock size allocation changed, so a setting must be doubled to match its earlier capacityDouble any value you set yourself
standard_conforming_strings always onOld dumps made with the setting off will not load properlyCreate the dump with pg_dump from PostgreSQL 19
btree_gist indexes on inet and cidrThey could exclude rows that should be returned, so the default opclass is now GiSTpg_upgrade refuses clusters with these indexes, so drop them first
Character classification in full text search and pg_trgmIt uses the database's locale provider, no longer libcReindex the affected indexes after pg_upgrade

Source: Migration to Version 19 in the PostgreSQL 19 release notes, and the announcement

One more default changed. Per the announcement, when PostgreSQL is built with LZ4 support the default TOAST compression method is now lz4. The release notes also disallow carriage returns and line feeds in database, role and tablespace names, and a json_array() call that returns no rows now gives an empty JSON array where it used to give NULL.

Reading: most of the large features in this release are about not stopping a running service. Cleaning up a table, starting logical replication and fixing a query that got slower all used to need a maintenance window or a new deploy. For a small team, finding that window is a cost in itself, so these changes may matter more than the new syntax.

Reading: the order of adoption depends on how you run the database.

  • A team on managed PostgreSQL waits until the provider offers 19. In the meantime, check the compatibility table against your own settings. MD5 authentication, io_workers and analytical queries that lean on JIT come first.
  • A team that runs its own servers can try REPACK (CONCURRENTLY) on a replica or a test environment first. The table needs a primary key, and there must be room for a replication slot.
  • Use plan advice only for the query that has a problem. As the documentation itself warns, more advice leaves the planner less room to respond to changing data.
  • An application that splits reads to standbys should consider WAIT FOR LSN together with a timeout. Without one it waits indefinitely.

The "nearly 2x" figure for bulk inserts is a testing result from the development group, and the conditions are not published. The announcement cannot tell you whether your own tables will see the same gain.