Function creation syntax in PostgreSQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we create a function in PostgreSQL, it's important to understand how the time it takes to run grows as the function handles more data or more calls.
We want to know how the function's execution time changes when the input or usage increases.
Analyze the time complexity of the following function creation code.
CREATE OR REPLACE FUNCTION sum_array(arr integer[])
RETURNS integer AS $$
DECLARE
total integer := 0;
i integer;
BEGIN
FOR i IN 1..array_length(arr, 1) LOOP
total := total + arr[i];
END LOOP;
RETURN total;
END;
$$ LANGUAGE plpgsql;
This function sums all numbers in an integer array by looping through each element.
Look for repeated actions inside the function.
- Primary operation: Looping through each element of the array to add it to total.
- How many times: Once for each element in the array (array length times).
The time to run grows as the array gets bigger because the function adds each number one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The number of operations grows directly with the size of the input array.
Time Complexity: O(n)
This means the time to complete the function grows in a straight line as the input array gets bigger.
[X] Wrong: "The function runs in the same time no matter how big the array is."
[OK] Correct: Because the function adds each element one by one, more elements mean more work and more time.
Understanding how function execution time grows helps you write efficient database code and explain your reasoning clearly in conversations.
"What if we changed the function to sum only the first half of the array? How would the time complexity change?"
Practice
CREATE FUNCTION statement in PostgreSQL?Solution
Step 1: Understand the role of functions in PostgreSQL
Functions store reusable code inside the database to perform tasks repeatedly.Step 2: Identify what
This statement defines a new function with parameters, return type, and body.CREATE FUNCTIONdoesFinal Answer:
To define a reusable block of code that can be called later -> Option DQuick Check:
CREATE FUNCTION defines reusable code [OK]
- Confusing function creation with table creation
- Thinking it inserts or deletes data directly
- Mixing up functions with SQL commands like SELECT or DELETE
Solution
Step 1: Check the correct syntax for function creation
The syntax requires parentheses after the function name, the RETURNS clause, the function body enclosed in $$, and the LANGUAGE specified.Step 2: Validate each option
CREATE FUNCTION myfunc() RETURNS int AS $$ BEGIN RETURN 1; END; $$ LANGUAGE plpgsql; correctly uses parentheses, RETURNS int, AS $$ ... $$, and LANGUAGE plpgsql. Others miss parentheses, AS $$, or semicolons.Final Answer:
CREATE FUNCTION myfunc() RETURNS int AS $$ BEGIN RETURN 1; END; $$ LANGUAGE plpgsql; -> Option AQuick Check:
Correct syntax includes parentheses, RETURNS, AS $$, LANGUAGE [OK]
- Omitting parentheses after function name
- Missing AS $$ ... $$ around function body
- Not specifying LANGUAGE plpgsql
- Forgetting semicolons inside function body
SELECT add_one(5);?
CREATE FUNCTION add_one(x integer) RETURNS integer AS $$ BEGIN RETURN x + 1; END; $$ LANGUAGE plpgsql;
Solution
Step 1: Understand the function logic
The function takes an integer input x and returns x + 1.Step 2: Apply the input value 5
Calling add_one(5) returns 5 + 1 = 6.Final Answer:
6 -> Option AQuick Check:
Input 5 plus 1 equals 6 [OK]
- Confusing input and output values
- Expecting syntax error due to unfamiliarity
- Assuming function returns NULL without reason
CREATE FUNCTION multiply_by_two(x integer) RETURNS integer AS $$ BEGIN RETURN x * 2 END; $$ LANGUAGE plpgsql;
Solution
Step 1: Review function body syntax
In PL/pgSQL, each statement inside the function body must end with a semicolon.Step 2: Locate missing semicolon
The RETURN statement lacks a semicolon afterx * 2, causing a syntax error.Final Answer:
Missing semicolon after RETURN statement -> Option CQuick Check:
Statements inside function need semicolons [OK]
- Forgetting semicolon after RETURN
- Misplacing LANGUAGE clause
- Omitting RETURNS clause
- Using invalid function names
concat_names that takes two text parameters and returns their concatenation separated by a space. Which of the following is the correct function definition?Solution
Step 1: Understand string concatenation in PostgreSQL
PostgreSQL uses the || operator to concatenate strings.Step 2: Evaluate each option's concatenation method
CREATE FUNCTION concat_names(a text, b text) RETURNS text AS $$ BEGIN RETURN a || ' ' || b; END; $$ LANGUAGE plpgsql; uses || correctly with BEGIN...END and semicolons. CREATE FUNCTION concat_names(a text, b text) RETURNS text AS $$ RETURN a + ' ' + b; $$ LANGUAGE plpgsql; uses + which is invalid for text. CREATE FUNCTION concat_names(a text, b text) RETURNS text AS $$ BEGIN RETURN concat(a, ' ', b); END $$ LANGUAGE plpgsql; misses semicolon after END. CREATE FUNCTION concat_names(a text, b text) RETURNS text AS $$ BEGIN RETURN a & ' ' & b; END; $$ LANGUAGE plpgsql; uses & which is invalid.Final Answer:
CREATE FUNCTION concat_names(a text, b text) RETURNS text AS $$ BEGIN RETURN a || ' ' || b; END; $$ LANGUAGE plpgsql; -> Option BQuick Check:
Use || for text concatenation in PL/pgSQL [OK]
- Using + or & instead of || for strings
- Forgetting semicolon after END
- Missing BEGIN...END block for multiple statements
