Sub topic 11
Limitations and Replication Notes
Prerequisites: you should know basic CREATE TABLE, INSERT, UPDATE, and SELECT statements. You should also understand the difference between a table and a view, and have a basic idea of how PostgreSQL indexes and logical replication work.
Generated columns are useful when a value depends only on other values in the same row. Their restrictions protect that rule. A generated column cannot obtain data from another table, call a function whose result can change unpredictably, or depend on session state.
Limits on generation expressions
A generation expression must be deterministic. PostgreSQL describes this requirement as immutability. For the same input row, the expression must always produce the same result.
The expression cannot:
- Contain a subquery.
- Read another table.
- Reference another generated column.
- Use a volatile or merely stable function.
- Depend on settings such as the current time zone, current time, or current user.
- Read values outside the current row.
For example, extracting a year with a time-zone-dependent formatting function is unsafe:
CREATE TABLE events (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
occurred_at timestamptz NOT NULL,
year_text text GENERATED ALWAYS AS
(to_char(occurred_at, 'YYYY')) STORED
);PostgreSQL rejects this definition because to_char for a timestamptz value can depend on the session time zone. A value computed on one server could differ from a value computed on another server.
Prefer an expression whose behavior is independent of session settings. If you need a particular time zone, convert the timestamp explicitly in application code or use a design that stores the desired value as an ordinary column.
A generated column also cannot be a partition key. Partition routing must happen before PostgreSQL has computed the generated value, so partition on the source columns or use another ordinary column instead.
Replication behavior
Physical replication copies database pages from the primary server. The standby receives the stored result for a stored generated column as part of those pages. It does not independently recalculate the expression.
Logical replication sends row changes through a publication and subscription. Generated columns require more care because the publisher and subscriber may have different table definitions.
By default, generated columns are excluded from a logical publication. A subscriber with the same generated-column definition can compute its own value from the replicated source columns. This makes matching schema definitions important.
In PostgreSQL 18 and later, a publication can opt to include stored generated columns:
CREATE TABLE order_lines (
order_id bigint NOT NULL,
quantity integer NOT NULL,
unit_price numeric(12, 2) NOT NULL,
line_total numeric(12, 2)
GENERATED ALWAYS AS (quantity * unit_price) STORED
);
CREATE PUBLICATION orders_pub
FOR TABLE order_lines
WITH (publish_generated_columns = true);This option publishes the stored result along with the row change. It can help when the subscriber must receive the materialized value, but it increases the need for compatible schemas. Test the behavior with your exact PostgreSQL versions and subscription setup. Virtual generated columns have no stored result, so they are not published as stored data.
| Replication method | What happens to a stored generated value |
|---|---|
| Physical replication | The stored value is copied with the table data |
| Logical replication by default | The generated column is omitted from the publication |
| Logical replication with `publish_generated_columns` | PostgreSQL 18 and later can publish the stored value |
If the subscriber computes the value locally, keep these items aligned:
- The column types and source-column meanings.
- The generation expression.
- Any functions used by the expression.
- The PostgreSQL behavior that affects those functions.
- The schema migration order.
An expression that changes between releases can produce different results on the subscriber. A stored generated column can therefore hide schema drift: replication may succeed while queries return different derived values on the two systems.
A replication decision path
Generated columns are a poor fit when their value must use other rows, external services, current time, or session-specific settings. Those requirements need a different design, such as an ordinary column maintained by controlled application or trigger logic.
Exercise
A logical subscriber has the same table name but a different generation expression from the publisher. With the default publication settings, which value should you expect the subscriber to store, and what should you do?
Show answer
The subscriber computes its own value from the replicated source columns, so it may differ from the publisher. Align the schema and expression before replication, or, where supported, publish the stored generated column and test the result.