Skip to content

Commit 0bbafb5

Browse files
committed
Allocate Backend structs in PostmasterContext.
The child processes don't need them. By allocating them in PostmasterContext, the memory gets free'd and is made available for other stuff in the child processes. Reviewed-by: Thomas Munro Discussion: https://www.postgresql.org/message-id/4f95c1fc-ad3c-7974-3a8c-6faa3931804c@iki.fi
1 parent 1ca3126 commit 0bbafb5

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

src/backend/postmaster/bgworker.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "storage/shmem.h"
3434
#include "tcop/tcopprot.h"
3535
#include "utils/ascii.h"
36+
#include "utils/memutils.h"
3637
#include "utils/ps_status.h"
3738
#include "utils/timeout.h"
3839

@@ -347,7 +348,9 @@ BackgroundWorkerStateChange(bool allow_new_workers)
347348
/*
348349
* Copy the registration data into the registered workers list.
349350
*/
350-
rw = malloc(sizeof(RegisteredBgWorker));
351+
rw = MemoryContextAllocExtended(PostmasterContext,
352+
sizeof(RegisteredBgWorker),
353+
MCXT_ALLOC_NO_OOM);
351354
if (rw == NULL)
352355
{
353356
ereport(LOG,
@@ -455,7 +458,7 @@ ForgetBackgroundWorker(slist_mutable_iter *cur)
455458
rw->rw_worker.bgw_name)));
456459

457460
slist_delete_current(cur);
458-
free(rw);
461+
pfree(rw);
459462
}
460463

461464
/*
@@ -951,7 +954,9 @@ RegisterBackgroundWorker(BackgroundWorker *worker)
951954
/*
952955
* Copy the registration data into the registered workers list.
953956
*/
954-
rw = malloc(sizeof(RegisteredBgWorker));
957+
rw = MemoryContextAllocExtended(PostmasterContext,
958+
sizeof(RegisteredBgWorker),
959+
MCXT_ALLOC_NO_OOM);
955960
if (rw == NULL)
956961
{
957962
ereport(LOG,

src/backend/postmaster/postmaster.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3321,7 +3321,7 @@ CleanupBackgroundWorker(int pid,
33213321
*/
33223322
if (rw->rw_backend->bgworker_notify)
33233323
BackgroundWorkerStopNotifications(rw->rw_pid);
3324-
free(rw->rw_backend);
3324+
pfree(rw->rw_backend);
33253325
rw->rw_backend = NULL;
33263326
rw->rw_pid = 0;
33273327
rw->rw_child_slot = 0;
@@ -3414,7 +3414,7 @@ CleanupBackend(int pid,
34143414
BackgroundWorkerStopNotifications(bp->pid);
34153415
}
34163416
dlist_delete(iter.cur);
3417-
free(bp);
3417+
pfree(bp);
34183418
break;
34193419
}
34203420
}
@@ -3470,7 +3470,7 @@ HandleChildCrash(int pid, int exitstatus, const char *procname)
34703470
#ifdef EXEC_BACKEND
34713471
ShmemBackendArrayRemove(rw->rw_backend);
34723472
#endif
3473-
free(rw->rw_backend);
3473+
pfree(rw->rw_backend);
34743474
rw->rw_backend = NULL;
34753475
rw->rw_pid = 0;
34763476
rw->rw_child_slot = 0;
@@ -3507,7 +3507,7 @@ HandleChildCrash(int pid, int exitstatus, const char *procname)
35073507
#endif
35083508
}
35093509
dlist_delete(iter.cur);
3510-
free(bp);
3510+
pfree(bp);
35113511
/* Keep looping so we can signal remaining backends */
35123512
}
35133513
else
@@ -4083,7 +4083,7 @@ BackendStartup(Port *port)
40834083
* Create backend data structure. Better before the fork() so we can
40844084
* handle failure cleanly.
40854085
*/
4086-
bn = (Backend *) malloc(sizeof(Backend));
4086+
bn = (Backend *) palloc_extended(sizeof(Backend), MCXT_ALLOC_NO_OOM);
40874087
if (!bn)
40884088
{
40894089
ereport(LOG,
@@ -4099,7 +4099,7 @@ BackendStartup(Port *port)
40994099
*/
41004100
if (!RandomCancelKey(&MyCancelKey))
41014101
{
4102-
free(bn);
4102+
pfree(bn);
41034103
ereport(LOG,
41044104
(errcode(ERRCODE_INTERNAL_ERROR),
41054105
errmsg("could not generate random cancel key")));
@@ -4129,8 +4129,6 @@ BackendStartup(Port *port)
41294129
pid = fork_process();
41304130
if (pid == 0) /* child */
41314131
{
4132-
free(bn);
4133-
41344132
/* Detangle from postmaster */
41354133
InitPostmasterChild();
41364134

@@ -4161,7 +4159,7 @@ BackendStartup(Port *port)
41614159

41624160
if (!bn->dead_end)
41634161
(void) ReleasePostmasterChildSlot(bn->child_slot);
4164-
free(bn);
4162+
pfree(bn);
41654163
errno = save_errno;
41664164
ereport(LOG,
41674165
(errmsg("could not fork new process for connection: %m")));
@@ -5424,7 +5422,7 @@ StartAutovacuumWorker(void)
54245422
return;
54255423
}
54265424

5427-
bn = (Backend *) malloc(sizeof(Backend));
5425+
bn = (Backend *) palloc_extended(sizeof(Backend), MCXT_ALLOC_NO_OOM);
54285426
if (bn)
54295427
{
54305428
bn->cancel_key = MyCancelKey;
@@ -5451,7 +5449,7 @@ StartAutovacuumWorker(void)
54515449
* logged by StartAutoVacWorker
54525450
*/
54535451
(void) ReleasePostmasterChildSlot(bn->child_slot);
5454-
free(bn);
5452+
pfree(bn);
54555453
}
54565454
else
54575455
ereport(LOG,
@@ -5696,7 +5694,7 @@ do_start_bgworker(RegisteredBgWorker *rw)
56965694
/* undo what assign_backendlist_entry did */
56975695
ReleasePostmasterChildSlot(rw->rw_child_slot);
56985696
rw->rw_child_slot = 0;
5699-
free(rw->rw_backend);
5697+
pfree(rw->rw_backend);
57005698
rw->rw_backend = NULL;
57015699
/* mark entry as crashed, so we'll try again later */
57025700
rw->rw_crashed_at = GetCurrentTimestamp();
@@ -5822,7 +5820,7 @@ assign_backendlist_entry(RegisteredBgWorker *rw)
58225820
return false;
58235821
}
58245822

5825-
bn = malloc(sizeof(Backend));
5823+
bn = palloc_extended(sizeof(Backend), MCXT_ALLOC_NO_OOM);
58265824
if (bn == NULL)
58275825
{
58285826
ereport(LOG,

0 commit comments

Comments
 (0)