Skip to content

Commit e29c464

Browse files
committed
Refactor ListenSocket array.
Keep track of the used size of the array. That avoids looping through the whole array in a few places. It doesn't matter from a performance point of view since the array is small anyway, but this feels less surprising and is a little less code. Now that we have an explicit NumListenSockets variable that is statically initialized to 0, we don't need the loop to initialize the array. Allocate the array in PostmasterContext. The array isn't needed in child processes, so this allows reusing that memory. We could easily make the array resizable now, but we haven't heard any complaints about the current 64 sockets limit. Discussion: https://www.postgresql.org/message-id/7bb7ad65-a018-2419-742f-fa5fd877d338@iki.fi
1 parent 1c99cde commit e29c464

File tree

3 files changed

+40
-61
lines changed

3 files changed

+40
-61
lines changed

src/backend/libpq/pqcomm.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -311,16 +311,17 @@ socket_close(int code, Datum arg)
311311
* specified. For TCP ports, hostName is either NULL for all interfaces or
312312
* the interface to listen on, and unixSocketDir is ignored (can be NULL).
313313
*
314-
* Successfully opened sockets are added to the ListenSocket[] array (of
315-
* length MaxListen), at the first position that isn't PGINVALID_SOCKET.
314+
* Successfully opened sockets are appended to the ListenSockets[] array. On
315+
* entry, *NumListenSockets holds the number of elements currently in the
316+
* array, and it is updated to reflect the opened sockets. MaxListen is the
317+
* allocated size of the array.
316318
*
317319
* RETURNS: STATUS_OK or STATUS_ERROR
318320
*/
319-
320321
int
321322
StreamServerPort(int family, const char *hostName, unsigned short portNumber,
322323
const char *unixSocketDir,
323-
pgsocket ListenSocket[], int MaxListen)
324+
pgsocket ListenSockets[], int *NumListenSockets, int MaxListen)
324325
{
325326
pgsocket fd;
326327
int err;
@@ -335,7 +336,6 @@ StreamServerPort(int family, const char *hostName, unsigned short portNumber,
335336
struct addrinfo *addrs = NULL,
336337
*addr;
337338
struct addrinfo hint;
338-
int listen_index = 0;
339339
int added = 0;
340340
char unixSocketPath[MAXPGPATH];
341341
#if !defined(WIN32) || defined(IPV6_V6ONLY)
@@ -401,12 +401,7 @@ StreamServerPort(int family, const char *hostName, unsigned short portNumber,
401401
}
402402

403403
/* See if there is still room to add 1 more socket. */
404-
for (; listen_index < MaxListen; listen_index++)
405-
{
406-
if (ListenSocket[listen_index] == PGINVALID_SOCKET)
407-
break;
408-
}
409-
if (listen_index >= MaxListen)
404+
if (*NumListenSockets == MaxListen)
410405
{
411406
ereport(LOG,
412407
(errmsg("could not bind to all requested addresses: MAXLISTEN (%d) exceeded",
@@ -573,7 +568,8 @@ StreamServerPort(int family, const char *hostName, unsigned short portNumber,
573568
(errmsg("listening on %s address \"%s\", port %d",
574569
familyDesc, addrDesc, (int) portNumber)));
575570

576-
ListenSocket[listen_index] = fd;
571+
ListenSockets[*NumListenSockets] = fd;
572+
(*NumListenSockets)++;
577573
added++;
578574
}
579575

src/backend/postmaster/postmaster.c

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ int ReservedConnections;
227227

228228
/* The socket(s) we're listening to. */
229229
#define MAXLISTEN 64
230-
static pgsocket ListenSocket[MAXLISTEN];
230+
static int NumListenSockets = 0;
231+
static pgsocket *ListenSockets = NULL;
231232

232233
/* still more option variables */
233234
bool EnableSSL = false;
@@ -588,7 +589,6 @@ PostmasterMain(int argc, char *argv[])
588589
int status;
589590
char *userDoption = NULL;
590591
bool listen_addr_saved = false;
591-
int i;
592592
char *output_config_variable = NULL;
593593

594594
InitProcessGlobals();
@@ -1142,17 +1142,6 @@ PostmasterMain(int argc, char *argv[])
11421142
errmsg("could not remove file \"%s\": %m",
11431143
LOG_METAINFO_DATAFILE)));
11441144

1145-
/*
1146-
* Initialize input sockets.
1147-
*
1148-
* Mark them all closed, and set up an on_proc_exit function that's
1149-
* charged with closing the sockets again at postmaster shutdown.
1150-
*/
1151-
for (i = 0; i < MAXLISTEN; i++)
1152-
ListenSocket[i] = PGINVALID_SOCKET;
1153-
1154-
on_proc_exit(CloseServerPorts, 0);
1155-
11561145
/*
11571146
* If enabled, start up syslogger collection subprocess
11581147
*/
@@ -1187,7 +1176,13 @@ PostmasterMain(int argc, char *argv[])
11871176

11881177
/*
11891178
* Establish input sockets.
1179+
*
1180+
* First set up an on_proc_exit function that's charged with closing the
1181+
* sockets again at postmaster shutdown.
11901182
*/
1183+
ListenSockets = palloc(MAXLISTEN * sizeof(pgsocket));
1184+
on_proc_exit(CloseServerPorts, 0);
1185+
11911186
if (ListenAddresses)
11921187
{
11931188
char *rawstring;
@@ -1216,12 +1211,16 @@ PostmasterMain(int argc, char *argv[])
12161211
status = StreamServerPort(AF_UNSPEC, NULL,
12171212
(unsigned short) PostPortNumber,
12181213
NULL,
1219-
ListenSocket, MAXLISTEN);
1214+
ListenSockets,
1215+
&NumListenSockets,
1216+
MAXLISTEN);
12201217
else
12211218
status = StreamServerPort(AF_UNSPEC, curhost,
12221219
(unsigned short) PostPortNumber,
12231220
NULL,
1224-
ListenSocket, MAXLISTEN);
1221+
ListenSockets,
1222+
&NumListenSockets,
1223+
MAXLISTEN);
12251224

12261225
if (status == STATUS_OK)
12271226
{
@@ -1249,7 +1248,7 @@ PostmasterMain(int argc, char *argv[])
12491248

12501249
#ifdef USE_BONJOUR
12511250
/* Register for Bonjour only if we opened TCP socket(s) */
1252-
if (enable_bonjour && ListenSocket[0] != PGINVALID_SOCKET)
1251+
if (enable_bonjour && NumListenSockets > 0)
12531252
{
12541253
DNSServiceErrorType err;
12551254

@@ -1313,7 +1312,9 @@ PostmasterMain(int argc, char *argv[])
13131312
status = StreamServerPort(AF_UNIX, NULL,
13141313
(unsigned short) PostPortNumber,
13151314
socketdir,
1316-
ListenSocket, MAXLISTEN);
1315+
ListenSockets,
1316+
&NumListenSockets,
1317+
MAXLISTEN);
13171318

13181319
if (status == STATUS_OK)
13191320
{
@@ -1339,7 +1340,7 @@ PostmasterMain(int argc, char *argv[])
13391340
/*
13401341
* check that we have some socket to listen on
13411342
*/
1342-
if (ListenSocket[0] == PGINVALID_SOCKET)
1343+
if (NumListenSockets == 0)
13431344
ereport(FATAL,
13441345
(errmsg("no socket created for listening")));
13451346

@@ -1487,14 +1488,9 @@ CloseServerPorts(int status, Datum arg)
14871488
* before we remove the postmaster.pid lockfile; otherwise there's a race
14881489
* condition if a new postmaster wants to re-use the TCP port number.
14891490
*/
1490-
for (i = 0; i < MAXLISTEN; i++)
1491-
{
1492-
if (ListenSocket[i] != PGINVALID_SOCKET)
1493-
{
1494-
StreamClose(ListenSocket[i]);
1495-
ListenSocket[i] = PGINVALID_SOCKET;
1496-
}
1497-
}
1491+
for (i = 0; i < NumListenSockets; i++)
1492+
StreamClose(ListenSockets[i]);
1493+
NumListenSockets = 0;
14981494

14991495
/*
15001496
* Next, remove any filesystem entries for Unix sockets. To avoid race
@@ -1695,29 +1691,19 @@ DetermineSleepTime(void)
16951691
static void
16961692
ConfigurePostmasterWaitSet(bool accept_connections)
16971693
{
1698-
int nsockets;
1699-
17001694
if (pm_wait_set)
17011695
FreeWaitEventSet(pm_wait_set);
17021696
pm_wait_set = NULL;
17031697

1704-
/* How many server sockets do we need to wait for? */
1705-
nsockets = 0;
1706-
if (accept_connections)
1707-
{
1708-
while (nsockets < MAXLISTEN &&
1709-
ListenSocket[nsockets] != PGINVALID_SOCKET)
1710-
++nsockets;
1711-
}
1712-
1713-
pm_wait_set = CreateWaitEventSet(CurrentMemoryContext, 1 + nsockets);
1698+
pm_wait_set = CreateWaitEventSet(CurrentMemoryContext,
1699+
accept_connections ? (1 + NumListenSockets) : 1);
17141700
AddWaitEventToSet(pm_wait_set, WL_LATCH_SET, PGINVALID_SOCKET, MyLatch,
17151701
NULL);
17161702

17171703
if (accept_connections)
17181704
{
1719-
for (int i = 0; i < nsockets; i++)
1720-
AddWaitEventToSet(pm_wait_set, WL_SOCKET_ACCEPT, ListenSocket[i],
1705+
for (int i = 0; i < NumListenSockets; i++)
1706+
AddWaitEventToSet(pm_wait_set, WL_SOCKET_ACCEPT, ListenSockets[i],
17211707
NULL, NULL);
17221708
}
17231709
}
@@ -2579,14 +2565,11 @@ ClosePostmasterPorts(bool am_syslogger)
25792565
* EXEC_BACKEND mode.
25802566
*/
25812567
#ifndef EXEC_BACKEND
2582-
for (int i = 0; i < MAXLISTEN; i++)
2583-
{
2584-
if (ListenSocket[i] != PGINVALID_SOCKET)
2585-
{
2586-
StreamClose(ListenSocket[i]);
2587-
ListenSocket[i] = PGINVALID_SOCKET;
2588-
}
2589-
}
2568+
for (int i = 0; i < NumListenSockets; i++)
2569+
StreamClose(ListenSockets[i]);
2570+
NumListenSockets = 0;
2571+
pfree(ListenSockets);
2572+
ListenSockets = NULL;
25902573
#endif
25912574

25922575
/*

src/include/libpq/libpq.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ extern PGDLLIMPORT WaitEventSet *FeBeWaitSet;
6666

6767
extern int StreamServerPort(int family, const char *hostName,
6868
unsigned short portNumber, const char *unixSocketDir,
69-
pgsocket ListenSocket[], int MaxListen);
69+
pgsocket ListenSocket[], int *NumListenSockets, int MaxListen);
7070
extern int StreamConnection(pgsocket server_fd, Port *port);
7171
extern void StreamClose(pgsocket sock);
7272
extern void TouchSocketFiles(void);

0 commit comments

Comments
 (0)