Trung BìnhPostgreSQL iconPostgreSQL

Generated columns trong PostgreSQL dùng khi nào?

Generated column lưu giá trị derived từ columns khác, giúp tránh duplicate logic ở app và tạo index/query dễ hơn. Nó phù hợp khi công thức deterministic và thuộc về dữ liệu, ví dụ full name, normalized key hoặc calculated amount.

Ví dụ:

sql
ALTER TABLE users ADD COLUMN full_name text
GENERATED ALWAYS AS (first_name || ' ' || last_name) STORED;

Không dùng generated column cho logic phụ thuộc external state, timezone biến động hoặc function không immutable.

Xem toàn bộ PostgreSQL cùng filter theo level & chủ đề con.

Mở danh sách PostgreSQL