Skip to content

Commit f85680d

Browse files
committed
Support ALTER TABLE SET (tsdb.chunk_time_interval='1 day')
1 parent c3bb352 commit f85680d

8 files changed

+64
-9
lines changed

src/process_utility.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3745,9 +3745,11 @@ process_create_table_end(Node *parsetree)
37453745
AttrNumber time_attno = get_attnum(table_relid, time_column);
37463746
Oid time_type = get_atttype(table_relid, time_attno);
37473747

3748-
interval = ts_create_table_parse_chunk_time_interval(create_table_info.with_clauses,
3749-
time_type,
3750-
&interval_type);
3748+
interval =
3749+
ts_create_table_parse_chunk_time_interval(create_table_info.with_clauses
3750+
[CreateTableFlagChunkTimeInterval],
3751+
time_type,
3752+
&interval_type);
37513753
}
37523754

37533755
if (!create_table_info.with_clauses[CreateTableFlagCreateDefaultIndexes].is_default)
@@ -4925,7 +4927,28 @@ process_altertable_set_options(AlterTableCmd *cmd, Hypertable *ht)
49254927

49264928
parse_results = ts_alter_table_with_clause_parse(compress_options);
49274929

4928-
ts_cm_functions->process_compress_table(ht, parse_results);
4930+
if (ht && !parse_results[AlterTableFlagChunkTimeInterval].is_default)
4931+
{
4932+
Dimension *dim = ts_hyperspace_get_mutable_dimension(ht->space, DIMENSION_TYPE_OPEN, 0);
4933+
Ensure(dim, "hypertable without open dimension");
4934+
Oid time_type = get_atttype(dim->main_table_relid, dim->column_attno);
4935+
Oid interval_type = InvalidOid;
4936+
Datum interval =
4937+
ts_create_table_parse_chunk_time_interval(parse_results
4938+
[AlterTableFlagChunkTimeInterval],
4939+
time_type,
4940+
&interval_type);
4941+
4942+
int64 chunk_interval = ts_interval_value_to_internal(interval, interval_type);
4943+
ts_dimension_set_chunk_interval(dim, chunk_interval);
4944+
}
4945+
4946+
if (!parse_results[AlterTableFlagCompressEnabled].is_default ||
4947+
!parse_results[AlterTableFlagCompressOrderBy].is_default ||
4948+
!parse_results[AlterTableFlagCompressSegmentBy].is_default ||
4949+
!parse_results[AlterTableFlagCompressChunkTimeInterval].is_default)
4950+
ts_cm_functions->process_compress_table(ht, parse_results);
4951+
49294952
return DDL_DONE;
49304953
}
49314954

src/with_clause/alter_table_with_clause.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
#include "alter_table_with_clause.h"
2525

2626
static const WithClauseDefinition alter_table_with_clause_def[] = {
27+
[AlterTableFlagChunkTimeInterval] = {
28+
.arg_names = {"chunk_time_interval", NULL},
29+
.type_id = TEXTOID,
30+
},
2731
[AlterTableFlagCompressEnabled] = {
2832
.arg_names = {"compress", "columnstore", "enable_columnstore", NULL},
2933
.type_id = BOOLOID,

src/with_clause/alter_table_with_clause.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
typedef enum AlterTableFlags
1717
{
18-
AlterTableFlagCompressEnabled = 0,
18+
AlterTableFlagChunkTimeInterval = 0,
19+
AlterTableFlagCompressEnabled,
1920
AlterTableFlagCompressSegmentBy,
2021
AlterTableFlagCompressOrderBy,
2122
AlterTableFlagCompressChunkTimeInterval,

src/with_clause/create_materialized_view_with_clause.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ ts_continuous_agg_get_compression_defelems(const WithClauseResult *with_clauses)
7979
int option_index = 0;
8080
switch (i)
8181
{
82+
case AlterTableFlagChunkTimeInterval:
83+
continue;
84+
break;
8285
case AlterTableFlagCompressEnabled:
8386
option_index = CreateMaterializedViewFlagCompress;
8487
break;

src/with_clause/create_table_with_clause.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ ts_create_table_with_clause_parse(const List *defelems)
3030
}
3131

3232
Datum
33-
ts_create_table_parse_chunk_time_interval(WithClauseResult *parsed_options, Oid column_type,
33+
ts_create_table_parse_chunk_time_interval(WithClauseResult option, Oid column_type,
3434
Oid *interval_type)
3535
{
36-
if (parsed_options[CreateTableFlagChunkTimeInterval].is_default == false)
36+
if (option.is_default == false)
3737
{
38-
Datum textarg = parsed_options[CreateTableFlagChunkTimeInterval].parsed;
38+
Datum textarg = option.parsed;
3939
switch (column_type)
4040
{
4141
case INT2OID:

src/with_clause/create_table_with_clause.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ typedef enum CreateTableFlags
2121

2222
WithClauseResult *ts_create_table_with_clause_parse(const List *defelems);
2323

24-
Datum ts_create_table_parse_chunk_time_interval(WithClauseResult *parsed_options, Oid column_type,
24+
Datum ts_create_table_parse_chunk_time_interval(WithClauseResult parsed_options, Oid column_type,
2525
Oid *interval_type);

test/expected/create_chunks.out

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,18 @@ SELECT count(*) FROM test_output;
292292
2
293293
(1 row)
294294

295+
-- test ALTER TABLE SET (tsdb.chunk_time_interval) on a hypertable
296+
CREATE TABLE t_with(time timestamptz not null, device text, value float) WITH (tsdb.hypertable,tsdb.time_column='time');
297+
SELECT time_interval FROM timescaledb_information.dimensions WHERE hypertable_name = 't_with';
298+
time_interval
299+
---------------
300+
@ 7 days
301+
(1 row)
302+
303+
ALTER TABLE t_with SET (tsdb.chunk_time_interval = '1 hour');
304+
SELECT time_interval FROM timescaledb_information.dimensions WHERE hypertable_name = 't_with';
305+
time_interval
306+
---------------
307+
@ 1 hour
308+
(1 row)
309+

test/sql/create_chunks.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,12 @@ SELECT relname, relreplident FROM show_chunks('test_ht') ch JOIN pg_class c ON (
171171
SELECT count(*) FROM test_tb;
172172
SELECT count(*) FROM test_ht;
173173
SELECT count(*) FROM test_output;
174+
175+
-- test ALTER TABLE SET (tsdb.chunk_time_interval) on a hypertable
176+
CREATE TABLE t_with(time timestamptz not null, device text, value float) WITH (tsdb.hypertable,tsdb.time_column='time');
177+
178+
SELECT time_interval FROM timescaledb_information.dimensions WHERE hypertable_name = 't_with';
179+
ALTER TABLE t_with SET (tsdb.chunk_time_interval = '1 hour');
180+
SELECT time_interval FROM timescaledb_information.dimensions WHERE hypertable_name = 't_with';
181+
182+

0 commit comments

Comments
 (0)