Skip to content

Commit

Permalink
Support GUC parameter: cron.timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
TsinghuaLucky912 committed Dec 9, 2022
1 parent cede446 commit 35d1475
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ The schedule uses the standard cron syntax, in which * means "run every time per

An easy way to create a cron schedule is: [crontab.guru](http://crontab.guru/).

The code in pg_cron that handles parsing and scheduling comes directly from the cron source code by Paul Vixie, hence the same options are supported. Be aware that pg_cron always uses GMT!
The code in pg_cron that handles parsing and scheduling comes directly from the cron source code by Paul Vixie, hence the same options are supported.

## Installing pg_cron

Expand Down Expand Up @@ -108,6 +108,14 @@ By default, the pg_cron background worker expects its metadata tables to be crea
cron.database_name = 'postgres'
```

Previously we could only use GMT time, but now you can adapt your time by setting cron.timezone. You can configure this by setting the `cron.timezone` configuration parameter in postgresql.conf.
```
# add to postgresql.conf
# optionally, specify the timezone in which the pg_cron background worker should run (defaults to GMT). E.g:
cron.timezone = 'PRC'
```

After restarting PostgreSQL, you can create the pg_cron functions and metadata tables using `CREATE EXTENSION pg_cron`.

_Note: `pg_cron` may only be installed to one database in a cluster. If you need to run jobs in multiple databases, use `cron.schedule_in_database()`._
Expand Down
15 changes: 14 additions & 1 deletion src/pg_cron.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#include "commands/extension.h"
#include "commands/sequence.h"
#include "commands/trigger.h"
#include "commands/variable.h"
#include "lib/stringinfo.h"
#include "libpq-fe.h"
#include "libpq/pqmq.h"
Expand Down Expand Up @@ -163,6 +164,8 @@ static int MaxRunningTasks = 0;
static int CronLogMinMessages = WARNING;
static bool UseBackgroundWorkers = false;

char *cron_timezone = NULL;

static const struct config_enum_entry cron_message_level_options[] = {
{"debug5", DEBUG5, false},
{"debug4", DEBUG4, false},
Expand Down Expand Up @@ -298,6 +301,16 @@ _PG_init(void)
GUC_SUPERUSER_ONLY,
NULL, NULL, NULL);

DefineCustomStringVariable(
"cron.timezone",
gettext_noop("Specify timezone used for cron schedule."),
NULL,
&cron_timezone,
"GMT",
PGC_POSTMASTER,
GUC_SUPERUSER_ONLY,
NULL, NULL, NULL);

/* set up common data for all our workers */
worker.bgw_flags = BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION;
worker.bgw_start_time = BgWorkerStart_RecoveryFinished;
Expand Down Expand Up @@ -934,7 +947,7 @@ ShouldRunTask(entry *schedule, TimestampTz currentTime, bool doWild,
bool doNonWild)
{
time_t currentTime_t = timestamptz_to_time_t(currentTime);
struct tm *tm = gmtime(&currentTime_t);
struct pg_tm* tm = pg_localtime(&currentTime_t, pg_tzset(cron_timezone));

int minute = tm->tm_min -FIRST_MINUTE;
int hour = tm->tm_hour -FIRST_HOUR;
Expand Down

0 comments on commit 35d1475

Please sign in to comment.