brainlock · GitHub

Commits on Aug 22, 2026

  1. Support changing a column into a stored generated column

    This adds an ALTER TABLE subcommand which turns a regular column into a
    stored generated column:
      ... ALTER col ADD GENERATED USING CONSTRAINT constr_name STORED
    The main purpose of this command is to make it possible to add a stored
    generated column without rewriting the table under an AccessExclusive
    lock.
    Before running this command, the table should have been prepared by
    adding a regular column, backfilling it with data matching the intended
    generation expression, and adding the constr_name constraint to prove
    that the data does satisfy said generation expression.
    The constraint must have a specific structure to be usable for this
    operation.
    If the column is nullable, the constraint must be of the form:
      CHECK (column_name IS NOT DISTINCT FROM expr)
    if the column is NOT NULL, either of the following is acceptable:
      CHECK (column_name IS NOT DISTINCT FROM expr)
      CHECK (column_name = expr)
    The column will then be changed into a stored generated column, with the
    "expr" from the constraint as its generator expression. The operation
    will be performed without rewriting the table, and without any
    verification scan.
    The syntax is chosen for its similarity to:
      ... ALTER COLUMN ... ADD GENERATED ... AS IDENTITY
    with the difference that:
    - since we are not talking about IDENTITY columns, ALWAYS is implicit
      and does not need to be explicitly supplied
    - STORED must always be supplied, to make it clear that we are talking
      about stored and not virtual generated columns (VIRTUAL is the default
      when omitted in other commands, so we mandate STORED here to be
      consistent)
    This new operation fits together with SET EXPRESSION and DROP
    EXPRESSION: the latter works in the opposite direction, turning a
    generated column into a regular column.
    Partitioning/inheritance is supported in the same way as DROP
    EXPRESSION: it is allowed to change the whole inheritace tree to/from a
    generated column at once; it is forbidden to change the parent table
    ONLY and it is forbidden to change a partition directly. See
     8bf6ec3  and the associated discussion.

    authored and Commitfest Bot committed

    Aug 22, 2026
    Configuration menu

    Browse the repository at this point in the history

  2. Configuration menu

    Browse the repository at this point in the history

Read the original on github.com ↗