/
plpgsql_lint.c
1410 lines (1139 loc) · 35.3 KB
/
plpgsql_lint.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
#include "postgres.h"
#include "executor/spi_priv.h"
#include "fmgr.h"
#include "funcapi.h"
#include "plpgsql.h"
#include "catalog/pg_type.h"
#include "utils/builtins.h"
#include "utils/elog.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
#include "utils/typcache.h"
#if PG_VERSION_NUM >= 90300
#include "access/htup_details.h"
#endif
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
static void lint_func_beg( PLpgSQL_execstate * estate, PLpgSQL_function * func );
static void exec_prepare_plan(PLpgSQL_execstate *estate,
PLpgSQL_expr *expr, int cursorOptions);
static void check_target(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt, int varno, TupleDesc tupdesc);
static void check_row_or_rec(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt,
PLpgSQL_row *row, PLpgSQL_rec *rec,
TupleDesc tupdesc);
static void prepare_expr(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt, PLpgSQL_expr *expr, bool *fresh_plan);
static TupleDesc prepare_tupdesc(PLpgSQL_execstate *estate, PLpgSQL_expr *expr,
int target_varno,
bool fresh_plan,
bool use_element_type,
bool expand_record,
bool is_expression);
static TupleDesc prepare_tupdesc_novar(PLpgSQL_execstate *estate, PLpgSQL_expr *expr,
bool fresh_plan,
bool is_expression);
static TupleDesc prepare_tupdesc_row_or_rec(PLpgSQL_execstate *estate, PLpgSQL_expr *expr,
PLpgSQL_row *row,
PLpgSQL_rec *rec,
bool fresh_plan);
static void assign_tupdesc_dno(PLpgSQL_execstate *estate, int varno, TupleDesc tupdesc);
static void assign_tupdesc_row_or_rec(PLpgSQL_execstate *estate, PLpgSQL_row *row, PLpgSQL_rec *rec, TupleDesc tupdesc);
static void cleanup(bool fresh_plan, PLpgSQL_expr *expr, TupleDesc tupdesc);
static void lint_stmts(PLpgSQL_execstate *estate, PLpgSQL_function *func, List *stmts);
static void lint_stmt(PLpgSQL_execstate *estate, PLpgSQL_function *func, PLpgSQL_stmt *stmt);
static TupleDesc query_get_desc(PLpgSQL_execstate *estate, PLpgSQL_expr *query,
bool use_element_type,
bool expand_record,
bool is_expression);
static void simple_check_expr(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt, PLpgSQL_expr *expr);
#if PG_VERSION_NUM < 90000
static Oid exec_get_datum_type(PLpgSQL_execstate *estate,
PLpgSQL_datum *datum);
#endif
static PLpgSQL_plugin plugin_funcs = { NULL, lint_func_beg, NULL, NULL, NULL};
/*
* disable a lint for processing of current function
*
*/
static bool plpgsql_lint_enable;
void _PG_init(void)
{
PLpgSQL_plugin ** var_ptr = (PLpgSQL_plugin **) find_rendezvous_variable( "PLpgSQL_plugin" );
/* Be sure we do initialization only once (should be redundant now) */
static bool inited = false;
if (inited)
return;
*var_ptr = &plugin_funcs;
#if PG_VERSION_NUM >= 90100
DefineCustomBoolVariable("plpgsql.enable_lint",
"when is true, then plpgsql_lint is active",
NULL,
&plpgsql_lint_enable,
true,
PGC_SUSET, 0,
NULL, NULL, NULL);
#else
DefineCustomBoolVariable("plpgsql.enable_lint",
"when is true, then plpgsql_lint is active",
NULL,
&plpgsql_lint_enable,
true,
PGC_SUSET, 0,
NULL, NULL);
#endif
inited = true;
}
static void
lint_func_beg( PLpgSQL_execstate * estate, PLpgSQL_function * func )
{
const char *err_text = estate->err_text;
if (plpgsql_lint_enable)
{
int i;
PLpgSQL_rec *saved_records;
PLpgSQL_var *saved_vars;
MemoryContext oldcontext;
ResourceOwner oldowner;
/*
* inside control a rec and vars variables are modified, so we should to save their
* content
*/
saved_records = palloc(sizeof(PLpgSQL_rec) * estate->ndatums);
saved_vars = palloc(sizeof(PLpgSQL_var) * estate->ndatums);
for (i = 0; i < estate->ndatums; i++)
{
if (estate->datums[i]->dtype == PLPGSQL_DTYPE_REC)
{
PLpgSQL_rec *rec = (PLpgSQL_rec *) estate->datums[i];
saved_records[i].tup = rec->tup;
saved_records[i].tupdesc = rec->tupdesc;
saved_records[i].freetup = rec->freetup;
saved_records[i].freetupdesc = rec->freetupdesc;
/* don't release a original tupdesc and original tup */
rec->freetup = false;
rec->freetupdesc = false;
}
else if (estate->datums[i]->dtype == PLPGSQL_DTYPE_VAR)
{
PLpgSQL_var *var = (PLpgSQL_var *) estate->datums[i];
saved_vars[i].value = var->value;
saved_vars[i].isnull = var->isnull;
saved_vars[i].freeval = var->freeval;
var->freeval = false;
}
}
estate->err_text = NULL;
/*
* Raised exception should be trapped in outer functtion. Protection
* against outer trap is QUERY_CANCELED exception.
*/
oldcontext = CurrentMemoryContext;
oldowner = CurrentResourceOwner;
PG_TRY();
{
lint_stmt(estate, func, (PLpgSQL_stmt *) func->action);
}
PG_CATCH();
{
ErrorData *edata;
/* Save error info */
MemoryContextSwitchTo(oldcontext);
edata = CopyErrorData();
FlushErrorState();
CurrentResourceOwner = oldowner;
edata->sqlerrcode = ERRCODE_QUERY_CANCELED;
ReThrowError(edata);
}
PG_END_TRY();
estate->err_text = err_text;
estate->err_stmt = NULL;
/* return back a original rec variables */
for (i = 0; i < estate->ndatums; i++)
{
if (estate->datums[i]->dtype == PLPGSQL_DTYPE_REC)
{
PLpgSQL_rec *rec = (PLpgSQL_rec *) estate->datums[i];
if (rec->freetupdesc)
FreeTupleDesc(rec->tupdesc);
rec->tup = saved_records[i].tup;
rec->tupdesc = saved_records[i].tupdesc;
rec->freetup = saved_records[i].freetup;
rec->freetupdesc = saved_records[i].freetupdesc;
}
else if (estate->datums[i]->dtype == PLPGSQL_DTYPE_VAR)
{
PLpgSQL_var *var = (PLpgSQL_var *) estate->datums[i];
var->value = saved_vars[i].value;
var->isnull = saved_vars[i].isnull;
var->freeval = saved_vars[i].freeval;
}
}
pfree(saved_records);
pfree(saved_vars);
}
}
/*
* Verify lvalue - actually this not compare lvalue against rvalue - that should
* be next improvent, other improvent should be checking a result type of subscripts
* expressions.
*/
static void
check_target(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt, int varno, TupleDesc tupdesc)
{
PLpgSQL_datum *target = estate->datums[varno];
switch (target->dtype)
{
case PLPGSQL_DTYPE_VAR:
break;
case PLPGSQL_DTYPE_REC:
break;
case PLPGSQL_DTYPE_ROW:
{
check_row_or_rec(estate, stmt, (PLpgSQL_row *) target, NULL, tupdesc);
}
break;
case PLPGSQL_DTYPE_RECFIELD:
{
PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) target;
PLpgSQL_rec *rec;
int fno;
rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
/*
* Check that there is already a tuple in the record. We need
* that because records don't have any predefined field
* structure.
*/
if (!HeapTupleIsValid(rec->tup))
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("record \"%s\" is not assigned yet",
rec->refname),
errdetail("The tuple structure of a not-yet-assigned record is indeterminate.")));
/*
* Get the number of the records field to change and the
* number of attributes in the tuple. Note: disallow system
* column names because the code below won't cope.
*/
fno = SPI_fnumber(rec->tupdesc, recfield->fieldname);
if (fno <= 0)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_COLUMN),
errmsg("record \"%s\" has no field \"%s\"",
rec->refname, recfield->fieldname)));
}
break;
case PLPGSQL_DTYPE_ARRAYELEM:
{
/*
* Target is an element of an array
*/
int nsubscripts;
Oid arrayelemtypeid;
/*
* To handle constructs like x[1][2] := something, we have to
* be prepared to deal with a chain of arrayelem datums. Chase
* back to find the base array datum, and save the subscript
* expressions as we go. (We are scanning right to left here,
* but want to evaluate the subscripts left-to-right to
* minimize surprises.)
*/
nsubscripts = 0;
do
{
PLpgSQL_arrayelem *arrayelem = (PLpgSQL_arrayelem *) target;
if (nsubscripts++ >= MAXDIM)
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
nsubscripts + 1, MAXDIM)));
simple_check_expr(estate, stmt, arrayelem->subscript);
target = estate->datums[arrayelem->arrayparentno];
} while (target->dtype == PLPGSQL_DTYPE_ARRAYELEM);
arrayelemtypeid = get_element_type(exec_get_datum_type(estate, target));
if (!OidIsValid(arrayelemtypeid))
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("subscripted object is not an array")));
}
break;
}
return;
}
/*
* Check composed lvalue
*/
static void
check_row_or_rec(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt,
PLpgSQL_row *row, PLpgSQL_rec *rec,
TupleDesc tupdesc)
{
int fnum;
/* there are nothing to check on rec now */
if (row != NULL)
{
for (fnum = 0; fnum < row->nfields; fnum++)
{
if (row->varnos[fnum] < 0)
continue;
check_target(estate, stmt, row->varnos[fnum], tupdesc);
}
}
}
/*
* Prepare expression's plan. When there are plan generated by plpgsql interpret
* then set flag fresh_plan to false and returns.
*/
static void
prepare_expr(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt, PLpgSQL_expr *expr, bool *fresh_plan)
{
int cursorOptions = 0;
Assert(stmt != NULL && expr != NULL);
if (expr->plan != NULL)
{
*fresh_plan = false;
return;
}
switch (stmt->cmd_type)
{
case PLPGSQL_STMT_OPEN:
{
PLpgSQL_stmt_open *stmt_open = (PLpgSQL_stmt_open *) stmt;
PLpgSQL_var *curvar = (PLpgSQL_var *) estate->datums[stmt_open->curvar];
cursorOptions = curvar->cursor_options;
}
break;
case PLPGSQL_STMT_FORC:
{
PLpgSQL_stmt_forc *stmt_forc = (PLpgSQL_stmt_forc *) stmt;
PLpgSQL_var *curvar = (PLpgSQL_var *) estate->datums[stmt_forc->curvar];
/*
* change a cursorOption only when this call is related to
* curvar->cursor_explicit_expr
*/
if (curvar->cursor_explicit_expr == expr)
cursorOptions = curvar->cursor_options;
}
break;
}
exec_prepare_plan(estate, expr, cursorOptions);
*fresh_plan = true;
}
/*
* Prepare tuple descriptor based on expression result. This process has a some overhed, so it
* returns NULL, when descriptor is not necessary.
*/
static TupleDesc
prepare_tupdesc(PLpgSQL_execstate *estate, PLpgSQL_expr *expr,
int target_varno,
bool fresh_plan,
bool use_element_type,
bool expand_record,
bool is_expression)
{
PLpgSQL_datum *target = estate->datums[target_varno];
TupleDesc tupdesc = NULL;
/*
* do tupdesc when:
* target_varno is DTYPE_REC
* is fresh_plan and is expression
*/
if ((fresh_plan && is_expression) || target->dtype == PLPGSQL_DTYPE_REC)
{
tupdesc = query_get_desc(estate, expr, use_element_type, expand_record, is_expression);
}
return tupdesc;
}
/*
* Prepare tuple desc when target is record or plan is fresh
*/
static TupleDesc
prepare_tupdesc_row_or_rec(PLpgSQL_execstate *estate, PLpgSQL_expr *expr,
PLpgSQL_row *row,
PLpgSQL_rec *rec,
bool fresh_plan)
{
TupleDesc tupdesc = NULL;
/*
* do tupdesc when:
* target_varno is DTYPE_REC
* is fresh_plan and is expression
*/
if (fresh_plan || rec != NULL)
{
tupdesc = query_get_desc(estate, expr, false, false, false);
}
return tupdesc;
}
/*
* same as above, but not used for assign tupledesc to record var
*/
static TupleDesc
prepare_tupdesc_novar(PLpgSQL_execstate *estate, PLpgSQL_expr *expr,
bool fresh_plan,
bool is_expression)
{
TupleDesc tupdesc = NULL;
if (fresh_plan && is_expression)
{
tupdesc = query_get_desc(estate, expr, false, false, is_expression);
if (is_expression)
if (tupdesc->natts != 1)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg_plural("query \"%s\" returned %d column",
"query \"%s\" returned %d columns",
tupdesc->natts,
expr->query,
tupdesc->natts)));
}
return tupdesc;
}
/*
* Check of simple expression. Checked expression is not used as source for any target
*/
static void
simple_check_expr(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt, PLpgSQL_expr *expr)
{
bool fresh_plan;
TupleDesc tupdesc;
if (expr != NULL)
{
prepare_expr(estate, stmt, expr, &fresh_plan);
tupdesc = prepare_tupdesc_novar(estate, expr, fresh_plan, true);
cleanup(fresh_plan, expr, tupdesc);
}
}
#if PG_VERSION_NUM < 90000
/*
* Similar function exec_get_datum_type is in 9nth line
*/
static Oid
exec_get_datum_type(PLpgSQL_execstate *estate,
PLpgSQL_datum *datum)
{
Oid typoid = InvalidOid;
switch (datum->dtype)
{
case PLPGSQL_DTYPE_VAR:
typoid = ((PLpgSQL_var *) datum)->datatype->typoid;
break;
case PLPGSQL_DTYPE_ROW:
typoid = ((PLpgSQL_row *) datum)->rowtupdesc->tdtypeid;
break;
case PLPGSQL_DTYPE_REC:
{
PLpgSQL_rec *rec = (PLpgSQL_rec *) datum;
if (!HeapTupleIsValid(rec->tup))
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("record \"%s\" is not assigned yet",
rec->refname),
errdetail("The tuple structure of a not-yet-assigned record is indeterminate.")));
Assert(rec->tupdesc != NULL);
/* Make sure we have a valid type/typmod setting */
BlessTupleDesc(rec->tupdesc);
typoid = rec->tupdesc->tdtypeid;
}
break;
case PLPGSQL_DTYPE_RECFIELD:
{
PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) datum;
PLpgSQL_rec *rec;
int fno;
rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
if (!HeapTupleIsValid(rec->tup))
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("record \"%s\" is not assigned yet",
rec->refname),
errdetail("The tuple structure of a not-yet-assigned record is indeterminate.")));
fno = SPI_fnumber(rec->tupdesc, recfield->fieldname);
if (fno == SPI_ERROR_NOATTRIBUTE)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_COLUMN),
errmsg("record \"%s\" has no field \"%s\"",
rec->refname, recfield->fieldname)));
typoid = SPI_gettypeid(rec->tupdesc, fno);
}
break;
case PLPGSQL_DTYPE_TRIGARG:
typoid = TEXTOID;
break;
}
return typoid;
}
#endif
/* ----------
* Generate a prepared plan - this is copy from pl_exec.c
* ----------
*/
static void
exec_prepare_plan(PLpgSQL_execstate *estate,
PLpgSQL_expr *expr, int cursorOptions)
{
#if PG_VERSION_NUM >= 90000
SPIPlanPtr plan;
/*
* The grammar can't conveniently set expr->func while building the parse
* tree, so make sure it's set before parser hooks need it.
*/
expr->func = estate->func;
/*
* Generate and save the plan
*/
plan = SPI_prepare_params(expr->query,
(ParserSetupHook) plpgsql_parser_setup,
(void *) expr,
cursorOptions);
if (plan == NULL)
{
/* Some SPI errors deserve specific error messages */
switch (SPI_result)
{
case SPI_ERROR_COPY:
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot COPY to/from client in PL/pgSQL")));
case SPI_ERROR_TRANSACTION:
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot begin/end transactions in PL/pgSQL"),
errhint("Use a BEGIN block with an EXCEPTION clause instead.")));
default:
elog(ERROR, "SPI_prepare_params failed for \"%s\": %s",
expr->query, SPI_result_code_string(SPI_result));
}
}
expr->plan = SPI_saveplan(plan);
SPI_freeplan(plan);
#else
int i;
SPIPlanPtr plan;
Oid *argtypes;
/*
* We need a temporary argtypes array to load with data. (The finished
* plan structure will contain a copy of it.)
*/
argtypes = (Oid *) palloc(expr->nparams * sizeof(Oid));
for (i = 0; i < expr->nparams; i++)
{
argtypes[i] = exec_get_datum_type(estate, estate->datums[expr->params[i]]);
}
/*
* Generate and save the plan
*/
plan = SPI_prepare_cursor(expr->query, expr->nparams, argtypes,
cursorOptions);
if (plan == NULL)
{
/* Some SPI errors deserve specific error messages */
switch (SPI_result)
{
case SPI_ERROR_COPY:
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot COPY to/from client in PL/pgSQL")));
case SPI_ERROR_TRANSACTION:
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot begin/end transactions in PL/pgSQL"),
errhint("Use a BEGIN block with an EXCEPTION clause instead.")));
default:
elog(ERROR, "SPI_prepare_cursor failed for \"%s\": %s",
expr->query, SPI_result_code_string(SPI_result));
}
}
expr->plan = SPI_saveplan(plan);
SPI_freeplan(plan);
pfree(argtypes);
#endif
}
/*
* call a lint_stmt for any statement in list
*
*/
static void
lint_stmts(PLpgSQL_execstate *estate,
PLpgSQL_function *func,
List *stmts)
{
ListCell *lc;
foreach(lc, stmts)
{
PLpgSQL_stmt *stmt = (PLpgSQL_stmt *) lfirst(lc);
lint_stmt(estate, func, stmt);
}
}
/*
* walk over all statements tree
*
*/
static void
lint_stmt(PLpgSQL_execstate *estate,
PLpgSQL_function *func,
PLpgSQL_stmt *stmt)
{
bool fresh_plan;
TupleDesc tupdesc = NULL;
ListCell *l;
if (stmt == NULL)
return;
estate->err_stmt = stmt;
switch ((enum PLpgSQL_stmt_types) stmt->cmd_type)
{
case PLPGSQL_STMT_BLOCK:
{
PLpgSQL_stmt_block *stmt_block = (PLpgSQL_stmt_block *) stmt;
int i;
PLpgSQL_datum *d;
for (i = 0; i < stmt_block->n_initvars; i++)
{
d = func->datums[stmt_block->initvarnos[i]];
if (d->dtype == PLPGSQL_DTYPE_VAR)
{
PLpgSQL_var *var = (PLpgSQL_var *) d;
simple_check_expr(estate, stmt, var->default_val);
}
}
lint_stmts(estate, func, stmt_block->body);
if (stmt_block->exceptions)
{
foreach(l, stmt_block->exceptions->exc_list)
{
lint_stmts(estate, func, ((PLpgSQL_exception *) lfirst(l))->action);
}
}
}
break;
case PLPGSQL_STMT_ASSIGN:
{
PLpgSQL_stmt_assign *stmt_assign = (PLpgSQL_stmt_assign *) stmt;
/* prepare plan if desn't exist yet */
prepare_expr(estate, stmt, stmt_assign->expr, &fresh_plan);
/*
* Tuple descriptor is necessary when we would to check expression
* first time or when target is record.
*/
tupdesc = prepare_tupdesc(estate, stmt_assign->expr, stmt_assign->varno,
fresh_plan,
false, /* no element type */
true, /* expand record */
true); /* is expression */
/* check target, ensure target can get a result */
check_target(estate, stmt, stmt_assign->varno, tupdesc);
/* assign a tupdesc to record variable */
assign_tupdesc_dno(estate, stmt_assign->varno, tupdesc);
/* cleanup fresh plan and tuple descriptor */
cleanup(fresh_plan, stmt_assign->expr, tupdesc);
}
break;
case PLPGSQL_STMT_IF:
{
PLpgSQL_stmt_if *stmt_if = (PLpgSQL_stmt_if *) stmt;
#if PG_VERSION_NUM >= 90200
ListCell *l;
#endif
simple_check_expr(estate, stmt, stmt_if->cond);
#if PG_VERSION_NUM >= 90200
lint_stmts(estate, func, stmt_if->then_body);
foreach(l, stmt_if->elsif_list)
{
PLpgSQL_if_elsif *elif = (PLpgSQL_if_elsif *) lfirst(l);
simple_check_expr(estate, stmt, elif->cond);
lint_stmts(estate, func, elif->stmts);
}
lint_stmts(estate, func, stmt_if->else_body);
#else
lint_stmts(estate, func, stmt_if->true_body);
lint_stmts(estate, func, stmt_if->false_body);
#endif
}
break;
case PLPGSQL_STMT_CASE:
{
PLpgSQL_stmt_case *stmt_case = (PLpgSQL_stmt_case *) stmt;
Oid result_oid;
if (stmt_case->t_expr != NULL)
{
PLpgSQL_var *t_var = (PLpgSQL_var *) estate->datums[stmt_case->t_varno];
prepare_expr(estate, stmt, stmt_case->t_expr, &fresh_plan);
/* we need tupdesc everytime */
tupdesc = prepare_tupdesc_novar(estate, stmt_case->t_expr, true, true);
result_oid = tupdesc->attrs[0]->atttypid;
/*
* When expected datatype is different from real, change it. Note that
* what we're modifying here is an execution copy of the datum, so
* this doesn't affect the originally stored function parse tree.
*/
if (t_var->datatype->typoid != result_oid)
#if PG_VERSION_NUM >= 90100
t_var->datatype = plpgsql_build_datatype(result_oid,
-1,
estate->func->fn_input_collation);
#else
t_var->datatype = plpgsql_build_datatype(result_oid, -1);
#endif
cleanup(fresh_plan, stmt_case->t_expr,tupdesc);
}
foreach(l, stmt_case->case_when_list)
{
PLpgSQL_case_when *cwt = (PLpgSQL_case_when *) lfirst(l);
simple_check_expr(estate, stmt, cwt->expr);
lint_stmts(estate, func, cwt->stmts);
}
lint_stmts(estate, func, stmt_case->else_stmts);
}
break;
case PLPGSQL_STMT_LOOP:
lint_stmts(estate, func, ((PLpgSQL_stmt_loop *) stmt)->body);
break;
case PLPGSQL_STMT_WHILE:
{
PLpgSQL_stmt_while *stmt_while = (PLpgSQL_stmt_while *) stmt;
simple_check_expr(estate, stmt, stmt_while->cond);
lint_stmts(estate, func, stmt_while->body);
}
break;
case PLPGSQL_STMT_FORI:
{
PLpgSQL_stmt_fori *stmt_fori = (PLpgSQL_stmt_fori *) stmt;
simple_check_expr(estate, stmt, stmt_fori->lower);
simple_check_expr(estate, stmt, stmt_fori->upper);
simple_check_expr(estate, stmt, stmt_fori->step);
lint_stmts(estate, func, stmt_fori->body);
}
break;
case PLPGSQL_STMT_FORS:
{
PLpgSQL_stmt_fors *stmt_fors = (PLpgSQL_stmt_fors *) stmt;
prepare_expr(estate, stmt, stmt_fors->query, &fresh_plan);
tupdesc = prepare_tupdesc_row_or_rec(estate,
stmt_fors->query,
stmt_fors->row,
stmt_fors->rec,
fresh_plan);
check_row_or_rec(estate, stmt,
stmt_fors->row, stmt_fors->rec,
tupdesc);
assign_tupdesc_row_or_rec(estate, stmt_fors->row, stmt_fors->rec, tupdesc);
lint_stmts(estate, func, stmt_fors->body);
cleanup(fresh_plan, stmt_fors->query, tupdesc);
}
break;
case PLPGSQL_STMT_FORC:
{
PLpgSQL_stmt_forc *stmt_forc = (PLpgSQL_stmt_forc *) stmt;
PLpgSQL_var *var = (PLpgSQL_var *) func->datums[stmt_forc->curvar];
simple_check_expr(estate, stmt, stmt_forc->argquery);
if (var->cursor_explicit_expr != NULL)
{
prepare_expr(estate, stmt, var->cursor_explicit_expr, &fresh_plan);
tupdesc = prepare_tupdesc_row_or_rec(estate,
var->cursor_explicit_expr,
stmt_forc->row,
stmt_forc->rec,
fresh_plan);
check_row_or_rec(estate, stmt,
stmt_forc->row, stmt_forc->rec,
tupdesc);
assign_tupdesc_row_or_rec(estate, stmt_forc->row, stmt_forc->rec, tupdesc);
}
lint_stmts(estate, func, stmt_forc->body);
cleanup(fresh_plan, var->cursor_explicit_expr, tupdesc);
}
break;
case PLPGSQL_STMT_DYNFORS:
{
PLpgSQL_stmt_dynfors * stmt_dynfors = (PLpgSQL_stmt_dynfors *) stmt;
if (stmt_dynfors->rec != NULL)
elog(ERROR, "cannot determinate a result of dynamic SQL");
simple_check_expr(estate, stmt, stmt_dynfors->query);
foreach(l, stmt_dynfors->params)
{
simple_check_expr(estate, stmt, (PLpgSQL_expr *) lfirst(l));
}
lint_stmts(estate, func, stmt_dynfors->body);
}
break;
#if PG_VERSION_NUM >= 90100
case PLPGSQL_STMT_FOREACH_A:
{
PLpgSQL_stmt_foreach_a *stmt_foreach_a = (PLpgSQL_stmt_foreach_a *) stmt;
prepare_expr(estate, stmt, stmt_foreach_a->expr, &fresh_plan);
tupdesc = prepare_tupdesc(estate, stmt_foreach_a->expr, stmt_foreach_a->varno,
fresh_plan,
true, /* element type */
false, /* expand record */
true); /* is expression */
check_target(estate, stmt, stmt_foreach_a->varno, tupdesc);
assign_tupdesc_dno(estate, stmt_foreach_a->varno, tupdesc);
cleanup(fresh_plan, stmt_foreach_a->expr, tupdesc);
lint_stmts(estate, func, stmt_foreach_a->body);
}
break;
#endif
case PLPGSQL_STMT_EXIT:
simple_check_expr(estate, stmt, ((PLpgSQL_stmt_exit *) stmt)->cond);
break;
case PLPGSQL_STMT_PERFORM:
{
PLpgSQL_stmt_perform *stmt_perform = (PLpgSQL_stmt_perform *) stmt;
prepare_expr(estate, stmt, stmt_perform->expr, &fresh_plan);
cleanup(fresh_plan, stmt_perform->expr, NULL);
}
break;
simple_check_expr(estate, stmt, ((PLpgSQL_stmt_perform *) stmt)->expr);
break;
case PLPGSQL_STMT_RETURN:
simple_check_expr(estate, stmt, ((PLpgSQL_stmt_return *) stmt)->expr);
break;
case PLPGSQL_STMT_RETURN_NEXT:
simple_check_expr(estate, stmt, ((PLpgSQL_stmt_return_next *) stmt)->expr);
break;
case PLPGSQL_STMT_RETURN_QUERY:
{
PLpgSQL_stmt_return_query *stmt_rq = (PLpgSQL_stmt_return_query *) stmt;
simple_check_expr(estate, stmt, stmt_rq->query);
simple_check_expr(estate, stmt, stmt_rq->dynquery);
foreach(l, stmt_rq->params)
{
simple_check_expr(estate, stmt, (PLpgSQL_expr *) lfirst(l));