-- Fleet-wide writers filter candidates through this VOLATILE predicate in -- the mutating statement so fenced tenants are skipped before row triggers run. CREATE FUNCTION community_write_allowed(target UUID) RETURNS BOOLEAN LANGUAGE plpgsql VOLATILE AS $$ DECLARE lifecycle TEXT; BEGIN IF current_setting('transaction_isolation') <> 'read committed' THEN RAISE EXCEPTION 'community writes require READ COMMITTED isolation' USING ERRCODE = 'invalid_transaction_state'; END IF; IF target IS NULL THEN RETURN true; END IF; PERFORM pg_advisory_xact_lock_shared(community_deletion_lock_key(target)); SELECT deletion_state INTO lifecycle FROM communities WHERE id = target; RETURN FOUND AND lifecycle = 'active'; END $$;