postgresql/src/backend/catalog
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
..
Catalog.pm Un-break genbki.pl's error reporting capabilities. 2024-03-20 18:02:50 -04:00
Makefile Put genbki.pl output into src/include/catalog/ directly 2024-03-14 07:11:21 +01:00
aclchk.c Reintroduce MAINTAIN privilege and pg_maintain predefined role. 2024-03-13 14:49:26 -05:00
catalog.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
dependency.c Fix type-checking of RECORD-returning functions in FROM, redux. 2024-04-15 12:56:56 -04:00
genbki.pl Generate syscache info from catalog files 2024-01-23 07:31:06 +01:00
heap.c Fix restore of not-null constraints with inheritance 2024-04-18 15:35:15 +02:00
index.c CREATE INDEX: do not update stats during binary upgrade. 2024-04-03 16:12:45 -07:00
indexing.c Revert: Let table AM insertion methods control index insertion 2024-04-11 16:01:30 +03:00
information_schema.sql Catalog domain not-null constraints 2024-03-20 10:05:37 +01:00
meson.build Update copyright for 2024 2024-01-03 20:49:05 -05:00
namespace.c Fix search_path to a safe value during maintenance operations. 2024-03-04 17:31:38 -08:00
objectaccess.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
objectaddress.c Remove ObjectClass type 2024-03-26 10:08:56 +01:00
partition.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
pg_aggregate.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
pg_attrdef.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
pg_cast.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
pg_class.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
pg_collation.c Introduce "builtin" collation provider. 2024-03-13 23:33:44 -07:00
pg_constraint.c Fix restore of not-null constraints with inheritance 2024-04-18 15:35:15 +02:00
pg_conversion.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
pg_db_role_setting.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
pg_depend.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
pg_enum.c Allow more cases to pass the unsafe-use-of-new-enum-value restriction. 2024-03-24 14:30:29 -04:00
pg_inherits.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
pg_largeobject.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
pg_namespace.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
pg_operator.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
pg_parameter_acl.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
pg_proc.c Separate equalRowTypes() from equalTupleDescs() 2024-03-17 05:58:04 +01:00
pg_publication.c Make the order of the header file includes consistent 2024-03-13 15:07:00 +01:00
pg_range.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
pg_shdepend.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
pg_subscription.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
pg_type.c Explicitly list dependent types as extension members in pg_depend. 2024-03-04 14:49:36 -05:00
sql_feature_packages.txt Update SQL features 2023-05-02 10:59:21 +02:00
sql_features.txt JSON_TABLE: Add support for NESTED paths and columns 2024-04-08 16:14:13 +09:00
storage.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
system_functions.sql Revert: Implement pg_wal_replay_wait() stored procedure 2024-04-11 17:28:15 +03:00
system_views.sql Use TidStore for dead tuple TIDs storage during lazy vacuum. 2024-04-02 10:15:37 +09:00
toasting.c Add attstattarget to FormExtraData_pg_attribute 2024-03-17 12:38:27 +01:00