fix: honour CRON starts_at in pg_cron materialization#37
Conversation
WalkthroughThe PR adds a start-time activation guard to pg_cron job execution. The ChangesCron Job Start-Time Guard
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
CRON jobs are fired by pg_cron, which is registered with only the cron expression and starts ticking as soon as the job is created. The per-tick materialization command guarded on cron_ends_at but never on cron_starts_at, so a job created with a future starts_at would begin materializing executions on the next matching tick instead of waiting. starts_at was only ever used to compute the displayed next_run_at value, not to gate execution. Add a symmetric cron_starts_at lower-bound guard to build_cron_command, plus a covering unit test.
e3ee643 to
55e6424
Compare
Summary
This change adds a
cron_starts_atguard to the SQL command that pg_cron executes for scheduled jobs, ensuring jobs don't fire before their intended start time.Key Changes
build_cron_commandfunction: AddedAND (j.cron_starts_at IS NULL OR j.cron_starts_at <= now())condition to the WHERE clause to prevent job execution before the scheduled start timecron_starts_atandcron_ends_atguards are necessary to properly bound the job's active windowcron_command_enforces_starts_at_window()test verifies that the generated SQL command includes thecron_starts_atguardImplementation Details
The fix addresses a timing issue where pg_cron registers with only the cron expression and begins ticking immediately. Without the
cron_starts_atguard, a job scheduled to start in the future would fire on the next matching cron tick instead of waiting until itsstarts_attime. The guard works in conjunction with the existingcron_ends_atguard to fully bound the job's execution window.https://claude.ai/code/session_01LFK9KdT41HPV497BcNJWbZ
Summary by CodeRabbit