Skip to content

Commit

Permalink
Based on user GUC configuration, call run_maintenance() or run_mainte…
Browse files Browse the repository at this point in the history
…nance_proc().
  • Loading branch information
toliujiayi committed Dec 10, 2018
1 parent d28670f commit 4d909e7
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/pg_partman_bgw.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ static volatile sig_atomic_t got_sighup = false;
static volatile sig_atomic_t got_sigterm = false;

/* GUC variables */
static char *pg_partman_bgw_maintenance_proc = "off";
static int pg_partman_bgw_interval = 3600; // Default hourly
static char *pg_partman_bgw_role = "postgres"; // Default to postgres role
static char *pg_partman_bgw_analyze = "on";
Expand Down Expand Up @@ -93,6 +94,17 @@ _PG_init(void)
{
BackgroundWorker worker;

DefineCustomStringVariable("pg_partman_bgw.maintenance_proc",
"use run_maintenance() or run_maintenance_proc(). Set to 'on' to send TRUE. Set to 'off' to send FALSE.(default).",
NULL,
&pg_partman_bgw_maintenance_proc,
"on",
PGC_SIGHUP,
0,
NULL,
NULL,
NULL);

DefineCustomIntVariable("pg_partman_bgw.interval",
"How often run_maintenance() is called (in seconds).",
NULL,
Expand Down Expand Up @@ -365,6 +377,7 @@ void pg_partman_bgw_run_maint(Datum arg) {
List *elemlist;
int ret;
StringInfoData buf;
char *maintenance_func_name;

/* Establish signal handlers before unblocking signals. */
pqsignal(SIGHUP, pg_partman_bgw_sighup);
Expand Down Expand Up @@ -476,14 +489,21 @@ void pg_partman_bgw_run_maint(Datum arg) {
} else {
jobmon = "false";
}
appendStringInfo(&buf, "SELECT \"%s\".run_maintenance(p_analyze := %s, p_jobmon := %s)", partman_schema, analyze, jobmon);
if (strcmp(pg_partman_bgw_maintenance_proc, "on") == 0) {
maintenance_func_name = "run_maintenance_proc";
} else {
maintenance_func_name = "run_maintenance";
}

appendStringInfo(&buf, "SELECT \"%s\".%s(p_analyze := %s, p_jobmon := %s)", partman_schema, maintenance_func_name, analyze, jobmon);


pgstat_report_activity(STATE_RUNNING, buf.data);

ret = SPI_execute(buf.data, false, 0);

if (ret != SPI_OK_SELECT)
elog(FATAL, "Cannot call pg_partman run_maintenance() function: error code %d", ret);
elog(FATAL, "Cannot call pg_partman %s() function: error code %d", maintenance_func_name, ret);

elog(LOG, "%s: %s called by role %s on database %s"
, MyBgworkerEntry->bgw_name
Expand Down

0 comments on commit 4d909e7

Please sign in to comment.