1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-12 05:01:15 +03:00

Here's an attempt at new socket and signal code for win32.

It works on the principle of turning sockets into non-blocking, and then
emulate blocking behaviour on top of that, while allowing signals to
run. Signals are now implemented using an event instead of APCs, thus
getting rid of the issue of APCs not being compatible with "old style"
sockets functions.

It also moves the win32 specific code away from pqsignal.h/c into
port/win32, and also removes the "thread style workaround" of the APC
issue previously in place.

In order to make things work, a few things are also changed in pgstat.c:

1) There is now a separate pipe to the collector and the bufferer. This
is required because the pipe will otherwise only be signalled in one of
the processes when the postmaster goes down. The MS winsock code for
select() must have some kind of workaround for this behaviour, but I
have found no stable way of doing that. You really are not supposed to
use the same socket from more than one process (unless you use
WSADuplicateSocket(), in which case the docs specifically say that only
one will be flagged).

2) The check for "postmaster death" is moved into a separate select()
call after the main loop. The previous behaviour select():ed on the
postmaster pipe, while later explicitly saying "we do NOT check for
postmaster exit inside the loop".
The issue was that the code relies on the same select() call seeing both
the postmaster pipe *and* the pgstat pipe go away. This does not always
happen, and it appears that useing WSAEventSelect() makes it even more
common that it does not.
Since it's only called when the process exits, I don't think using a
separate select() call will have any significant impact on how the stats
collector works.

Magnus Hagander
This commit is contained in:
Bruce Momjian
2004-04-12 16:19:18 +00:00
parent abdabeb995
commit a4c40f140d
11 changed files with 831 additions and 374 deletions

View File

@@ -13,7 +13,7 @@
*
* Copyright (c) 2001-2003, PostgreSQL Global Development Group
*
* $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.65 2004/04/05 03:16:21 momjian Exp $
* $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.66 2004/04/12 16:19:18 momjian Exp $
* ----------
*/
#include "postgres.h"
@@ -83,6 +83,7 @@ NON_EXEC_STATIC int pgStatSock = -1;
static int pgStatPipe[2];
static struct sockaddr_storage pgStatAddr;
static int pgStatPmPipe[2] = {-1, -1};
static int pgStatCollectorPmPipe[2] = {-1, -1};
static int pgStatPid;
static time_t last_pgstat_start_time;
@@ -410,7 +411,7 @@ pgstat_init(void)
/*
* Create the pipe that controls the statistics collector shutdown
*/
if (pgpipe(pgStatPmPipe) < 0)
if (pgpipe(pgStatPmPipe) < 0 || pgpipe(pgStatCollectorPmPipe) < 0)
{
ereport(LOG,
(errcode_for_socket_access(),
@@ -451,9 +452,9 @@ static pid_t
pgstat_forkexec(STATS_PROCESS_TYPE procType)
{
pid_t pid;
char *av[13];
char *av[15];
int ac = 0, bufc = 0, i;
char pgstatBuf[10][MAXPGPATH];
char pgstatBuf[12][MAXPGPATH];
av[ac++] = "postgres";
switch (procType)
@@ -475,6 +476,8 @@ pgstat_forkexec(STATS_PROCESS_TYPE procType)
snprintf(pgstatBuf[bufc++],MAXPGPATH,"%d",pgStatSock);
snprintf(pgstatBuf[bufc++],MAXPGPATH,"%d",pgStatPmPipe[0]);
snprintf(pgstatBuf[bufc++],MAXPGPATH,"%d",pgStatPmPipe[1]);
snprintf(pgstatBuf[bufc++],MAXPGPATH,"%d",pgStatCollectorPmPipe[0]);
snprintf(pgstatBuf[bufc++],MAXPGPATH,"%d",pgStatCollectorPmPipe[1]);
snprintf(pgstatBuf[bufc++],MAXPGPATH,"%d",pgStatPipe[0]);
snprintf(pgstatBuf[bufc++],MAXPGPATH,"%d",pgStatPipe[1]);
@@ -518,11 +521,13 @@ pgstat_forkexec(STATS_PROCESS_TYPE procType)
static void
pgstat_parseArgs(PGSTAT_FORK_ARGS)
{
Assert(argc == 10);
Assert(argc == 12);
argc = 0;
pgStatSock = atoi(argv[argc++]);
pgStatPmPipe[0] = atoi(argv[argc++]);
pgStatPmPipe[1] = atoi(argv[argc++]);
pgStatCollectorPmPipe[0] = atoi(argv[argc++]);
pgStatCollectorPmPipe[1] = atoi(argv[argc++]);
pgStatPipe[0] = atoi(argv[argc++]);
pgStatPipe[1] = atoi(argv[argc++]);
MaxBackends = atoi(argv[argc++]);
@@ -676,6 +681,12 @@ pgstat_close_sockets(void)
if (pgStatPmPipe[1] >= 0)
closesocket(pgStatPmPipe[1]);
pgStatPmPipe[1] = -1;
if (pgStatCollectorPmPipe[0] >= 0)
closesocket(pgStatCollectorPmPipe[0]);
pgStatCollectorPmPipe[0] = -1;
if (pgStatCollectorPmPipe[1] >= 0)
closesocket(pgStatCollectorPmPipe[1]);
pgStatCollectorPmPipe[1] = -1;
}
@@ -1491,6 +1502,8 @@ pgstat_main(PGSTAT_FORK_ARGS)
*/
closesocket(pgStatPmPipe[1]);
pgStatPmPipe[1] = -1;
closesocket(pgStatCollectorPmPipe[1]);
pgStatCollectorPmPipe[1] = -1;
/*
* Start a buffering process to read from the socket, so we have a
@@ -1547,7 +1560,6 @@ pgstat_mainChild(PGSTAT_FORK_ARGS)
fd_set rfds;
int readPipe;
int pmPipe;
int maxfd;
int nready;
int len = 0;
struct timeval timeout;
@@ -1564,7 +1576,7 @@ pgstat_mainChild(PGSTAT_FORK_ARGS)
closesocket(pgStatPipe[1]);
closesocket(pgStatSock);
pmPipe = pgStatPmPipe[0];
pmPipe = pgStatCollectorPmPipe[0];
/*
* In the child we can have default SIGCHLD handling (in case we want
@@ -1666,14 +1678,11 @@ pgstat_mainChild(PGSTAT_FORK_ARGS)
*/
FD_ZERO(&rfds);
FD_SET(readPipe, &rfds);
FD_SET(pmPipe, &rfds);
maxfd = Max(readPipe, pmPipe);
/*
* Now wait for something to do.
*/
nready = select(maxfd + 1, &rfds, NULL, NULL,
nready = select(readPipe+1, &rfds, NULL, NULL,
(need_statwrite) ? &timeout : NULL);
if (nready < 0)
{
@@ -1845,7 +1854,12 @@ pgstat_mainChild(PGSTAT_FORK_ARGS)
* is still open. If it is read-ready (ie, EOF), the postmaster must
* have quit.
*/
if (FD_ISSET(pmPipe, &rfds))
FD_ZERO(&rfds);
FD_SET(pmPipe, &rfds);
timeout.tv_sec = 0;
timeout.tv_usec = 0;
nready = select(pmPipe+1,&rfds,NULL,NULL,&timeout);
if (nready > 0 && FD_ISSET(pmPipe, &rfds))
pgstat_write_statsfile();
}

View File

@@ -37,7 +37,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.379 2004/03/24 15:20:54 tgl Exp $
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.380 2004/04/12 16:19:18 momjian Exp $
*
* NOTES
*
@@ -2017,35 +2017,6 @@ reaper_done:
}
#ifdef WIN32
/*
* On WIN32, we cannot use socket functions inside
* an APC (signal handler). If we do, select() will return
* with incorrect return values, causing the postmaster to
* enter a blocking accept(). We work around this by
* running it on a separate thread. We still block the main
* thread until it is done, so we don't scribble over any
* data from the wrong thread (pgstat functions aqre not
* thread safe).
*/
static DWORD WINAPI win32_pgstat_beterm_thread(LPVOID param)
{
pgstat_beterm((int)param);
return 0;
}
static void win32_pgstat_beterm(int pid) {
HANDLE beterm_thread = CreateThread(NULL, 64*1024, win32_pgstat_beterm_thread, (LPVOID)pid, 0, NULL);
if (!beterm_thread)
ereport(FATAL,
(errmsg_internal("failed to create beterm sender thread: %i", (int)GetLastError())));
if (WaitForSingleObject(beterm_thread,INFINITE) != WAIT_OBJECT_0)
ereport(FATAL,
(errmsg_internal("failed to wait for beterm sender thread: %i", (int)GetLastError())));
CloseHandle(beterm_thread);
}
#endif
/*
* CleanupProc -- cleanup after terminated backend.
*
@@ -2099,11 +2070,7 @@ CleanupProc(int pid,
else if (pid == BgWriterPID)
BgWriterPID = 0;
else
#ifndef WIN32
pgstat_beterm(pid);
#else
win32_pgstat_beterm(pid);
#endif
return;
}