Sub topic 3
Seeing the New Row Values After UPDATE
This chapter assumes you know basic SELECT and UPDATE statements, WHERE clauses, primary keys, and how to run SQL in psql or another PostgreSQL client.
An UPDATE changes rows but normally does not show the changed data. Add a RETURNING clause to make PostgreSQL send the affected rows back as the statement result:
UPDATE table_name
SET column_name = expression
WHERE condition
RETURNING column_name;The returned values are the values after PostgreSQL applies the update. This lets an application update a row and receive its current state without running a separate SELECT.
A basic UPDATE with RETURNING
Consider a task table:
CREATE TABLE tasks (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
title text NOT NULL,
status text NOT NULL DEFAULT 'open',
updated_at timestamptz NOT NULL DEFAULT now()
);
INSERT INTO tasks (title)
VALUES ('Write report'), ('Review PR');Change one task to done and return its new values:
UPDATE tasks
SET status = 'done',
updated_at = now()
WHERE title = 'Write report'
RETURNING id, title, status, updated_at;A possible result is:
| id | title | status | updated_at |
|---|---|---|---|
| 1 | Write report | done | 2026-11-27 21:20:00+00 |
The exact timestamp depends on when the statement runs. RETURNING produces a result set, so a client can read it like the result of a SELECT.
If the WHERE clause matches several rows, PostgreSQL returns one row for each updated row. If it matches no rows, the statement succeeds but returns zero rows.
flowchart TD
A[Find rows matching WHERE] --> B[Apply SET expressions]
B --> C[Run applicable triggers]
C --> D[Return post-update values]caption: How UPDATE ... RETURNING produces its result alt: A flow from matching rows through updates and triggers to the returned post-update values
Return expressions, not only columns
The RETURNING list accepts expressions. You can rename a result with AS, calculate a value, or return every column with *.
UPDATE tasks
SET status = 'done',
updated_at = now()
WHERE id = 2
RETURNING
id,
title,
status,
updated_at,
status = 'done' AS is_complete;The expression status = 'done' is evaluated using the updated row, so is_complete is true for this result.
Use RETURNING * when the caller needs the complete updated row:
UPDATE tasks
SET status = 'archived'
WHERE status = 'done'
RETURNING *;For a stable application interface, listing columns explicitly is usually better than *. Adding a column to the table will otherwise change the shape of the returned result.
> Note > The RETURNING result is not a row count. A client must consume the returned rows. To know whether exactly one row changed, inspect the number of rows returned or use a condition that identifies one row, such as a primary key.
Detecting a missing row
RETURNING makes optimistic updates straightforward. An optimistic update assumes the row is still eligible for change and checks whether the update returned a row.
UPDATE tasks
SET status = 'done',
updated_at = now()
WHERE id = 1
AND status = 'open'
RETURNING id, status, updated_at;If the result contains one row, the task was open and is now done. If it contains no rows, either the task does not exist or its status was no longer open. This avoids changing a row and then issuing a separate query to discover what happened.
Exercise
Exercise
Write an UPDATE statement that changes task ID 3 to 'inprogress', sets updatedat to the current time, and returns the task's ID, title, and new status.
Show answer
UPDATE tasks SET status = 'inprogress', updatedat = now() WHERE id = 3 RETURNING id, title, status;