-- -- one_c_planner_policy -- SET client_min_messages = warning; DROP SCHEMA IF EXISTS one_c_policy CASCADE; CREATE SCHEMA one_c_policy; SET search_path = one_c_policy, public; CREATE FUNCTION explain_root(query_text text) RETURNS jsonb LANGUAGE plpgsql AS $$ DECLARE plan_doc jsonb; BEGIN EXECUTE 'EXPLAIN (FORMAT JSON) ' || query_text INTO plan_doc; RETURN plan_doc -> 0 -> 'Plan'; END; $$; CREATE FUNCTION explain_plan_rows(query_text text) RETURNS numeric LANGUAGE plpgsql AS $$ BEGIN RETURN (one_c_policy.explain_root(query_text) ->> 'Plan Rows')::numeric; END; $$; CREATE FUNCTION explain_startup_cost(query_text text) RETURNS numeric LANGUAGE plpgsql AS $$ BEGIN RETURN (one_c_policy.explain_root(query_text) ->> 'Startup Cost')::numeric; END; $$; CREATE FUNCTION explain_total_cost(query_text text) RETURNS numeric LANGUAGE plpgsql AS $$ BEGIN RETURN (one_c_policy.explain_root(query_text) ->> 'Total Cost')::numeric; END; $$; CREATE FUNCTION explain_first_child_startup_cost(query_text text) RETURNS numeric LANGUAGE plpgsql AS $$ DECLARE plan_root jsonb; BEGIN plan_root := one_c_policy.explain_root(query_text); RETURN (plan_root -> 'Plans' -> 0 ->> 'Startup Cost')::numeric; END; $$; CREATE FUNCTION plan_contains(query_text text, wanted text) RETURNS boolean LANGUAGE plpgsql AS $$ BEGIN RETURN position(wanted IN one_c_policy.explain_root(query_text)::text) > 0; END; $$; CREATE FUNCTION assert_stat_attnums(index_oid oid, expected smallint[]) RETURNS void LANGUAGE plpgsql AS $$ DECLARE actual smallint[]; BEGIN SELECT COALESCE(array_agg(staattnum ORDER BY staattnum), ARRAY[]::smallint[]) INTO actual FROM pg_catalog.pg_statistic WHERE starelid = index_oid; IF actual IS DISTINCT FROM expected THEN RAISE EXCEPTION 'unexpected staattnum set: got %, expected %', actual, expected; END IF; END; $$; SET max_parallel_workers_per_gather = 0; CREATE TABLE one_c_policy.bool_t( flag boolean, a integer ) WITH (autovacuum_enabled = false); INSERT INTO one_c_policy.bool_t SELECT (g % 100) < 50, g % 100 FROM generate_series(1, 10000) AS g; ANALYZE one_c_policy.bool_t; DO $$ DECLARE actual_rows integer; baseline_rows numeric; BEGIN SELECT count(*) INTO actual_rows FROM one_c_policy.bool_t WHERE flag AND a = 42; IF actual_rows <> 100 THEN RAISE EXCEPTION 'unexpected bool_t actual rows: got %, expected 100', actual_rows; END IF; baseline_rows := one_c_policy.explain_plan_rows($sql$ SELECT * FROM one_c_policy.bool_t WHERE flag AND a = 42 $sql$); IF baseline_rows >= 75 THEN RAISE EXCEPTION 'baseline bool estimate too high: got %, expected < 75', baseline_rows; END IF; END; $$; CREATE INDEX bool_t_flag_a ON one_c_policy.bool_t(flag, a); ANALYZE one_c_policy.bool_t; DO $$ BEGIN PERFORM one_c_policy.assert_stat_attnums('one_c_policy.bool_t_flag_a'::regclass, ARRAY[1, 2]::smallint[]); END; $$; DO $$ DECLARE plan_rows numeric; BEGIN plan_rows := one_c_policy.explain_plan_rows($sql$ SELECT * FROM one_c_policy.bool_t WHERE flag AND a = 42 $sql$); IF plan_rows < 80 OR plan_rows > 120 THEN RAISE EXCEPTION 'flag=true estimate out of range: got %, expected 80..120', plan_rows; END IF; plan_rows := one_c_policy.explain_plan_rows($sql$ SELECT * FROM one_c_policy.bool_t WHERE a = 42 AND flag $sql$); IF plan_rows < 80 OR plan_rows > 120 THEN RAISE EXCEPTION 'reordered flag=true estimate out of range: got %, expected 80..120', plan_rows; END IF; plan_rows := one_c_policy.explain_plan_rows($sql$ SELECT * FROM one_c_policy.bool_t WHERE NOT flag AND a = 84 $sql$); IF plan_rows < 80 OR plan_rows > 120 THEN RAISE EXCEPTION 'flag=false estimate out of range: got %, expected 80..120', plan_rows; END IF; END; $$; CREATE TABLE one_c_policy.self_t( a integer, b integer ) WITH (autovacuum_enabled = false); INSERT INTO one_c_policy.self_t SELECT g % 100, g % 100 FROM generate_series(1, 10000) AS g; ANALYZE one_c_policy.self_t; DO $$ DECLARE actual_rows bigint; plan_rows numeric; BEGIN SELECT count(*) INTO actual_rows FROM one_c_policy.self_t l JOIN one_c_policy.self_t r ON l.a = r.a AND l.b = r.b; IF actual_rows <> 1000000 THEN RAISE EXCEPTION 'unexpected exact self-join count: got %, expected 1000000', actual_rows; END IF; plan_rows := one_c_policy.explain_plan_rows($sql$ SELECT l.a, l.b FROM one_c_policy.self_t l JOIN one_c_policy.self_t r ON l.a = r.a AND l.b = r.b $sql$); IF plan_rows < 800000 OR plan_rows > 1200000 THEN RAISE EXCEPTION 'exact self-join estimate out of range: got %, expected 800000..1200000', plan_rows; END IF; END; $$; CREATE TABLE one_c_policy.self_other( a integer, b integer ) WITH (autovacuum_enabled = false); INSERT INTO one_c_policy.self_other SELECT g % 100, g % 100 FROM generate_series(1, 10000) AS g; ANALYZE one_c_policy.self_other; DO $$ DECLARE plan_rows numeric; BEGIN plan_rows := one_c_policy.explain_plan_rows($sql$ SELECT l.a, l.b FROM one_c_policy.self_t l JOIN one_c_policy.self_other r ON l.a = r.a AND l.b = r.b $sql$); IF plan_rows >= 50000 THEN RAISE EXCEPTION 'separate-table estimate too high: got %, expected < 50000', plan_rows; END IF; plan_rows := one_c_policy.explain_plan_rows($sql$ SELECT l.a, l.b FROM one_c_policy.self_t l JOIN one_c_policy.self_t r ON l.a = r.b AND l.b = r.a $sql$); IF plan_rows >= 50000 THEN RAISE EXCEPTION 'cross-column self-join estimate too high: got %, expected < 50000', plan_rows; END IF; END; $$; CREATE TABLE one_c_policy.tiny_est( a integer, b integer ) WITH (autovacuum_enabled = false); INSERT INTO one_c_policy.tiny_est SELECT g, g FROM generate_series(1, 100000) AS g; ANALYZE one_c_policy.tiny_est; CREATE FUNCTION one_c_policy.costly_identity(integer) RETURNS integer LANGUAGE plpgsql VOLATILE STRICT COST 100000 AS $$ BEGIN RETURN $1; END $$; DO $$ DECLARE cheap_total numeric; expensive_total numeric; plan_rows numeric; delta numeric; BEGIN plan_rows := one_c_policy.explain_plan_rows($sql$ SELECT a FROM one_c_policy.tiny_est WHERE a = -1 AND b = -2 $sql$); IF plan_rows <> 1 THEN RAISE EXCEPTION 'tiny_est seqscan plan rows mismatch: got %, expected 1', plan_rows; END IF; cheap_total := one_c_policy.explain_total_cost($sql$ SELECT a FROM one_c_policy.tiny_est WHERE a = -1 AND b = -2 $sql$); expensive_total := one_c_policy.explain_total_cost($sql$ SELECT one_c_policy.costly_identity(a) FROM one_c_policy.tiny_est WHERE a = -1 AND b = -2 $sql$); delta := expensive_total - cheap_total; IF expensive_total <= cheap_total THEN RAISE EXCEPTION 'seqscan expensive total must exceed cheap total'; END IF; IF delta <= 0.01 OR delta >= 5 THEN RAISE EXCEPTION 'seqscan total-cost delta out of range: got %, expected > 0.01 and < 5', delta; END IF; END; $$; CREATE INDEX tiny_est_a_idx ON one_c_policy.tiny_est(a); SET enable_seqscan = off; SET enable_indexscan = off; SET enable_indexonlyscan = off; SET enable_bitmapscan = on; DO $$ DECLARE cheap_total numeric; expensive_total numeric; delta numeric; BEGIN IF NOT one_c_policy.plan_contains($sql$ SELECT a FROM one_c_policy.tiny_est WHERE a = -1 AND b = -2 $sql$, '"Node Type": "Bitmap Heap Scan"') THEN RAISE EXCEPTION 'bitmap test did not choose Bitmap Heap Scan'; END IF; cheap_total := one_c_policy.explain_total_cost($sql$ SELECT a FROM one_c_policy.tiny_est WHERE a = -1 AND b = -2 $sql$); expensive_total := one_c_policy.explain_total_cost($sql$ SELECT one_c_policy.costly_identity(a) FROM one_c_policy.tiny_est WHERE a = -1 AND b = -2 $sql$); delta := expensive_total - cheap_total; IF expensive_total <= cheap_total THEN RAISE EXCEPTION 'bitmap expensive total must exceed cheap total'; END IF; IF delta <= 0.01 OR delta >= 5 THEN RAISE EXCEPTION 'bitmap total-cost delta out of range: got %, expected > 0.01 and < 5', delta; END IF; END; $$; RESET enable_seqscan; RESET enable_indexscan; RESET enable_indexonlyscan; RESET enable_bitmapscan; CREATE TABLE one_c_policy.co_t( a integer, b integer, c integer, d integer, e integer, f integer ) WITH (autovacuum_enabled = false); INSERT INTO one_c_policy.co_t SELECT g, g, g, g, g, g FROM generate_series(1, 10000) AS g; ANALYZE one_c_policy.co_t; DO $$ DECLARE short_total numeric; long_total numeric; BEGIN short_total := one_c_policy.explain_total_cost($sql$ SELECT * FROM one_c_policy.co_t WHERE COALESCE(a, b) = -1 $sql$); long_total := one_c_policy.explain_total_cost($sql$ SELECT * FROM one_c_policy.co_t WHERE COALESCE(a, b, c, d, e, f) = -1 $sql$); IF long_total <= short_total + 50 THEN RAISE EXCEPTION 'long COALESCE total cost did not increase enough'; END IF; END; $$; CREATE INDEX co_t_a_idx ON one_c_policy.co_t(a); SET plan_cache_mode = force_generic_plan; SET enable_seqscan = off; PREPARE co_idx_2(integer, integer) AS SELECT * FROM one_c_policy.co_t WHERE a = COALESCE($1, $2); PREPARE co_idx_6(integer, integer, integer, integer, integer, integer) AS SELECT * FROM one_c_policy.co_t WHERE a = COALESCE($1, $2, $3, $4, $5, $6); DO $$ DECLARE startup_2 numeric; startup_6 numeric; diff numeric; BEGIN IF NOT one_c_policy.plan_contains( 'EXECUTE co_idx_2(1, 2)', '"Node Type": "Index Scan"') THEN RAISE EXCEPTION 'co_idx_2 did not choose Index Scan'; END IF; IF NOT one_c_policy.plan_contains( 'EXECUTE co_idx_6(1, 2, 3, 4, 5, 6)', '"Node Type": "Index Scan"') THEN RAISE EXCEPTION 'co_idx_6 did not choose Index Scan'; END IF; startup_2 := one_c_policy.explain_startup_cost( 'EXECUTE co_idx_2(1, 2)'); startup_6 := one_c_policy.explain_startup_cost( 'EXECUTE co_idx_6(1, 2, 3, 4, 5, 6)'); diff := abs(startup_6 - startup_2); IF diff > 0.10 THEN RAISE EXCEPTION 'generic COALESCE index startup delta too large: got %, expected <= 0.10', diff; END IF; END; $$; DEALLOCATE co_idx_2; DEALLOCATE co_idx_6; RESET plan_cache_mode; RESET enable_seqscan; CREATE TABLE one_c_policy.left_l( id integer ) WITH (autovacuum_enabled = false); CREATE TABLE one_c_policy.left_r( id integer, v integer ) WITH (autovacuum_enabled = false); INSERT INTO one_c_policy.left_l SELECT g FROM generate_series(1, 1000) AS g; INSERT INTO one_c_policy.left_r SELECT g, 1 FROM generate_series(1, 10) AS g; ANALYZE one_c_policy.left_l; ANALYZE one_c_policy.left_r; DO $$ DECLARE actual_rows integer; plan_rows numeric; plan_text text; BEGIN SELECT count(*) INTO actual_rows FROM one_c_policy.left_l l LEFT JOIN one_c_policy.left_r r ON l.id = r.id WHERE COALESCE(r.v, 0) > 0; IF actual_rows <> 10 THEN RAISE EXCEPTION 'unexpected LEFT JOIN actual rows: got %, expected 10', actual_rows; END IF; plan_text := one_c_policy.explain_root($sql$ SELECT l.id, r.v FROM one_c_policy.left_l l LEFT JOIN one_c_policy.left_r r ON l.id = r.id WHERE COALESCE(r.v, 0) > 0 $sql$)::text; IF position('"Join Type": "Left"' IN plan_text) = 0 THEN RAISE EXCEPTION 'LEFT JOIN test-design blocker: %', plan_text; END IF; plan_rows := one_c_policy.explain_plan_rows($sql$ SELECT l.id, r.v FROM one_c_policy.left_l l LEFT JOIN one_c_policy.left_r r ON l.id = r.id WHERE COALESCE(r.v, 0) > 0 $sql$); IF plan_rows < 1 OR plan_rows > 20 THEN RAISE EXCEPTION 'LEFT JOIN estimate out of range: got %, expected 1..20', plan_rows; END IF; END; $$; CREATE TABLE one_c_policy.limit_t( a integer ) WITH (autovacuum_enabled = false); INSERT INTO one_c_policy.limit_t SELECT g FROM generate_series(1, 10000) AS g; ANALYZE one_c_policy.limit_t; CREATE FUNCTION one_c_policy.expensive_true(integer) RETURNS boolean LANGUAGE plpgsql VOLATILE STRICT COST 100000 AS $$ BEGIN RETURN true; END $$; DO $$ DECLARE limit_startup numeric; child_startup numeric; BEGIN IF NOT one_c_policy.plan_contains($sql$ SELECT * FROM one_c_policy.limit_t WHERE one_c_policy.expensive_true(a) LIMIT 1 $sql$, '"Node Type": "Limit"') THEN RAISE EXCEPTION 'LIMIT startup test did not produce Limit root'; END IF; limit_startup := one_c_policy.explain_startup_cost($sql$ SELECT * FROM one_c_policy.limit_t WHERE one_c_policy.expensive_true(a) LIMIT 1 $sql$); child_startup := one_c_policy.explain_first_child_startup_cost($sql$ SELECT * FROM one_c_policy.limit_t WHERE one_c_policy.expensive_true(a) LIMIT 1 $sql$); IF limit_startup <= child_startup + 1 THEN RAISE EXCEPTION 'LIMIT startup cost did not increase enough'; END IF; END; $$; CREATE TABLE one_c_policy.skew_t( x integer ) WITH (autovacuum_enabled = false); INSERT INTO one_c_policy.skew_t SELECT CASE WHEN g <= 9000 THEN 1 ELSE 2 END FROM generate_series(1, 10000) AS g; ALTER TABLE one_c_policy.skew_t ALTER COLUMN x SET STATISTICS 100; ANALYZE one_c_policy.skew_t; SET plan_cache_mode = force_generic_plan; PREPARE skew_q(integer) AS SELECT * FROM one_c_policy.skew_t WHERE x = $1; DO $$ DECLARE plan_rows numeric; BEGIN plan_rows := one_c_policy.explain_plan_rows('EXECUTE skew_q(1)'); IF plan_rows < 5600 OR plan_rows > 6400 THEN RAISE EXCEPTION 'skew generic-plan estimate out of range: got %, expected 5600..6400', plan_rows; END IF; END; $$; DEALLOCATE skew_q; RESET plan_cache_mode; RESET max_parallel_workers_per_gather; DROP SCHEMA one_c_policy CASCADE;