postgresql/src/backend/commands
Alvaro Herrera d9f686a72e
Fix restore of not-null constraints with inheritance
In tables with primary keys, pg_dump creates tables with primary keys by
initially dumping them with throw-away not-null constraints (marked "no
inherit" so that they don't create problems elsewhere), to later drop
them once the primary key is restored.  Because of a unrelated
consideration, on tables with children we add not-null constraints to
all columns of the primary key when it is created.

If both a table and its child have primary keys, and pg_dump happens to
emit the child table first (and its throw-away not-null) and later its
parent table, the creation of the parent's PK will fail because the
throw-away not-null constraint collides with the permanent not-null
constraint that the PK wants to add, so the dump fails to restore.

We can work around this problem by letting the primary key "take over"
the child's not-null.  This requires no changes to pg_dump, just two
changes to ALTER TABLE: first, the ability to convert a no-inherit
not-null constraint into a regular inheritable one (including recursing
down to children, if there are any); second, the ability to "drop" a
constraint that is defined both directly in the table and inherited from
a parent (which simply means to mark it as no longer having a local
definition).

Secondarily, change ATPrepAddPrimaryKey() to acquire locks all the way
down the inheritance hierarchy, in case we need to recurse when
propagating constraints.

These two changes allow pg_dump to reproduce more cases involving
inheritance from versions 16 and older.

Lastly, make two changes to pg_dump: 1) do not try to drop a not-null
constraint that's marked as inherited; this allows a dump to restore
with no errors if a table with a PK inherits from another which also has
a PK; 2) avoid giving inherited constraints throwaway names, for the
rare cases where such a constraint survives after the restore.

Reported-by: Andrew Bille <andrewbille@gmail.com>
Reported-by: Justin Pryzby <pryzby@telsasoft.com>
Discussion: https://postgr.es/m/CAJnzarwkfRu76_yi3dqVF_WL-MpvT54zMwAxFwJceXdHB76bOA@mail.gmail.com
Discussion: https://postgr.es/m/Zh0aAH7tbZb-9HbC@pryzbyj2023
2024-04-18 15:35:15 +02:00
..
Makefile Revert: Implement pg_wal_replay_wait() stored procedure 2024-04-11 17:28:15 +03:00
aggregatecmds.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
alter.c Remove ObjectClass type 2024-03-26 10:08:56 +01:00
amcmds.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
analyze.c revert: Generalize relation analyze in table AM interface 2024-04-16 13:14:20 +03:00
async.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
cluster.c Reintroduce MAINTAIN privilege and pg_maintain predefined role. 2024-03-13 14:49:26 -05:00
collationcmds.c Address more review comments on commit 2d819a08a1. 2024-03-18 11:58:13 -07:00
comment.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
constraint.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
conversioncmds.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
copy.c Disallow specifying ON_ERROR option without value. 2024-04-17 11:31:27 +09:00
copyfrom.c Revert: Let table AM insertion methods control index insertion 2024-04-11 16:01:30 +03:00
copyfromparse.c Remove obsolete comment in CopyReadLineText(). 2024-04-06 11:16:27 -04:00
copyto.c Add RETURNING support to MERGE. 2024-03-17 13:58:59 +00:00
createas.c Revert: Let table AM insertion methods control index insertion 2024-04-11 16:01:30 +03:00
dbcommands.c Introduce "builtin" collation provider. 2024-03-13 23:33:44 -07:00
define.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
discard.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
dropcmds.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
event_trigger.c Fix typos with function name in event_trigger.c 2024-04-17 14:56:31 +09:00
explain.c Fix incorrect format placeholders 2024-04-09 14:33:06 +02:00
extension.c Explicitly list dependent types as extension members in pg_depend. 2024-03-04 14:49:36 -05:00
foreigncmds.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
functioncmds.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
indexcmds.c Add temporal FOREIGN KEY contraints 2024-03-24 07:37:13 +01:00
lockcmds.c Reintroduce MAINTAIN privilege and pg_maintain predefined role. 2024-03-13 14:49:26 -05:00
matview.c Revert: Let table AM insertion methods control index insertion 2024-04-11 16:01:30 +03:00
meson.build Revert: Implement pg_wal_replay_wait() stored procedure 2024-04-11 17:28:15 +03:00
opclasscmds.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
operatorcmds.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
policy.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
portalcmds.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
prepare.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
proclang.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
publicationcmds.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
schemacmds.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
seclabel.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
sequence.c Replace BackendIds with 0-based ProcNumbers 2024-03-03 19:38:22 +02:00
statscmds.c Initialize variables to placate compiler. 2024-03-17 20:16:15 -05:00
subscriptioncmds.c Add destroyStringInfo function for cleaning up StringInfos 2024-03-16 23:18:28 +01:00
tablecmds.c Fix restore of not-null constraints with inheritance 2024-04-18 15:35:15 +02:00
tablespace.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
trigger.c Revert: Allow locking updated tuples in tuple_update() and tuple_delete() 2024-04-11 16:01:34 +03:00
tsearchcmds.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
typecmds.c Catalog domain not-null constraints 2024-03-20 10:05:37 +01:00
user.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
vacuum.c Revert: Custom reloptions for table AM 2024-04-11 15:46:35 +03:00
vacuumparallel.c Use TidStore for dead tuple TIDs storage during lazy vacuum. 2024-04-02 10:15:37 +09:00
variable.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
view.c Separate equalRowTypes() from equalTupleDescs() 2024-03-17 05:58:04 +01:00