-- pg_plan_guard regression tests -- -- The point of these tests is not that the functions run: it is that drift is -- actually DETECTED, and that the failure modes behave. A plan guard that -- reports "ok" no matter what is worse than none, because it is trusted. CREATE EXTENSION pg_plan_guard; CREATE SCHEMA guard_test; CREATE TABLE guard_test.t (id int, val text); INSERT INTO guard_test.t SELECT g, 'v' || g FROM generate_series(1, 20000) g; CREATE INDEX t_id_idx ON guard_test.t (id); ANALYZE guard_test.t; -- Capture the good plan (index scan on a selective lookup). SELECT plan_guard.capture( 'lookup', 'SELECT val FROM guard_test.t WHERE id = 42', 'point lookup that must use the index') LIKE '%INDEX_SCAN%' AS captured_index_scan; -- Nothing changed: must be ok. SELECT name, state FROM plan_guard.verify('lookup'); -- Take the index away from the planner. The query still returns the same row — -- this is the regression the extension exists to catch. SET enable_indexscan = off; SET enable_bitmapscan = off; SELECT name, state FROM plan_guard.verify('lookup'); -- The drift must be recorded with both sides of the change. SELECT baseline_name, expected_advice LIKE '%INDEX_SCAN%' AS expected_was_index, actual_advice LIKE '%SEQ_SCAN%' AS actual_is_seqscan FROM plan_guard.drift_log WHERE baseline_name = 'lookup'; -- Re-verifying while still drifted must NOT append another log row: a baseline -- that has been broken for a week should not produce one row per cron run. SELECT name, state FROM plan_guard.verify('lookup'); SELECT count(*) AS drift_rows FROM plan_guard.drift_log WHERE baseline_name = 'lookup'; -- Restore the planner: state must go back to ok on its own. RESET enable_indexscan; RESET enable_bitmapscan; SELECT name, state FROM plan_guard.verify('lookup'); -- The view a monitor would poll. SELECT name, state, drift_events FROM plan_guard.status WHERE name = 'lookup'; -- A query with parameters is captured as the application sends it, planned -- with a generic plan, and has the query_id of the same statement prepared. SELECT plan_guard.capture('lookup_param', 'SELECT val FROM guard_test.t WHERE id = $1') LIKE '%INDEX_SCAN%' AS captured_index_scan; SELECT name, state FROM plan_guard.verify('lookup_param'); SET compute_query_id = on; PREPARE lookup_param(int) AS SELECT val FROM guard_test.t WHERE id = $1; CREATE FUNCTION guard_test.prepared_query_id() RETURNS bigint LANGUAGE plpgsql AS $$ DECLARE l text; BEGIN FOR l IN EXECUTE 'EXPLAIN (VERBOSE, COSTS OFF) EXECUTE lookup_param(42)' LOOP IF l ~ 'Query Identifier:' THEN RETURN substring(l FROM '-?\d+$')::bigint; END IF; END LOOP; RETURN NULL; END $$; SELECT plan_guard.query_id_for('SELECT val FROM guard_test.t WHERE id = $1') = guard_test.prepared_query_id() AS same_query_id; DEALLOCATE lookup_param; RESET compute_query_id; -- A baseline whose query no longer plans must be reported as 'error' WITHOUT -- aborting the run: one broken baseline must not stop the others from being -- checked. A monitor that dies on the first problem stops working exactly when -- something is wrong. SELECT plan_guard.capture('broken', 'SELECT * FROM guard_test.t WHERE id = 1') IS NOT NULL AS ok; DROP TABLE guard_test.t CASCADE; SELECT name, state FROM plan_guard.verify() ORDER BY name; -- Capturing a query that cannot be planned raises an error and stores nothing. SELECT plan_guard.capture('nope', 'SELECT * FROM guard_test.does_not_exist'); -- A lock taken twice while planning is released, with no warning. CREATE FUNCTION guard_test.lock_twice() RETURNS int LANGUAGE sql IMMUTABLE AS 'SELECT 1 FROM (SELECT pg_advisory_lock(777), pg_advisory_lock(777)) s'; SELECT plan_guard.capture('locks', 'SELECT 1 WHERE guard_test.lock_twice() = 1') <> '' AS captured; SELECT count(*) AS advisory_locks_left FROM pg_locks WHERE locktype = 'advisory' AND pid = pg_backend_pid(); -- verify() is described by what it reports. SELECT obj_description('plan_guard.verify(text)'::regprocedure); DROP SCHEMA guard_test CASCADE; DROP EXTENSION pg_plan_guard CASCADE;