drop table if exists test_table; CREATE TABLE test_table (a int); create or replace function doit(r int, a int) returns bool language plpgsql as $$ begin raise notice 'r = %, a = %', r, a; if (r = 10) then CREATE RULE make_noise AS ON DELETE TO test_table DO ALSO INSERT INTO test_table SELECT 2; raise notice 'made rule'; end if; if (r = 20 and a = 1) then CREATE RULE make_noise_2 AS ON DELETE TO test_table DO ALSO INSERT INTO test_table SELECT 3; raise notice 'made rule 2'; end if; return true; end$$; set plan_cache_mode to force_generic_plan; DO $$ BEGIN FOR r IN 1..30 LOOP TRUNCATE test_table; INSERT INTO test_table SELECT 1; DELETE FROM test_table where doit(r,a); END LOOP; END$$; table test_table;