I'm using PostgreSQL on WinNT to support (along with Zope) several
websites.  I've currently got postmaster running as an NT service by
wrapping it with the 'invoker' utility, but I'd prefer to have it run
directly as a service so that it can be stopped cleanly.  I now know
the basics of how to extend a program to make it run directly as an NT
service, as I've enhanced the Cygipc (Cygwin) 'ipc-daemon' process to
do just that (and I've submitted the patch to the current maintainer
of Cygipc).  So now I'd like to do the same for postgres/postmaster,
but I want you all to vet this plan before I invest much more time.

For me, the fundamental question is:  If I create a patch that
implements this function, what will it take to get it into the main
CVS version?  Will some established PostgreSQL developer offer to
review/champion/mentor my work?

Expected plan of operation:

1) Administrator runs 'postgres --install-as-service'.  Program
   installs NT registry entries and calls Windows APIs to register the
   service, leaving it ready for 'automatic' startup, but not starting
   the service.  The ImagePath key value uses exactly the full path
   of the 'postgres' program that did the install.  After installing
   the service the program returns immediately.

2) When starting the service from, say, the Services Control Panel,
   postgres is invoked with a new '--service' command-line flag.  When
   the postgres program starts running it looks for this option
   right away (much like '-boot') and, finding it, starts up in
   postmaster mode and passes on the --services flag, causing the
   postmaster code to do the necessary magic to start up as an NT
   service.  This does not involve any additional forks; the process
   splits into a supervisory thread and a main thread that runs the
   existing main work loop.

3) When the service is stopped, the supervisory service thread sends some
   kind of event/signal/semaphore/... to the main work thread, causing
   that thread to shut down as if 'pg_ctl -m fast stop' had been
   called [is this a good choice?].

4) Administrator runs 'postgres --remove-as-service'.  The program
   removes the registry data for the service and calls APIs to
   completely remove the service.  It shuts down the service first if
   it's already running.  Then it quits.

Design questions:

a) The 'install' and 'remove' code is quite bulky, although simple.
Is it OK to create a new source file for it, rather than working it
into some existing file?

b) How can the service log error messages?  Is there any syslog type
facility?  If not, is it OK to include a new output interface that
would log to the NT event log?  Or is there some better scheme?

c) What is a good way for the supervisory thread to communicate with
the main thread and shut it down cleanly?  (I haven't studied the
code to find the main wait/select loop yet.  Sorry.)

d) How can we insure that the ipc-daemon service starts before the
postgres/postmaster service?  I believe that NT has some facility that
allows one service to request start-up of another service, but I have
no idea how to make that happen.

Your feedback on this plan is more than welcome.  I'm willing to put
time into getting this working, but only if I get some support from you
all.

-- 
Fred Yankowski           fred@OntoSys.com      tel: +1.630.879.1312
Principal Consultant     www.OntoSys.com       fax: +1.630.879.1370
OntoSys, Inc             38W242 Deerpath Rd, Batavia, IL 60510, USA

Sat, 27 Jan 2001 08:49:46 -0600
Fred Yankowski <fred@ontosys.com>
plan for running postmaster directly as NT service
Fred Yankowski <fred@ontosys.com> writes:
> a) The 'install' and 'remove' code is quite bulky, although simple.
> Is it OK to create a new source file for it, rather than working it
> into some existing file?

I think this'd be a lot easier to sell (and probably to implement)
if the NT-specific stuff is a separate, arm's-length wrapper around
the standard Unix version.  Possibly you want something that replaces
src/backend/main/main.c in an NT build.

> b) How can the service log error messages?  Is there any syslog type
> facility?

Syslog support exists.  There's still a lot of debugging-type code that
scribbles to stderr, but (at least for a first cut) you could just let
that go to the bit bucket.

> c) What is a good way for the supervisory thread to communicate with
> the main thread and shut it down cleanly?

Send it an appropriate signal, same as pg_ctl does.

			regards, tom lane

Sat, 27 Jan 2001 11:03:10 -0500
Tom Lane <tgl@sss.pgh.pa.us>
Re: plan for running postmaster directly as NT service 