Generated column là cột mà giá trị được tự tính từ các cột khác, nên bạn không phải lặp lại logic đó ở tầng app và có thể index/query trực tiếp trên nó.
Hợp với công thức cố định, thuộc về dữ liệu: họ tên đầy đủ, key đã chuẩn hóa, hay một số tiền tính sẵn.
ALTER TABLE users ADD COLUMN full_name text
GENERATED ALWAYS AS (first_name || ' ' || last_name) STORED;Đừng dùng generated column cho thứ phụ thuộc trạng thái bên ngoài, múi giờ thay đổi, hay hàm không "immutable" (kết quả có thể đổi theo thời điểm) — PostgreSQL sẽ không cho.
A generated column is one whose value is computed automatically from other columns, so you don't repeat that logic in the app and can index/query it directly.
It fits fixed formulas that belong to the data: a full name, a normalized key, or a precomputed amount.
ALTER TABLE users ADD COLUMN full_name text
GENERATED ALWAYS AS (first_name || ' ' || last_name) STORED;Do not use a generated column for anything depending on external state, changing time zones, or a non-immutable function (whose result can change over time) — PostgreSQL will reject it.