如何在TimescaleDB中的一个表上创建多个连续聚合?

时间:2019-06-20 06:23:44

标签: sql postgresql-11 timescaledb

我有这个series表及其超级表。我想对这张表中的数据进行不同的连续汇总。

CREATE TABLE series (
    time TIMESTAMPTZ PRIMARY KEY,
    value INTEGER
);

SELECT create_hypertable('series', 'time');

CREATE VIEW mat_view1
WITH (timescaledb.continuous) AS
SELECT time_bucket('1 day', time) AS day,
AVG(value)
FROM series
GROUP BY day;

CREATE VIEW mat_view2
WITH (timescaledb.continuous) AS
SELECT time_bucket('1 week', time) AS week,
COUNT(value)
FROM series
GROUP BY week;

但是看来在 PostgreSQL 11 中是不可能的-这是我在上面的查询中运行的结果:

ERROR:  hypertable already has a continuous aggregate
RECOMMENDATION:  hypertables currently only support a single continuous aggregate. Drop the other continuous aggreagate to add a new one.

甚至不可能在同一个表上创建另一个超表。

ERROR:  hypertable already has a continuous aggregate
RECOMMENDATION:  hypertables currently only support a single continuous aggregate. Drop the other continuous aggreagate to add a new one.

是否可以解决此限制?还是我应该使用另一种方法(例如,对于每个连续的集合:-x,都使用重复的series表)?

1 个答案:

答案 0 :(得分:1)

当前在时标中不支持。

我们计划在Timescale 1.4中使用此功能,该功能应在未来几周内发布。有关详细信息,请检查:https://github.com/timescale/timescaledb/pull/1257

对于您的特定用例,我现在唯一能建议的就是采用以下解决方法,您将创建1个连续聚合,其中包含您可能需要的所有详细信息,并为第二个用例创建一个常规视图:

CREATE VIEW mat_view1
WITH (timescaledb.continuous) AS
SELECT time_bucket('1 day', time) AS day,
AVG(value) AS avg,
COUNT(value) AS count
FROM series
GROUP BY day;

CREATE VIEW view2 AS
SELECT time_bucket('1 week', day) AS week,
SUM(count) AS count
FROM mat_view1
GROUP BY week;