QA
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→

More PostgreSQL Interview Questions

Beginner LevelPostgreSQL

How to use pgvector in PostgreSQL for vector similarity search with HNSW indexes?

Beginner LevelPostgreSQL

What is PostgreSQL architecture and how do client backends connect via process fork model?

Beginner LevelPostgreSQL

What special built-in data types exist in PostgreSQL (JSONB, UUID, ARRAY, INET, TEXT)?