How should I automate refreshing of my continuous aggregates every time I insert historical data

I have created multiple continous aggregates on my hypertable using timescaledb 2.15.3 extension in postgreSQL 15.

When inserting historical data to this hypertable, I have to manually refresh all continous aggregates by calling refresh_continuous_aggregate procedure.

Is there any way that automates refreshing of all continous aggregates immediatly after insertion of historical data ?

I have tried to create an after insert foreach statement trigger on my hypertable inside which I was refreshing all continous aggregates.

  1. Created one stored procedure which refresh all continous aggregates:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>CREATE OR REPLACE PROCEDURE sp_refresh_all_continuous_aggregates()
LANGUAGE plpgsql
AS $$
begin
CALL refresh_continuous_aggregate('three_minute_candles'::regclass, null::timestamptz, null::timestamptz);
CALL refresh_continuous_aggregate('five_minute_candles'::regclass, null::timestamptz, null::timestamptz);
CALL refresh_continuous_aggregate('ten_minute_candles'::regclass, null::timestamptz, null::timestamptz);
CALL refresh_continuous_aggregate('fifteen_minute_candles'::regclass, null::timestamptz, null::timestamptz);
CALL refresh_continuous_aggregate('thirty_minute_candles', null::timestamptz, null::timestamptz);
CALL refresh_continuous_aggregate('one_hour_candles'::regclass, null::timestamptz, null::timestamptz);
CALL refresh_continuous_aggregate('one_day_candles'::regclass, null::timestamptz, null::timestamptz);
CALL refresh_continuous_aggregate('one_month_candles'::regclass, null::timestamptz, null::timestamptz);
END;
$$;
</code>
<code>CREATE OR REPLACE PROCEDURE sp_refresh_all_continuous_aggregates() LANGUAGE plpgsql AS $$ begin CALL refresh_continuous_aggregate('three_minute_candles'::regclass, null::timestamptz, null::timestamptz); CALL refresh_continuous_aggregate('five_minute_candles'::regclass, null::timestamptz, null::timestamptz); CALL refresh_continuous_aggregate('ten_minute_candles'::regclass, null::timestamptz, null::timestamptz); CALL refresh_continuous_aggregate('fifteen_minute_candles'::regclass, null::timestamptz, null::timestamptz); CALL refresh_continuous_aggregate('thirty_minute_candles', null::timestamptz, null::timestamptz); CALL refresh_continuous_aggregate('one_hour_candles'::regclass, null::timestamptz, null::timestamptz); CALL refresh_continuous_aggregate('one_day_candles'::regclass, null::timestamptz, null::timestamptz); CALL refresh_continuous_aggregate('one_month_candles'::regclass, null::timestamptz, null::timestamptz); END; $$; </code>
CREATE OR REPLACE PROCEDURE sp_refresh_all_continuous_aggregates()
LANGUAGE plpgsql
AS $$
begin
    CALL refresh_continuous_aggregate('three_minute_candles'::regclass, null::timestamptz, null::timestamptz);
    CALL refresh_continuous_aggregate('five_minute_candles'::regclass, null::timestamptz, null::timestamptz);
    CALL refresh_continuous_aggregate('ten_minute_candles'::regclass, null::timestamptz, null::timestamptz);
    CALL refresh_continuous_aggregate('fifteen_minute_candles'::regclass, null::timestamptz, null::timestamptz);
    CALL refresh_continuous_aggregate('thirty_minute_candles', null::timestamptz, null::timestamptz);
    CALL refresh_continuous_aggregate('one_hour_candles'::regclass, null::timestamptz, null::timestamptz);
    CALL refresh_continuous_aggregate('one_day_candles'::regclass, null::timestamptz, null::timestamptz);
    CALL refresh_continuous_aggregate('one_month_candles'::regclass, null::timestamptz, null::timestamptz);
END;
$$;
  1. Created one trigger function which calls sp_refresh_all_continuous_aggregates:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>CREATE OR REPLACE FUNCTION fn_call_refresh_all_continuous_aggregate()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
CALL sp_refresh_all_continuous_aggregates();
return null;
END;
$$;
</code>
<code>CREATE OR REPLACE FUNCTION fn_call_refresh_all_continuous_aggregate() RETURNS TRIGGER LANGUAGE plpgsql AS $$ BEGIN CALL sp_refresh_all_continuous_aggregates(); return null; END; $$; </code>
CREATE OR REPLACE FUNCTION fn_call_refresh_all_continuous_aggregate()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
    CALL sp_refresh_all_continuous_aggregates();
    return null;
END;
$$;
  1. Finally created a trigger on my hypertable.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>CREATE TRIGGER refresh_all_continuous_aggregates
AFTER INSERT
ON candles
FOR EACH statement
execute function fn_call_refresh_all_continuous_aggregate();
</code>
<code>CREATE TRIGGER refresh_all_continuous_aggregates AFTER INSERT ON candles FOR EACH statement execute function fn_call_refresh_all_continuous_aggregate(); </code>
CREATE TRIGGER refresh_all_continuous_aggregates
AFTER INSERT
ON candles
FOR EACH statement
execute function fn_call_refresh_all_continuous_aggregate();

But when inserting data into candles hypertable I get this error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>SQL Error [XX000]: ERROR: portal snapshots (0) did not account for all active snapshots (1)
Where: SQL statement "CALL refresh_continuous_aggregate('three_minute_candles'::regclass, null::timestamptz, null::timestamptz)"
PL/pgSQL function sp_refresh_all_continuous_aggregates() line 3 at CALL
SQL statement "CALL sp_refresh_all_continuous_aggregates()"
PL/pgSQL function fn_call_refresh_all_continuous_aggregate() line 3 at CALL
</code>
<code>SQL Error [XX000]: ERROR: portal snapshots (0) did not account for all active snapshots (1) Where: SQL statement "CALL refresh_continuous_aggregate('three_minute_candles'::regclass, null::timestamptz, null::timestamptz)" PL/pgSQL function sp_refresh_all_continuous_aggregates() line 3 at CALL SQL statement "CALL sp_refresh_all_continuous_aggregates()" PL/pgSQL function fn_call_refresh_all_continuous_aggregate() line 3 at CALL </code>
SQL Error [XX000]: ERROR: portal snapshots (0) did not account for all active snapshots (1)
  Where: SQL statement "CALL refresh_continuous_aggregate('three_minute_candles'::regclass, null::timestamptz, null::timestamptz)"
PL/pgSQL function sp_refresh_all_continuous_aggregates() line 3 at CALL
SQL statement "CALL sp_refresh_all_continuous_aggregates()"
PL/pgSQL function fn_call_refresh_all_continuous_aggregate() line 3 at CALL

Also, If I don’t use this trigger and manually call sp_refresh_all_continuous_aggregates then I get this error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>SQL Error [42P01]: ERROR: relation "five_minute_candles" does not exist
Where: PL/pgSQL function sp_refresh_all_continuous_aggregates() line 4 at CALL
</code>
<code>SQL Error [42P01]: ERROR: relation "five_minute_candles" does not exist Where: PL/pgSQL function sp_refresh_all_continuous_aggregates() line 4 at CALL </code>
SQL Error [42P01]: ERROR: relation "five_minute_candles" does not exist
  Where: PL/pgSQL function sp_refresh_all_continuous_aggregates() line 4 at CALL

I have made sure that there are no spelling mistakes, Also this query works normally if I execute it outside of the stored procedure.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật