Skip to content

Add cron.dom_dow_and_logic parameter to control days logic#421

Open
vdjakovic wants to merge 1 commit intocitusdata:mainfrom
vdjakovic:feature/dom-dow-and-logic
Open

Add cron.dom_dow_and_logic parameter to control days logic#421
vdjakovic wants to merge 1 commit intocitusdata:mainfrom
vdjakovic:feature/dom-dow-and-logic

Conversation

@vdjakovic
Copy link

Currently, pg_cron follows the Vixie cron standard where a configured day-of-month (DOM) and day-of-week (DOW) are combined with OR logic. This makes it impossible to schedule jobs that must satisfy both a day-of-month and a day-of-week configuration.

A common use case is scheduling a job to run on the "first Sunday of every month." With the default OR logic, an expression like '0 5 1-7 * 0' would run at 5:00 AM on days 1 through 7 of the month AND on every Sunday of the month. Which does not have much practical value.

This commit introduces a new parameter, cron.dom_dow_and_logic, to give users control over this behavior. When set to true, pg_cron will use AND logic when both DOM and DOW fields are configured.

With this feature enabled, the expression '0 5 1-7 * 0' runs only on the day that is both within the first 7 days of the month AND is a Sunday.

Currently, pg_cron follows the Vixie cron standard where a configured
day-of-month (DOM) and day-of-week (DOW) are combined with OR logic.
This makes it impossible to schedule jobs that must satisfy both a
day-of-month and a day-of-week configuration.

A common use case is scheduling a job to run on the "first Sunday of
every month." With the default OR logic, an expression like '0 5 1-7 * 0'
would run at 5:00 AM on days 1 through 7 of the month AND on every
Sunday of the month. Which does not have much practical value.

This commit introduces a new parameter, `cron.dom_dow_and_logic`,
to give users control over this behavior. When set to `true`, pg_cron
will use AND logic when both DOM and DOW fields are configured.

With this feature enabled, the expression '0 5 1-7 * 0' runs
only on the day that is both within the first 7 days of the month AND
is a Sunday.
Copy link
Collaborator

@sfc-gh-mslot sfc-gh-mslot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think first Sunday of the month is a reasonable request. This patch is very small, so that's good. There probably isn't a very simple and intuitive syntax change we can make.

The general recommendation would be for the query/function being invoked to check what day it is, but that is a bit annoying.

check_timezone, NULL, NULL);

DefineCustomBoolVariable(
"cron.dom_dow_and_logic",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Struggling a bit with this name, maybe cron.match_dom_and_dow communicates the intent slightly more rather than referring to the implementation logic. Any thoughts?

Copy link
Author

@vdjakovic vdjakovic Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, it becomes even more complicated with vacuum because it can't be run inside transaction/function, having is fixed here sounds most logical and future proof, cron.match_dom_and_dow is better name.

Is this one good for you?

DefineCustomBoolVariable(
	"cron.match_dom_and_dow",
	gettext_noop("Requires a day to match both the day-of-month and day-of-week fields."),
	NULL,
	&CronDomDowAndLogic,
	false,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants