Skip to content

Commit e28b115

Browse files
committed
Don't disallow dropping NOT NULL for a list partition key.
Range partitioning doesn't support nulls in the partitioning columns, but list partitioning does. Amit Langote, per a complaint from Amul Sul
1 parent 8d396a0 commit e28b115

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

src/backend/commands/tablecmds.c

+13-9
Original file line numberDiff line numberDiff line change
@@ -5593,18 +5593,22 @@ ATExecDropNotNull(Relation rel, const char *colName, LOCKMODE lockmode)
55935593
if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
55945594
{
55955595
PartitionKey key = RelationGetPartitionKey(rel);
5596-
int partnatts = get_partition_natts(key),
5597-
i;
55985596

5599-
for (i = 0; i < partnatts; i++)
5597+
if (get_partition_strategy(key) == PARTITION_STRATEGY_RANGE)
56005598
{
5601-
AttrNumber partattnum = get_partition_col_attnum(key, i);
5599+
int partnatts = get_partition_natts(key),
5600+
i;
56025601

5603-
if (partattnum == attnum)
5604-
ereport(ERROR,
5605-
(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
5606-
errmsg("column \"%s\" is in range partition key",
5607-
colName)));
5602+
for (i = 0; i < partnatts; i++)
5603+
{
5604+
AttrNumber partattnum = get_partition_col_attnum(key, i);
5605+
5606+
if (partattnum == attnum)
5607+
ereport(ERROR,
5608+
(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
5609+
errmsg("column \"%s\" is in range partition key",
5610+
colName)));
5611+
}
56085612
}
56095613
}
56105614

src/test/regress/expected/alter_table.out

+4
Original file line numberDiff line numberDiff line change
@@ -3029,6 +3029,10 @@ ERROR: cannot alter type of column referenced in partition key expression
30293029
-- cannot drop NOT NULL on columns in the range partition key
30303030
ALTER TABLE partitioned ALTER COLUMN a DROP NOT NULL;
30313031
ERROR: column "a" is in range partition key
3032+
-- it's fine however to drop one on the list partition key column
3033+
CREATE TABLE list_partitioned (a int not null) partition by list (a);
3034+
ALTER TABLE list_partitioned ALTER a DROP NOT NULL;
3035+
DROP TABLE list_partitioned;
30323036
-- partitioned table cannot participate in regular inheritance
30333037
CREATE TABLE foo (
30343038
a int,

src/test/regress/sql/alter_table.sql

+5
Original file line numberDiff line numberDiff line change
@@ -1917,6 +1917,11 @@ ALTER TABLE partitioned ALTER COLUMN b TYPE char(5);
19171917
-- cannot drop NOT NULL on columns in the range partition key
19181918
ALTER TABLE partitioned ALTER COLUMN a DROP NOT NULL;
19191919

1920+
-- it's fine however to drop one on the list partition key column
1921+
CREATE TABLE list_partitioned (a int not null) partition by list (a);
1922+
ALTER TABLE list_partitioned ALTER a DROP NOT NULL;
1923+
DROP TABLE list_partitioned;
1924+
19201925
-- partitioned table cannot participate in regular inheritance
19211926
CREATE TABLE foo (
19221927
a int,

0 commit comments

Comments
 (0)