mirror of
https://github.com/timescale/timescaledb.git
synced 2025-04-20 13:53:19 +08:00
if_not_exist flag to create_hypertable now works on hypertables with data as well
This commit is contained in:
parent
347a8bdacf
commit
997029a86d
@ -33,6 +33,7 @@ DECLARE
|
||||
time_column_type REGTYPE;
|
||||
att_row pg_attribute;
|
||||
main_table_has_items BOOLEAN;
|
||||
is_hypertable BOOLEAN;
|
||||
BEGIN
|
||||
SELECT relname, nspname, reltablespace
|
||||
INTO STRICT table_name, schema_name, tablespace_oid
|
||||
@ -74,6 +75,21 @@ BEGIN
|
||||
END IF;
|
||||
END IF;
|
||||
|
||||
EXECUTE format('SELECT TRUE FROM _timescaledb_catalog.hypertable WHERE
|
||||
hypertable.schema_name = %L AND
|
||||
hypertable.table_name = %L',
|
||||
schema_name, table_name) INTO is_hypertable;
|
||||
|
||||
IF is_hypertable THEN
|
||||
IF if_not_exists THEN
|
||||
RAISE NOTICE 'hypertable % already exists, skipping', main_table;
|
||||
RETURN;
|
||||
ELSE
|
||||
RAISE EXCEPTION 'hypertable % already exists', main_table
|
||||
USING ERRCODE = 'IO110';
|
||||
END IF;
|
||||
END IF;
|
||||
|
||||
EXECUTE format('SELECT TRUE FROM %s LIMIT 1', main_table) INTO main_table_has_items;
|
||||
|
||||
IF main_table_has_items THEN
|
||||
|
@ -49,3 +49,17 @@ NOTICE: hypertable test_schema.test_1dim already exists, skipping
|
||||
select create_hypertable('test_schema.test_1dim', 'time');
|
||||
ERROR: hypertable test_schema.test_1dim already exists
|
||||
\set ON_ERROR_STOP 1
|
||||
-- if_not_exist should also work with data in the hypertable
|
||||
insert into test_schema.test_1dim VALUES ('2004-10-19 10:23:54+02',1.0);
|
||||
select create_hypertable('test_schema.test_1dim', 'time', if_not_exists => true);
|
||||
NOTICE: hypertable test_schema.test_1dim already exists, skipping
|
||||
create_hypertable
|
||||
-------------------
|
||||
|
||||
(1 row)
|
||||
|
||||
-- Should error when creating again without if_not_exists set to true
|
||||
\set ON_ERROR_STOP 0
|
||||
select create_hypertable('test_schema.test_1dim', 'time');
|
||||
ERROR: hypertable test_schema.test_1dim already exists
|
||||
\set ON_ERROR_STOP 1
|
||||
|
@ -57,12 +57,12 @@ INSERT INTO should_drop VALUES (now(), 1.0);
|
||||
SELECT * from _timescaledb_catalog.hypertable;
|
||||
id | schema_name | table_name | associated_schema_name | associated_table_prefix | root_schema_name | root_table_name | replication_factor | placement | time_column_name | time_column_type | created_on | chunk_time_interval
|
||||
----+-------------+-------------+------------------------+-------------------------+-----------------------+-----------------+--------------------+-----------+------------------+-----------------------------+------------+---------------------
|
||||
3 | public | should_drop | _timescaledb_internal | _hyper_3 | _timescaledb_internal | _hyper_3_root | 1 | STICKY | time | timestamp without time zone | single | 2592000000000
|
||||
2 | public | should_drop | _timescaledb_internal | _hyper_2 | _timescaledb_internal | _hyper_2_root | 1 | STICKY | time | timestamp without time zone | single | 2592000000000
|
||||
(1 row)
|
||||
|
||||
SELECT * from _timescaledb_catalog.partition_epoch;
|
||||
id | hypertable_id | start_time | end_time | num_partitions | partitioning_func_schema | partitioning_func | partitioning_mod | partitioning_column
|
||||
----+---------------+------------+----------+----------------+--------------------------+-------------------+------------------+---------------------
|
||||
2 | 3 | | | 1 | | | 32768 |
|
||||
2 | 2 | | | 1 | | | 32768 |
|
||||
(1 row)
|
||||
|
||||
|
@ -18,3 +18,12 @@ select create_hypertable('test_schema.test_1dim', 'time', if_not_exists => true)
|
||||
select create_hypertable('test_schema.test_1dim', 'time');
|
||||
\set ON_ERROR_STOP 1
|
||||
|
||||
-- if_not_exist should also work with data in the hypertable
|
||||
insert into test_schema.test_1dim VALUES ('2004-10-19 10:23:54+02',1.0);
|
||||
select create_hypertable('test_schema.test_1dim', 'time', if_not_exists => true);
|
||||
|
||||
-- Should error when creating again without if_not_exists set to true
|
||||
\set ON_ERROR_STOP 0
|
||||
select create_hypertable('test_schema.test_1dim', 'time');
|
||||
\set ON_ERROR_STOP 1
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user