Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
-- DBMS_XPLAN.DISPLAY: план EXPLAIN PLAN, форматы BASIC, TYPICAL, ALL.
-- Пара к sql/oracle/dbms_xplan_display.sql. Expected совпадает со стендом Oracle 23.5.
-- В Actual нет текста плана, хеша и стоимости.
SET client_min_messages = notice;
DROP TABLE IF EXISTS rxp_t;
NOTICE:  table "rxp_t" does not exist, skipping
DROP TABLE IF EXISTS rxp_u;
NOTICE:  table "rxp_u" does not exist, skipping
CREATE TABLE rxp_t (id numeric);
INSERT INTO rxp_t VALUES (1);
CREATE TABLE rxp_u (id numeric);
INSERT INTO rxp_u VALUES (2);
DROP FUNCTION IF EXISTS rxp_hit(text, text, text, text, text);
NOTICE:  function rxp_hit(text,text,text,text,text) does not exist, skipping
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: has_table_access=YES';
    EXECUTE 'EXPLAIN PLAN SET STATEMENT_ID = ''rxp_stmt'' FOR SELECT * FROM rxp_t';
    v_hit := rxp_hit(NULL, NULL, 'BASIC', NULL, 'TABLE ACCESS');
    RAISE NOTICE 'Actual:   has_table_access=%', v_hit;
END $$;
NOTICE:  Expected: has_table_access=YES
NOTICE:  Actual:   has_table_access=YES
DO $$
 
DECLARE
    v_hit text;
BEGIN
    RAISE NOTICE 'Expected: has_cost=NO';
    EXECUTE 'EXPLAIN PLAN SET STATEMENT_ID = ''rxp_stmt'' FOR SELECT * FROM rxp_t';
    v_hit := rxp_hit('PLAN_TABLE', 'rxp_stmt', 'BASIC', NULL, 'COST');
    RAISE NOTICE 'Actual:   has_cost=%', v_hit;
END $$;
NOTICE:  Expected: has_cost=NO
NOTICE:  Actual:   has_cost=NO
DO $$
 
DECLARE
    v_hit text;
BEGIN
    RAISE NOTICE 'Expected: has_cost=YES';
    EXECUTE 'EXPLAIN PLAN SET STATEMENT_ID = ''rxp_stmt'' FOR SELECT * FROM rxp_t';
    v_hit := rxp_hit('PLAN_TABLE', 'rxp_stmt', 'TYPICAL', NULL, 'COST');
    RAISE NOTICE 'Actual:   has_cost=%', v_hit;
END $$;
NOTICE:  Expected: has_cost=YES
NOTICE:  Actual:   has_cost=YES
DO $$
 
DECLARE
    v_t text;
    v_u text;
BEGIN
    RAISE NOTICE 'Expected: stmt=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', 'rxp_stmt', 'BASIC', NULL, 'RXP_T');
    v_u := rxp_hit('PLAN_TABLE', 'rxp_stmt', 'BASIC', NULL, 'RXP_U');
    RAISE NOTICE 'Actual:   %',
        CASE WHEN v_t = 'YES' AND v_u = 'NO' THEN 'stmt=OK' ELSE 'stmt=BAD' END;
END $$;
NOTICE:  Expected: stmt=OK
NOTICE:  Actual:   stmt=OK
DO $$
 
DECLARE
    v_all text;
    v_typ text;
BEGIN
    RAISE NOTICE 'Expected: all=YES typical=NO';
    EXECUTE 'EXPLAIN PLAN SET STATEMENT_ID = ''rxp_stmt'' FOR SELECT * FROM rxp_t';
    v_all := rxp_hit('PLAN_TABLE', 'rxp_stmt', 'ALL', NULL, 'COLUMN PROJECTION');
    v_typ := rxp_hit('PLAN_TABLE', 'rxp_stmt', 'TYPICAL', NULL, 'COLUMN PROJECTION');
    RAISE NOTICE 'Actual:   all=% typical=%', v_all, v_typ;
END $$;
NOTICE:  Expected: all=YES typical=NO
NOTICE:  Actual:   all=YES typical=NO
DO $$
 
DECLARE
    v_rows text;
    v_cost text;
BEGIN
    RAISE NOTICE 'Expected: rows=YES cost=NO';
    EXECUTE 'EXPLAIN PLAN SET STATEMENT_ID = ''rxp_stmt'' FOR SELECT * FROM rxp_t';
    v_rows := rxp_hit('PLAN_TABLE', 'rxp_stmt', 'BASIC +ROWS', NULL, 'ROWS');
    v_cost := rxp_hit('PLAN_TABLE', 'rxp_stmt', 'BASIC +ROWS', NULL, 'COST');
    RAISE NOTICE 'Actual:   rows=% cost=%', v_rows, v_cost;
END $$;
NOTICE:  Expected: rows=YES cost=NO
NOTICE:  Actual:   rows=YES cost=NO
DO $$
 
DECLARE
    v_rows text;
    v_cost text;
BEGIN
    RAISE NOTICE 'Expected: rows=YES cost=NO';
    EXECUTE 'EXPLAIN PLAN SET STATEMENT_ID = ''rxp_stmt'' FOR SELECT * FROM rxp_t WHERE id = 1';
    v_rows := rxp_hit('PLAN_TABLE', 'rxp_stmt', 'TYPICAL -COST', NULL, 'ROWS');
    v_cost := rxp_hit('PLAN_TABLE', 'rxp_stmt', 'TYPICAL -COST', NULL, 'COST');
    RAISE NOTICE 'Actual:   rows=% cost=%', v_rows, v_cost;
END $$;
NOTICE:  Expected: rows=YES cost=NO
NOTICE:  Actual:   rows=YES cost=NO
DO $$
 
BEGIN
    DELETE FROM plan_table WHERE statement_id IN ('rxp_stmt', 'rxp_other');
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;