Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot add dimension if table has empty chunks #1668

Merged
merged 1 commit into from Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@ accidentally triggering the load of a previous DB version.**
**Bugfixes**
* #1648 Drop chunks for materialized hypertable
* #1665 Add ignore_invalidation_older_than to timescaledb_information.continuous_aggregates view
* #1668 Cannot add dimension if hypertable has empty chunks
* #1674 Fix time_bucket_gapfill's interaction with GROUP BY

**Thanks**
Expand Down
8 changes: 5 additions & 3 deletions src/dimension.c
Expand Up @@ -1355,11 +1355,13 @@ ts_dimension_add(PG_FUNCTION_ARGS)

if (!info.skip)
{
if (ts_hypertable_has_tuples(info.table_relid, AccessShareLock))
if (ts_hypertable_has_chunks(info.table_relid, AccessShareLock))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("hypertable \"%s\" is not empty", get_rel_name(info.table_relid)),
errdetail("It is not possible to add dimensions to a non-empty hypertable")));
errmsg("hypertable \"%s\" has tuples or empty chunks",
get_rel_name(info.table_relid)),
errdetail("It is not possible to add dimensions to a hypertable that has "
"chunks. Please truncate the table.")));

/*
* Note that space->num_dimensions reflects the actual number of
Expand Down
10 changes: 10 additions & 0 deletions src/hypertable.c
Expand Up @@ -1250,6 +1250,16 @@ ts_hypertable_has_tuples(Oid table_relid, LOCKMODE lockmode)
return false;
}

bool
ts_hypertable_has_chunks(Oid table_relid, LOCKMODE lockmode)
{
List *chunks = find_inheritance_children(table_relid, lockmode);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
List *chunks = find_inheritance_children(table_relid, lockmode);
return find_inheritance_children(table_relid, lockmode) != NIL;

Can be simplified.

if (chunks != NIL)
return true;
else
return false;
}

static void
hypertable_create_schema(const char *schema_name)
{
Expand Down
1 change: 1 addition & 0 deletions src/hypertable.h
Expand Up @@ -114,6 +114,7 @@ extern char *ts_hypertable_select_tablespace_name(Hypertable *ht, Chunk *chunk);
extern Tablespace *ts_hypertable_get_tablespace_at_offset_from(int32 hypertable_id,
Oid tablespace_oid, int16 offset);
extern bool ts_hypertable_has_tuples(Oid table_relid, LOCKMODE lockmode);
extern bool ts_hypertable_has_chunks(Oid table_relid, LOCKMODE lockmode);
extern void ts_hypertables_rename_schema_name(const char *old_name, const char *new_name);
extern List *ts_hypertable_get_all_by_name(Name schema_name, Name table_name, MemoryContext mctx);
extern bool ts_is_partitioning_column(Hypertable *ht, Index column_attno);
Expand Down
16 changes: 14 additions & 2 deletions test/expected/create_hypertable.out
Expand Up @@ -238,10 +238,10 @@ ERROR: cannot specify both the number of partitions and an interval
--adding a new dimension on a non-empty table should also fail
insert into test_schema.test_table values (123456789, 23.8, 'blue', 'type1', 'nyc', 1, 1);
select add_dimension('test_schema.test_table', 'device_type', 2);
ERROR: hypertable "test_table" is not empty
ERROR: hypertable "test_table" has tuples or empty chunks
-- should fail on non-empty table with 'if_not_exists' in case the dimension does not exists
select add_dimension('test_schema.test_table', 'device_type', 2, if_not_exists => true);
ERROR: hypertable "test_table" is not empty
ERROR: hypertable "test_table" has tuples or empty chunks
\set ON_ERROR_STOP 1
-- should not fail on non-empty table with 'if_not_exists' in case the dimension exists
select add_dimension('test_schema.test_table', 'location', 2, if_not_exists => true);
Expand All @@ -251,6 +251,18 @@ NOTICE: column "location" is already a dimension, skipping
(5,test_schema,test_table,location,f)
(1 row)

--should fail on empty table that still has chunks --
\set ON_ERROR_STOP 0
delete from test_schema.test_table where time is not null;
select count(*) from test_schema.test_table;
count
-------
0
(1 row)

select add_dimension('test_schema.test_table', 'device_type', 2);
ERROR: hypertable "test_table" has tuples or empty chunks
\set ON_ERROR_STOP 1
--show chunks in the associated schema
\dt "chunk_schema".*
List of relations
Expand Down
7 changes: 7 additions & 0 deletions test/sql/create_hypertable.sql
Expand Up @@ -135,6 +135,13 @@ select add_dimension('test_schema.test_table', 'device_type', 2, if_not_exists =
-- should not fail on non-empty table with 'if_not_exists' in case the dimension exists
select add_dimension('test_schema.test_table', 'location', 2, if_not_exists => true);

--should fail on empty table that still has chunks --
\set ON_ERROR_STOP 0
delete from test_schema.test_table where time is not null;
select count(*) from test_schema.test_table;
select add_dimension('test_schema.test_table', 'device_type', 2);
\set ON_ERROR_STOP 1

--show chunks in the associated schema
\dt "chunk_schema".*

Expand Down