Block DROP NOT NULL on time-partitioned columns.

Time-partitioning cannot handle NULL values.
This commit is contained in:
Matvey Arye 2018-01-10 09:59:11 -05:00 committed by Matvey Arye
parent ad7d361418
commit 264956f0c1
5 changed files with 33 additions and 34 deletions

View File

@ -774,6 +774,23 @@ process_altertable_drop_constraint(Hypertable *ht, AlterTableCmd *cmd)
catalog_restore_user(&sec_ctx);
}
static void
process_altertable_drop_not_null(Hypertable *ht, AlterTableCmd *cmd)
{
int i;
for (i = 0; i < ht->space->num_dimensions; i++)
{
Dimension *dim = &ht->space->dimensions[i];
if (IS_OPEN_DIMENSION(dim) &&
strncmp(NameStr(dim->fd.column_name), cmd->name, NAMEDATALEN) == 0)
ereport(ERROR,
(errcode(ERRCODE_IO_OPERATION_NOT_SUPPORTED),
errmsg("Cannot Drop NOT NULL constraint from a time-partitioned column")));
}
}
/* process all regular-table alter commands to make sure they aren't adding
* foreign-key constraints to hypertables */
static void
@ -1312,6 +1329,10 @@ process_altertable_start_table(Node *parsetree)
verify_constraint_hypertable(ht, cmd->def);
}
break;
case AT_DropNotNull:
if (ht != NULL)
process_altertable_drop_not_null(ht, cmd);
break;
case AT_DropConstraint:
case AT_DropConstraintRecurse:
if (ht != NULL)

View File

@ -17,11 +17,16 @@ ERROR: new row for relation "_hyper_1_1_chunk" violates check constraint "hyper
INSERT INTO hyper(time, device_id,sensor_1) VALUES
(1257987700000000000, NULL, 11);
ERROR: null value in column "device_id" violates not-null constraint
ALTER TABLE hyper ALTER COLUMN time DROP NOT NULL;
ERROR: Cannot Drop NOT NULL constraint from a time-partitioned column
\set ON_ERROR_STOP 1
INSERT INTO hyper(time, device_id,sensor_1) VALUES
(1257987700000000000, 'dev2', 11);
INSERT INTO hyper(time, device_id,sensor_1) VALUES
(1257987700000000000, 'dev2', 11);
ALTER TABLE hyper ALTER COLUMN device_id DROP NOT NULL;
INSERT INTO hyper(time, device_id,sensor_1) VALUES
(1257987700000000000, NULL, 11);
----------------------- UNIQUE CONSTRAINTS ------------------
CREATE TABLE hyper_unique_with_looooooooooooooooooooooooooooooooooooong_name (
time BIGINT NOT NULL UNIQUE,

View File

@ -44,34 +44,6 @@ insert into test_schema.test_table_no_not_null (device_id) VALUES('foo');
ERROR: null value in column "time" violates not-null constraint
\set ON_ERROR_STOP 1
insert into test_schema.test_table_no_not_null (time, device_id) VALUES(1, 'foo');
SELECT * FROM test.show_columns('test_schema.test_table_no_not_null');
Column | Type | Nullable
-----------+--------+----------
time | bigint | t
device_id | text | f
(2 rows)
ALTER TABLE test_schema.test_table_no_not_null ALTER time DROP NOT NULL;
SELECT * FROM test.show_columns('test_schema.test_table_no_not_null');
Column | Type | Nullable
-----------+--------+----------
time | bigint | f
device_id | text | f
(2 rows)
SELECT _timescaledb_internal.set_time_columns_not_null();
set_time_columns_not_null
---------------------------
(1 row)
SELECT * FROM test.show_columns('test_schema.test_table_no_not_null');
Column | Type | Nullable
-----------+--------+----------
time | bigint | t
device_id | text | f
(2 rows)
RESET ROLE;
SET ROLE :ROLE_DEFAULT_PERM_USER;
\set ON_ERROR_STOP 0

View File

@ -16,6 +16,8 @@ INSERT INTO hyper(time, device_id,sensor_1) VALUES
INSERT INTO hyper(time, device_id,sensor_1) VALUES
(1257987700000000000, NULL, 11);
ALTER TABLE hyper ALTER COLUMN time DROP NOT NULL;
\set ON_ERROR_STOP 1
INSERT INTO hyper(time, device_id,sensor_1) VALUES
@ -24,6 +26,11 @@ INSERT INTO hyper(time, device_id,sensor_1) VALUES
INSERT INTO hyper(time, device_id,sensor_1) VALUES
(1257987700000000000, 'dev2', 11);
ALTER TABLE hyper ALTER COLUMN device_id DROP NOT NULL;
INSERT INTO hyper(time, device_id,sensor_1) VALUES
(1257987700000000000, NULL, 11);
----------------------- UNIQUE CONSTRAINTS ------------------
CREATE TABLE hyper_unique_with_looooooooooooooooooooooooooooooooooooong_name (

View File

@ -38,12 +38,6 @@ insert into test_schema.test_table_no_not_null (device_id) VALUES('foo');
\set ON_ERROR_STOP 1
insert into test_schema.test_table_no_not_null (time, device_id) VALUES(1, 'foo');
SELECT * FROM test.show_columns('test_schema.test_table_no_not_null');
ALTER TABLE test_schema.test_table_no_not_null ALTER time DROP NOT NULL;
SELECT * FROM test.show_columns('test_schema.test_table_no_not_null');
SELECT _timescaledb_internal.set_time_columns_not_null();
SELECT * FROM test.show_columns('test_schema.test_table_no_not_null');
RESET ROLE;
SET ROLE :ROLE_DEFAULT_PERM_USER;