jonatas / timescaledb

The timescaledb gem. Pack of helpers to work with TimescaleDB extension in Ruby.
https://jonatas.github.io/timescaledb/
MIT License
58 stars 17 forks source link

Dump hierarchical continuous aggregates in the right order #70

Open jonatas opened 1 month ago

jonatas commented 1 month ago

I fall into a problem while implementing rubygems/rubygems.org#4642 which I have multiple views that are interdependent but the dump is in the wrong order.

Here we go with the error:

rake aborted!
   (3.3ms)  CREATE MATERIALIZED VIEW downloads_gems_per_month
WITH (
  timescaledb.continuous
  ,timescaledb.materialized_only=true

  ,timescaledb.finalized=true
) AS
    SELECT time_bucket('P1M'::interval, ts) AS ts,
      gem_name,
      count(*) AS downloads
     FROM downloads_gems_per_day
    GROUP BY (time_bucket('P1M'::interval, ts)), gem_name

WITH NO DATA;

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "downloads_gems_per_day" does not exist (ActiveRecord::StatementInvalid)
LINE 11:      FROM downloads_gems_per_day
                   ^
/Users/jonatasdp/code/rubygems.org/db/downloads_schema.rb:622:in `block in <main>'

Because the schema file is bringing _per_day before the _per_hour.

jonatas commented 1 month ago

Query example to get the views in the proper order:

WITH RECURSIVE caggs AS (
  SELECT mat_hypertable_id, parent_mat_hypertable_id, user_view_name
  FROM _timescaledb_catalog.continuous_agg
  WHERE user_view_name = 'metrics_by_week'
  UNION ALL
  SELECT continuous_agg.mat_hypertable_id, continuous_agg.parent_mat_hypertable_id, continuous_agg.user_view_name
  FROM _timescaledb_catalog.continuous_agg
  JOIN caggs ON caggs.parent_mat_hypertable_id = continuous_agg.mat_hypertable_id
)
SELECT * FROM caggs ORDER BY mat_hypertable_id;

From Timescaledb Community Slack.