Why advanced PL/pgSQL matters in PostgreSQL - Performance Analysis
When using advanced PL/pgSQL, it is important to understand how the time it takes to run your code changes as your data grows.
We want to know how the work done by PL/pgSQL functions scales with input size.
Analyze the time complexity of the following PL/pgSQL function.
CREATE OR REPLACE FUNCTION sum_array_elements(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 elements in an integer array by looping through each element.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each element of the array to add it to a total.
- How many times: Once for each element in the array (n times, where n is the array length).
As the array gets bigger, the function does more additions, one for each element.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of elements; doubling the array doubles the work.
Time Complexity: O(n)
This means the time to run the function grows in a straight line with the size of the input array.
[X] Wrong: "Using a loop in PL/pgSQL is always slow and inefficient regardless of input size."
[OK] Correct: While loops do add work, the time grows linearly with input size, which is expected and manageable for many cases.
Understanding how loops in PL/pgSQL scale helps you write efficient database functions and shows you can think about performance clearly.
"What if we changed the function to call another function inside the loop that also loops over the array? How would the time complexity change?"