Postgresql database TimescaleDB modify partition time (range)

TimescaleDB super table modification partition time of postgresql database (range)


Before encountering a problem today, the super table created with TimescaleDB has been in units of 7 days. Because the amount of data in 7 days is too large, the query efficiency needs to be changed from 7 days to one day.
Insert picture description here

Create a hypertable create_hypertable()

1. Create a regular version table

CREATE TABLE "超表名" (
  "collect_time" timestamp(6) NOT NULL,
  "code" varchar(36) COLLATE "pg_catalog"."default" NOT NULL,
  "value" numeric(14,4) NOT NULL,
  "create_time" timestamp(6) NOT NULL
)
;

2. Change to super table create_hypertable()

SELECT create_hypertable('超表名', 'collect_time', chunk_time_interval => INTERVAL '7 day');

3. Insert data

INSERT INTO 超表名("collect_time", "code", "value",  "create_time") VALUES ('2020-10-15 16:35:00', '22255220522', '23.4672',  '2020-10-14 16:35:26.659');
INSERT INTO 超表名("collect_time", "code", "value",  "create_time") VALUES ('2020-10-16 16:35:00', '26622569666', '0.1085',  '2020-10-14 16:35:27.546');
INSERT INTO 超表名("collect_time", "code", "value",  "create_time") VALUES ('2020-10-13 16:35:00', '525941155555', '25.0549',  '2020-10-14 16:35:28.473');
INSERT INTO 超表名("collect_time", "code", "value",  "create_time") VALUES ('2020-10-14 16:35:00', '744445411114', '0.0000',  '2020-10-14 16:35:24.01');
INSERT INTO 超表名("collect_time", "code", "value",  "create_time") VALUES ('2020-10-12 16:35:00', '774484457444', '0.0000',  '2020-10-14 16:35:23.032');

Check the partition, you will find that the data is in 2 partitions

Modify the partition set_chunk_time_interval()

1. Check the partition situation

1. View _timescaledb_catalog.dimension table

SELECT * FROM "_timescaledb_catalog"."dimension"

Insert picture description here
604800000000 (TIMESTAMP type) is displayed on interval_length, which means one week
. 2. View the status of sub-blocks.
View the dimension_slice table.
Insert picture description here
Conversion timestamp
1602720000000000 2020-10-15 08:00:00
1603324800000000 2020-10-22 08:00:00
Here you can see the partition Is 7 days

2. Modify the partition time set_chunk_time_interval()

1. Modify the partition time

SELECT set_chunk_time_interval('超表名', interval '24 hours');

2. Insert data verification

INSERT INTO 超表名("collect_time", "code", "value", "create_time") VALUES ('2021-1-14 16:35:00', '375222D001', '27.7932', '2020-10-14 16:35:15.011');
INSERT INTO 超表名("collect_time", "code", "value", "create_time") VALUES ('2021-1-15 16:35:00', '3715044111', '0.0000',  '2020-10-14 16:35:20.389');
INSERT INTO 超表名("collect_time", "code", "value", "create_time") VALUES ('2021-1-16 16:35:00', '202Q0019QT001', '0.3663',  '2020-10-14 16:35:19.087');
INSERT INTO 超表名("collect_time", "code", "value", "create_time") VALUES ('2021-1-17 16:35:00', '3702000284441', '22.2946',  '2020-10-14 16:35:15.035');
INSERT INTO 超表名("collect_time", "code", "value", "create_time") VALUES ('2021-1-18 16:35:00', '37075225555501', '0.3022',  '2020-10-14 16:35:24.041');
INSERT INTO 超表名("collect_time", "code", "value", "create_time") VALUES ('2021-1-19 16:35:00', '25555222206001', '0.0000',  '2020-10-14 16:35:23.956');

Three. View the modified results

Look at the _timescaledb_catalog.dimension table and it Insert picture description here
becomes 86400000000.
2. Look at the partitions.
There are more partitions.
Insert picture description here
There is also the second type (not tested).
I wonder if I can directly "_timescaledb_catalog". The interval_length field of the "dimension" table is directly changed to 86400000000

Guess you like

Origin blog.csdn.net/yang_z_1/article/details/112766944