mirror of
https://github.com/postgres/postgres.git
synced 2025-07-28 23:42:10 +03:00
Improve handling of inherited GENERATED expressions.
In both partitioning and traditional inheritance, require child
columns to be GENERATED if and only if their parent(s) are.
Formerly we allowed the case of an inherited column being
GENERATED when its parent isn't, but that results in inconsistent
behavior: the column can be directly updated through an UPDATE
on the parent table, leading to it containing a user-supplied
value that might not match the generation expression. This also
fixes an oversight that we enforced partition-key-columns-can't-
be-GENERATED against parent tables, but not against child tables
that were dynamically attached to them.
Also, remove the restriction that the child's generation expression
be equivalent to the parent's. In the wake of commit 3f7836ff6
,
there doesn't seem to be any reason that we need that restriction,
since generation expressions are always computed per-table anyway.
By removing this, we can also allow a child to merge multiple
inheritance parents with inconsistent generation expressions, by
overriding them with its own expression, much as we've long allowed
for DEFAULT expressions.
Since we're rejecting a case that we used to accept, this doesn't
seem like a back-patchable change. Given the lack of field
complaints about the inconsistent behavior, it's likely that no
one is doing this anyway, but we won't change it in minor releases.
Amit Langote and Tom Lane
Discussion: https://postgr.es/m/2793383.1672944799@sss.pgh.pa.us
This commit is contained in:
@ -325,27 +325,54 @@ CREATE TABLE people (
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>For inheritance:</para>
|
||||
<para>For inheritance and partitioning:</para>
|
||||
<itemizedlist>
|
||||
<listitem>
|
||||
<para>
|
||||
If a parent column is a generated column, a child column must also be
|
||||
a generated column using the same expression. In the definition of
|
||||
the child column, leave off the <literal>GENERATED</literal> clause,
|
||||
as it will be copied from the parent.
|
||||
If a parent column is a generated column, its child column must also
|
||||
be a generated column; however, the child column can have a
|
||||
different generation expression. 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.)
|
||||
</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 ... ADD 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 and with the
|
||||
same expression.
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
If a parent column is not a generated column, a child column may be
|
||||
defined to be a generated column or not.
|
||||
column, then all parent columns must be generated columns. If they
|
||||
do not all have the same generation expression, then the desired
|
||||
expression for the child must be specified explicitly.
|
||||
</para>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
|
Reference in New Issue
Block a user