[英]how can i create materialized view with psycopg2?
我在使用 postgres 物化視圖的 timescaledb 創建連續聚合時遇到錯誤:
connection = psycopg2.connect(DATABASE_URI)
cursor = connection.cursor()
cursor.execute(
"""CREATE MATERIALIZED VIEW quotes_1h WITH
(timescaledb.continuous)
AS
SELECT ticker, time_bucket('1h', time) as hour,
min(close) as low,
max(close) as high,
first(close, time) as open,
last(close, time) as close
FROM quotes
GROUP BY
ticker, time_bucket('1h', time);""")
connection.commit()
錯誤:psycopg2.errors.ActiveSqlTransaction: CREATE MATERIALIZED VIEW... WITH DATA 不能在事務塊內運行
我已經設置了自動提交,但它沒有幫助
修復它:
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
connection.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
TimescaleDB 目前不支持在同一事務中創建連續聚合並將其具體化。 因此有兩種選擇:
WITH NO DATA
並單獨或通過策略刷新來實現連續聚合。第二種情況將是:
cursor.execute(
"""CREATE MATERIALIZED VIEW quotes_1h WITH
(timescaledb.continuous)
AS
SELECT ticker, time_bucket('1h', time) as hour,
min(close) as low,
max(close) as high,
first(close, time) as open,
last(close, time) as close
FROM quotes
GROUP BY
ticker, time_bucket('1h', time)
WITH NO DATA;""")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.