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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
|
/* -*-pgsql-c-*- */
/*
*
* pgpool: a language independent connection pool server for PostgreSQL
* written by Tatsuo Ishii
*
* Copyright (c) 2003-2024 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.
*
*/
#include "pool.h"
#include "pool_config.h"
#include "protocol/pool_proto_modules.h"
#include "protocol/pool_process_query.h"
#include "protocol/pool_pg_utils.h"
#include "utils/palloc.h"
#include "utils/memutils.h"
#include "utils/elog.h"
#include "utils/statistics.h"
#include "utils/pool_select_walker.h"
#include "utils/pool_stream.h"
#include "context/pool_session_context.h"
#include "context/pool_query_context.h"
#include "parser/nodes.h"
#include <string.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
/*
* Where to send query
*/
typedef enum
{
POOL_PRIMARY,
POOL_STANDBY,
POOL_EITHER,
POOL_BOTH
} POOL_DEST;
#define CHECK_QUERY_CONTEXT_IS_VALID \
do { \
if (!query_context) \
ereport(ERROR, \
(errmsg("setting db node for query to be sent, no query context")));\
} while (0)
static POOL_DEST send_to_where(Node *node);
static void where_to_send_deallocate(POOL_QUERY_CONTEXT * query_context, Node *node);
static void where_to_send_main_replica(POOL_QUERY_CONTEXT * query_context, char *query, Node *node);
static void where_to_send_native_replication(POOL_QUERY_CONTEXT * query_context, char *query, Node *node);
static char *remove_read_write(int len, const char *contents, int *rewritten_len);
static void set_virtual_main_node(POOL_QUERY_CONTEXT *query_context);
static void set_load_balance_info(POOL_QUERY_CONTEXT *query_context);
static bool is_in_list(char *name, List *list);
static bool is_select_object_in_temp_write_list(Node *node, void *context);
static bool add_object_into_temp_write_list(Node *node, void *context);
static void dml_adaptive(Node *node, char *query);
static char* get_associated_object_from_dml_adaptive_relations
(char *left_token, DBObjectTypes object_type);
/*
* Create and initialize per query session context
*/
POOL_QUERY_CONTEXT *
pool_init_query_context(void)
{
MemoryContext memory_context = AllocSetContextCreate(QueryContext,
"QueryContextMemoryContext",
ALLOCSET_SMALL_MINSIZE,
ALLOCSET_SMALL_INITSIZE,
ALLOCSET_SMALL_MAXSIZE);
MemoryContext oldcontext = MemoryContextSwitchTo(memory_context);
POOL_QUERY_CONTEXT *qc;
qc = palloc0(sizeof(*qc));
qc->memory_context = memory_context;
MemoryContextSwitchTo(oldcontext);
return qc;
}
/*
* Destroy query context
*/
void
pool_query_context_destroy(POOL_QUERY_CONTEXT * query_context)
{
POOL_SESSION_CONTEXT *session_context;
if (query_context)
{
MemoryContext memory_context = query_context->memory_context;
ereport(DEBUG5,
(errmsg("pool_query_context_destroy: query context:%p query: \"%s\"",
query_context, query_context->original_query)));
session_context = pool_get_session_context(false);
pool_unset_query_in_progress();
if (!pool_is_command_success() && query_context->pg_terminate_backend_conn)
{
ereport(DEBUG1,
(errmsg("clearing the connection flag for pg_terminate_backend")));
pool_unset_connection_will_be_terminated(query_context->pg_terminate_backend_conn);
}
query_context->pg_terminate_backend_conn = NULL;
query_context->original_query = NULL;
session_context->query_context = NULL;
pfree(query_context);
MemoryContextDelete(memory_context);
}
}
/*
* Perform shallow copy of given query context. Used in parse_before_bind.
*/
POOL_QUERY_CONTEXT *
pool_query_context_shallow_copy(POOL_QUERY_CONTEXT * query_context)
{
POOL_QUERY_CONTEXT *qc;
MemoryContext memory_context;
qc = pool_init_query_context();
memory_context = qc->memory_context;
memcpy(qc, query_context, sizeof(POOL_QUERY_CONTEXT));
qc->memory_context = memory_context;
return qc;
}
/*
* Start query
*/
void
pool_start_query(POOL_QUERY_CONTEXT * query_context, char *query, int len, Node *node)
{
POOL_SESSION_CONTEXT *session_context;
if (query_context)
{
MemoryContext old_context;
session_context = pool_get_session_context(false);
old_context = MemoryContextSwitchTo(query_context->memory_context);
query_context->original_length = len;
query_context->rewritten_length = -1;
query_context->original_query = pstrdup(query);
query_context->rewritten_query = NULL;
query_context->parse_tree = node;
query_context->virtual_main_node_id = my_main_node_id;
query_context->load_balance_node_id = my_main_node_id;
query_context->is_cache_safe = false;
query_context->num_original_params = -1;
if (pool_config->memory_cache_enabled)
query_context->temp_cache = pool_create_temp_query_cache(query);
pool_set_query_in_progress();
query_context->skip_cache_commit = false;
query_context->atEnd = false;
query_context->partial_fetch = false;
session_context->query_context = query_context;
MemoryContextSwitchTo(old_context);
}
}
/*
* Specify DB node to send query
*/
void
pool_set_node_to_be_sent(POOL_QUERY_CONTEXT * query_context, int node_id)
{
CHECK_QUERY_CONTEXT_IS_VALID;
if (node_id < 0 || node_id >= MAX_NUM_BACKENDS)
ereport(ERROR,
(errmsg("setting db node for query to be sent, invalid node id:%d", node_id),
errdetail("backend node id: %d out of range, node id can be between 0 and %d", node_id, MAX_NUM_BACKENDS)));
query_context->where_to_send[node_id] = true;
return;
}
/*
* Unspecified DB node to send query
*/
void
pool_unset_node_to_be_sent(POOL_QUERY_CONTEXT * query_context, int node_id)
{
CHECK_QUERY_CONTEXT_IS_VALID;
if (node_id < 0 || node_id >= MAX_NUM_BACKENDS)
ereport(ERROR,
(errmsg("un setting db node for query to be sent, invalid node id:%d", node_id),
errdetail("backend node id: %d out of range, node id can be between 0 and %d", node_id, MAX_NUM_BACKENDS)));
query_context->where_to_send[node_id] = false;
return;
}
/*
* Clear DB node map
*/
void
pool_clear_node_to_be_sent(POOL_QUERY_CONTEXT * query_context)
{
CHECK_QUERY_CONTEXT_IS_VALID;
memset(query_context->where_to_send, false, sizeof(query_context->where_to_send));
return;
}
/*
* Set all DB node map entry
*/
void
pool_setall_node_to_be_sent(POOL_QUERY_CONTEXT * query_context)
{
int i;
POOL_SESSION_CONTEXT *sc;
sc = pool_get_session_context(false);
CHECK_QUERY_CONTEXT_IS_VALID;
for (i = 0; i < NUM_BACKENDS; i++)
{
if (private_backend_status[i] == CON_UP ||
(private_backend_status[i] == CON_CONNECT_WAIT))
{
if (SL_MODE)
{
/*
* If load balance mode is disabled, only send to the primary node.
* If primary node does not exist, send to the main node.
*/
if (!pool_config->load_balance_mode)
{
if (i == PRIMARY_NODE_ID ||
(PRIMARY_NODE_ID < 0 && MAIN_NODE_ID == i))
{
query_context->where_to_send[i] = true;
break;
}
continue;
}
else
/*
* If the node is not primary node nor load balance node,
* there's no point to send query except statement level
* load balance is enabled.
*/
if (!pool_config->statement_level_load_balance &&
i != PRIMARY_NODE_ID && i != sc->load_balance_node_id)
continue;
}
query_context->where_to_send[i] = true;
}
}
return;
}
/*
* Return true if multiple nodes are targets
*/
bool
pool_multi_node_to_be_sent(POOL_QUERY_CONTEXT * query_context)
{
int i;
int cnt = 0;
CHECK_QUERY_CONTEXT_IS_VALID;
for (i = 0; i < NUM_BACKENDS; i++)
{
if (((BACKEND_INFO(i)).backend_status == CON_UP ||
BACKEND_INFO((i)).backend_status == CON_CONNECT_WAIT) &&
query_context->where_to_send[i])
{
cnt++;
if (cnt > 1)
{
return true;
}
}
}
return false;
}
/*
* Return if the DB node is needed to send query
*/
bool
pool_is_node_to_be_sent(POOL_QUERY_CONTEXT * query_context, int node_id)
{
CHECK_QUERY_CONTEXT_IS_VALID;
if (node_id < 0 || node_id >= MAX_NUM_BACKENDS)
ereport(ERROR,
(errmsg("checking if db node is needed to be sent, invalid node id:%d", node_id),
errdetail("backend node id: %d out of range, node id can be between 0 and %d", node_id, MAX_NUM_BACKENDS)));
return query_context->where_to_send[node_id];
}
/*
* Returns true if the DB node is needed to send query.
* Intended to be called from VALID_BACKEND
*/
bool
pool_is_node_to_be_sent_in_current_query(int node_id)
{
POOL_SESSION_CONTEXT *sc;
if (RAW_MODE)
return node_id == REAL_MAIN_NODE_ID;
sc = pool_get_session_context(true);
if (!sc)
return true;
if (pool_is_query_in_progress() && sc->query_context)
{
return pool_is_node_to_be_sent(sc->query_context, node_id);
}
return true;
}
/*
* Returns virtual main DB node id,
*/
int
pool_virtual_main_db_node_id(void)
{
volatile POOL_REQUEST_INFO *my_req;
POOL_SESSION_CONTEXT *sc;
/*
* Check whether failover is in progress and we are child process.
* If so, we will wait for failover to finish.
*/
my_req = Req_info;
if (processType == PT_CHILD && my_req->switching)
{
#ifdef NOT_USED
POOL_SETMASK(&BlockSig);
ereport(WARNING,
(errmsg("failover/failback is in progress"),
errdetail("executing failover or failback on backend"),
errhint("In a moment you should be able to reconnect to the database")));
POOL_SETMASK(&UnBlockSig);
#endif
/*
* Wait for failover to finish
*/
if (wait_for_failover_to_finish() == -2)
/*
* Waiting for failover/failback to finish was timed out.
* Time to exit this process (and session disconnection).
*/
child_exit(POOL_EXIT_AND_RESTART);
}
sc = pool_get_session_context(true);
if (!sc)
{
/*
* We used to return REAL_MAIN_NODE_ID here. Problem with it is, it
* is possible that REAL_MAIN_NODE_ID could be changed
* anytime. Suppose REAL_MAIN_NODE_ID == my_main_node_id == 1. Then
* due to failback, REAL_MAIN_NODE_ID is changed to 0. Then
* MAIN_CONNECTION(cp) will return NULL and any reference to it will
* cause segmentation fault. To prevent the issue we should return
* my_main_node_id instead.
*/
return my_main_node_id;
}
if (sc->in_progress && sc->query_context)
{
int node_id = sc->query_context->virtual_main_node_id;
if (SL_MODE)
{
/*
* Make sure that virtual_main_node_id is either primary node id
* or load balance node id. If not, it is likely that
* virtual_main_node_id is not set up yet. Let's use the primary
* node id. except for the special case where we need to send the
* query to the node which is not primary nor the load balance
* node. Currently there is only one special such case that is
* handling of pg_terminate_backend() function, which may refer to
* the backend connection that is neither hosted by the primary or
* load balance node for current child process, but the query must
* be forwarded to that node. Since only that backend node can
* handle that pg_terminate_backend query
*
*/
ereport(DEBUG5,
(errmsg("pool_virtual_main_db_node_id: virtual_main_node_id:%d load_balance_node_id:%d PRIMARY_NODE_ID:%d",
node_id, sc->load_balance_node_id, PRIMARY_NODE_ID)));
if (node_id != sc->query_context->load_balance_node_id && node_id != PRIMARY_NODE_ID)
{
/*
* Only return the primary node id if we are not processing
* the pg_terminate_backend query
*/
if (sc->query_context->pg_terminate_backend_conn == NULL)
node_id = PRIMARY_NODE_ID;
}
}
return node_id;
}
/*
* No query context exists. If in streaming replication mode, returns primary node
* if exists. Otherwise returns my_main_node_id, which represents the
* last REAL_MAIN_NODE_ID.
*/
if (MAIN_REPLICA)
{
return PRIMARY_NODE_ID;
}
return my_main_node_id;
}
/*
* Set the destination for the current query to the specific backend node.
*/
void
pool_force_query_node_to_backend(POOL_QUERY_CONTEXT * query_context, int backend_id)
{
CHECK_QUERY_CONTEXT_IS_VALID;
ereport(DEBUG1,
(errmsg("forcing query destination node to backend node:%d", backend_id)));
pool_set_node_to_be_sent(query_context, backend_id);
set_virtual_main_node(query_context);
}
/*
* Decide where to send queries(thus expecting response)
*/
void
pool_where_to_send(POOL_QUERY_CONTEXT * query_context, char *query, Node *node)
{
CHECK_QUERY_CONTEXT_IS_VALID;
/*
* Zap out DB node map
*/
pool_clear_node_to_be_sent(query_context);
/*
* In raw mode, we send only to main node. Simple enough.
*/
if (RAW_MODE)
{
pool_set_node_to_be_sent(query_context, REAL_MAIN_NODE_ID);
}
else if (MAIN_REPLICA)
{
if (query_context->is_multi_statement)
{
/*
* If we are in streaming replication mode and we have multi statement query,
* we should send it to primary server only. Otherwise it is possible
* to send a write query to standby servers because we only use the
* first element of the multi statement query and don't care about the
* rest. Typical situation where we are bugged by this is,
* "BEGIN;DELETE FROM table;END". Note that from pgpool-II 3.1.0
* transactional statements such as "BEGIN" is unconditionally sent to
* all nodes(see send_to_where() for more details). Someday we might
* be able to understand all part of multi statement queries, but
* until that day we need this band aid.
*/
pool_set_node_to_be_sent(query_context, PRIMARY_NODE_ID);
}
else
where_to_send_main_replica(query_context, query, node);
}
else if (REPLICATION)
{
if (query_context->is_multi_statement)
{
pool_setall_node_to_be_sent(query_context);
}
else
where_to_send_native_replication(query_context, query, node);
}
else
{
ereport(WARNING,
(errmsg("unknown pgpool-II mode while deciding for where to send query")));
return;
}
/*
* DEALLOCATE or EXECUTE?
*/
if (IsA(node, DeallocateStmt) || IsA(node, ExecuteStmt))
{
where_to_send_deallocate(query_context, node);
}
/* Set virtual main node according to the where_to_send map. */
set_virtual_main_node(query_context);
return;
}
/*
* Send simple query and wait for response
* send_type:
* -1: do not send this node_id
* 0: send to all nodes
* >0: send to this node_id
*/
POOL_STATUS
pool_send_and_wait(POOL_QUERY_CONTEXT * query_context,
int send_type, int node_id)
{
POOL_SESSION_CONTEXT *session_context;
POOL_CONNECTION *frontend;
POOL_CONNECTION_POOL *backend;
bool is_commit;
bool is_begin_read_write;
int i;
int len;
char *string;
session_context = pool_get_session_context(false);
frontend = session_context->frontend;
backend = session_context->backend;
is_commit = is_commit_or_rollback_query(query_context->parse_tree);
is_begin_read_write = false;
len = 0;
string = NULL;
/*
* If the query is BEGIN READ WRITE or BEGIN ... SERIALIZABLE in
* streaming replication mode, we send BEGIN to standbys instead.
* The original_query which is BEGIN READ WRITE is sent to primary.
* The rewritten_query BEGIN is sent to standbys.
*/
if (pool_need_to_treat_as_if_default_transaction(query_context))
{
is_begin_read_write = true;
}
else
{
if (query_context->rewritten_query)
{
len = query_context->rewritten_length;
string = query_context->rewritten_query;
}
else
{
len = query_context->original_length;
string = query_context->original_query;
}
}
/* Send query */
for (i = 0; i < NUM_BACKENDS; i++)
{
if (!VALID_BACKEND(i))
continue;
else if (send_type < 0 && i == node_id)
continue;
else if (send_type > 0 && i != node_id)
continue;
/*
* If we are in streaming replication mode or logical replication mode,
* we do not send COMMIT/ABORT to standbys if it's in I (idle) state.
*/
if (is_commit && MAIN_REPLICA && !IS_MAIN_NODE_ID(i) && TSTATE(backend, i) == 'I')
{
pool_unset_node_to_be_sent(query_context, i);
continue;
}
/*
* If in reset context, we send COMMIT/ABORT to nodes those are not in
* I(idle) state. This will ensure that transactions are closed.
*/
if (is_commit && session_context->reset_context && TSTATE(backend, i) == 'I')
{
pool_unset_node_to_be_sent(query_context, i);
continue;
}
if (is_begin_read_write)
{
if (REAL_PRIMARY_NODE_ID == i)
{
len = query_context->original_length;
string = query_context->original_query;
}
else
{
len = query_context->rewritten_length;
string = query_context->rewritten_query;
}
}
per_node_statement_log(backend, i, string);
per_node_statement_notice(backend, i, string);
stat_count_up(i, query_context->parse_tree);
send_simplequery_message(CONNECTION(backend, i), len, string, MAJOR(backend));
}
/* Wait for response */
for (i = 0; i < NUM_BACKENDS; i++)
{
if (!VALID_BACKEND(i))
continue;
else if (send_type < 0 && i == node_id)
continue;
else if (send_type > 0 && i != node_id)
continue;
#ifdef NOT_USED
/*
* If in native replication mode, we do not send COMMIT/ABORT to
* standbys if it's in I(idle) state.
*/
if (is_commit && MAIN_REPLICA && !IS_MAIN_NODE_ID(i) && TSTATE(backend, i) == 'I')
{
continue;
}
#endif
if (is_begin_read_write)
{
if (REAL_PRIMARY_NODE_ID == i)
string = query_context->original_query;
else
string = query_context->rewritten_query;
}
wait_for_query_response_with_trans_cleanup(frontend,
CONNECTION(backend, i),
MAJOR(backend),
MAIN_CONNECTION(backend)->pid,
MAIN_CONNECTION(backend)->key);
/*
* Check if some error detected. If so, emit log. This is useful when
* invalid encoding error occurs. In this case, PostgreSQL does not
* report what statement caused that error and make users confused.
* Also set reset_query_error to true in ERROR case. This does
* anything in normal query processing but when processing reset
* queries, this is important because it might mean DISCARD ALL
* command fails. If so, we need to discard the connection cache so
* that any session object (i.e. named statement) does not remain in
* the last session.
*/
if (per_node_error_log(backend, i, string, "pool_send_and_wait: Error or notice message from backend", true) == 'E')
reset_query_error = true;
}
return POOL_CONTINUE;
}
/*
* Send extended query and wait for response
* send_type:
* -1: do not send this node_id
* 0: send to all nodes
* >0: send to this node_id
*/
POOL_STATUS
pool_extended_send_and_wait(POOL_QUERY_CONTEXT * query_context,
char *kind, int len, char *contents,
int send_type, int node_id, bool nowait)
{
POOL_SESSION_CONTEXT *session_context;
POOL_CONNECTION *frontend;
POOL_CONNECTION_POOL *backend;
bool is_commit;
bool is_begin_read_write;
int i;
int str_len;
int rewritten_len;
char *str;
char *rewritten_begin;
session_context = pool_get_session_context(false);
frontend = session_context->frontend;
backend = session_context->backend;
is_commit = is_commit_or_rollback_query(query_context->parse_tree);
is_begin_read_write = false;
str_len = 0;
rewritten_len = 0;
str = NULL;
rewritten_begin = NULL;
/*
* If the query is BEGIN READ WRITE or BEGIN ... SERIALIZABLE in
* streaming replication mode, we send BEGIN to standbys instead.
* The original_query which is BEGIN READ WRITE is sent to primary.
* The rewritten_query BEGIN is sent to standbys.
*/
if (pool_need_to_treat_as_if_default_transaction(query_context))
{
is_begin_read_write = true;
if (*kind == 'P')
rewritten_begin = remove_read_write(len, contents, &rewritten_len);
}
if (!rewritten_begin)
{
str_len = len;
str = contents;
}
/* Send query */
for (i = 0; i < NUM_BACKENDS; i++)
{
if (!VALID_BACKEND(i))
continue;
else if (send_type < 0 && i == node_id)
continue;
else if (send_type > 0 && i != node_id)
continue;
/*
* If in reset context, we send COMMIT/ABORT to nodes those are not in
* I(idle) state. This will ensure that transactions are closed.
*/
if (is_commit && session_context->reset_context && TSTATE(backend, i) == 'I')
{
pool_unset_node_to_be_sent(query_context, i);
continue;
}
if (rewritten_begin)
{
if (REAL_PRIMARY_NODE_ID == i)
{
str = contents;
str_len = len;
}
else
{
str = rewritten_begin;
str_len = rewritten_len;
}
}
if (pool_config->log_per_node_statement)
{
char msgbuf[QUERY_STRING_BUFFER_LEN];
char *stmt;
if (*kind == 'P' || *kind == 'E' || *kind == 'B')
{
if (query_context->rewritten_query)
{
if (is_begin_read_write)
{
if (REAL_PRIMARY_NODE_ID == i)
stmt = query_context->original_query;
else
stmt = query_context->rewritten_query;
}
else
{
stmt = query_context->rewritten_query;
}
}
else
{
stmt = query_context->original_query;
}
if (*kind == 'P')
snprintf(msgbuf, sizeof(msgbuf), "Parse: %s", stmt);
else if (*kind == 'B')
snprintf(msgbuf, sizeof(msgbuf), "Bind: %s", stmt);
else
snprintf(msgbuf, sizeof(msgbuf), "Execute: %s", stmt);
}
else
{
snprintf(msgbuf, sizeof(msgbuf), "%c message", *kind);
}
per_node_statement_log(backend, i, msgbuf);
per_node_statement_notice(backend, i, msgbuf);
}
/* if Execute message, count up stats count */
if (*kind == 'E')
{
stat_count_up(i, query_context->parse_tree);
}
send_extended_protocol_message(backend, i, kind, str_len, str);
if ((*kind == 'P' || *kind == 'E' || *kind == 'C') && STREAM)
{
/*
* Send flush message to backend to make sure that we get any
* response from backend in Streaming replication mode.
*/
POOL_CONNECTION *cp = CONNECTION(backend, i);
int len;
pool_write(cp, "H", 1);
len = htonl(sizeof(len));
pool_write_and_flush(cp, &len, sizeof(len));
ereport(DEBUG5,
(errmsg("pool_send_and_wait: send flush message to %d", i)));
}
}
if (!is_begin_read_write)
{
if (query_context->rewritten_query)
str = query_context->rewritten_query;
else
str = query_context->original_query;
}
if (!nowait)
{
/* Wait for response */
for (i = 0; i < NUM_BACKENDS; i++)
{
if (!VALID_BACKEND(i))
continue;
else if (send_type < 0 && i == node_id)
continue;
else if (send_type > 0 && i != node_id)
continue;
/*
* If in native replication mode, we do not send COMMIT/ABORT to
* standbys if it's in I(idle) state.
*/
if (is_commit && MAIN_REPLICA && !IS_MAIN_NODE_ID(i) && TSTATE(backend, i) == 'I')
{
continue;
}
if (is_begin_read_write)
{
if (REAL_PRIMARY_NODE_ID == i)
str = query_context->original_query;
else
str = query_context->rewritten_query;
}
wait_for_query_response_with_trans_cleanup(frontend,
CONNECTION(backend, i),
MAJOR(backend),
MAIN_CONNECTION(backend)->pid,
MAIN_CONNECTION(backend)->key);
/*
* Check if some error detected. If so, emit log. This is useful
* when invalid encoding error occurs. In this case, PostgreSQL
* does not report what statement caused that error and make users
* confused.
*/
per_node_error_log(backend, i, str, "pool_send_and_wait: Error or notice message from backend", true);
}
}
if (rewritten_begin)
pfree(rewritten_begin);
return POOL_CONTINUE;
}
/*
* From syntactically analysis decide the statement to be sent to the
* primary, the standby or either or both in native replication+HR/SR mode.
*/
static POOL_DEST send_to_where(Node *node)
{
/* From storage/lock.h */
#define NoLock 0
#define AccessShareLock 1 /* SELECT */
#define RowShareLock 2 /* SELECT FOR UPDATE/FOR SHARE */
#define RowExclusiveLock 3 /* INSERT, UPDATE, DELETE */
#define ShareUpdateExclusiveLock 4 /* VACUUM (non-FULL),ANALYZE, CREATE INDEX
* CONCURRENTLY */
#define ShareLock 5 /* CREATE INDEX (WITHOUT CONCURRENTLY) */
#define ShareRowExclusiveLock 6 /* like EXCLUSIVE MODE, but allows ROW
* SHARE */
#define ExclusiveLock 7 /* blocks ROW SHARE/SELECT...FOR UPDATE */
#define AccessExclusiveLock 8 /* ALTER TABLE, DROP TABLE, VACUUM FULL,
* and unqualified LOCK TABLE */
/*
* SELECT INTO SELECT FOR SHARE or UPDATE
*/
if (IsA(node, SelectStmt))
{
/* SELECT INTO or SELECT FOR SHARE or UPDATE ? */
if (pool_has_insertinto_or_locking_clause(node))
return POOL_PRIMARY;
/* non-SELECT query in WITH clause ? */
if (((SelectStmt *) node)->withClause)
{
List *ctes = ((SelectStmt *) node)->withClause->ctes;
ListCell *cte_item;
foreach(cte_item, ctes)
{
CommonTableExpr *cte = (CommonTableExpr *) lfirst(cte_item);
if (!IsA(cte->ctequery, SelectStmt))
return POOL_PRIMARY;
}
}
return POOL_EITHER;
}
/*
* COPY
*/
else if (IsA(node, CopyStmt))
{
if (((CopyStmt *) node)->is_from)
return POOL_PRIMARY;
else
{
if (((CopyStmt *) node)->query == NULL)
return POOL_EITHER;
else
return (IsA(((CopyStmt *) node)->query, SelectStmt)) ? POOL_EITHER : POOL_PRIMARY;
}
}
/*
* LOCK
*/
else if (IsA(node, LockStmt))
{
return (((LockStmt *) node)->mode >= RowExclusiveLock) ? POOL_PRIMARY : POOL_BOTH;
}
/*
* Transaction commands
*/
else if (IsA(node, TransactionStmt))
{
/*
* Check "BEGIN READ WRITE" "START TRANSACTION READ WRITE"
*/
if (is_start_transaction_query(node))
{
/*
* But actually, we send BEGIN to standby if it's BEGIN READ
* WRITE or START TRANSACTION READ WRITE
*/
if (is_read_write((TransactionStmt *) node))
return POOL_BOTH;
/*
* Other TRANSACTION start commands are sent to both primary
* and standby
*/
else
return POOL_BOTH;
}
/* SAVEPOINT related commands are sent to both primary and standby */
else if (is_savepoint_query(node))
{
if (SL_MODE && is_tx_started_by_multi_statement_query())
{
/*
* But in streaming replication mode, if a transaction was
* started by a multi statement query, SAVEPOINT should be
* sent to primary because the transaction was started on
* primary only.
*/
return POOL_PRIMARY;
}
return POOL_BOTH;
}
/*
* 2PC commands
*/
else if (is_2pc_transaction_query(node))
return POOL_PRIMARY;
else
/* COMMIT etc. */
return POOL_BOTH;
}
/*
* SET
*/
else if (IsA(node, VariableSetStmt))
{
ListCell *list_item;
bool ret = POOL_BOTH;
/*
* SET transaction_read_only TO off
*/
if (((VariableSetStmt *) node)->kind == VAR_SET_VALUE &&
!strcmp(((VariableSetStmt *) node)->name, "transaction_read_only"))
{
List *options = ((VariableSetStmt *) node)->args;
foreach(list_item, options)
{
A_Const *v = (A_Const *) lfirst(list_item);
switch (nodeTag(&v->val))
{
case T_String:
if (!strcasecmp(v->val.sval.sval, "off") ||
!strcasecmp(v->val.sval.sval, "f") ||
!strcasecmp(v->val.sval.sval, "false"))
ret = POOL_PRIMARY;
break;
case T_Integer:
if (v->val.ival.ival)
ret = POOL_PRIMARY;
default:
break;
}
}
return ret;
}
/*
* SET TRANSACTION ISOLATION LEVEL SERIALIZABLE or SET SESSION
* CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE or
* SET transaction_isolation TO 'serializable' SET
* default_transaction_isolation TO 'serializable'
*/
else if (is_set_transaction_serializable(node))
{
return POOL_PRIMARY;
}
/*
* Check "SET TRANSACTION READ WRITE" "SET SESSION CHARACTERISTICS
* AS TRANSACTION READ WRITE"
*/
else if (((VariableSetStmt *) node)->kind == VAR_SET_MULTI &&
(!strcmp(((VariableSetStmt *) node)->name, "TRANSACTION") ||
!strcmp(((VariableSetStmt *) node)->name, "SESSION CHARACTERISTICS")))
{
List *options = ((VariableSetStmt *) node)->args;
foreach(list_item, options)
{
DefElem *opt = (DefElem *) lfirst(list_item);
if (!strcmp("transaction_read_only", opt->defname))
{
bool read_only;
read_only = ((A_Const *) opt->arg)->val.ival.ival;
if (!read_only)
return POOL_PRIMARY;
}
}
return POOL_BOTH;
}
else
{
/*
* All other SET command sent to both primary and standby
*/
return POOL_BOTH;
}
}
/*
* DISCARD
*/
else if (IsA(node, DiscardStmt))
{
return POOL_BOTH;
}
/*
* PREPARE
*/
else if (IsA(node, PrepareStmt))
{
PrepareStmt *prepare_statement = (PrepareStmt *) node;
/* Note that this is a recursive call */
return send_to_where((Node *) (prepare_statement->query));
}
/*
* EXECUTE
*/
else if (IsA(node, ExecuteStmt))
{
/*
* This is a temporary decision. where_to_send will inherit same
* destination as PREPARE.
*/
return POOL_PRIMARY;
}
/*
* DEALLOCATE
*/
else if (IsA(node, DeallocateStmt))
{
/*
* This is temporary decision. where_to_send will inherit same
* destination AS PREPARE.
*/
return POOL_PRIMARY;
}
/*
* SHOW
*/
else if (IsA(node, VariableShowStmt))
{
return POOL_EITHER;
}
/*
* All other statements are sent to primary
*/
return POOL_PRIMARY;
}
/*
* Decide where to send given message.
* "node" must be a parse tree of either DEALLOCATE or EXECUTE.
*/
static
void
where_to_send_deallocate(POOL_QUERY_CONTEXT * query_context, Node *node)
{
DeallocateStmt *d = NULL;
ExecuteStmt *e = NULL;
char *name;
POOL_SENT_MESSAGE *msg;
if (IsA(node, DeallocateStmt))
{
d = (DeallocateStmt *) node;
name = d->name;
}
else if (IsA(node, ExecuteStmt))
{
e = (ExecuteStmt *) node;
name = e->name;
}
else
{
ereport(ERROR,
(errmsg("invalid node type for where_to_send_deallocate")));
return;
}
/* DEALLOCATE ALL? */
if (d && (name == NULL))
{
/* send to all backend node */
pool_setall_node_to_be_sent(query_context);
return;
}
/* ordinary DEALLOCATE or EXECUTE */
else
{
/* check if message was created by SQL PREPARE */
msg = pool_get_sent_message('Q', name, POOL_SENT_MESSAGE_CREATED);
if (!msg)
/* message may be created by Parse message */
msg = pool_get_sent_message('P', name, POOL_SENT_MESSAGE_CREATED);
if (msg)
{
/* Inherit same map from PREPARE or Parse */
pool_copy_prep_where(msg->query_context->where_to_send,
query_context->where_to_send);
/* copy load balance node id as well */
query_context->load_balance_node_id = msg->query_context->load_balance_node_id;
}
else
{
/*
* prepared statement was not found.
* There are two cases when this could happen.
* (1) mistakes by client. In this case backend will return ERROR
* anyway.
* (2) previous query was issued as multi-statement query. e.g.
* SELECT 1\;PREPARE foo AS SELECT 1;
* In this case pgpool does not know anything about the prepared
* statement "foo".
*/
if (SL_MODE)
{
/*
* In streaming replication or logical replication, sent to
* primary node only.
*/
pool_set_node_to_be_sent(query_context, PRIMARY_NODE_ID);
}
else
{
/*
* In other mode, sent to all node.
*/
pool_setall_node_to_be_sent(query_context);
}
}
}
}
/*
* Returns parse tree for current query.
* Precondition: the query is in progress state.
*/
Node *
pool_get_parse_tree(void)
{
POOL_SESSION_CONTEXT *sc;
sc = pool_get_session_context(true);
if (!sc)
return NULL;
if (pool_is_query_in_progress() && sc->query_context)
{
return sc->query_context->parse_tree;
}
return NULL;
}
/*
* Returns raw query string for current query.
* Precondition: the query is in progress state.
*/
char *
pool_get_query_string(void)
{
POOL_SESSION_CONTEXT *sc;
sc = pool_get_session_context(true);
if (!sc)
return NULL;
if (pool_is_query_in_progress() && sc->query_context)
{
return sc->query_context->original_query;
}
return NULL;
}
/*
* Returns true if the query is one of:
*
* SET TRANSACTION ISOLATION LEVEL SERIALIZABLE or
* SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE or
* SET transaction_isolation TO 'serializable'
* SET default_transaction_isolation TO 'serializable'
*/
bool
is_set_transaction_serializable(Node *node)
{
ListCell *list_item;
if (!IsA(node, VariableSetStmt))
return false;
if (((VariableSetStmt *) node)->kind == VAR_SET_VALUE &&
(!strcmp(((VariableSetStmt *) node)->name, "transaction_isolation") ||
!strcmp(((VariableSetStmt *) node)->name, "default_transaction_isolation")))
{
List *options = ((VariableSetStmt *) node)->args;
foreach(list_item, options)
{
A_Const *v = (A_Const *) lfirst(list_item);
switch (nodeTag(&v->val))
{
case T_String:
if (!strcasecmp(v->val.sval.sval, "serializable"))
return true;
break;
default:
break;
}
}
return false;
}
else if (((VariableSetStmt *) node)->kind == VAR_SET_MULTI &&
(!strcmp(((VariableSetStmt *) node)->name, "TRANSACTION") ||
!strcmp(((VariableSetStmt *) node)->name, "SESSION CHARACTERISTICS")))
{
List *options = ((VariableSetStmt *) node)->args;
foreach(list_item, options)
{
DefElem *opt = (DefElem *) lfirst(list_item);
if (!strcmp("transaction_isolation", opt->defname) ||
!strcmp("default_transaction_isolation", opt->defname))
{
A_Const *v = (A_Const *) opt->arg;
if (!strcasecmp(v->val.sval.sval, "serializable"))
return true;
}
}
}
return false;
}
/*
* Returns true if SQL is transaction starting command (START
* TRANSACTION or BEGIN)
*/
bool
is_start_transaction_query(Node *node)
{
TransactionStmt *stmt;
if (node == NULL || !IsA(node, TransactionStmt))
return false;
stmt = (TransactionStmt *) node;
return stmt->kind == TRANS_STMT_START || stmt->kind == TRANS_STMT_BEGIN;
}
/*
* Return true if start transaction query with "READ WRITE" option.
*/
bool
is_read_write(TransactionStmt *node)
{
ListCell *list_item;
List *options = node->options;
foreach(list_item, options)
{
DefElem *opt = (DefElem *) lfirst(list_item);
if (!strcmp("transaction_read_only", opt->defname))
{
bool read_only;
read_only = ((A_Const *) opt->arg)->val.ival.ival;
if (read_only)
return false; /* TRANSACTION READ ONLY */
else
/*
* TRANSACTION READ WRITE specified. This sounds a little bit
* strange, but actually the parse code works in the way.
*/
return true;
}
}
/*
* No TRANSACTION READ ONLY/READ WRITE clause specified.
*/
return false;
}
/*
* Return true if start transaction query with "SERIALIZABLE" option.
*/
bool
is_serializable(TransactionStmt *node)
{
ListCell *list_item;
List *options = node->options;
foreach(list_item, options)
{
DefElem *opt = (DefElem *) lfirst(list_item);
if (!strcmp("transaction_isolation", opt->defname) &&
IsA(opt->arg, A_Const) &&
IsA(&((A_Const *) opt->arg)->val, String) &&
!strcmp("serializable", ((A_Const *) opt->arg)->val.sval.sval))
return true;
}
return false;
}
/*
* If the query is BEGIN READ WRITE or
* BEGIN ... SERIALIZABLE in streaming replication mode,
* we send BEGIN to standbys instead.
* The original_query which is BEGIN READ WRITE is sent to primary.
* The rewritten_query BEGIN is sent to standbys.
*/
bool
pool_need_to_treat_as_if_default_transaction(POOL_QUERY_CONTEXT * query_context)
{
return (MAIN_REPLICA &&
is_start_transaction_query(query_context->parse_tree) &&
(is_read_write((TransactionStmt *) query_context->parse_tree) ||
is_serializable((TransactionStmt *) query_context->parse_tree)));
}
/*
* Return true if the query is SAVEPOINT related query.
*/
bool
is_savepoint_query(Node *node)
{
if (((TransactionStmt *) node)->kind == TRANS_STMT_SAVEPOINT ||
((TransactionStmt *) node)->kind == TRANS_STMT_ROLLBACK_TO ||
((TransactionStmt *) node)->kind == TRANS_STMT_RELEASE)
return true;
return false;
}
/*
* Return true if the query is 2PC transaction query.
*/
bool
is_2pc_transaction_query(Node *node)
{
if (((TransactionStmt *) node)->kind == TRANS_STMT_PREPARE ||
((TransactionStmt *) node)->kind == TRANS_STMT_COMMIT_PREPARED ||
((TransactionStmt *) node)->kind == TRANS_STMT_ROLLBACK_PREPARED)
return true;
return false;
}
/*
* Set query state, if a current state is before it than the specified state.
*/
void
pool_set_query_state(POOL_QUERY_CONTEXT * query_context, POOL_QUERY_STATE state)
{
int i;
CHECK_QUERY_CONTEXT_IS_VALID;
for (i = 0; i < NUM_BACKENDS; i++)
{
if (query_context->where_to_send[i] &&
statecmp(query_context->query_state[i], state) < 0)
query_context->query_state[i] = state;
}
}
/*
* Return -1, 0 or 1 according to s1 is "before, equal or after" s2 in terms of state
* transition order.
* The State transition order is defined as: UNPARSED < PARSE_COMPLETE < BIND_COMPLETE < EXECUTE_COMPLETE
*/
int
statecmp(POOL_QUERY_STATE s1, POOL_QUERY_STATE s2)
{
int ret;
switch (s2)
{
case POOL_UNPARSED:
ret = (s1 == s2) ? 0 : 1;
break;
case POOL_PARSE_COMPLETE:
if (s1 == POOL_UNPARSED)
ret = -1;
else
ret = (s1 == s2) ? 0 : 1;
break;
case POOL_BIND_COMPLETE:
if (s1 == POOL_UNPARSED || s1 == POOL_PARSE_COMPLETE)
ret = -1;
else
ret = (s1 == s2) ? 0 : 1;
break;
case POOL_EXECUTE_COMPLETE:
ret = (s1 == s2) ? 0 : -1;
break;
default:
ret = -2;
break;
}
return ret;
}
/*
* Remove READ WRITE option from the packet of START TRANSACTION command.
* To free the return value is required.
*/
static
char *
remove_read_write(int len, const char *contents, int *rewritten_len)
{
char *rewritten_query;
char *rewritten_contents;
const char *name;
const char *stmt;
rewritten_query = "BEGIN";
name = contents;
stmt = contents + strlen(name) + 1;
*rewritten_len = len - strlen(stmt) + strlen(rewritten_query);
if (len < *rewritten_len)
{
ereport(ERROR,
(errmsg("invalid message length of transaction packet")));
}
rewritten_contents = palloc(*rewritten_len);
strcpy(rewritten_contents, name);
strcpy(rewritten_contents + strlen(name) + 1, rewritten_query);
memcpy(rewritten_contents + strlen(name) + strlen(rewritten_query) + 2,
stmt + strlen(stmt) + 1,
len - (strlen(name) + strlen(stmt) + 2));
return rewritten_contents;
}
/*
* Return true if current query is safe to cache.
*/
bool
pool_is_cache_safe(void)
{
POOL_SESSION_CONTEXT *sc;
sc = pool_get_session_context(true);
if (!sc)
return false;
if (pool_is_query_in_progress() && sc->query_context)
{
return sc->query_context->is_cache_safe;
}
return false;
}
/*
* Set safe to cache.
*/
void
pool_set_cache_safe(void)
{
POOL_SESSION_CONTEXT *sc;
sc = pool_get_session_context(true);
if (!sc)
return;
if (sc->query_context)
{
sc->query_context->is_cache_safe = true;
}
}
/*
* Unset safe to cache.
*/
void
pool_unset_cache_safe(void)
{
POOL_SESSION_CONTEXT *sc;
sc = pool_get_session_context(true);
if (!sc)
return;
if (sc->query_context)
{
sc->query_context->is_cache_safe = false;
}
}
/*
* Return true if current temporary query cache is exceeded
*/
bool
pool_is_cache_exceeded(void)
{
POOL_SESSION_CONTEXT *sc;
sc = pool_get_session_context(true);
if (!sc)
return false;
if (pool_is_query_in_progress() && sc->query_context)
{
if (sc->query_context->temp_cache)
return sc->query_context->temp_cache->is_exceeded;
return true;
}
return false;
}
/*
* Set current temporary query cache is exceeded
*/
void
pool_set_cache_exceeded(void)
{
POOL_SESSION_CONTEXT *sc;
sc = pool_get_session_context(true);
if (!sc)
return;
if (sc->query_context && sc->query_context->temp_cache)
{
sc->query_context->temp_cache->is_exceeded = true;
}
}
/*
* Unset current temporary query cache is exceeded
*/
void
pool_unset_cache_exceeded(void)
{
POOL_SESSION_CONTEXT *sc;
sc = pool_get_session_context(true);
if (!sc)
return;
if (sc->query_context && sc->query_context->temp_cache)
{
sc->query_context->temp_cache->is_exceeded = false;
}
}
/*
* Return true if one of followings is true
*
* SET transaction_read_only TO on
* SET TRANSACTION READ ONLY
* SET TRANSACTION CHARACTERISTICS AS TRANSACTION READ ONLY
*
* Note that if the node is not a variable statement, returns false.
*/
bool
pool_is_transaction_read_only(Node *node)
{
ListCell *list_item;
bool ret = false;
if (!IsA(node, VariableSetStmt))
return ret;
/*
* SET transaction_read_only TO on
*/
if (((VariableSetStmt *) node)->kind == VAR_SET_VALUE &&
!strcmp(((VariableSetStmt *) node)->name, "transaction_read_only"))
{
List *options = ((VariableSetStmt *) node)->args;
foreach(list_item, options)
{
A_Const *v = (A_Const *) lfirst(list_item);
switch (nodeTag(&v->val))
{
case T_String:
if (!strcasecmp(v->val.sval.sval, "on") ||
!strcasecmp(v->val.sval.sval, "t") ||
!strcasecmp(v->val.sval.sval, "true"))
ret = true;
break;
case T_Integer:
if (v->val.ival.ival)
ret = true;
default:
break;
}
}
}
/*
* SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY SET TRANSACTION
* READ ONLY
*/
else if (((VariableSetStmt *) node)->kind == VAR_SET_MULTI &&
(!strcmp(((VariableSetStmt *) node)->name, "TRANSACTION") ||
!strcmp(((VariableSetStmt *) node)->name, "SESSION CHARACTERISTICS")))
{
List *options = ((VariableSetStmt *) node)->args;
foreach(list_item, options)
{
DefElem *opt = (DefElem *) lfirst(list_item);
if (!strcmp("transaction_read_only", opt->defname))
{
bool read_only;
read_only = ((A_Const *) opt->arg)->val.ival.ival;
if (read_only)
{
ret = true;
break;
}
}
}
}
return ret;
}
/*
* Set virtual main node according to the where_to_send map. If there are
* multiple sending requests are in the map, the first node id is set to the
* virtual_main_node_id.
*/
static void
set_virtual_main_node(POOL_QUERY_CONTEXT *query_context)
{
int i;
for (i = 0; i < NUM_BACKENDS; i++)
{
if (query_context->where_to_send[i])
{
query_context->virtual_main_node_id = i;
break;
}
}
}
/*
* Set load balance info.
*/
static void
set_load_balance_info(POOL_QUERY_CONTEXT *query_context)
{
POOL_SESSION_CONTEXT *session_context;
session_context = pool_get_session_context(false);
if (pool_config->statement_level_load_balance)
session_context->load_balance_node_id = select_load_balancing_node();
session_context->query_context->load_balance_node_id = session_context->load_balance_node_id;
pool_set_node_to_be_sent(query_context,
query_context->load_balance_node_id);
}
/*
* Check if the name is in the list.
*/
static bool
is_in_list(char *name, List *list)
{
if (name == NULL || list == NIL)
return false;
ListCell *cell;
foreach (cell, list)
{
char *cell_name = (char *)lfirst(cell);
if (strcasecmp(name, cell_name) == 0)
{
ereport(DEBUG1,
(errmsg("[%s] is in list", name)));
return true;
}
}
return false;
}
/*
* Check if the relname of SelectStmt is in the temp write list.
*/
static bool
is_select_object_in_temp_write_list(Node *node, void *context)
{
if (node == NULL || pool_config->disable_load_balance_on_write != DLBOW_DML_ADAPTIVE)
return false;
if (IsA(node, RangeVar))
{
RangeVar *rgv = (RangeVar *) node;
POOL_SESSION_CONTEXT *session_context = pool_get_session_context(false);
if (pool_config->disable_load_balance_on_write == DLBOW_DML_ADAPTIVE && session_context->is_in_transaction)
{
ereport(DEBUG1,
(errmsg("is_select_object_in_temp_write_list: \"%s\", found relation \"%s\"", (char*)context, rgv->relname)));
return is_in_list(rgv->relname, session_context->transaction_temp_write_list);
}
}
return raw_expression_tree_walker(node, is_select_object_in_temp_write_list, context);
}
static char*
get_associated_object_from_dml_adaptive_relations
(char *left_token, DBObjectTypes object_type)
{
int i;
char *right_token = NULL;
if (!pool_config->parsed_dml_adaptive_object_relationship_list)
return NULL;
for (i=0 ;; i++)
{
if (pool_config->parsed_dml_adaptive_object_relationship_list[i].left_token.name == NULL)
break;
if (pool_config->parsed_dml_adaptive_object_relationship_list[i].left_token.object_type != object_type)
continue;
if (strcasecmp(pool_config->parsed_dml_adaptive_object_relationship_list[i].left_token.name, left_token) == 0)
{
right_token = pool_config->parsed_dml_adaptive_object_relationship_list[i].right_token.name;
break;
}
}
return right_token;
}
/*
* Check the object relationship list.
* If find the name in the list, will add related objects to the transaction temp write list.
*/
void
check_object_relationship_list(char *name, bool is_func_name)
{
if (pool_config->disable_load_balance_on_write == DLBOW_DML_ADAPTIVE && pool_config->parsed_dml_adaptive_object_relationship_list)
{
POOL_SESSION_CONTEXT *session_context = pool_get_session_context(false);
if (session_context->is_in_transaction)
{
char *right_token =
get_associated_object_from_dml_adaptive_relations
(name, is_func_name? OBJECT_TYPE_FUNCTION : OBJECT_TYPE_RELATION);
if (right_token)
{
MemoryContext old_context = MemoryContextSwitchTo(session_context->memory_context);
session_context->transaction_temp_write_list =
lappend(session_context->transaction_temp_write_list, pstrdup(right_token));
MemoryContextSwitchTo(old_context);
}
}
}
}
/*
* Find the relname and add it to the transaction temp write list.
*/
static bool
add_object_into_temp_write_list(Node *node, void *context)
{
if (node == NULL)
return false;
if (IsA(node, RangeVar))
{
RangeVar *rgv = (RangeVar *) node;
ereport(DEBUG5,
(errmsg("add_object_into_temp_write_list: \"%s\", found relation \"%s\"", (char*)context, rgv->relname)));
POOL_SESSION_CONTEXT *session_context = pool_get_session_context(false);
MemoryContext old_context = MemoryContextSwitchTo(session_context->memory_context);
if (!is_in_list(rgv->relname, session_context->transaction_temp_write_list))
{
ereport(DEBUG1,
(errmsg("add \"%s\" into transaction_temp_write_list", rgv->relname)));
session_context->transaction_temp_write_list = lappend(session_context->transaction_temp_write_list, pstrdup(rgv->relname));
}
MemoryContextSwitchTo(old_context);
check_object_relationship_list(rgv->relname, false);
}
return raw_expression_tree_walker(node, add_object_into_temp_write_list, context);
}
/*
* dml adaptive.
*/
static void
dml_adaptive(Node *node, char *query)
{
if (pool_config->disable_load_balance_on_write == DLBOW_DML_ADAPTIVE)
{
/* Set/Unset transaction status flags */
if (IsA(node, TransactionStmt))
{
POOL_SESSION_CONTEXT *session_context = pool_get_session_context(false);
MemoryContext old_context = MemoryContextSwitchTo(session_context->memory_context);
if (is_start_transaction_query(node))
{
session_context->is_in_transaction = true;
if (session_context->transaction_temp_write_list != NIL)
list_free_deep(session_context->transaction_temp_write_list);
session_context->transaction_temp_write_list = NIL;
}
else if(is_commit_or_rollback_query(node))
{
session_context->is_in_transaction = false;
if (session_context->transaction_temp_write_list != NIL)
list_free_deep(session_context->transaction_temp_write_list);
session_context->transaction_temp_write_list = NIL;
}
MemoryContextSwitchTo(old_context);
return;
}
/* If non-selectStmt, find the relname and add it to the transaction temp write list. */
if (!is_select_query(node, query))
add_object_into_temp_write_list(node, query);
}
}
/*
* Decide the backend node to be sent in streaming replication mode, logical
* replication mode and slony mode. Called by pool_where_to_send.
*/
static void
where_to_send_main_replica(POOL_QUERY_CONTEXT * query_context, char *query, Node *node)
{
POOL_DEST dest;
POOL_SESSION_CONTEXT *session_context;
POOL_CONNECTION_POOL *backend;
dest = send_to_where(node);
session_context = pool_get_session_context(false);
backend = session_context->backend;
dml_adaptive(node, query);
ereport(DEBUG1,
(errmsg("decide where to send the query"),
errdetail("destination = %d for query= \"%s\"", dest, query)));
/* Should be sent to primary only? */
if (dest == POOL_PRIMARY)
{
pool_set_node_to_be_sent(query_context, PRIMARY_NODE_ID);
}
/* Should be sent to both primary and standby? */
else if (dest == POOL_BOTH)
{
if (is_tx_started_by_multi_statement_query())
{
/*
* If we are in an explicit transaction and the transaction
* was started by a multi statement query, we should send
* query to primary node only (which was supposed to be sent
* to all nodes) until the transaction gets committed or
* aborted.
*/
pool_set_node_to_be_sent(query_context, PRIMARY_NODE_ID);
}
else
{
pool_setall_node_to_be_sent(query_context);
}
}
else if (pool_is_writing_transaction() &&
pool_config->disable_load_balance_on_write == DLBOW_ALWAYS)
{
pool_set_node_to_be_sent(query_context, PRIMARY_NODE_ID);
}
/*
* Ok, we might be able to load balance the SELECT query.
*/
else
{
if (pool_config->load_balance_mode &&
is_select_query(node, query) &&
MAJOR(backend) == PROTO_MAJOR_V3)
{
/*
* If (we are outside of an explicit transaction) OR (the
* transaction has not issued a write query yet, AND
* transaction isolation level is not SERIALIZABLE) we might
* be able to load balance.
*/
ereport(DEBUG1,
(errmsg("checking load balance preconditions. TSTATE:%c writing_transaction:%d failed_transaction:%d isolation:%d",
TSTATE(backend, PRIMARY_NODE_ID),
pool_is_writing_transaction(),
pool_is_failed_transaction(),
pool_get_transaction_isolation()),
errdetail("destination = %d for query= \"%s\"", dest, query)));
if (TSTATE(backend, PRIMARY_NODE_ID) == 'I' ||
(!pool_is_writing_transaction() &&
!pool_is_failed_transaction() &&
pool_get_transaction_isolation() != POOL_SERIALIZABLE))
{
/*
* Load balance if possible
*/
/*
* If system catalog is used in the SELECT, we prefer to
* send to the primary. Example: SELECT * FROM pg_class
* WHERE relname = 't1'; Because 't1' is a constant, it's
* hard to recognize as table name. Most use case such
* query is against system catalog, and the table name can
* be a temporary table, it's best to query against
* primary system catalog. Please note that this test must
* be done *before* test using pool_has_temp_table.
*/
if (pool_has_system_catalog(node))
{
ereport(DEBUG1,
(errmsg("could not load balance because systems catalogs are used"),
errdetail("destination = %d for query= \"%s\"", dest, query)));
pool_set_node_to_be_sent(query_context, PRIMARY_NODE_ID);
}
/*
* If temporary table is used in the SELECT, we prefer to
* send to the primary.
*/
else if (pool_config->check_temp_table && pool_has_temp_table(node))
{
ereport(DEBUG1,
(errmsg("could not load balance because temporary tables are used"),
errdetail("destination = %d for query= \"%s\"", dest, query)));
pool_set_node_to_be_sent(query_context, PRIMARY_NODE_ID);
}
/*
* If unlogged table is used in the SELECT, we prefer to
* send to the primary.
*/
else if (pool_config->check_unlogged_table && pool_has_unlogged_table(node))
{
ereport(DEBUG1,
(errmsg("could not load balance because unlogged tables are used"),
errdetail("destination = %d for query= \"%s\"", dest, query)));
pool_set_node_to_be_sent(query_context, PRIMARY_NODE_ID);
}
/*
* When query match the query patterns in primary_routing_query_pattern_list, we
* send only to main node.
*/
else if (pattern_compare(query, WRITELIST, "primary_routing_query_pattern_list") == 1)
{
pool_set_node_to_be_sent(query_context, PRIMARY_NODE_ID);
}
/*
* If a writing function call is used, we prefer to send
* to the primary.
*/
else if (pool_has_function_call(node))
{
ereport(DEBUG1,
(errmsg("could not load balance because writing functions are used"),
errdetail("destination = %d for query= \"%s\"", dest, query)));
pool_set_node_to_be_sent(query_context, PRIMARY_NODE_ID);
}
else if (is_select_object_in_temp_write_list(node, query))
{
pool_set_node_to_be_sent(query_context, PRIMARY_NODE_ID);
}
else
{
if (pool_config->statement_level_load_balance)
{
session_context->load_balance_node_id = select_load_balancing_node();
}
/*
* As streaming replication delay is too much, if
* prefer_lower_delay_standby is true then elect new
* load balance node which is lowest delayed,
* false then send to the primary.
*/
if (STREAM && check_replication_delay(session_context->load_balance_node_id))
{
ereport(DEBUG1,
(errmsg("could not load balance because of too much replication delay"),
errdetail("destination = %d for query= \"%s\"", dest, query)));
if (pool_config->prefer_lower_delay_standby)
{
int new_load_balancing_node = select_load_balancing_node();
session_context->load_balance_node_id = new_load_balancing_node;
session_context->query_context->load_balance_node_id = session_context->load_balance_node_id;
pool_set_node_to_be_sent(query_context, session_context->query_context->load_balance_node_id);
}
else
{
pool_set_node_to_be_sent(query_context, PRIMARY_NODE_ID);
}
}
else
{
session_context->query_context->load_balance_node_id = session_context->load_balance_node_id;
pool_set_node_to_be_sent(query_context,
session_context->query_context->load_balance_node_id);
}
}
}
else
{
/* Send to the primary only */
pool_set_node_to_be_sent(query_context, PRIMARY_NODE_ID);
}
}
else
{
/* Send to the primary only */
pool_set_node_to_be_sent(query_context, PRIMARY_NODE_ID);
}
}
}
/*
* Decide the backend node to be sent in replication mode and snapshot
* isolation mode.
* Called by pool_where_to_send.
*/
static void
where_to_send_native_replication(POOL_QUERY_CONTEXT * query_context, char *query, Node *node)
{
POOL_SESSION_CONTEXT *session_context;
POOL_CONNECTION_POOL *backend;
session_context = pool_get_session_context(false);
backend = session_context->backend;
/*
* Check to see if we can load balance the SELECT (or any read only query
* from syntactical point of view).
*/
elog(DEBUG1, "Maybe: load balance mode: %d is_select_query: %d",
pool_config->load_balance_mode, is_select_query(node, query));
if (pool_config->load_balance_mode &&
is_select_query(node, query) &&
MAJOR(backend) == PROTO_MAJOR_V3)
{
/*
* In snapshot isolation mode, we always load balance if current
* transaction is read only unless load balance mode is off.
*/
if (pool_config->backend_clustering_mode == CM_SNAPSHOT_ISOLATION &&
pool_config->load_balance_mode)
{
if (TSTATE(backend, MAIN_NODE_ID) == 'T')
{
/*
* We are in an explicit transaction. If the transaction is
* read only, we can load balance.
*/
if (session_context->transaction_read_only)
{
/* Ok, we can load balance. We are done! */
set_load_balance_info(query_context);
set_virtual_main_node(query_context);
return;
}
}
else if (TSTATE(backend, MAIN_NODE_ID) == 'I')
{
/*
* We are out side transaction. If default transaction is read only,
* we can load balance.
*/
static char *si_query = "SELECT current_setting('transaction_read_only')";
POOL_SELECT_RESULT *res;
bool load_balance = false;
do_query(CONNECTION(backend, MAIN_NODE_ID), si_query, &res, MAJOR(backend));
if (res)
{
if (res->data[0] && !strcmp(res->data[0], "on"))
{
load_balance = true;
}
free_select_result(res);
}
per_node_statement_log(backend, MAIN_NODE_ID, si_query);
if (load_balance)
{
/* Ok, we can load balance. We are done! */
set_load_balance_info(query_context);
set_virtual_main_node(query_context);
return;
}
}
}
/*
* If a writing function call is used or replicate_select is true, we
* have to send to all nodes since the function may modify database.
*/
elog(DEBUG1, "Maybe sent to all node: pool_has_function_call: %d pool_config->replicate_select: %d",
pool_has_function_call(node), pool_config->replicate_select);
if (pool_has_function_call(node) || pool_config->replicate_select)
{
pool_setall_node_to_be_sent(query_context);
}
/*
* If (we are outside of an explicit transaction) OR (the
* transaction has not issued a write query yet, AND transaction
* isolation level is not SERIALIZABLE) we might be able to load
* balance.
*/
else if (TSTATE(backend, MAIN_NODE_ID) == 'I' ||
(!pool_is_writing_transaction() &&
!pool_is_failed_transaction() &&
pool_get_transaction_isolation() != POOL_SERIALIZABLE))
{
elog(DEBUG1, "load balance TSTATE: %c pool_is_writing_transaction: %d pool_is_failed_transaction: %d pool_get_transaction_isolation: %d",
TSTATE(backend, MAIN_NODE_ID),
pool_is_writing_transaction(),
pool_is_failed_transaction(),
pool_get_transaction_isolation());
set_load_balance_info(query_context);
}
else
{
/* only send to main node */
elog(DEBUG1, "unable to load balance");
pool_set_node_to_be_sent(query_context, REAL_MAIN_NODE_ID);
}
}
else
{
if (is_select_query(node, query) && !pool_config->replicate_select &&
!pool_has_function_call(node))
{
/* only send to main node */
pool_set_node_to_be_sent(query_context, REAL_MAIN_NODE_ID);
}
else
{
/* send to all nodes */
pool_setall_node_to_be_sent(query_context);
}
}
}
/*
* Wait for failover/failback to finish.
* Return values:
* 0: no failover/failback occurred.
* -1: failover/failback occurred and finished within certain period.
* -2: failover/failback occurred and timed out.
*/
int
wait_for_failover_to_finish(void)
{
#define MAX_FAILOVER_WAIT 30 /* waiting for failover finish timeout in seconds */
volatile POOL_REQUEST_INFO *my_req;
int ret = 0;
int i;
/*
* Wait for failover to finish
*/
for (i = 0;i < MAX_FAILOVER_WAIT; i++)
{
my_req = Req_info;
if (my_req->switching == 0)
return ret;
ret = -1; /* failover/failback finished */
sleep(1);
}
return -2; /* timed out */
}
|