Skip to content

Worked Walkthrough

Start to finish: a check, a threshold, and notifications across trigger modes and delivery kinds.

-- 1. A check already exists with a threshold (built-in or your own).
--    Here, a built-in: PGHF11-009 (superuser login percentage), warn/crit
--    already seeded.

-- 2. Page on-call when it FIRST goes critical (on_change), via a function:
CREATE OR REPLACE FUNCTION my_schema.page_oncall(
    p_notification_log_id bigint, p_evaluation_id bigint, p_args jsonb
) RETURNS void
LANGUAGE plpgsql AS $$
DECLARE v RECORD;
BEGIN
    SELECT * INTO v FROM pghf.notification_context(p_evaluation_id);
    -- swap in your real paging integration; this just shows the shape
    PERFORM pg_notify('oncall_bridge', jsonb_build_object(
        'check', v.check_id, 'severity', v.severity, 'service', p_args ->> 'service'
    )::text);
    PERFORM pghf.mark_notification_delivered(p_notification_log_id, true, 'paged');
END;
$$;

SELECT pghf.create_notification('page oncall', 'function', p_check_id => 'PGHF11-009',
    p_trigger_mode => 'on_change', p_severities => ARRAY['critical'],
    p_routine => 'my_schema.page_oncall(bigint, bigint, jsonb)'::regprocedure,
    p_function_args => '{"service": "postgres-prod"}'::jsonb);

-- 3. A log-only heads-up on warning via the NOTIFY doorbell, staged inactive
--    until the listener is confirmed running:
SELECT pghf.create_notification('heads up', 'notify', p_check_id => 'PGHF11-009',
    p_min_severity => 'warning', p_channel_name => 'pghf_heads_up', p_is_active => false);

-- 4. Confirm the listener, then activate it:
SELECT pghf.update_notification(
    (SELECT notification_id FROM pghf.check_notifications WHERE notification_name = 'heads up'),
    'heads up', 'notify', true, p_min_severity => 'warning', p_channel_name => 'pghf_heads_up');

-- 5. A reminder while it STAYS critical (on_state), at most once every 4 hours:
SELECT pghf.create_notification('still critical', 'outbox', p_check_id => 'PGHF11-009',
    p_trigger_mode => 'on_state', p_severities => ARRAY['critical'], p_cooldown_minutes => 240);

-- 6. Run collection + evaluation as usual — notifications fire as part of this,
--    no separate step:
CALL pghf.run_and_evaluate(p_run_key => 'seeded-1h');

-- 7. Adjust without losing the registration, or remove outright:
SELECT pghf.update_notification(
    (SELECT notification_id FROM pghf.check_notifications WHERE notification_name = 'heads up'),
    'heads up', 'notify', false, p_min_severity => 'warning', p_channel_name => 'pghf_heads_up');  -- pause
SELECT pghf.delete_notification(
    (SELECT notification_id FROM pghf.check_notifications WHERE notification_name = 'heads up'));   -- remove

One thing this walkthrough never did is send anything a human actually receives — for that, and for wiring in your own delivery generically, continue to Sending Real Notifications with pg_relay_notifier.