Returning modified rows with RETURNING in PostgreSQL - Time & Space Complexity
When we update rows in a database and ask it to return the changed rows, we want to know how the time it takes grows as the data grows.
How does the database handle returning modified rows as the number of rows changes?
Analyze the time complexity of the following code snippet.
UPDATE employees
SET salary = salary * 1.1
WHERE department = 'Sales'
RETURNING id, salary;
This code increases the salary by 10% for all employees in the Sales department and returns their id and new salary.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning all rows in the employees table to find those in the Sales department.
- How many times: Once per row in the table, to check the department and update if needed.
As the number of employees grows, the database must check each one to see if they belong to Sales and update their salary if so.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks and some updates |
| 100 | About 100 checks and more updates |
| 1000 | About 1000 checks and many updates |
Pattern observation: The work grows roughly in direct proportion to the number of rows.
Time Complexity: O(n)
This means the time to update and return rows grows linearly with the number of rows in the table.
[X] Wrong: "The RETURNING clause makes the update run faster because it returns data immediately."
[OK] Correct: Returning rows adds work because the database must collect and send the updated rows, so it does not reduce the time needed to find and update rows.
Understanding how returning modified rows affects performance helps you explain database behavior clearly and shows you think about real data sizes and costs.
"What if we added an index on the department column? How would the time complexity change?"
Practice
RETURNING clause do in a PostgreSQL UPDATE statement?Solution
Step 1: Understand the purpose of RETURNING
The RETURNING clause is used to get the data of rows affected by an INSERT, UPDATE, or DELETE immediately.Step 2: Apply to UPDATE statement
When used with UPDATE, RETURNING shows the updated rows without needing a separate SELECT query.Final Answer:
It returns the rows that were updated immediately after the update. -> Option CQuick Check:
RETURNING shows changed rows = B [OK]
- Thinking RETURNING delays changes
- Assuming RETURNING prevents updates
- Confusing RETURNING with row count only
Solution
Step 1: Recall correct RETURNING syntax
PostgreSQL uses RETURNING followed by column names or * to return all columns.Step 2: Identify correct option
Only UPDATE table_name SET col = val RETURNING *; uses RETURNING * which is valid syntax to return all columns.Final Answer:
UPDATE table_name SET col = val RETURNING *; -> Option BQuick Check:
RETURNING * returns all columns [OK]
- Using RETURN instead of RETURNING
- Writing RETURNING ALL instead of RETURNING *
- Adding extra keywords like ALL COLUMNS
users(id INT, name TEXT, age INT) with rows:(1, 'Alice', 30), (2, 'Bob', 25)What will this query return?
UPDATE users SET age = age + 1 WHERE id = 2 RETURNING id, age;
Solution
Step 1: Identify updated row
The WHERE clause updates only the row with id = 2 (Bob).Step 2: Calculate new age and returned columns
Bob's age increases from 25 to 26. RETURNING id, age returns (2, 26).Final Answer:
(2, 26) -> Option AQuick Check:
Updated row with id=2 returns new age 26 [OK]
- Returning old values instead of updated
- Confusing which row is updated
- Ignoring WHERE clause effect
UPDATE products SET price = price * 1.1 RETURNING;
Solution
Step 1: Check RETURNING syntax
RETURNING must be followed by column names or *; empty RETURNING is invalid syntax.Step 2: Validate other parts
Multiplication and UPDATE without WHERE are valid; RETURNING with no columns is the error.Final Answer:
RETURNING clause requires columns or * to specify what to return. -> Option DQuick Check:
RETURNING needs columns or * [OK]
- Leaving RETURNING empty
- Thinking WHERE is mandatory for syntax
- Believing RETURNING disallowed in UPDATE
id and new salary. Which query correctly does this and returns the updated rows?Solution
Step 1: Identify correct UPDATE with WHERE and RETURNING
The query must update only department 5 employees and return id and salary.Step 2: Check syntax correctness
UPDATE employees SET salary = salary * 1.1 WHERE department_id = 5 RETURNING id, salary; uses correct syntax: UPDATE ... SET ... WHERE ... RETURNING id, salary;Step 3: Eliminate incorrect options
Adding a fixed 10 instead of multiplying by 1.1 doesn't implement a 10% increase. Placing RETURNING before WHERE is invalid syntax. Using RETURN instead of RETURNING is wrong.Final Answer:
UPDATE employees SET salary = salary * 1.1 WHERE department_id = 5 RETURNING id, salary; -> Option AQuick Check:
Correct syntax with WHERE and RETURNING columns [OK]
- Placing RETURNING before WHERE
- Using RETURN instead of RETURNING
- Adding fixed amount instead of percentage
Ready to prove it?
Pass the test and this pattern moves unknown
