Home Explore Blog CI



postgresql

6th chunk of `doc/src/sgml/ddl.sgml`
8ef6f01c8ec8dbcb66431d7f8043089b3136cc3d49b576160000000100000fa7
 column default is evaluated once when the row is first
   inserted if no other value was provided; a generated column is updated
   whenever the row changes and cannot be overridden.  A column default may
   not refer to other columns of the table; a generation expression would
   normally do so.  A column default can use volatile functions, for example
   <literal>random()</literal> or functions referring to the current time;
   this is not allowed for generated columns.
  </para>

  <para>
   Several restrictions apply to the definition of generated columns and
   tables involving generated columns:

   <itemizedlist>
    <listitem>
     <para>
      The generation expression can only use immutable functions and cannot
      use subqueries or reference anything other than the current row in any
      way.
     </para>
    </listitem>
    <listitem>
     <para>
      A generation expression cannot reference another generated column.
     </para>
    </listitem>
    <listitem>
     <para>
      A generation expression cannot reference a system column, except
      <varname>tableoid</varname>.
     </para>
    </listitem>
    <listitem>
     <para>
      A generated column cannot have a column default or an identity definition.
     </para>
    </listitem>
    <listitem>
     <para>
      A generated column cannot be part of a partition key.
     </para>
    </listitem>
    <listitem>
     <para>
      Foreign tables can have generated columns.  See <xref
      linkend="sql-createforeigntable"/> for details.
     </para>
    </listitem>
    <listitem>
     <para>For inheritance and partitioning:</para>
     <itemizedlist>
      <listitem>
       <para>
        If a parent column is a generated column, its child column must also
        be a generated column of the same kind (stored or virtual); however,
        the child column can have a different generation expression.
       </para>

       <para>
        For stored generated columns, the generation expression that is
        actually applied during insert or update of a row is the one
        associated with the table that the row is physically in.  (This is
        unlike the behavior for column defaults: for those, the default value
        associated with the table named in the query applies.)  For virtual
        generated columns, the generation expression of the table named in the
        query applies when a table is read.
       </para>
      </listitem>
      <listitem>
       <para>
        If a parent column is not a generated column, its child column must
        not be generated either.
       </para>
      </listitem>
      <listitem>
       <para>
        For inherited tables, if you write a child column definition without
        any <literal>GENERATED</literal> clause in <command>CREATE TABLE
        ... INHERITS</command>, then its <literal>GENERATED</literal> clause
        will automatically be copied from the parent.  <command>ALTER TABLE
        ... INHERIT</command> will insist that parent and child columns
        already match as to generation status, but it will not require their
        generation expressions to match.
       </para>
      </listitem>
      <listitem>
       <para>
        Similarly for partitioned tables, if you write a child column
        definition without any <literal>GENERATED</literal> clause
        in <command>CREATE TABLE ... PARTITION OF</command>, then
        its <literal>GENERATED</literal> clause will automatically be copied
        from the parent.  <command>ALTER TABLE ... ATTACH PARTITION</command>
        will insist that parent and child columns already match as to
        generation status, but it will not require their generation
        expressions to match.
       </para>
      </listitem>
      <listitem>
       <para>
        In case of multiple inheritance, if one parent column is a generated
        column, then all parent columns must be generated columns.  If they
        do not all have the same generation

Title: Generated Columns: Restrictions, Inheritance, and Partitioning
Summary
This section details restrictions on generated columns, including limitations on functions (only immutable allowed, no subqueries), references (only to the current row and the 'tableoid' system column), and features (no column defaults, identity definitions, or inclusion in partition keys). It also covers generated columns in foreign tables and the rules governing inheritance and partitioning, mandating consistency in generated status between parent and child columns, with specific behaviors for stored and virtual generated columns. The section further discusses scenarios of multiple inheritance and the requirements for consistency in generation expressions across parent columns.