Sub topic 1
The problem: keeping derived values correct
Prerequisites
Assume you know basic CREATE TABLE, INSERT, UPDATE, and SELECT statements. You should also know the difference between a base value, which is stored directly, and a derived value, which is calculated from other values.
Many database rows contain values that can be calculated from other columns. A person might have first_name and last_name, while an application also wants a full_name. An order might store quantity and unit_price, while reports need line_total.
Derived values are useful because they make queries simpler. They can also make a database harder to keep correct.
The duplicate-value problem
Suppose an application stores both the source values and the result:
CREATE TABLE people (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
first_name text NOT NULL,
last_name text NOT NULL,
full_name text NOT NULL
);
INSERT INTO people (first_name, last_name, full_name)
VALUES ('Ada', 'Lovelace', 'Ada Lovelace');At first, the row is consistent. The problem appears when a source value changes:
UPDATE people
SET last_name = 'King'
WHERE id = 1;
SELECT first_name, last_name, full_name
FROM people;The result can be:
| first_name | last_name | full_name |
|---|---|---|
| Ada | King | Ada Lovelace |
The database accepted the update because last_name and full_name are independent columns. It does not know that one should be calculated from the other.
This creates a consistency problem. A value is consistent when it agrees with the source data from which it should be derived. Once inconsistent data is stored, every reader must decide which value to trust.
Why application-only calculation is fragile
One solution is to avoid storing full_name and calculate it in application code. That works only if every reader follows the same rule. In a system with several services, reports, import jobs, and administrative SQL access, that assumption is easy to break.
Another solution is to update both columns in every write:
UPDATE people
SET last_name = 'King',
full_name = first_name || ' ' || 'King'
WHERE id = 1;This is better, but the rule still exists in each client. A new client can forget to update full_name. A bulk import can use a different rule. A direct SQL statement can update only last_name.
A generated column moves this calculation into the table definition. PostgreSQL computes the value from an expression based on columns in the same row. The source columns remain the values that users write, while the derived column follows them automatically.
Choosing what belongs in the table
Before adding a derived column, identify the source of truth. The source of truth is the value that should be edited directly. For a name, the source may be first_name and last_name. full_name is then a dependent result.
Ask these questions:
- Can the result be calculated from values in the same row?
- Must every client see the same calculation?
- Would stale data cause incorrect decisions or reports?
- Is the result queried often enough to justify making it part of the table design?
- Can the calculation be expressed as a deterministic database expression?
The last question matters because generated columns are database-managed values, not general-purpose application scripts. They are intended for predictable calculations from row data.
| Approach | Main calculation location | Risk of stale derived data |
|---|---|---|
| Application-only calculation | Each application client | High when clients disagree |
| Repeated writes | Each insert or update statement | Medium |
| Generated column | PostgreSQL table definition | Low for supported expressions |
The design direction
The important modeling decision is whether to store a result or derive it. Store a value directly when it is an independent fact, such as a customer's legal name. Derive it when it is fully determined by other columns and should always follow them.
Generated columns provide one way to enforce that relationship at the database boundary. The next step is learning their syntax and the rules PostgreSQL applies to their expressions.
Exercise
A table stores quantity, unit_price, and line_total. Every client can write orders, and reports must never see a line_total that disagrees with the first two columns. What should you investigate first?
Show answer
Treat quantity and unit_price as the source values, then investigate a database-managed generated column for line_total. Confirm that the calculation fits PostgreSQL's expression rules before changing the schema.