Sub topic 12
RETURNING OLD and NEW (PostgreSQL 18+)
Assume you know basic UPDATE, INSERT, DELETE, primary keys, and PostgreSQL clients such as psql. This chapter focuses on PostgreSQL 18's ability to show both versions of a row in one RETURNING clause.
Earlier PostgreSQL versions let RETURNING show the row produced by a data-changing statement. PostgreSQL 18 adds the OLD and NEW row names:
OLDis the row before the change.NEWis the row after the change.
This is useful when an application needs to record exactly what changed without running a separate SELECT.
Comparing values with UPDATE
An UPDATE can return a before-and-after audit record:
CREATE TABLE tasks (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
title text NOT NULL,
status text NOT NULL DEFAULT 'open'
);
INSERT INTO tasks (title) VALUES ('Write report');
UPDATE tasks
SET status = 'done'
WHERE title = 'Write report'
RETURNING
id,
old.status AS previous_status,
new.status AS current_status;The result contains open as previous_status and done as current_status. The names old and new refer to row versions, so old.status and new.status select their respective column values.
You can also return an expression that uses both versions:
UPDATE tasks
SET status = 'done'
WHERE id = 1
RETURNING
id,
old.status || ' -> ' || new.status AS transition;The expression returns open -> done. This is often more useful to an event logger than two separate columns.
What OLD and NEW mean for each command
The available row version depends on the operation.
| Statement | `OLD` contains | `NEW` contains |
|---|---|---|
| `INSERT` | No previous row, normally null | The inserted row |
| `UPDATE` | The row before the update | The row after the update |
| `DELETE` | The deleted row | No replacement row, normally null |
For DELETE, use OLD to capture values before they disappear:
DELETE FROM tasks
WHERE status = 'done'
RETURNING
old.id,
old.title,
old.status;For a plain INSERT, NEW represents the inserted row. It includes values produced by identity columns and defaults:
INSERT INTO tasks (title)
VALUES ('Review pull request')
RETURNING new.id, new.title, new.status;Aliasing the row versions
If old or new conflicts with a name in a larger statement, PostgreSQL 18 allows aliases for the row versions:
UPDATE tasks
SET status = 'done'
WHERE id = 1
RETURNING WITH (OLD AS before, NEW AS after)
before.status AS previous_status,
after.status AS current_status;The aliases apply only inside the RETURNING list. They do not rename table columns.
A trigger can change the final values that RETURNING reports. For example, a BEFORE UPDATE trigger can rewrite NEW.status. The returned new.status is the value after that trigger's change. This makes RETURNING suitable for reporting the row version PostgreSQL actually stores, rather than merely the values requested by the original statement.
Upserts and no-match behavior
With INSERT ... ON CONFLICT DO UPDATE, the meaning depends on the path taken. On the insert path, NEW is the inserted row. On the update path, OLD is the conflicting stored row and NEW is the updated row.
If an UPDATE or DELETE matches no rows, RETURNING produces no result rows. A client must distinguish “one returned row” from “no returned rows”; a successful SQL command does not guarantee that a row changed.
Exercise
In an UPDATE that changes status from open to done, which expressions return the previous and current values?
Show answer
old.status returns open, and new.status returns done.