timescaledb/sql/views_experimental.sql
Erik Nordström b4710501dd Add experimental chunk replication view
A new view in the experimental schema shows information related to
chunk replication. The view can be used to learn the replication
status of a chunk while also providing a way to easily find nodes to
move or copy chunks between in order to ensure a fully replicated
multi-node cluster.

Tests have been added to illustrate the potential usage.
2021-07-29 16:53:12 +03:00

29 lines
1.4 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 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;
GRANT SELECT ON ALL TABLES IN SCHEMA timescaledb_experimental TO PUBLIC;