Can cron-scheduled functions interact with DB?

I saw this example: https://supabase.com/docs/guides/functions/schedule-functions It shows how to use pg_cron to schedule the call of an edge function. My question is: Can a function triggered from the db also open its own db connection to interact with the DB, or would it not work because of re-entrancy? (DB calling the function which tries to access the DB)
Scheduling Edge Functions | Supabase Docs
Schedule Edge Functions with pg_cron.
3 Replies
garyaustin
garyaustin2y ago
It should work. I assume you need to do stuff in the edge function you can't do with a cron scheduled db function? Note the http extension has a 5 second timeout if that is what you are using.
localλorse
localλorseOP2y ago
For my use case the cron function doesn't have to be triggered by the db. It just has to be triggered on a cron schedule. It then runs, fetches data from external APIs, processes it and inserts it into the DB. Will that work? Is this 5s CPU time or wall time? It will process the external-API-returned data with openai, so most wall-time will be spent waiting on IO. I definitely need more than 5s wall-time. What's the solution for that? E.g. what if I need it to run for 1 min (wall-time)?
garyaustin
garyaustin2y ago
The 5 seconds is if you use an http call to kick off the edge function from the database cron. You technically could come up with some sort of 3rd party tool to run your edge functions. 5 seconds is the limit before you have to reply to the http request. pg_net can also be used and you can ignore the response and it returns back to the database as soon as the call is made. Note there are also time limits for how long you have for edge functions to run both CPU time and wall time.

Did you find this page helpful?