Sub topic 6
Returning rows with SETOF and RETURNS TABLE
This chapter assumes you know basic SELECT statements, PostgreSQL data types, and how to run SQL in a PostgreSQL client. A set-returning function is a function that produces zero or more rows instead of one scalar value.
Choosing a row-shaped return type
Use RETURNS SETOF some_type when the function returns rows whose structure already exists as a PostgreSQL type, such as a table row:
CREATE FUNCTION orders_for_customer(p_customer_id integer)
RETURNS SETOF orders
LANGUAGE sql
STABLE
AS $$
SELECT o
FROM orders AS o
WHERE o.customer_id = p_customer_id
ORDER BY o.ordered_at DESC;
$$;Because orders is a table, it is also a composite type. Each result row has the table's complete row structure.
Call the function in the FROM clause when you want its columns:
SELECT id, ordered_at, total
FROM orders_for_customer(42);If you write SELECT orders_for_customer(42), PostgreSQL returns each table row as one composite value. That form is valid, but usually less convenient.
Naming the returned columns
RETURNS TABLE is useful when the function returns a selected subset of columns or calculated values. It declares the output column names and types directly:
CREATE TABLE orders (
id integer PRIMARY KEY,
customer_id integer NOT NULL,
ordered_at timestamptz NOT NULL,
total numeric(10, 2) NOT NULL
);
INSERT INTO orders (id, customer_id, ordered_at, total)
VALUES
(1, 42, '2025-01-10 09:00+00', 25.50),
(2, 42, '2025-02-03 14:30+00', 80.00),
(3, 7, '2025-02-04 10:15+00', 12.75);
CREATE FUNCTION recent_orders(
p_customer_id integer,
p_limit integer DEFAULT 10
)
RETURNS TABLE (
order_id integer,
ordered_at timestamptz,
total numeric
)
LANGUAGE sql
STABLE
AS $$
SELECT o.id, o.ordered_at, o.total
FROM orders AS o
WHERE o.customer_id = p_customer_id
ORDER BY o.ordered_at DESC
LIMIT p_limit;
$$;
SELECT *
FROM recent_orders(42, 5);RETURNS TABLE makes this function set-returning. The query can therefore return no rows, one row, or many rows.
The selected expressions must match the declared output columns in number and compatible type. An alias such as order_id is not required inside the function because the return declaration supplies the public names.
Empty results and ordering
A set-returning function with no matching rows returns an empty result set. It does not return a row containing NULL. This distinction matters when using it in joins or counting results.
The ORDER BY inside the function controls the order produced by that query, but callers should add their own ORDER BY when the final ordering is important:
SELECT *
FROM recent_orders(42)
ORDER BY ordered_at DESC, order_id DESC;Using a set-returning function with other rows
You can join a set-returning function like a table. LATERAL allows the function argument to use a column from an earlier table expression:
SELECT c.id, r.order_id, r.total
FROM customers AS c
CROSS JOIN LATERAL recent_orders(c.id, 3) AS r;A common error is calling a multi-row function in a scalar expression where PostgreSQL expects one value. Put the function in FROM, or use a scalar subquery only when the query guarantees at most one row.
Exercise
What happens when recent_orders is called for a customer with no matching orders?
Show answer
It returns zero rows, not one row whose columns contain NULL.