2026-04-19 19:18:58 +02:00
|
|
|
-- Alerting projections — additive and idempotent (IF NOT EXISTS).
|
|
|
|
|
-- Safe to run on every startup alongside init.sql.
|
|
|
|
|
--
|
2026-04-20 07:36:55 +02:00
|
|
|
-- executions uses ReplacingMergeTree. ClickHouse 24.x requires deduplicate_merge_projection_mode='rebuild'
|
|
|
|
|
-- for projections to work on ReplacingMergeTree. ALTER TABLE MODIFY SETTING persists the setting in
|
|
|
|
|
-- table metadata (survives restarts) and runs before the ADD PROJECTION statements.
|
|
|
|
|
-- logs and agent_metrics use plain MergeTree and do not need this setting.
|
2026-04-19 19:18:58 +02:00
|
|
|
--
|
2026-04-20 07:36:55 +02:00
|
|
|
-- MATERIALIZE statements are wrapped as non-fatal to handle empty tables in fresh deployments.
|
2026-04-19 19:18:58 +02:00
|
|
|
|
|
|
|
|
-- Plain MergeTree tables: always succeed
|
|
|
|
|
ALTER TABLE logs
|
|
|
|
|
ADD PROJECTION IF NOT EXISTS alerting_app_level
|
|
|
|
|
(SELECT * ORDER BY (tenant_id, environment, application, level, timestamp));
|
|
|
|
|
|
|
|
|
|
ALTER TABLE agent_metrics
|
|
|
|
|
ADD PROJECTION IF NOT EXISTS alerting_instance_metric
|
|
|
|
|
(SELECT * ORDER BY (tenant_id, environment, instance_id, metric_name, collected_at));
|
|
|
|
|
|
2026-04-20 07:36:55 +02:00
|
|
|
-- ReplacingMergeTree: set table-level setting so ADD PROJECTION succeeds on any connection
|
|
|
|
|
ALTER TABLE executions MODIFY SETTING deduplicate_merge_projection_mode = 'rebuild';
|
|
|
|
|
|
2026-04-19 19:18:58 +02:00
|
|
|
ALTER TABLE executions
|
|
|
|
|
ADD PROJECTION IF NOT EXISTS alerting_app_status
|
|
|
|
|
(SELECT * ORDER BY (tenant_id, environment, application_id, status, start_time));
|
|
|
|
|
|
|
|
|
|
ALTER TABLE executions
|
|
|
|
|
ADD PROJECTION IF NOT EXISTS alerting_route_status
|
|
|
|
|
(SELECT * ORDER BY (tenant_id, environment, route_id, status, start_time));
|
|
|
|
|
|
|
|
|
|
-- MATERIALIZE: best-effort on all tables (non-fatal if table is empty or already running)
|
|
|
|
|
ALTER TABLE logs MATERIALIZE PROJECTION alerting_app_level;
|
|
|
|
|
ALTER TABLE agent_metrics MATERIALIZE PROJECTION alerting_instance_metric;
|
|
|
|
|
ALTER TABLE executions MATERIALIZE PROJECTION alerting_app_status;
|
|
|
|
|
ALTER TABLE executions MATERIALIZE PROJECTION alerting_route_status;
|