summaryrefslogtreecommitdiff
path: root/doc/src/sgml/loadbalance.sgml
blob: ee19fabebab2210cd4abe59a711a036ac0ac8943 (plain)
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
<!-- doc/src/sgml/config.sgml -->

<sect1 id="runtime-config-load-balancing">
 <title>Load Balancing</title>

 <para>
  <productname>Pgpool-II</productname> load balancing of SELECT queries
  works with any clustering mode except raw mode. When enabled
    <productname>Pgpool-II</productname> sends the writing queries to the
    <acronym>primary node</acronym> in Native Replication mode, all of the
    backend nodes in Replication mode, and other queries get load
    balanced among all backend nodes.  To which node the load
    balancing mechanism sends read queries is decided at the session
    start time and will not be changed until the session ends unless <xref linkend="guc-statement-level-load-balance"> is specified. However
    there are some exceptions.  See below for more details.
 </para>
 <note>
  <para>
   Queries which are sent to primary node or replicated because they cannot be balanced are
   also accounted for in the load balancing algorithm.
  </para>
 </note>

 <note>
  <para>
   You can check which DB node is assigned as the load balancing
   node by using <xref linkend="sql-show-pool-nodes">.
  </para>
 </note>
 
 <sect2 id="runtime-config-load-balancing-condition">
  <title>Condition for Load Balancing</title>

  <para>
   For a query to be load balanced, all the following requirements
   must be met:
   <itemizedlist>
    <listitem>
     <para>
      <productname>PostgreSQL</> version 7.4 or later
     </para>
    </listitem>
    <listitem>
     <para>
      either
      in <xref linkend="runtime-config-streaming-replication-mode">,
      <xref linkend="guc-replication-mode">,
       <xref linkend="guc-snapshot-isolation-mode"> or
	<link linkend="runtime-config-logical-replication-mode">logical
	replication mode</link>.
     </para>
    </listitem>
    <listitem>
     <para>
      the query must not be in an explicitly declared transaction
      (i.e. not in a BEGIN ~ END block)
     </para>
     <itemizedlist>
      <listitem>
       <para>
	However, if following conditions are met, load balance is possible
	even if in an explicit transaction
	<itemizedlist>
	 <listitem>
	  <para>
	   transaction isolation level is not SERIALIZABLE
	  </para>
	 </listitem>
	 <listitem>
	  <para>
	   transaction has not issued a write query yet (until a write
	   query is issued, load balance is possible. Here "write query"
	   means non SELECT DML or DDL. <EMPHASIS>Before <productname>Pgpool-II</> 4.1</>,
	   SELECTs having write functions as specified in write or
	   read_only function list is not regarded as a write query.)
	  </para>
	 </listitem>
	 <listitem>
	  <para>
	   If write and read_only function list is empty, SELECT having
	   functions which are not volatile is regarded as a read only query.
	  </para>
	 </listitem>
	</itemizedlist>
       </para>
      </listitem>
     </itemizedlist>
    </listitem>
    <listitem>
     <para>
      it's not SELECT INTO
     </para>
    </listitem>
    <listitem>
     <para>
      it's not SELECT FOR UPDATE nor FOR SHARE
     </para>
    </listitem>
    <listitem>
     <para>
      it starts with "SELECT" or one of COPY TO STDOUT, EXPLAIN,
      EXPLAIN ANALYZE SELECT... <xref linkend="guc-ignore-leading-white-space"> = <literal>true</>
       will ignore leading white space.
       (Except for SELECTs using writing functions specified in <xref linkend="guc-write-function-list"> or
	<xref linkend="guc-read-only-function-list">)
     </para>
    </listitem>
    <listitem>
     <para>
      in <xref linkend="runtime-config-streaming-replication-mode"> in addition to above, following conditions must be met:
     </para>
     <itemizedlist>
      <listitem>
       <para>
	temporary tables are not used (temporary tables are not replicated)
       </para>
      </listitem>
      <listitem>
       <para>
	unlogged tables are not used (unlogged tables are not replicated)
       </para>
      </listitem>
      <listitem>
       <para>
	system catalogs are not used (system catalog information is
	important and it is desirable to avoid replication delay)
       </para>
      </listitem>
     </itemizedlist>
    </listitem>
   </itemizedlist>
  </para>

  <note>
   <para>
    You could suppress load balancing by inserting arbitrary
    comments just in front of the SELECT query:
   </para>
   <programlisting>
/*REPLICATION*/ SELECT ...
   </programlisting>
   <para>
    If you want to use comments without suppressing load balancing, you can set
    <xref linkend="guc-allow-sql-comments"> to on.
     Please refer to <xref linkend="guc-replicate-select"> as well.
   </para>
  </note>

  <note>
   <para>
    The JDBC driver has an autocommit option. If the autocommit is false,
    the JDBC driver sends "BEGIN" and "COMMIT" by itself. In this case
    the same restriction above regarding load balancing will be applied.
   </para>
  </note>

 </sect2>

 <sect2 id="runtime-config-writing-queries-may-affect-load-balancing">

  <title>Writing queries may affect Load Balancing</title>
  <para>
   In general, read queries are load balanced if certain conditions
   are met. However, writing queries may affect the load
   balancing. Here "writing queries" mean all the queries except
   below:
  </para>

  <para>
   <itemizedlist>

    <listitem>
     <para>
      SELECT/WITH without writing functions.
      Volatile functions are regarded writing functions.
      You can define your own writing functions by using <xref linkend="guc-write-function-list">
      or <xref linkend="guc-read-only-function-list">.
     </para>
    </listitem>

    <listitem>
     <para>
      SELECT/WITH without FOR UPDATE/SHARE
     </para>
    </listitem>

    <listitem>
     <para>
      WITH without DML statements
     </para>
    </listitem>

    <listitem>
     <para>
      COPY TO STDOUT
     </para>
    </listitem>

    <listitem>
     <para>
      EXPLAIN
     </para>
    </listitem>	 

    <listitem>
     <para>
      EXPLAIN ANALYZE and the query is SELECT not including writing functions
     </para>
    </listitem>	 

    <listitem>
     <para>
      SHOW
     </para>
    </listitem>	 

   </itemizedlist>
  </para>

  <para>
   If writing queries appear, succeeding read queries may not be
   load balanced. i.e. sent to primary node (in streaming
   replication mode) or main node (in other mode) depending on the
   setting of <xref linkend="guc-disable-load-balance-on-write">.
  </para>
 </sect2>

 <sect2 id="runtime-config-load-balancing-in-streaming-replication">

  <title>Load Balancing in Streaming Replication</title>

  <para>
   While using Streaming replication and Hot Standby, it is important to
   determine which query can be sent to the primary or the standby,
   and which one should not be sent to the standby.
   <productname>Pgpool-II</>'s Streaming Replication mode carefully
   takes care of this.
  </para>

  <para>
   We distinguish which query should be sent to which node by looking
   at the query itself.
   <itemizedlist>
    <listitem>
     <para>
      These queries should be sent to the primary node only
      <itemizedlist>
       <listitem>
	<para>
	 INSERT, UPDATE, DELETE, COPY FROM, TRUNCATE, CREATE, DROP, ALTER, COMMENT
	</para>
       </listitem>
       <listitem>
	<para>
	 SELECT ... FOR SHARE | UPDATE
	</para>
       </listitem>
       <listitem>
	<para>
	 SELECT in transaction isolation level SERIALIZABLE
	</para>
       </listitem>
       <listitem>
	<para>
	 LOCK command more strict than ROW EXCLUSIVE MODE
	</para>
       </listitem>
       <listitem>
	<para>
	 DECLARE, FETCH, CLOSE
	</para>
       </listitem>
       <listitem>
	<para>
	 SHOW
	</para>
       </listitem>
       <listitem>
	<para>
	 Some transactional commands:
	 <itemizedlist>
	  <listitem>
	   <para>
	    BEGIN READ WRITE, START TRANSACTION READ WRITE
	   </para>
	  </listitem>
	  <listitem>
	   <para>
	    SET TRANSACTION READ WRITE, SET SESSION CHARACTERISTICS AS TRANSACTION READ WRITE
	   </para>
	  </listitem>
	  <listitem>
	   <para>
	    SET transaction_read_only = off
	   </para>
	  </listitem>
	 </itemizedlist>
	</para>
       </listitem>
       <listitem>
	<para>
	 Two phase commit commands: PREPARE TRANSACTION, COMMIT PREPARED, ROLLBACK PREPARED
	</para>
       </listitem>
       <listitem>
	<para>
	 LISTEN, UNLISTEN, NOTIFY
	</para>
       </listitem>
       <listitem>
	<para>
	 VACUUM
	</para>
       </listitem>
       <listitem>
	<para>
	 Some sequence functions (nextval and setval)
	</para>
       </listitem>
       <listitem>
	<para>
	 Large objects creation commands
	</para>
       </listitem>
       <listitem>
	<para>
	 Multi-statement queries (multiple SQL commands on single line)
	</para>
       </listitem>
      </itemizedlist>
     </para>
    </listitem>
    <listitem>
     <para>
      These queries can be sent to both the primary node and the standby node.
      If load balancing is enabled, these types of queries can be sent to the standby node.
      However, if delay_threshold is set and the replication delay is higher than
      <xref linkend="guc-delay-threshold">, queries are sent to the primary node.

       <itemizedlist>
	<listitem>
	 <para>
	  SELECT not listed above
	 </para>
	</listitem>

	<listitem>
	 <para>
	  COPY TO STDOUT
	 </para>
	</listitem>

	<listitem>
	 <para>
	  EXPLAIN
	 </para>
	</listitem>	 

	<listitem>
	 <para>
	  EXPLAIN ANALYZE and the query is SELECT not including writing functions
	 </para>
	</listitem>	 

	<listitem>
	 <para>
	  SHOW
	 </para>
	</listitem>	 

       </itemizedlist>
     </para>
    </listitem>
    <listitem>
     <para>
      These queries are sent to both the primary node and the standby node
      <itemizedlist>
       <listitem>
	<para>
	 SET
	</para>
       </listitem>
       <listitem>
	<para>
	 DISCARD
	</para>
       </listitem>
       <listitem>
	<para>
	 DEALLOCATE ALL
	</para>
       </listitem>
       <listitem>
	<para>
	 SAVEPOINT (and related commands such as RELEASE SAVEPOINT)
	</para>
       </listitem>
      </itemizedlist>
     </para>
    </listitem>
   </itemizedlist>
  </para>

  <para>
   In an explicit transaction:
   <itemizedlist>

    <listitem>
     <para>
      Transaction starting commands such as BEGIN are sent to both the primary node
      and the standby node.
     </para>
    </listitem>
    <listitem>
     <para>
      Following SELECT and some other queries that can be sent to both
      primary or standby are executed in the transaction or on the standby node.
     </para>
    </listitem>
    <listitem>
     <para>
      Commands which cannot be executed on the standby such as INSERT are sent
      to the primary.
      After one of these commands, even SELECTs are sent to the primary node,
      This is because these SELECTs might want to see the result of an INSERT immediately.
      This behavior continues until the transaction closes or aborts.
     </para>
    </listitem>
   </itemizedlist>
  </para>

  <para>
   In the extended protocol, it is possible to determine if the query can
   be sent to standby or not in load balance mode while parsing the query.
   The rules are the same as for the non extended protocol.
   For example, INSERTs are sent to the primary node.
   Following bind, describe and execute will be sent to the primary node as well.
  </para>

  <note>
   <para>
    If the parse of a SELECT statement is sent to the standby node due to load
    balancing, and then a DML statement, such as an INSERT, is sent to <productname>Pgpool-II</>,
    then the parsed SELECT will have to be executed on the primary node.
    Therefore, we re-parse the SELECT on the primary node.
   </para>
  </note>

  <para>
   Lastly, queries that <productname>Pgpool-II</>'s parser thinks to be an
   error are sent to the primary node.
  </para>
 </sect2>

 <sect2 id="runtime-config-load-balancing-settings">

  <title>Load Balancing Settings</title>

  <variablelist>

   <varlistentry id="guc-load-balance-mode" xreflabel="load_balance_mode">
    <term><varname>load_balance_mode</varname> (<type>boolean</type>)
     <indexterm>
      <primary><varname>load_balance_mode</varname> configuration parameter</primary>
     </indexterm>
    </term>
    <listitem>
     <para>
      When set to on, <productname>Pgpool-II</productname> enables the
      load balancing on incoming <acronym>SELECT</acronym> queries.
      i.e. <acronym>SELECT</acronym> queries from the clients gets distributed to
      the configured <productname>PostgreSQL</> backends.
      Default is on.
     </para>
     <para>
      This parameter can only be set at server start.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry id="guc-ignore-leading-white-space" xreflabel="ignore_leading_white_space">
    <term><varname>ignore_leading_white_space</varname> (<type>boolean</type>)
     <indexterm>
      <primary><varname>ignore_leading_white_space</varname> configuration parameter</primary>
     </indexterm>
    </term>
    <listitem>
     <para>
      When set to on, <productname>Pgpool-II</productname> ignores the
      white spaces at the beginning of SQL queries in load balancing.
      It is useful if used with APIs like DBI/DBD:Pg which adds
      white spaces against the user's intention.
     </para>
     <para>
      This parameter can be changed by reloading the <productname>Pgpool-II</> configurations.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry id="guc-read-only-function-list" xreflabel="read_only_function_list">
    <term><varname>read_only_function_list</varname> (<type>string</type>)
     <indexterm>
      <primary><varname>read_only_function_list</varname> configuration parameter</primary>
     </indexterm>
    </term>
    <listitem>
     <para>
      Specifies a comma separated list of function names that
      <emphasis>DO NOT</emphasis> update the database. SELECTs including
      functions <emphasis>not specified </emphasis> in this list are not load balanced.
      These are replicated among all the DB nodes in Replication mode,
      sent to the primary node only in other mode.
     </para>
     <para>
      You can use regular expression to match function names,
      to which <literal>^</> and <literal>$</> are automatically added.
     </para>

     <example id="example-read-only-function-list-1">
      <title>Using regular expression</title>
      <para>
       If you have prefixed all your read only function
       with 'get_' or 'select_', You can
       set the <xref linkend="guc-read-only-function-list"> like below:
	<programlisting>
read_only_function_list = 'get_.*,select_.*'
	</programlisting>
      </para>
     </example>

     <note>
      <para>
       If the queries can refer to the function with and without the schema
       qualification then you must add both entries (with and without
       schema name) in the list.
       <programlisting>
#For example:
#If the queries sometime use "f1()" and other times "public.f1()"
#to refer the function f1 then the read_only_function_list
#would be configured as follows.

read_only_function_list = "f1,public.f1"
       </programlisting>

      </para>
     </note>

     <note>
      <para>
       If this parameter and <xref linkend="guc-write-function-list">
       is empty string, function's volatile property will be checked. If
       the property is volatile, the function is regarded as a writing
       function.  This is convenient and recommended way. However this
       requires one extra query against system catalog for the first
       time (in the next time cached query result is used and no extra
       query will be sent). If you don't want to send such query, you
       can keep on using this parameter.
      </para>
     </note>

     <para>
      This parameter can be changed by reloading the <productname>Pgpool-II</> configurations.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry id="guc-write-function-list" xreflabel="write_function_list">
    <term><varname>write_function_list</varname> (<type>string</type>)
     <indexterm>
      <primary><varname>write_function_list</varname> configuration parameter</primary>
     </indexterm>
    </term>
    <listitem>
     <para>
      Specifies a comma separated list of function names that
      <emphasis>DO</emphasis> update the database.
      SELECTs including functions <emphasis>specified</emphasis> in this list are
      not load balanced.
      These are replicated among all the DB nodes in Replication mode,
      sent to the primary node only in other mode.
     </para>
     <para>
      You can use regular expression to match function names,
      to which <literal>^</> and <literal>$</> are automatically added.
     </para>

     <example id="example-write-function-list-1">
      <title>Using regular expression</title>
      <para>
       If you have prefixed all your updating functions
       with 'set_', 'update_', 'delete_' or 'insert_', You can
       set the <xref linkend="guc-write-function-list"> like below:
	<programlisting>
write_function_list = 'nextval,setval,set_.*,update_.*,delete_.*,insert_.*'
	</programlisting>
      </para>
     </example>

     <note>
      <para>
       If the queries can refer the function with and without the schema
       qualification then you must add both entries(with and without
       schema name) in the list.
       <programlisting>
#For example:
#If the queries sometime use "f1()" and other times "public.f1()"
#to refer the function f1 then the write_function_list
#would be configured as follows.

write_function_list = "f1,public.f1"
       </programlisting>

      </para>
     </note>

     <note>
      <para>
       <xref linkend="guc-write-function-list"> and <xref linkend="guc-read-only-function-list">
	 are mutually exclusive and only one of the two lists can be set in the configuration.
      </para>
     </note>

     <example id="example-write-function-list-2">
      <title>Configuring using <literal>nextval()</literal> and <literal>setval()</literal> to land on proper backend</title>
      <para>
       Prior to <productname>Pgpool-II</productname><emphasis>V3.0</emphasis>,
       <literal>nextval()</literal> and <literal>setval()</literal> were known as functions writing to the database.
       You can configure this by setting <xref linkend="guc-write-function-list">
	and <xref linkend="guc-read-only-function-list"> as follows
	 <programlisting>
read_only_function_list = ''
write_function_list = 'nextval,setval,lastval,currval'
	 </programlisting>
      </para>
     </example>

     <note>
      <para>
       <productname>PostgreSQL</> also contains <literal>lastval()</literal> and
       <literal>currval()</literal> in addition to
       <literal>nextval()</literal> and <literal>setval()</literal>.
       Though <literal>lastval()</literal> and <literal>currval()</literal>
       are not writing function type, but it is advised to treat
       <literal>lastval()</literal> and <literal>currval()</literal>
       as writing functions to avoid errors which occur when
       these functions are accidentally load balanced.
      </para>
     </note>

     <note>
      <para>
       If this parameter and <xref linkend="guc-read-only-function-list">
       is empty string, function's volatile proper will be checked. If
       the property is volatile, the function is regarded as a writing
       function.  This is convenient and recommended way. However this
       requires one extra query against system catalog for the first
       time (in the next time cached query result is used and no extra
       query will be sent). If you don't want to send such query, you
       can keep on using this parameter.
      </para>
     </note>

     <para>
      This parameter can be changed by reloading the <productname>Pgpool-II</> configurations.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry id="guc-primary-routing-query-pattern-list" xreflabel="primary_routing_query_pattern_list">
    <term><varname>primary_routing_query_pattern_list</varname> (<type>string</type>)
     <indexterm>
      <primary><varname>primary_routing_query_pattern_list</varname> configuration parameter</primary>
     </indexterm>
    </term>
    <listitem>
     <para>
      Specifies a semicolon separated list of SQL patterns that
      should be sent to primary node.
      SQL that matched patterns specified in this list are
      not load balanced.
      Other than Only Native Replication mode is supported.
     </para>
     <para>
      You can use regular expression to match SQL patterns,
      to which <literal>^</> and <literal>$</> are automatically added.
      When using  special characters in regular expressions 
      (such as "'", ";", "*", "(", ")", "|", "+", ".", "\", "?", "^", "$",
      "{","}", "{" or "}", etc.)
      in SQL patterns, you need to escape them by using "\".
      SQL pattern specified in this parameter is case-insensitive.
     </para>

     <example id="example-primary-routing-query-pattern-list-1">
      <title>Using regular expression</title>
      <para>
       If the following SQL should be sent to the primary node only, You can
       set the <xref linkend="guc-primary-routing-query-pattern-list"> like below:
	<itemizedlist>
	 <listitem>
	  <para>
	   SELECT * FROM table_name1;
	  </para>
	 </listitem>
	 <listitem>
	  <para>
	   SELECT col1, col2 FROM table_name2 WHERE col1 LIKE '%a%';
	  </para>
	 </listitem>
	 <listitem>
	  <para>
	   SQL including table_name3
	  </para>
	 </listitem>
	</itemizedlist>
      </para>

      <para>
       <programlisting>
primary_routing_query_pattern_list = 'SELECT \* FROM table_name1\;;SELECT col1, col2 FROM table_name2 WHERE col1 LIKE \'%a%\'\;;.*table_name3.*'
       </programlisting>
      </para>
     </example>

     <note>
      <para>
       If SQL matches both <xref linkend="guc-write-function-list"> and
	<xref linkend="guc-read-only-function-list">, <xref linkend="guc-read-only-function-list">
	  setting is ignored and the SQL should be sent only to the primary node.
      </para>
     </note>
     <para>
      Depending on the SQL patterns, performance might be 1-2% lower when using this feature.
     </para>
     <para>
      This parameter can be changed by reloading the <productname>Pgpool-II</> configurations.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry id="guc-user-redirect-preference-list" xreflabel="user_redirect_preference_list">
    <term><varname>user_redirect_preference_list</varname> (<type>string</type>)
     <indexterm>
      <primary><varname>user_redirect_preference_list</varname> configuration parameter</primary>
     </indexterm>
    </term>
    <listitem>
     <para>
      Specifies the list of <replaceable>"user-name:node id(ratio)"</replaceable> pairs
      to send <acronym>SELECT</acronym> queries to a particular backend
      node for a particular user connection at a specified load balance ratio.
      The load balance ratio specifies a value between 0 and 1. The default is 1.0.
     </para>
     <para>
      For example, by specifying "user1:1(0.5)", <productname>Pgpool-II</productname>
      will redirect 50% <acronym>SELECT</acronym> queries to the backend node of ID 1 for
      the connection of user <literal>user1</literal>.
     </para>
     <para>
      You can specify multiple <replaceable>"user-name:node id(ratio)"</replaceable> pairs
      by separating them using comma (,).
     </para>
     <para>
      Regular expressions are also accepted for user name.
      You can use special keywords as <replaceable>node id</replaceable>.
      If <emphasis>"primary"</emphasis> is specified, queries are sent to the primary node, and
      if <emphasis>"standby"</emphasis> is specified, one of the standby nodes are selected randomly
      based on load balance ratio.
     </para>

     <example id="example-user-redirect-list">
      <title>Using user_redirect_preference_list</title>
      <para>
       If you want to configure the following <acronym>SELECT</acronym> query routing rules:
      </para>

      <itemizedlist>
       <listitem>
        <para>
         Route all <acronym>SELECT</acronym> queries from the connections
         using user <literal>postgres</literal> to the primary backend node.
        </para>
       </listitem>
       <listitem>
        <para>
         Route 30% <acronym>SELECT</acronym> queries from the connections
         using user <literal>user0</literal> or <literal>user1</literal>
         to backend node of ID 1.
         The other 70% <acronym>SELECT</acronym> queries will be sent to other backend nodes.
        </para>
       </listitem>
       <listitem>
        <para>
         Route all <acronym>SELECT</acronym> queries from the connections
         using user <literal>user2</literal> to standby backend nodes.
        </para>
       </listitem>

      </itemizedlist>
      <para>
       then the <xref linkend="guc-user-redirect-preference-list"> will be configured as follows:
       <programlisting>
user_redirect_preference_list = 'postgres:primary,user[01]:1(0.3),user2:standby'
       </programlisting>
      </para>
     </example>

     <para>
      This parameter can be changed by reloading the <productname>Pgpool-II</> configurations.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry id="guc-database-redirect-preference-list" xreflabel="database_redirect_preference_list">
    <term><varname>database_redirect_preference_list</varname> (<type>string</type>)
     <indexterm>
      <primary><varname>database_redirect_preference_list</varname> configuration parameter</primary>
     </indexterm>
    </term>
    <listitem>
     <para>
      Specifies the list of <replaceable>"database-name:node id(ratio)"</replaceable> pairs
      to send <acronym>SELECT</acronym> queries to a particular backend
      node for a particular database connection at a specified load balance ratio.
      The load balance ratio specifies a value between 0 and 1. The default is 1.0.
     </para>
     <para>
      For example, by specifying "test:1(0.5)", <productname>Pgpool-II</productname>
      will redirect 50% <acronym>SELECT</acronym> queries to the backend node of ID 1 for
      the connection to <literal>test</literal> database.
     </para>
     <para>
      You can specify multiple <replaceable>"database-name:node id(ratio)"</replaceable> pairs
      by separating them using comma (,).
     </para>
     <para>
      Regular expressions are also accepted for database name.
      You can use special keywords as <replaceable>node id</replaceable>.
      If <emphasis>"primary"</emphasis> is specified, queries are sent to the primary node, and
      if <emphasis>"standby"</emphasis> is specified, one of the standby nodes are selected randomly
      based on load balance ratio.
     </para>

     <example id="example-database-redirect-list">
      <title>Using database_redirect_preference_list</title>
      <para>
       If you want to configure the following <acronym>SELECT</acronym> query routing rules:
      </para>

      <itemizedlist>
       <listitem>
        <para>
         Route all <acronym>SELECT</acronym> queries on <literal>postgres</literal>
         database to the primary backend node.
        </para>
       </listitem>
       <listitem>
        <para>
         Route 30% <acronym>SELECT</acronym> queries on <literal>mydb0</literal> or on
         <literal>mydb1</literal> databases to backend node of ID 1.
         The other 70% <acronym>SELECT</acronym> queries will be sent to other backend nodes.
        </para>
       </listitem>
       <listitem>
        <para>
         Route all <acronym>SELECT</acronym> queries on <literal>mydb2</literal>
         database to standby backend nodes.
        </para>
       </listitem>

      </itemizedlist>
      <para>
       then the <xref linkend="guc-database-redirect-preference-list"> will be configured as follows:
       <programlisting>
database_redirect_preference_list = 'postgres:primary,mydb[01]:1(0.3),mydb2:standby'
       </programlisting>
      </para>
     </example>

     <para>
      This parameter can be changed by reloading the <productname>Pgpool-II</> configurations.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry id="guc-app-name-redirect-preference-list" xreflabel="app_name_redirect_preference_list">
    <term><varname>app_name_redirect_preference_list</varname> (<type>string</type>)
     <indexterm>
      <primary><varname>app_name_redirect_preference_list</varname> configuration parameter</primary>
     </indexterm>
    </term>
    <listitem>

     <para>
      Specifies the list of <replaceable>"application-name:node id(ratio)"</replaceable> pairs
      to send <acronym>SELECT</acronym> queries to a particular backend
      node for a particular client application connection at a specified load balance ratio.
      The load balance ratio specifies a value between 0 and 1. The default is 1.0.
      You can specify multiple <replaceable>"application-name:node id(ratio)"</replaceable> pairs
      by separating them using comma (,).
     </para>

     <note>
      <para>
       In <productname>PostgreSQL</> <emphasis>V9.0</> or later the "Application name" is a name specified
       by a client when it connects to database.
      </para>
     </note>

     <para>
      For example, application name of <command>psql</command> command is
      <literal>"psql"</literal>.
     </para>

     <note>
      <para>
       <productname>Pgpool-II</productname> recognizes the application name
       only specified in the start-up packet.
       Although a client can provide the application name
       later in the session, but that does not get considered by the
       <productname>Pgpool-II</productname> for query routing.
      </para>
     </note>

     <para>
      Regular expressions are also accepted for database name.
      You can use special keywords as <replaceable>node id</replaceable>.
      If <emphasis>"primary"</emphasis> is specified, queries are sent to the primary node, and
      if <emphasis>"standby"</emphasis> is specified, one of the standby nodes are selected randomly
      based on load balance ratio.
     </para>

     <example id="example-app-name-redirect-list">
      <title>Using app-name_redirect_preference_list</title>
      <para>
       If you want to configure the following <acronym>SELECT</acronym> query routing rules:
      </para>

      <itemizedlist>
       <listitem>
        <para>
         Route all <acronym>SELECT</acronym> from <literal>psql</literal>
         client to the primary backend node.
        </para>
       </listitem>
       <listitem>
        <para>
         Route 30% <acronym>SELECT</acronym> queries from <literal>myapp1</literal>
         client to backend node of ID 1. The other 70% SELECT queries will be sent to other backend nodes.
        </para>
       </listitem>
       <listitem>
        <para>
         Route all <acronym>SELECT</acronym> queries from <literal>myapp2</literal>
         client to standby backend nodes.
        </para>
       </listitem>

      </itemizedlist>
      <para>
       then the <xref linkend="guc-app-name-redirect-preference-list"> will be configured as follows:
       <programlisting>
app_name_redirect_preference_list = 'psql:primary,myapp1:1(0.3),myapp2:standby'
       </programlisting>
      </para>
     </example>

     <note>
      <para>
       The priority of <xref linkend="guc-user-redirect-preference-list">,
       <xref linkend="guc-database-redirect-preference-list"> and
       <xref linkend="guc-app-name-redirect-preference-list"> are:
       <programlisting>
app_name_redirect_preference_list &gt; database_redirect_preference_list &gt; user_redirect_preference_list
       </programlisting>
      </para>
      <para>
       For example, if you set
       <literal>database_redirect_preference_list = 'postgres:standby(1.0)'</literal> and
       <literal>app_name_redirect_preference_list = 'myapp1:primary(1.0)'</literal>,
       all SELECT from application myapp1 on postgres database will be sent to primary backend node.
      </para>
     </note>

     <note>
      <para>
       By specifying of <xref linkend="guc-user-redirect-preference-list">,
       <xref linkend="guc-database-redirect-preference-list"> and
       <xref linkend="guc-app-name-redirect-preference-list">,
       when multiple database names and application names are matched,
       the first setting will be used.
      </para>
      <para>
       For example, if you set
       <literal>database_redirect_preference_list = 'postgres:primary,postgres:standby'</literal>,
       <literal>"postgres: primary"</literal> will be used.
      </para>
     </note>

     <caution>
      <para>
       <acronym>JDBC</acronym> driver PostgreSQL-9.3 and earlier versions
       does not send the application name in the startup packet even if
       the application name is specified using the <acronym>JDBC</acronym>
       driver option <literal>"ApplicationName"</literal> and
       <literal>"assumeMinServerVersion=9.0"</literal>.
       So if you want to use the <xref linkend="guc-app-name-redirect-preference-list">
       feature through <acronym>JDBC</acronym>, Use PostgreSQL-9.4 or later version of the driver.
      </para>
     </caution>

     <para>
      This parameter can be changed by reloading the <productname>Pgpool-II</> configurations.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry id="guc-allow-sql-comments" xreflabel="allow_sql_comments">
    <term><varname>allow_sql_comments</varname> (<type>boolean</type>)
     <indexterm>
      <primary><varname>allow_sql_comments</varname> configuration parameter</primary>
     </indexterm>
    </term>
    <listitem>
     <para>
      When set to on, <productname>Pgpool-II</productname> ignore the
      <acronym>SQL</acronym> comments when identifying if the load balance
      or query cache is possible on the query.
      When this parameter is set to off, the <acronym>SQL</acronym> comments
      on the query could effectively prevent the query from being
      load balanced or cached (pre <productname>Pgpool-II</productname>
      <emphasis>V3.4</emphasis> behavior).
     </para>
     <para>
      This parameter can be changed by reloading the <productname>Pgpool-II</> configurations.
      You can also use <xref linkend="SQL-PGPOOL-SET"> command to alter the value of
       this parameter for a current session.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry id="guc-disable-load-balance-on-write" xreflabel="disable_load_balance_on_write">
    <term><varname>disable_load_balance_on_write</varname> (<type>enum</type>)
     <indexterm>
      <primary><varname>disable_load_balance_on_write</varname> configuration parameter</primary>
     </indexterm>
    </term>
    <listitem>
     <para>
      Specify load balance behavior after write queries appear.
      This parameter is especially useful in streaming replication
      mode. When write queries are sent to primary server, the
      changes are applied to standby servers but there's a time
      lag. So if a client read the same row right after the write
      query, the client may not see the latest value of the
      row. If that's the problem, clients should always read data
      from the primary server. However this effectively disables
      load balancing, which leads to lesser performance. This
      parameter allows a fine tuning for the trade off between
      not-clustering-aware applications compatibility and
      performance.
     </para>
     <para>
      If this parameter is set to <varname>off</varname>, read
      queries are load balanced even if write queries appear. This
      gives the best load balance performance but clients may see
      older data. This is useful for an environment where
      PostgreSQL parameter synchronous_commit = 'remote_apply', or
      in the native replication mode, since there's no replication
      delay in such environments.
     </para>
     <para>
      If this parameter is set to <varname>transaction</varname>
      and write queries appear in an explicit transaction,
      subsequent read queries are not load balanced until the
      transaction ends.  Please note that read queries not in an
      explicit transaction are not affected by the parameter. This
      setting gives the best balance in most cases and you should
      start from this. This is the default and same behavior in
      <productname>Pgpool-II 3.7</productname> or before.
     </para>
     <para>
      If this parameter is set
      to <varname>trans_transaction</varname> and write queries
      appear in an explicit transaction, subsequent read queries
      are not load balanced in the transaction and subsequent
      explicit transaction until the session ends. So this
      parameter is safer for older applications but give lesser
      performance than <varname>transaction</varname>. Please note
      that read queries not in an explicit transaction are not
      affected by the parameter.
     </para>

     <para>
      If this parameter is set to <varname>always</varname> and
      write queries appear, subsequent read queries are not load
      balanced until the session ends regardless they are in
      explicit transactions or not. This gives the highest
      compatibility with not-clustering-aware applications and the
      lowest performance.
     </para>

     <para>
		If this parameter is set to <varname>dml_adaptive</varname> <productname>Pgpool-II</>
		keep track of each TABLE referenced in the WRITE statements within
		the explicit transactions and will not load balances the subsequent
		READ queries if the TABLE they are reading from is previously modified
		inside the same transaction.
		Dependent functions, triggers, and views on the tables can be configured
		using <xref linkend="guc-dml-adaptive-object-relationship-list">
     </para>
    </listitem>
   </varlistentry>

   <varlistentry id="guc-dml-adaptive-object-relationship-list" xreflabel="dml_adaptive_object_relationship_list">
    <term><varname>dml_adaptive_object_relationship_list</varname> (<type>string</type>)
     <indexterm>
      <primary><varname>dml_adaptive_object_relationship_list</varname> configuration parameter</primary>
     </indexterm>
    </term>
    <listitem>

     <para>
	 To prevent load balancing of READ dependent objects, you may specify the object name
	 followed by a colon(<literal>:</>) and then a comma(<literal>,</>) separated list of dependent object names.
	 <replaceable>"[object]:[dependent-object]"</replaceable>
     In an explicit transaction block after a WRITE statement has been issues, this will prevent
	 load balancing of any READ statements containing references of dependent object(s).
	 <example id="example-dml-adaptive-object-relationship-list-1">
	 <title>Configuring dml adaptive object relationship</title>
	   <para>
	    If you have a trigger installed on table_1 that do INSERT in  <literal>table_2</> for each
		INSERT on  <literal>table_1</>. Then you would want to make sure that
		read on  <literal>table_2</> must not get load-balanced within the same transaction
		after INSERT into  <literal>table_1</>.
		For this configuration you can set
		<programlisting>
dml_adaptive_object_relationship_list = 'table_1:table_2'
		</programlisting>
	  </para>
	 </example>

     This parameter is only valid for
	 <xref linkend="guc-disable-load-balance-on-write">=<emphasis>'dml_adaptive'</emphasis>

	  <note>
	   <para>
	   To configure the dependency on the function,
	   The function must be present in the <xref linkend="guc-write-function-list">
	   </para>
	  </note>

     </para>

    </listitem>
   </varlistentry>

   <varlistentry id="guc-statement-level-load-balance" xreflabel="statement_level_load_balance">
    <term><varname>statement_level_load_balance</varname> (<type>boolean</type>)
     <indexterm>
      <primary><varname>statement_level_load_balance</varname> configuration parameter</primary>
     </indexterm>
    </term>
    <listitem>
     <para>
      When set to on and <xref linkend="guc-load-balance-mode"> is set to on, the load balancing node is decided for each read query.
      When set to off, load balancing node is decided at the session start time
      and will not be changed until the session ends.
      For example, in applications that use connection pooling remain connections
      open to the backend server, because the session may be held for a long time,
      the load balancing node does not change until the session ends.
      In such applications, when <varname>statement_level_load_balance</varname> is enabled,
      it is possible to decide load balancing node per query, not per session.
      The default is off.
     </para>

     <note>
      <para>
       In streaming replication mode, certain kind of queries such as
       BEGIN/END/COMMIT/ABORT/SET/SAVEPOINT/RELEASE SAVEPOINT/DEALLOCATE ALL/DISCARD are sent to
       primary node and load balance node. If
       <xref linkend="guc-statement-level-load-balance"> is on, such
       queries are sent to all standby nodes as well. This is not
       usually a problem. But when one of standbys are in remote
       network, the network latency may cause significant slow down in
       case of such queries.
      </para>
     </note>
     <para>
      This parameter can be changed by reloading the <productname>Pgpool-II</> configurations.
     </para>
    </listitem>
   </varlistentry>

  </variablelist>
 </sect2>
</sect1>