-- Copyright 2026 Manuel Reyes Bravo -- SPDX-License-Identifier: Apache-2.0 -- pg_plan_guard 1.1.12 -> 1.2.0 -- -- 1. The comment on verify() says what it reports. It said "This is the capability -- pg_plan_advice does not provide", which is a claim about another module, shown in \df+. -- -- 2. A session advisory lock taken while planning is released without a warning. The -- release called pg_advisory_unlock() once more than the lock was held, to learn that it -- was gone, and that call warns "you don't own a lock": two warnings for each capture() -- of such a baseline. Now pg_locks says when the lock is gone. -- -- 3. A query with parameters ($1, $2, ...) can be captured, and is planned with EXPLAIN -- (GENERIC_PLAN), the plan a prepared statement uses once PostgreSQL settles on a generic -- one. Until 1.1.12 it had to be captured with literals, and a literal gives another -- query_id than $1: advice that sync_stash() stashed never reached an application that -- sends parameters. A query without them is planned as before, so no baseline drifts -- from this upgrade. \echo Use "ALTER EXTENSION pg_plan_guard UPDATE TO '1.2.0'" to load this file. \quit COMMENT ON FUNCTION plan_guard.verify(text) IS 'Re-plan every baseline and report it as ok, drifted or error.'; -- Session advisory locks survive the rollback of the subtransaction that took them. Release -- the ones this backend holds now and did not hold before the seal, as often as it holds -- them: pg_locks shows one row for a lock however many times it was taken. CREATE OR REPLACE FUNCTION plan_guard._release_advisory_locks(p_before text[]) RETURNS void LANGUAGE plpgsql SET search_path = pg_catalog, pg_temp AS $$ DECLARE r record; BEGIN FOR r IN SELECT l.classid::bigint AS c, l.objid::bigint AS o, l.objsubid, l.mode, l.classid || ':' || l.objid || ':' || l.objsubid || ':' || l.mode AS k FROM pg_locks l WHERE l.locktype = 'advisory' AND l.pid = pg_backend_pid() AND l.granted AND (l.classid || ':' || l.objid || ':' || l.objsubid || ':' || l.mode) <> ALL (coalesce(p_before, '{}')) LOOP LOOP IF r.objsubid = 1 THEN -- One bigint key: classid holds its high half, objid its low half. IF r.mode = 'ExclusiveLock' THEN PERFORM pg_advisory_unlock((r.c << 32) | r.o); ELSE PERFORM pg_advisory_unlock_shared((r.c << 32) | r.o); END IF; ELSE -- Two int4 keys, stored as oids. IF r.mode = 'ExclusiveLock' THEN PERFORM pg_advisory_unlock((r.c - CASE WHEN r.c > 2147483647 THEN 4294967296 ELSE 0 END)::int, (r.o - CASE WHEN r.o > 2147483647 THEN 4294967296 ELSE 0 END)::int); ELSE PERFORM pg_advisory_unlock_shared((r.c - CASE WHEN r.c > 2147483647 THEN 4294967296 ELSE 0 END)::int, (r.o - CASE WHEN r.o > 2147483647 THEN 4294967296 ELSE 0 END)::int); END IF; END IF; EXIT WHEN NOT EXISTS ( SELECT 1 FROM pg_locks l WHERE l.locktype = 'advisory' AND l.pid = pg_backend_pid() AND l.granted AND l.classid || ':' || l.objid || ':' || l.objsubid || ':' || l.mode = r.k); END LOOP; END LOOP; END; $$; -- Every EXPLAIN of stored text goes through here: read-only, as p_role when given -- in a frame -- that role owns -- and rolled back. A query PostgreSQL refuses for an undefined parameter is -- planned again with GENERIC_PLAN (1.2.0). CREATE OR REPLACE FUNCTION plan_guard._explain_lines( p_search_path text, p_options text, p_query_sql text, p_compute_query_id boolean DEFAULT false, p_role text DEFAULT NULL) RETURNS text[] LANGUAGE plpgsql SET search_path = pg_catalog, pg_temp AS $$ DECLARE v_lines text[]; v_line text; v_locks text[]; v_explain text; v_frame text; v_hand text; v_framed boolean := p_role IS NOT NULL AND plan_guard._needs_a_definer_frame(p_role); v_generic boolean := false; -- plpgsql variables survive the rollback of the block that set them: the plan is -- read here, and the rollback throws away only what planning did. answered boolean; BEGIN LOOP v_lines := '{}'; answered := false; v_explain := 'EXPLAIN (' || CASE WHEN v_generic THEN 'GENERIC_PLAN, ' ELSE '' END || p_options || ') ' || p_query_sql; v_frame := NULL; v_hand := NULL; IF v_framed THEN -- Built here, before the baseline's path applies: from then on an unqualified -- name in this function would resolve through the author's schemas (F-03). v_frame := format( 'CREATE FUNCTION pg_temp.plan_guard_sealed_explain() RETURNS pg_catalog.text[] ' 'LANGUAGE plpgsql SECURITY DEFINER SET search_path FROM CURRENT AS %L', format($body$DECLARE l pg_catalog.text[] := '{}'; x pg_catalog.text; BEGIN FOR x IN EXECUTE %L LOOP l := pg_catalog.array_append(l, x); END LOOP; RETURN l; END$body$, v_explain)); IF p_role IS DISTINCT FROM current_user::text THEN v_hand := format('ALTER FUNCTION pg_temp.plan_guard_sealed_explain() OWNER TO %I', p_role); END IF; END IF; SELECT array_agg(classid || ':' || objid || ':' || objsubid || ':' || mode) INTO v_locks FROM pg_locks WHERE locktype = 'advisory' AND pid = pg_backend_pid() AND granted; BEGIN IF p_compute_query_id THEN PERFORM set_config('compute_query_id', 'on', true); END IF; PERFORM set_config('search_path', coalesce(plan_guard._with_pg_temp_last(p_search_path), p_search_path), true); IF v_frame IS NOT NULL THEN EXECUTE v_frame; IF v_hand IS NOT NULL THEN EXECUTE v_hand; END IF; -- Read-only after the frame exists: creating it is the seal's own write. PERFORM pg_catalog.set_config('transaction_read_only', 'on', true); v_lines := pg_temp.plan_guard_sealed_explain(); ELSE PERFORM pg_catalog.set_config('transaction_read_only', 'on', true); FOR v_line IN EXECUTE v_explain LOOP v_lines := pg_catalog.array_append(v_lines, v_line); END LOOP; END IF; answered := true; RAISE EXCEPTION 'undo whatever planning did'; EXCEPTION WHEN OTHERS THEN PERFORM plan_guard._release_advisory_locks(v_locks); IF NOT answered THEN IF SQLSTATE = '42P02' AND NOT v_generic THEN -- "there is no parameter $1": the query has parameters. v_generic := true; ELSIF SQLSTATE = '42501' AND SQLERRM LIKE 'cannot set parameter %' THEN RAISE EXCEPTION 'the baseline tried to change the role it is planned as, and it is planned as %, the role that wrote it: %', p_role, SQLERRM USING ERRCODE = '42501'; ELSE RAISE; END IF; END IF; END; EXIT WHEN answered; END LOOP; RETURN v_lines; END; $$;