Skip to content

Commit 13daa33

Browse files
committed
Disallow NO INHERIT not-null constraints on partitioned tables
Such constraints are semantically useless and only bring weird cases along, so reject them. As a side effect, we can no longer have "throwaway" constraints in pg_dump for primary keys in partitioned tables, but since they don't serve any useful purpose, we can just omit them. Maybe this should be done for all types of constraints, but it's just not-null ones that acquired this "ability" in the 17 timeframe, so for the moment I'm not changing anything else. Per note by Alexander Lakhin. Discussion: https://postgr.es/m/7d923a66-55f0-3395-cd40-81c142b5448b@gmail.com
1 parent a27ccc2 commit 13daa33

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

src/backend/parser/parse_utilcmd.c

+10
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,10 @@ transformColumnDefinition(CreateStmtContext *cxt, ColumnDef *column)
679679
break;
680680

681681
case CONSTR_NOTNULL:
682+
if (cxt->ispartitioned && constraint->is_no_inherit)
683+
ereport(ERROR,
684+
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
685+
errmsg("not-null constraints on partitioned tables cannot be NO INHERIT"));
682686

683687
/*
684688
* Disallow conflicting [NOT] NULL markings
@@ -969,6 +973,12 @@ transformTableConstraint(CreateStmtContext *cxt, Constraint *constraint)
969973
break;
970974

971975
case CONSTR_NOTNULL:
976+
if (cxt->ispartitioned && constraint->is_no_inherit)
977+
ereport(ERROR,
978+
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
979+
errmsg("not-null constraints on partitioned tables cannot be NO INHERIT"));
980+
981+
972982
cxt->nnconstraints = lappend(cxt->nnconstraints, constraint);
973983
break;
974984

src/bin/pg_dump/pg_dump.c

+14-2
Original file line numberDiff line numberDiff line change
@@ -9052,7 +9052,15 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
90529052
tbinfo->attnames[j]);
90539053
}
90549054
else if (PQgetvalue(res, r, i_notnull_is_pk)[0] == 't')
9055-
use_throwaway_notnull = true;
9055+
{
9056+
/*
9057+
* We want this flag to be set for columns of a primary
9058+
* key in which data is going to be loaded by the dump we
9059+
* produce; thus a partitioned table doesn't need it.
9060+
*/
9061+
if (tbinfo->relkind != RELKIND_PARTITIONED_TABLE)
9062+
use_throwaway_notnull = true;
9063+
}
90569064
else if (!PQgetisnull(res, r, i_notnull_name))
90579065
use_unnamed_notnull = true;
90589066
}
@@ -9092,7 +9100,11 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
90929100
}
90939101
}
90949102
else if (PQgetvalue(res, r, i_notnull_is_pk)[0] == 't')
9095-
use_throwaway_notnull = true;
9103+
{
9104+
/* see above */
9105+
if (tbinfo->relkind != RELKIND_PARTITIONED_TABLE)
9106+
use_throwaway_notnull = true;
9107+
}
90969108
}
90979109

90989110
if (use_unnamed_notnull)

src/test/regress/expected/constraints.out

+5
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,11 @@ ALTER TABLE ATACC1 ADD NOT NULL a NO INHERIT;
321321
Inherits: atacc1
322322

323323
DROP TABLE ATACC1, ATACC2;
324+
-- no can do
325+
CREATE TABLE ATACC1 (a int NOT NULL NO INHERIT) PARTITION BY LIST (a);
326+
ERROR: not-null constraints on partitioned tables cannot be NO INHERIT
327+
CREATE TABLE ATACC1 (a int, NOT NULL a NO INHERIT) PARTITION BY LIST (a);
328+
ERROR: not-null constraints on partitioned tables cannot be NO INHERIT
324329
-- overridding a no-inherit constraint with an inheritable one
325330
CREATE TABLE ATACC2 (a int, CONSTRAINT a_is_not_null NOT NULL a NO INHERIT);
326331
CREATE TABLE ATACC1 (a int);

src/test/regress/sql/constraints.sql

+4
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ ALTER TABLE ATACC1 ADD NOT NULL a NO INHERIT;
212212
\d+ ATACC2
213213
DROP TABLE ATACC1, ATACC2;
214214

215+
-- no can do
216+
CREATE TABLE ATACC1 (a int NOT NULL NO INHERIT) PARTITION BY LIST (a);
217+
CREATE TABLE ATACC1 (a int, NOT NULL a NO INHERIT) PARTITION BY LIST (a);
218+
215219
-- overridding a no-inherit constraint with an inheritable one
216220
CREATE TABLE ATACC2 (a int, CONSTRAINT a_is_not_null NOT NULL a NO INHERIT);
217221
CREATE TABLE ATACC1 (a int);

0 commit comments

Comments
 (0)