pgsql: Support identity columns in partitioned tables

From: Peter Eisentraut <peter(at)eisentraut(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Support identity columns in partitioned tables
Date: 2024-01-16 16:30:16
Message-ID: E1rPmKO-001hi6-JW@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Support identity columns in partitioned tables

Previously, identity columns were disallowed on partitioned tables.
(The reason was mainly that no one had gotten around to working
through all the details to make it work.) This makes it work now.

Some details on the behavior:

* A newly created partition inherits identity property

The partitions of a partitioned table are integral part of the
partitioned table. A partition inherits identity columns from the
partitioned table. An identity column of a partition shares the
identity space with the corresponding column of the partitioned
table. In other words, the same identity column across all
partitions of a partitioned table share the same identity space.
This is effected by sharing the same underlying sequence.

When INSERTing directly into a partition, the sequence associated
with the topmost partitioned table is used to calculate the value of
the corresponding identity column.

In regular inheritance, identity columns and their properties in a
child table are independent of those in its parent tables. A child
table does not inherit identity columns or their properties
automatically from the parent. (This is unchanged.)

* Attached partition inherits identity column

A table being attached as a partition inherits the identity property
from the partitioned table. This should be fine since we expect
that the partition table's column has the same type as the
partitioned table's corresponding column. If the table being
attached is a partitioned table, the identity properties are
propagated down its partition hierarchy.

An identity column in the partitioned table is also marked as NOT
NULL. The corresponding column in the partition needs to be marked
as NOT NULL for the attach to succeed.

* Drop identity property when detaching partition

A partition's identity column shares the identity space
(i.e. underlying sequence) as the corresponding column of the
partitioned table. If a partition is detached it can longer share
the identity space as before. Hence the identity columns of the
partition being detached loose their identity property.

When identity of a column of a regular table is dropped it retains
the NOT NULL constraint that came with the identity property.
Similarly the columns of the partition being detached retain the NOT
NULL constraints that came with identity property, even though the
identity property itself is lost.

The sequence associated with the identity property is linked to the
partitioned table (and not the partition being detached). That
sequence is not dropped as part of detach operation.

* Partitions with their own identity columns are not allowed.

* The usual ALTER operations (add identity column, add identity
property to existing column, alter properties of an indentity
column, drop identity property) are supported for partitioned
tables. Changing a column only in a partitioned table or a
partition is not allowed; the change needs to be applied to the
whole partition hierarchy.

Author: Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>
Reviewed-by: Peter Eisentraut <peter(at)eisentraut(dot)org>
Discussion: https://www.postgresql.org/message-id/flat/CAExHW5uOykuTC+C6R1yDSp=o8Q83jr8xJdZxgPkxfZ1Ue5RRGg(at)mail(dot)gmail(dot)com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/699586315704a8268808e3bdba4cb5924a038c49

Modified Files
--------------
doc/src/sgml/ddl.sgml | 4 +-
src/backend/commands/tablecmds.c | 221 ++++++++++++++++++---
src/backend/rewrite/rewriteHandler.c | 19 +-
.../test_ddl_deparse/expected/alter_table.out | 6 +-
src/test/regress/expected/generated.out | 3 +-
src/test/regress/expected/identity.out | 199 ++++++++++++++++++-
src/test/regress/sql/identity.sql | 110 +++++++++-
7 files changed, 515 insertions(+), 47 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Robert Haas 2024-01-16 19:21:44 pgsql: Be more consistent about whether to update the FSM while vacuumi
Previous Message Daniel Gustafsson 2024-01-16 13:32:29 pgsql: Decorate WITH with literal markup tags