Re: System shutdown signal on Windows (was Re: )

From: Krystian Bigaj <krystian(dot)bigaj(at)gmail(dot)com>
To: Kalai R <softlinne(dot)kv(at)gmail(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: System shutdown signal on Windows (was Re: )
Date: 2014-07-24 19:36:24
Message-ID: CAN=kAeH7Tutr+RF=61djWPoa6AtHN1K05dzOO6LhGoChLzns_Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

On 24 July 2014 07:51, Kalai R <softlinne(dot)kv(at)gmail(dot)com> wrote:

> Thank You so much Krystian Bigaj.
>
> Since last 2 years I had suffering this problem. But today I got solution
> from you.
> I am developing .Net application with Postgres.
> I am using WCF service (host as windows service) to connect postgres
> database.
>
> *My workaround: own service which will start/stop Postgres (mainly to wait
> for our service to stop, running initdb.exe in context of NetworkService,
> etc.).*
> *I've also written small Postgres module loaded via
> shared_preload_libraries which will call SetConsoleCtrlHandler, and my
> HandlerRoutine which simply returns TRUE. Because of
> this pg_console_handler won't be called (so no processing
> CTRL_SHUTDOWN_EVENT by any Postgress process).*
>
> * I need to wait first that our service stops, and then I let Postgres to
> stop (I have my own service - not pg_ctl - which will start/stop
> postgres.exe process).*
>
>
> I have similar situation to you. I also want create my own service to
> start /stop postgres. Please let me know how to start/stop postgres without
> pg_ctl. Could you share code for your small postgres module loaded via
> shared_preloaded libraries?
>
How to start postgres you can find here:
http://www.postgresql.org/docs/9.3/static/server-start.html
You could even use pg_ctl to start/stop postgres.exe process from your
service, but not using pg_ctl as service handler - I think that this should
be sufficient for you, and it's very easy to do, just simply run:
- pg_ctl.exe start -D "data dir" -o "--shared_preload_libraries=your.dll"
and to stop run:
- pg_ctl.exe stop D "data dir"
from your service.

For additional parameters read docs at:
http://www.postgresql.org/docs/9.3/static/app-pg-ctl.html

My PG module looks like this (pseudo code, my code is in Delphi):

dllMain(...)
{
switch(reason)
case DLL_PROCESS_ATTACH:
SetConsoleCtrlHandler(HandlerRoutine, TRUE);

case DLL_PROCESS_DETACH:
SetConsoleCtrlHandler(HandlerRoutine, FALSE);
}

BOOL HandlerRoutine(...)
{
return TRUE; // this will prevent call to next handler routine declared
in postgres process: src\backend\port\win32\singal.c: pg_console_handler <-
won't be called
}

Also that DLL module must export function Pg_magic_func which returns
static/global pointer to initialized Pg_magic_struct structure:
http://doxygen.postgresql.org/structPg__magic__struct.html

To start/stop postgres.exe I'm using:
- CreateProcess - starting postgres.exe, here I'm adding that
--shared_preload_libraries
- CallNamedPipe - sending stop or ping signal (parts of code
from src\port\kill.c)
- RegisterWaitForSingleObject - notification if postgres.exe process will
be abnormally terminated (in that case I do simple logging and terminating
my DB service - to not leave zombie service without really working
postgres.exe)
- pg_isready.exe - while starting service I'm waitng for Postgres to be
ready to accecpt connections, if it's ready, then I finally set my db
service state as running.
- on service start I'm checking if any postgres.exe is running on my
cluster directory, and I'm sending SIGTERM to it, and waiting for process
terminate
- stopping service: before I will send SIGINT signal to postgres, first I
wait for my main service appication to close clearly.
- my DB service also creates database cluster (initdb.exe) - because it's
too complicated to run initdb.exe on NetworkService while running on user
session (I know that I could use cacls/icacls - and I did before, but I had
customers who had mess in ACLs on installation drive)

Best regards,
Krystian Bigaj

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Fabio Milano 2014-07-24 19:40:07 Re: Standby Server Bus 7 error
Previous Message Thomas Kellerer 2014-07-24 19:23:22 Re: copy/dump database to text/csv files

Browse pgsql-hackers by date

  From Date Subject
Next Message Robert Haas 2014-07-24 19:36:32 Re: Verbose output of pg_dump not show schema name
Previous Message Robert Haas 2014-07-24 19:32:06 Re: Least Active Transaction ID function