-- -- macavity: API surface -- points, status, arm/disarm/reset bookkeeping. -- -- No event may fire in this file: every event armed here uses an -- occurrence far beyond the number of hits the file produces. The -- default occurrence (1) and events that really fire are covered by -- macavity_faults.sql and macavity_events.sql. -- CREATE EXTENSION IF NOT EXISTS macavity; -- Every advertised point must be implemented; this list is the contract. SELECT point, description FROM macavity_points() ORDER BY point; point | description ----------------+------------------------------------------------------------------------------------ before_abort | during top-level transaction abort, not subtransaction rollback (XACT_EVENT_ABORT) before_commit | before transaction commit processing (XACT_EVENT_PRE_COMMIT) executor_end | after executor execution completes (ExecutorEnd_hook) executor_start | before executor execution begins (ExecutorStart_hook) (4 rows) -- Nothing created yet: the registry is empty. SELECT * FROM macavity_status(); event_id | point | action | occurrence | hits | remaining | state ----------+-------+--------+------------+------+-----------+------- (0 rows) -- Arming creates an event and returns its ID. The occurrence is remembered -- verbatim. SELECT macavity_arm('executor_start', 'error', 400); macavity_arm -------------- 1 (1 row) SELECT event_id, point, action, occurrence, state FROM macavity_status(); event_id | point | action | occurrence | state ----------+----------------+--------+------------+------- 1 | executor_start | error | 400 | armed (1 row) -- Arming again creates a second, independent event with the next ID. SELECT macavity_arm('executor_end', 'delay', 400); macavity_arm -------------- 2 (1 row) SELECT event_id, point, action, occurrence, state FROM macavity_status(); event_id | point | action | occurrence | state ----------+----------------+--------+------------+------- 1 | executor_start | error | 400 | armed 2 | executor_end | delay | 400 | armed (2 rows) -- The action-specific forms create events too, and continue the same ID -- sequence. SELECT macavity_arm_error('before_commit', 400); macavity_arm_error -------------------- 3 (1 row) SELECT macavity_arm_delay('before_abort', 400); macavity_arm_delay -------------------- 4 (1 row) SELECT macavity_arm_crash('executor_start', 400); macavity_arm_crash -------------------- 5 (1 row) SELECT event_id, point, action, occurrence, state FROM macavity_status(); event_id | point | action | occurrence | state ----------+----------------+--------+------------+------- 1 | executor_start | error | 400 | armed 2 | executor_end | delay | 400 | armed 3 | before_commit | error | 400 | armed 4 | before_abort | delay | 400 | armed 5 | executor_start | crash | 400 | armed (5 rows) -- Disarming everything leaves the events in the registry, disarmed ... SELECT macavity_disarm(); macavity_disarm ----------------- t (1 row) SELECT event_id, state FROM macavity_status(); event_id | state ----------+---------- 1 | disarmed 2 | disarmed 3 | disarmed 4 | disarmed 5 | disarmed (5 rows) -- ... and a second disarm has nothing left to change. SELECT macavity_disarm(); macavity_disarm ----------------- f (1 row) SELECT macavity_disarm(NULL); macavity_disarm ----------------- f (1 row) -- Reset forgets every event and restarts IDs at 1. SELECT macavity_reset(); macavity_reset ---------------- 5 (1 row) SELECT * FROM macavity_status(); event_id | point | action | occurrence | hits | remaining | state ----------+-------+--------+------------+------+-----------+------- (0 rows) SELECT macavity_reset(); macavity_reset ---------------- 0 (1 row) SELECT macavity_arm('executor_start', 'error', 400); macavity_arm -------------- 1 (1 row) SELECT macavity_reset(); macavity_reset ---------------- 1 (1 row) -- Arming does not count the arming statement itself. Arm executor_end at -- a high occurrence and watch the counter: right after arming hits is 0, -- because the arming statement's own ExecutorEnd was skipped. Every -- statement after it -- including each status query -- counts one hit, so -- the second status query reports 2. SELECT macavity_arm('executor_end', 'error', 500); macavity_arm -------------- 1 (1 row) SELECT hits, remaining FROM macavity_status(); hits | remaining ------+----------- 0 | 500 (1 row) SELECT 1 AS first_statement; first_statement ----------------- 1 (1 row) SELECT hits, remaining FROM macavity_status(); hits | remaining ------+----------- 2 | 498 (1 row) SELECT macavity_disarm(1); macavity_disarm ----------------- t (1 row) SELECT event_id, state FROM macavity_status(); event_id | state ----------+---------- 1 | disarmed (1 row) SELECT macavity_reset(); macavity_reset ---------------- 1 (1 row)