/
22-install-support.sql
216 lines (196 loc) · 7.52 KB
/
22-install-support.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
call mgr.set_role('support');
call mgr.revoke_all_from_public('schema', 'support');
call mgr.grant_priv( 'usage', 'schema', 'support', 'api');
call mgr.grant_priv( 'usage', 'schema', 'support', 'qa');
--------------------------------------------------------------------------------
-- "no information" is represented by the empty string.
create table support.incidents(
ticket serial primary key,
unit text not null,
returned_sqlstate text not null,
column_name text not null,
constraint_name text not null,
pg_datatype_name text not null,
message_text text not null,
table_name text not null,
schema_name text not null,
pg_exception_detail text not null,
pg_exception_hint text not null,
pg_exception_context text not null);
call mgr.revoke_all_from_public('table', 'support.incidents');
call mgr.grant_priv( 'delete', 'table', 'support.incidents', 'qa');
create type support.stacked_diagnostics as (
returned_sqlstate text,
column_name text,
constraint_name text,
pg_datatype_name text,
message_text text,
table_name text,
schema_name text,
pg_exception_detail text,
pg_exception_hint text,
pg_exception_context text);
call mgr.revoke_all_from_public('type', 'support.stacked_diagnostics');
call mgr.grant_priv( 'usage', 'type', 'support.stacked_diagnostics', 'code');
call mgr.grant_priv( 'usage', 'type', 'support.stacked_diagnostics', 'json');
create procedure support.insert_incident(
the_unit in text,
stacked_diagnostics in support.stacked_diagnostics,
ticket_no inout int)
security definer
set search_path = pg_catalog, pg_temp
language plpgsql
as $body$
begin
insert into support.incidents(
unit,
returned_sqlstate,
column_name,
constraint_name,
pg_datatype_name,
message_text,
table_name,
schema_name,
pg_exception_detail,
pg_exception_hint,
pg_exception_context)
values(
the_unit,
stacked_diagnostics.returned_sqlstate,
stacked_diagnostics.column_name,
stacked_diagnostics.constraint_name,
stacked_diagnostics.pg_datatype_name,
stacked_diagnostics.message_text,
stacked_diagnostics.table_name,
stacked_diagnostics.schema_name,
stacked_diagnostics.pg_exception_detail,
stacked_diagnostics.pg_exception_hint,
stacked_diagnostics.pg_exception_context)
returning ticket into ticket_no;
end;
$body$;
call mgr.revoke_all_from_public('procedure', 'support.insert_incident(text, support.stacked_diagnostics, int)');
call mgr.grant_priv( 'execute', 'procedure', 'support.insert_incident(text, support.stacked_diagnostics, int)', 'api');
--------------------------------------------------------------------------------
create function support.incidents_report_worker(
i_ticket in int)
returns table(z text)
stable
security definer
set search_path = pg_catalog, pg_temp
language plpgsql
as $body$
declare
-- a Type is always partnered by an implicitly create type with the same name and shape.
r support.incidents;
pad constant int not null := 22;
tab constant text not null := rpad(' ', (pad + 2));
cr constant text not null :=
'
';
begin
select
ticket,
unit,
returned_sqlstate,
column_name,
constraint_name,
pg_datatype_name,
message_text,
table_name,
schema_name,
pg_exception_detail,
pg_exception_hint,
pg_exception_context
into r
from support.incidents
where ticket = i_ticket;
z := rpad('unit:', pad)||r.unit; return next;
z := rpad('returned_sqlstate:', pad)||r.returned_sqlstate; return next;
z := rpad('message_text:', pad)||r.message_text; return next;
if r.pg_exception_detail != '' then
declare
len int not null := length(r.pg_exception_detail);
cut int not null := 60;
d1 text not null := substr(r.pg_exception_detail, 1, cut);
d2 text not null := substr(r.pg_exception_detail, (cut + 1));
d text not null := case d2
when '' then d1
else d1||cr||tab||d2
end;
begin
z := rpad('pg_exception_detail:', pad)||d; return next;
end;
end if;
if r.pg_exception_hint != '' then
z := rpad('pg_exception_hint:', pad)||r.pg_exception_hint; return next;
end if;
if r.column_name != '' then
z := rpad('column_name:', pad)||r.column_name; return next;
end if;
if r.constraint_name != '' then
z := rpad('constraint_name:', pad)||r.constraint_name; return next;
end if;
if r.pg_datatype_name != '' then
z := rpad('pg_datatype_name:', pad)||r.pg_datatype_name; return next;
end if;
if r.table_name != '' then
z := rpad('table_name:', pad)||r.table_name; return next;
end if;
if r.schema_name != '' then
z := rpad('schema_name:', pad)||r.schema_name; return next;
end if;
z := ''; return next;
z := 'pg_exception_context'; return next;
z := '--------------------'; return next;
z := r.pg_exception_context; return next;
end;
$body$;
create function support.incidents_report(
tickets in int[] = null::int[])
returns table(line text)
stable
security definer
set search_path = pg_catalog, pg_temp
language plpgsql
as $body$
declare
nof_tickets constant int not null := (select count(*) from support.incidents);
mode constant boolean not null :=
(tickets is null) or (cardinality(tickets) < 1);
where_clause constant text :=
case mode
when true then null
else 'where ticket in (select unnest('''||tickets::text||'''::int[]))'
end;
tkt int not null := 0;
begin
case
when nof_tickets > 0 then
for
-- Notice the dynamic SQL to accommdate possible "where" clause.
tkt in execute format('
select ticket
from support.incidents
%s
order by ticket',
where_clause)
loop
line := ''; return next;
line := 'TICKET NO. '||tkt::text; return next;
line := ''; return next;
for line in (select z from support.incidents_report_worker(tkt)) loop
/* line is now populated */ return next;
end loop;
line := ''; return next;
line := rpad('_', 15, '_'); return next;
end loop;
else
line := ''; return next;
line := 'There are no incidents to report.'; return next;
line := rpad('_', 15, '_'); return next;
end case;
end;
$body$;
call mgr.revoke_all_from_public('function', 'support.incidents_report(int[])');
call mgr.grant_priv( 'execute', 'function', 'support.incidents_report(int[])', 'qa');