|
| 1 | +create or replace function net.worker_restart() returns bool as $$ |
| 2 | + select pg_reload_conf(); |
| 3 | + select pg_terminate_backend(pid) |
| 4 | + from pg_stat_activity |
| 5 | + where backend_type ilike '%pg_net%'; |
| 6 | +$$ |
| 7 | +security definer -- needs SDF because of pg_terminate_backend |
| 8 | +language sql; |
| 9 | + |
| 10 | +grant usage on schema net to PUBLIC; |
| 11 | +grant all on all tables in schema net to PUBLIC; |
| 12 | + |
| 13 | +alter function net.http_get ( text, jsonb, jsonb, integer) security invoker; |
| 14 | +alter function net.http_post (text, jsonb, jsonb, jsonb, integer) security invoker; |
| 15 | +alter function net.http_delete (text, jsonb, jsonb, integer) security invoker; |
| 16 | +alter function net._http_collect_response ( bigint, boolean) security invoker; |
| 17 | +alter function net.http_collect_response ( bigint, boolean) security invoker; |
| 18 | + |
| 19 | +create or replace function net.http_get( |
| 20 | + url text, |
| 21 | + params jsonb default '{}'::jsonb, |
| 22 | + headers jsonb default '{}'::jsonb, |
| 23 | + timeout_milliseconds int default 5000 |
| 24 | +) |
| 25 | + returns bigint |
| 26 | + strict |
| 27 | + volatile |
| 28 | + parallel safe |
| 29 | + language plpgsql |
| 30 | +as $$ |
| 31 | +declare |
| 32 | + request_id bigint; |
| 33 | + params_array text[]; |
| 34 | +begin |
| 35 | + select coalesce(array_agg(net._urlencode_string(key) || '=' || net._urlencode_string(value)), '{}') |
| 36 | + into params_array |
| 37 | + from jsonb_each_text(params); |
| 38 | + |
| 39 | + insert into net.http_request_queue(method, url, headers, timeout_milliseconds) |
| 40 | + values ( |
| 41 | + 'GET', |
| 42 | + net._encode_url_with_params_array(url, params_array), |
| 43 | + headers, |
| 44 | + timeout_milliseconds |
| 45 | + ) |
| 46 | + returning id |
| 47 | + into request_id; |
| 48 | + |
| 49 | + return request_id; |
| 50 | +end |
| 51 | +$$; |
| 52 | + |
| 53 | +create or replace function net.http_post( |
| 54 | + url text, |
| 55 | + body jsonb default '{}'::jsonb, |
| 56 | + params jsonb default '{}'::jsonb, |
| 57 | + headers jsonb default '{"Content-Type": "application/json"}'::jsonb, |
| 58 | + timeout_milliseconds int DEFAULT 5000 |
| 59 | +) |
| 60 | + returns bigint |
| 61 | + volatile |
| 62 | + parallel safe |
| 63 | + language plpgsql |
| 64 | +as $$ |
| 65 | +declare |
| 66 | + request_id bigint; |
| 67 | + params_array text[]; |
| 68 | + content_type text; |
| 69 | +begin |
| 70 | + |
| 71 | + select |
| 72 | + header_value into content_type |
| 73 | + from |
| 74 | + jsonb_each_text(coalesce(headers, '{}'::jsonb)) r(header_name, header_value) |
| 75 | + where |
| 76 | + lower(header_name) = 'content-type' |
| 77 | + limit |
| 78 | + 1; |
| 79 | + |
| 80 | + if content_type is null then |
| 81 | + select headers || '{"Content-Type": "application/json"}'::jsonb into headers; |
| 82 | + end if; |
| 83 | + |
| 84 | + if content_type <> 'application/json' then |
| 85 | + raise exception 'Content-Type header must be "application/json"'; |
| 86 | + end if; |
| 87 | + |
| 88 | + select |
| 89 | + coalesce(array_agg(net._urlencode_string(key) || '=' || net._urlencode_string(value)), '{}') |
| 90 | + into |
| 91 | + params_array |
| 92 | + from |
| 93 | + jsonb_each_text(params); |
| 94 | + |
| 95 | + insert into net.http_request_queue(method, url, headers, body, timeout_milliseconds) |
| 96 | + values ( |
| 97 | + 'POST', |
| 98 | + net._encode_url_with_params_array(url, params_array), |
| 99 | + headers, |
| 100 | + convert_to(body::text, 'UTF8'), |
| 101 | + timeout_milliseconds |
| 102 | + ) |
| 103 | + returning id |
| 104 | + into request_id; |
| 105 | + |
| 106 | + return request_id; |
| 107 | +end |
| 108 | +$$; |
| 109 | + |
| 110 | +create or replace function net.http_delete( |
| 111 | + url text, |
| 112 | + params jsonb default '{}'::jsonb, |
| 113 | + headers jsonb default '{}'::jsonb, |
| 114 | + timeout_milliseconds int default 5000 |
| 115 | +) |
| 116 | + returns bigint |
| 117 | + strict |
| 118 | + volatile |
| 119 | + parallel safe |
| 120 | + language plpgsql |
| 121 | +as $$ |
| 122 | +declare |
| 123 | + request_id bigint; |
| 124 | + params_array text[]; |
| 125 | +begin |
| 126 | + select coalesce(array_agg(net._urlencode_string(key) || '=' || net._urlencode_string(value)), '{}') |
| 127 | + into params_array |
| 128 | + from jsonb_each_text(params); |
| 129 | + |
| 130 | + insert into net.http_request_queue(method, url, headers, timeout_milliseconds) |
| 131 | + values ( |
| 132 | + 'DELETE', |
| 133 | + net._encode_url_with_params_array(url, params_array), |
| 134 | + headers, |
| 135 | + timeout_milliseconds |
| 136 | + ) |
| 137 | + returning id |
| 138 | + into request_id; |
| 139 | + |
| 140 | + return request_id; |
| 141 | +end |
| 142 | +$$; |
0 commit comments