Skip to content

Commit 8003205

Browse files
committed
Support associated_schema and associated_table_prefix in CREATE TABLE ... WITH ()
1 parent 541856d commit 8003205

File tree

5 files changed

+146
-2
lines changed

5 files changed

+146
-2
lines changed

src/process_utility.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3724,8 +3724,12 @@ process_create_table_end(Node *parsetree)
37243724
char *time_column =
37253725
TextDatumGetCString(create_table_info.with_clauses[CreateTableFlagTimeColumn].parsed);
37263726
NameData time_column_name;
3727+
NameData associated_schema_name;
3728+
NameData associated_table_prefix;
37273729
namestrcpy(&time_column_name, time_column);
37283730
uint32 flags = 0;
3731+
bool has_associated_schema = false;
3732+
bool has_associated_table_prefix = false;
37293733

37303734
if (get_attnum(table_relid, time_column) == InvalidAttrNumber)
37313735
ereport(ERROR,
@@ -3752,6 +3756,23 @@ process_create_table_end(Node *parsetree)
37523756
flags |= HYPERTABLE_CREATE_DISABLE_DEFAULT_INDEXES;
37533757
}
37543758

3759+
if (!create_table_info.with_clauses[CreateTableFlagAssociatedSchema].is_default)
3760+
{
3761+
has_associated_schema = true;
3762+
namestrcpy(&associated_schema_name,
3763+
TextDatumGetCString(
3764+
create_table_info.with_clauses[CreateTableFlagAssociatedSchema].parsed));
3765+
}
3766+
3767+
if (!create_table_info.with_clauses[CreateTableFlagAssociatedTablePrefix].is_default)
3768+
{
3769+
has_associated_table_prefix = true;
3770+
namestrcpy(&associated_table_prefix,
3771+
TextDatumGetCString(
3772+
create_table_info.with_clauses[CreateTableFlagAssociatedTablePrefix]
3773+
.parsed));
3774+
}
3775+
37553776
DimensionInfo *open_dim_info =
37563777
ts_dimension_info_create_open(table_relid,
37573778
&time_column_name, /* column name */
@@ -3768,8 +3789,11 @@ process_create_table_end(Node *parsetree)
37683789
flags, /* flags */
37693790
open_dim_info, /* open_dim_info */
37703791
NULL, /* closed_dim_info */
3771-
NULL, /* associated_schema_name */
3772-
NULL, /* associated_table_prefix */
3792+
has_associated_schema ? &associated_schema_name :
3793+
NULL, /* associated_schema_name */
3794+
has_associated_table_prefix ?
3795+
&associated_table_prefix :
3796+
NULL, /* associated_table_prefix */
37733797
csi);
37743798
}
37753799
}

src/with_clause/create_table_with_clause.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ static const WithClauseDefinition create_table_with_clauses_def[] = {
1717
[CreateTableFlagTimeColumn] = {.arg_names = {"time_column", NULL}, .type_id = TEXTOID,},
1818
[CreateTableFlagChunkTimeInterval] = {.arg_names = {"chunk_time_interval", NULL}, .type_id = TEXTOID,},
1919
[CreateTableFlagCreateDefaultIndexes] = {.arg_names = {"create_default_indexes", NULL}, .type_id = BOOLOID, .default_val = (Datum) true,},
20+
[CreateTableFlagAssociatedSchema] = {.arg_names = {"associated_schema", NULL}, .type_id = TEXTOID,},
21+
[CreateTableFlagAssociatedTablePrefix] = {.arg_names = {"associated_table_prefix", NULL}, .type_id = TEXTOID,},
2022
};
2123

2224
WithClauseResult *

src/with_clause/create_table_with_clause.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ typedef enum CreateTableFlags
1515
CreateTableFlagTimeColumn,
1616
CreateTableFlagChunkTimeInterval,
1717
CreateTableFlagCreateDefaultIndexes,
18+
CreateTableFlagAssociatedSchema,
19+
CreateTableFlagAssociatedTablePrefix,
1820
} CreateTableFlags;
1921

2022
WithClauseResult *ts_create_table_with_clause_parse(const List *defelems);

test/expected/create_table_with.out

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
-- This file and its contents are licensed under the Apache License 2.0.
22
-- Please see the included NOTICE for copyright information and
33
-- LICENSE-APACHE for a copy of the license.
4+
-- our user needs permission to create schema for the schema tests
5+
\c :TEST_DBNAME :ROLE_SUPERUSER
6+
GRANT CREATE ON DATABASE :TEST_DBNAME TO :ROLE_DEFAULT_PERM_USER;
7+
\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER
48
-- create table with non-tsdb option should not be affected
59
CREATE TABLE t1(time timestamptz, device text, value float) WITH (autovacuum_enabled);
610
DROP TABLE t1;
@@ -177,3 +181,75 @@ SELECT indexrelid::regclass from pg_index where indrelid='t10'::regclass ORDER B
177181
(0 rows)
178182

179183
ROLLBACK;
184+
-- associated_schema
185+
BEGIN;
186+
CREATE TABLE t11(time timestamptz NOT NULL, device text, value float) WITH (tsdb.hypertable,tsdb.time_column='time');
187+
SELECT associated_schema_name FROM _timescaledb_catalog.hypertable WHERE table_name = 't11';
188+
associated_schema_name
189+
------------------------
190+
_timescaledb_internal
191+
(1 row)
192+
193+
ROLLBACK;
194+
BEGIN;
195+
CREATE TABLE t11(time timestamptz NOT NULL, device text, value float) WITH (tsdb.hypertable,tsdb.time_column='time', tsdb.associated_schema='abc');
196+
SELECT associated_schema_name FROM _timescaledb_catalog.hypertable WHERE table_name = 't11';
197+
associated_schema_name
198+
------------------------
199+
abc
200+
(1 row)
201+
202+
INSERT INTO t11 SELECT '2025-01-01', 'd1', 0.1;
203+
SELECT relname from pg_class where relnamespace = 'abc'::regnamespace ORDER BY 1;
204+
relname
205+
--------------------------------
206+
_hyper_18_1_chunk
207+
_hyper_18_1_chunk_t11_time_idx
208+
(2 rows)
209+
210+
ROLLBACK;
211+
BEGIN;
212+
CREATE SCHEMA abc2;
213+
CREATE TABLE t11(time timestamptz NOT NULL, device text, value float) WITH (tsdb.hypertable,tsdb.time_column='time', tsdb.associated_schema='abc2');
214+
SELECT associated_schema_name FROM _timescaledb_catalog.hypertable WHERE table_name = 't11';
215+
associated_schema_name
216+
------------------------
217+
abc2
218+
(1 row)
219+
220+
INSERT INTO t11 SELECT '2025-01-01', 'd1', 0.1;
221+
SELECT relname from pg_class where relnamespace = 'abc2'::regnamespace ORDER BY 1;
222+
relname
223+
--------------------------------
224+
_hyper_19_2_chunk
225+
_hyper_19_2_chunk_t11_time_idx
226+
(2 rows)
227+
228+
ROLLBACK;
229+
-- associated_table_prefix
230+
BEGIN;
231+
CREATE TABLE t12(time timestamptz NOT NULL, device text, value float) WITH (tsdb.hypertable,tsdb.time_column='time');
232+
SELECT associated_table_prefix FROM _timescaledb_catalog.hypertable WHERE table_name = 't12';
233+
associated_table_prefix
234+
-------------------------
235+
_hyper_20
236+
(1 row)
237+
238+
ROLLBACK;
239+
BEGIN;
240+
CREATE TABLE t12(time timestamptz NOT NULL, device text, value float) WITH (tsdb.hypertable,tsdb.time_column='time', tsdb.associated_schema='abc', tsdb.associated_table_prefix='tbl_prefix');
241+
SELECT associated_table_prefix FROM _timescaledb_catalog.hypertable WHERE table_name = 't12';
242+
associated_table_prefix
243+
-------------------------
244+
tbl_prefix
245+
(1 row)
246+
247+
INSERT INTO t12 SELECT '2025-01-01', 'd1', 0.1;
248+
SELECT relname from pg_class where relnamespace = 'abc'::regnamespace ORDER BY 1;
249+
relname
250+
---------------------------------
251+
tbl_prefix_3_chunk
252+
tbl_prefix_3_chunk_t12_time_idx
253+
(2 rows)
254+
255+
ROLLBACK;

test/sql/create_table_with.sql

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
-- Please see the included NOTICE for copyright information and
33
-- LICENSE-APACHE for a copy of the license.
44

5+
-- our user needs permission to create schema for the schema tests
6+
\c :TEST_DBNAME :ROLE_SUPERUSER
7+
GRANT CREATE ON DATABASE :TEST_DBNAME TO :ROLE_DEFAULT_PERM_USER;
8+
\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER
9+
510
-- create table with non-tsdb option should not be affected
611
CREATE TABLE t1(time timestamptz, device text, value float) WITH (autovacuum_enabled);
712
DROP TABLE t1;
@@ -107,3 +112,38 @@ CREATE TABLE t10(time timestamptz NOT NULL, device text, value float) WITH (tsdb
107112
SELECT indexrelid::regclass from pg_index where indrelid='t10'::regclass ORDER BY indexrelid::regclass::text;
108113
ROLLBACK;
109114

115+
-- associated_schema
116+
BEGIN;
117+
CREATE TABLE t11(time timestamptz NOT NULL, device text, value float) WITH (tsdb.hypertable,tsdb.time_column='time');
118+
SELECT associated_schema_name FROM _timescaledb_catalog.hypertable WHERE table_name = 't11';
119+
ROLLBACK;
120+
121+
BEGIN;
122+
CREATE TABLE t11(time timestamptz NOT NULL, device text, value float) WITH (tsdb.hypertable,tsdb.time_column='time', tsdb.associated_schema='abc');
123+
SELECT associated_schema_name FROM _timescaledb_catalog.hypertable WHERE table_name = 't11';
124+
INSERT INTO t11 SELECT '2025-01-01', 'd1', 0.1;
125+
SELECT relname from pg_class where relnamespace = 'abc'::regnamespace ORDER BY 1;
126+
ROLLBACK;
127+
128+
BEGIN;
129+
CREATE SCHEMA abc2;
130+
CREATE TABLE t11(time timestamptz NOT NULL, device text, value float) WITH (tsdb.hypertable,tsdb.time_column='time', tsdb.associated_schema='abc2');
131+
SELECT associated_schema_name FROM _timescaledb_catalog.hypertable WHERE table_name = 't11';
132+
INSERT INTO t11 SELECT '2025-01-01', 'd1', 0.1;
133+
SELECT relname from pg_class where relnamespace = 'abc2'::regnamespace ORDER BY 1;
134+
ROLLBACK;
135+
136+
-- associated_table_prefix
137+
BEGIN;
138+
CREATE TABLE t12(time timestamptz NOT NULL, device text, value float) WITH (tsdb.hypertable,tsdb.time_column='time');
139+
SELECT associated_table_prefix FROM _timescaledb_catalog.hypertable WHERE table_name = 't12';
140+
ROLLBACK;
141+
142+
BEGIN;
143+
CREATE TABLE t12(time timestamptz NOT NULL, device text, value float) WITH (tsdb.hypertable,tsdb.time_column='time', tsdb.associated_schema='abc', tsdb.associated_table_prefix='tbl_prefix');
144+
SELECT associated_table_prefix FROM _timescaledb_catalog.hypertable WHERE table_name = 't12';
145+
INSERT INTO t12 SELECT '2025-01-01', 'd1', 0.1;
146+
SELECT relname from pg_class where relnamespace = 'abc'::regnamespace ORDER BY 1;
147+
ROLLBACK;
148+
149+

0 commit comments

Comments
 (0)