mirror of
https://github.com/timescale/timescaledb.git
synced 2025-05-18 03:23:37 +08:00
Add logic for creating RULE to redirect INSERTs on root table.
This commit is contained in:
parent
ff7762ea9e
commit
94f3161370
@ -217,6 +217,9 @@ BEGIN
|
||||
-- update insert temp table
|
||||
PERFORM _sysinternal.create_field_on_table(hypertable_row.associated_schema_name, hypertable_row.insert_temp_table_name,
|
||||
NEW.name, NEW.attnum, NEW.data_type, NEW.default_value, NEW.not_null);
|
||||
-- update temp table redirect rule
|
||||
PERFORM _sysinternal.create_insert_temp_table_rule(hypertable_row.associated_schema_name, hypertable_row.insert_temp_table_name,
|
||||
hypertable_row.root_schema_name, hypertable_row.root_table_name);
|
||||
|
||||
IF new.created_on <> current_database() THEN
|
||||
PERFORM set_config('io.ignore_ddl_in_trigger', 'true', true);
|
||||
@ -241,6 +244,9 @@ BEGIN
|
||||
-- update insert temp table
|
||||
PERFORM _sysinternal.exec_alter_column_set_default(hypertable_row.associated_schema_name, hypertable_row.insert_temp_table_name,
|
||||
NEW.name, NEW.default_value);
|
||||
-- update temp table redirect rule
|
||||
PERFORM _sysinternal.create_insert_temp_table_rule(hypertable_row.associated_schema_name, hypertable_row.insert_temp_table_name,
|
||||
hypertable_row.root_schema_name, hypertable_row.root_table_name);
|
||||
|
||||
IF NEW.modified_on <> current_database() THEN
|
||||
PERFORM set_config('io.ignore_ddl_in_trigger', 'true', true);
|
||||
@ -257,6 +263,9 @@ BEGIN
|
||||
-- update insert temp table
|
||||
PERFORM _sysinternal.exec_alter_column_set_not_null(hypertable_row.associated_schema_name, hypertable_row.insert_temp_table_name,
|
||||
NEW.name, NEW.not_null);
|
||||
-- update temp table redirect rule
|
||||
PERFORM _sysinternal.create_insert_temp_table_rule(hypertable_row.associated_schema_name, hypertable_row.insert_temp_table_name,
|
||||
hypertable_row.root_schema_name, hypertable_row.root_table_name);
|
||||
IF NEW.modified_on <> current_database() THEN
|
||||
PERFORM set_config('io.ignore_ddl_in_trigger', 'true', true);
|
||||
-- update main table on others
|
||||
@ -272,6 +281,9 @@ BEGIN
|
||||
-- update insert temp table
|
||||
PERFORM _sysinternal.exec_alter_table_rename_column(hypertable_row.associated_schema_name, hypertable_row.insert_temp_table_name,
|
||||
OLD.name, NEW.name);
|
||||
-- update temp table redirect rule
|
||||
PERFORM _sysinternal.create_insert_temp_table_rule(hypertable_row.associated_schema_name, hypertable_row.insert_temp_table_name,
|
||||
hypertable_row.root_schema_name, hypertable_row.root_table_name);
|
||||
IF NEW.modified_on <> current_database() THEN
|
||||
PERFORM set_config('io.ignore_ddl_in_trigger', 'true', true);
|
||||
-- update main table on others
|
||||
@ -318,12 +330,17 @@ BEGIN
|
||||
FROM hypertable AS h
|
||||
WHERE h.name = NEW.hypertable_name;
|
||||
|
||||
-- drop temp table redirect rule briefly
|
||||
PERFORM _sysinternal.drop_insert_temp_table_rule(hypertable_row.root_schema_name, hypertable_row.root_table_name);
|
||||
-- update root table
|
||||
PERFORM _sysinternal.drop_field_on_table(hypertable_row.root_schema_name, hypertable_row.root_table_name,
|
||||
NEW.name);
|
||||
-- update insert temp table
|
||||
PERFORM _sysinternal.drop_field_on_table(hypertable_row.associated_schema_name, hypertable_row.insert_temp_table_name,
|
||||
NEW.name);
|
||||
-- update temp table redirect rule
|
||||
PERFORM _sysinternal.create_insert_temp_table_rule(hypertable_row.associated_schema_name, hypertable_row.insert_temp_table_name,
|
||||
hypertable_row.root_schema_name, hypertable_row.root_table_name);
|
||||
|
||||
IF NEW.deleted_on <> current_database() THEN
|
||||
PERFORM set_config('io.ignore_ddl_in_trigger', 'true', true);
|
||||
|
@ -16,7 +16,7 @@ BEGIN
|
||||
|
||||
PERFORM _sysinternal.create_root_table(NEW.root_schema_name, NEW.root_table_name);
|
||||
PERFORM _sysinternal.create_root_distinct_table(NEW.distinct_schema_name, NEW.distinct_table_name);
|
||||
PERFORM _sysinternal.create_insert_temp_table(NEW.associated_schema_name, NEW.insert_temp_table_name);
|
||||
PERFORM _sysinternal.create_insert_temp_table(NEW.associated_schema_name, NEW.insert_temp_table_name, NEW.main_schema_name, NEW.main_table_name);
|
||||
|
||||
IF NEW.created_on <> current_database() THEN
|
||||
PERFORM _sysinternal.create_main_table(NEW.main_schema_name, NEW.main_table_name);
|
||||
|
@ -41,13 +41,51 @@ BEGIN
|
||||
END
|
||||
$BODY$;
|
||||
|
||||
-- Creates the temporary table for INSERTs. INSERTs on the root table are
|
||||
-- redirected to the temporary table using a RULE. An associated trigger
|
||||
-- on this table then inserts all rows in bulk after all rows are INSERTed.
|
||||
-- The table is UNLOGGED for performance.
|
||||
-- Creates/updates the rule governing INSERTs.
|
||||
-- Called whenever a field is added to the root table so that the redirection
|
||||
-- of data is kept up to date.
|
||||
CREATE OR REPLACE FUNCTION _sysinternal.create_insert_temp_table_rule(
|
||||
temp_schema_name NAME,
|
||||
temp_table_name NAME,
|
||||
main_schema_name NAME,
|
||||
main_table_name NAME
|
||||
)
|
||||
RETURNS VOID LANGUAGE PLPGSQL VOLATILE AS
|
||||
$BODY$
|
||||
BEGIN
|
||||
EXECUTE format(
|
||||
$$
|
||||
CREATE OR REPLACE RULE %I AS ON INSERT TO %I.%I DO INSTEAD INSERT INTO %I.%I (SELECT NEW.*)
|
||||
$$, '_redirect', main_schema_name, main_table_name, temp_schema_name, temp_table_name);
|
||||
END
|
||||
$BODY$;
|
||||
|
||||
-- Drops the rule governing INSERTs.
|
||||
-- Called whenever a field is removed from the root table otherwise the rule
|
||||
-- refers to a non-existent field.
|
||||
CREATE OR REPLACE FUNCTION _sysinternal.drop_insert_temp_table_rule(
|
||||
root_schema_name NAME,
|
||||
root_table_name NAME
|
||||
)
|
||||
RETURNS VOID LANGUAGE PLPGSQL VOLATILE AS
|
||||
$BODY$
|
||||
BEGIN
|
||||
EXECUTE format(
|
||||
$$
|
||||
DROP RULE IF EXISTS %I ON %I.%I
|
||||
$$, '_redirect', root_schema_name, root_table_name);
|
||||
END
|
||||
$BODY$;
|
||||
|
||||
-- Creates the temporary table for INSERTs.
|
||||
-- INSERTs on the root table are redirected to the temporary table using a RULE.
|
||||
-- An associated trigger on this table then inserts all rows in bulk after
|
||||
-- all rows are INSERTed. The table is UNLOGGED for performance.
|
||||
CREATE OR REPLACE FUNCTION _sysinternal.create_insert_temp_table(
|
||||
schema_name NAME,
|
||||
table_name NAME
|
||||
temp_schema_name NAME,
|
||||
temp_table_name NAME,
|
||||
main_schema_name NAME,
|
||||
main_table_name NAME
|
||||
)
|
||||
RETURNS VOID LANGUAGE PLPGSQL VOLATILE AS
|
||||
$BODY$
|
||||
@ -56,7 +94,8 @@ BEGIN
|
||||
$$
|
||||
CREATE UNLOGGED TABLE IF NOT EXISTS %I.%I (
|
||||
)
|
||||
$$, schema_name, table_name);
|
||||
$$, temp_schema_name, temp_table_name);
|
||||
PERFORM _sysinternal.create_insert_temp_table_rule(temp_schema_name, temp_table_name, main_schema_name, main_table_name);
|
||||
END
|
||||
$BODY$;
|
||||
|
||||
|
@ -430,6 +430,14 @@ primary key, btree, for table "_sys_1_testNs._hyper_1_distinct"
|
||||
occupied | boolean | | plain | |
|
||||
latitude | bigint | | plain | |
|
||||
really_long_field_goes_on_and_on_and_on_and_on_and_on_and_on_an | bigint | | plain | |
|
||||
Rules:
|
||||
_redirect AS
|
||||
ON INSERT TO "_sys_1_testNs"._hyper_1_root DO INSTEAD INSERT INTO "_sys_1_testNs"._hyper_1_insert_temp ("time", "Device_id", temp, occupied, latitude, really_long_field_goes_on_and_on_and_on_and_on_and_on_and_on_an) SELECT new."time",
|
||||
new."Device_id",
|
||||
new.temp,
|
||||
new.occupied,
|
||||
new.latitude,
|
||||
new.really_long_field_goes_on_and_on_and_on_and_on_and_on_and_on_an
|
||||
Child tables: "_sys_1_testNs"._hyper_1_0_replica
|
||||
|
||||
--\d+ "_sys_1_testNs"."_sys_1_testNs_1_0_partition"
|
||||
@ -592,6 +600,14 @@ primary key, btree, for table "_sys_1_testNs._hyper_1_distinct"
|
||||
occupied | boolean | | plain | |
|
||||
latitude | bigint | | plain | |
|
||||
really_long_field_goes_on_and_on_and_on_and_on_and_on_and_on_an | bigint | | plain | |
|
||||
Rules:
|
||||
_redirect AS
|
||||
ON INSERT TO "_sys_1_testNs"._hyper_1_root DO INSTEAD INSERT INTO "_sys_1_testNs"._hyper_1_insert_temp ("time", "Device_id", temp, occupied, latitude, really_long_field_goes_on_and_on_and_on_and_on_and_on_and_on_an) SELECT new."time",
|
||||
new."Device_id",
|
||||
new.temp,
|
||||
new.occupied,
|
||||
new.latitude,
|
||||
new.really_long_field_goes_on_and_on_and_on_and_on_and_on_and_on_an
|
||||
Child tables: "_sys_1_testNs"._hyper_1_0_replica
|
||||
|
||||
\c meta
|
||||
@ -826,5 +842,13 @@ primary key, btree, for table "_sys_1_testNs._hyper_1_distinct"
|
||||
occupied | boolean | | plain | |
|
||||
latitude | bigint | | plain | |
|
||||
really_long_field_goes_on_and_on_and_on_and_on_and_on_and_on_an | bigint | | plain | |
|
||||
Rules:
|
||||
_redirect AS
|
||||
ON INSERT TO "_sys_1_testNs"._hyper_1_root DO INSTEAD INSERT INTO "_sys_1_testNs"._hyper_1_insert_temp ("time", "Device_id", temp, occupied, latitude, really_long_field_goes_on_and_on_and_on_and_on_and_on_and_on_an) SELECT new."time",
|
||||
new."Device_id",
|
||||
new.temp,
|
||||
new.occupied,
|
||||
new.latitude,
|
||||
new.really_long_field_goes_on_and_on_and_on_and_on_and_on_and_on_an
|
||||
Child tables: "_sys_1_testNs"._hyper_1_0_replica
|
||||
|
||||
|
@ -163,6 +163,16 @@ Indexes:
|
||||
sensor_2 | numeric | not null default 1 | main | |
|
||||
sensor_3 | numeric | not null default 1 | main | |
|
||||
sensor_4 | numeric | not null default 1 | main | |
|
||||
Rules:
|
||||
_redirect AS
|
||||
ON INSERT TO _sys_1_._hyper_1_root DO INSTEAD INSERT INTO _sys_1_._hyper_1_insert_temp ("time", "Device_id", temp_c, humidity, sensor_1, sensor_2, sensor_3, sensor_4) SELECT new."time",
|
||||
new."Device_id",
|
||||
new.temp_c,
|
||||
new.humidity,
|
||||
new.sensor_1,
|
||||
new.sensor_2,
|
||||
new.sensor_3,
|
||||
new.sensor_4
|
||||
Child tables: _sys_1_._hyper_1_0_replica
|
||||
|
||||
\d+ _sys_1_._hyper_1_1_0_1_data
|
||||
@ -224,6 +234,16 @@ Indexes:
|
||||
sensor_2 | numeric | not null default 1 | main | |
|
||||
sensor_3 | numeric | not null default 1 | main | |
|
||||
sensor_4 | numeric | not null default 1 | main | |
|
||||
Rules:
|
||||
_redirect AS
|
||||
ON INSERT TO _sys_1_._hyper_1_root DO INSTEAD INSERT INTO _sys_1_._hyper_1_insert_temp ("time", "Device_id", temp_c, humidity, sensor_1, sensor_2, sensor_3, sensor_4) SELECT new."time",
|
||||
new."Device_id",
|
||||
new.temp_c,
|
||||
new.humidity,
|
||||
new.sensor_1,
|
||||
new.sensor_2,
|
||||
new.sensor_3,
|
||||
new.sensor_4
|
||||
Child tables: _sys_1_._hyper_1_0_replica
|
||||
|
||||
SELECT set_is_distinct_flag('"public"."Hypertable_1"', 'sensor_2', FALSE);
|
||||
@ -288,6 +308,17 @@ Indexes:
|
||||
temp_f | integer | not null default 31 | plain | |
|
||||
sensor_3 | bigint | not null default 131 | plain | |
|
||||
sensor_4 | bigint | not null default 131 | plain | |
|
||||
Rules:
|
||||
_redirect AS
|
||||
ON INSERT TO _sys_1_._hyper_1_root DO INSTEAD INSERT INTO _sys_1_._hyper_1_insert_temp ("time", "Device_id", humidity, sensor_1, sensor_2_renamed, sensor_3_renamed, temp_f, sensor_3, sensor_4) SELECT new."time",
|
||||
new."Device_id",
|
||||
new.humidity,
|
||||
new.sensor_1,
|
||||
new.sensor_2_renamed,
|
||||
new.sensor_3_renamed,
|
||||
new.temp_f,
|
||||
new.sensor_3,
|
||||
new.sensor_4
|
||||
Child tables: _sys_1_._hyper_1_0_replica
|
||||
|
||||
SELECT * FROM _sys_1_._hyper_1_0_1_distinct_data;
|
||||
@ -328,6 +359,17 @@ Indexes:
|
||||
temp_f | integer | not null default 31 | plain | |
|
||||
sensor_3 | bigint | not null default 131 | plain | |
|
||||
sensor_4 | bigint | not null default 131 | plain | |
|
||||
Rules:
|
||||
_redirect AS
|
||||
ON INSERT TO _sys_1_._hyper_1_root DO INSTEAD INSERT INTO _sys_1_._hyper_1_insert_temp ("time", "Device_id", humidity, sensor_1, sensor_2_renamed, sensor_3_renamed, temp_f, sensor_3, sensor_4) SELECT new."time",
|
||||
new."Device_id",
|
||||
new.humidity,
|
||||
new.sensor_1,
|
||||
new.sensor_2_renamed,
|
||||
new.sensor_3_renamed,
|
||||
new.temp_f,
|
||||
new.sensor_3,
|
||||
new.sensor_4
|
||||
Child tables: _sys_1_._hyper_1_0_replica
|
||||
|
||||
\d+ _sys_1_._hyper_1_1_0_1_data
|
||||
|
@ -338,6 +338,14 @@ primary key, btree, for table "testNs._hyper_1_distinct"
|
||||
series_1 | double precision | | plain | |
|
||||
series_2 | double precision | | plain | |
|
||||
series_bool | boolean | | plain | |
|
||||
Rules:
|
||||
_redirect AS
|
||||
ON INSERT TO "testNs"._hyper_1_root DO INSTEAD INSERT INTO "testNs"._hyper_1_insert_temp ("time", device_id, series_0, series_1, series_2, series_bool) SELECT new."time",
|
||||
new.device_id,
|
||||
new.series_0,
|
||||
new.series_1,
|
||||
new.series_2,
|
||||
new.series_bool
|
||||
Child tables: "testNs"._hyper_1_0_replica
|
||||
|
||||
\c test2
|
||||
@ -467,6 +475,14 @@ primary key, btree, for table "testNs._hyper_1_distinct"
|
||||
series_1 | double precision | | plain | |
|
||||
series_2 | double precision | | plain | |
|
||||
series_bool | boolean | | plain | |
|
||||
Rules:
|
||||
_redirect AS
|
||||
ON INSERT TO "testNs"._hyper_1_root DO INSTEAD INSERT INTO "testNs"._hyper_1_insert_temp ("time", device_id, series_0, series_1, series_2, series_bool) SELECT new."time",
|
||||
new.device_id,
|
||||
new.series_0,
|
||||
new.series_1,
|
||||
new.series_2,
|
||||
new.series_bool
|
||||
Child tables: "testNs"._hyper_1_0_replica
|
||||
|
||||
SELECT *
|
||||
|
@ -338,6 +338,14 @@ primary key, btree, for table "testNs._hyper_1_distinct"
|
||||
series_1 | double precision | | plain | |
|
||||
series_2 | double precision | | plain | |
|
||||
series_bool | boolean | | plain | |
|
||||
Rules:
|
||||
_redirect AS
|
||||
ON INSERT TO "testNs"._hyper_1_root DO INSTEAD INSERT INTO "testNs"._hyper_1_insert_temp ("time", device_id, series_0, series_1, series_2, series_bool) SELECT new."time",
|
||||
new.device_id,
|
||||
new.series_0,
|
||||
new.series_1,
|
||||
new.series_2,
|
||||
new.series_bool
|
||||
Child tables: "testNs"._hyper_1_0_replica
|
||||
|
||||
\c test2
|
||||
@ -467,6 +475,14 @@ primary key, btree, for table "testNs._hyper_1_distinct"
|
||||
series_1 | double precision | | plain | |
|
||||
series_2 | double precision | | plain | |
|
||||
series_bool | boolean | | plain | |
|
||||
Rules:
|
||||
_redirect AS
|
||||
ON INSERT TO "testNs"._hyper_1_root DO INSTEAD INSERT INTO "testNs"._hyper_1_insert_temp ("time", device_id, series_0, series_1, series_2, series_bool) SELECT new."time",
|
||||
new.device_id,
|
||||
new.series_0,
|
||||
new.series_1,
|
||||
new.series_2,
|
||||
new.series_bool
|
||||
Child tables: "testNs"._hyper_1_0_replica
|
||||
|
||||
SELECT *
|
||||
|
Loading…
x
Reference in New Issue
Block a user