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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
|
/*-------------------------------------------------------------------------
*
* parallel_slot.c
* Parallel support for front-end parallel database connections
*
*
* Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* src/fe_utils/parallel_slot.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/logging.h"
#include "fe_utils/cancel.h"
#include "fe_utils/parallel_slot.h"
#include "fe_utils/query_utils.h"
#define ERRCODE_UNDEFINED_TABLE "42P01"
static int select_loop(int maxFd, fd_set *workerset);
static bool processQueryResult(ParallelSlot *slot, PGresult *result);
/*
* Process (and delete) a query result. Returns true if there's no problem,
* false otherwise. It's up to the handler to decide what constitutes a
* problem.
*/
static bool
processQueryResult(ParallelSlot *slot, PGresult *result)
{
Assert(slot->handler != NULL);
/* On failure, the handler should return NULL after freeing the result */
if (!slot->handler(result, slot->connection, slot->handler_context))
return false;
/* Ok, we have to free it ourself */
PQclear(result);
return true;
}
/*
* Consume all the results generated for the given connection until
* nothing remains. If at least one error is encountered, return false.
* Note that this will block if the connection is busy.
*/
static bool
consumeQueryResult(ParallelSlot *slot)
{
bool ok = true;
PGresult *result;
SetCancelConn(slot->connection);
while ((result = PQgetResult(slot->connection)) != NULL)
{
if (!processQueryResult(slot, result))
ok = false;
}
ResetCancelConn();
return ok;
}
/*
* 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;
}
/*
* Return the offset of a suitable idle slot, or -1 if none are available. If
* the given dbname is not null, only idle slots connected to the given
* database are considered suitable, otherwise all idle connected slots are
* considered suitable.
*/
static int
find_matching_idle_slot(const ParallelSlotArray *sa, const char *dbname)
{
int i;
for (i = 0; i < sa->numslots; i++)
{
if (sa->slots[i].inUse)
continue;
if (sa->slots[i].connection == NULL)
continue;
if (dbname == NULL ||
strcmp(PQdb(sa->slots[i].connection), dbname) == 0)
return i;
}
return -1;
}
/*
* Return the offset of the first slot without a database connection, or -1 if
* all slots are connected.
*/
static int
find_unconnected_slot(const ParallelSlotArray *sa)
{
int i;
for (i = 0; i < sa->numslots; i++)
{
if (sa->slots[i].inUse)
continue;
if (sa->slots[i].connection == NULL)
return i;
}
return -1;
}
/*
* Return the offset of the first idle slot, or -1 if all slots are busy.
*/
static int
find_any_idle_slot(const ParallelSlotArray *sa)
{
int i;
for (i = 0; i < sa->numslots; i++)
if (!sa->slots[i].inUse)
return i;
return -1;
}
/*
* Wait for any slot's connection to have query results, consume the results,
* and update the slot's status as appropriate. Returns true on success,
* false on cancellation, on error, or if no slots are connected.
*/
static bool
wait_on_slots(ParallelSlotArray *sa)
{
int i;
fd_set slotset;
int maxFd = 0;
PGconn *cancelconn = NULL;
/* We must reconstruct the fd_set for each call to select_loop */
FD_ZERO(&slotset);
for (i = 0; i < sa->numslots; i++)
{
int sock;
/* We shouldn't get here if we still have slots without connections */
Assert(sa->slots[i].connection != NULL);
sock = PQsocket(sa->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;
/* Keep track of the first valid connection we see. */
if (cancelconn == NULL)
cancelconn = sa->slots[i].connection;
FD_SET(sock, &slotset);
if (sock > maxFd)
maxFd = sock;
}
/*
* If we get this far with no valid connections, processing cannot
* continue.
*/
if (cancelconn == NULL)
return false;
SetCancelConn(sa->slots->connection);
i = select_loop(maxFd, &slotset);
ResetCancelConn();
/* failure? */
if (i < 0)
return false;
for (i = 0; i < sa->numslots; i++)
{
int sock;
sock = PQsocket(sa->slots[i].connection);
if (sock >= 0 && FD_ISSET(sock, &slotset))
{
/* select() says input is available, so consume it */
PQconsumeInput(sa->slots[i].connection);
}
/* Collect result(s) as long as any are available */
while (!PQisBusy(sa->slots[i].connection))
{
PGresult *result = PQgetResult(sa->slots[i].connection);
if (result != NULL)
{
/* Handle and discard the command result */
if (!processQueryResult(&sa->slots[i], result))
return false;
}
else
{
/* This connection has become idle */
sa->slots[i].inUse = false;
ParallelSlotClearHandler(&sa->slots[i]);
break;
}
}
}
return true;
}
/*
* Open a new database connection using the stored connection parameters and
* optionally a given dbname if not null, execute the stored initial command if
* any, and associate the new connection with the given slot.
*/
static void
connect_slot(ParallelSlotArray *sa, int slotno, const char *dbname)
{
const char *old_override;
ParallelSlot *slot = &sa->slots[slotno];
old_override = sa->cparams->override_dbname;
if (dbname)
sa->cparams->override_dbname = dbname;
slot->connection = connectDatabase(sa->cparams, sa->progname, sa->echo, false, true);
sa->cparams->override_dbname = old_override;
if (PQsocket(slot->connection) >= FD_SETSIZE)
{
pg_log_fatal("too many jobs for this platform");
exit(1);
}
/* Setup the connection using the supplied command, if any. */
if (sa->initcmd)
executeCommand(slot->connection, sa->initcmd, sa->echo);
}
/*
* ParallelSlotsGetIdle
* Return a connection slot that is ready to execute a command.
*
* The slot returned is chosen as follows:
*
* If any idle slot already has an open connection, and if either dbname is
* null or the existing connection is to the given database, that slot will be
* returned allowing the connection to be reused.
*
* Otherwise, if any idle slot is not yet connected to any database, the slot
* will be returned with it's connection opened using the stored cparams and
* optionally the given dbname if not null.
*
* Otherwise, if any idle slot exists, an idle slot will be chosen and returned
* after having it's connection disconnected and reconnected using the stored
* cparams and optionally the given dbname if not null.
*
* Otherwise, if any slots have connections that are busy, 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. We then select a
* slot using the same rules as above.
*
* Otherwise, we cannot return a slot, which is an error, and NULL is returned.
*
* For any connection created, if the stored initcmd is not null, it will be
* executed as a command on the newly formed connection before the slot is
* returned.
*
* If an error occurs, NULL is returned.
*/
ParallelSlot *
ParallelSlotsGetIdle(ParallelSlotArray *sa, const char *dbname)
{
int offset;
Assert(sa);
Assert(sa->numslots > 0);
while (1)
{
/* First choice: a slot already connected to the desired database. */
offset = find_matching_idle_slot(sa, dbname);
if (offset >= 0)
{
sa->slots[offset].inUse = true;
return &sa->slots[offset];
}
/* Second choice: a slot not connected to any database. */
offset = find_unconnected_slot(sa);
if (offset >= 0)
{
connect_slot(sa, offset, dbname);
sa->slots[offset].inUse = true;
return &sa->slots[offset];
}
/* Third choice: a slot connected to the wrong database. */
offset = find_any_idle_slot(sa);
if (offset >= 0)
{
disconnectDatabase(sa->slots[offset].connection);
sa->slots[offset].connection = NULL;
connect_slot(sa, offset, dbname);
sa->slots[offset].inUse = true;
return &sa->slots[offset];
}
/*
* Fourth choice: block until one or more slots become available. If
* any slots hit a fatal error, we'll find out about that here and
* return NULL.
*/
if (!wait_on_slots(sa))
return NULL;
}
}
/*
* ParallelSlotsSetup
* Prepare a set of parallel slots but do not connect to any database.
*
* This creates and initializes a set of slots, marking all parallel slots as
* free and ready to use. Establishing connections is delayed until requesting
* a free slot. The cparams, progname, echo, and initcmd are stored for later
* use and must remain valid for the lifetime of the returned array.
*/
ParallelSlotArray *
ParallelSlotsSetup(int numslots, ConnParams *cparams, const char *progname,
bool echo, const char *initcmd)
{
ParallelSlotArray *sa;
Assert(numslots > 0);
Assert(cparams != NULL);
Assert(progname != NULL);
sa = (ParallelSlotArray *) palloc0(offsetof(ParallelSlotArray, slots) +
numslots * sizeof(ParallelSlot));
sa->numslots = numslots;
sa->cparams = cparams;
sa->progname = progname;
sa->echo = echo;
sa->initcmd = initcmd;
return sa;
}
/*
* ParallelSlotsAdoptConn
* Assign an open connection to the slots array for reuse.
*
* This turns over ownership of an open connection to a slots array. The
* caller should not further use or close the connection. All the connection's
* parameters (user, host, port, etc.) except possibly dbname should match
* those of the slots array's cparams, as given in ParallelSlotsSetup. If
* these parameters differ, subsequent behavior is undefined.
*/
void
ParallelSlotsAdoptConn(ParallelSlotArray *sa, PGconn *conn)
{
int offset;
offset = find_unconnected_slot(sa);
if (offset >= 0)
sa->slots[offset].connection = conn;
else
disconnectDatabase(conn);
}
/*
* ParallelSlotsTerminate
* Clean up a set of parallel slots
*
* Iterate through all connections in a given set of ParallelSlots and
* terminate all connections.
*/
void
ParallelSlotsTerminate(ParallelSlotArray *sa)
{
int i;
for (i = 0; i < sa->numslots; i++)
{
PGconn *conn = sa->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(ParallelSlotArray *sa)
{
int i;
for (i = 0; i < sa->numslots; i++)
{
if (sa->slots[i].connection == NULL)
continue;
if (!consumeQueryResult(&sa->slots[i]))
return false;
}
return true;
}
/*
* TableCommandResultHandler
*
* ParallelSlotResultHandler for results of commands (not queries) against
* tables.
*
* Requires that the result status is either PGRES_COMMAND_OK or an error about
* a missing table. This is useful for utilities that compile a list of tables
* to process and then run commands (vacuum, reindex, or whatever) against
* those tables, as there is a race condition between the time the list is
* compiled and the time the command attempts to open the table.
*
* For missing tables, logs an error but allows processing to continue.
*
* For all other errors, logs an error and terminates further processing.
*
* res: PGresult from the query executed on the slot's connection
* conn: connection belonging to the slot
* context: unused
*/
bool
TableCommandResultHandler(PGresult *res, PGconn *conn, void *context)
{
Assert(res != NULL);
Assert(conn != NULL);
/*
* If it's an error, report it. Errors about a missing table are harmless
* so we continue processing; but die for other errors.
*/
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
char *sqlState = PQresultErrorField(res, PG_DIAG_SQLSTATE);
pg_log_error("processing of database \"%s\" failed: %s",
PQdb(conn), PQerrorMessage(conn));
if (sqlState && strcmp(sqlState, ERRCODE_UNDEFINED_TABLE) != 0)
{
PQclear(res);
return false;
}
}
return true;
}
|