worker_spi.patch
text/x-patch
Filename: worker_spi.patch
Type: text/x-patch
Part: 0
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: unified
| File | + | − |
|---|---|---|
| contrib/worker_spi/worker_spi.c | 56 | 18 |
diff --git a/contrib/worker_spi/worker_spi.c b/contrib/worker_spi/worker_spi.c
index 6da747b..4b6de45 100644
--- a/contrib/worker_spi/worker_spi.c
+++ b/contrib/worker_spi/worker_spi.c
@@ -35,12 +35,16 @@
#include "lib/stringinfo.h"
#include "utils/builtins.h"
#include "utils/snapmgr.h"
+#include "tcop/utility.h"
PG_MODULE_MAGIC;
void _PG_init(void);
+static bool got_sighup = false;
static bool got_sigterm = false;
+static int worker_spi_naptime = 1;
+static int worker_spi_total_workers = 2;
typedef struct worktable
@@ -65,6 +69,7 @@ static void
worker_spi_sighup(SIGNAL_ARGS)
{
elog(LOG, "got sighup!");
+ got_sighup = true;
if (MyProc)
SetLatch(&MyProc->procLatch);
}
@@ -176,13 +181,22 @@ worker_spi_main(void *main_arg)
*/
rc = WaitLatch(&MyProc->procLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
- 1000L);
+ worker_spi_naptime*1000L);
ResetLatch(&MyProc->procLatch);
/* emergency bailout if postmaster has died */
if (rc & WL_POSTMASTER_DEATH)
proc_exit(1);
+ /*
+ * In case of a sighup, just reload the configuration.
+ */
+ if (got_sighup)
+ {
+ got_sighup = false;
+ ProcessConfigFile(PGC_SIGHUP);
+ }
+
StartTransactionCommand();
SPI_connect();
PushActiveSnapshot(GetTransactionSnapshot());
@@ -225,11 +239,40 @@ _PG_init(void)
{
BackgroundWorker worker;
worktable *table;
+ unsigned int i;
+ char name[20];
+
+ /* get the configuration */
+ DefineCustomIntVariable("worker_spi.naptime",
+ "Duration between each check (in seconds).",
+ NULL,
+ &worker_spi_naptime,
+ 1,
+ 1,
+ INT_MAX,
+ PGC_SIGHUP,
+ 0,
+ NULL,
+ NULL,
+ NULL);
+ DefineCustomIntVariable("worker_spi.total_workers",
+ "Number of workers.",
+ NULL,
+ &worker_spi_total_workers,
+ 2,
+ 1,
+ 100,
+ PGC_POSTMASTER,
+ 0,
+ NULL,
+ NULL,
+ NULL);
/* register the worker processes. These values are common for both */
worker.bgw_flags = BGWORKER_SHMEM_ACCESS |
BGWORKER_BACKEND_DATABASE_CONNECTION;
worker.bgw_start_time = BgWorkerStart_RecoveryFinished;
+ worker.bgw_restart_time = BGW_NEVER_RESTART;
worker.bgw_main = worker_spi_main;
worker.bgw_sighup = worker_spi_sighup;
worker.bgw_sigterm = worker_spi_sigterm;
@@ -242,22 +285,17 @@ _PG_init(void)
* memory in the child process; and if we fork and then exec, the exec'd
* process will run this code again, and so the memory is also valid there.
*/
- table = palloc(sizeof(worktable));
- table->schema = pstrdup("schema1");
- table->name = pstrdup("counted");
+ for (i = 1; i <= worker_spi_total_workers; i++)
+ {
+ sprintf(name, "worker %d", i);
+ worker.bgw_name = pstrdup(name);
- worker.bgw_name = "SPI worker 1";
- worker.bgw_restart_time = BGW_NEVER_RESTART;
- worker.bgw_main_arg = (void *) table;
- RegisterBackgroundWorker(&worker);
-
- /* Values for the second worker */
- table = palloc(sizeof(worktable));
- table->schema = pstrdup("our schema2");
- table->name = pstrdup("counted rows");
-
- worker.bgw_name = "SPI worker 2";
- worker.bgw_restart_time = 2;
- worker.bgw_main_arg = (void *) table;
- RegisterBackgroundWorker(&worker);
+ table = palloc(sizeof(worktable));
+ sprintf(name, "schema%d", i);
+ table->schema = pstrdup(name);
+ table->name = pstrdup("counted");
+ worker.bgw_main_arg = (void *) table;
+
+ RegisterBackgroundWorker(&worker);
+ }
}