Sub topic 5
Expressions, aliases and `*` in the `RETURNING` List
This chapter assumes you know basic SELECT, INSERT, UPDATE, and DELETE, and that you have used PostgreSQL through psql or another client. The RETURNING clause does more than return raw columns. It can calculate values, rename output columns, and expand a row into all of its columns.
Return columns and calculated values
A RETURNING list looks like a SELECT list:
RETURNING column_name, expression AS output_name;An expression is a value PostgreSQL calculates from columns, constants, operators, or functions. The expression is evaluated for each row affected by the data-changing statement.
This example returns the original price, the discounted price, and a readable label:
CREATE TABLE products (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text NOT NULL,
price_cents integer NOT NULL,
stock integer NOT NULL
);
INSERT INTO products (name, price_cents, stock)
VALUES ('Keyboard', 7500, 12);
UPDATE products
SET price_cents = price_cents - 500,
stock = stock - 1
WHERE name = 'Keyboard'
RETURNING
id,
name,
price_cents AS new_price_cents,
price_cents / 100.0 AS new_price_dollars,
CASE
WHEN stock = 0 THEN 'out of stock'
WHEN stock < 5 THEN 'low stock'
ELSE 'in stock'
END AS stock_label;The names after AS are output column labels. They do not rename columns in the table. Without an alias, PostgreSQL chooses a label based on the expression, which can be inconvenient for application code.
You can use functions and concatenation too:
UPDATE products
SET stock = stock + 5
WHERE id = 1
RETURNING
concat(name, ' #', id) AS display_name,
stock,
now() AS reported_at;The returned reported_at value is calculated while the statement runs. It is not automatically stored in the table.
Using `*` and table-qualified `*`
The * expression returns every column from the affected target row:
DELETE FROM products
WHERE stock = 0
RETURNING *;This is useful when you need the complete deleted row for an audit record or an undo operation. It can also make application code fragile: adding a column later changes the returned shape.
You can mix * with individual expressions:
UPDATE products
SET price_cents = price_cents + 100
WHERE id = 1
RETURNING *, price_cents / 100.0 AS price_dollars;Here, price_cents appears once through * and again as the calculated price_dollars value. Duplicate values are allowed, but the client must handle the resulting column names carefully.
When the target table has an alias, qualify the columns through that alias:
UPDATE products AS p
SET stock = p.stock - 1
WHERE p.id = 1
RETURNING p.id, p.name, p.stock;The alias qualifies references. It does not change the labels returned to the client. Use AS in the RETURNING list when you need stable, explicit labels.
What values are available?
For INSERT, expressions normally see the inserted row, including values supplied by defaults. For UPDATE, they see the row after the update. For DELETE, they see the row immediately before deletion.
A WHERE condition that matches no rows produces no RETURNING rows. It does not produce a row containing nulls.
Exercise
Exercise
Write an UPDATE that raises every product price by 10% and returns the product name plus the new price in dollars with the output label price_dollars.
Show answer
UPDATE products SET pricecents = round(pricecents * 1.10) RETURNING name, pricecents / 100.0 AS pricedollars;