http

HTTP客户端,允许在数据库内收发HTTP请求 (supabase)

概览

扩展包名版本分类许可证语言
pg_http1.7.2UTILMITC
ID扩展名BinLibLoadCreateTrustReloc模式
4070http-
相关扩展pg_net pg_curl pgjwt pg_smtp_client gzip bzip zstd pgjq pgmb
下游依赖pgmb

版本

类型仓库版本PG 大版本包名依赖
EXTPGDG1.7.21817161514pg_http-
RPMPGDG1.7.21817161514pgsql_http_$v-
DEBPGDG1.7.21817161514postgresql-$v-http-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
u22.x86_64
u22.aarch64
u24.x86_64
u24.aarch64
u26.x86_64
u26.aarch64

构建

您可以使用 pig build 命令构建 pg_http 扩展的 RPM 包:

pig build pkg pg_http         # 构建 RPM 包

安装

您可以直接安装 pg_http 扩展包的预置二进制包,首先确保 PGDG 仓库已经添加并启用:

pig repo add pgdg -u          # 添加 PGDG 仓库并更新缓存

使用 pig 或者是 apt/yum/dnf 安装扩展:

pig install pg_http;          # 当前活跃 PG 版本安装
pig ext install -y pg_http -v 18  # PG 18
pig ext install -y pg_http -v 17  # PG 17
pig ext install -y pg_http -v 16  # PG 16
pig ext install -y pg_http -v 15  # PG 15
pig ext install -y pg_http -v 14  # PG 14
dnf install -y pgsql_http_18       # PG 18
dnf install -y pgsql_http_17       # PG 17
dnf install -y pgsql_http_16       # PG 16
dnf install -y pgsql_http_15       # PG 15
dnf install -y pgsql_http_14       # PG 14
apt install -y postgresql-18-http   # PG 18
apt install -y postgresql-17-http   # PG 17
apt install -y postgresql-16-http   # PG 16
apt install -y postgresql-15-http   # PG 15
apt install -y postgresql-14-http   # PG 14

创建扩展

CREATE EXTENSION http;

用法

来源:

http 允许 PostgreSQL 通过 libcurl 发出同步 HTTP 请求。它适用于受控集成和管理调用,但后端会在 SQL 语句和事务内部等待远程服务的响应。限制可以调用它的用户、设置较短的超时时间,并确保不可信输入不会选择任意 URL。

核心工作流程

CREATE EXTENSION http;

SELECT status, content_type, content
FROM http_get('https://httpbingo.org/get');

发送 JSON 并检查响应:

SELECT status, content::jsonb
FROM http_post(
  'https://httpbingo.org/post',
  '{"event":"invoice.paid"}',
  'application/json'
);

通用入口点接受完整的请求:

SELECT (http((
  'GET',
  'https://httpbingo.org/headers',
  http_headers('Authorization', 'Bearer example'),
  NULL,
  NULL
)::http_request)).status;

重要对象

  • http_request 包含 method, uri, headers, content_type, 和 content
  • http_response 包含 status, content_type, headers, 和 content
  • http_header, http_header(...), 和 http_headers(...) 构建请求头;unnest(response.headers) 将响应头暴露为行。
  • http(...) 执行完整的 http_request
  • http_get, http_post, http_put, http_patch, http_delete, 和 http_head 是方便的包装器。
  • urlencode(text)urlencode(jsonb) 编码查询数据。
  • http_set_curlopt, http_list_curlopt, 和 http_reset_curlopt 管理支持的会话级 libcurl 设置。

超时、连接和安全性

默认情况下,每个请求使用一个新的连接。在测量后端生命周期和远程服务器行为之后再启用持久连接:

SET http.curlopt_timeout_ms = 1000;
SET http.curlopt_connecttimeout_ms = 250;
SET http.curlopt_tcp_keepalive = 1;

默认请求超时为五秒。超时将引发 SQL 错误,因此调用者必须处理事务回滚。触发器或长时间事务中的网络延迟可能会持有锁并耗尽数据库连接;优先使用外部工作进程进行持久异步传递。

保持 TLS 验证启用、保护包含凭据的 curl 设置、在使用前验证响应状态和内容,并在网络层和 SQL 权限层限制出站目的地。版本 1.7.2 包含相对于 1.7.1 的构建、测试和 curl 选项常量维护;它没有引入实质性 SQL API 变更。控制文件仍然声明 SQL 扩展版本为 1.7。