Repository navigation
Expand file tree
/
Copy pathpg_net.sql
More file actions
358 lines (288 loc) · 10.1 KB
/
Copy pathpg_net.sql
File metadata and controls
358 lines (288 loc) · 10.1 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
CREATE SCHEMA net;
ALTER SCHEMA net OWNER TO postgres;
CREATE DOMAIN net.http_method AS text CONSTRAINT http_method_check CHECK (value ~~* CAST('get' AS text)
OR value ~~* CAST('post' AS text)
OR value ~~* CAST('delete' AS text));
ALTER DOMAIN net.http_method OWNER TO postgres;
CREATE TYPE net.http_response AS ( status_code integer
, headers jsonb
, body text);
ALTER TYPE net.http_response OWNER TO postgres;
CREATE TYPE net.request_status AS ENUM ('PENDING'
, 'SUCCESS'
, 'ERROR');
ALTER TYPE net.request_status OWNER TO postgres;
CREATE TYPE net.http_response_result AS ( status net.request_status
, message text
, response net.http_response);
ALTER TYPE net.http_response_result OWNER TO postgres;
CREATE FUNCTION net._await_response(request_id bigint)
RETURNS boolean
LANGUAGE plpgsql
AS $$
declare
rec net._http_response;
begin
while rec is null loop
select *
into rec
from net._http_response
where id = request_id;
if rec is null then
-- Wait 50 ms before checking again
perform pg_sleep(0.05);
end if;
end loop;
return true;
end;
$$;
ALTER FUNCTION net._await_response (request_id bigint) OWNER TO postgres;
CREATE FUNCTION net._encode_url_with_params_array(url text
, params_array text[])
RETURNS text
LANGUAGE c IMMUTABLE AS '$libdir/pg_net'
, '_encode_url_with_params_array';
ALTER FUNCTION net._encode_url_with_params_array (url text, params_array text[]) OWNER TO postgres;
CREATE FUNCTION net._http_collect_response(request_id bigint
, async boolean = TRUE)
RETURNS net.http_response_result
LANGUAGE plpgsql
AS $$
declare
rec net._http_response;
begin
if not async then
perform net._await_response(request_id);
end if;
select *
into rec
from net._http_response
where id = request_id;
if rec is null or rec.error_msg is not null then
-- The request is either still processing or the request_id provided does not exist
-- TODO: request in progress is indistinguishable from request that doesn't exist
-- No request matching request_id found
return (
'ERROR',
coalesce(rec.error_msg, 'request matching request_id not found'),
null
)::net.http_response_result;
end if;
-- Return a valid, populated http_response_result
return (
'SUCCESS',
'ok',
(
rec.status_code,
rec.headers,
rec.content
)::net.http_response
)::net.http_response_result;
end;
$$;
ALTER FUNCTION net._http_collect_response (request_id bigint, async boolean) OWNER TO postgres;
CREATE FUNCTION net._urlencode_string(string varchar)
RETURNS text
LANGUAGE c IMMUTABLE AS '$libdir/pg_net'
, '_urlencode_string';
ALTER FUNCTION net._urlencode_string (string varchar) OWNER TO postgres;
CREATE FUNCTION net.check_worker_is_up()
RETURNS void
LANGUAGE plpgsql
AS $$
begin
if not exists (select pid from pg_stat_activity where backend_type ilike '%pg_net%') then
raise exception using
message = 'the pg_net background worker is not up'
, detail = 'the pg_net background worker is down due to an internal error and cannot process requests'
, hint = 'make sure that you didn''t modify any of pg_net internal tables';
end if;
end
$$;
ALTER FUNCTION net.check_worker_is_up () OWNER TO postgres;
COMMENT ON FUNCTION net.check_worker_is_up ()
IS 'raises an exception if the pg_net background worker is not up, otherwise it doesn''t return anything';
CREATE FUNCTION net.http_collect_response(request_id bigint
, async boolean = TRUE)
RETURNS net.http_response_result
LANGUAGE plpgsql
AS $$
begin
raise notice 'The net.http_collect_response function is deprecated.';
return net._http_collect_response(request_id, async);
end;
$$;
ALTER FUNCTION net.http_collect_response (request_id bigint, async boolean) OWNER TO postgres;
CREATE FUNCTION net.http_delete(url text
, params jsonb = CAST('{}' AS jsonb)
, headers jsonb = CAST('{}' AS jsonb)
, timeout_milliseconds integer = 5000
, body jsonb = CAST(NULL AS jsonb))
RETURNS bigint
LANGUAGE plpgsql
AS $$
declare
request_id bigint;
params_array text[];
begin
select coalesce(array_agg(net._urlencode_string(key) || '=' || net._urlencode_string(value)), '{}')
into params_array
from jsonb_each_text(params);
-- Add to the request queue
insert into net.http_request_queue(method, url, headers, body, timeout_milliseconds)
values (
'DELETE',
net._encode_url_with_params_array(url, params_array),
headers,
convert_to(body::text, 'UTF8'),
timeout_milliseconds
)
returning id
into request_id;
perform net.wake();
return request_id;
end
$$;
ALTER FUNCTION net.http_delete (url text, params jsonb, headers jsonb, timeout_milliseconds integer, body jsonb) OWNER TO postgres;
CREATE FUNCTION net.http_get(url text
, params jsonb = CAST('{}' AS jsonb)
, headers jsonb = CAST('{}' AS jsonb)
, timeout_milliseconds integer = 5000)
RETURNS bigint
LANGUAGE plpgsql
AS $$
declare
request_id bigint;
params_array text[];
begin
select coalesce(array_agg(net._urlencode_string(key) || '=' || net._urlencode_string(value)), '{}')
into params_array
from jsonb_each_text(params);
-- Add to the request queue
insert into net.http_request_queue(method, url, headers, timeout_milliseconds)
values (
'GET',
net._encode_url_with_params_array(url, params_array),
headers,
timeout_milliseconds
)
returning id
into request_id;
perform net.wake();
return request_id;
end
$$;
ALTER FUNCTION net.http_get (url text, params jsonb, headers jsonb, timeout_milliseconds integer) OWNER TO postgres;
CREATE FUNCTION net.http_post(url text
, body jsonb = CAST('{}' AS jsonb)
, params jsonb = CAST('{}' AS jsonb)
, headers jsonb = CAST('{"Content-Type": "application/json"}' AS jsonb)
, timeout_milliseconds integer = 5000)
RETURNS bigint
LANGUAGE plpgsql
AS $$
declare
request_id bigint;
params_array text[];
content_type text;
begin
-- Exctract the content_type from headers
select
header_value into content_type
from
jsonb_each_text(coalesce(headers, '{}'::jsonb)) r(header_name, header_value)
where
lower(header_name) = 'content-type'
limit
1;
-- If the user provided new headers and omitted the content type
-- add it back in automatically
if content_type is null then
select headers || '{"Content-Type": "application/json"}'::jsonb into headers;
end if;
-- Confirm that the content-type is set as "application/json"
if content_type <> 'application/json' then
raise exception 'Content-Type header must be "application/json"';
end if;
select
coalesce(array_agg(net._urlencode_string(key) || '=' || net._urlencode_string(value)), '{}')
into
params_array
from
jsonb_each_text(params);
-- Add to the request queue
insert into net.http_request_queue(method, url, headers, body, timeout_milliseconds)
values (
'POST',
net._encode_url_with_params_array(url, params_array),
headers,
convert_to(body::text, 'UTF8'),
timeout_milliseconds
)
returning id
into request_id;
perform net.wake();
return request_id;
end
$$;
ALTER FUNCTION net.http_post (url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) OWNER TO postgres;
CREATE FUNCTION net.wait_until_running()
RETURNS void
LANGUAGE c AS '$libdir/pg_net'
, 'wait_until_running';
ALTER FUNCTION net.wait_until_running () OWNER TO postgres;
COMMENT ON FUNCTION net.wait_until_running ()
IS 'waits until the worker is running';
CREATE FUNCTION net.wake()
RETURNS void
LANGUAGE c AS '$libdir/pg_net'
, 'wake';
ALTER FUNCTION net.wake () OWNER TO postgres;
CREATE FUNCTION net.worker_restart()
RETURNS boolean
LANGUAGE c AS '$libdir/pg_net'
, 'worker_restart';
ALTER FUNCTION net.worker_restart () OWNER TO postgres;
CREATE UNLOGGED TABLE net._http_response (
id bigint
, status_code integer
, content_type text
, headers jsonb
, content text
, timed_out boolean
, error_msg text
, created timestamp with time zone DEFAULT now() NOT NULL
);
ALTER TABLE net._http_response OWNER TO postgres;
CREATE UNLOGGED TABLE net.http_request_queue (
id bigint NOT NULL
, method net.http_method NOT NULL
, url text NOT NULL
, headers jsonb
, body bytea
, timeout_milliseconds integer NOT NULL
);
ALTER TABLE net.http_request_queue OWNER TO postgres;
CREATE UNLOGGED SEQUENCE net.http_request_queue_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE net.http_request_queue_id_seq OWNER TO postgres;
ALTER SEQUENCE net.http_request_queue_id_seq OWNED BY net.http_request_queue.id;
ALTER TABLE ONLY net.http_request_queue ALTER COLUMN id SET DEFAULT nextval(CAST('net.http_request_queue_id_seq' AS regclass));
CREATE INDEX _http_response_created_idx
ON net._http_response (created);
GRANT USAGE
ON SCHEMA net
TO PUBLIC;
GRANT ALL PRIVILEGES
ON TABLE net._http_response
TO PUBLIC;
GRANT ALL PRIVILEGES
ON TABLE net.http_request_queue
TO PUBLIC;
GRANT ALL PRIVILEGES
ON SEQUENCE net.http_request_queue_id_seq
TO PUBLIC