-- -- one_c_executor_guaranteed_empty -- SET client_min_messages = warning; DROP SCHEMA IF EXISTS one_c_executor CASCADE; CREATE SCHEMA one_c_executor; SET search_path = one_c_executor, public; CREATE FUNCTION explain_analyze_root(query_text text) RETURNS jsonb LANGUAGE plpgsql AS $$ DECLARE plan_doc jsonb; BEGIN EXECUTE 'EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF, FORMAT JSON) ' || query_text INTO plan_doc; RETURN plan_doc -> 0 -> 'Plan'; END; $$; CREATE FUNCTION find_plan_node(plan_doc jsonb, wanted_node_type text, wanted_relation_name text DEFAULT NULL) RETURNS jsonb LANGUAGE plpgsql AS $$ DECLARE child_plan jsonb; found_plan jsonb; BEGIN IF plan_doc IS NULL THEN RETURN NULL; END IF; IF plan_doc ->> 'Node Type' = wanted_node_type AND (wanted_relation_name IS NULL OR plan_doc ->> 'Relation Name' = wanted_relation_name) THEN RETURN plan_doc; END IF; IF jsonb_typeof(plan_doc -> 'Plans') = 'array' THEN FOR child_plan IN SELECT value FROM jsonb_array_elements(plan_doc -> 'Plans') LOOP found_plan := one_c_executor.find_plan_node(child_plan, wanted_node_type, wanted_relation_name); IF found_plan IS NOT NULL THEN RETURN found_plan; END IF; END LOOP; END IF; RETURN NULL; END; $$; SET max_parallel_workers_per_gather = 0; SET enable_hashjoin = off; SET enable_mergejoin = off; SET enable_nestloop = on; SET enable_material = on; SET jit = off; CREATE TABLE ge_outer(id integer) WITH (autovacuum_enabled = false); CREATE TABLE ge_inner(id integer) WITH (autovacuum_enabled = false); INSERT INTO ge_outer SELECT g FROM generate_series(1, 100000) AS g; INSERT INTO ge_inner SELECT g FROM generate_series(1, 1000) AS g; ANALYZE ge_outer; ANALYZE ge_inner; DELETE FROM ge_inner; DO $$ DECLARE actual_rows bigint; estimated_rows integer; query_text text; plan_root jsonb; nested_loop jsonb; materialize_node jsonb; outer_scan jsonb; BEGIN SELECT count(*) INTO actual_rows FROM one_c_executor.ge_inner; IF actual_rows <> 0 THEN RAISE EXCEPTION 'unexpected ge_inner rowcount: got %, expected 0', actual_rows; END IF; SELECT reltuples::integer INTO estimated_rows FROM pg_catalog.pg_class WHERE oid = 'one_c_executor.ge_inner'::regclass; IF estimated_rows <> 1000 THEN RAISE EXCEPTION 'unexpected ge_inner reltuples: got %, expected 1000', estimated_rows; END IF; query_text := $sql$ SELECT count(*) FROM one_c_executor.ge_outer o WHERE EXISTS ( SELECT 1 FROM one_c_executor.ge_inner i WHERE i.id = o.id ) $sql$; SELECT count(*) INTO actual_rows FROM one_c_executor.ge_outer o WHERE EXISTS ( SELECT 1 FROM one_c_executor.ge_inner i WHERE i.id = o.id ); IF actual_rows <> 0 THEN RAISE EXCEPTION 'unexpected semi-join result: got %, expected 0', actual_rows; END IF; plan_root := one_c_executor.explain_analyze_root(query_text); nested_loop := one_c_executor.find_plan_node(plan_root, 'Nested Loop'); materialize_node := one_c_executor.find_plan_node(plan_root, 'Materialize'); outer_scan := one_c_executor.find_plan_node(plan_root, 'Seq Scan', 'ge_outer'); IF nested_loop IS NULL THEN RAISE EXCEPTION 'Nested Loop node not found in semi-join plan'; END IF; IF materialize_node IS NULL THEN RAISE EXCEPTION 'Materialize node not found in semi-join plan'; END IF; IF (materialize_node ->> 'Actual Rows')::numeric <> 0 THEN RAISE EXCEPTION 'unexpected Materialize Actual Rows: got %, expected 0', materialize_node ->> 'Actual Rows'; END IF; IF outer_scan IS NULL THEN RAISE EXCEPTION 'Seq Scan on ge_outer not found in semi-join plan'; END IF; IF (outer_scan ->> 'Actual Rows')::numeric <> 1 THEN RAISE EXCEPTION 'unexpected ge_outer Actual Rows: got %, expected 1', outer_scan ->> 'Actual Rows'; END IF; END; $$; DO $$ DECLARE actual_rows bigint; BEGIN SELECT count(*) INTO actual_rows FROM one_c_executor.ge_outer o LEFT JOIN one_c_executor.ge_inner i ON i.id = o.id; IF actual_rows <> 100000 THEN RAISE EXCEPTION 'unexpected LEFT JOIN count: got %, expected 100000', actual_rows; END IF; SELECT count(*) INTO actual_rows FROM one_c_executor.ge_outer o WHERE NOT EXISTS ( SELECT 1 FROM one_c_executor.ge_inner i WHERE i.id = o.id ); IF actual_rows <> 100000 THEN RAISE EXCEPTION 'unexpected anti-semantics count: got %, expected 100000', actual_rows; END IF; END; $$; CREATE TABLE expr_outer(id integer) WITH (autovacuum_enabled = false); CREATE TABLE expr_a(id integer) WITH (autovacuum_enabled = false); CREATE TABLE expr_b(id integer) WITH (autovacuum_enabled = false); INSERT INTO expr_outer SELECT g FROM generate_series(1, 100000) AS g; INSERT INTO expr_a SELECT g FROM generate_series(1, 1000) AS g; INSERT INTO expr_b SELECT g FROM generate_series(1, 1000) AS g; ANALYZE expr_outer; ANALYZE expr_a; ANALYZE expr_b; DELETE FROM expr_a; DELETE FROM expr_b; DO $$ DECLARE actual_rows bigint; query_text text; plan_root jsonb; outer_scan jsonb; rows_removed numeric; BEGIN query_text := $sql$ SELECT count(*) FROM one_c_executor.expr_outer o WHERE ( o.id IN (SELECT a.id FROM one_c_executor.expr_a a) AND o.id > 0 ) OR ( o.id IN (SELECT b.id FROM one_c_executor.expr_b b) AND o.id > 0 ) $sql$; SELECT count(*) INTO actual_rows FROM one_c_executor.expr_outer o WHERE ( o.id IN (SELECT a.id FROM one_c_executor.expr_a a) AND o.id > 0 ) OR ( o.id IN (SELECT b.id FROM one_c_executor.expr_b b) AND o.id > 0 ); IF actual_rows <> 0 THEN RAISE EXCEPTION 'unexpected OR/SubPlan result: got %, expected 0', actual_rows; END IF; plan_root := one_c_executor.explain_analyze_root(query_text); outer_scan := one_c_executor.find_plan_node(plan_root, 'Seq Scan', 'expr_outer'); IF outer_scan IS NULL THEN RAISE EXCEPTION 'Seq Scan on expr_outer not found in OR/SubPlan plan'; END IF; IF (outer_scan ->> 'Actual Rows')::numeric <> 0 THEN RAISE EXCEPTION 'unexpected expr_outer Actual Rows: got %, expected 0', outer_scan ->> 'Actual Rows'; END IF; rows_removed := COALESCE((outer_scan ->> 'Rows Removed by Filter')::numeric, 0); IF rows_removed > 1 THEN RAISE EXCEPTION 'too many rows removed by filter: got %, expected <= 1', rows_removed; END IF; IF position('"Subplan Name"' IN plan_root::text) = 0 OR position('"Parent Relationship": "SubPlan"' IN plan_root::text) = 0 THEN RAISE EXCEPTION 'expected SubPlan metadata not found in OR/SubPlan plan'; END IF; END; $$; DO $$ DECLARE query_text text := $sql$ SELECT count(*) FROM one_c_executor.expr_outer o WHERE ( o.id IN (SELECT a.id FROM one_c_executor.expr_a a) AND o.id > 0 ) OR ( o.id IN (SELECT b.id FROM one_c_executor.expr_b b) AND o.id > 0 ) $sql$; actual_rows bigint; plan_root jsonb; outer_scan jsonb; rows_removed numeric; BEGIN IF pg_catalog.pg_jit_available() THEN PERFORM pg_catalog.set_config('jit', 'on', true); PERFORM pg_catalog.set_config('jit_above_cost', '0', true); SELECT count(*) INTO actual_rows FROM one_c_executor.expr_outer o WHERE ( o.id IN (SELECT a.id FROM one_c_executor.expr_a a) AND o.id > 0 ) OR ( o.id IN (SELECT b.id FROM one_c_executor.expr_b b) AND o.id > 0 ); IF actual_rows <> 0 THEN RAISE EXCEPTION 'unexpected JIT OR/SubPlan result: got %, expected 0', actual_rows; END IF; plan_root := one_c_executor.explain_analyze_root(query_text); outer_scan := one_c_executor.find_plan_node(plan_root, 'Seq Scan', 'expr_outer'); IF outer_scan IS NULL THEN RAISE EXCEPTION 'Seq Scan on expr_outer not found in JIT OR/SubPlan plan'; END IF; rows_removed := COALESCE((outer_scan ->> 'Rows Removed by Filter')::numeric, 0); IF rows_removed > 1 THEN RAISE EXCEPTION 'too many JIT rows removed by filter: got %, expected <= 1', rows_removed; END IF; END IF; END; $$; DO $$ DECLARE actual_rows bigint; query_text text; plan_root jsonb; outer_scan jsonb; BEGIN query_text := $sql$ SELECT count(*) FROM one_c_executor.expr_outer o WHERE o.id IN ( SELECT a.id FROM one_c_executor.expr_a a ) OR o.id < 0 $sql$; SELECT count(*) INTO actual_rows FROM one_c_executor.expr_outer o WHERE o.id IN ( SELECT a.id FROM one_c_executor.expr_a a ) OR o.id < 0; IF actual_rows <> 0 THEN RAISE EXCEPTION 'unexpected mixed OR result: got %, expected 0', actual_rows; END IF; plan_root := one_c_executor.explain_analyze_root(query_text); outer_scan := one_c_executor.find_plan_node(plan_root, 'Seq Scan', 'expr_outer'); IF outer_scan IS NULL THEN RAISE EXCEPTION 'Seq Scan on expr_outer not found in mixed OR plan'; END IF; IF (outer_scan ->> 'Actual Rows')::numeric <> 0 THEN RAISE EXCEPTION 'unexpected mixed OR Actual Rows: got %, expected 0', outer_scan ->> 'Actual Rows'; END IF; IF COALESCE((outer_scan ->> 'Rows Removed by Filter')::numeric, 0) <> 100000 THEN RAISE EXCEPTION 'unexpected mixed OR Rows Removed by Filter: got %, expected 100000', COALESCE(outer_scan ->> 'Rows Removed by Filter', 'NULL'); END IF; IF position('"Subplan Name"' IN plan_root::text) = 0 OR position('"Parent Relationship": "SubPlan"' IN plan_root::text) = 0 THEN RAISE EXCEPTION 'expected SubPlan metadata not found in mixed OR plan'; END IF; END; $$; DO $$ DECLARE actual_rows bigint; BEGIN SELECT count(*) INTO actual_rows FROM one_c_executor.expr_outer o WHERE NOT ( o.id IN ( SELECT a.id FROM one_c_executor.expr_a a ) ); IF actual_rows <> 100000 THEN RAISE EXCEPTION 'unexpected NOT/SubPlan result: got %, expected 100000', actual_rows; END IF; END; $$; DO $$ DECLARE actual_rows bigint; query_text text; plan_root jsonb; outer_scan jsonb; rows_removed numeric; BEGIN query_text := $sql$ SELECT count(*) FROM one_c_executor.expr_outer o WHERE ( o.id IN ( SELECT a.id FROM one_c_executor.expr_a a ) OR o.id IN ( SELECT b.id FROM one_c_executor.expr_b b ) ) AND o.id > 0 $sql$; SELECT count(*) INTO actual_rows FROM one_c_executor.expr_outer o WHERE ( o.id IN ( SELECT a.id FROM one_c_executor.expr_a a ) OR o.id IN ( SELECT b.id FROM one_c_executor.expr_b b ) ) AND o.id > 0; IF actual_rows <> 0 THEN RAISE EXCEPTION 'unexpected top-level AND result: got %, expected 0', actual_rows; END IF; plan_root := one_c_executor.explain_analyze_root(query_text); outer_scan := one_c_executor.find_plan_node(plan_root, 'Seq Scan', 'expr_outer'); IF outer_scan IS NULL THEN RAISE EXCEPTION 'Seq Scan on expr_outer not found in top-level AND plan'; END IF; IF (outer_scan ->> 'Actual Rows')::numeric <> 0 THEN RAISE EXCEPTION 'unexpected top-level AND Actual Rows: got %, expected 0', outer_scan ->> 'Actual Rows'; END IF; rows_removed := COALESCE((outer_scan ->> 'Rows Removed by Filter')::numeric, 0); IF rows_removed > 1 THEN RAISE EXCEPTION 'too many top-level AND rows removed by filter: got %, expected <= 1', rows_removed; END IF; IF position('"Subplan Name"' IN plan_root::text) = 0 OR position('"Parent Relationship": "SubPlan"' IN plan_root::text) = 0 THEN RAISE EXCEPTION 'expected SubPlan metadata not found in top-level AND plan'; END IF; END; $$; CREATE TABLE expr_nonempty(id integer) WITH (autovacuum_enabled = false); INSERT INTO expr_nonempty VALUES (100000); ANALYZE expr_nonempty; DO $$ DECLARE actual_rows bigint; query_text text; plan_root jsonb; BEGIN query_text := $sql$ SELECT count(*) FROM one_c_executor.expr_outer o WHERE o.id IN ( SELECT n.id FROM one_c_executor.expr_nonempty n ) OR o.id < 0 $sql$; SELECT count(*) INTO actual_rows FROM one_c_executor.expr_outer o WHERE o.id IN ( SELECT n.id FROM one_c_executor.expr_nonempty n ) OR o.id < 0; IF actual_rows <> 1 THEN RAISE EXCEPTION 'unexpected nonempty OR result: got %, expected 1', actual_rows; END IF; plan_root := one_c_executor.explain_analyze_root(query_text); IF position('"Subplan Name"' IN plan_root::text) = 0 OR position('"Parent Relationship": "SubPlan"' IN plan_root::text) = 0 THEN RAISE EXCEPTION 'expected SubPlan metadata not found in nonempty OR plan'; END IF; END; $$; RESET jit_above_cost; RESET jit; RESET enable_material; RESET enable_nestloop; RESET enable_mergejoin; RESET enable_hashjoin; RESET max_parallel_workers_per_gather; RESET search_path; DROP SCHEMA one_c_executor CASCADE;