postgres/src/backend
Álvaro Herrera 14e87ffa5c
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 e49ae8d3bc.  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 (b0e96f3119) 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
2024-11-08 13:28:48 +01:00
..
access Remove an obsolete comment in gistinsert() 2024-11-07 15:13:50 +09:00
archive Apply more quoting to GUC names in messages 2024-09-04 13:50:44 +09:00
backup Remove unused #include's from backend .c files 2024-10-27 08:26:50 +01:00
bootstrap Increase the number of fast-path lock slots 2024-09-21 20:09:35 +02:00
catalog Add pg_constraint rows for not-null constraints 2024-11-08 13:28:48 +01:00
commands Add pg_constraint rows for not-null constraints 2024-11-08 13:28:48 +01:00
executor Fix hypothetical bug in ExprState building for hashing 2024-11-06 09:16:00 +13:00
foreign Improve style of two code paths 2024-10-08 10:51:20 +09:00
jit Monkey-patch LLVM code to fix ARM relocation bug. 2024-11-06 23:17:18 +13:00
lib Revert pg_wal_replay_wait() stored procedure 2024-11-04 22:47:57 +02:00
libpq Remove unused #include's from backend .c files 2024-10-27 08:26:50 +01:00
main Remove unused #include's from backend .c files 2024-10-27 08:26:50 +01:00
nodes Add pg_constraint rows for not-null constraints 2024-11-08 13:28:48 +01:00
optimizer Add pg_constraint rows for not-null constraints 2024-11-08 13:28:48 +01:00
parser Add pg_constraint rows for not-null constraints 2024-11-08 13:28:48 +01:00
partitioning Revert support for ALTER TABLE ... MERGE/SPLIT PARTITION(S) commands 2024-08-24 18:48:48 +03:00
po Translation updates 2024-06-24 13:11:27 +02:00
port Require memory barrier support. 2024-07-30 23:01:55 +12:00
postmaster Rename two functions that wake up other processes 2024-11-01 13:47:24 +02:00
regex Simplify checks for deterministic collations. 2024-09-12 13:35:56 -07:00
replication Add pg_constraint rows for not-null constraints 2024-11-08 13:28:48 +01:00
rewrite Fix unnecessary casts of copyObject() result 2024-10-17 08:36:48 +02:00
snowball Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
statistics Add missing CommandCounterIncrement() in stats import functions. 2024-10-29 10:14:23 -07:00
storage Revert pg_wal_replay_wait() stored procedure 2024-11-04 22:47:57 +02:00
tcop doc: Remove event trigger firing matrix 2024-11-06 13:43:17 +01:00
tsearch Constify fields and parameters in spell.c 2024-08-06 23:04:51 +03:00
utils Add pg_constraint rows for not-null constraints 2024-11-08 13:28:48 +01:00
.gitignore
common.mk
Makefile Fix make build on MinGW 2024-06-21 08:17:23 +02:00
meson.build meson: Fix import library name in Windows 2024-06-20 09:08:36 +02:00
nls.mk Add missing gettext triggers 2024-05-14 12:57:22 +02:00