DEV Community

Cover image for PostgreSQL 19 Is Out: 5 Features Developers Should Know
Muiduzzaman Lipu for Refactorly

Posted on

PostgreSQL 19 Is Out: 5 Features Developers Should Know

PostgreSQL 19 was released on November 19, 2026. The release notes are long and mostly written for DBAs. Below are the five changes I think matter most if you write application code, each one run on postgres:19 (PostgreSQL 19.0) in Docker.

Video version:

1. Get-or-create in one query: ON CONFLICT DO SELECT

The classic "find this tag or create it" problem. Until now, DO NOTHING was the usual tool, but it has a catch:

CREATE TABLE tags (id serial PRIMARY KEY, name text UNIQUE);
INSERT INTO tags (name) VALUES ('python');

-- Postgres 18 and earlier
INSERT INTO tags (name) VALUES ('python')
ON CONFLICT (name) DO NOTHING
RETURNING id, name;
-- (0 rows)
Enter fullscreen mode Exit fullscreen mode

When the row already exists, RETURNING gives you nothing, so you need a second SELECT (and a race between them). PostgreSQL 19 adds DO SELECT:

INSERT INTO tags (name) VALUES ('sql')
ON CONFLICT (name) DO SELECT
RETURNING id, name;
--  id | name
--   2 | sql        ← new row

INSERT INTO tags (name) VALUES ('python')
ON CONFLICT (name) DO SELECT
RETURNING id, name;
--  id |  name
--   1 | python     ← the existing row
Enter fullscreen mode Exit fullscreen mode

You can lock the returned row too: ON CONFLICT (name) DO SELECT FOR UPDATE RETURNING ….

2. Read your own writes on replicas: WAIT FOR LSN

If you send reads to a replica, you've seen this: a user saves something, the next page load hits the replica, and their change isn't there yet. I set up a primary and a streaming replica (with recovery_min_apply_delay = 3s to make the lag visible):

-- on the primary
INSERT INTO comments (body) VALUES ('Great video!');
SELECT pg_current_wal_lsn();          -- 0/030363D0

-- on the replica
SELECT count(*) FROM comments;        -- 0  (not replayed yet)
WAIT FOR LSN '0/030363D0';            -- success, after 2.9 s
SELECT count(*) FROM comments;        -- 1
Enter fullscreen mode Exit fullscreen mode

Your app stores the LSN after a write (in the session, a cookie, wherever) and waits for it before reading from the replica. Always add a timeout. With NO_THROW you get a status instead of an error:

WAIT FOR LSN '0/03036438' WITH (TIMEOUT '500ms', NO_THROW);
--  status
-- ---------
--  timeout
Enter fullscreen mode Exit fullscreen mode

On timeout, fall back to reading from the primary. Without NO_THROW you get ERROR: timed out while waiting for target LSN … to be replayed.

3. IGNORE NULLS in window functions

Filling gaps with the last known value used to need a subquery trick. Now lag, lead, first_value, last_value and nth_value accept IGNORE NULLS:

SELECT t, temp,
       last_value(temp) IGNORE NULLS OVER (ORDER BY t) AS filled
FROM readings;

--  t | temp | filled
--  1 |   20 |     20
--  2 |      |     20
--  3 |      |     20
--  4 |   22 |     22
--  5 |      |     22
Enter fullscreen mode Exit fullscreen mode

4. COPY that survives a bad value: ON_ERROR SET_NULL

COPY users FROM 'users.csv' WITH (FORMAT csv, ON_ERROR SET_NULL);
-- NOTICE:  in 1 row, columns were set to null due to data type incompatibility
-- COPY 3

--  id | email  | age
--   1 | a@x.io |  31
--   2 | b@x.io |       ← the CSV said "thirty"
--   3 | c@x.io |
Enter fullscreen mode Exit fullscreen mode

Before, one bad value failed the whole import.

5. REPACK (CONCURRENTLY): shrink a table without locking it

Deleting rows doesn't shrink the table file. VACUUM FULL does, but it locks the table. The new REPACK command (which replaces VACUUM FULL and CLUSTER; the old commands still work) has a CONCURRENTLY option:

-- 100,000 rows, then delete half
SELECT pg_size_pretty(pg_table_size('orders'));   -- 4360 kB
REPACK (CONCURRENTLY) orders;
SELECT pg_size_pretty(pg_table_size('orders'));   -- 2592 kB
Enter fullscreen mode Exit fullscreen mode

The table needs an identity index (a primary key works). Without one:

ERROR:  cannot execute REPACK (CONCURRENTLY) on relation "users"
DETAIL:  Relation "users" has no identity index.
Enter fullscreen mode Exit fullscreen mode

One upgrade gotcha

json_array() over a subquery with no rows used to return NULL. On 19 it returns []:

SELECT json_array(SELECT 1 WHERE false);
-- PG 18: NULL    PG 19: []
Enter fullscreen mode Exit fullscreen mode

If your code checks for NULL there, it changes behavior after the upgrade.

Cheat sheet

  • ON CONFLICT … DO SELECT RETURNING: get-or-create in one query.
  • WAIT FOR LSN '…' WITH (TIMEOUT '500ms', NO_THROW): read your own writes on replicas.
  • last_value(x) IGNORE NULLS OVER (…): fill gaps.
  • COPY … WITH (ON_ERROR SET_NULL): bad values become NULL.
  • REPACK (CONCURRENTLY) t: shrink without locking (needs a primary key).
  • json_array() of zero rows is now [].

Which one will you use first? 👇


I post one tested coding tip every day on YouTube at Refactorly. The 30-second version of this one is here.

Top comments (0)