Beginner LevelPostgreSQL
How do STORED Generated Columns work in PostgreSQL 12+?
❓ Interview Prompt: Explain STORED Generated Columns (`GENERATED ALWAYS AS (expr) STORED`), automatic computation during writes, and indexing.
PostgreSQL 12+ supports STORED Generated Columns:
Mechanics:
•
Values are automatically computed from an expression referencing other columns in the same table during 'INSERT' or 'UPDATE'.
•
Results are persisted to disk and can be indexed using standard B-Tree indexes!
Code Implementation Example
sql
CREATE TABLE order_items (
unit_price NUMERIC(10,2) NOT NULL,
quantity INT NOT NULL,
total_price NUMERIC(10,2) GENERATED ALWAYS AS (unit_price * quantity) STORED
);💡Defining a STORED Generated Column automatically computing total prices.
🎯 Interview Key Takeaway
Use STORED Generated Columns to persist computed column expressions for fast indexing and simplified querying.
#Generated Columns#STORED#Schema Design#Beginner
Back to all PostgreSQL Questions→