Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 36c1895

Browse files
committed
Fix postmaster to not try to start more than MaxBackendId children,
per patch from Tatsuo Ishii
1 parent bfa6d51 commit 36c1895

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.99 1999/01/17 06:18:34 momjian Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.100 1999/01/30 20:04:37 tgl Exp $
1414
*
1515
* NOTES
1616
*
@@ -244,6 +244,7 @@ static int initMasks(fd_set *rmask, fd_set *wmask);
244244
static long PostmasterRandom(void);
245245
static void RandomSalt(char *salt);
246246
static void SignalChildren(SIGNAL_ARGS);
247+
static int CountChildren(void);
247248

248249
#ifdef CYR_RECODE
249250
void GetCharSetByHost(char *, int, char *);
@@ -667,8 +668,8 @@ ServerLoop(void)
667668
{
668669
if (errno == EINTR)
669670
continue;
670-
fprintf(stderr, "%s: ServerLoop: select failed\n",
671-
progname);
671+
fprintf(stderr, "%s: ServerLoop: select failed: %s\n",
672+
progname, strerror(errno));
672673
return STATUS_ERROR;
673674
}
674675

@@ -763,19 +764,24 @@ ServerLoop(void)
763764

764765
if (status == STATUS_OK && port->pktInfo.state == Idle)
765766
{
766-
767-
/*
768-
* If the backend start fails then keep the connection
769-
* open to report it. Otherwise, pretend there is an
770-
* error to close the connection which will now be managed
771-
* by the backend.
772-
*/
773-
774-
if (BackendStartup(port) != STATUS_OK)
767+
/* Can't start backend if max backend count is exceeded. */
768+
if (CountChildren() >= MaxBackendId)
775769
PacketSendError(&port->pktInfo,
776-
"Backend startup failed");
770+
"Sorry, too many clients already");
777771
else
778-
status = STATUS_ERROR;
772+
{
773+
/*
774+
* If the backend start fails then keep the connection
775+
* open to report it. Otherwise, pretend there is an
776+
* error to close the connection which will now be managed
777+
* by the backend.
778+
*/
779+
if (BackendStartup(port) != STATUS_OK)
780+
PacketSendError(&port->pktInfo,
781+
"Backend startup failed");
782+
else
783+
status = STATUS_ERROR;
784+
}
779785
}
780786

781787
/* Close the connection if required. */
@@ -1332,8 +1338,8 @@ BackendStartup(Port *port)
13321338
/* in parent */
13331339
if (pid < 0)
13341340
{
1335-
fprintf(stderr, "%s: BackendStartup: fork failed\n",
1336-
progname);
1341+
fprintf(stderr, "%s: BackendStartup: fork failed: %s\n",
1342+
progname, strerror(errno));
13371343
return STATUS_ERROR;
13381344
}
13391345

@@ -1641,3 +1647,23 @@ PostmasterRandom(void)
16411647

16421648
return random() ^ random_seed;
16431649
}
1650+
1651+
/*
1652+
* Count up number of child processes.
1653+
*/
1654+
static int
1655+
CountChildren(void)
1656+
{
1657+
Dlelem *curr;
1658+
Backend *bp;
1659+
int mypid = getpid();
1660+
int cnt = 0;
1661+
1662+
for (curr = DLGetHead(BackendList); curr; curr = DLGetSucc(curr))
1663+
{
1664+
bp = (Backend *) DLE_VAL(curr);
1665+
if (bp->pid != mypid)
1666+
cnt++;
1667+
}
1668+
return cnt;
1669+
}

0 commit comments

Comments
 (0)