pgsql: Catalog not-null constraints

Поиск
Список
Период
Сортировка
От Alvaro Herrera
Тема pgsql: Catalog not-null constraints
Дата
Msg-id E1qZV2v-000p1M-8n@gemulon.postgresql.org
обсуждение исходный текст
Список pgsql-committers
Catalog not-null constraints

We now create contype='n' pg_constraint rows for not-null constraints.

We propagate these constraints to other tables during operations such as
adding inheritance relationships, creating and attaching partitions and
creating tables LIKE other tables.  We also spawn not-null constraints
for inheritance child tables when their parents have primary keys.
These related constraints mostly follow the well-known rules of
conislocal and coninhcount that we have for CHECK constraints, with some
adaptations: for example, as opposed to CHECK constraints, we don't
match not-null ones by name when descending a hierarchy to alter it,
instead matching by column name that they apply to.  This means we don't
require the constraint names to be identical across a hierarchy.

For now, we omit them for system catalogs.  Maybe this is worth
reconsidering.  We don't support NOT VALID nor DEFERRABLE clauses
either; these can be added as separate features later (this patch is
already large and complicated enough.)

psql shows these constraints in \d+.

pg_dump requires some ad-hoc hacks, particularly when dumping a primary
key.  We now create one "throwaway" not-null constraint for each column
in the PK together with the CREATE TABLE command, and once the PK is
created, all those throwaway constraints are removed.  This avoids
having to check each tuple for nullness when the dump restores the
primary key creation.

pg_upgrading from an older release requires a somewhat brittle procedure
to create a constraint state that matches what would be created if the
database were being created fresh in Postgres 17.  I have tested all the
scenarios I could think of, and it works correctly as far as I can tell,
but I could have neglected weird cases.

This patch has been very long in the making.  The first patch was
written by Bernd Helmle in 2010 to add a new pg_constraint.contype value
('n'), which I (Álvaro) then hijacked in 2011 and 2012, until that one
was killed by the realization that we ought to use contype='c' instead:
manufactured CHECK constraints.  However, later SQL standard
development, as well as nonobvious emergent properties of that design
(mostly, failure to distinguish them from "normal" CHECK constraints as
well as the performance implication of having to test the CHECK
expression) led us to reconsider this choice, so now the current
implementation uses contype='n' again.  During Postgres 16 this had
already been introduced by commit e056c557aef4, but there were some
problems mainly with the pg_upgrade procedure that couldn't be fixed in
reasonable time, so it was reverted.

In 2016 Vitaly Burovoy also worked on this feature[1] but found no
consensus for his proposed approach, which was claimed to be closer to
the letter of the standard, requiring an additional pg_attribute column
to track the OID of the not-null constraint for that column.
[1] https://postgr.es/m/CAKOSWNkN6HSyatuys8xZxzRCR-KL1OkHS5-b9qd9bf1Rad3PLA@mail.gmail.com

Author: Álvaro Herrera <alvherre@alvh.no-ip.org>
Author: Bernd Helmle <mailings@oopsware.de>
Reviewed-by: Justin Pryzby <pryzby@telsasoft.com>
Reviewed-by: Peter Eisentraut <peter.eisentraut@enterprisedb.com>
Reviewed-by: Dean Rasheed <dean.a.rasheed@gmail.com>

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/b0e96f311985bceba79825214f8e43f65afa653a

Modified Files
--------------
contrib/sepgsql/expected/alter.out                 |    3 -
contrib/sepgsql/expected/ddl.out                   |    2 +
contrib/test_decoding/expected/ddl.out             |   12 +
doc/src/sgml/catalogs.sgml                         |   11 +-
doc/src/sgml/ddl.sgml                              |   55 +-
doc/src/sgml/ref/alter_table.sgml                  |   11 +-
doc/src/sgml/ref/create_table.sgml                 |    8 +-
src/backend/catalog/heap.c                         |  529 +++++--
src/backend/catalog/pg_constraint.c                |  286 ++++
src/backend/commands/tablecmds.c                   | 1595 ++++++++++++++------
src/backend/nodes/outfuncs.c                       |    5 +
src/backend/nodes/readfuncs.c                      |    9 +-
src/backend/optimizer/util/plancat.c               |    2 +
src/backend/parser/gram.y                          |   19 +-
src/backend/parser/parse_utilcmd.c                 |  277 +++-
src/backend/utils/adt/ruleutils.c                  |   14 +
src/backend/utils/cache/relcache.c                 |   34 +-
src/bin/pg_dump/common.c                           |   18 +-
src/bin/pg_dump/pg_dump.c                          |  292 +++-
src/bin/pg_dump/pg_dump.h                          |    9 +-
src/bin/pg_dump/t/002_pg_dump.pl                   |   10 +-
src/bin/psql/describe.c                            |   44 +
src/include/catalog/catversion.h                   |    2 +-
src/include/catalog/heap.h                         |    8 +-
src/include/catalog/pg_constraint.h                |   14 +-
src/include/nodes/parsenodes.h                     |   16 +-
.../test_ddl_deparse/expected/alter_table.out      |   18 +-
.../test_ddl_deparse/expected/create_table.out     |   26 +-
.../modules/test_ddl_deparse/test_ddl_deparse.c    |    6 +-
src/test/regress/expected/alter_table.out          |   62 +-
src/test/regress/expected/cluster.out              |    7 +-
src/test/regress/expected/constraints.out          |  252 ++++
src/test/regress/expected/create_table.out         |   41 +-
src/test/regress/expected/create_table_like.out    |   10 +
src/test/regress/expected/event_trigger.out        |    2 +
src/test/regress/expected/foreign_data.out         |  108 +-
src/test/regress/expected/foreign_key.out          |   16 +-
src/test/regress/expected/generated.out            |    2 +
src/test/regress/expected/identity.out             |    4 +
src/test/regress/expected/indexing.out             |   41 +-
src/test/regress/expected/inherit.out              |  442 ++++++
src/test/regress/expected/publication.out          |    6 +
src/test/regress/expected/replica_identity.out     |   24 +
src/test/regress/expected/rowsecurity.out          |    2 +
src/test/regress/sql/alter_table.sql               |   24 +-
src/test/regress/sql/constraints.sql               |  102 ++
src/test/regress/sql/create_table.sql              |    6 +-
src/test/regress/sql/indexing.sql                  |    8 +-
src/test/regress/sql/inherit.sql                   |  183 +++
src/test/regress/sql/replica_identity.sql          |   15 +
50 files changed, 3928 insertions(+), 764 deletions(-)


В списке pgsql-committers по дате отправления:

Предыдущее
От: Amit Kapila
Дата:
Сообщение: pgsql: Reset the logical worker type while cleaning up other worker inf
Следующее
От: Alvaro Herrera
Дата:
Сообщение: pgsql: Rename test table to avoid cs_CZ locale problem