OUT parameters in PostgreSQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time it takes to run a function with OUT parameters changes as the input grows.
Specifically, how does the function's work increase when it returns results through OUT parameters?
Analyze the time complexity of the following PostgreSQL function using OUT parameters.
CREATE OR REPLACE FUNCTION get_user_stats(user_id INT, OUT post_count INT, OUT comment_count INT) AS $$
BEGIN
SELECT COUNT(*) INTO post_count FROM posts WHERE author_id = user_id;
SELECT COUNT(*) INTO comment_count FROM comments WHERE author_id = user_id;
END;
$$ LANGUAGE plpgsql;
This function counts how many posts and comments a user has, returning the counts through OUT parameters.
Look for repeated work inside the function.
- Primary operation: Counting rows in two separate tables filtered by user_id.
- How many times: Each COUNT scans all matching rows once per table.
The time depends on how many posts and comments the user has.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 posts/comments | Scans about 10 rows in posts and 10 in comments |
| 100 posts/comments | Scans about 100 rows in posts and 100 in comments |
| 1000 posts/comments | Scans about 1000 rows in posts and 1000 in comments |
Pattern observation: The work grows roughly in direct proportion to the number of posts and comments the user has.
Time Complexity: O(n)
This means the time to run the function grows linearly with the number of rows it counts for the user.
[X] Wrong: "Using OUT parameters makes the function run faster because it returns multiple values at once."
[OK] Correct: OUT parameters only change how results are returned, not how much work the function does to get those results.
Understanding how functions with OUT parameters behave helps you explain performance clearly and shows you know how database functions work under the hood.
"What if the function counted posts and comments for all users instead of one user? How would the time complexity change?"
Practice
OUT parameters in PostgreSQL functions?Solution
Step 1: Understand OUT parameters role
OUT parameters are used to return multiple values from a function as separate columns.Step 2: Compare with other options
Input variables are declared with IN, not OUT. Temporary tables and single return types are unrelated to OUT parameters.Final Answer:
To allow a function to return multiple values as columns -> Option DQuick Check:
OUT parameters = multiple return columns [OK]
- Confusing OUT with IN parameters
- Thinking OUT creates tables
- Assuming OUT returns a single value
Solution
Step 1: Check correct OUT parameter syntax
OUT parameters are declared inside the parameter list asOUT name type. CREATE FUNCTION f(OUT a INT) RETURNS RECORD AS $$ ... $$ LANGUAGE plpgsql; matches this.Step 2: Analyze other options
CREATE FUNCTION f(a INT OUT) RETURNS VOID AS $$ ... $$ LANGUAGE plpgsql; wrongly places OUT after type. CREATE FUNCTION f(a INT) RETURNS OUT INT AS $$ ... $$ LANGUAGE plpgsql; misuses RETURNS OUT. CREATE FUNCTION f(a INT) RETURNS TABLE(OUT a INT) AS $$ ... $$ LANGUAGE plpgsql; misuses RETURNS TABLE with OUT inside parentheses.Final Answer:
CREATE FUNCTION f(OUT a INT) RETURNS RECORD AS $$ ... $$ LANGUAGE plpgsql; -> Option BQuick Check:
OUT parameters declared as 'OUT name type' [OK]
- Placing OUT after the type
- Using RETURNS OUT instead of parameter list
- Confusing RETURNS TABLE syntax
CREATE FUNCTION get_person() RETURNS TABLE(id INT, name TEXT) AS $$ BEGIN RETURN QUERY SELECT 1, 'Alice'; END; $$ LANGUAGE plpgsql;
What will be the output of
SELECT * FROM get_person();?Solution
Step 1: Understand RETURNS TABLE behavior
RETURNS TABLE defines OUT parameters implicitly, so the function returns rows with columns id and name.Step 2: Analyze the RETURN QUERY statement
The query returns one row with values (1, 'Alice'), so SELECT * FROM function returns that row.Final Answer:
One row with columns id=1 and name='Alice' -> Option AQuick Check:
RETURNS TABLE returns rows with named columns [OK]
- Thinking RETURNS TABLE needs explicit OUT keyword
- Expecting no rows or error
- Assuming single column record output
CREATE FUNCTION calc_sum(a INT, b INT, OUT result INT) AS $$ BEGIN result := a + b; END; $$ LANGUAGE plpgsql;
Which of the following calls will cause an error?
Solution
Step 1: Understand how OUT parameters affect function call
Functions with OUT parameters return a record, soSELECT * FROM calc_sum(2, 3);works and returns result column.Step 2: Analyze each call
SELECT calc_sum(2, 3); returns a record but as a single column, valid. SELECT result FROM calc_sum(2, 3); tries to select a column named 'result' directly from function call without FROM, which is invalid syntax. CALL calc_sum(2, 3, result_var); is invalid syntax for CALL with OUT parameters and will cause an error.Final Answer:
CALL calc_sum(2, 3, result_var); -> Option CQuick Check:
CALL with OUT parameters requires different syntax [OK]
- Selecting OUT column without FROM clause
- Using CALL incorrectly with OUT parameters
- Assuming function returns scalar value
Solution
Step 1: Check OUT parameter declaration and assignment
CREATE FUNCTION text_info(input TEXT, OUT len INT, OUT upper TEXT) AS $$ BEGIN len := length(input); upper := upper(input); END; $$ LANGUAGE plpgsql; declares OUT parameters in the signature and assigns values inside the function body, which is correct.Step 2: Compare other options
CREATE FUNCTION text_info(input TEXT) RETURNS TABLE(len INT, upper TEXT) AS $$ BEGIN len := length(input); upper := upper(input); END; $$ LANGUAGE plpgsql; uses RETURNS TABLE but does not assign values properly. CREATE FUNCTION text_info(input TEXT, OUT len INT, OUT upper TEXT) RETURNS RECORD AS $$ BEGIN len := length(input); upper := upper(input); END; $$ LANGUAGE plpgsql; mixes OUT parameters with RETURNS RECORD incorrectly. CREATE FUNCTION text_info(input TEXT) RETURNS RECORD AS $$ BEGIN RETURN QUERY SELECT length(input), upper(input); END; $$ LANGUAGE plpgsql; returns a record but does not use OUT parameters as requested.Final Answer:
CREATE FUNCTION text_info(input TEXT, OUT len INT, OUT upper TEXT) AS $$ BEGIN len := length(input); upper := upper(input); END; $$ LANGUAGE plpgsql; -> Option AQuick Check:
OUT params declared and assigned inside function [OK]
- Mixing RETURNS TABLE with OUT params incorrectly
- Not assigning OUT parameters inside function
- Using RETURNS RECORD without OUT params
