diff --git a/Makefile b/Makefile index 205505d..53b0c0c 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ PG_CFLAGS = -Werror -Wno-declaration-after-statement EXTENSION = pg_net -EXTVERSION = 0.7.3 +EXTVERSION = 0.8.0 DATA = $(wildcard sql/*--*.sql) diff --git a/sql/pg_net--0.7.3--0.8.0.sql b/sql/pg_net--0.7.3--0.8.0.sql new file mode 100644 index 0000000..cc23dc2 --- /dev/null +++ b/sql/pg_net--0.7.3--0.8.0.sql @@ -0,0 +1,142 @@ +create or replace function net.worker_restart() returns bool as $$ + select pg_reload_conf(); + select pg_terminate_backend(pid) + from pg_stat_activity + where backend_type ilike '%pg_net%'; +$$ +security definer -- needs SDF because of pg_terminate_backend +language sql; + +grant usage on schema net to PUBLIC; +grant all on all tables in schema net to PUBLIC; + +alter function net.http_get ( text, jsonb, jsonb, integer) security invoker; +alter function net.http_post (text, jsonb, jsonb, jsonb, integer) security invoker; +alter function net.http_delete (text, jsonb, jsonb, integer) security invoker; +alter function net._http_collect_response ( bigint, boolean) security invoker; +alter function net.http_collect_response ( bigint, boolean) security invoker; + +create or replace function net.http_get( + url text, + params jsonb default '{}'::jsonb, + headers jsonb default '{}'::jsonb, + timeout_milliseconds int default 5000 +) + returns bigint + strict + volatile + parallel safe + 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); + + 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; + + return request_id; +end +$$; + +create or replace function net.http_post( + url text, + body jsonb default '{}'::jsonb, + params jsonb default '{}'::jsonb, + headers jsonb default '{"Content-Type": "application/json"}'::jsonb, + timeout_milliseconds int DEFAULT 5000 +) + returns bigint + volatile + parallel safe + language plpgsql +as $$ +declare + request_id bigint; + params_array text[]; + content_type text; +begin + + 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 content_type is null then + select headers || '{"Content-Type": "application/json"}'::jsonb into headers; + end if; + + 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); + + 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; + + return request_id; +end +$$; + +create or replace function net.http_delete( + url text, + params jsonb default '{}'::jsonb, + headers jsonb default '{}'::jsonb, + timeout_milliseconds int default 5000 +) + returns bigint + strict + volatile + parallel safe + 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); + + insert into net.http_request_queue(method, url, headers, timeout_milliseconds) + values ( + 'DELETE', + net._encode_url_with_params_array(url, params_array), + headers, + timeout_milliseconds + ) + returning id + into request_id; + + return request_id; +end +$$; diff --git a/sql/pg_net.sql b/sql/pg_net.sql index a32f287..208bbd4 100644 --- a/sql/pg_net.sql +++ b/sql/pg_net.sql @@ -115,7 +115,6 @@ create or replace function net.http_get( volatile parallel safe language plpgsql - security definer as $$ declare request_id bigint; @@ -159,7 +158,6 @@ create or replace function net.http_post( volatile parallel safe language plpgsql - security definer as $$ declare request_id bigint; @@ -229,7 +227,6 @@ create or replace function net.http_delete( volatile parallel safe language plpgsql - security definer as $$ declare request_id bigint; @@ -290,7 +287,6 @@ create or replace function net._http_collect_response( volatile parallel safe language plpgsql - security definer as $$ declare rec net._http_response; @@ -345,7 +341,6 @@ create or replace function net.http_collect_response( volatile parallel safe language plpgsql - security definer as $$ begin raise notice 'The net.http_collect_response function is deprecated.'; @@ -359,8 +354,8 @@ create or replace function net.worker_restart() returns bool as $$ from pg_stat_activity where backend_type ilike '%pg_net%'; $$ -security definer +security definer -- needs SDF because of pg_terminate_backend language sql; -grant all on schema net to postgres; -grant all on all tables in schema net to postgres; +grant usage on schema net to PUBLIC; +grant all on all tables in schema net to PUBLIC;