Skip to content

`pg_cron launcher crashes with 'duplicate key value violates unique constraint'`

pg_cron 启动器进程大约每分钟崩溃一次,显示错误 'duplicate key value violates unique constraint "job_run_details_pkey"'

🌐 The pg_cron launcher process crashes approximately every minute, displaying the error 'duplicate key value violates unique constraint "job_run_details_pkey"'.

这为什么会发生? #

🌐 Why does this happen?

cron.runid_seq 序列与 cron.job_run_details 表不同步时,就会发生这个问题。序列会试图生成表中已经存在的 runid 值,从而导致唯一键冲突。这通常是因为序列的最后一个值没有正确与表中已存在的最大 runid 对齐。

🌐 This issue occurs when the cron.runid_seq sequence is out of sync with the cron.job_run_details table. The sequence attempts to generate runid values that already exist in the table, leading to a unique key violation. This is typically due to the sequence's last value not being correctly aligned with the maximum runid already present in the table.

如何解决这个问题 #

🌐 How to resolve this

要解决这个问题,你需要重置 cron.job_run_details 表。如果需要保留数据,记得在操作前备份内容。

🌐 To resolve this, you need to reset the cron.job_run_details table. If data preservation is required, ensure you back up its contents before proceeding.

通过 SQL 编辑器 执行以下 SQL 命令:TRUNCATE cron.job_run_details;

🌐 Execute the following SQL command via the SQL editor: TRUNCATE cron.job_run_details;