-- DBMS_XPLAN.DISPLAY: NULL-аргументы, свой стол планов, filter_preds. -- Пара к sql/oracle/dbms_xplan_display_bounds.sql. Expected совпадает со стендом Oracle 23.5. SET client_min_messages = notice; DROP TABLE IF EXISTS rxp_t; DROP TABLE IF EXISTS rxp_u; DROP TABLE IF EXISTS rxp_plan; CREATE TABLE rxp_t (id numeric); INSERT INTO rxp_t VALUES (1); CREATE TABLE rxp_u (id numeric); INSERT INTO rxp_u VALUES (2); CREATE TABLE rxp_plan ( statement_id text, plan_id numeric, timestamp timestamp, remarks text, operation text, options text, object_node text, object_owner text, object_name text, object_alias text, object_instance numeric, object_type text, optimizer text, search_columns numeric, id numeric, parent_id numeric, depth numeric, position numeric, cost numeric, cardinality numeric, bytes numeric, other_tag text, partition_start text, partition_stop text, partition_id numeric, other text, other_xml text, distribution text, cpu_cost numeric, io_cost numeric, temp_space numeric, access_predicates text, filter_predicates text, projection text, time numeric, qblock_name text ); DROP FUNCTION IF EXISTS rxp_hit(text, text, text, text, text); CREATE FUNCTION rxp_hit( p_table text, p_sid text, p_fmt text, p_filter text, p_needle text ) RETURNS text LANGUAGE plpgsql AS $$ DECLARE v_hit text := 'NO'; r record; BEGIN FOR r IN SELECT plan_table_output AS line FROM dbms_xplan_display(p_table, p_sid, p_fmt, p_filter) LOOP IF r.line IS NOT NULL AND position(upper(p_needle) IN upper(r.line)) > 0 THEN v_hit := 'YES'; END IF; END LOOP; RETURN v_hit; END $$; DO $$ DECLARE v_hit text; BEGIN RAISE NOTICE 'Expected: null_tab=YES'; EXECUTE 'EXPLAIN PLAN SET STATEMENT_ID = ''rxp_stmt'' FOR SELECT * FROM rxp_t'; v_hit := rxp_hit(NULL, 'rxp_stmt', 'BASIC', NULL, 'RXP_T'); RAISE NOTICE 'Actual: null_tab=%', v_hit; END $$; DO $$ DECLARE v_hit text; BEGIN RAISE NOTICE 'Expected: custom=YES'; EXECUTE 'EXPLAIN PLAN SET STATEMENT_ID = ''rxp_custom'' INTO rxp_plan FOR SELECT * FROM rxp_t'; v_hit := rxp_hit('rxp_plan', 'rxp_custom', 'BASIC', NULL, 'TABLE ACCESS'); RAISE NOTICE 'Actual: custom=%', v_hit; END $$; DO $$ DECLARE v_t text; v_u text; BEGIN RAISE NOTICE 'Expected: filter=OK'; EXECUTE 'EXPLAIN PLAN SET STATEMENT_ID = ''rxp_stmt'' FOR SELECT * FROM rxp_t'; EXECUTE 'EXPLAIN PLAN SET STATEMENT_ID = ''rxp_other'' FOR SELECT * FROM rxp_u'; v_t := rxp_hit('PLAN_TABLE', NULL, 'BASIC', 'statement_id = ''rxp_stmt''', 'RXP_T'); v_u := rxp_hit('PLAN_TABLE', NULL, 'BASIC', 'statement_id = ''rxp_stmt''', 'RXP_U'); RAISE NOTICE 'Actual: %', CASE WHEN v_t = 'YES' AND v_u = 'NO' THEN 'filter=OK' ELSE 'filter=BAD' END; END $$; DO $$ DECLARE v_hit text; BEGIN RAISE NOTICE 'Expected: null_fmt=TYPICAL'; EXECUTE 'EXPLAIN PLAN SET STATEMENT_ID = ''rxp_stmt'' FOR SELECT * FROM rxp_t WHERE id = 1'; v_hit := rxp_hit('PLAN_TABLE', 'rxp_stmt', NULL, NULL, 'COST'); RAISE NOTICE 'Actual: %', CASE v_hit WHEN 'YES' THEN 'null_fmt=TYPICAL' ELSE 'null_fmt=OTHER' END; END $$; DO $$ DECLARE v_hit text; BEGIN RAISE NOTICE 'Expected: empty_fmt=TYPICAL'; EXECUTE 'EXPLAIN PLAN SET STATEMENT_ID = ''rxp_stmt'' FOR SELECT * FROM rxp_t WHERE id = 1'; v_hit := rxp_hit('PLAN_TABLE', 'rxp_stmt', '', NULL, 'COST'); RAISE NOTICE 'Actual: %', CASE v_hit WHEN 'YES' THEN 'empty_fmt=TYPICAL' ELSE 'empty_fmt=OTHER' END; END $$; DO $$ BEGIN DELETE FROM plan_table WHERE statement_id IN ('rxp_stmt', 'rxp_other', 'rxp_custom'); EXCEPTION WHEN SQLSTATE '42P01' THEN NULL; END $$; DROP FUNCTION IF EXISTS rxp_hit(text, text, text, text, text); DROP TABLE IF EXISTS rxp_t; DROP TABLE IF EXISTS rxp_u; DROP TABLE IF EXISTS rxp_plan;