INSERT with RETURNING clause in PostgreSQL - Time & Space Complexity
When we add new rows to a database table using INSERT, it is helpful to know how the time to do this grows as we add more rows.
We want to understand how the database handles inserting data and returning results as the input size changes.
Analyze the time complexity of the following code snippet.
INSERT INTO employees (name, department, salary)
VALUES ('Alice', 'Sales', 50000),
('Bob', 'HR', 45000),
('Carol', 'IT', 60000)
RETURNING id, name;
This code inserts three new employee records and returns their generated IDs and names.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Inserting each row into the table and writing it to disk.
- How many times: Once for each row in the VALUES list (3 times here, but can be many more).
As the number of rows to insert grows, the work grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 insert operations and 10 returned rows |
| 100 | About 100 insert operations and 100 returned rows |
| 1000 | About 1000 insert operations and 1000 returned rows |
Pattern observation: The time grows linearly as you add more rows to insert and return.
Time Complexity: O(n)
This means the time to insert and return rows grows directly with the number of rows you add.
[X] Wrong: "The RETURNING clause makes the insert take much longer regardless of how many rows are inserted."
[OK] Correct: Returning data adds some work, but it scales with the number of rows inserted, not independently. The main cost is still inserting each row.
Understanding how inserting multiple rows and returning data scales helps you explain database performance clearly and confidently in real situations.
"What if we changed the INSERT to add rows one at a time inside a loop instead of all at once? How would the time complexity change?"
Practice
RETURNING clause do in a PostgreSQL INSERT statement?Solution
Step 1: Understand the purpose of RETURNING
The RETURNING clause in PostgreSQL is used to get back specified columns from the rows that were just inserted.Step 2: Compare with other options
The other options describe actions unrelated to RETURNING. RETURNING does not delete, update, or backup data.Final Answer:
It returns specified columns of the newly inserted rows immediately. -> Option DQuick Check:
RETURNING returns inserted data [OK]
- Thinking RETURNING deletes or updates data
- Confusing RETURNING with SELECT
- Assuming RETURNING creates backups
id in PostgreSQL?Solution
Step 1: Check correct RETURNING syntax
The correct syntax places RETURNING after VALUES and specifies the column to return, like RETURNING id.Step 2: Validate each option
INSERT INTO users (name) VALUES ('Alice') RETURNING id; matches the correct syntax. Placing RETURNING before VALUES or using RETURN instead of RETURNING is incorrect. INSERT INTO users VALUES ('Alice') RETURNING id; misses column list before VALUES.Final Answer:
INSERT INTO users (name) VALUES ('Alice') RETURNING id; -> Option CQuick Check:
RETURNING follows VALUES with column list [OK]
- Placing RETURNING before VALUES
- Omitting column list in INSERT
- Using RETURN instead of RETURNING
products(id SERIAL PRIMARY KEY, name TEXT, price NUMERIC), what will this query return?INSERT INTO products (name, price) VALUES ('Pen', 1.5) RETURNING id, price;Solution
Step 1: Understand RETURNING with multiple columns
RETURNING can return one or more columns from the inserted row(s). Here, id and price are requested.Step 2: Predict output
The query inserts a new product and returns its auto-generated id and the price 1.5 as a row, e.g., (5, 1.5).Final Answer:
The new product's id and price, e.g., (5, 1.5) -> Option AQuick Check:
RETURNING multiple columns returns all requested [OK]
- Assuming RETURNING returns only one column
- Expecting no output from INSERT
- Thinking RETURNING causes syntax error with multiple columns
INSERT INTO orders (product_id, quantity) VALUES (10, 3) RETURNING;
Solution
Step 1: Check RETURNING syntax
RETURNING must specify at least one column or * to return. Here, it is empty, which is invalid syntax.Step 2: Verify other parts
VALUES clause and columns look correct. INSERT supports RETURNING. Assuming columns exist, no error there.Final Answer:
RETURNING clause is missing column names to return. -> Option BQuick Check:
RETURNING needs columns or * [OK]
- Leaving RETURNING empty
- Confusing RETURNING with WHERE
- Assuming RETURNING is optional but empty
employees(name, department) and get back their generated id values. Which query correctly does this and returns all new ids?Solution
Step 1: Understand multi-row INSERT with RETURNING
PostgreSQL allows inserting multiple rows in one statement and RETURNING returns all inserted rows' specified columns.Step 2: Analyze each option
INSERT INTO employees (name, department) VALUES ('John', 'HR'), ('Jane', 'IT') RETURNING id; correctly inserts two rows and returns their ids. INSERT INTO employees (name, department) RETURNING id VALUES ('John', 'HR'), ('Jane', 'IT'); misplaces RETURNING before VALUES. INSERT INTO employees (name, department) VALUES ('John', 'HR'); RETURNING id; INSERT INTO employees (name, department) VALUES ('Jane', 'IT'); RETURNING id; splits inserts and RETURNING incorrectly. INSERT INTO employees (name, department) VALUES ('John', 'HR'), ('Jane', 'IT') RETURN id; uses invalid RETURN keyword.Final Answer:
INSERT INTO employees (name, department) VALUES ('John', 'HR'), ('Jane', 'IT') RETURNING id; -> Option AQuick Check:
Multi-row INSERT with RETURNING returns all rows [OK]
- Placing RETURNING before VALUES
- Splitting inserts instead of multi-row insert
- Using RETURN instead of RETURNING
Ready to prove it?
Pass the test and this pattern moves unknown
