Sub topic 1
What RETURNING Does and Why It Replaces a Follow-Up SELECT
Prerequisites
This chapter assumes you know basic SELECT, INSERT, UPDATE, and DELETE statements. You should also know what primary keys and column defaults are, and have used psql or another PostgreSQL client.
The basic idea
RETURNING is a PostgreSQL clause that makes a data-change statement produce rows as its result. You can use it with INSERT, UPDATE, and DELETE. The returned rows contain the values affected by that statement.
Without RETURNING, an application often changes a row and then runs a second query to find out what happened:
- Insert a row.
- Run
SELECTto find its generated ID. - Use that ID in the application.
RETURNING combines these steps. The database changes the row and sends the requested values back in one statement.
A follow-up SELECT can also introduce a timing problem. Another transaction might change the row between your write and your read. RETURNING reports the result of the same statement that made the change.
What rows are returned?
The RETURNING list works much like a SELECT list. You can name columns, use expressions, or request every column with *.
| Statement | Rows returned |
|---|---|
| `INSERT ... RETURNING` | Rows inserted |
| `UPDATE ... RETURNING` | Rows updated |
| `DELETE ... RETURNING` | Rows deleted |
For example, this table gives each row an identity key and a default status:
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')
RETURNING id, title, status;The INSERT supplies only title. PostgreSQL generates id and applies the default value for status. The result includes all three values, so the application immediately knows the new row:
| id | title | status |
|---|---|---|
| 1 | Write report | open |
RETURNING does not return a separate result for each value expression. It returns a result set, just like SELECT. An INSERT with multiple input rows can therefore return multiple rows.
UPDATE and DELETE
With UPDATE, RETURNING shows the row after the update:
UPDATE tasks
SET status = 'done'
WHERE title = 'Write report'
RETURNING id, title, status;The result contains status = 'done'. If the WHERE condition matches no rows, the statement succeeds but returns zero rows. This lets application code distinguish “the command ran” from “a row was actually changed.”
DELETE can return the rows as they existed before deletion:
DELETE FROM tasks
WHERE status = 'done'
RETURNING *;This is useful for an audit record, a response to an API caller, or an undo operation. The returned row is available to the client even though it no longer exists in the table.
Why one statement is safer
A second SELECT asks the database to inspect the table later. RETURNING asks PostgreSQL to report the rows handled by the current statement. This reduces round trips and ties the result directly to the write.
Use RETURNING when you need generated keys, defaults, updated timestamps, deleted values, or confirmation that a match existed. Return only the columns the caller needs. That keeps the result smaller and avoids exposing unnecessary data.
Exercise
An UPDATE statement with RETURNING id, status runs successfully but produces no result rows. What does that mean?
Show answer
The WHERE clause matched no rows, so PostgreSQL updated nothing. The statement itself was valid, but no row satisfied its condition.