timescaledb/sql/views_experimental.sql
Fabrízio de Royes Mello 09565acae4 Fix timescaledb_experimental.policies duplicates
Commit 16fdb6ca5e introduced `timescaledb_experimental.policies` view
to expose the Continuous Aggregate policies but the current JOINS over
our catalog are not accurate.

Fixed it by properly JOIN the underlying catalog tables to expose the
correct information without duplicates about the Continuous Aggregate
policies.

Fixes #5492
2023-04-12 15:28:29 -03:00

42 lines
1.8 KiB
SQL

-- This file and its contents are licensed under the Apache License 2.0.
-- Please see the included NOTICE for copyright information and
-- LICENSE-APACHE for a copy of the license.
CREATE OR REPLACE VIEW timescaledb_experimental.chunk_replication_status AS
SELECT
h.schema_name AS hypertable_schema,
h.table_name AS hypertable_name,
c.schema_name AS chunk_schema,
c.table_name AS chunk_name,
h.replication_factor AS desired_num_replicas,
count(cdn.chunk_id) AS num_replicas,
array_agg(cdn.node_name) AS replica_nodes,
-- compute the set of data nodes that doesn't have the chunk
(SELECT array_agg(node_name) FROM
(SELECT node_name FROM _timescaledb_catalog.hypertable_data_node hdn
WHERE hdn.hypertable_id = h.id
EXCEPT
SELECT node_name FROM _timescaledb_catalog.chunk_data_node cdn
WHERE cdn.chunk_id = c.id
ORDER BY node_name) nodes) AS non_replica_nodes
FROM _timescaledb_catalog.chunk c
INNER JOIN _timescaledb_catalog.chunk_data_node cdn ON (cdn.chunk_id = c.id)
INNER JOIN _timescaledb_catalog.hypertable h ON (h.id = c.hypertable_id)
GROUP BY h.id, c.id, hypertable_schema, hypertable_name, chunk_schema, chunk_name
ORDER BY h.id, c.id, hypertable_schema, hypertable_name, chunk_schema, chunk_name;
CREATE OR REPLACE VIEW timescaledb_experimental.policies AS
SELECT ca.user_view_name AS relation_name,
ca.user_view_schema AS relation_schema,
j.schedule_interval,
j.proc_schema,
j.proc_name,
j.config,
ht.schema_name AS hypertable_schema,
ht.table_name AS hypertable_name
FROM _timescaledb_config.bgw_job j
JOIN _timescaledb_catalog.continuous_agg ca ON ca.mat_hypertable_id = j.hypertable_id
JOIN _timescaledb_catalog.hypertable ht ON ht.id = ca.mat_hypertable_id;
GRANT SELECT ON ALL TABLES IN SCHEMA timescaledb_experimental TO PUBLIC;