-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool_select_walker.c
1112 lines (937 loc) · 26 KB
/
pool_select_walker.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-2012 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 <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "pool.h"
#include "pool_config.h"
#include "pool_select_walker.h"
#include "pool_relcache.h"
#include "parser/parsenodes.h"
#include "pool_session_context.h"
#include "pool_timestamp.h"
static bool function_call_walker(Node *node, void *context);
static bool system_catalog_walker(Node *node, void *context);
static bool is_system_catalog(char *table_name);
static bool temp_table_walker(Node *node, void *context);
static bool unlogged_table_walker(Node *node, void *context);
static bool view_walker(Node *node, void *context);
static bool is_temp_table(char *table_name);
static bool insertinto_or_locking_clause_walker(Node *node, void *context);
static bool is_immutable_function(char *fname);
static bool select_table_walker(Node *node, void *context);
static bool non_immutable_function_call_walker(Node *node, void *context);
static char *strip_quote(char *str);
static char *make_table_name_from_rangevar(RangeVar *rangevar);
/*
* Return true if this SELECT has function calls *and* supposed to
* modify database. We check black/white function list to determine
* whether the function modifies database.
*/
bool pool_has_function_call(Node *node)
{
SelectContext ctx;
if (!IsA(node, SelectStmt))
return false;
ctx.has_function_call = false;
raw_expression_tree_walker(node, function_call_walker, &ctx);
return ctx.has_function_call;
}
/*
* Return true if this SELECT has system catalog table.
*/
bool pool_has_system_catalog(Node *node)
{
SelectContext ctx;
if (!IsA(node, SelectStmt))
return false;
ctx.has_system_catalog = false;
raw_expression_tree_walker(node, system_catalog_walker, &ctx);
return ctx.has_system_catalog;
}
/*
* Return true if this SELECT has temporary table.
*/
bool pool_has_temp_table(Node *node)
{
SelectContext ctx;
if (!IsA(node, SelectStmt))
return false;
ctx.has_temp_table = false;
raw_expression_tree_walker(node, temp_table_walker, &ctx);
return ctx.has_temp_table;
}
/*
* Return true if this SELECT has unlogged table.
*/
bool pool_has_unlogged_table(Node *node)
{
SelectContext ctx;
if (!IsA(node, SelectStmt))
return false;
ctx.has_unlogged_table = false;
raw_expression_tree_walker(node, unlogged_table_walker, &ctx);
return ctx.has_unlogged_table;
}
/*
* Return true if this SELECT has a view.
*/
bool pool_has_view(Node *node)
{
SelectContext ctx;
if (!IsA(node, SelectStmt))
return false;
ctx.has_view = false;
raw_expression_tree_walker(node, view_walker, &ctx);
return ctx.has_view;
}
/*
* Return true if this SELECT has INSERT INTO or FOR SHARE or FOR UDPATE.
*/
bool pool_has_insertinto_or_locking_clause(Node *node)
{
SelectContext ctx;
if (!IsA(node, SelectStmt))
return false;
ctx.has_insertinto_or_locking_clause = false;
raw_expression_tree_walker(node, insertinto_or_locking_clause_walker, &ctx);
pool_debug("pool_has_insertinto_or_locking_clause: returns %d",
ctx.has_insertinto_or_locking_clause);
return ctx.has_insertinto_or_locking_clause;
}
/*
* Search function name in whilelist or blacklist regex array
* Return 1 on success (found in list)
* Return 0 when not found in list
* Return -1 if the given search type doesn't exist.
* Search type supported are: WHITELIST and BLACKLIST
*/
int pattern_compare(char *str, const int type, const char *param_name)
{
int i = 0;
RegPattern *lists_patterns;
int *pattc;
if (strcmp(param_name, "white_function_list") == 0 ||
strcmp(param_name, "black_function_list") == 0)
{
lists_patterns = pool_config->lists_patterns;
pattc = &pool_config->pattc;
} else if (strcmp(param_name, "white_memqcache_table_list") == 0 ||
strcmp(param_name, "black_memqcache_table_list") == 0)
{
lists_patterns = pool_config->lists_memqcache_table_patterns;
pattc = &pool_config->memqcache_table_pattc;
} else {
pool_error("pattern_compare: unknown paramname %s", param_name);
return -1;
}
for (i = 0; i < *pattc; i++) {
if (lists_patterns[i].type != type)
continue;
if (regexec(&lists_patterns[i].regexv, str, 0, 0, 0) == 0)
{
switch(type) {
/* return 1 if string matches whitelist pattern */
case WHITELIST:
pool_debug("pattern_compare: %s (%s) matched: %s",
param_name, lists_patterns[i].pattern, str);
return 1;
/* return 1 if string matches blacklist pattern */
case BLACKLIST:
pool_debug("pattern_compare: %s (%s) matched: %s",
param_name, lists_patterns[i].pattern, str);
return 1;
default:
pool_error("pattern_compare: %s unknown pattern match type: %s", param_name, str);
return -1;
}
}
pool_debug("pattern_compare: %s (%s) not matched: %s",
param_name, lists_patterns[i].pattern, str);
}
/* return 0 otherwise */
return 0;
}
/*
* Walker function to find a function call which is supposed to write
* database.
*/
static bool function_call_walker(Node *node, void *context)
{
SelectContext *ctx = (SelectContext *) context;
if (node == NULL)
return false;
if (IsA(node, FuncCall))
{
FuncCall *fcall = (FuncCall *)node;
char *fname;
int length = list_length(fcall->funcname);
if (length > 0)
{
if (length == 1) /* no schema qualification? */
{
fname = strVal(linitial(fcall->funcname));
}
else
{
fname = strVal(lsecond(fcall->funcname)); /* with schema qualification */
}
pool_debug("function_call_walker: function name: %s", fname);
/*
* Check white list if any.
*/
if (pool_config->num_white_function_list > 0)
{
/* Search function in the white list regex patterns */
if (pattern_compare(fname, WHITELIST, "white_function_list") == 1) {
/* If the function is found in the white list, we can ignore it */
return raw_expression_tree_walker(node, function_call_walker, context);
}
/*
* Since the function was not found in white list, we
* have found a writing function.
*/
ctx->has_function_call = true;
return false;
}
/*
* Check black list if any.
*/
if (pool_config->num_black_function_list > 0)
{
/* Search function in the black list regex patterns */
if (pattern_compare(fname, BLACKLIST, "black_function_list") == 1) {
/* Found. */
ctx->has_function_call = true;
return false;
}
}
}
}
return raw_expression_tree_walker(node, function_call_walker, context);
}
/*
* Walker function to find a system catalog
*/
static bool
system_catalog_walker(Node *node, void *context)
{
SelectContext *ctx = (SelectContext *) context;
if (node == NULL)
return false;
if (IsA(node, RangeVar))
{
RangeVar *rgv = (RangeVar *)node;
pool_debug("system_catalog_walker: relname: %s", rgv->relname);
if (is_system_catalog(rgv->relname))
{
ctx->has_system_catalog = true;
return false;
}
}
return raw_expression_tree_walker(node, system_catalog_walker, context);
}
/*
* Walker function to find a temp table
*/
static bool
temp_table_walker(Node *node, void *context)
{
SelectContext *ctx = (SelectContext *) context;
if (node == NULL)
return false;
if (IsA(node, RangeVar))
{
RangeVar *rgv = (RangeVar *)node;
pool_debug("temp_table_walker: relname: %s", rgv->relname);
if (is_temp_table(rgv->relname))
{
ctx->has_temp_table = true;
return false;
}
}
return raw_expression_tree_walker(node, temp_table_walker, context);
}
/*
* Walker function to find a unlogged table
*/
static bool
unlogged_table_walker(Node *node, void *context)
{
SelectContext *ctx = (SelectContext *) context;
char *relname;
if (node == NULL)
return false;
if (IsA(node, RangeVar))
{
RangeVar *rgv = (RangeVar *)node;
relname = make_table_name_from_rangevar(rgv);
pool_debug("unlogged_table_walker: relname: %s", relname);
if (is_unlogged_table(relname))
{
ctx->has_unlogged_table = true;
return false;
}
}
return raw_expression_tree_walker(node, unlogged_table_walker, context);
}
/*
* Walker function to find a view
*/
static bool
view_walker(Node *node, void *context)
{
SelectContext *ctx = (SelectContext *) context;
char *relname;
if (node == NULL)
return false;
if (IsA(node, RangeVar))
{
RangeVar *rgv = (RangeVar *)node;
relname = make_table_name_from_rangevar(rgv);
pool_debug("view_walker: relname: %s", relname);
if (is_view(relname))
{
ctx->has_view = true;
return false;
}
}
return raw_expression_tree_walker(node, view_walker, context);
}
/*
* Judge the table used in a query represented by node is a system
* catalog or not.
*/
static bool is_system_catalog(char *table_name)
{
/*
* Query to know if pg_namespace exists. PostgreSQL 7.2 or before doesn't have.
*/
#define HASPGNAMESPACEQUERY "SELECT count(*) FROM pg_catalog.pg_class AS c WHERE c.relname = '%s'"
/*
* Query to know if the target table belongs pg_catalog schema.
*/
#define ISBELONGTOPGCATALOGQUERY "SELECT count(*) FROM pg_class AS c, pg_namespace AS n WHERE c.relname = '%s' AND c.relnamespace = n.oid AND n.nspname = 'pg_catalog'"
#define ISBELONGTOPGCATALOGQUERY2 "SELECT count(*) FROM pg_class AS c, pg_namespace AS n WHERE c.oid = pgpool_regclass('%s') AND c.relnamespace = n.oid AND n.nspname = 'pg_catalog'"
int hasreliscatalog;
bool result;
static POOL_RELCACHE *hasreliscatalog_cache;
static POOL_RELCACHE *relcache;
POOL_CONNECTION_POOL *backend;
if (table_name == NULL)
{
return false;
}
backend = pool_get_session_context()->backend;
/*
* Check if pg_namespace exists
*/
if (!hasreliscatalog_cache)
{
char *query;
/* pgpool_regclass has been installed */
if (pool_has_pgpool_regclass())
{
query = ISBELONGTOPGCATALOGQUERY2;
}
else
{
query = ISBELONGTOPGCATALOGQUERY;
}
hasreliscatalog_cache = pool_create_relcache(pool_config->relcache_size, query,
int_register_func, int_unregister_func,
false);
if (hasreliscatalog_cache == NULL)
{
pool_error("is_system_catalog: pool_create_relcache error");
return false;
}
}
hasreliscatalog = pool_search_relcache(hasreliscatalog_cache, backend, "pg_namespace")==0?0:1;
if (hasreliscatalog)
{
/*
* If relcache does not exist, create it.
*/
if (!relcache)
{
char *query;
/* pgpool_regclass has been installed */
if (pool_has_pgpool_regclass())
{
query = ISBELONGTOPGCATALOGQUERY2;
}
else
{
query = ISBELONGTOPGCATALOGQUERY;
}
relcache = pool_create_relcache(pool_config->relcache_size, query,
int_register_func, int_unregister_func,
false);
if (relcache == NULL)
{
pool_error("is_system_catalog: pool_create_relcache error");
return false;
}
}
/*
* Search relcache.
*/
result = pool_search_relcache(relcache, backend, table_name)==0?false:true;
return result;
}
/*
* Pre 7.3. Just check whether the table starts with "pg_".
*/
return (strcasecmp(table_name, "pg_") == 0);
}
/*
* Judge the table used in a query represented by node is a temporary
* table or not.
*/
static POOL_RELCACHE *is_temp_table_relcache;
static bool is_temp_table(char *table_name)
{
/*
* Query to know if pg_class has relistemp column or not.
* PostgreSQL 8.4 and 9.0 have this.
*/
#define HASRELITEMPPQUERY "SELECT count(*) FROM pg_catalog.pg_class AS c, pg_attribute AS a WHERE c.relname = 'pg_class' AND a.attrelid = c.oid AND a.attname = 'relistemp'"
/*
* Query to know if the target table is a temporary one. This query
* is valid in PostgreSQL 7.3 to 8.3 and 9.1 or later. We do not use
* regclass (or its variant) here, because temporary tables never have
* schema qualified name.
*/
#define ISTEMPQUERY83 "SELECT count(*) FROM pg_class AS c, pg_namespace AS n WHERE c.relname = '%s' AND c.relnamespace = n.oid AND n.nspname ~ '^pg_temp_'"
/*
* Query to know if the target table is a temporary one. This query
* is valid in PostgreSQL 8.4 and 9.0. We do not use regclass (or its
* variant) here, because temporary tables never have schema qualified
* name.
*/
#define ISTEMPQUERY84 "SELECT count(*) FROM pg_catalog.pg_class AS c WHERE c.relname = '%s' AND c.relistemp"
int hasrelistemp;
bool result;
static POOL_RELCACHE *hasrelistemp_cache;
char *query;
POOL_CONNECTION_POOL *backend;
if (table_name == NULL)
{
return false;
}
backend = pool_get_session_context()->backend;
/*
* Check backend version
*/
if (!hasrelistemp_cache)
{
hasrelistemp_cache = pool_create_relcache(pool_config->relcache_size, HASRELITEMPPQUERY,
int_register_func, int_unregister_func,
false);
if (hasrelistemp_cache == NULL)
{
pool_error("is_temp_table: pool_create_relcache error");
return false;
}
}
hasrelistemp = pool_search_relcache(hasrelistemp_cache, backend, "pg_class")==0?0:1;
if (hasrelistemp)
query = ISTEMPQUERY84;
else
query = ISTEMPQUERY83;
/*
* If relcache does not exist, create it.
*/
if (!is_temp_table_relcache)
{
is_temp_table_relcache = pool_create_relcache(pool_config->relcache_size, query,
int_register_func, int_unregister_func,
true);
if (is_temp_table_relcache == NULL)
{
pool_error("is_temp_table: pool_create_relcache error");
return false;
}
}
/*
* Search relcache.
*/
result = pool_search_relcache(is_temp_table_relcache, backend, table_name)==0?false:true;
return result;
}
/*
* Discard relcache used by is_temp_table_relcache().
*/
void discard_temp_table_relcache(void)
{
if (is_temp_table_relcache)
{
pool_discard_relcache(is_temp_table_relcache);
is_temp_table_relcache = NULL;
}
}
/*
* Judge the table used in a query represented by node is a unlogged
* table or not.
*/
bool is_unlogged_table(char *table_name)
{
/*
* Query to know if pg_class has relpersistence column or not.
* PostgreSQL 9.1 or later has this.
*/
#define HASRELPERSISTENCEQUERY "SELECT count(*) FROM pg_catalog.pg_class AS c, pg_catalog.pg_attribute AS a WHERE c.relname = 'pg_class' AND a.attrelid = c.oid AND a.attname = 'relpersistence'"
/*
* Query to know if the target table is a unlogged one. This query
* is valid in PostgreSQL 9.1 or later.
*/
#define ISUNLOGGEDQUERY "SELECT count(*) FROM pg_catalog.pg_class AS c WHERE c.relname = '%s' AND c.relpersistence = 'u'"
#define ISUNLOGGEDQUERY2 "SELECT count(*) FROM pg_catalog.pg_class AS c WHERE c.oid = pgpool_regclass('%s') AND c.relpersistence = 'u'"
int hasrelpersistence;
static POOL_RELCACHE *hasrelpersistence_cache;
static POOL_RELCACHE *relcache;
POOL_CONNECTION_POOL *backend;
if (table_name == NULL)
{
return false;
}
backend = pool_get_session_context()->backend;
/*
* Check backend version
*/
if (!hasrelpersistence_cache)
{
hasrelpersistence_cache = pool_create_relcache(pool_config->relcache_size, HASRELPERSISTENCEQUERY,
int_register_func, int_unregister_func,
false);
if (hasrelpersistence_cache == NULL)
{
pool_error("is_unlogged_table: pool_create_relcache error");
return false;
}
}
hasrelpersistence = pool_search_relcache(hasrelpersistence_cache, backend, "pg_class")==0?0:1;
if (hasrelpersistence)
{
bool result;
char *query;
/* pgpool_regclass has been installed */
if (pool_has_pgpool_regclass())
{
query = ISUNLOGGEDQUERY2;
}
else
{
query = ISUNLOGGEDQUERY;
}
/*
* If relcache does not exist, create it.
*/
if (!relcache)
{
relcache = pool_create_relcache(pool_config->relcache_size, query,
int_register_func, int_unregister_func,
true);
if (relcache == NULL)
{
pool_error("is_unlogged_table: pool_create_relcache error");
return false;
}
}
/*
* Search relcache.
*/
result = pool_search_relcache(relcache, backend, table_name)==0?false:true;
return result;
}
else
{
return false;
}
}
/*
* Judge the table used in a query is a view or not.
*/
bool is_view(char *table_name)
{
/*
* Query to know if the target table is a view.
*/
#define ISVIEWQUERY "SELECT count(*) FROM pg_catalog.pg_class AS c WHERE c.relname = '%s' AND c.relkind = 'v'"
#define ISVIEWQUERY2 "SELECT count(*) FROM pg_catalog.pg_class AS c WHERE c.oid = pgpool_regclass('%s') AND c.relkind = 'v'"
static POOL_RELCACHE *relcache;
POOL_CONNECTION_POOL *backend;
bool result;
char *query;
if (table_name == NULL)
{
return false;
}
backend = pool_get_session_context()->backend;
/* pgpool_regclass has been installed */
if (pool_has_pgpool_regclass())
{
query = ISVIEWQUERY2;
}
else
{
query = ISVIEWQUERY;
}
if (!relcache)
{
relcache = pool_create_relcache(pool_config->relcache_size, query,
int_register_func, int_unregister_func,
false);
if (relcache == NULL)
{
pool_error("is_view: pool_create_relcache error");
return false;
}
}
/*
* Search relcache.
*/
result = pool_search_relcache(relcache, backend, table_name)==0?false:true;
return result;
}
/*
* Judge if we have pgpool_regclass or not.
*/
bool pool_has_pgpool_regclass(void)
{
/*
* Query to know if pgpool_regclass exists.
*/
#define HASPGPOOL_REGCLASSQUERY "SELECT count(*) FROM pg_catalog.pg_proc AS p WHERE p.proname = '%s'"
bool result;
static POOL_RELCACHE *relcache;
POOL_CONNECTION_POOL *backend;
backend = pool_get_session_context()->backend;
if (!relcache)
{
relcache = pool_create_relcache(32, HASPGPOOL_REGCLASSQUERY,
int_register_func, int_unregister_func,
false);
if (relcache == NULL)
{
pool_error("has_pgpool_regclass: pool_create_relcache error");
return false;
}
}
result = pool_search_relcache(relcache, backend, "pgpool_regclass")==0?0:1;
return result;
}
/*
* Walker function to find intoClause or lockingClause.
*/
static bool insertinto_or_locking_clause_walker(Node *node, void *context)
{
SelectContext *ctx = (SelectContext *) context;
if (node == NULL)
return false;
if (IsA(node, IntoClause) || IsA(node, LockingClause))
{
ctx->has_insertinto_or_locking_clause = true;
return false;
}
return raw_expression_tree_walker(node, insertinto_or_locking_clause_walker, ctx);
}
/*
* Return true if this SELECT has non immutable function calls.
*/
bool pool_has_non_immutable_function_call(Node *node)
{
SelectContext ctx;
if (!IsA(node, SelectStmt))
return false;
ctx.has_non_immutable_function_call = false;
raw_expression_tree_walker(node, non_immutable_function_call_walker, &ctx);
pool_debug("pool_has_non_immutable_function_call: %d", ctx.has_non_immutable_function_call);
return ctx.has_non_immutable_function_call;
}
/*
* Walker function to find non immutable function call.
*/
static bool non_immutable_function_call_walker(Node *node, void *context)
{
SelectContext *ctx = (SelectContext *) context;
if (node == NULL)
return false;
if (IsA(node, FuncCall))
{
FuncCall *fcall = (FuncCall *)node;
char *fname;
int length = list_length(fcall->funcname);
if (length > 0)
{
if (length == 1) /* no schema qualification? */
{
fname = strVal(linitial(fcall->funcname));
}
else
{
fname = strVal(lsecond(fcall->funcname)); /* with schema qualification */
}
pool_debug("non_immutable_function_call_walker: function name: %s", fname);
/* Check system catalog if the function is immutable */
if (is_immutable_function(fname) == false)
{
/* Non immutable function call found */
ctx->has_non_immutable_function_call = true;
return false;
}
}
}
else if (IsA(node, TypeCast))
{
/* CURRENT_DATE, CURRENT_TIME, LOCALTIMESTAMP, LOCALTIME etc.*/
TypeCast *tc = (TypeCast *) node;
if ((isSystemType((Node *) tc->typeName, "date") ||
isSystemType((Node *) tc->typeName, "timestamp") ||
isSystemType((Node *) tc->typeName, "timestamptz") ||
isSystemType((Node *) tc->typeName, "time") ||
isSystemType((Node *) tc->typeName, "timetz")))
{
ctx->has_non_immutable_function_call = true;
return false;
}
}
return raw_expression_tree_walker(node, non_immutable_function_call_walker, context);
}
/*
* Check if the function is stable.
*/
static bool is_immutable_function(char *fname)
{
/*
* Query to know if the function is IMMUTABLE
*/
#define IS_STABLE_FUNCTION_QUERY "SELECT count(*) FROM pg_catalog.pg_proc AS p WHERE p.proname = '%s' AND p.provolatile = 'i'"
bool result;
static POOL_RELCACHE *relcache;
POOL_CONNECTION_POOL *backend;
backend = pool_get_session_context()->backend;
if (!relcache)
{
relcache = pool_create_relcache(pool_config->relcache_size, IS_STABLE_FUNCTION_QUERY,
int_register_func, int_unregister_func,
false);
if (relcache == NULL)
{
pool_error("is_immutable_function: pool_create_relcache error");
return false;
}
pool_debug("is_immutable_function: relcache created");
}
result = pool_search_relcache(relcache, backend, fname)==0?0:1;
pool_debug("is_immutable_function: search result:%d", result);
return result;
}
/*
* Convert table_name(possibly including schema name) to oid
*/
int pool_table_name_to_oid(char *table_name)
{
/*
* Query to convert table name to oid
*/
#define TABLE_TO_OID_QUERY "SELECT pgpool_regclass('%s')"
#define TABLE_TO_OID_QUERY2 "SELECT oid FROM pg_class WHERE relname = '%s'"
int oid = 0;
static POOL_RELCACHE *relcache;
POOL_CONNECTION_POOL *backend;
char *query;
if (table_name == NULL)
{
return oid;
}
backend = pool_get_session_context()->backend;
if (pool_has_pgpool_regclass())
{
query = TABLE_TO_OID_QUERY;
}
else
{
query = TABLE_TO_OID_QUERY2;
}
/*
* If relcache does not exist, create it.
*/
if (!relcache)
{
relcache = pool_create_relcache(pool_config->relcache_size, query,
int_register_func, int_unregister_func,
true);
if (relcache == NULL)
{
pool_error("table_name_to_oid: pool_create_relcache error");
return oid;
}
/* Se do not cache if pgpool_regclass() returns 0, which indicates
* there's no such a table. In this case we do not want to cache the
* state because the table might be created later in this session.
*/
relcache->no_cache_if_zero = true;
}
/*
* Search relcache.
*/
oid = (int)(intptr_t)pool_search_relcache(relcache, backend, table_name);
return oid;
}
/*
* Extract table oids from SELECT statement. Returns number of oids.
* Oids are returned as an int array. The contents of oid array are
* discarded by next call to this function.
*/
int pool_extract_table_oids_from_select_stmt(Node *node, SelectContext *ctx)
{
if (!IsA(node, SelectStmt))
return 0;
ctx->num_oids = 0;
raw_expression_tree_walker(node, select_table_walker, ctx);
return ctx->num_oids;
}
/*
* Walker function to extract table oids from SELECT statement.
*/
static bool
select_table_walker(Node *node, void *context)
{
SelectContext *ctx = (SelectContext *) context;
int num_oids;
if (node == NULL)
return false;
if (IsA(node, RangeVar))
{
RangeVar *rgv = (RangeVar *)node;
char *table;
int oid;
char *s;
table = make_table_name_from_rangevar(rgv);
oid = pool_table_name_to_oid(table);
if (oid)
{
if (POOL_MAX_SELECT_OIDS <= ctx->num_oids)
{
pool_debug("select_table_walker: number of oids exceeds");
return false;
}
num_oids = ctx->num_oids++;
ctx->table_oids[num_oids] = oid;
s = strip_quote(table);