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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
|
/* -*-pgsql-c-*- */
/*
* $Header$
*
* pgpool: a language independent connection pool server for PostgreSQL
* written by Tatsuo Ishii
*
* Copyright (c) 2003-2019 PgPool Global Development Group
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby
* granted, provided that the above copyright notice appear in all
* copies and that both that copyright notice and this permission
* notice appear in supporting documentation, and that the name of the
* author not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior
* permission. The author makes no representations about the
* suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
*---------------------------------------------------------------------
* pool_proto2.c: modules corresponding to protocol 2.0.
* used by pool_process_query()
*---------------------------------------------------------------------
*/
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include "pool.h"
#include "pool_config.h"
#include "protocol/pool_proto_modules.h"
#include "utils/pool_stream.h"
#include "utils/elog.h"
POOL_STATUS
AsciiRow(POOL_CONNECTION * frontend,
POOL_CONNECTION_POOL * backend,
short num_fields)
{
static char nullmap[8192],
nullmap1[8192];
int nbytes;
int i,
j;
unsigned char mask;
int size,
size1 = 0;
char *buf = NULL,
*sendbuf = NULL;
char msgbuf[1024];
pool_write(frontend, "D", 1);
nbytes = (num_fields + 7) / 8;
if (nbytes <= 0)
return POOL_CONTINUE;
/* NULL map */
pool_read(MAIN(backend), nullmap, nbytes);
memcpy(nullmap1, nullmap, nbytes);
for (i = 0; i < NUM_BACKENDS; i++)
{
if (VALID_BACKEND(i) && !IS_MAIN_NODE_ID(i))
{
pool_read(CONNECTION(backend, i), nullmap, nbytes);
if (memcmp(nullmap, nullmap1, nbytes))
{
/*
* XXX: NULLMAP maybe different among backends. If we were a
* paranoid, we have to treat this as a fatal error. However
* in the real world we'd better to adapt this situation. Just
* throw a log...
*/
ereport(DEBUG1,
(errmsg("processing ASCII row"),
errdetail("NULLMAP is different between main and backend no %d", i)));
}
}
}
if (pool_write(frontend, nullmap1, nbytes) < 0)
return POOL_END;
mask = 0;
for (i = 0; i < num_fields; i++)
{
if (mask == 0)
mask = 0x80;
/* NOT NULL? */
if (mask & nullmap[i / 8])
{
/* field size */
if (pool_read(MAIN(backend), &size, sizeof(int)) < 0)
return POOL_END;
size1 = ntohl(size) - 4;
/* read and send actual data only when size > 0 */
if (size1 > 0)
{
sendbuf = pool_read2(MAIN(backend), size1);
if (sendbuf == NULL)
return POOL_END;
}
/* forward to frontend */
pool_write(frontend, &size, sizeof(int));
if (size1 > 0)
pool_write(frontend, sendbuf, size1);
snprintf(msgbuf, Min(sizeof(msgbuf), size1 + 1), "%s", sendbuf);
ereport(DEBUG1,
(errmsg("processing ASCII row"),
errdetail("len: %d data: %s", size1, msgbuf)));
for (j = 0; j < NUM_BACKENDS; j++)
{
if (VALID_BACKEND(j) && !IS_MAIN_NODE_ID(j))
{
/* field size */
if (pool_read(CONNECTION(backend, j), &size, sizeof(int)) < 0)
return POOL_END;
buf = NULL;
size = ntohl(size) - 4;
/*
* XXX: field size maybe different among backends. If we
* were a paranoid, we have to treat this as a fatal
* error. However in the real world we'd better to adapt
* this situation. Just throw a log...
*/
if (size != size1)
ereport(DEBUG1,
(errmsg("processing ASCII row"),
errdetail("size of field no %d does not match between main [size:%d] and backend no %d [size:%d]",
i, ntohl(size), j, ntohl(size1))));
/* read and send actual data only when size > 0 */
if (size > 0)
{
buf = pool_read2(CONNECTION(backend, j), size);
if (buf == NULL)
return POOL_END;
}
}
}
}
mask >>= 1;
}
if (pool_flush(frontend))
return POOL_END;
return POOL_CONTINUE;
}
POOL_STATUS
BinaryRow(POOL_CONNECTION * frontend,
POOL_CONNECTION_POOL * backend,
short num_fields)
{
static char nullmap[8192],
nullmap1[8192];
int nbytes;
int i,
j;
unsigned char mask;
int size,
size1 = 0;
char *buf = NULL;
pool_write(frontend, "B", 1);
nbytes = (num_fields + 7) / 8;
if (nbytes <= 0)
return POOL_CONTINUE;
/* NULL map */
pool_read(MAIN(backend), nullmap, nbytes);
if (pool_write(frontend, nullmap, nbytes) < 0)
return POOL_END;
memcpy(nullmap1, nullmap, nbytes);
for (i = 0; i < NUM_BACKENDS; i++)
{
if (VALID_BACKEND(i) && !IS_MAIN_NODE_ID(i))
{
pool_read(CONNECTION(backend, i), nullmap, nbytes);
if (memcmp(nullmap, nullmap1, nbytes))
{
/*
* XXX: NULLMAP maybe different among backends. If we were a
* paranoid, we have to treat this as a fatal error. However
* in the real world we'd better to adapt this situation. Just
* throw a log...
*/
ereport(DEBUG1,
(errmsg("processing binary row"),
errdetail("NULLMAP is different between main and backend no %d", i)));
}
}
}
mask = 0;
for (i = 0; i < num_fields; i++)
{
if (mask == 0)
mask = 0x80;
/* NOT NULL? */
if (mask & nullmap[i / 8])
{
/* field size */
if (pool_read(MAIN(backend), &size, sizeof(int)) < 0)
return POOL_END;
for (j = 0; j < NUM_BACKENDS; j++)
{
if (VALID_BACKEND(j) && !IS_MAIN_NODE_ID(j))
{
/* field size */
if (pool_read(CONNECTION(backend, i), &size, sizeof(int)) < 0)
return POOL_END;
/*
* XXX: field size maybe different among backends. If we
* were a paranoid, we have to treat this as a fatal
* error. However in the real world we'd better to adapt
* this situation. Just throw a log...
*/
if (size != size1)
ereport(DEBUG1,
(errmsg("processing binary row"),
errdetail("size of field no %d does not match between main [size:%d] and backend no %d [size:%d]",
i, ntohl(size), j, ntohl(size1))));
}
buf = NULL;
/* forward to frontend */
if (IS_MAIN_NODE_ID(j))
pool_write(frontend, &size, sizeof(int));
size = ntohl(size) - 4;
/* read and send actual data only when size > 0 */
if (size > 0)
{
buf = pool_read2(CONNECTION(backend, j), size);
if (buf == NULL)
return POOL_END;
if (IS_MAIN_NODE_ID(j))
{
pool_write(frontend, buf, size);
}
}
}
mask >>= 1;
}
}
if (pool_flush(frontend))
return POOL_END;
return POOL_CONTINUE;
}
POOL_STATUS
CompletedResponse(POOL_CONNECTION * frontend,
POOL_CONNECTION_POOL * backend)
{
int i;
char *string = NULL;
char *string1 = NULL;
int len,
len1 = 0;
/* read command tag */
string = pool_read_string(MAIN(backend), &len, 0);
if (string == NULL)
return POOL_END;
else if (!strncmp(string, "BEGIN", 5))
TSTATE(backend, MAIN_NODE_ID) = 'T';
else if (!strncmp(string, "COMMIT", 6) || !strncmp(string, "ROLLBACK", 8))
TSTATE(backend, MAIN_NODE_ID) = 'I';
len1 = len;
string1 = pstrdup(string);
for (i = 0; i < NUM_BACKENDS; i++)
{
if (!VALID_BACKEND(i) || IS_MAIN_NODE_ID(i))
continue;
/* read command tag */
string = pool_read_string(CONNECTION(backend, i), &len, 0);
if (string == NULL)
ereport(FATAL,
(return_code(2),
errmsg("unable to process completed response"),
errdetail("read from backend node %d failed", i)));
else if (!strncmp(string, "BEGIN", 5))
TSTATE(backend, i) = 'T';
else if (!strncmp(string, "COMMIT", 6) || !strncmp(string, "ROLLBACK", 8))
TSTATE(backend, i) = 'I';
if (len != len1)
{
ereport(DEBUG1,
(errmsg("processing completed response"),
errdetail("message length does not match between main(%d \"%s\",) and %d th server (%d \"%s\",)",
len, string, i, len1, string1)));
/* we except INSERT, because INSERT response has OID */
if (strncmp(string1, "INSERT", 6))
{
pfree(string1);
return POOL_END;
}
}
}
/* forward to the frontend */
pool_write(frontend, "C", 1);
ereport(DEBUG2,
(errmsg("processing completed response"),
errdetail("string: \"%s\"", string1)));
pool_write(frontend, string1, len1);
pfree(string1);
return pool_flush(frontend);
}
POOL_STATUS
CursorResponse(POOL_CONNECTION * frontend,
POOL_CONNECTION_POOL * backend)
{
char *string = NULL;
char *string1 = NULL;
int len,
len1 = 0;
int i;
/* read cursor name */
string = pool_read_string(MAIN(backend), &len, 0);
if (string == NULL)
return POOL_END;
len1 = len;
string1 = pstrdup(string);
for (i = 0; i < NUM_BACKENDS; i++)
{
if (VALID_BACKEND(i) && !IS_MAIN_NODE_ID(i))
{
/* read cursor name */
string = pool_read_string(CONNECTION(backend, i), &len, 0);
if (string == NULL)
ereport(FATAL,
(return_code(2),
errmsg("unable to process cursor response"),
errdetail("read failed on backend node %d", i)));
if (len != len1)
{
ereport(FATAL,
(return_code(2),
errmsg("unable to process cursor response"),
errdetail("length does not match between main(%d) and %d th backend(%d)", len, i, len1),
errhint("main(%s) %d th backend(%s)", string1, i, string)));
}
}
}
/* forward to the frontend */
pool_write(frontend, "P", 1);
pool_write(frontend, string1, len1);
pool_flush(frontend);
pfree(string1);
return POOL_CONTINUE;
}
void
EmptyQueryResponse(POOL_CONNECTION * frontend,
POOL_CONNECTION_POOL * backend)
{
char c;
int i;
for (i = 0; i < NUM_BACKENDS; i++)
{
if (VALID_BACKEND(i))
{
pool_read(CONNECTION(backend, i), &c, sizeof(c));
}
}
pool_write(frontend, "I", 1);
pool_write_and_flush(frontend, "", 1);
}
POOL_STATUS
ErrorResponse(POOL_CONNECTION * frontend,
POOL_CONNECTION_POOL * backend)
{
char *string = "";
int len = 0;
int i;
for (i = 0; i < NUM_BACKENDS; i++)
{
if (VALID_BACKEND(i))
{
/* read error message */
string = pool_read_string(CONNECTION(backend, i), &len, 0);
if (string == NULL)
return POOL_END;
}
}
/* forward to the frontend */
pool_write(frontend, "E", 1);
pool_write_and_flush(frontend, string, len);
/*
* check session context, because this function is called by pool_do_auth
* too.
*/
if (pool_get_session_context(true))
raise_intentional_error_if_need(backend);
/* change transaction state */
for (i = 0; i < NUM_BACKENDS; i++)
{
if (VALID_BACKEND(i))
{
if (TSTATE(backend, i) == 'T')
TSTATE(backend, i) = 'E';
}
}
return POOL_CONTINUE;
}
POOL_STATUS
FunctionResultResponse(POOL_CONNECTION * frontend,
POOL_CONNECTION_POOL * backend)
{
char dummy;
int len;
char *result = NULL;
int i;
pool_write(frontend, "V", 1);
for (i = 0; i < NUM_BACKENDS; i++)
{
if (VALID_BACKEND(i))
{
pool_read(CONNECTION(backend, i), &dummy, 1);
}
}
pool_write(frontend, &dummy, 1);
/* non empty result? */
if (dummy == 'G')
{
for (i = 0; i < NUM_BACKENDS; i++)
{
if (VALID_BACKEND(i))
{
/* length of result in bytes */
pool_read(CONNECTION(backend, i), &len, sizeof(len));
}
}
pool_write(frontend, &len, sizeof(len));
len = ntohl(len);
for (i = 0; i < NUM_BACKENDS; i++)
{
if (VALID_BACKEND(i))
{
/* result value itself */
if ((result = pool_read2(MAIN(backend), len)) == NULL)
ereport(FATAL,
(return_code(2),
errmsg("unable to process function result response"),
errdetail("reading from backend node %d failed", i)));
}
}
if (result)
pool_write(frontend, result, len);
else
ereport(FATAL,
(return_code(2),
errmsg("unable to process function result response"),
errdetail("reading from backend node failed")));
}
for (i = 0; i < NUM_BACKENDS; i++)
{
if (VALID_BACKEND(i))
{
/* unused ('0') */
pool_read(MAIN(backend), &dummy, 1);
}
}
pool_write(frontend, "0", 1);
return pool_flush(frontend);
}
void
NoticeResponse(POOL_CONNECTION * frontend,
POOL_CONNECTION_POOL * backend)
{
char *string = NULL;
int len = 0;
int i;
for (i = 0; i < NUM_BACKENDS; i++)
{
if (VALID_BACKEND(i))
{
/* read notice message */
string = pool_read_string(CONNECTION(backend, i), &len, 0);
if (string == NULL)
ereport(FATAL,
(return_code(2),
errmsg("unable to process Notice response"),
errdetail("reading from backend node %d failed", i)));
}
}
/* forward to the frontend */
pool_write(frontend, "N", 1);
if (string == NULL)
ereport(FATAL,
(return_code(2),
errmsg("unable to process Notice response"),
errdetail("reading from backend node failed")));
else
pool_write_and_flush(frontend, string, len);
}
POOL_STATUS
NotificationResponse(POOL_CONNECTION * frontend,
POOL_CONNECTION_POOL * backend)
{
int pid,
pid1;
char *condition,
*condition1 = NULL;
int len,
len1 = 0;
int i;
pool_write(frontend, "A", 1);
for (i = 0; i < NUM_BACKENDS; i++)
{
if (VALID_BACKEND(i))
{
pool_read(CONNECTION(backend, i), &pid, sizeof(pid));
condition = pool_read_string(CONNECTION(backend, i), &len, 0);
if (condition == NULL)
ereport(FATAL,
(return_code(2),
errmsg("unable to process Notification response"),
errdetail("reading from backend node %d failed", i)));
if (IS_MAIN_NODE_ID(i))
{
pid1 = pid;
len1 = len;
if (condition1)
pfree(condition1);
condition1 = pstrdup(condition);
}
}
}
pool_write(frontend, &pid1, sizeof(pid1));
pool_write_and_flush(frontend, condition1, len1);
pfree(condition1);
return POOL_CONTINUE;
}
int
RowDescription(POOL_CONNECTION * frontend,
POOL_CONNECTION_POOL * backend,
short *result)
{
short num_fields,
num_fields1 = 0;
int oid,
mod;
int oid1,
mod1;
short size,
size1;
char *string;
int len,
len1;
int i;
pool_read(MAIN(backend), &num_fields, sizeof(short));
num_fields1 = num_fields;
for (i = 0; i < NUM_BACKENDS; i++)
{
if (VALID_BACKEND(i) && !IS_MAIN_NODE_ID(i))
{
/* # of fields (could be 0) */
pool_read(CONNECTION(backend, i), &num_fields, sizeof(short));
if (num_fields != num_fields1)
{
ereport(FATAL,
(return_code(2),
errmsg("unable to process row description"),
errdetail("num_fields does not match between backends main(%d) and %d th backend(%d)",
num_fields, i, num_fields1)));
}
}
}
/* forward it to the frontend */
pool_write(frontend, "T", 1);
pool_write(frontend, &num_fields, sizeof(short));
num_fields = ntohs(num_fields);
for (i = 0; i < num_fields; i++)
{
int j;
/* field name */
string = pool_read_string(MAIN(backend), &len, 0);
if (string == NULL)
return POOL_END;
len1 = len;
pool_write(frontend, string, len);
for (j = 0; j < NUM_BACKENDS; j++)
{
if (VALID_BACKEND(j) && !IS_MAIN_NODE_ID(j))
{
string = pool_read_string(CONNECTION(backend, j), &len, 0);
if (string == NULL)
ereport(FATAL,
(return_code(2),
errmsg("unable to process row description"),
errdetail("read failed on backend node %d", j)));
if (len != len1)
{
ereport(FATAL,
(return_code(2),
errmsg("unable to process row description"),
errdetail("field length does not match between backends main(%d) and %d th backend(%d)",
ntohl(len), j, ntohl(len1))));
}
}
}
/* type oid */
pool_read(MAIN(backend), &oid, sizeof(int));
oid1 = oid;
ereport(DEBUG1,
(errmsg("processing ROW DESCRIPTION"),
errdetail("type oid: %d", ntohl(oid))));
for (j = 0; j < NUM_BACKENDS; j++)
{
if (VALID_BACKEND(j) && !IS_MAIN_NODE_ID(j))
{
pool_read(CONNECTION(backend, j), &oid, sizeof(int));
/* we do not regard oid mismatch as fatal */
if (oid != oid1)
{
ereport(DEBUG1,
(errmsg("processing ROW DESCRIPTION"),
errdetail("field oid does not match between backends main(%d) and %d th backend(%d)",
ntohl(oid), j, ntohl(oid1))));
}
}
}
pool_write(frontend, &oid1, sizeof(int));
/* size */
pool_read(MAIN(backend), &size, sizeof(short));
size1 = size;
for (j = 0; j < NUM_BACKENDS; j++)
{
if (VALID_BACKEND(j) && !IS_MAIN_NODE_ID(j))
{
pool_read(CONNECTION(backend, j), &size, sizeof(short));
if (size1 != size)
{
ereport(FATAL,
(errmsg("data among backends are different"),
errdetail("field size does not match between backends main(%d) and %d th backend(%d", ntohs(size), j, ntohs(size1))));
}
}
}
ereport(DEBUG1,
(errmsg("processing ROW DESCRIPTION"),
errdetail("field size: %d", ntohs(size))));
pool_write(frontend, &size1, sizeof(short));
/* modifier */
pool_read(MAIN(backend), &mod, sizeof(int));
ereport(DEBUG1,
(errmsg("processing ROW DESCRIPTION"),
errdetail("modifier: %d", ntohs(mod))));
mod1 = mod;
for (j = 0; j < NUM_BACKENDS; j++)
{
if (VALID_BACKEND(j) && !IS_MAIN_NODE_ID(j))
{
pool_read(CONNECTION(backend, j), &mod, sizeof(int));
if (mod != mod1)
{
ereport(DEBUG1,
(errmsg("processing ROW DESCRIPTION"),
errdetail("modifier does not match between backends main(%d) and %d th backend(%d)",
ntohl(mod), j, ntohl(mod1))));
}
}
}
pool_write(frontend, &mod1, sizeof(int));
}
*result = num_fields;
return pool_flush(frontend);
}
|