Postgres functions basics in Supabase - Time & Space Complexity
When using Postgres functions in Supabase, it's important to know how the time to run them changes as you work with more data.
We want to understand how the number of operations grows when calling these functions.
Analyze the time complexity of calling a simple Postgres function that returns rows from a table.
-- SQL function to get all users
CREATE OR REPLACE FUNCTION get_all_users()
RETURNS TABLE(id INT, name TEXT) AS $$
BEGIN
RETURN QUERY SELECT id, name FROM users;
END;
$$ LANGUAGE plpgsql;
-- Call the function
SELECT * FROM get_all_users();
This function fetches all user records from the users table when called.
Look at what happens when the function runs:
- Primary operation: Scanning the users table to return rows.
- How many times: Once per function call, but the amount of data scanned depends on table size.
As the number of users grows, the function reads more rows.
| Input Size (n) | Approx. Rows Returned |
|---|---|
| 10 | 10 rows |
| 100 | 100 rows |
| 1000 | 1000 rows |
Pattern observation: The work grows directly with the number of rows in the table.
Time Complexity: O(n)
This means the time to run the function grows linearly with the number of rows it returns.
[X] Wrong: "Calling a Postgres function always takes the same time no matter the data size."
[OK] Correct: The function's time depends on how much data it processes; more rows mean more work.
Understanding how database functions scale with data size shows you can predict performance and design efficient queries.
"What if the function filtered users by a condition instead of returning all? How would the time complexity change?"
Practice
Solution
Step 1: Understand what Postgres functions do
Postgres functions hold code that can be run repeatedly inside the database.Step 2: Compare options with this purpose
Only To store reusable code inside the database matches this description; others relate to UI, hardware, or backups, which are unrelated.Final Answer:
To store reusable code inside the database -> Option AQuick Check:
Postgres functions = reusable database code [OK]
- Confusing functions with UI components
- Thinking functions manage hardware
- Assuming functions handle backups automatically
Solution
Step 1: Recall Postgres function syntax
The correct syntax starts with CREATE FUNCTION, includes RETURNS, and uses $$ to wrap the body.Step 2: Match options to syntax
Only CREATE FUNCTION function_name() RETURNS type AS $$ BEGIN ... END; $$ LANGUAGE plpgsql; matches the exact syntax; others use incorrect keywords or order.Final Answer:
CREATE FUNCTION function_name() RETURNS type AS $$ BEGIN ... END; $$ LANGUAGE plpgsql; -> Option CQuick Check:
CREATE FUNCTION + RETURNS + $$ ... $$ = correct syntax [OK]
- Using MAKE or DEFINE instead of CREATE
- Omitting LANGUAGE plpgsql
- Not wrapping function body with $$
SELECT add_two_numbers(3, 4); return?CREATE FUNCTION add_two_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;
Solution
Step 1: Understand function behavior
The function adds two integers a and b and returns their sum.Step 2: Calculate the result for inputs 3 and 4
3 + 4 equals 7, so the function returns 7.Final Answer:
7 -> Option DQuick Check:
3 + 4 = 7 [OK]
- Concatenating numbers as strings (34)
- Assuming function is missing or returns NULL
- Confusing syntax errors with output
CREATE FUNCTION multiply_by_two(x integer) RETURNS integer AS $$ BEGIN RETURN x * 2 END; $$ LANGUAGE plpgsql;
Solution
Step 1: Check function syntax carefully
In PL/pgSQL, statements inside BEGIN...END must end with semicolons.Step 2: Locate missing semicolon
The RETURN statement lacks a semicolon at the end, causing a syntax error.Final Answer:
Missing semicolon after RETURN statement -> Option AQuick Check:
Statements inside BEGIN...END need semicolons [OK]
- Forgetting semicolon after RETURN
- Assuming function name or language is wrong
- Thinking function is correct without semicolon
Solution
Step 1: Understand the condition to check empty string
The function must check if input equals empty string ('') and return NULL in that case.Step 2: Evaluate each option's condition
CREATE FUNCTION text_length_if_not_empty(input text) RETURNS integer AS $$ BEGIN IF input = '' THEN RETURN NULL; ELSE RETURN length(input); END IF; END; $$ LANGUAGE plpgsql; correctly checks input = '' and returns NULL; CREATE FUNCTION text_length_if_not_empty(input text) RETURNS integer AS $$ BEGIN RETURN length(input); END; $$ LANGUAGE plpgsql; always returns length; CREATE FUNCTION text_length_if_not_empty(input text) RETURNS integer AS $$ BEGIN IF input IS NULL THEN RETURN 0; ELSE RETURN length(input); END IF; END; $$ LANGUAGE plpgsql; checks NULL instead of empty; CREATE FUNCTION text_length_if_not_empty(input text) RETURNS integer AS $$ BEGIN IF input = NULL THEN RETURN NULL; ELSE RETURN length(input); END IF; END; $$ LANGUAGE plpgsql; uses incorrect NULL comparison.Final Answer:
CREATE FUNCTION text_length_if_not_empty(input text) RETURNS integer AS $$ BEGIN IF input = '' THEN RETURN NULL; ELSE RETURN length(input); END IF; END; $$ LANGUAGE plpgsql; -> Option BQuick Check:
Check empty string with = '' and return NULL [OK]
- Using input = NULL instead of IS NULL
- Not checking for empty string
- Returning 0 instead of NULL for empty input
Ready to prove it?
Pass the test and this pattern moves unknown
