Re: System shutdown signal on Windows (was Re: ) - Mailing list pgsql-general

From Krystian Bigaj
Subject Re: System shutdown signal on Windows (was Re: )
Date
Msg-id CAN=kAeH7Tutr+RF=61djWPoa6AtHN1K05dzOO6LhGoChLzns_Q@mail.gmail.com
Whole thread Raw
In response to Re: System shutdown signal on Windows (was Re: )  (Kalai R <softlinne.kv@gmail.com>)
List pgsql-general
On 24 July 2014 07:51, Kalai R <softlinne.kv@gmail.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

pgsql-general by date:

Previous
From: Thomas Kellerer
Date:
Subject: Re: copy/dump database to text/csv files
Next
From: Fabio Milano
Date:
Subject: Re: Standby Server Bus 7 error