Increase Supabase CRON Timeout

Hi, I'm seeing Supabase CRON Runs have error:
ERROR: Operation timed out after 5002 milliseconds with 0 bytes received
CONTEXT: PL/pgSQL function run_browser_gym(text) line 87 at assignment
ERROR: Operation timed out after 5002 milliseconds with 0 bytes received
CONTEXT: PL/pgSQL function run_browser_gym(text) line 87 at assignment
I'm trying to run a LRO HTTP POST on a schedule that may take up to 60 minutes. Do I need to somehow modify pg_cron or my function? pg_cron:
create extension if not exists "pg_cron" with schema "pg_catalog";
create extension if not exists "pg_cron" with schema "pg_catalog";
CRON schedule (0 10 * * *):
SELECT run_browser_gym('gusto');
SELECT run_browser_gym('gusto');
CRON function:
-- Add TOTP secret to toast provider in gym endpoint
-- This migration adds the totpSecret field to the toast provider configuration

CREATE OR REPLACE FUNCTION run_browser_gym(p_source_provider text)
RETURNS http_response
LANGUAGE plpgsql
SECURITY DEFINER
AS $$
DECLARE
req http_request;
resp http_response;
v_payload jsonb;
v_login_input jsonb;
BEGIN
v_payload := jsonb_build_object(
'sourceProvider', p_source_provider,
'loginInput', v_login_input
);

req := (
'POST',
worker_url() || '/test/gym',
array[
('Content-Type', 'application/json'),
('Authorization', 'Bearer ' || create_stytch_m2m_jwt())
]::http_header[],
'application/json',
v_payload::text
)::http_request;
resp := http(req);

IF resp.status NOT BETWEEN 200 AND 299 THEN
RAISE EXCEPTION 'HTTP request failed with status %: %', resp.status, resp.content;
END IF;

RETURN resp;
END;
$$;
-- Add TOTP secret to toast provider in gym endpoint
-- This migration adds the totpSecret field to the toast provider configuration

CREATE OR REPLACE FUNCTION run_browser_gym(p_source_provider text)
RETURNS http_response
LANGUAGE plpgsql
SECURITY DEFINER
AS $$
DECLARE
req http_request;
resp http_response;
v_payload jsonb;
v_login_input jsonb;
BEGIN
v_payload := jsonb_build_object(
'sourceProvider', p_source_provider,
'loginInput', v_login_input
);

req := (
'POST',
worker_url() || '/test/gym',
array[
('Content-Type', 'application/json'),
('Authorization', 'Bearer ' || create_stytch_m2m_jwt())
]::http_header[],
'application/json',
v_payload::text
)::http_request;
resp := http(req);

IF resp.status NOT BETWEEN 200 AND 299 THEN
RAISE EXCEPTION 'HTTP request failed with status %: %', resp.status, resp.content;
END IF;

RETURN resp;
END;
$$;
11 Replies
garyaustin
garyaustin5h ago
The issue is not pg_cron. It is with the http extension. It has a 5 second timeout.
Grant
GrantOP5h ago
Thank you. Great incite. https://pgxn.org/dist/http/ From my understanding I can update the http extension's timeout with: ALTER SYSTEM SET http.timeout_msec = 3600000; -- 60 minutes in milliseconds I'll investigate more.
PGXN: PostgreSQL Extension Network
http: HTTP client for PostgreSQL / PostgreSQL Extension Network
HTTP allows you to get the content of a web page in a SQL function call.
garyaustin
garyaustin5h ago
https://github.com/pramsey/pgsql-http?tab=readme-ov-file#keep-alive--timeouts I don't think anyone has ever figured out how to change it.
Grant
GrantOP5h ago
What makes you believe that ^ By default a 5 second timeout is set for the completion of a request. If a different timeout is desired the following GUC variable can be used to set it in milliseconds: https://github.com/pramsey/pgsql-http?tab=readme-ov-file#keep-alive--timeouts
garyaustin
garyaustin5h ago
I don't believe you can set that in Supabase. Several users have tried in the past. Maybe you will be successful. Also not great to have it lock up the transaction for that long. pg_net is async, but you have to come back and get the results later from a result table. Maybe an edge function should be called and update the DB when it gets the result back.
Grant
GrantOP5h ago
In our case, we don't really care about the HTTP response. Worst case I believe we can ignore the timeout. 5 seconds without configurability seems like it should be a warning on this doc page: https://supabase.com/docs/guides/database/extensions/http
garyaustin
garyaustin5h ago
If you don't need the response use pg_net.
Grant
GrantOP5h ago
ahh – thank you. Seems like better fit
garyaustin
garyaustin5h ago
GitHub
Adjusting timeout for http-extension · supabase · Discussion #13760
Is there a way to adjust the timeout for the http-extension? I've tried using the commands they suggest on their GitHub(https://github.com/pramsey/pgsql-http, I've tried both http_set_curlo...
garyaustin
garyaustin5h ago
Been a long time since I played with it though. But pg_net is better for what you want.
Grant
GrantOP5h ago
That will likely fix this error. Will be testing it. Will raise a separate issue if something arises. Thanks Austin.

Did you find this page helpful?