Variable declaration and assignment in PostgreSQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Let's explore how the time needed to run code changes when we declare and assign variables in PostgreSQL.
We want to know how the work grows as we add more variables or assignments.
Analyze the time complexity of the following code snippet.
DO $$
DECLARE
counter INTEGER := 0;
total INTEGER := 100;
BEGIN
counter := total;
END $$;
This code declares two variables and assigns a value to one of them inside a block.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Variable declaration and assignment
- How many times: Each happens once in this code
When you add more variables or assignments, the work grows in a simple way.
| Input Size (number of variables) | Approx. Operations |
|---|---|
| 10 | 10 declarations and assignments |
| 100 | 100 declarations and assignments |
| 1000 | 1000 declarations and assignments |
Pattern observation: The work grows directly with the number of variables.
Time Complexity: O(n)
This means the time grows in a straight line as you add more variables or assignments.
[X] Wrong: "Declaring variables takes no time or is instant no matter how many."
[OK] Correct: Each declaration and assignment takes some time, so more variables mean more work.
Understanding how simple operations like variable assignments scale helps you reason about bigger database tasks confidently.
"What if we declared variables inside a loop that runs n times? How would the time complexity change?"
Practice
counter of type integer in a PL/pgSQL block?Solution
Step 1: Understand variable declaration syntax
In PL/pgSQL, variables are declared inside aDECLAREsection without repeating the DECLARE keyword for each variable. The correct syntax is:variable_name data_type;Step 2: Identify the correct declaration
The linecounter integer;correctly declares the variable inside the DECLARE block. Including the DECLARE keyword before each variable is incorrect.Final Answer:
counter integer; -> Option BQuick Check:
Declare variables inside DECLARE block without repeating DECLARE [OK]
- Omitting DECLARE keyword
- Assigning value during declaration without :=
- Using := in declaration line
count after it has been declared in PL/pgSQL?Solution
Step 1: Recall assignment operator in PL/pgSQL
PL/pgSQL uses:=to assign values to variables, not=or==.Step 2: Identify correct assignment syntax
count := 10; usescount := 10;which is the correct way to assign a value.Final Answer:
count := 10; -> Option DQuick Check:
Use := for assignment in PL/pgSQL [OK]
- Using = instead of :=
- Using SET keyword incorrectly
- Using == like in other languages
DECLARE total integer := 5; BEGIN total := total + 3; RAISE NOTICE '%', total; END;
What will be the output when this block runs?
Solution
Step 1: Analyze initial value assignment
The variabletotalis declared and initialized to 5.Step 2: Calculate the new value after addition
The statementtotal := total + 3;adds 3 to 5, resulting in 8.Final Answer:
8 -> Option AQuick Check:
5 + 3 = 8 [OK]
- Ignoring initial value and assuming zero
- Confusing assignment operator with equality
- Expecting error due to missing BEGIN
DECLARE name text; BEGIN name = 'Alice'; END;
Solution
Step 1: Check variable declaration
The variablenameis declared correctly with typetext.Step 2: Check assignment syntax
The assignment uses=which is incorrect in PL/pgSQL; it should use:=.Final Answer:
Incorrect assignment operator used -> Option AQuick Check:
Use := for assignment, not = [OK]
- Using = instead of :=
- Forgetting semicolon after assignment
- Confusing declaration and assignment syntax
a and b as integers, assign a the value 4, and then assign b the value of a multiplied by 3. Which of the following PL/pgSQL code snippets correctly does this?Solution
Step 1: Check variable declaration and initialization
DECLARE a integer; b integer; BEGIN a := 4; b := a * 3; END; declares both variables without initial values, then assigns values inside theBEGINblock using correct assignment operator:=.Step 2: Verify assignment and calculation
It assignsa := 4;and thenb := a * 3;, which correctly setsbto 12.Final Answer:
correctly declares and assigns variables with := -> Option CQuick Check:
Declare first, assign with := inside BEGIN [OK]
- Using = instead of :=
- Assigning values during declaration with expressions
- Missing BEGIN block for assignments
