Skip to content

Commit 026bc46

Browse files
committed
Back-patch bgworker API changes to 9.3.
Commit 7f7485a made these changes in master; per discussion, backport the API changes (but not the functional changes), so that people don't get used to the 9.3 API only to see it get broken in the next release. There are already some people coding to the original 9.3 API, and this will cause minor breakage, but there will be even more if we wait until next year to roll out these changes.
1 parent 295f9bb commit 026bc46

File tree

4 files changed

+18
-21
lines changed

4 files changed

+18
-21
lines changed

contrib/worker_spi/worker_spi.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,17 @@ initialize_worker_spi(worktable *table)
154154
}
155155

156156
static void
157-
worker_spi_main(void *main_arg)
157+
worker_spi_main(Datum main_arg)
158158
{
159-
worktable *table = (worktable *) main_arg;
159+
int index = DatumGetInt32(main_arg);
160+
worktable *table;
160161
StringInfoData buf;
162+
char name[20];
163+
164+
table = palloc(sizeof(worktable));
165+
sprintf(name, "schema%d", index);
166+
table->schema = pstrdup(name);
167+
table->name = pstrdup("counted");
161168

162169
/* Establish signal handlers before unblocking signals. */
163170
pqsignal(SIGHUP, worker_spi_sighup);
@@ -296,9 +303,7 @@ void
296303
_PG_init(void)
297304
{
298305
BackgroundWorker worker;
299-
worktable *table;
300306
unsigned int i;
301-
char name[20];
302307

303308
/* get the configuration */
304309
DefineCustomIntVariable("worker_spi.naptime",
@@ -338,14 +343,8 @@ _PG_init(void)
338343
*/
339344
for (i = 1; i <= worker_spi_total_workers; i++)
340345
{
341-
sprintf(name, "worker %d", i);
342-
worker.bgw_name = pstrdup(name);
343-
344-
table = palloc(sizeof(worktable));
345-
sprintf(name, "schema%d", i);
346-
table->schema = pstrdup(name);
347-
table->name = pstrdup("counted");
348-
worker.bgw_main_arg = (void *) table;
346+
snprintf(worker.bgw_name, BGW_MAXLEN, "worker %d", i);
347+
worker.bgw_main_arg = Int32GetDatum(i);
349348

350349
RegisterBackgroundWorker(&worker);
351350
}

doc/src/sgml/bgworker.sgml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@
4040
typedef void (*bgworker_main_type)(void *main_arg);
4141
typedef struct BackgroundWorker
4242
{
43-
char *bgw_name;
43+
char bgw_name[BGW_MAXLEN];
4444
int bgw_flags;
4545
BgWorkerStartTime bgw_start_time;
4646
int bgw_restart_time; /* in seconds, or BGW_NEVER_RESTART */
47-
bgworker_main_type bgw_main;
48-
void *bgw_main_arg;
47+
bgworker_main_type bgw_main;
48+
Datum bgw_main_arg;
4949
} BackgroundWorker;
5050
</programlisting>
5151
</para>

src/backend/postmaster/postmaster.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5247,9 +5247,6 @@ RegisterBackgroundWorker(BackgroundWorker *worker)
52475247
}
52485248

52495249
rw->rw_worker = *worker;
5250-
rw->rw_worker.bgw_name = ((char *) rw) + sizeof(RegisteredBgWorker);
5251-
strlcpy(rw->rw_worker.bgw_name, worker->bgw_name, namelen + 1);
5252-
52535250
rw->rw_backend = NULL;
52545251
rw->rw_pid = 0;
52555252
rw->rw_child_slot = 0;

src/include/postmaster/bgworker.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
#define BGWORKER_BACKEND_DATABASE_CONNECTION 0x0002
5353

5454

55-
typedef void (*bgworker_main_type) (void *main_arg);
55+
typedef void (*bgworker_main_type) (Datum main_arg);
5656

5757
/*
5858
* Points in time at which a bgworker can request to be started
@@ -66,15 +66,16 @@ typedef enum
6666

6767
#define BGW_DEFAULT_RESTART_INTERVAL 60
6868
#define BGW_NEVER_RESTART -1
69+
#define BGW_MAXLEN 64
6970

7071
typedef struct BackgroundWorker
7172
{
72-
char *bgw_name;
73+
char bgw_name[BGW_MAXLEN];
7374
int bgw_flags;
7475
BgWorkerStartTime bgw_start_time;
7576
int bgw_restart_time; /* in seconds, or BGW_NEVER_RESTART */
7677
bgworker_main_type bgw_main;
77-
void *bgw_main_arg;
78+
Datum bgw_main_arg;
7879
} BackgroundWorker;
7980

8081
/* Register a new bgworker */

0 commit comments

Comments
 (0)