Memra

Generated columns

Columns the database computes for you.

Let the database compute it

A generated column is derived from other columns and kept in sync automatically — you never write to it:

CREATE TABLE line (
  qty   int,
  price numeric,
  total numeric GENERATED ALWAYS AS (qty * price) STORED
);

Insert qty and price; total fills itself. This guarantees the derived value can never drift out of sync, which a plain default can't promise.

~/memra/learn/postgresql/generated-columns