postgresql/src/bin/pg_dump
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
..
po Update copyright for 2024 2024-01-03 20:49:05 -05:00
t Rearrange pg_dump's handling of large objects for better efficiency. 2024-04-01 16:25:56 -04:00
.gitignore Clean up after pg_dump test runs. 2016-05-06 22:28:01 -04:00
Makefile Update copyright for 2024 2024-01-03 20:49:05 -05:00
common.c Rearrange pg_dump's handling of large objects for better efficiency. 2024-04-01 16:25:56 -04:00
compress_gzip.c Use printf's %m format instead of strerror(errno) in more places 2024-03-12 10:02:54 +09:00
compress_gzip.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
compress_io.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
compress_io.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
compress_lz4.c Make the order of the header file includes consistent 2024-03-13 15:07:00 +01:00
compress_lz4.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
compress_none.c Use printf's %m format instead of strerror(errno) in more places 2024-03-12 10:02:54 +09:00
compress_none.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
compress_zstd.c Make the order of the header file includes consistent 2024-03-13 15:07:00 +01:00
compress_zstd.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
dumputils.c Reintroduce MAINTAIN privilege and pg_maintain predefined role. 2024-03-13 14:49:26 -05:00
dumputils.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
filter.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
filter.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
meson.build Update copyright for 2024 2024-01-03 20:49:05 -05:00
nls.mk Add missing file to nls Makefile 2023-11-30 10:56:51 +01:00
parallel.c Rename static function to avoid conflicting names 2024-02-07 22:16:21 +01:00
parallel.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
pg_backup.h Invent --transaction-size option for pg_restore. 2024-04-01 16:46:24 -04:00
pg_backup_archiver.c Revert indexed and enlargable binary heap implementation. 2024-04-11 17:18:05 +09:00
pg_backup_archiver.h Invent --transaction-size option for pg_restore. 2024-04-01 16:46:24 -04:00
pg_backup_custom.c Rearrange pg_dump's handling of large objects for better efficiency. 2024-04-01 16:25:56 -04:00
pg_backup_db.c Invent --transaction-size option for pg_restore. 2024-04-01 16:46:24 -04:00
pg_backup_db.h Revert "pg_dump: Lock all relations, not just plain tables". 2020-11-06 15:48:04 -05:00
pg_backup_directory.c Rearrange pg_dump's handling of large objects for better efficiency. 2024-04-01 16:25:56 -04:00
pg_backup_null.c Rearrange pg_dump's handling of large objects for better efficiency. 2024-04-01 16:25:56 -04:00
pg_backup_tar.c Rearrange pg_dump's handling of large objects for better efficiency. 2024-04-01 16:25:56 -04:00
pg_backup_tar.h Fix tar files emitted by pg_dump and pg_basebackup to be POSIX conformant. 2012-09-28 15:19:15 -04:00
pg_backup_utils.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
pg_backup_utils.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
pg_dump.c Fix restore of not-null constraints with inheritance 2024-04-18 15:35:15 +02:00
pg_dump.h Rearrange pg_dump's handling of large objects for better efficiency. 2024-04-01 16:25:56 -04:00
pg_dump_sort.c Revert indexed and enlargable binary heap implementation. 2024-04-11 17:18:05 +09:00
pg_dumpall.c Convert uses of hash_string_pointer to fasthash equivalent 2024-04-06 12:20:40 +07:00
pg_restore.c Invent --transaction-size option for pg_restore. 2024-04-01 16:46:24 -04:00