Inconsistent bgworker behaviour

From: Beena Emerson <memissemerson(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Inconsistent bgworker behaviour
Date: 2015-01-07 03:54:16
Message-ID: CAOG9ApERAYKscHQcReyH_F=fAc_1uStJz2uzCON+=_1k9A-ctw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello,

I have been working on a module which launches background workers for a
list of databases provided by a configuration parameter(say m_databases).
This configuration parameter can be edited and reloaded.
It has a launcher which manages all the workers launched by the module.
The worker.bgw_notify_pid of the workers are set to the launcher pid.

The number of background workers that can be launched is restricted by
max_worker_processes.

Consider the following scenario:
max_worker_processes = 3
m_databases='db1, db2'

The server is started.

The m_databases is updated to
m_databases='db3, db2'

$ pg_ctl reload

The expected behavior is that the db1 worker should be terminated and db3
worker should be launched. However I found that this behavior is not
consistent. In few runs, when the databases parameter is edited and
reloaded, the new worker is launched before the old ones are terminated
causing an error.

I have used the following code on the launcher to ensure that the old
unnecessary background workers are terminated before the new background
workers are launched for newly added databases.
for (i = 0; i < workers_launched; i++)
{
if (!is_inlist(new_dblist, workers[i]->dbname))
{
/* Ensure that the background worker is terminated before
regitsering
* new workers to avoid crossing the limit of
max_worker_processes
*/
ResetLatch(&MyProc->procLatch);
TerminateBackgroundWorker(workers[i]->handle);
WaitLatch(&MyProc->procLatch, WL_LATCH_SET, 0);
}
}
.
.
. (launch new workers)
.

The Latch is set when the SIGUSR1 signal is received. IIUC correctly, the
launcher gets the SIGUSR1 when the bgworker process has exited. No new
worker is launched or terminated in between still the code does not work as
expected for all the runs.

Any help will be appreciated.

Thank you,

Beena

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jiří Hlinka 2015-01-07 08:41:04 Central management for regular tasks on multiple databases
Previous Message deans 2015-01-07 03:28:00 BDR Error restarted