pg_net是一个PostgreSQL扩展,为异步网络暴露了一个SQL接口,重点是可扩展性和用户体验。

它与http扩展的不同之处在于,它默认是异步的。这使得它在阻塞函数(如触发器)中很有用。

用法

启用扩展功能

http_get

创建一个HTTP GET请求,返回该请求的ID。在事务提交之前,HTTP请求不会被启动。

签名

  net.http_get(
    -- url for the request
    url text,
    -- key/value pairs to be url encoded and appended to the `url`
    params jsonb default '{}'::jsonb,
    -- key/values to be included in request headers
    headers jsonb default '{}'::jsonb,
    -- WARNING: this is currently ignored, so there is no timeout
    -- the maximum number of milliseconds the request may take before being cancelled
    timeout_milliseconds int default 1000
)
    -- request_id reference
    returns bigint

    strict
    volatile
    parallel safe
    language plpgsql
  

使用方法

  select net.http_get('https://news.ycombinator.com') as request_id;
request_id
----------
         1
(1 row)
  

在触发了http_get之后,使用http_get_result来获取请求的结果。

http_post

创建一个带有JSON主体的HTTP POST请求,返回请求的ID。HTTP请求在事务提交之前不会被启动。

主体的字符集编码与数据库的server_encoding设置一致。

签名

  net.http_post(
    -- url for the request
    url text,
    -- body of the POST request
    body jsonb default '{}'::jsonb,
    -- key/value pairs to be url encoded and appended to the `url`
    params jsonb default '{}'::jsonb,
    -- key/values to be included in request headers
    headers jsonb default '{"Content-Type": "application/json"}'::jsonb,
    -- WARNING: this is currently ignored, so there is no timeout
    -- the maximum number of milliseconds the request may take before being cancelled
    timeout_milliseconds int default 1000
)
    -- request_id reference
    returns bigint

    volatile
    parallel safe
    language plpgsql
  

使用方法

  select
    net.http_post(
        url:='https://httpbin.org/post',
        body:='{"hello": "world"}'::jsonb
    ) as request_id;
request_id
----------
         1
(1 row)
  

在触发了http_post之后,使用http_get_result来获得请求的结果。

http_collect_response

给出一个request_id参考,检索响应。

async:=false被设置时,建议将statement_timeout设置为调用者愿意等待的最大时间,以防止响应填充缓慢。

签名

  net.http_collect_response(
    -- request_id reference
    request_id bigint,
    -- when `true`, return immediately. when `false` wait for the request to complete before returning
    async bool default true
)
    -- http response composite wrapped in a result type
    returns net.http_response_result

    strict
    volatile
    parallel safe
  

使用方法

  select
    net.http_post(
        url:='https://httpbin.org/post',
        body:='{"hello": "world"}'::jsonb
    ) as request_id;
request_id
----------
         1
(1 row)

select * from net.http_collect_response(1, async:=false);
status  | message | response
--------+---------+----------
SUCCESS        ok   (
                      status_code := 200,
                      headers     := '{"date": ...}',
                      body        := '{"args": ...}'
                    )::net.http_response_result


select
    (response).body::json
from
    net.http_collect_response(request_id:=1);
                               body
-------------------------------------------------------------------
 {
   "args": {},
   "data": "{\"hello\": \"world\"}",
   "files": {},
   "form": {},
   "headers": {
     "Accept": "*/*",
     "Content-Length": "18",
     "Content-Type": "application/json",
     "Host": "httpbin.org",
     "User-Agent": "pg_net/0.2",
     "X-Amzn-Trace-Id": "Root=1-61031a5c-7e1afeae69bffa8614d8e48e"
   },
   "json": {
     "hello": "world"
   },
   "origin": "135.63.38.488",
   "url": "https://httpbin.org/post"
 }
(1 row)
  

其中,response是一个组合:

  status_code integer
headers jsonb
body text
  

net.http_response_result.status的可能值是(‘PENDING’, ‘SUCCESS’, ‘ERROR’)’。

资源