时序数据库 TimescaleDB 安装与使用

news/2024/5/19 23:47:34/文章来源:https://blog.csdn.net/qq_41773806/article/details/132677234

在这里插入图片描述

TimescaleDB 是一个时间序列数据库,建立在 PostgreSQL 之上。然而,不仅如此,它还是时间序列的关系数据库。使用 TimescaleDB 的开发人员将受益于专门构建的时间序列数据库以及经典的关系数据库 (PostgreSQL),所有这些都具有完整的 SQL 支持。本文介绍 TimescaleDB 的 CentOS 7 环境源码编译安装与使用。

01 源码安装


安装 TimescaleDB 之前确保你的机器上已经安装好了 PostgreSQL,并且检查安装的 PG 版本与 TimescaleDB 版本兼容情况:https://docs.timescale.com/self-hosted/latest/upgrades/upgrade-pg/
在这里插入图片描述
源码安装需要使用到 CMake 和 GUN 编译器 gcc 等工具,确保机器已经安装了这些工具

1.1 源码安装 TimescaleDB

  1. 拉取 TimescaleDB 源码
git clone https://github.com/timescale/timescaledb
  1. 进入源码目录并切换分支到指定版本
cd timescaledb
git checkout 2.11.1
  1. 执行 bootstrap 引导构建系统
./bootstrap 

遇到报错:PostgreSQL was built without OpenSSL support, which TimescaleDB needs for full compatibility
解决方法:重新编译安装 postgresql,增加 --with-openssl 选项
./configure --prefix=/home/randy/soft/postgresql --with-openssl
或者执行 ./bootstrap -DUSE_OPENSSL=0 不使用 openssl
在这里插入图片描述

  1. 进入上一步生成的 build 目录,编译安装 timescaledb
cd build
make && make install

安装完成之后,就可以在 postgresql 插件目录下看到 timescaledb 了
在这里插入图片描述

1.2 修改 PG 配置

完成 TimescaleDB 安装之后,需要修改 postgresql 配置文件增加 timescaledb 预加载库

  1. 找到 pg 配置文件
$ psql -Upostgres -dpostgres -c "SHOW config_file;"
Password for user postgres:config_file
--------------------------------------------------/home/randy/soft/postgresql/data/postgresql.conf
(1 row)
  1. 修改 pg 配置文件
vim /home/randy/soft/postgresql/data/postgresql.conf
# 做如下修改
shared_preload_libraries = 'timescaledb'

修改完成之后重启 pg

1.3 安装 TimescaleDB 插件

  1. 执行如下命令创建 timescaledb 插件
CREATE EXTENSION IF NOT EXISTS timescaledb;
  1. 完成插件创建之后可以使用 \dx 命令查看插件是否成功安装
testdb=# \dxList of installed extensionsName     | Version |   Schema   |                                      Description
-------------+---------+------------+---------------------------------------------------------------------------------------plpgsql     | 1.0     | pg_catalog | PL/pgSQL procedural languagetimescaledb | 2.11.1  | public     | Enables scalable inserts and complex queries for time-series data (Community Edition)
(2 rows)

在这里插入图片描述
至此,postgresql + timescaledb 时序数据库就安装完成了

  1. 不想再用这个插件的话,使用如下命令删除插件
DROP EXTENSION IF EXISTS timescaledb;

02 Timescale 基础使用


2.1 创建超表 hypertables

  1. CREATE 创建一个 pg 普通表
testdb=# CREATE TABLE conditions (time        TIMESTAMPTZ       NOT NULL,location    TEXT              NOT NULL,device      TEXT              NOT NULL,temperature DOUBLE PRECISION  NULL,humidity    DOUBLE PRECISION  NULL
);
CREATE TABLE

然后使用 create_hypertable 函数将普通表转化成 hypertable

testdb=# SELECT create_hypertable('conditions', 'time');create_hypertable
-------------------------(1,public,conditions,t)
(1 row)
  1. INSERT 向 hypertable 中插入数据
testdb=# INSERT INTO conditions(time, location, device, temperature, humidity)
SELECT now(), to_char(i, 'FM0000'), to_char(i, 'FM00000'), random()*i, random()*i FROM generate_series(1,10000) i;
INSERT 0 10000
  1. SELECT 查询 hypertable 数据
testdb=# SELECT * FROM conditions ORDER BY time DESC LIMIT 10;time              | location | device |     temperature     |       humidity
-------------------------------+----------+--------+---------------------+----------------------2023-08-03 12:37:00.345665+08 | 0001     | 00001  | 0.24349980974765373 |   0.59486877297972642023-08-03 12:37:00.345665+08 | 0002     | 00002  |  1.8149739913052656 |    0.6162655023691672023-08-03 12:37:00.345665+08 | 0003     | 00003  |   2.400422475569293 |   0.68700570944077912023-08-03 12:37:00.345665+08 | 0004     | 00004  |   2.639553072461581 |   1.94090348497051932023-08-03 12:37:00.345665+08 | 0005     | 00005  |   2.127623537273031 |   3.88715035379826552023-08-03 12:37:00.345665+08 | 0006     | 00006  |  2.3469833801156312 |    4.4115299335274262023-08-03 12:37:00.345665+08 | 0007     | 00007  |  1.0073460031664823 | 0.0168279977406164962023-08-03 12:37:00.345665+08 | 0008     | 00008  |   7.023014897212306 |   0.86792935440220732023-08-03 12:37:00.345665+08 | 0009     | 00009  |   6.744935559863428 |    6.3129489812979682023-08-03 12:37:00.345665+08 | 0010     | 00010  |   9.279208258323166 |    9.451548543523778
(10 rows)
  1. UPDATE 更新 hypertable 数据

更新数据的语句与标准 SQL 一致,如下示例修改指定记录的值

testdb=# SELECT * FROM conditions
WHERE time = '2023-08-03 12:37:00.345665+08' AND location = '0001';time              | location | device |     temperature     |      humidity
-------------------------------+----------+--------+---------------------+--------------------2023-08-03 12:37:00.345665+08 | 0001     | 00001  | 0.24349980974765373 | 0.5948687729797264
(1 row)testdb=# UPDATE conditions SET temperature = 70.2, humidity = 50.0
WHERE time = '2023-08-03 12:37:00.345665+08' AND location = '0001';
UPDATE 1
testdb=# SELECT * FROM conditions
WHERE time = '2023-08-03 12:37:00.345665+08' AND location = '0001';time              | location | device | temperature | humidity
-------------------------------+----------+--------+-------------+----------2023-08-03 12:37:00.345665+08 | 0001     | 00001  |        70.2 |       50
(1 row)

也可以修改该指定范围内的多行记录,如下例子中修改时间 2023-08-03 12:37:00 到 2023-08-03 12:37:05 这 5 秒内的数据如下

testdb=# SELECT * FROM conditions ORDER BY time DESC LIMIT 10;time              | location | device |    temperature     |       humidity
-------------------------------+----------+--------+--------------------+----------------------2023-08-03 12:37:00.345665+08 | 0002     | 00002  | 1.8149739913052656 |    0.6162655023691672023-08-03 12:37:00.345665+08 | 0003     | 00003  |  2.400422475569293 |   0.68700570944077912023-08-03 12:37:00.345665+08 | 0004     | 00004  |  2.639553072461581 |   1.94090348497051932023-08-03 12:37:00.345665+08 | 0005     | 00005  |  2.127623537273031 |   3.88715035379826552023-08-03 12:37:00.345665+08 | 0006     | 00006  | 2.3469833801156312 |    4.4115299335274262023-08-03 12:37:00.345665+08 | 0007     | 00007  | 1.0073460031664823 | 0.0168279977406164962023-08-03 12:37:00.345665+08 | 0008     | 00008  |  7.023014897212306 |   0.86792935440220732023-08-03 12:37:00.345665+08 | 0009     | 00009  |  6.744935559863428 |    6.3129489812979682023-08-03 12:37:00.345665+08 | 0010     | 00010  |  9.279208258323166 |    9.4515485435237782023-08-03 12:37:00.345665+08 | 0011     | 00011  |  1.994685462372594 |    7.361677356344085
(10 rows)testdb=# UPDATE conditions SET temperature = temperature + 0.1
WHERE time >= '2023-08-03 12:37:00' AND time < '2023-08-03 12:37:05';
UPDATE 10000
testdb=# SELECT * FROM conditions ORDER BY time DESC LIMIT 10;time              | location | device |    temperature     |       humidity
-------------------------------+----------+--------+--------------------+----------------------2023-08-03 12:37:00.345665+08 | 0002     | 00002  | 1.9149739913052657 |    0.6162655023691672023-08-03 12:37:00.345665+08 | 0003     | 00003  |  2.500422475569293 |   0.68700570944077912023-08-03 12:37:00.345665+08 | 0004     | 00004  |  2.739553072461581 |   1.94090348497051932023-08-03 12:37:00.345665+08 | 0005     | 00005  | 2.2276235372730313 |   3.88715035379826552023-08-03 12:37:00.345665+08 | 0006     | 00006  | 2.4469833801156313 |    4.4115299335274262023-08-03 12:37:00.345665+08 | 0007     | 00007  | 1.1073460031664824 | 0.0168279977406164962023-08-03 12:37:00.345665+08 | 0008     | 00008  |  7.123014897212306 |   0.86792935440220732023-08-03 12:37:00.345665+08 | 0009     | 00009  |  6.844935559863428 |    6.3129489812979682023-08-03 12:37:00.345665+08 | 0010     | 00010  |  9.379208258323166 |    9.4515485435237782023-08-03 12:37:00.345665+08 | 0011     | 00011  |  2.094685462372594 |    7.361677356344085
(10 rows)
  1. Upsert 插入新数据或更新已存在的数据

Upsert 只能在存在唯一索引或唯一约束的表中生效,可以使用 ALTER TABLE … ADD CONSTRAINT … UNIQUE 语句为已经存在的 hypertable 创建唯一约束

testdb=# ALTER TABLE conditionsADD CONSTRAINT conditions_time_locationUNIQUE (time, location);
ALTER TABLE
testdb=# \d conditionsTable "public.conditions"Column    |           Type           | Collation | Nullable | Default
-------------+--------------------------+-----------+----------+---------time        | timestamp with time zone |           | not null |location    | text                     |           | not null |device      | text                     |           | not null |temperature | double precision         |           |          |humidity    | double precision         |           |          |
Indexes:"conditions_time_location" UNIQUE CONSTRAINT, btree ("time", location)"conditions_time_idx" btree ("time")
Triggers:ts_insert_blocker BEFORE INSERT ON conditions FOR EACH ROW EXECUTE FUNCTION _timescaledb_internal.insert_blocker()
Number of child tables: 1 (Use \d+ to list them.)

插入新数据或更新已存在的数据使用 INSERT INTO … VALUES … ON CONFLICT … DO UPDATE 语句实现

testdb=# SELECT * FROM conditions
WHERE time = '2023-08-03 12:37:00.345665+08' AND location = '0001';time              | location | device | temperature | humidity
-------------------------------+----------+--------+-------------+----------2023-08-03 12:37:00.345665+08 | 0001     | 00001  |        70.3 |       50
(1 row)testdb=# INSERT INTO conditions
VALUES ('2023-08-03 12:37:00.345665+08', '0001', '00001', 70.2, 50.1)
ON CONFLICT (time, location) DO UPDATESET temperature = excluded.temperature,humidity = excluded.humidity;
INSERT 0 1
testdb=# SELECT * FROM conditions
WHERE time = '2023-08-03 12:37:00.345665+08' AND location = '0001';time              | location | device | temperature | humidity
-------------------------------+----------+--------+-------------+----------2023-08-03 12:37:00.345665+08 | 0001     | 00001  |        70.2 |     50.1
(1 row)

也可以只执行插入操作,如果记录已存在则直接跳过,该操作使用 INSERT INTO … VALUES … ON CONFLICT DO NOTHING 语句实现

testdb=# INSERT INTO conditionsVALUES (NOW(), 'new', '00001', 70.1, 50.0)ON CONFLICT DO NOTHING;
INSERT 0 1
testdb=# SELECT * FROM conditions WHERE location = 'new';time              | location | device | temperature | humidity
-------------------------------+----------+--------+-------------+----------2023-08-04 10:33:16.698018+08 | new      | 00001  |        70.1 |       50
(1 row)testdb=# INSERT INTO conditions
VALUES ('2023-08-04 10:33:16.698018+08', '0001', '00001', 71, 50.1)
ON CONFLICT DO NOTHING;
INSERT 0 1
testdb=# SELECT * FROM conditions WHERE location = 'new';time              | location | device | temperature | humidity
-------------------------------+----------+--------+-------------+----------2023-08-04 10:33:16.698018+08 | new      | 00001  |        70.1 |       50
(1 row)
  1. DELETE 删除数据

删除数据的语句与标准 SQL 一致 DELETE FROM …

testdb=# SELECT * FROM conditions WHERE location = 'new';time              | location | device | temperature | humidity
-------------------------------+----------+--------+-------------+----------2023-08-04 10:33:16.698018+08 | new      | 00001  |        70.1 |       50
(1 row)testdb=# DELETE FROM conditions WHERE location = 'new';
DELETE 1
testdb=# SELECT * FROM conditions WHERE location = 'new';time | location | device | temperature | humidity
------+----------+--------+-------------+----------
(0 rows)
  1. DROP 删除 hypertable 只需要使用 PG 命令删除基表(普通表)即可
testdb=# DROP TABLE conditions;
DROP TABLE

2.2 编辑超表 hypertable

hypertable 的修改和标准 SQL 一致,可以使用 ALTER TABLE 相关语句实现:http://www.postgres.cn/docs/12/ddl-alter.html

  1. 增删列

增加列 column 时,如果没有默认值即 NULL 时,新增操作可以很快完成;但是默认值为非空的时候,就需要花大量时间填充所有分区中每条记录新增列的值。

另外,无法对已经被压缩的 hypertable 进行增删列操作,如果要进行该操作需要先解压

testdb=# \d conditionsTable "public.conditions"Column    |           Type           | Collation | Nullable | Default
-------------+--------------------------+-----------+----------+---------time        | timestamp with time zone |           | not null |location    | text                     |           | not null |device      | text                     |           | not null |temperature | double precision         |           |          |humidity    | double precision         |           |          |
Indexes:"conditions_time_idx" btree ("time" DESC)
Triggers:ts_insert_blocker BEFORE INSERT ON conditions FOR EACH ROW EXECUTE FUNCTION _timescaledb_internal.insert_blocker()
Number of child tables: 1 (Use \d+ to list them.)testdb=# ALTER TABLE conditions
ADD COLUMN test_column DOUBLE PRECISION NULL;
ALTER TABLE
testdb=# \d conditionsTable "public.conditions"Column    |           Type           | Collation | Nullable | Default
-------------+--------------------------+-----------+----------+---------time        | timestamp with time zone |           | not null |location    | text                     |           | not null |device      | text                     |           | not null |temperature | double precision         |           |          |humidity    | double precision         |           |          |test_column | double precision         |           |          |
Indexes:"conditions_time_idx" btree ("time" DESC)
Triggers:ts_insert_blocker BEFORE INSERT ON conditions FOR EACH ROW EXECUTE FUNCTION _timescaledb_internal.insert_blocker()
Number of child tables: 1 (Use \d+ to list them.)testdb=# ALTER TABLE conditions
DROP COLUMN test_column;
ALTER TABLE
testdb=# \d conditionsTable "public.conditions"Column    |           Type           | Collation | Nullable | Default
-------------+--------------------------+-----------+----------+---------time        | timestamp with time zone |           | not null |location    | text                     |           | not null |device      | text                     |           | not null |temperature | double precision         |           |          |humidity    | double precision         |           |          |
Indexes:"conditions_time_idx" btree ("time" DESC)
Triggers:ts_insert_blocker BEFORE INSERT ON conditions FOR EACH ROW EXECUTE FUNCTION _timescaledb_internal.insert_blocker()
Number of child tables: 1 (Use \d+ to list them.)
  1. 重命名列或表
testdb=# \d conditionsTable "public.conditions"Column    |           Type           | Collation | Nullable | Default
-------------+--------------------------+-----------+----------+---------time        | timestamp with time zone |           | not null |location    | text                     |           | not null |device      | text                     |           | not null |temperature | double precision         |           |          |humidity    | double precision         |           |          |test_column | double precision         |           |          |
Indexes:"conditions_time_idx" btree ("time" DESC)
Triggers:ts_insert_blocker BEFORE INSERT ON conditions FOR EACH ROW EXECUTE FUNCTION _timescaledb_internal.insert_blocker()
Number of child tables: 1 (Use \d+ to list them.)testdb=# ALTER TABLE conditions RENAME COLUMN test_column TO ts_column;
ALTER TABLE
testdb=# ALTER TABLE conditions RENAME TO weather;
ALTER TABLE
testdb=# \d conditions
Did not find any relation named "conditions".
testdb=# \d weatherTable "public.weather"Column    |           Type           | Collation | Nullable | Default
-------------+--------------------------+-----------+----------+---------time        | timestamp with time zone |           | not null |location    | text                     |           | not null |device      | text                     |           | not null |temperature | double precision         |           |          |humidity    | double precision         |           |          |ts_column   | double precision         |           |          |
Indexes:"conditions_time_idx" btree ("time" DESC)
Triggers:ts_insert_blocker BEFORE INSERT ON weather FOR EACH ROW EXECUTE FUNCTION _timescaledb_internal.insert_blocker()
Number of child tables: 1 (Use \d+ to list them.)

2.3 修改 hypertable 分区间隔

  1. 普通表转化成 hypertable 时指定分区时间间隔 chunk_time_interval

如果创建 hypertable 是没有指定分区间隔,默认值是 7 天;可以通过查询 _timescaledb.catalog 查看分区间隔的当前设置

testdb=# CREATE TABLE conditions (time        TIMESTAMPTZ       NOT NULL,location    TEXT              NOT NULL,device      TEXT              NOT NULL,temperature DOUBLE PRECISION  NULL,humidity    DOUBLE PRECISION  NULL
);
CREATE TABLE
testdb=# SELECT create_hypertable('conditions', 'time');create_hypertable
-------------------------(2,public,conditions,t)
(1 row)testdb=# SELECT h.table_name, c.interval_lengthFROM _timescaledb_catalog.dimension cJOIN _timescaledb_catalog.hypertable hON h.id = c.hypertable_id;table_name | interval_length
------------+-----------------conditions |    604800000000
(1 row)

可以看到没有指定 chunk_time_interval 的 hypertable 分区间隔为 604800000000 / 1000 / 1000 / 60 / 60 / 24 = 7,interval_length 的单位为微秒(microsecond)

创建 hypertable 时我们通过 chunk_time_interval 指定分区间隔为 1 天

testdb=# DROP TABLE conditions;
DROP TABLE
testdb=# CREATE TABLE conditions (time        TIMESTAMPTZ       NOT NULL,location    TEXT              NOT NULL,device      TEXT              NOT NULL,temperature DOUBLE PRECISION  NULL,humidity    DOUBLE PRECISION  NULL
);
CREATE TABLE
testdb=# SELECT create_hypertable('conditions','time',chunk_time_interval => INTERVAL '1 day'
);create_hypertable
-------------------------(3,public,conditions,t)
(1 row)testdb=# SELECT h.table_name, c.interval_lengthFROM _timescaledb_catalog.dimension cJOIN _timescaledb_catalog.hypertable hON h.id = c.hypertable_id;table_name | interval_length
------------+-----------------conditions |     86400000000
(1 row)
  1. 通过 set_chunk_time_interval 函数修改 hypertable 分区间隔

需要注意的是修改分区时间间隔只会再新建的分区中生效,已经创建的分区不会受到影响。所以如果创建了一个很长时间的分区间隔例如 1 年,然后你想修改成一个更小的分区间隔,那这个更小间隔只会在一年后生效了,这时候只能新建一个表设置分区间隔,然后迁移数据了。

testdb=# SELECT set_chunk_time_interval('conditions', INTERVAL '12 hours');set_chunk_time_interval
-------------------------(1 row)testdb=# SELECT h.table_name, c.interval_lengthFROM _timescaledb_catalog.dimension cJOIN _timescaledb_catalog.hypertable hON h.id = c.hypertable_id;table_name | interval_length
------------+-----------------conditions |     43200000000
(1 row)

2.4 hypertable 创建索引

在一个 hypertable 中创建索引分为两步:

  1. 确定该超表的分区列有哪些,其中 time 列是所有 hypertable 表的分区列,所以创建索引必须包含该列;另外,在创建 hypertable 表时可以通过 partitioning_column 字段指定空间分区列。
  2. 创建的索引组合必须包括所有分区列,在此基础上增加其他列
  1. 创建索引

hypertable 中索引的创建仍是使用标准的 SQL 语句 CREATE INDEX / CREATE UNIQUE INDEX,例如,如下例子中在 conditions 超表的 time 和 device 列上建立索引 idx_device_time

testdb=# \d conditionsTable "public.conditions"Column    |           Type           | Collation | Nullable | Default
-------------+--------------------------+-----------+----------+---------time        | timestamp with time zone |           | not null |location    | text                     |           | not null |device      | text                     |           | not null |temperature | double precision         |           |          |humidity    | double precision         |           |          |
Indexes:"conditions_time_idx" btree ("time" DESC)
Triggers:ts_insert_blocker BEFORE INSERT ON conditions FOR EACH ROW EXECUTE FUNCTION _timescaledb_internal.insert_blocker()
Number of child tables: 1 (Use \d+ to list them.)testdb=# CREATE UNIQUE INDEX idx_device_time
ON conditions(device, time);
CREATE INDEX
testdb=# \d conditionsTable "public.conditions"Column    |           Type           | Collation | Nullable | Default
-------------+--------------------------+-----------+----------+---------time        | timestamp with time zone |           | not null |location    | text                     |           | not null |device      | text                     |           | not null |temperature | double precision         |           |          |humidity    | double precision         |           |          |
Indexes:"idx_device_time" UNIQUE, btree (device, "time")"conditions_time_idx" btree ("time" DESC)
Triggers:ts_insert_blocker BEFORE INSERT ON conditions FOR EACH ROW EXECUTE FUNCTION _timescaledb_internal.insert_blocker()
Number of child tables: 1 (Use \d+ to list them.)

idx_device_time 索引中包含了所有分区列 time,而 device 不是分区列,如果创建的索引组合不包含分区列,会抛出错误,如下 idx_device 索引仅建立在 device 列上

testdb=# CREATE UNIQUE INDEX idx_device
ON conditions(device);
ERROR:  cannot create a unique index without the column "time" (used in partitioning)
testdb=#
  1. 删除索引

hypertable 中删除索引仍是使用标准的 SQL 语句 DROP INDEX,删除 conditions 超表中的索引 idx_device_time

需要注意的是DBMS为主键约束和唯一约束自动创建的索引是无法删除的

testdb=# \d conditionsTable "public.conditions"Column    |           Type           | Collation | Nullable | Default
-------------+--------------------------+-----------+----------+---------time        | timestamp with time zone |           | not null |location    | text                     |           | not null |device      | text                     |           | not null |temperature | double precision         |           |          |humidity    | double precision         |           |          |
Indexes:"idx_device_time" UNIQUE, btree (device, "time")"conditions_time_idx" btree ("time" DESC)
Triggers:ts_insert_blocker BEFORE INSERT ON conditions FOR EACH ROW EXECUTE FUNCTION _timescaledb_internal.insert_blocker()
Number of child tables: 1 (Use \d+ to list them.)testdb=# DROP INDEX idx_device_time;
DROP INDEX
testdb=# \d conditionsTable "public.conditions"Column    |           Type           | Collation | Nullable | Default
-------------+--------------------------+-----------+----------+---------time        | timestamp with time zone |           | not null |location    | text                     |           | not null |device      | text                     |           | not null |temperature | double precision         |           |          |humidity    | double precision         |           |          |
Indexes:"conditions_time_idx" btree ("time" DESC)
Triggers:ts_insert_blocker BEFORE INSERT ON conditions FOR EACH ROW EXECUTE FUNCTION _timescaledb_internal.insert_blocker()
Number of child tables: 1 (Use \d+ to list them.)
  1. 已建索引的普通表转化为 hypertable,空间分区列 partitioning_column 必须包含在已建的索引中

例如,我们在普通表 basic_table 中已经在 device_id 和 time 列上建立了索引 idx_deviceid_time,那么创建以该表为基表的 hypertable 的分区列需要包含在索引列中,即分区列仅能为 time 与 device_id 的组合。

下例中,将已经创建好索引的 basic_table 表转化为仅以 time 为分区列的 hypertable

testdb=# CREATE TABLE basic_table(time TIMESTAMPTZ,user_id BIGINT,device_id BIGINT,value FLOAT
);
CREATE TABLE
testdb=# CREATE UNIQUE INDEX idx_deviceid_timeON basic_table(device_id, time);
CREATE INDEX
testdb=# \d basic_tableTable "public.basic_table"Column   |           Type           | Collation | Nullable | Default
-----------+--------------------------+-----------+----------+---------time      | timestamp with time zone |           |          |user_id   | bigint                   |           |          |device_id | bigint                   |           |          |value     | double precision         |           |          |
Indexes:"idx_deviceid_time" UNIQUE, btree (device_id, "time")testdb=# SELECT * from create_hypertable('basic_table', 'time');
NOTICE:  adding not-null constraint to column "time"
DETAIL:  Time dimensions cannot have NULL values.hypertable_id | schema_name | table_name  | created
---------------+-------------+-------------+---------4 | public      | basic_table | t
(1 row)testdb=# \d basic_tableTable "public.basic_table"Column   |           Type           | Collation | Nullable | Default
-----------+--------------------------+-----------+----------+---------time      | timestamp with time zone |           | not null |user_id   | bigint                   |           |          |device_id | bigint                   |           |          |value     | double precision         |           |          |
Indexes:"idx_deviceid_time" UNIQUE, btree (device_id, "time")"basic_table_time_idx" btree ("time" DESC)
Triggers:ts_insert_blocker BEFORE INSERT ON basic_table FOR EACH ROW EXECUTE FUNCTION _timescaledb_internal.insert_blocker()

也可以将已经创建好索引的 basic_table 表转化为以 time 和 device_id 为分区列的 hypertable

testdb=# SELECT * FROM create_hypertable('basic_table','time',partitioning_column => 'device_id',number_partitions => 4
);
NOTICE:  adding not-null constraint to column "time"
DETAIL:  Time dimensions cannot have NULL values.hypertable_id | schema_name | table_name  | created
---------------+-------------+-------------+---------5 | public      | basic_table | t
(1 row)testdb=# \d basic_tableTable "public.basic_table"Column   |           Type           | Collation | Nullable | Default
-----------+--------------------------+-----------+----------+---------time      | timestamp with time zone |           | not null |user_id   | bigint                   |           |          |device_id | bigint                   |           |          |value     | double precision         |           |          |
Indexes:"idx_deviceid_time" UNIQUE, btree (device_id, "time")"basic_table_time_idx" btree ("time" DESC)
Triggers:ts_insert_blocker BEFORE INSERT ON basic_table FOR EACH ROW EXECUTE FUNCTION _timescaledb_internal.insert_blocker()

但是,我们无法将不在索引组合中的 user_id 作为创建 hypertable 时的分区列

要解决这个问题,要确保在转化为超表之前建立的所有索引都要包含 hypertable 的分区列,所以将已存在的索引中加入 user_id 即可(重建索引可以重建一个索引,然后删除老的索引,避免使用 reindex 重建索引,因为该过程是阻塞的,一般大表不建议使用这个命令 )

testdb=# \d basic_tableTable "public.basic_table"Column   |           Type           | Collation | Nullable | Default
-----------+--------------------------+-----------+----------+---------time      | timestamp with time zone |           |          |user_id   | bigint                   |           |          |device_id | bigint                   |           |          |value     | double precision         |           |          |
Indexes:"idx_deviceid_time" UNIQUE, btree (device_id, "time")testdb=# SELECT * FROM create_hypertable('basic_table','time',partitioning_column => 'user_id',number_partitions => 4
);
NOTICE:  adding not-null constraint to column "time"
DETAIL:  Time dimensions cannot have NULL values.
ERROR:  cannot create a unique index without the column "user_id" (used in partitioning)
testdb=# DROP INDEX idx_deviceid_time;
DROP INDEX
testdb=# CREATE UNIQUE INDEX idx_userid_deviceid_timeON basic_table(user_id, device_id, time);
CREATE INDEX
testdb=# \d basic_tableTable "public.basic_table"Column   |           Type           | Collation | Nullable | Default
-----------+--------------------------+-----------+----------+---------time      | timestamp with time zone |           |          |user_id   | bigint                   |           |          |device_id | bigint                   |           |          |value     | double precision         |           |          |
Indexes:"idx_userid_deviceid_time" UNIQUE, btree (user_id, device_id, "time")testdb=# SELECT * FROM create_hypertable('basic_table','time',partitioning_column => 'user_id',number_partitions => 4
);
NOTICE:  adding not-null constraint to column "time"
DETAIL:  Time dimensions cannot have NULL values.hypertable_id | schema_name | table_name  | created
---------------+-------------+-------------+---------12 | public      | basic_table | t
(1 row)

如果文章对你有帮助,欢迎一键三连 👍 ⭐️ 💬 。如果还能够点击关注,那真的是对我最大的鼓励 🔥 🔥 🔥 。


参考资料

https://docs.timescale.com/self-hosted/latest/install/installation-source/
https://docs.timescale.com/use-timescale/latest/hypertables/create/
PolarDB 开源版 使用TimescaleDB 实现时序数据高速写入、压缩、实时聚合计算、自动老化等-阿里云开发者社区
https://zhuanlan.zhihu.com/p/57120174

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_550694.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

力扣:103. 二叉树的锯齿形层序遍历(Python3)

题目&#xff1a; 给你二叉树的根节点 root &#xff0c;返回其节点值的 锯齿形层序遍历 。&#xff08;即先从左往右&#xff0c;再从右往左进行下一层遍历&#xff0c;以此类推&#xff0c;层与层之间交替进行&#xff09;。 来源&#xff1a;力扣&#xff08;LeetCode&#…

Android StateFlow初探

Android StateFlow初探 前言&#xff1a; 最近在学习StateFlow&#xff0c;感觉很好用&#xff0c;也很神奇&#xff0c;于是记录了一下. 1.简介&#xff1a; StateFlow 是一个状态容器式可观察数据流&#xff0c;可以向其收集器发出当前状态更新和新状态更新。还可通过其 …

Go语言开发环境搭建指南:快速上手构建高效的Go开发环境

Go 官网&#xff1a;https://go.dev/dl/ Go 语言中文网&#xff1a;https://studygolang.com/dl 下载 Go 的语言包 进入官方网站 Go 官网 或 Go 语言中文网&#xff1a; 选择下载对应操作系统的安装包&#xff1a; 等待下载完成&#xff1a; 安装 Go 的语言包 双击运行上…

Linux 远程登录(Xshell7)

为什么需要远程登录Linux&#xff1f;因为通常在公司做开发的时候&#xff0c;Linux 一般作为服务器使用&#xff0c;而服务器一般放在机房&#xff0c;linux服务器是开发小组共享&#xff0c;且正式上线的项目是运行在公网&#xff0c;因此需要远程登录到Liux进行项日管理或者…

PgSQL-安全加固实践-如何设置非全零监听

PgSQL-安全加固实践-如何设置非全零监听 1、介绍 PgSQL在启动前需要配置listen_addresses配置项&#xff0c;该配置项表示允许PgSQL服务监听程序绑定的IP。我们知道一个host上可以有多个网卡&#xff0c;每个网卡可以绑定多个IP&#xff0c;该参数就是控制PgSQL服务绑定在哪个或…

容器的数据卷

容器的数据卷 操作数据卷 # 基本格式 docker volume [common] # 创建一个volume docker volume create # 显示一个或多个volume docker volume inspect # 列出所以的volume docker volume ls # 删除未使用的volume docker volume prune # 删除一个或多个volume docker volume…

C++笔记之引用折叠规则

C笔记之引用折叠规则 文章目录 C笔记之引用折叠规则1. 当两个左值引用结合在一起时&#xff0c;它们会折叠成一个左值引用。2. 当一个左值引用和一个右值引用结合在一起时&#xff0c;它们会折叠成一个左值引用。3. 当两个右值引用结合在一起时&#xff0c;它们也会折叠成一个右…

typeof的作用

typeof 是 JavaScript 中的一种运算符&#xff0c;用于获取给定值的数据类型。 它的作用是返回一个字符串&#xff0c;表示目标值的数据类型。通过使用 typeof 运算符&#xff0c;我们可以在运行时确定一个值的类型&#xff0c;从而进行相应的处理或逻辑判断。 常见的数据类型…

9月19日上课内容 zabbix监控系统与部署Zabbix5.0监控

本章结构 前言 监控软件的作用 作为一个运维&#xff0c;需要会使用监控系统查看服务器状态以及网站流量指标&#xff0c;利用监控系统的数据去了解上线发布的结果和网站的健康状态 利用一个优秀的监控软件&#xff0c;我们可以&#xff1a; ●通过一个友好的界面进行浏览整…

Renko图表发展史,一次说清楚

Renko图表的发展史&#xff0c;很多交易者都不清楚&#xff0c;今天FPmarkets澳福一次说清楚。 自从几百年前&#xff0c;大米交易商在记录他们产品每一次价格变动的同时&#xff0c;也可以节省时间、纸张和墨水&#xff0c;也是他们建立了一个系统&#xff0c;也就是Renko图。…

springboot线程池简单用法及配置解析

定义线程池 package com.example.demo.start.config;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.sche…

【深度学习框架格式转化】【CPU】Pytorch模型转ONNX模型格式流程详解【入门】

【深度学习框架格式转化】【GPU】Pytorch模型转ONNX模型格式流程详解【入门】 提示:博主取舍了很多大佬的博文并亲测有效,分享笔记邀大家共同学习讨论 文章目录 【深度学习框架格式转化】【GPU】Pytorch模型转ONNX模型格式流程详解【入门】前言PyTorch模型环境搭建(CPU)安装onn…

开源日报 0825 | 简化开发过程,提升Swift应用性能的扩展工具库

OpenZeppelin/openzeppelin-contracts Stars: 22.8k License: MIT OpenZeppelin Contracts 是一个用于安全智能合约开发的库。它建立在社区验证过的代码基础上&#xff0c;具有以下主要功能&#xff1a; 实现了 ERC20 和 ERC721 等标准。灵活的基于角色的权限控制方案。可重…

【Springboot】整合Xxl-job

目录 一&#xff0c;下载1.1 官方文档地址1.2 源码仓库地址 二&#xff0c; 配置部署 "调度中心"2.1 初始化 "调度数据库"2.2 修改配置文件2.3 启动调度中心 三&#xff0c;springboot项目整合3.1 添加依赖3.2 修改配置文件3.3 执行器组件配置4.4 编写测试…

spring的ThreadPoolTaskExecutor装饰器传递调用线程信息给线程池中的线程

概述 需求是想在线程池执行任务的时候&#xff0c;在开始前将调用线程的信息传到子线程中&#xff0c;在子线程完成后&#xff0c;再清除传入的数据。 下面使用了spring的ThreadPoolTaskExecutor来实现这个需求. ThreadPoolTaskExecutor 在jdk中使用的是ThreadPoolExecutor…

LabVIEW开发航天器模拟器的姿态控制和反作用轮动量管理

LabVIEW开发航天器模拟器的姿态控制和反作用轮动量管理 在过去十年中&#xff0c;航天器一直是现代技术进步的先决条件。迄今为止&#xff0c;为了更好地完成各种实际任务&#xff0c;已经在航天器姿态控制领域进行了大量研究。航天器一旦进入太空&#xff0c;就容易出现不确定…

Linux 编译安装中的 configure 命令

用了这么久的 Linux 系统&#xff0c;也许你会发现&#xff0c;在编译安装中&#xff0c;有的服务编译安装需要执行 configure 命令&#xff0c;而有的却不需要&#xff0c;这是为什么呢&#xff1f;也是不是像我一样一头雾水呢&#xff1f;其实这取决于服务的构建系统和配置方…

简单聊聊G1垃圾回收算法整个流程 --- 理论篇 -- 下

简单聊聊G1垃圾回收算法整个流程 --- 理论篇 -- 下 软实时性预测转移时间预测可信度GC 暂停处理的调度并发标记中的暂停处理 分代 G1 GC 模式不同点新生代区域分代对象转移具体转移流程分代选择回收集合设置最大新生代区域数 GC的切换GC执行的时机 总结 上一篇 文章我们简单看了…

linux 磁盘命令之du和df命令

du相关的命令: du -ah 显示所有目录或文件所占空间 du -KG 显示所有目录或文件所占空间 块大小K为单位 du -BM 显示所有目录或文件所占空间 块大小M为单位 du -BG 显示所有目录或文件所占空间 块大小G为单位du -sh [目录名] 返回该目录的大小 du -sm [文件夹] 返回该文…

记录一些奇怪的报错

错误&#xff1a;AttributeError: module distutils has no attribute version 解决方案&#xff1a; 第一步&#xff1a;pip uninstall setuptools 第二步&#xff1a;conda install setuptools58.0.4 错误&#xff1a;ModuleNotFoundError: No module named _distutils_hac…