-- M7.9b upgrade barrier (ADR-0068). A build can be retired from publication while -- remaining retained for recovery of the history it wrote: no new commit (published -- link, provisioned genesis or clone genesis) may name a retired build. The owner-only -- barrier retires a build and fences every current lease holder in the database in -- one transaction, so a paused or still-running old writer can never commit again -- and every region is resumed by a host that recovers from the committed head. ALTER TABLE evolution.retained_builds ADD COLUMN retired_at timestamptz; CREATE FUNCTION evolution.refuse_retired_build() RETURNS trigger LANGUAGE plpgsql SET search_path = pg_catalog, pg_temp AS $$ BEGIN IF EXISTS (SELECT 1 FROM evolution.retained_builds b WHERE b.build_identity = NEW.build_identity AND b.retired_at IS NOT NULL) THEN RAISE EXCEPTION 'build retired from publication' USING ERRCODE = 'EVL06'; END IF; RETURN NEW; END $$; CREATE TRIGGER region_commits_current_build BEFORE INSERT ON evolution.region_commits FOR EACH ROW EXECUTE FUNCTION evolution.refuse_retired_build(); -- Authority now names the build that will use it. The grant itself is the v2 -- acquisition body, internal and not executable by the runtime role. ALTER FUNCTION evolution.acquire_region_lease(evolution.u64, evolution.u64, integer) RENAME TO grant_region_lease; REVOKE ALL ON FUNCTION evolution.grant_region_lease(evolution.u64, evolution.u64, integer) FROM PUBLIC, :"runtime_role"; ALTER FUNCTION evolution.grant_region_lease(evolution.u64, evolution.u64, integer) SECURITY INVOKER; -- Build-aware acquisition (M7.9b hosts): only a retained, unretired build may hold -- authority, so a retired build can neither publish nor sit on an idle lease. CREATE FUNCTION evolution.acquire_region_lease(p_world evolution.u64, p_region evolution.u64, p_duration_ms integer, p_build bytea) RETURNS TABLE (lease_holder uuid, lease_epoch evolution.u64, region_frame evolution.u64, granted_ms integer) LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ BEGIN IF p_build IS NULL OR octet_length(p_build) <> 32 THEN RAISE EXCEPTION 'invalid lease request' USING ERRCODE = '22023'; END IF; IF NOT EXISTS (SELECT 1 FROM evolution.retained_builds b WHERE b.build_identity = p_build AND b.retired_at IS NULL) THEN RAISE EXCEPTION 'build not current' USING ERRCODE = 'EVL06'; END IF; RETURN QUERY SELECT * FROM evolution.grant_region_lease(p_world, p_region, p_duration_ms); END $$; -- Pre-v8 hosts name no build. They keep working until the first barrier, after -- which none of them can take authority again. CREATE FUNCTION evolution.acquire_region_lease(p_world evolution.u64, p_region evolution.u64, p_duration_ms integer) RETURNS TABLE (lease_holder uuid, lease_epoch evolution.u64, region_frame evolution.u64, granted_ms integer) LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ BEGIN IF EXISTS (SELECT 1 FROM evolution.retained_builds b WHERE b.retired_at IS NOT NULL) THEN RAISE EXCEPTION 'build-less acquisition ended by an upgrade barrier' USING ERRCODE = 'EVL06'; END IF; RETURN QUERY SELECT * FROM evolution.grant_region_lease(p_world, p_region, p_duration_ms); END $$; -- Returns the number of leases fenced. Retirement is permanent through this -- interface; at least one current build must remain, or nothing could publish. CREATE FUNCTION evolution.upgrade_barrier(p_retire bytea) RETURNS integer LANGUAGE plpgsql SECURITY INVOKER SET search_path = pg_catalog, pg_temp AS $$ DECLARE fenced integer; BEGIN IF p_retire IS NULL OR octet_length(p_retire) <> 32 THEN RAISE EXCEPTION 'invalid build identity' USING ERRCODE = '22023'; END IF; PERFORM 1 FROM evolution.retained_builds b WHERE b.build_identity = p_retire FOR UPDATE; IF NOT FOUND THEN RAISE EXCEPTION 'build not retained' USING ERRCODE = '22023'; END IF; IF NOT EXISTS (SELECT 1 FROM evolution.retained_builds b WHERE b.build_identity <> p_retire AND b.retired_at IS NULL) THEN RAISE EXCEPTION 'no current build would remain' USING ERRCODE = '22023'; END IF; UPDATE evolution.retained_builds b SET retired_at = coalesce(b.retired_at, clock_timestamp()) WHERE b.build_identity = p_retire; -- Holders keep their epochs; the next acquisition takes a fresh one. UPDATE evolution.region_leases l SET holder = NULL, expires_at = NULL WHERE l.holder IS NOT NULL; GET DIAGNOSTICS fenced = ROW_COUNT; RETURN fenced; END $$; REVOKE ALL ON FUNCTION evolution.refuse_retired_build(), evolution.upgrade_barrier(bytea), evolution.acquire_region_lease(evolution.u64, evolution.u64, integer, bytea), evolution.acquire_region_lease(evolution.u64, evolution.u64, integer) FROM PUBLIC; GRANT EXECUTE ON FUNCTION evolution.acquire_region_lease(evolution.u64, evolution.u64, integer, bytea), evolution.acquire_region_lease(evolution.u64, evolution.u64, integer) TO :"runtime_role"; INSERT INTO evolution.schema_migrations(version, sha256) VALUES (8, :'migration_sha_8');