1--- postgresql-9.1.1/src/backend/libpq/auth.c	2011-09-22 16:57:57.000000000 -0500
2+++ postgresql/src/backend/libpq/auth.c	2011-10-10 13:30:45.000000000 -0500
3@@ -20,6 +20,7 @@
4 #include <netinet/in.h>
5 #include <arpa/inet.h>
6 #include <unistd.h>
7+#include <poll.h>
8 
9 #include "libpq/auth.h"
10 #include "libpq/crypt.h"
11@@ -2454,7 +2455,7 @@
12 	struct addrinfo *serveraddrs;
13 	char		portstr[128];
14 	ACCEPT_TYPE_ARG3 addrsize;
15-	fd_set		fdset;
16+	struct pollfd	pfd;
17 	struct timeval endtime;
18 	int			i,
19 				r;
20@@ -2621,10 +2622,11 @@
21 	 */
22 	gettimeofday(&endtime, NULL);
23 	endtime.tv_sec += RADIUS_TIMEOUT;
24+	pfd.fd = sock;
25+	pfd.events = POLLIN;
26 
27 	while (true)
28 	{
29-		struct timeval timeout;
30 		struct timeval now;
31 		int64		timeoutval;
32 
33@@ -2637,13 +2639,10 @@
34 			closesocket(sock);
35 			return STATUS_ERROR;
36 		}
37-		timeout.tv_sec = timeoutval / 1000000;
38-		timeout.tv_usec = timeoutval % 1000000;
39 
40-		FD_ZERO(&fdset);
41-		FD_SET(sock, &fdset);
42-
43-		r = select(sock + 1, &fdset, NULL, NULL, &timeout);
44+		pfd.revents = 0;
45+		/* timeoutval is in usecs; poll(2) takes msecs. */
46+		r = poll(&pfd, 1, (timeoutval / 1000));
47 		if (r < 0)
48 		{
49 			if (errno == EINTR)
50diff -aur postgresql-9.2.0/src/backend/postmaster/pgstat.c postgresql/src/backend/postmaster/pgstat.c
51--- postgresql-9.2.0/src/backend/postmaster/pgstat.c	2012-09-06 14:26:17.000000000 -0700
52+++ postgresql/src/backend/postmaster/pgstat.c	2012-09-13 17:33:49.000000000 -0700
53@@ -28,6 +28,7 @@
54 #include <arpa/inet.h>
55 #include <signal.h>
56 #include <time.h>
57+#include <poll.h>
58 
59 #include "pgstat.h"
60 
61@@ -307,10 +308,9 @@
62 			   *addr,
63 				hints;
64 	int			ret;
65-	fd_set		rset;
66-	struct timeval tv;
67+	struct pollfd pfd;
68 	char		test_byte;
69-	int			sel_res;
70+	int			poll_res;
71 	int			tries = 0;
72 
73 #define TESTBYTEVAL ((char) 199)
74@@ -435,25 +435,24 @@
75 		 */
76 		for (;;)				/* need a loop to handle EINTR */
77 		{
78-			FD_ZERO(&rset);
79-			FD_SET(pgStatSock, &rset);
80+			pfd.fd = pgStatSock;
81+			pfd.events = POLLIN;
82+			pfd.revents = 0;
83 
84-			tv.tv_sec = 0;
85-			tv.tv_usec = 500000;
86-			sel_res = select(pgStatSock + 1, &rset, NULL, NULL, &tv);
87-			if (sel_res >= 0 || errno != EINTR)
88+			poll_res = poll(&pfd, 1, 500);
89+			if (poll_res >= 0 || errno != EINTR)
90 				break;
91 		}
92-		if (sel_res < 0)
93+		if (poll_res < 0)
94 		{
95 			ereport(LOG,
96 					(errcode_for_socket_access(),
97-					 errmsg("select() failed in statistics collector: %m")));
98+					errmsg("poll() failed in statistics collector: %m")));
99 			closesocket(pgStatSock);
100 			pgStatSock = PGINVALID_SOCKET;
101 			continue;
102 		}
103-		if (sel_res == 0 || !FD_ISSET(pgStatSock, &rset))
104+		if (poll_res == 0 || (pfd.revents & POLLIN) == 0)
105 		{
106 			/*
107 			 * This is the case we actually think is likely, so take pains to
108diff -aur postgresql-9.0.2/src/backend/postmaster/postmaster.c postgresql/src/backend/postmaster/postmaster.c
109--- postgresql-9.0.2/src/backend/postmaster/postmaster.c	2010-12-13 18:55:50.000000000 -0800
110+++ postgresql/src/backend/postmaster/postmaster.c	2010-12-14 12:36:07.000000000 -0800
111@@ -79,6 +79,7 @@
112 #include <arpa/inet.h>
113 #include <netdb.h>
114 #include <limits.h>
115+#include <poll.h>
116 
117 #ifdef HAVE_SYS_SELECT_H
118 #include <sys/select.h>
119@@ -347,7 +348,7 @@
120 static int	BackendStartup(Port *port);
121 static int	ProcessStartupPacket(Port *port, bool SSLdone);
122 static void processCancelRequest(Port *port, void *pkt);
123-static int	initMasks(fd_set *rmask);
124+static int	initPollfds(struct pollfd *fds);
125 static void report_fork_failure_to_client(Port *port, int errnum);
126 static CAC_state canAcceptConnections(void);
127 static long PostmasterRandom(void);
128@@ -1347,19 +1348,18 @@
129 static int
130 ServerLoop(void)
131 {
132-	fd_set		readmask;
133+	struct pollfd pfds[MAXLISTEN];
134 	int			nSockets;
135 	time_t		now,
136 				last_touch_time;
137 
138 	last_touch_time = time(NULL);
139 
140-	nSockets = initMasks(&readmask);
141+	nSockets = initPollfds(pfds);
142 
143 	for (;;)
144 	{
145-		fd_set		rmask;
146-		int			selres;
147+		int			pollres;
148 
149 		/*
150 		 * Wait for a connection request to arrive.
151@@ -1371,24 +1371,17 @@
152 		 * any new connections, so we don't call select() at all; just sleep
153 		 * for a little bit with signals unblocked.
154 		 */
155-		memcpy((char *) &rmask, (char *) &readmask, sizeof(fd_set));
156 
157 		PG_SETMASK(&UnBlockSig);
158 
159 		if (pmState == PM_WAIT_DEAD_END)
160 		{
161 			pg_usleep(100000L); /* 100 msec seems reasonable */
162-			selres = 0;
163+			pollres = 0;
164 		}
165 		else
166 		{
167-			/* must set timeout each time; some OSes change it! */
168-			struct timeval timeout;
169-
170-			timeout.tv_sec = 60;
171-			timeout.tv_usec = 0;
172-
173-			selres = select(nSockets, &rmask, NULL, NULL, &timeout);
174+			pollres = poll(pfds, nSockets, 60 * 1000);
175 		}
176 
177 		/*
178@@ -1397,14 +1390,14 @@
179 		 */
180 		PG_SETMASK(&BlockSig);
181 
182-		/* Now check the select() result */
183-		if (selres < 0)
184+		/* Now check the poll() result */
185+		if (pollres < 0)
186 		{
187 			if (errno != EINTR && errno != EWOULDBLOCK)
188 			{
189 				ereport(LOG,
190 						(errcode_for_socket_access(),
191-						 errmsg("select() failed in postmaster: %m")));
192+						 errmsg("poll() failed in postmaster: %m")));
193 				return STATUS_ERROR;
194 			}
195 		}
196@@ -1413,7 +1406,7 @@
197 		 * New connection pending on any of our sockets? If so, fork a child
198 		 * process to deal with it.
199 		 */
200-		if (selres > 0)
201+		if (pollres > 0)
202 		{
203 			int			i;
204 
205@@ -1421,7 +1414,7 @@
206 			{
207 				if (ListenSocket[i] == PGINVALID_SOCKET)
208 					break;
209-				if (FD_ISSET(ListenSocket[i], &rmask))
210+				if (pfds[i].revents & POLLIN)
211 				{
212 					Port	   *port;
213 
214@@ -1505,30 +1498,27 @@
215 
216 
217 /*
218- * Initialise the masks for select() for the ports we are listening on.
219+ * Initialise the pollfds for poll() for the ports we are listening on.
220  * Return the number of sockets to listen on.
221  */
222 static int
223-initMasks(fd_set *rmask)
224+initPollfds(struct pollfd *pfds)
225 {
226-	int			maxsock = -1;
227 	int			i;
228 
229-	FD_ZERO(rmask);
230-
231 	for (i = 0; i < MAXLISTEN; i++)
232 	{
233 		int			fd = ListenSocket[i];
234 
235 		if (fd == PGINVALID_SOCKET)
236 			break;
237-		FD_SET(fd, rmask);
238-
239-		if (fd > maxsock)
240-			maxsock = fd;
241+		pfds->fd = fd;
242+		pfds->events = POLLIN;
243+		pfds->revents = 0;
244+		pfds++;
245 	}
246 
247-	return maxsock + 1;
248+	return i;
249 }
250
251
252diff -ur postgresql-9.0.1/src/port/pgsleep.c postgresql/src/port/pgsleep.c
253--- postgresql-9.0.1/src/port/pgsleep.c.orig	2010-01-02 10:58:13.000000000 -0600
254+++ postgresql/src/port/pgsleep.c	2010-10-02 17:06:56.000000000 -0500
255@@ -36,11 +36,7 @@
256 	if (microsec > 0)
257 	{
258 #ifndef WIN32
259-		struct timeval delay;
260-
261-		delay.tv_sec = microsec / 1000000L;
262-		delay.tv_usec = microsec % 1000000L;
263-		(void) select(0, NULL, NULL, NULL, &delay);
264+		usleep(microsec);
265 #else
266 		SleepEx((microsec < 500 ? 1 : (microsec + 500) / 1000), FALSE);
267 #endif
268