Don't disallow dropping NOT NULL for a list partition key.
authorRobert Haas <rhaas@postgresql.org>
Tue, 14 Feb 2017 17:12:34 +0000 (12:12 -0500)
committerRobert Haas <rhaas@postgresql.org>
Tue, 14 Feb 2017 17:13:41 +0000 (12:13 -0500)
Range partitioning doesn't support nulls in the partitioning columns,
but list partitioning does.

Amit Langote, per a complaint from Amul Sul

src/backend/commands/tablecmds.c
src/test/regress/expected/alter_table.out
src/test/regress/sql/alter_table.sql

index 37a4c4a3d6abcd5638c5c09a6e31e15473a11f69..f33aa70da69926ddc9dd8de82098a0702eb876a8 100644 (file)
@@ -5593,18 +5593,22 @@ ATExecDropNotNull(Relation rel, const char *colName, LOCKMODE lockmode)
    if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
    {
        PartitionKey key = RelationGetPartitionKey(rel);
-       int         partnatts = get_partition_natts(key),
-                   i;
 
-       for (i = 0; i < partnatts; i++)
+       if (get_partition_strategy(key) == PARTITION_STRATEGY_RANGE)
        {
-           AttrNumber  partattnum = get_partition_col_attnum(key, i);
+           int         partnatts = get_partition_natts(key),
+                       i;
 
-           if (partattnum == attnum)
-               ereport(ERROR,
-                       (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
-                        errmsg("column \"%s\" is in range partition key",
-                               colName)));
+           for (i = 0; i < partnatts; i++)
+           {
+               AttrNumber  partattnum = get_partition_col_attnum(key, i);
+
+               if (partattnum == attnum)
+                   ereport(ERROR,
+                           (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
+                            errmsg("column \"%s\" is in range partition key",
+                                   colName)));
+           }
        }
    }
 
index d8e7b612940fde9d52dd830161be9bc48c56e81a..b0e80a7788fb7f49c25755b85e2db8387d0f8f3d 100644 (file)
@@ -3029,6 +3029,10 @@ ERROR:  cannot alter type of column referenced in partition key expression
 -- cannot drop NOT NULL on columns in the range partition key
 ALTER TABLE partitioned ALTER COLUMN a DROP NOT NULL;
 ERROR:  column "a" is in range partition key
+-- it's fine however to drop one on the list partition key column
+CREATE TABLE list_partitioned (a int not null) partition by list (a);
+ALTER TABLE list_partitioned ALTER a DROP NOT NULL;
+DROP TABLE list_partitioned;
 -- partitioned table cannot participate in regular inheritance
 CREATE TABLE foo (
    a int,
index 1f551ec53c46e7271e5a24df3964dc1bff929e13..7513769359756c15f6db04c7c199feff8891c8d6 100644 (file)
@@ -1917,6 +1917,11 @@ ALTER TABLE partitioned ALTER COLUMN b TYPE char(5);
 -- cannot drop NOT NULL on columns in the range partition key
 ALTER TABLE partitioned ALTER COLUMN a DROP NOT NULL;
 
+-- it's fine however to drop one on the list partition key column
+CREATE TABLE list_partitioned (a int not null) partition by list (a);
+ALTER TABLE list_partitioned ALTER a DROP NOT NULL;
+DROP TABLE list_partitioned;
+
 -- partitioned table cannot participate in regular inheritance
 CREATE TABLE foo (
    a int,