pgsql: Add pg_constraint rows for not-null constraints - Mailing list pgsql-committers
From | Álvaro Herrera |
---|---|
Subject | pgsql: Add pg_constraint rows for not-null constraints |
Date | |
Msg-id | E1t9Oel-000p5S-G2@gemulon.postgresql.org Whole thread Raw |
List | pgsql-committers |
Add pg_constraint rows for not-null constraints We now create contype='n' pg_constraint rows for not-null constraints on user tables. Only one such constraint is allowed for a column. We propagate these constraints to other tables during operations such as adding inheritance relationships, creating and attaching partitions and creating tables LIKE other tables. 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 or remove it, instead matching by the name of the column that they apply to. This means we don't require the constraint names to be identical across a hierarchy. The inheritance status of these constraints can be controlled: now we can be sure that if a parent table has one, then all children will have it as well. They can optionally be marked NO INHERIT, and then children are free not to have one. (There's currently no support for altering a NO INHERIT constraint into inheriting down the hierarchy, but that's a desirable future feature.) This also opens the door for having these constraints be marked NOT VALID, as well as allowing UNIQUE+NOT NULL to be used for functional dependency determination, as envisioned by commit e49ae8d3bc58. It's likely possible to allow DEFERRABLE constraints as followup work, as well. psql shows these constraints in \d+, though we may want to reconsider if this turns out to be too noisy. Earlier versions of this patch hid constraints that were on the same columns of the primary key, but I'm not sure that that's very useful. If clutter is a problem, we might be better off inventing a new \d++ command and not showing the constraints in \d+. For now, we omit these constraints on system catalog columns, because they're unlikely to achieve anything. The main difference to the previous attempt at this (b0e96f311985) is that we now require that such a constraint always exists when a primary key is in the column; we didn't require this previously which had a number of unpalatable consequences. With this requirement, the code is easier to reason about. For example: - We no longer have "throwaway constraints" during pg_dump. We needed those for the case where a table had a PK without a not-null underneath, to prevent a slow scan of the data during restore of the PK creation, which was particularly problematic for pg_upgrade. - We no longer have to cope with attnotnull being set spuriously in case a primary key is dropped indirectly (e.g., via DROP COLUMN). Some bits of code in this patch were authored by Jian He. Author: Álvaro Herrera <alvherre@alvh.no-ip.org> Author: Bernd Helmle <mailings@oopsware.de> Reviewed-by: 何建 (jian he) <jian.universality@gmail.com> Reviewed-by: 王刚 (Tender Wang) <tndrwang@gmail.com> Reviewed-by: Justin Pryzby <pryzby@telsasoft.com> Reviewed-by: Peter Eisentraut <peter.eisentraut@enterprisedb.com> Reviewed-by: Dean Rasheed <dean.a.rasheed@gmail.com> Discussion: https://postgr.es/m/202408310358.sdhumtyuy2ht@alvherre.pgsql Branch ------ master Details ------- https://git.postgresql.org/pg/commitdiff/14e87ffa5c543b5f30ead7413084c25f7735039f Modified Files -------------- contrib/sepgsql/expected/alter.out | 3 - contrib/test_decoding/expected/ddl.out | 12 + doc/src/sgml/catalogs.sgml | 12 +- doc/src/sgml/ddl.sgml | 65 +- doc/src/sgml/ref/alter_foreign_table.sgml | 6 +- doc/src/sgml/ref/alter_table.sgml | 30 +- doc/src/sgml/ref/create_foreign_table.sgml | 10 +- doc/src/sgml/ref/create_table.sgml | 17 +- src/backend/catalog/heap.c | 391 +++++- src/backend/catalog/information_schema.sql | 71 +- src/backend/catalog/pg_constraint.c | 252 ++++ src/backend/commands/tablecmds.c | 1264 +++++++++++++------- src/backend/commands/typecmds.c | 4 + src/backend/nodes/makefuncs.c | 23 + src/backend/optimizer/util/plancat.c | 2 + src/backend/parser/gram.y | 23 +- src/backend/parser/parse_utilcmd.c | 342 ++++-- src/backend/replication/logical/relation.c | 2 +- src/backend/utils/adt/ruleutils.c | 22 + src/backend/utils/cache/relcache.c | 44 +- src/bin/pg_dump/common.c | 30 +- src/bin/pg_dump/pg_dump.c | 305 ++++- src/bin/pg_dump/pg_dump.h | 8 +- src/bin/pg_dump/t/002_pg_dump.pl | 6 +- src/bin/psql/describe.c | 44 + src/bin/psql/t/010_tab_completion.pl | 8 +- src/include/catalog/catversion.h | 2 +- src/include/catalog/heap.h | 8 +- src/include/catalog/pg_constraint.h | 7 + src/include/nodes/makefuncs.h | 1 + src/include/nodes/parsenodes.h | 10 +- src/include/utils/relcache.h | 2 +- .../test_ddl_deparse/expected/alter_table.out | 17 +- .../test_ddl_deparse/expected/create_table.out | 2 - .../modules/test_ddl_deparse/test_ddl_deparse.c | 3 - src/test/regress/expected/alter_table.out | 49 +- src/test/regress/expected/cluster.out | 7 +- src/test/regress/expected/constraints.out | 525 ++++++++ src/test/regress/expected/create_table.out | 35 +- src/test/regress/expected/create_table_like.out | 34 +- src/test/regress/expected/foreign_data.out | 110 +- src/test/regress/expected/foreign_key.out | 16 +- src/test/regress/expected/generated_stored.out | 2 + src/test/regress/expected/identity.out | 6 +- src/test/regress/expected/index_including.out | 4 +- src/test/regress/expected/indexing.out | 56 +- src/test/regress/expected/inherit.out | 612 ++++++++++ src/test/regress/expected/publication.out | 15 + src/test/regress/expected/replica_identity.out | 24 + src/test/regress/expected/rowsecurity.out | 2 + src/test/regress/sql/alter_table.sql | 29 +- src/test/regress/sql/constraints.sql | 185 +++ src/test/regress/sql/create_table_like.sql | 5 +- src/test/regress/sql/index_including.sql | 4 +- src/test/regress/sql/indexing.sql | 3 +- src/test/regress/sql/inherit.sql | 278 +++++ src/test/regress/sql/replica_identity.sql | 15 + 57 files changed, 4246 insertions(+), 818 deletions(-)
pgsql-committers by date: