快速入门
工作名称区分大小写,创建后无法编辑。
🌐 Job names are case sensitive and cannot be edited once created.
尝试创建一个同名(大小写相同)的第二个作业会覆盖第一个作业。
🌐 Attempting to create a second Job with the same name (and case) will overwrite the first Job.
安排一个工作 #
🌐 Schedule a job
只要你的 Postgres 版本是 15.1.1.61 或更高,你就可以为你的作业调度间隔输入秒数。
🌐 You can input seconds for your Job schedule interval as long as you're on Postgres version 15.1.1.61 or later.
编辑职位 #
🌐 Edit a job
- 去职位部分,找到你想编辑的职位。
- 点击职位右侧的三个竖点菜单,然后点击
Edit cron job。 - 进行更改后,然后点击
Save cron job。
激活/停用工作 #
🌐 Activate/Deactivate a job
- 去职位部分,找到你想取消安排的职位。
- 切换作业名称旁边的
Active/Inactive开关。
取消作业计划 #
🌐 Unschedule a job
- 去职位部分,找到你想删除的职位。
- 点击职位右侧的三个竖点菜单,然后点击
Delete cron job。 - 通过输入职位名称确认删除。
查看作业运行情况 #
🌐 Inspecting job runs
- 去 职位 部分,找到你想查看运行情况的职位。
- 点击工作名称旁边的
History按钮。
示例 #
🌐 Examples
每周删除数据 #
🌐 Delete data every week
每周六凌晨3:30(GMT)删除旧数据:
🌐 Delete old data every Saturday at 3:30AM (GMT):
1select cron.schedule (2 'saturday-cleanup', -- name of the cron job3 '30 3 * * 6', -- Saturday at 3:30AM (GMT)4 $$ delete from events where event_time < now() - interval '1 week' $$5);每天吸尘 #
🌐 Run a vacuum every day
每天凌晨3:00(格林尼治标准时间)吸尘:
🌐 Vacuum every day at 3:00AM (GMT):
1select cron.schedule('nightly-vacuum', '0 3 * * *', 'VACUUM');每五分钟调用一次数据库函数 #
🌐 Call a database function every 5 minutes
创建一个 hello_world() 数据库函数,然后每5分钟调用一次:
🌐 Create a hello_world() database function and then call it every 5 minutes:
1select cron.schedule('call-db-function', '*/5 * * * *', 'SELECT hello_world()');调用数据库存储过程 #
🌐 Call a database stored procedure
要使用存储过程,你可以这样调用它:
🌐 To use a stored procedure, you can call it like this:
1select cron.schedule('call-db-procedure', '*/5 * * * *', 'CALL my_procedure()');每30秒调用一次Supabase Edge函数 #
🌐 Invoke Supabase Edge Function every 30 seconds
每隔 30 秒向 Supabase Edge Function 发送一次 POST 请求:
🌐 Make a POST request to a Supabase Edge Function every 30 seconds:
1select2 cron.schedule(3 'invoke-function-every-half-minute',4 '30 seconds',5 $$6 select7 net.http_post(8 url:='https://project-ref.supabase.co/functions/v1/function-name',9 headers:=jsonb_build_object('Content-Type','application/json', 'apikey', 'YOUR_PUBLISHABLE_KEY'),10 body:=jsonb_build_object('time', now() ),11 timeout_milliseconds:=500012 ) as request_id;13 $$14 );这需要启用 pg_net 扩展。
🌐 This requires the pg_net extension to be enabled.
注意:计划进行系统维护 #
🌐 Caution: Scheduling system maintenance
在设置系统维护任务的作业时要非常小心,因为它们可能会带来意想不到的后果。
🌐 Be extremely careful when setting up Jobs for system maintenance tasks as they can have unintended consequences.
例如,安排一个命令用 pg_terminate_backend(pid) 终止空闲连接可能会干扰像夜间备份这样的关键后台进程。通常,会有一个现成的 Postgres 设置,比如 idle_session_timeout,可以在不冒风险的情况下执行这些常见的维护任务。
🌐 For instance, scheduling a command to terminate idle connections with pg_terminate_backend(pid) can disrupt critical background processes like nightly backups. Often, there is an existing Postgres setting, such as idle_session_timeout, that can perform these common maintenance tasks without the risk.
如果你不确定这是否适用于你的使用情况,可以联系Supabase支持。
🌐 Reach out to Supabase Support if you're unsure if that applies to your use case.