skip to content

RETURNING, with OLD and NEW

get back the rows you changed, and since postgres 18, what they looked like before.

address
everything.writes.returning_old_and_new
part
part 5, changing data
chapter
chapter 3 of 10
reading time
8 min
version
pg 18+

RETURNING makes INSERT, UPDATE, DELETE and MERGE hand back the rows they changed, like a SELECT would. since postgres 18 pg 18+ it can return both sides: old. columns are the row before the statement, new. columns the row after.

one round trip instead of two, and no window for anyone to change the row in between.

the picture

you know how after an UPDATE you usually want to see what it did, so you SELECT the rows again? that's a second query, a second trip to the server, and a gap in which someone else can change those rows. RETURNING is the receipt that comes with the change itself.

think of handing in a form and getting a stamped copy back. before 18 the copy showed the form as filed (for DELETE, as it was when it was thrown away). now you can ask for a before-and-after copy, side by side.

statement =
returning.sql
  1. 1INSERT INTO products (sku, name, category, price)
  2. 2VALUES ('BOO-0201', 'the sql cookbook', 'books', 24.00)
  3. 3RETURNING old.sku AS old_sku, old.price AS old_price,
  4. 4 new.sku AS new_sku, new.price AS new_price;
the result for INSERT
old_skutextold_pricenumericnew_skutextnew_pricenumeric
nullnullBOO-020124.00
(1 row)

INSERT: there was no row before, so every old column is NULL.

real output, postgres 18.6 (PGlite 0.5.12) · returning
switch the statement. the same four columns every time: old.* on the left, new.* on the right. an INSERT has no before, a DELETE has no after, and they show it as NULL. MERGE does both kinds of write in one statement, and says which with merge_action().

every block here starts from a fresh copy of shop, so nothing you run sticks.

the receipt

the most common RETURNING of all: insert a row and get back the id the database made up for it. no SELECT max(id) afterwards (which is wrong the moment two people insert at once), no driver-specific "last insert id" call.

sqldb=shop
INSERT INTO customers (name, country, created_at)
VALUES ('Ravi Shah', 'IN', '2026-05-01 09:00+00')
RETURNING id, name;
output, from postgres 18.6
INSERT 0 1
idint4nametext
2001Ravi Shah
(1 row)

the list after RETURNING is a select list: columns, *, expressions, aliases. it sees the row as it ended up, after defaults, identity columns and any triggers that changed it.

before postgres 18, what you got depended on the statement: INSERT and UPDATE returned the new row, DELETE the old one. you couldn't ask an UPDATE for the old price. now you prefix a column with old. or new.:

sqldb=shop
UPDATE products SET price = round(price * 1.10, 2)
WHERE category = 'books' AND price < 40
RETURNING sku, old.price AS old_price, new.price AS new_price,
          new.price - old.price AS change;
output, from postgres 18.6
UPDATE 3
skutextold_pricenumericnew_pricenumericchangenumeric
BOO-000332.0035.203.20
BOO-002816.9018.591.69
BOO-012016.6018.261.66
(3 rows)

a bare price still means the new value, as it always did. old. and new. just make it explicit, and add the side you couldn't get before.

predict the output
sql
DELETE FROM products WHERE name = 'left-handed teapot'
RETURNING new.price;
sqldb=shop
DELETE FROM products WHERE name = 'left-handed teapot'
RETURNING price, old.price AS old_price, new.price AS new_price;
output, from postgres 18.6
DELETE 1
pricenumericold_pricenumericnew_pricenumeric
19.9919.99null
(1 row)

upserts that say what they did

the place old and new really pay off is an upsert. INSERT ... ON CONFLICT DO UPDATE either inserts a row or updates the one that was there, and until 18 RETURNING couldn't tell you which. old is NULL for a row that was inserted and filled in for a row that was updated, so one more column answers it:

skutextold_pricenumericnew_pricenumericinsertedbool
BOO-000332.0030.00f
BOO-0202null38.00t
(2 rows)
an upsert of two books into products. BOO-0003 existed, so its old price is there and it was updated. BOO-0202 didn't, so old is NULL and it was inserted.
sqldb=shop
INSERT INTO products (sku, name, category, price)
VALUES ('BOO-0003', 'the art of sql', 'books', 30.00),
       ('BOO-0202', 'sql antipatterns', 'books', 38.00)
ON CONFLICT (sku) DO UPDATE SET price = EXCLUDED.price
RETURNING new.sku, old.price AS old_price, new.price AS new_price,
          old.sku IS NULL AS inserted;
output, from postgres 18.6
INSERT 0 2
skutextold_pricenumericnew_pricenumericinsertedbool
BOO-000332.0030.00f
BOO-0202null38.00t
(2 rows)

(the old trick for this was RETURNING (xmax = 0) AS inserted, which reads a system column and leans on how postgres stamps row versions. it worked, mostly. you don't need it anymore. INSERT ... ON CONFLICT has the rest of the upsert.)

if your table happens to have a column or alias called old or new, or you're inside a trigger function where OLD and NEW already mean something, rename them for the statement: RETURNING WITH (OLD AS o, NEW AS n) o.price, n.price.

moving rows in one statement

RETURNING also works inside WITH, and that's how you hand the rows one statement changed to another. archiving cancelled orders is a DELETE and an INSERT, done as one statement:

before anything runs
row counts, after this step
row counts after: before
tbltextnint8
orders10000
orders_archive0
(2 rows)
changed

before: 10,000 orders, an empty archive.

step 1/2
real output, postgres 18.6 (PGlite 0.5.12) · returning
two steps: before, and after one statement. the DELETE's RETURNING feeds the INSERT, so the 317 rows leave orders and land in the archive together. there's no moment where they're in both, or in neither.
sqldb=shop · session archive
CREATE TABLE orders_archive (LIKE orders);
 
WITH gone AS (
  DELETE FROM orders WHERE status = 'cancelled'
  RETURNING *
)
INSERT INTO orders_archive SELECT * FROM gone;
output, from postgres 18.6
#1 · CREATE TABLE
#2 · INSERT 0 317

INSERT 0 317. if the INSERT fails, the DELETE never happened: it's one statement, so it's one transaction. (the cancelled orders' line items went too, through the cascade, and they weren't archived. a real archive would move them first, the same way.)

RETURNING has no order

RETURNING gives rows back in whatever order the statement happened to process them. that's usually the order it found them, which is usually stable, which is exactly the kind of thing that changes the day the plan does. if the order matters, wrap it and sort:

sqldb=shop
WITH changed AS (
  UPDATE products SET price = round(price * 1.10, 2)
  WHERE category = 'books'
  RETURNING id, name, old.price AS old_price, new.price AS new_price
)
SELECT * FROM changed ORDER BY new_price DESC, id LIMIT 5;
output, from postgres 18.6
SELECT 5
idint4nametextold_pricenumericnew_pricenumeric
177cotton cookbook196.45216.10
122spare atlas174.68192.15
53mini notebook153.60168.96
142deluxe cookbook128.41141.25
97deluxe notebook114.68126.15
(5 rows)
one level deeperold values before postgres 18

on 17 and earlier, the usual way to get the before value out of an UPDATE was to join the table to a locked copy of itself:

sqldb=shop
UPDATE products AS p
SET price = round(p.price * 1.10, 2)
FROM (SELECT id, price FROM products
      WHERE category = 'books' AND price < 40
      FOR UPDATE) AS before
WHERE p.id = before.id
RETURNING p.sku, before.price AS old_price, p.price AS new_price;
output, from postgres 18.6
UPDATE 3
skutextold_pricenumericnew_pricenumeric
BOO-000332.0035.20
BOO-002816.9018.59
BOO-012016.6018.26
(3 rows)

the FOR UPDATE matters: without it, another session could change a price between the subquery reading it and the UPDATE writing it, and old_price would be a value that was never really replaced. it works, it's just a lot of ceremony for "what was it before". the release notes for 18 list old and new in RETURNING among the headline changes.

keylines

  • RETURNING hands back the rows a write changed, in the same round trip.
  • since postgres 18, old.col is the row before and new.col the row after.
  • old is NULL for an insert, new is NULL for a delete.
  • in an upsert, old.col IS NULL tells inserted rows from updated ones.
  • RETURNING inside WITH passes changed rows to the next statement. sort it yourself if order matters.