Home

pg_net: Async Networking

caution

The pg_net API is in alpha. Functions signatures may change.

pg_net is a PostgreSQL extension exposing a SQL interface for async networking with a focus on scalability and UX.

It differs from the http extension in that it is asynchronous by default. This makes it useful in blocking functions (like triggers).

Enable the extension#

  1. Go to the Database page in the Dashboard.
  2. Click on Extensions in the sidebar.
  3. Search for "pg_net" and enable the extension.

http_get#

Creates an HTTP GET request returning the request's ID. HTTP requests are not started until the transaction is committed.

Signature #

caution

This is a Postgres SECURITY DEFINER function.


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

Usage #


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

http_post#

Creates an HTTP POST request with a JSON body, returning the request's ID. HTTP requests are not started until the transaction is committed.

The body's character set encoding matches the database's server_encoding setting.

Signature #

caution

This is a Postgres SECURITY DEFINER function


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

Usage #


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

After triggering http_post, use http_get_result to get the result of the request.

Examples#

Invoke a Supabase Edge Function#

Make a POST request to a Supabase Edge Function with auth header and JSON body payload:


_10
select
_10
net.http_post(
_10
url:='https://project-ref.supabase.co/functions/v1/function-name',
_10
headers:='{"Content-Type": "application/json", "Authorization": "Bearer YOUR_ANON_KEY"}'::jsonb,
_10
body:='{"name": "pg_net"}'::jsonb
_10
) as request_id;

Resources#