|
77 | 77 | #include "utils/varlena.h" |
78 | 78 |
|
79 | 79 | #ifdef PG_MODULE_MAGIC_EXT |
80 | | -PG_MODULE_MAGIC_EXT(.name = "pg_rewrite", .version = "2.0"); |
| 80 | +PG_MODULE_MAGIC_EXT(.name = "pg_rewrite", .version = "2.1"); |
81 | 81 | #else |
82 | 82 | PG_MODULE_MAGIC; |
83 | 83 | #endif |
@@ -179,6 +179,7 @@ static void dump_check_constraint(Oid relid_dst, const char *relname_dst, |
179 | 179 | #if PG_VERSION_NUM >= 180000 |
180 | 180 | static void dump_null_constraint(Oid relid_dst, const char *relname_dst, |
181 | 181 | HeapTuple tup, StringInfo buf); |
| 182 | +static bool is_notnull_in_pk(Form_pg_constraint notnull, Oid relid); |
182 | 183 | #endif |
183 | 184 | static void dump_constraint_common(const char *nsp, const char *relname, |
184 | 185 | Form_pg_constraint con, StringInfo buf); |
@@ -3327,7 +3328,12 @@ copy_constraints(Oid relid_dst, const char *relname_dst, Oid relid_src) |
3327 | 3328 | #if PG_VERSION_NUM >= 180000 |
3328 | 3329 | case CONSTRAINT_NOTNULL: |
3329 | 3330 | { |
3330 | | - if (con->conrelid == relid_src) |
| 3331 | + /* |
| 3332 | + * Do not create NOT NULL constraint if it should already |
| 3333 | + * exist due to primary key constraint. |
| 3334 | + */ |
| 3335 | + if (con->conrelid == relid_src && |
| 3336 | + !is_notnull_in_pk(con, relid_src)) |
3331 | 3337 | dump_null_constraint(relid_dst, relname_dst, tuple, |
3332 | 3338 | buf); |
3333 | 3339 | break; |
@@ -3655,6 +3661,25 @@ dump_null_constraint(Oid relid_dst, const char *relname_dst, |
3655 | 3661 | appendStringInfoString(buf, " NO INHERIT"); |
3656 | 3662 |
|
3657 | 3663 | } |
| 3664 | + |
| 3665 | +/* |
| 3666 | + * Should NOT NULL constraint have already been created due to PK of table |
| 3667 | + * specified by 'relid'? |
| 3668 | + */ |
| 3669 | +static bool |
| 3670 | +is_notnull_in_pk(Form_pg_constraint notnull, Oid relid) |
| 3671 | +{ |
| 3672 | + Bitmapset *notnull_attnos, *pk_attnos; |
| 3673 | + Oid notnullid, pkoid; |
| 3674 | + |
| 3675 | + notnull_attnos = get_relation_constraint_attnos(relid, |
| 3676 | + NameStr(notnull->conname), |
| 3677 | + false, ¬nullid); |
| 3678 | + Assert(bms_num_members(notnull_attnos) == 1); |
| 3679 | + pk_attnos = get_primary_key_attnos(relid, true, &pkoid); |
| 3680 | + |
| 3681 | + return bms_overlap(notnull_attnos, pk_attnos); |
| 3682 | +} |
3658 | 3683 | #endif |
3659 | 3684 |
|
3660 | 3685 | static void |
|
0 commit comments