-- +goose Up -- Regions appear when a checker for them first starts. Organization-private -- regions for customer-hosted checkers follow with the remote mode. CREATE TABLE regions ( key text PRIMARY KEY CHECK (key ~ '^[a-z0-9][a-z0-9-]{0,39}$'), name text NOT NULL, created_at timestamptz NOT NULL DEFAULT now() ); CREATE TABLE monitors ( id uuid PRIMARY KEY DEFAULT gen_random_uuid(), org_id uuid NOT NULL REFERENCES orgs (id) ON DELETE CASCADE, name text NOT NULL CHECK (length(name) BETWEEN 1 AND 200), type text NOT NULL CHECK (type IN ('http', 'tcp', 'icmp', 'dns', 'tls')), target text NOT NULL CHECK (length(target) BETWEEN 1 AND 2000), interval_seconds integer NOT NULL CHECK (interval_seconds BETWEEN 10 AND 86400), timeout_ms integer NOT NULL CHECK (timeout_ms BETWEEN 100 AND 60000), -- Type-specific settings, validated by the application. config jsonb NOT NULL DEFAULT '{}', enabled boolean NOT NULL DEFAULT true, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now() ); CREATE INDEX monitors_org_idx ON monitors (org_id); CREATE TABLE monitor_regions ( monitor_id uuid NOT NULL REFERENCES monitors (id) ON DELETE CASCADE, org_id uuid NOT NULL REFERENCES orgs (id) ON DELETE CASCADE, region text NOT NULL REFERENCES regions (key), PRIMARY KEY (monitor_id, region) ); ALTER TABLE monitors ENABLE ROW LEVEL SECURITY; ALTER TABLE monitors FORCE ROW LEVEL SECURITY; CREATE POLICY monitors_tenant ON monitors FOR ALL USING (app_scope_system() OR org_id = app_org_id()) WITH CHECK (app_scope_system() OR org_id = app_org_id()); ALTER TABLE monitor_regions ENABLE ROW LEVEL SECURITY; ALTER TABLE monitor_regions FORCE ROW LEVEL SECURITY; CREATE POLICY monitor_regions_tenant ON monitor_regions FOR ALL USING (app_scope_system() OR org_id = app_org_id()) WITH CHECK (app_scope_system() OR org_id = app_org_id()); -- check_results is deliberately not under row-level security: TimescaleDB -- refuses both compression and continuous aggregates on tables with row -- security. Tenant isolation for this table and its aggregates is enforced -- in the queries, which all take org_id as a parameter. CREATE TABLE check_results ( time timestamptz NOT NULL, org_id uuid NOT NULL, monitor_id uuid NOT NULL, region text NOT NULL, checker_id uuid NOT NULL, ok boolean NOT NULL, latency_ms integer, status_code integer, error text ); SELECT create_hypertable('check_results', by_range('time', INTERVAL '1 day')); CREATE INDEX check_results_monitor_idx ON check_results (monitor_id, time DESC); ALTER TABLE check_results SET ( timescaledb.compress, timescaledb.compress_segmentby = 'org_id, monitor_id, region', timescaledb.compress_orderby = 'time DESC' ); SELECT add_compression_policy('check_results', INTERVAL '7 days'); SELECT add_retention_policy('check_results', INTERVAL '90 days'); CREATE MATERIALIZED VIEW check_results_1h WITH (timescaledb.continuous) AS SELECT time_bucket(INTERVAL '1 hour', time) AS bucket, org_id, monitor_id, region, count(*) AS total, count(*) FILTER (WHERE ok) AS ok_count, avg(latency_ms)::real AS avg_latency_ms, max(latency_ms) AS max_latency_ms FROM check_results GROUP BY 1, 2, 3, 4 WITH NO DATA; -- Include the not yet materialized recent hours in queries. ALTER MATERIALIZED VIEW check_results_1h SET (timescaledb.materialized_only = false); SELECT add_continuous_aggregate_policy('check_results_1h', start_offset => INTERVAL '3 days', end_offset => INTERVAL '5 minutes', schedule_interval => INTERVAL '5 minutes'); CREATE MATERIALIZED VIEW check_results_1d WITH (timescaledb.continuous) AS SELECT time_bucket(INTERVAL '1 day', bucket) AS bucket, org_id, monitor_id, region, sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count, (sum(avg_latency_ms * total) / nullif(sum(total), 0))::real AS avg_latency_ms, max(max_latency_ms) AS max_latency_ms FROM check_results_1h GROUP BY 1, 2, 3, 4 WITH NO DATA; ALTER MATERIALIZED VIEW check_results_1d SET (timescaledb.materialized_only = false); SELECT add_continuous_aggregate_policy('check_results_1d', start_offset => INTERVAL '30 days', end_offset => INTERVAL '1 hour', schedule_interval => INTERVAL '1 hour'); -- Aggregates outlive the raw data. SELECT add_retention_policy('check_results_1h', INTERVAL '400 days'); -- +goose Down DROP MATERIALIZED VIEW check_results_1d; DROP MATERIALIZED VIEW check_results_1h; DROP TABLE check_results; DROP TABLE monitor_regions; DROP TABLE monitors; DROP TABLE regions;