bgworkers should share more of BackendRun / PostgresMain - Mailing list pgsql-hackers

From Craig Ringer
Subject bgworkers should share more of BackendRun / PostgresMain
Date
Msg-id CAMsr+YEwjEUj78hGGobMZ1FugpTXpjdFyJ_rLJ4=Zk0n=YayJw@mail.gmail.com
Whole thread Raw
List pgsql-hackers
Hi folks

As I work on background workers almost exclusively at the moment, I keep on running into things that are just not initialized, or not supported, in bgworkers. Many are quite surprising.

I think bgworkers should just share more of the main Pg startup path.

My latest surprise is that we don't re-initialize the RNG for bgworkers, because it's done in BackendRun before calling PostgresMain, and bgworker code path diverges before we call BackendRun.

bgworkers also have to do all their own error handling, duplicating lots of logic in PostgresMain, if they want to have any error recovery except "die and restart".

bgworkers don't participate in the ProcSignal mechanism, get query cancels, etc, unless they do extra setup.

There are all sorts of other small surprises, like the way we don't set up the dbname in shmem, so logs for bgworkers show it as empty.

There's no patch here because I'm not yet sure what the best approach is. Some things seem clear, like moving the RNG init work from BackendRun a bit earlier so all backends hit it. Other parts much less so.  

Look at the number of calls to InitProcess() from our profusion of custom worker types, user backends, the bgworker infrastructure, etc.

I could just copy stuff from BackendRun / PostgresMain to StartBackgroundWorker() but I'm sure that's not the right approach. I think we need to decide what should be common and factor it out a bit.

So discussion/ideas appreciated.

--
 Craig Ringer                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services

pgsql-hackers by date:

Previous
From: Michael Paquier
Date:
Subject: Re: _isnan() on Windows
Next
From: Michael Paquier
Date:
Subject: Re: TRUNCATE tables referenced by FKs on partitioned tables