1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/*-------------------------------------------------------------------------
*
* scripts_parallel.c
* Parallel support for bin/scripts/
*
*
* Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* src/bin/scripts/scripts_parallel.c
*
*-------------------------------------------------------------------------
*/
#ifdef WIN32
#define FD_SETSIZE 1024 /* must set before winsock2.h is included */
#endif
#include "postgres_fe.h"
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#include "common.h"
#include "common/logging.h"
#include "fe_utils/cancel.h"
#include "scripts_parallel.h"
static void init_slot(ParallelSlot *slot, PGconn *conn);
static int select_loop(int maxFd, fd_set *workerset);
static void
init_slot(ParallelSlot *slot, PGconn *conn)
{
slot->connection = conn;
/* Initially assume connection is idle */
slot->isFree = true;
}
/*
* Wait until a file descriptor from the given set becomes readable.
*
* Returns the number of ready descriptors, or -1 on failure (including
* getting a cancel request).
*/
static int
select_loop(int maxFd, fd_set *workerset)
{
int i;
fd_set saveSet = *workerset;
if (CancelRequested)
return -1;
for (;;)
{
/*
* On Windows, we need to check once in a while for cancel requests;
* on other platforms we rely on select() returning when interrupted.
*/
struct timeval *tvp;
#ifdef WIN32
struct timeval tv = {0, 1000000};
tvp = &tv;
#else
tvp = NULL;
#endif
*workerset = saveSet;
i = select(maxFd + 1, workerset, NULL, NULL, tvp);
#ifdef WIN32
if (i == SOCKET_ERROR)
{
i = -1;
if (WSAGetLastError() == WSAEINTR)
errno = EINTR;
}
#endif
if (i < 0 && errno == EINTR)
continue; /* ignore this */
if (i < 0 || CancelRequested)
return -1; /* but not this */
if (i == 0)
continue; /* timeout (Win32 only) */
break;
}
return i;
}
/*
* ParallelSlotsGetIdle
* Return a connection slot that is ready to execute a command.
*
* This returns the first slot we find that is marked isFree, if one is;
* otherwise, we loop on select() until one socket becomes available. When
* this happens, we read the whole set and mark as free all sockets that
* become available. If an error occurs, NULL is returned.
*/
ParallelSlot *
ParallelSlotsGetIdle(ParallelSlot *slots, int numslots)
{
int i;
int firstFree = -1;
/*
* Look for any connection currently free. If there is one, mark it as
* taken and let the caller know the slot to use.
*/
for (i = 0; i < numslots; i++)
{
if (slots[i].isFree)
{
slots[i].isFree = false;
return slots + i;
}
}
/*
* No free slot found, so wait until one of the connections has finished
* its task and return the available slot.
*/
while (firstFree < 0)
{
fd_set slotset;
int maxFd = 0;
/* We must reconstruct the fd_set for each call to select_loop */
FD_ZERO(&slotset);
for (i = 0; i < numslots; i++)
{
int sock = PQsocket(slots[i].connection);
/*
* We don't really expect any connections to lose their sockets
* after startup, but just in case, cope by ignoring them.
*/
if (sock < 0)
continue;
FD_SET(sock, &slotset);
if (sock > maxFd)
maxFd = sock;
}
SetCancelConn(slots->connection);
i = select_loop(maxFd, &slotset);
ResetCancelConn();
/* failure? */
if (i < 0)
return NULL;
for (i = 0; i < numslots; i++)
{
int sock = PQsocket(slots[i].connection);
if (sock >= 0 && FD_ISSET(sock, &slotset))
{
/* select() says input is available, so consume it */
PQconsumeInput(slots[i].connection);
}
/* Collect result(s) as long as any are available */
while (!PQisBusy(slots[i].connection))
{
PGresult *result = PQgetResult(slots[i].connection);
if (result != NULL)
{
/* Check and discard the command result */
if (!processQueryResult(slots[i].connection, result))
return NULL;
}
else
{
/* This connection has become idle */
slots[i].isFree = true;
if (firstFree < 0)
firstFree = i;
break;
}
}
}
}
slots[firstFree].isFree = false;
return slots + firstFree;
}
/*
* ParallelSlotsSetup
* Prepare a set of parallel slots to use on a given database.
*
* This creates and initializes a set of connections to the database
* using the information given by the caller, marking all parallel slots
* as free and ready to use. "conn" is an initial connection set up
* by the caller and is associated with the first slot in the parallel
* set.
*/
ParallelSlot *
ParallelSlotsSetup(const ConnParams *cparams,
const char *progname, bool echo,
PGconn *conn, int numslots)
{
ParallelSlot *slots;
int i;
Assert(conn != NULL);
slots = (ParallelSlot *) pg_malloc(sizeof(ParallelSlot) * numslots);
init_slot(slots, conn);
if (numslots > 1)
{
for (i = 1; i < numslots; i++)
{
conn = connectDatabase(cparams, progname, echo, false, true);
/*
* Fail and exit immediately if trying to use a socket in an
* unsupported range. POSIX requires open(2) to use the lowest
* unused file descriptor and the hint given relies on that.
*/
if (PQsocket(conn) >= FD_SETSIZE)
{
pg_log_fatal("too many jobs for this platform -- try %d", i);
exit(1);
}
init_slot(slots + i, conn);
}
}
return slots;
}
/*
* ParallelSlotsTerminate
* Clean up a set of parallel slots
*
* Iterate through all connections in a given set of ParallelSlots and
* terminate all connections.
*/
void
ParallelSlotsTerminate(ParallelSlot *slots, int numslots)
{
int i;
for (i = 0; i < numslots; i++)
{
PGconn *conn = slots[i].connection;
if (conn == NULL)
continue;
disconnectDatabase(conn);
}
}
/*
* ParallelSlotsWaitCompletion
*
* Wait for all connections to finish, returning false if at least one
* error has been found on the way.
*/
bool
ParallelSlotsWaitCompletion(ParallelSlot *slots, int numslots)
{
int i;
for (i = 0; i < numslots; i++)
{
if (!consumeQueryResult((slots + i)->connection))
return false;
}
return true;
}
|