GENERATED columns (stored and virtual) in PostgreSQL - Time & Space Complexity
We want to understand how the time to compute generated columns changes as the table grows.
How does adding more rows affect the work done to create these columns?
Analyze the time complexity of this table with a generated column.
CREATE TABLE sales (
price numeric,
quantity int,
total numeric GENERATED ALWAYS AS (price * quantity) STORED
);
INSERT INTO sales (price, quantity) VALUES
(10, 2), (15, 3), (7, 5);
SELECT * FROM sales;
This code creates a table where the total is automatically calculated by multiplying price and quantity for each row.
Look at what repeats when the table grows.
- Primary operation: Calculating the generated column expression (price * quantity) for each row.
- How many times: Once per row inserted or updated.
Each new row requires one calculation of the generated column.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calculations |
| 100 | 100 calculations |
| 1000 | 1000 calculations |
Pattern observation: The work grows directly with the number of rows; double the rows, double the calculations.
Time Complexity: O(n)
This means the time to compute generated columns grows linearly with the number of rows.
[X] Wrong: "The generated column calculation happens only once for the whole table, so it's constant time."
[OK] Correct: Each row's generated column is calculated separately when inserted or updated, so the total work grows with the number of rows.
Understanding how generated columns scale helps you explain database behavior clearly and shows you can reason about query costs in real projects.
What if the generated column used a subquery instead of a simple expression? How would the time complexity change?
Practice
Solution
Step 1: Understand GENERATED column concept
A GENERATED column automatically computes its value based on other columns in the same row.Step 2: Compare options with definition
Only A column that automatically calculates its value from other columns describes this automatic calculation feature correctly.Final Answer:
A column that automatically calculates its value from other columns -> Option BQuick Check:
GENERATED column = auto-calculated column [OK]
- Thinking GENERATED columns store manual input
- Confusing GENERATED with regular stored columns
- Assuming GENERATED columns only store text
Solution
Step 1: Recall PostgreSQL syntax for STORED GENERATED columns
PostgreSQL uses 'GENERATED ALWAYS AS (expression) STORED' to define such columns.Step 2: Check each option
CREATE TABLE t (a INT, b INT GENERATED ALWAYS AS (a * 2) STORED); matches the correct syntax. CREATE TABLE t (a INT, b INT AS (a * 2)); misses 'GENERATED ALWAYS' and 'STORED'. CREATE TABLE t (a INT, b INT GENERATED ALWAYS AS (a * 2) VIRTUAL); uses 'VIRTUAL' which PostgreSQL does not support. CREATE TABLE t (a INT, b INT COMPUTED AS (a * 2)); uses 'COMPUTED' which is invalid in PostgreSQL.Final Answer:
CREATE TABLE t (a INT, b INT GENERATED ALWAYS AS (a * 2) STORED); -> Option AQuick Check:
PostgreSQL STORED syntax = GENERATED ALWAYS AS (...) STORED [OK]
- Using VIRTUAL keyword which PostgreSQL does not support
- Omitting GENERATED ALWAYS clause
- Using COMPUTED instead of GENERATED
CREATE TABLE sales ( price NUMERIC, quantity INT, total NUMERIC GENERATED ALWAYS AS (price * quantity) STORED );
What will be the result of:
INSERT INTO sales (price, quantity) VALUES (10, 3); SELECT total FROM sales;
Solution
Step 1: Understand the GENERATED column calculation
The 'total' column is calculated as price * quantity, so 10 * 3 = 30.Step 2: Check the INSERT and SELECT results
After inserting (10, 3), selecting 'total' returns 30 as the stored generated value.Final Answer:
[30] -> Option DQuick Check:
price * quantity = 10 * 3 = 30 [OK]
- Expecting manual input for generated column
- Confusing addition with multiplication
- Thinking generated columns can be NULL after insert
CREATE TABLE products ( price NUMERIC, discount NUMERIC, final_price NUMERIC GENERATED ALWAYS AS (price - discount) );
What is wrong with this statement?
Solution
Step 1: Check PostgreSQL GENERATED column syntax
PostgreSQL requires the STORED keyword for generated columns; omitting it causes an error.Step 2: Analyze the given statement
The statement misses 'STORED' after the expression, so it is invalid syntax.Final Answer:
Missing STORED keyword for the GENERATED column -> Option CQuick Check:
GENERATED columns must include STORED in PostgreSQL [OK]
- Assuming VIRTUAL columns are supported
- Thinking arithmetic is not allowed
- Believing data types must be integers
first_name, last_name, and a generated column full_name that combines both names separated by a space. Which is the correct way to define full_name in PostgreSQL?Solution
Step 1: Use correct string concatenation operator in PostgreSQL
PostgreSQL uses '||' to concatenate strings, not '+'.Step 2: Use correct GENERATED column syntax
PostgreSQL requires 'GENERATED ALWAYS AS (expression) STORED' for generated columns; VIRTUAL and COMPUTED are invalid.Final Answer:
full_name TEXT GENERATED ALWAYS AS (first_name || ' ' || last_name) STORED -> Option AQuick Check:
String concat with || and STORED keyword [OK]
- Using + instead of || for string concatenation
- Using VIRTUAL or COMPUTED keywords
- Omitting STORED keyword
Ready to prove it?
Pass the test and this pattern moves unknown
