diff -Nacr a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c *** a/src/backend/postmaster/postmaster.c Tue Sep 20 05:17:08 2016 --- b/src/backend/postmaster/postmaster.c Tue Oct 4 18:02:42 2016 *************** *** 272,277 **** --- 272,278 ---- static int Shutdown = NoShutdown; static bool FatalError = false; /* T if recovering from backend crash */ + static bool ClosedSockets = false; /* T if recovering from backend crash */ /* * We use a simple state machine to control startup, shutdown, and *************** *** 379,384 **** --- 380,386 ---- static void checkDataDir(void); static Port *ConnCreate(int serverFd); static void ConnFree(Port *port); + static void ClosePostmasterSockets(void); static void reset_shared(int port); static void SIGHUP_handler(SIGNAL_ARGS); static void pmdie(SIGNAL_ARGS); *************** *** 1633,1639 **** */ memcpy((char *) &rmask, (char *) &readmask, sizeof(fd_set)); ! if (pmState == PM_WAIT_DEAD_END) { PG_SETMASK(&UnBlockSig); --- 1635,1641 ---- */ memcpy((char *) &rmask, (char *) &readmask, sizeof(fd_set)); ! if (pmState == PM_WAIT_DEAD_END || ClosedSockets) { PG_SETMASK(&UnBlockSig); *************** *** 2378,2383 **** --- 2380,2405 ---- /* + * ClosePostmasterSockets -- close all the postmaster's listen sockets + */ + static void + ClosePostmasterSockets(void) + { + int i; + + for (i = 0; i < MAXLISTEN; i++) + { + if (ListenSocket[i] != PGINVALID_SOCKET) + { + StreamClose(ListenSocket[i]); + ListenSocket[i] = PGINVALID_SOCKET; + } + } + + ClosedSockets = true; + } + + /* * ClosePostmasterPorts -- close all the postmaster's open sockets * * This is called during child process startup to release file descriptors *************** *** 2390,2397 **** void ClosePostmasterPorts(bool am_syslogger) { - int i; - #ifndef WIN32 /* --- 2412,2417 ---- *************** *** 2407,2420 **** #endif /* Close the listen sockets */ ! for (i = 0; i < MAXLISTEN; i++) ! { ! if (ListenSocket[i] != PGINVALID_SOCKET) ! { ! StreamClose(ListenSocket[i]); ! ListenSocket[i] = PGINVALID_SOCKET; ! } ! } /* If using syslogger, close the read side of the pipe */ if (!am_syslogger) --- 2427,2433 ---- #endif /* Close the listen sockets */ ! ClosePostmasterSockets(); /* If using syslogger, close the read side of the pipe */ if (!am_syslogger) *************** *** 2670,2675 **** --- 2683,2694 ---- AbortStartTime = time(NULL); /* + * Close the listen sockets to avoid wasting resources by creating + * dead-end children, so that postmaster stops as fast as possible. + */ + ClosePostmasterSockets(); + + /* * Now wait for backends to exit. If there are none, * PostmasterStateMachine will take the next step. */ *************** *** 3210,3215 **** --- 3229,3243 ---- LogChildExit(LOG, procname, pid, exitstatus); ereport(LOG, (errmsg("terminating any other active server processes"))); + + /* + * If the startup process failed, or the user does not want an automatic + * restart after backend crashes, close the listen sockets to avoid wasting + * resources by creating dead-end children, so that postmaster stops as + * fast as possible. + */ + if (StartupStatus == STARTUP_CRASHED || !restart_after_crash) + ClosePostmasterSockets(); } /* Process background workers. */