# PostgreSQL 19: non-blocking REPACK and stable query plans

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.

- Language: en
- Published: 2026-11-23
- Tags: dev, postgresql, database, backend, release
- URL: https://devs7ud.io/en/trends/postgresql-19-repack-plan-advice-wait-for-lsn-en
- Translation (ko): https://devs7ud.io/trends/postgresql-19-repack-plan-advice-wait-for-lsn.md

## Four changes in PostgreSQL 19

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.

![Four tiles: REPACK, plan advice, WAIT FOR LSN and DO SELECT](/blog-img/postgresql-19-repack-plan-advice-wait-for-lsn/overview.png "zoom")

_Image: devs7udio, four changes picked from the PostgreSQL 19 announcement_

## REPACK and CONCURRENTLY

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.

```sql
-- 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.

## A plan change and a slower query

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

**Fixing a plan that got slower**

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

## WAIT FOR LSN

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.

![The primary's write travels to the standby as WAL, and the standby waits, then reads](/blog-img/postgresql-19-repack-plan-advice-wait-for-lsn/wait.png "zoom")

_Image: devs7udio, the order of steps with WAIT FOR LSN_

```sql
-- 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 or create in one statement

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

```sql
-- 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.

## Bulk inserts nearly 2x

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.

## New defaults and removed features

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

| Change | Effect | What to do |
| --- | --- | --- |
| JIT disabled by default | The cost estimates that decided when to use JIT proved unreliable | Enable it yourself if you run many large analytical queries |
| RADIUS authentication removed | It was supported only over UDP, which the notes call unfixably insecure | Move to another authentication method |
| Warning after a successful MD5 login | MD5 was deprecated in PostgreSQL 18 and will be removed in a future release | Move to SCRAM. md5\_password\_warnings turns the warning off |
| io\_workers setting removed | Four new settings replace it | Remove io\_workers from your configuration |
| max\_locks\_per\_transaction default from 64 to 128 | Lock size allocation changed, so a setting must be doubled to match its earlier capacity | Double any value you set yourself |
| standard\_conforming\_strings always on | Old dumps made with the setting off will not load properly | Create the dump with pg\_dump from PostgreSQL 19 |
| btree\_gist indexes on inet and cidr | They could exclude rows that should be returned, so the default opclass is now GiST | pg\_upgrade refuses clusters with these indexes, so drop them first |
| Character classification in full text search and pg\_trgm | It uses the database's locale provider, no longer libc | Reindex 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.

## An order of adoption for a small team

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.

## Sources

- [PostgreSQL 19 Released\!](https://www.postgresql.org/about/news/postgresql-19-released-3449/) — PostgreSQL Global Development Group, November 19
- [E.1. Release 19](https://www.postgresql.org/docs/19/release-19.html) — PostgreSQL 19 release notes
