postgresql/src/backend/commands
Alvaro Herrera b0e96f3119
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 e056c557ae, 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>
2023-08-25 13:31:24 +02:00
..
Makefile Move parallel vacuum code to vacuumparallel.c. 2021-12-23 11:42:52 +05:30
aggregatecmds.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
alter.c Add missing ObjectIdGetDatum() in syscache lookup calls for Oids 2023-07-20 15:18:25 +09:00
amcmds.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
analyze.c Revert MAINTAIN privilege and pg_maintain predefined role. 2023-07-07 11:25:13 -07:00
async.c Introduce macros for protocol characters. 2023-08-22 19:16:12 -07:00
cluster.c Add more sanity checks with callers of changeDependencyFor() 2023-07-10 13:08:10 +09:00
collationcmds.c All supported systems have locale_t. 2023-07-09 11:55:18 +12:00
comment.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
constraint.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
conversioncmds.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
copy.c Improve several permission-related error messages. 2023-03-17 10:33:09 +01:00
copyfrom.c Fix performance problem with new COPY DEFAULT code 2023-07-27 14:47:05 +12:00
copyfromparse.c Introduce macros for protocol characters. 2023-08-22 19:16:12 -07:00
copyto.c Introduce macros for protocol characters. 2023-08-22 19:16:12 -07:00
createas.c Simplify useless 0L constants 2023-03-29 08:25:12 +02:00
dbcommands.c Handle DROP DATABASE getting interrupted 2023-07-13 13:03:28 -07:00
define.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
discard.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
dropcmds.c Pre-beta mechanical code beautification. 2023-05-19 17:24:48 -04:00
event_trigger.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
explain.c Remove dependency to query text in JumbleQuery() 2023-06-28 08:59:36 +09:00
extension.c Reject substituting extension schemas or owners matching ["$'\]. 2023-08-07 06:05:56 -07:00
foreigncmds.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
functioncmds.c Add more sanity checks with callers of changeDependencyFor() 2023-07-10 13:08:10 +09:00
indexcmds.c Some vertical reformatting 2023-08-23 06:39:39 +02:00
lockcmds.c Revert MAINTAIN privilege and pg_maintain predefined role. 2023-07-07 11:25:13 -07:00
matview.c Revert MAINTAIN privilege and pg_maintain predefined role. 2023-07-07 11:25:13 -07:00
meson.build Update copyright for 2023 2023-01-02 15:00:37 -05:00
opclasscmds.c Fix typos in comments 2023-05-02 12:23:08 +09:00
operatorcmds.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
policy.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
portalcmds.c Simplify useless 0L constants 2023-03-29 08:25:12 +02:00
prepare.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
proclang.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
publicationcmds.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
schemacmds.c Pre-beta mechanical code beautification. 2023-05-19 17:24:48 -04:00
seclabel.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
sequence.c ExtendBufferedWhat -> BufferManagerRelation. 2023-08-23 12:31:23 +12:00
statscmds.c Add macro for maximum statistics target 2023-07-03 07:18:57 +02:00
subscriptioncmds.c Improved CREATE SUBSCRIPTION message for clarity 2023-08-16 15:27:42 +02:00
tablecmds.c Catalog not-null constraints 2023-08-25 13:31:24 +02:00
tablespace.c Pre-beta mechanical code beautification. 2023-05-19 17:24:48 -04:00
trigger.c Fix code indentation violations 2023-07-03 12:47:49 +02:00
tsearchcmds.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
typecmds.c Add more sanity checks with callers of changeDependencyFor() 2023-07-10 13:08:10 +09:00
user.c Add missing ObjectIdGetDatum() in syscache lookup calls for Oids 2023-07-20 15:18:25 +09:00
vacuum.c Handle DROP DATABASE getting interrupted 2023-07-13 13:03:28 -07:00
vacuumparallel.c Report index vacuum progress. 2023-07-11 12:34:01 +09:00
variable.c Fix privilege check for SET SESSION AUTHORIZATION. 2023-07-13 21:13:45 -07:00
view.c Pre-beta mechanical code beautification. 2023-05-19 17:24:48 -04:00