forked from PaulGarneau/pgpool2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool_query_context.c
1662 lines (1482 loc) · 37.6 KB
/
pool_query_context.c
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
/* -*-pgsql-c-*- */
/*
*
* $Header$
*
* pgpool: a language independent connection pool server for PostgreSQL
* written by Tatsuo Ishii
*
* Copyright (c) 2003-2013 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 "pool_proto_modules.h"
#include "pool_session_context.h"
#include "pool_query_context.h"
#include "pool_select_walker.h"
#include "parser/nodes.h"
#include <string.h>
#include <netinet/in.h>
#include <stdlib.h>
/*
* Where to send query
*/
typedef enum {
POOL_PRIMARY,
POOL_STANDBY,
POOL_EITHER,
POOL_BOTH
} POOL_DEST;
static POOL_DEST send_to_where(Node *node, char *query);
static void where_to_send_deallocate(POOL_QUERY_CONTEXT *query_context, Node *node);
static char* remove_read_write(int len, const char *contents, int *rewritten_len);
/*
* Create and initialize per query session context
*/
POOL_QUERY_CONTEXT *pool_init_query_context(void)
{
POOL_QUERY_CONTEXT *qc;
qc = calloc(1, sizeof(*qc));
if (!qc)
{
pool_error("pool_init_query_context: cannot allocate memory");
return NULL;
}
/* Create memory context */
qc->memory_context = pool_memory_create(PARSER_BLOCK_SIZE);
return qc;
}
/*
* Destroy query context
*/
void pool_query_context_destroy(POOL_QUERY_CONTEXT *query_context)
{
POOL_SESSION_CONTEXT *session_context;
if (query_context)
{
session_context = pool_get_session_context();
pool_unset_query_in_progress();
session_context->query_context = NULL;
pool_memory_delete(query_context->memory_context, 0);
free(query_context);
}
}
/*
* 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)
{
session_context = pool_get_session_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_master_node_id = my_master_node_id;
query_context->is_cache_safe = false;
if (pool_config->memory_cache_enabled)
query_context->temp_cache = pool_create_temp_query_cache(query);
pool_set_query_in_progress();
session_context->query_context = query_context;
}
}
/*
* Specify DB node to send query
*/
void pool_set_node_to_be_sent(POOL_QUERY_CONTEXT *query_context, int node_id)
{
if (!query_context)
{
pool_error("pool_set_node_to_be_sent: no query context");
return;
}
if (node_id < 0 || node_id > MAX_NUM_BACKENDS)
{
pool_error("pool_set_node_to_be_sent: invalid node id:%d", node_id);
return;
}
query_context->where_to_send[node_id] = true;
return;
}
/*
* Unspecify DB node to send query
*/
void pool_unset_node_to_be_sent(POOL_QUERY_CONTEXT *query_context, int node_id)
{
if (!query_context)
{
pool_error("pool_unset_node_to_be_sent: no query context");
return;
}
if (node_id < 0 || node_id > MAX_NUM_BACKENDS)
{
pool_error("pool_unset_node_to_be_sent: invalid node id:%d", node_id);
return;
}
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)
{
if (!query_context)
{
pool_error("pool_clear_node_to_be_sent: no query context");
return;
}
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;
if (!query_context)
{
pool_error("pool_setall_node_to_be_sent: no query context");
return;
}
for (i=0;i<NUM_BACKENDS;i++)
{
if (private_backend_status[i] == CON_UP ||
(private_backend_status[i] == CON_CONNECT_WAIT))
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;
if (!query_context)
{
pool_error("pool_multi_node_to_be_sent: no query context");
return false;
}
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)
{
if (!query_context)
{
pool_error("pool_is_node_to_be_sent: no query context");
return false;
}
if (node_id < 0 || node_id > MAX_NUM_BACKENDS)
{
pool_error("pool_is_node_to_be_sent: invalid node id:%d", node_id);
return false;
}
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_MASTER_NODE_ID;
sc = pool_get_session_context();
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 master DB node id,
*/
int pool_virtual_master_db_node_id(void)
{
POOL_SESSION_CONTEXT *sc;
sc = pool_get_session_context();
if (!sc)
{
return REAL_MASTER_NODE_ID;
}
if (sc->query_context)
{
return sc->query_context->virtual_master_node_id;
}
/*
* No query context exists. If in master/slave mode, returns
* primary node if exists. Oterwise returns my_master_node_id,
* which represents the last REAL_MASTER_NODE_ID.
*/
if (MASTER_SLAVE)
{
return PRIMARY_NODE_ID;
}
return my_master_node_id;
}
/*
* Decide where to send queries(thus expecting response)
*/
void pool_where_to_send(POOL_QUERY_CONTEXT *query_context, char *query, Node *node)
{
POOL_SESSION_CONTEXT *session_context;
POOL_CONNECTION_POOL *backend;
int i;
if (!query_context)
{
pool_error("pool_where_to_send: no query context");
return;
}
session_context = pool_get_session_context();
backend = session_context->backend;
/*
* Zap out DB node map
*/
pool_clear_node_to_be_sent(query_context);
/*
* If there is "NO LOAD BALANCE" comment, we send only to master node.
*/
if (!strncasecmp(query, NO_LOAD_BALANCE, NO_LOAD_BALANCE_COMMENT_SZ))
{
pool_set_node_to_be_sent(query_context,
MASTER_SLAVE ? PRIMARY_NODE_ID : REAL_MASTER_NODE_ID);
for (i=0;i<NUM_BACKENDS;i++)
{
if (query_context->where_to_send[i])
{
query_context->virtual_master_node_id = i;
break;
}
}
return;
}
/*
* In raw mode, we send only to master node. Simple enough.
*/
if (RAW_MODE)
{
pool_set_node_to_be_sent(query_context, REAL_MASTER_NODE_ID);
}
else if (MASTER_SLAVE && query_context->is_multi_statement)
{
/*
* If we are in master/slave mode and we have multi stametemt
* 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 unconditionaly
* 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.
*/
if (query_context->is_multi_statement)
{
pool_set_node_to_be_sent(query_context, PRIMARY_NODE_ID);
}
}
else if (MASTER_SLAVE)
{
POOL_DEST dest;
POOL_MEMORY_POOL *old_context;
old_context = pool_memory_context_switch_to(query_context->memory_context);
dest = send_to_where(node, query);
pool_memory_context_switch_to(old_context);
pool_debug("send_to_where: %d 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)
{
pool_setall_node_to_be_sent(query_context);
}
/*
* 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.
*/
if (TSTATE(backend, PRIMARY_NODE_ID) == 'I' ||
(!pool_is_writing_transaction() &&
!pool_is_failed_transaction() &&
pool_get_transaction_isolation() != POOL_SERIALIZABLE))
{
BackendInfo *bkinfo = pool_get_node_info(session_context->load_balance_node_id);
/*
* Load balance if possible
*/
/*
* If replication delay is too much, we prefer to send to the primary.
*/
if (!strcmp(pool_config->master_slave_sub_mode, MODE_STREAMREP) &&
pool_config->delay_threshold &&
bkinfo->standby_delay > pool_config->delay_threshold)
{
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))
{
pool_set_node_to_be_sent(query_context, PRIMARY_NODE_ID);
}
/*
* 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.
*/
else if (pool_has_system_catalog(node))
{
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))
{
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_has_unlogged_table(node))
{
pool_set_node_to_be_sent(query_context, PRIMARY_NODE_ID);
}
else
{
pool_set_node_to_be_sent(query_context,
session_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);
}
}
}
else if (REPLICATION || PARALLEL_MODE)
{
if (is_select_query(node, query))
{
/*
* If a writing function call is used or replicate_select is true,
* we prefer to send to all nodes.
*/
if (pool_has_function_call(node) || pool_config->replicate_select)
{
pool_setall_node_to_be_sent(query_context);
}
else if (pool_config->load_balance_mode &&
MAJOR(backend) == PROTO_MAJOR_V3 &&
TSTATE(backend, MASTER_NODE_ID) == 'I')
{
/* load balance */
pool_set_node_to_be_sent(query_context,
session_context->load_balance_node_id);
}
else
{
/* only send to master node */
pool_set_node_to_be_sent(query_context, REAL_MASTER_NODE_ID);
}
}
else
{
/* send to all nodes */
pool_setall_node_to_be_sent(query_context);
}
}
else
{
pool_error("pool_where_to_send: unknown mode");
return;
}
/*
* EXECUTE?
*/
if (IsA(node, ExecuteStmt))
{
POOL_SENT_MESSAGE *msg;
msg = pool_get_sent_message('Q', ((ExecuteStmt *)node)->name);
if (!msg)
msg = pool_get_sent_message('P', ((ExecuteStmt *)node)->name);
if (msg)
pool_copy_prep_where(msg->query_context->where_to_send,
query_context->where_to_send);
}
/*
* DEALLOCATE?
*/
else if (IsA(node, DeallocateStmt))
{
where_to_send_deallocate(query_context, node);
}
for (i=0;i<NUM_BACKENDS;i++)
{
if (query_context->where_to_send[i])
{
query_context->virtual_master_node_id = i;
break;
}
}
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();
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 master/slave mode,
* we send BEGIN to slaves/standbys instead.
* original_query which is BEGIN READ WRITE is sent to primary.
* rewritten_query which is 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 in master/slave mode, we do not send COMMIT/ABORT to
* slaves/standbys if it's in I(idle) state.
*/
if (is_commit && MASTER_SLAVE && !IS_MASTER_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);
if (send_simplequery_message(CONNECTION(backend, i), len, string, MAJOR(backend)) != POOL_CONTINUE)
{
return POOL_END;
}
}
/* 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 master/slave mode, we do not send COMMIT/ABORT to
* slaves/standbys if it's in I(idle) state.
*/
if (is_commit && MASTER_SLAVE && !IS_MASTER_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;
}
if (wait_for_query_response(frontend, CONNECTION(backend, i), MAJOR(backend)) != POOL_CONTINUE)
{
/* Cancel current transaction */
CancelPacket cancel_packet;
cancel_packet.protoVersion = htonl(PROTO_CANCEL);
cancel_packet.pid = MASTER_CONNECTION(backend)->pid;
cancel_packet.key= MASTER_CONNECTION(backend)->key;
cancel_request(&cancel_packet);
return POOL_END;
}
/*
* Check if some error detected. If so, emit
* log. This is usefull 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, string, "pool_send_and_wait: Error or notice message from backend: ", 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)
{
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();
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 master/slave mode,
* we send BEGIN to slaves/standbys instead.
* original_query which is BEGIN READ WRITE is sent to primary.
* rewritten_query which is 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 == NULL)
return POOL_END;
}
}
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')
{
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
snprintf(msgbuf, sizeof(msgbuf), "Execute: %s", stmt);
}
else
{
snprintf(msgbuf, sizeof(msgbuf), "%c message", *kind);
}
per_node_statement_log(backend, i, msgbuf);
}
if (send_extended_protocol_message(backend, i, kind, str_len, str) != POOL_CONTINUE)
{
free(rewritten_begin);
return POOL_END;
}
}
if (!is_begin_read_write)
{
if (query_context->rewritten_query)
str = query_context->rewritten_query;
else
str = query_context->original_query;
}
/* 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 master/slave mode, we do not send COMMIT/ABORT to
* slaves/standbys if it's in I(idle) state.
*/
if (is_commit && MASTER_SLAVE && !IS_MASTER_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;
}
if (wait_for_query_response(frontend, CONNECTION(backend, i), MAJOR(backend)) != POOL_CONTINUE)
{
/* Cancel current transaction */
CancelPacket cancel_packet;
cancel_packet.protoVersion = htonl(PROTO_CANCEL);
cancel_packet.pid = MASTER_CONNECTION(backend)->pid;
cancel_packet.key= MASTER_CONNECTION(backend)->key;
cancel_request(&cancel_packet);
free(rewritten_begin);
return POOL_END;
}
/*
* Check if some error detected. If so, emit
* log. This is usefull 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);
}
free(rewritten_begin);
return POOL_CONTINUE;
}
/*
* From syntactically analysis decide the statement to be sent to the
* primary, the standby or either or both in master/slave+HR/SR mode.
*/
static POOL_DEST send_to_where(Node *node, char *query)
{
/* 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 */
/* From 9.0 include/nodes/node.h */
static NodeTag nodemap[] = {
T_PlannedStmt,
T_InsertStmt,
T_DeleteStmt,
T_UpdateStmt,
T_SelectStmt,
T_AlterTableStmt,
T_AlterTableCmd,
T_AlterDomainStmt,
T_SetOperationStmt,
T_GrantStmt,
T_GrantRoleStmt,
/*
T_AlterDefaultPrivilegesStmt, Our parser does not support yet
*/
T_ClosePortalStmt,
T_ClusterStmt,
T_CopyStmt,
T_CreateStmt, /* CREAE TABLE */
T_DefineStmt, /* CREATE AGGREGATE, OPERATOR, TYPE */
T_DropStmt, /* DROP TABLE etc. */
T_TruncateStmt,
T_CommentStmt,
T_FetchStmt,
T_IndexStmt, /* CREATE INDEX */
T_CreateFunctionStmt,
T_AlterFunctionStmt,
/*
T_DoStmt, Our parser does not support yet
*/
T_RenameStmt, /* ALTER AGGREGATE etc. */
T_RuleStmt, /* CREATE RULE */
T_NotifyStmt,
T_ListenStmt,
T_UnlistenStmt,
T_TransactionStmt,
T_ViewStmt, /* CREATE VIEW */
T_LoadStmt,
T_CreateDomainStmt,
T_CreatedbStmt,
T_DropdbStmt,
T_VacuumStmt,
T_ExplainStmt,
T_CreateSeqStmt,
T_AlterSeqStmt,
T_VariableSetStmt, /* SET */
T_VariableShowStmt,
T_DiscardStmt,
T_CreateTrigStmt,
T_CreatePLangStmt,
T_CreateRoleStmt,
T_AlterRoleStmt,
T_DropRoleStmt,
T_LockStmt,
T_ConstraintsSetStmt,
T_ReindexStmt,
T_CheckPointStmt,
T_CreateSchemaStmt,
T_AlterDatabaseStmt,
T_AlterDatabaseSetStmt,
T_AlterRoleSetStmt,
T_CreateConversionStmt,
T_CreateCastStmt,