Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
RekGRpth committed Nov 26, 2023
1 parent 8e52edf commit be5103f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 29 deletions.
15 changes: 5 additions & 10 deletions conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ static void conf_user(const Work *w) {
static int conf_bgw_main_arg(WorkShared *ws) {
LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
for (int slot = 0; slot < max_worker_processes; slot++) if (!workshared[slot].in_use) {
pg_write_barrier();
workshared[slot] = *ws;
workshared[slot].in_use = true;
LWLockRelease(BackgroundWorkerLock);
Expand All @@ -80,12 +81,6 @@ static int conf_bgw_main_arg(WorkShared *ws) {
return -1;
}

static void conf_workshared_free(int slot) {
LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
MemSet(&workshared[slot], 0, sizeof(*workshared));
LWLockRelease(BackgroundWorkerLock);
}

static void conf_work(Work *w) {
BackgroundWorkerHandle *handle;
BackgroundWorker worker = {0};
Expand All @@ -109,14 +104,14 @@ static void conf_work(Work *w) {
worker.bgw_restart_time = work_restart;
worker.bgw_start_time = BgWorkerStart_RecoveryFinished;
if (!RegisterDynamicBackgroundWorker(&worker, &handle)) {
conf_workshared_free(worker.bgw_main_arg);
workshared_free(worker.bgw_main_arg);
ereport(ERROR, (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED), errmsg("could not register background worker"), errhint("Consider increasing configuration parameter \"max_worker_processes\".")));
}
switch (WaitForBackgroundWorkerStartup(handle, &w->pid)) {
case BGWH_NOT_YET_STARTED: conf_workshared_free(worker.bgw_main_arg); ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("BGWH_NOT_YET_STARTED is never returned!"))); break;
case BGWH_POSTMASTER_DIED: conf_workshared_free(worker.bgw_main_arg); ereport(ERROR, (errcode(ERRCODE_INSUFFICIENT_RESOURCES), errmsg("cannot start background worker without postmaster"), errhint("Kill all remaining database processes and restart the database."))); break;
case BGWH_NOT_YET_STARTED: workshared_free(worker.bgw_main_arg); ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("BGWH_NOT_YET_STARTED is never returned!"))); break;
case BGWH_POSTMASTER_DIED: workshared_free(worker.bgw_main_arg); ereport(ERROR, (errcode(ERRCODE_INSUFFICIENT_RESOURCES), errmsg("cannot start background worker without postmaster"), errhint("Kill all remaining database processes and restart the database."))); break;
case BGWH_STARTED: elog(DEBUG1, "started"); conf_free(w); break;
case BGWH_STOPPED: conf_workshared_free(worker.bgw_main_arg); ereport(ERROR, (errcode(ERRCODE_INSUFFICIENT_RESOURCES), errmsg("could not start background worker"), errhint("More details may be available in the server log."))); break;
case BGWH_STOPPED: workshared_free(worker.bgw_main_arg); ereport(ERROR, (errcode(ERRCODE_INSUFFICIENT_RESOURCES), errmsg("could not start background worker"), errhint("More details may be available in the server log."))); break;
}
if (handle) pfree(handle);
}
Expand Down
2 changes: 2 additions & 0 deletions include.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,5 +243,7 @@ void SPI_execute_with_args_my(const char *src, int nargs, Oid *argtypes, Datum *
void SPI_finish_my(void);
void task_error(ErrorData *edata);
void task_free(Task *t);
void taskshared_free(int slot);
void workshared_free(int slot);

#endif // _INCLUDE_H_
11 changes: 8 additions & 3 deletions task.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,18 @@ static void task_execute(void) {
if (IsTransactionState()) ereport(ERROR, (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION), errmsg("still active sql transaction")));
}

static void task_shmem_exit(int code, Datum arg) {
elog(DEBUG1, "code = %i", code);
void taskshared_free(int slot) {
LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
MemSet(&taskshared[DatumGetInt32(arg)], 0, sizeof(*taskshared));
pg_read_barrier();
MemSet(&taskshared[slot], 0, sizeof(*taskshared));
LWLockRelease(BackgroundWorkerLock);
}

static void task_shmem_exit(int code, Datum arg) {
elog(DEBUG1, "code = %i", code);
taskshared_free(DatumGetInt32(arg));
}

static void task_catch(void) {
HOLD_INTERRUPTS();
disable_all_timeouts(false);
Expand Down
29 changes: 13 additions & 16 deletions work.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,14 +417,17 @@ static void work_connect(Task *t) {
}
}

void workshared_free(int slot) {
LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
pg_read_barrier();
MemSet(&workshared[slot], 0, sizeof(*workshared));
LWLockRelease(BackgroundWorkerLock);
}

static void work_shmem_exit(int code, Datum arg) {
dlist_mutable_iter iter;
elog(DEBUG1, "code = %i", code);
if (!code) {
LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
MemSet(&workshared[DatumGetInt32(arg)], 0, sizeof(*workshared));
LWLockRelease(BackgroundWorkerLock);
}
if (!code) workshared_free(DatumGetInt32(arg));
dlist_foreach_modify(iter, &head) {
Task *t = dlist_container(Task, node, iter.cur);
if (PQstatus(t->conn) == CONNECTION_OK) {
Expand Down Expand Up @@ -513,6 +516,7 @@ static void work_remote(Task *t) {
static int work_bgw_main_arg(TaskShared *ts) {
LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
for (int slot = 0; slot < max_worker_processes; slot++) if (!taskshared[slot].in_use) {
pg_write_barrier();
taskshared[slot] = *ts;
taskshared[slot].in_use = true;
LWLockRelease(BackgroundWorkerLock);
Expand All @@ -523,12 +527,6 @@ static int work_bgw_main_arg(TaskShared *ts) {
return -1;
}

static void work_taskshared_free(int slot) {
LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
MemSet(&taskshared[slot], 0, sizeof(*taskshared));
LWLockRelease(BackgroundWorkerLock);
}

static void work_task(Task *t) {
BackgroundWorkerHandle *handle = NULL;
BackgroundWorker worker = {0};
Expand All @@ -547,13 +545,12 @@ static void work_task(Task *t) {
worker.bgw_start_time = BgWorkerStart_RecoveryFinished;
t->shared->slot = DatumGetUInt32(MyBgworkerEntry->bgw_main_arg);
if (!RegisterDynamicBackgroundWorker(&worker, &handle)) {
work_taskshared_free(worker.bgw_main_arg);
work_ereport(true, ERROR, (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED), errmsg("could not register background worker"), errhint("Consider increasing configuration parameter \"max_worker_processes\".")));
taskshared_free(worker.bgw_main_arg); work_ereport(true, ERROR, (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED), errmsg("could not register background worker"), errhint("Consider increasing configuration parameter \"max_worker_processes\".")));
} else switch (WaitForBackgroundWorkerStartup(handle, &t->pid)) {
case BGWH_NOT_YET_STARTED: work_taskshared_free(worker.bgw_main_arg); work_ereport(true, ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("BGWH_NOT_YET_STARTED is never returned!"))); break;
case BGWH_POSTMASTER_DIED: work_taskshared_free(worker.bgw_main_arg); work_ereport(true, ERROR, (errcode(ERRCODE_INSUFFICIENT_RESOURCES), errmsg("cannot start background worker without postmaster"), errhint("Kill all remaining database processes and restart the database."))); break;
case BGWH_NOT_YET_STARTED: taskshared_free(worker.bgw_main_arg); work_ereport(true, ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("BGWH_NOT_YET_STARTED is never returned!"))); break;
case BGWH_POSTMASTER_DIED: taskshared_free(worker.bgw_main_arg); work_ereport(true, ERROR, (errcode(ERRCODE_INSUFFICIENT_RESOURCES), errmsg("cannot start background worker without postmaster"), errhint("Kill all remaining database processes and restart the database."))); break;
case BGWH_STARTED: elog(DEBUG1, "started id = %li", t->shared->id); work_free(t); break;
case BGWH_STOPPED: work_taskshared_free(worker.bgw_main_arg); work_ereport(true, ERROR, (errcode(ERRCODE_INSUFFICIENT_RESOURCES), errmsg("could not start background worker"), errhint("More details may be available in the server log."))); break;
case BGWH_STOPPED: taskshared_free(worker.bgw_main_arg); work_ereport(true, ERROR, (errcode(ERRCODE_INSUFFICIENT_RESOURCES), errmsg("could not start background worker"), errhint("More details may be available in the server log."))); break;
}
if (handle) pfree(handle);
}
Expand Down

0 comments on commit be5103f

Please sign in to comment.