-
Notifications
You must be signed in to change notification settings - Fork 11
/
init.sql
55 lines (47 loc) · 1.5 KB
/
init.sql
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
/* contrib/sr_plan/init.sql */
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION sr_plan" to load this file. \quit
CREATE TABLE sr_plans (
query_hash int NOT NULL,
query_id int8 NOT NULL,
plan_hash int NOT NULL,
enable boolean NOT NULL,
query varchar NOT NULL,
plan text NOT NULL,
reloids oid[],
index_reloids oid[]
);
CREATE INDEX sr_plans_query_hash_idx ON sr_plans (query_hash);
CREATE INDEX sr_plans_query_oids ON sr_plans USING gin(reloids);
CREATE INDEX sr_plans_query_index_oids ON sr_plans USING gin(index_reloids);
CREATE FUNCTION _p(anyelement)
RETURNS anyelement
AS 'MODULE_PATHNAME', 'do_nothing'
LANGUAGE C STRICT VOLATILE;
CREATE FUNCTION show_plan(query_hash int4,
index int4 default null,
format cstring default null)
RETURNS SETOF RECORD
AS 'MODULE_PATHNAME', 'show_plan'
LANGUAGE C VOLATILE;
CREATE FUNCTION sr_plan_invalid_table() RETURNS event_trigger
LANGUAGE plpgsql AS $$
DECLARE
obj record;
indobj record;
BEGIN
FOR obj IN SELECT * FROM pg_event_trigger_dropped_objects()
WHERE object_type = 'table' OR object_type = 'index'
LOOP
IF obj.object_type = 'table' THEN
DELETE FROM @[email protected]_plans WHERE reloids @> ARRAY[obj.objid];
ELSE
IF obj.object_type = 'index' THEN
DELETE FROM @[email protected]_plans WHERE index_reloids @> ARRAY[obj.objid];
END IF;
END IF;
END LOOP;
END
$$;
CREATE EVENT TRIGGER sr_plan_invalid_table ON sql_drop
EXECUTE PROCEDURE sr_plan_invalid_table();