Skip to content

Commit d40dcbd

Browse files
committed
fix: incorrect shared memory setup
Before the shared memory wasn't guarded with lwlocks plus there was no call to RequestAddinShmemSpace to estimate used memory. Closes #220
1 parent 077ab7a commit d40dcbd

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/pg_prelude.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747

4848
#pragma GCC diagnostic pop
4949

50+
#define PG15_GTE (PG_VERSION_NUM >= 150000)
51+
5052
const char *xact_event_name(XactEvent event);
5153
#endif /* PG_PRELUDE_H */
5254

src/worker.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ static int guc_batch_size;
3737
static char* guc_database_name;
3838
static char* guc_username;
3939
static MemoryContext CurlMemContext = NULL;
40+
41+
#if PG15_GTE
42+
static shmem_request_hook_type prev_shmem_request_hook = NULL;
43+
#endif
44+
4045
static shmem_startup_hook_type prev_shmem_startup_hook = NULL;
4146
static volatile sig_atomic_t got_sighup = false;
4247

@@ -357,12 +362,27 @@ void pg_net_worker(__attribute__ ((unused)) Datum main_arg) {
357362
proc_exit(EXIT_FAILURE);
358363
}
359364

365+
static Size net_memsize(void) {
366+
return MAXALIGN(sizeof(WorkerState));
367+
}
368+
369+
#if PG15_GTE
370+
static void net_shmem_request(void) {
371+
if (prev_shmem_request_hook)
372+
prev_shmem_request_hook();
373+
374+
RequestAddinShmemSpace(net_memsize());
375+
}
376+
#endif
377+
360378
static void net_shmem_startup(void) {
361379
if (prev_shmem_startup_hook)
362380
prev_shmem_startup_hook();
363381

364382
bool found;
365383

384+
LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
385+
366386
worker_state = ShmemInitStruct("pg_net worker state", sizeof(WorkerState), &found);
367387

368388
if (!found) {
@@ -375,6 +395,8 @@ static void net_shmem_startup(void) {
375395
worker_state->epfd = 0;
376396
worker_state->curl_mhandle = NULL;
377397
}
398+
399+
LWLockRelease(AddinShmemInitLock);
378400
}
379401

380402
void _PG_init(void) {
@@ -397,6 +419,13 @@ void _PG_init(void) {
397419
.bgw_restart_time = net_worker_restart_time_sec,
398420
});
399421

422+
#if PG15_GTE
423+
prev_shmem_request_hook = shmem_request_hook;
424+
shmem_request_hook = net_shmem_request;
425+
#else
426+
RequestAddinShmemSpace(net_memsize());
427+
#endif
428+
400429
prev_shmem_startup_hook = shmem_startup_hook;
401430
shmem_startup_hook = net_shmem_startup;
402431

0 commit comments

Comments
 (0)