From 401f81b0bf45243ca5165ee4d3cad7e0c4303ae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabr=C3=ADzio=20de=20Royes=20Mello?= Date: Thu, 5 Jan 2023 16:02:11 -0300 Subject: [PATCH] Fix adding column with NULL constraint Adding new column with NULL constraint to a compressed hypertable is raising an error but it make no sense because NULL constraints in Postgres does nothing, I mean it is useless and exist just for compatibility with other database systems: https://www.postgresql.org/docs/current/ddl-constraints.html#id-1.5.4.6.6 Fixed it by ignoring the NULL constraints when we check for `ALTER TABLE .. ADD COLUMN ..` to a compressed hypertable. Fixes #5151 --- CHANGELOG.md | 4 ++++ src/process_utility.c | 8 ++++++++ tsl/test/expected/compression_ddl.out | 5 ++++- tsl/test/sql/compression_ddl.sql | 5 ++++- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0e30e50835..8785c8adca4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ accidentally triggering the load of a previous DB version.** **Bugfixes** * #4926 Fix corruption when inserting into compressed chunks +* #5152 Fix adding column with NULL constraint to compressed hypertable + +**Thanks** +* @ikkala for reporting error when adding column with NULL constraint to compressed hypertable ## 2.9.1 (2022-12-23) diff --git a/src/process_utility.c b/src/process_utility.c index 055b0b82677..04d81a79f70 100644 --- a/src/process_utility.c +++ b/src/process_utility.c @@ -281,6 +281,14 @@ check_altertable_add_column_for_compressed(Hypertable *ht, ColumnDef *col) Constraint *constraint = lfirst_node(Constraint, lc); switch (constraint->contype) { + /* + * We can safelly ignore NULL constraints because it does nothing + * and according to Postgres docs is useless and exist just for + * compatibility with other database systems + * https://www.postgresql.org/docs/current/ddl-constraints.html#id-1.5.4.6.6 + */ + case CONSTR_NULL: + continue; case CONSTR_NOTNULL: has_notnull = true; continue; diff --git a/tsl/test/expected/compression_ddl.out b/tsl/test/expected/compression_ddl.out index b9b10cf1735..8f020354898 100644 --- a/tsl/test/expected/compression_ddl.out +++ b/tsl/test/expected/compression_ddl.out @@ -72,7 +72,10 @@ WARNING: column type "timestamp without time zone" used for "time" does not fol (1 row) ALTER TABLE records SET (timescaledb.compress = true); -ALTER TABLE records ADD COLUMN col boolean DEFAULT false NOT NULL; +ALTER TABLE records ADD COLUMN col1 boolean DEFAULT false NOT NULL; +-- NULL constraints are useless and it is safe allow adding this +-- column with NULL constraint to a compressed hypertable (Issue #5151) +ALTER TABLE records ADD COLUMN col2 BOOLEAN NULL; DROP table records CASCADE; -- TABLESPACES -- For tablepaces with compressed chunks the semantics are the following: diff --git a/tsl/test/sql/compression_ddl.sql b/tsl/test/sql/compression_ddl.sql index c8d0588fe01..b7263c7e655 100644 --- a/tsl/test/sql/compression_ddl.sql +++ b/tsl/test/sql/compression_ddl.sql @@ -46,7 +46,10 @@ ALTER TABLE test1 ALTER COLUMN b SET STATISTICS 10; CREATE TABLE records (time timestamp NOT NULL); SELECT create_hypertable('records', 'time'); ALTER TABLE records SET (timescaledb.compress = true); -ALTER TABLE records ADD COLUMN col boolean DEFAULT false NOT NULL; +ALTER TABLE records ADD COLUMN col1 boolean DEFAULT false NOT NULL; +-- NULL constraints are useless and it is safe allow adding this +-- column with NULL constraint to a compressed hypertable (Issue #5151) +ALTER TABLE records ADD COLUMN col2 BOOLEAN NULL; DROP table records CASCADE; -- TABLESPACES