Skip to content

Commit

Permalink
Fix adding column with NULL constraint
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabriziomello committed Jan 5, 2023
1 parent 1751efb commit 401f81b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 8 additions & 0 deletions src/process_utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion tsl/test/expected/compression_ddl.out
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion tsl/test/sql/compression_ddl.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 401f81b

Please sign in to comment.