Functions returning SETOF in PostgreSQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When a function returns multiple rows, it processes each row one by one. We want to understand how the time it takes grows as the number of rows grows.
How does the function's work increase when it returns more rows?
Analyze the time complexity of the following code snippet.
CREATE FUNCTION get_active_users()
RETURNS SETOF users AS $$
BEGIN
RETURN QUERY
SELECT * FROM users WHERE active = true;
END;
$$ LANGUAGE plpgsql;
This function returns all active users from the users table one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning the users table rows that match the condition.
- How many times: Once for each active user row returned.
As the number of active users grows, the function processes more rows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 row reads and returns |
| 100 | About 100 row reads and returns |
| 1000 | About 1000 row reads and returns |
Pattern observation: The work grows roughly in direct proportion to the number of rows returned.
Time Complexity: O(n)
This means the time grows linearly with the number of rows the function returns.
[X] Wrong: "The function runs in constant time no matter how many rows it returns."
[OK] Correct: Because the function must process and return each row, the time increases as more rows are returned.
Understanding how functions that return multiple rows scale helps you explain performance in real database tasks. It shows you can think about how data size affects work done.
"What if the function used a LIMIT clause to return only a fixed number of rows? How would the time complexity change?"
Practice
What does a PostgreSQL function declared with RETURNS SETOF do?
Solution
Step 1: Understand the meaning of RETURNS SETOF
In PostgreSQL,RETURNS SETOFmeans the function returns multiple rows, not just one value.Step 2: Compare with other return types
Other return types like scalar or boolean return single values, not sets of rows.Final Answer:
It returns multiple rows as a set of values. -> Option AQuick Check:
RETURNS SETOF = multiple rows [OK]
- Thinking it returns a single value
- Confusing SETOF with JSON return
- Assuming it returns a boolean
Which of the following is the correct syntax to declare a PostgreSQL function that returns a set of integers?
CREATE FUNCTION get_numbers() RETURNS SETOF integer AS $$ BEGIN RETURN QUERY SELECT ...; END; $$ LANGUAGE plpgsql;
Solution
Step 1: Check the correct RETURNS clause
To return multiple rows of integers, useRETURNS SETOF integer.Step 2: Use RETURN QUERY for sets
Inside the function,RETURN QUERY SELECT ...returns multiple rows properly.Final Answer:
CREATE FUNCTION get_numbers() RETURNS SETOF integer AS $$ BEGIN RETURN QUERY SELECT 1; END; $$ LANGUAGE plpgsql; -> Option CQuick Check:
RETURNS SETOF + RETURN QUERY = CREATE FUNCTION get_numbers() RETURNS SETOF integer AS $$ BEGIN RETURN QUERY SELECT 1; END; $$ LANGUAGE plpgsql; [OK]
- Using RETURNS integer instead of SETOF integer
- Using RETURN instead of RETURN QUERY for sets
- Confusing RETURNS TABLE with RETURNS SETOF
Given this function:
CREATE FUNCTION get_even_numbers() RETURNS SETOF integer AS $$ BEGIN RETURN QUERY SELECT generate_series(1,5) WHERE generate_series % 2 = 0; END; $$ LANGUAGE plpgsql;
What will
SELECT * FROM get_even_numbers(); return?Solution
Step 1: Understand generate_series and filter
The function selects numbers from 1 to 5 but filters only even numbers usingWHERE generate_series % 2 = 0.Step 2: Identify even numbers in range
Even numbers between 1 and 5 are 2 and 4.Final Answer:
[2, 4] -> Option DQuick Check:
Even numbers 1-5 = [2,4] [OK]
- Including odd numbers by mistake
- Returning all numbers without filter
- Expecting empty set due to syntax confusion
Identify the error in this function that returns a set of text values:
CREATE FUNCTION get_names() RETURNS SETOF text AS $$ BEGIN RETURN SELECT name FROM users; END; $$ LANGUAGE plpgsql;
Solution
Step 1: Check how to return multiple rows in plpgsql
To return multiple rows, useRETURN QUERY SELECT ...inside the function.Step 2: Identify missing keyword
The function usesRETURN SELECT ...which is invalid; it must beRETURN QUERY SELECT ....Final Answer:
Missing RETURN QUERY before SELECT statement. -> Option AQuick Check:
Use RETURN QUERY for sets [OK]
- Using RETURN instead of RETURN QUERY
- Confusing RETURNS SETOF with RETURNS TABLE
- Thinking LANGUAGE plpgsql disallows set returns
You want to create a function that returns all employees with salary above a given amount. Which is the best way to write this function?
CREATE FUNCTION get_high_salary(min_salary numeric) RETURNS SETOF employees AS $$ BEGIN RETURN QUERY SELECT * FROM employees WHERE salary > min_salary; END; $$ LANGUAGE plpgsql;
What is the correct way to call this function to get all employees earning more than 50000?
Solution
Step 1: Understand function returns SETOF employees
The function returns multiple rows, so it must be called in a FROM clause to get rows.Step 2: Choose correct call syntax
UseSELECT * FROM function_name(args);to get all rows returned by the function.Final Answer:
SELECT * FROM get_high_salary(50000); -> Option BQuick Check:
Call set-returning function in FROM clause [OK]
- Using SELECT function() without FROM
- Using CALL which is for procedures
- Using EXECUTE which is for dynamic SQL
