forked from nextgres/uplpgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupl_exec.c
More file actions
9876 lines (8672 loc) · 288 KB
/
Copy pathupl_exec.c
File metadata and controls
9876 lines (8672 loc) · 288 KB
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
/*-------------------------------------------------------------------------
*
* pl_exec.c - Executor for the PL/pgSQL
* procedural language
*
* Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
* Portions Copyright (c) 2003-2014, Jonah H. Harris <[email protected]>
* Portions Copyright (c) 2014-2026, NEXTGRES, LLC. <[email protected]>
*
* Derived from PostgreSQL src/pl/plpgsql/src/pl_exec.c; modifications are
* licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain a
* copy of the License in LICENSE or at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0 AND PostgreSQL
*
*
* IDENTIFICATION
* src/pl/plpgsql/src/pl_exec.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include <ctype.h>
#include "access/detoast.h"
#include "access/htup_details.h"
#include "access/tupconvert.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "executor/execExpr.h"
#include "executor/spi.h"
#include "executor/tstoreReceiver.h"
#include "funcapi.h"
#include "mb/stringinfo_mb.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "nodes/supportnodes.h"
#include "optimizer/optimizer.h"
#include "parser/parse_coerce.h"
#include "parser/parse_type.h"
#include "upl_common.h"
#include "storage/proc.h"
#include "tcop/cmdtag.h"
#include "tcop/pquery.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/snapmgr.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
/*
* All plpgsql function executions within a single transaction share the same
* executor EState for evaluating "simple" expressions. Each function call
* creates its own "eval_econtext" ExprContext within this estate for
* per-evaluation workspace. eval_econtext is freed at normal function exit,
* and the EState is freed at transaction end (in case of error, we assume
* that the abort mechanisms clean it all up). Furthermore, any exception
* block within a function has to have its own eval_econtext separate from
* the containing function's, so that we can clean up ExprContext callbacks
* properly at subtransaction exit. We maintain a stack that tracks the
* individual econtexts so that we can clean up correctly at subxact exit.
*
* This arrangement is a bit tedious to maintain, but it's worth the trouble
* so that we don't have to re-prepare simple expressions on each trip through
* a function. (We assume the case to optimize is many repetitions of a
* function within a transaction.)
*
* However, there's no value in trying to amortize simple expression setup
* across multiple executions of a DO block (inline code block), since there
* can never be any. If we use the shared EState for a DO block, the expr
* state trees are effectively leaked till end of transaction, and that can
* add up if the user keeps on submitting DO blocks. Therefore, each DO block
* has its own simple-expression EState, which is cleaned up at exit from
* uplpgsql_inline_handler(). DO blocks still use the simple_econtext_stack,
* though, so that subxact abort cleanup does the right thing.
*
* (However, if a DO block executes COMMIT or ROLLBACK, then exec_stmt_commit
* or exec_stmt_rollback will unlink it from the DO's simple-expression EState
* and create a new shared EState that will be used thenceforth. The original
* EState will be cleaned up when we get back to uplpgsql_inline_handler. This
* is a bit ugly, but it isn't worth doing better, since scenarios like this
* can't result in indefinite accumulation of state trees.)
*/
typedef struct SimpleEcontextStackEntry
{
ExprContext *stack_econtext; /* a stacked econtext */
SubTransactionId xact_subxid; /* ID for current subxact */
struct SimpleEcontextStackEntry *next; /* next stack entry up */
} SimpleEcontextStackEntry;
static EState *shared_simple_eval_estate = NULL;
static SimpleEcontextStackEntry *simple_econtext_stack = NULL;
/*
* In addition to the shared simple-eval EState, we have a shared resource
* owner that holds refcounts on the CachedPlans for any "simple" expressions
* we have evaluated in the current transaction. This allows us to avoid
* continually grabbing and releasing a plan refcount when a simple expression
* is used over and over. (DO blocks use their own resowner, in exactly the
* same way described above for shared_simple_eval_estate.)
*/
static ResourceOwner shared_simple_eval_resowner = NULL;
/*
* Memory management within a plpgsql function generally works with three
* contexts:
*
* 1. Function-call-lifespan data, such as variable values, is kept in the
* "main" context, a/k/a the "SPI Proc" context established by SPI_connect().
* This is usually the CurrentMemoryContext while running code in this module
* (which is not good, because careless coding can easily cause
* function-lifespan memory leaks, but we live with it for now).
*
* 2. Some statement-execution routines need statement-lifespan workspace.
* A suitable context is created on-demand by get_stmt_mcontext(), and must
* be reset at the end of the requesting routine. Error recovery will clean
* it up automatically. Nested statements requiring statement-lifespan
* workspace will result in a stack of such contexts, see push_stmt_mcontext().
*
* 3. We use the eval_econtext's per-tuple memory context for expression
* evaluation, and as a general-purpose workspace for short-lived allocations.
* Such allocations usually aren't explicitly freed, but are left to be
* cleaned up by a context reset, typically done by exec_eval_cleanup().
*
* These macros are for use in making short-lived allocations:
*/
#define get_eval_mcontext(estate) \
((estate)->eval_econtext->ecxt_per_tuple_memory)
#define eval_mcontext_alloc(estate, sz) \
MemoryContextAlloc(get_eval_mcontext(estate), sz)
#define eval_mcontext_alloc0(estate, sz) \
MemoryContextAllocZero(get_eval_mcontext(estate), sz)
/*
* We use two session-wide hash tables for caching cast information.
*
* cast_expr_hash entries (of type uplpgsql_CastExprHashEntry) hold compiled
* expression trees for casts. These survive for the life of the session and
* are shared across all PL/pgSQL functions and DO blocks. At some point it
* might be worth invalidating them after pg_cast changes, but for the moment
* we don't bother.
*
* There is a separate hash table shared_cast_hash (with entries of type
* uplpgsql_CastHashEntry) containing evaluation state trees for these
* expressions, which are managed in the same way as simple expressions
* (i.e., we assume cast expressions are always simple).
*
* As with simple expressions, DO blocks don't use the shared_cast_hash table
* but must have their own evaluation state trees. This isn't ideal, but we
* don't want to deal with multiple simple_eval_estates within a DO block.
*/
typedef struct /* lookup key for cast info */
{
/* NB: we assume this struct contains no padding bytes */
Oid srctype; /* source type for cast */
Oid dsttype; /* destination type for cast */
int32 srctypmod; /* source typmod for cast */
int32 dsttypmod; /* destination typmod for cast */
} uplpgsql_CastHashKey;
typedef struct /* cast_expr_hash table entry */
{
uplpgsql_CastHashKey key; /* hash key --- MUST BE FIRST */
Expr *cast_expr; /* cast expression, or NULL if no-op cast */
CachedExpression *cast_cexpr; /* cached expression backing the above */
} uplpgsql_CastExprHashEntry;
typedef struct /* cast_hash table entry */
{
uplpgsql_CastHashKey key; /* hash key --- MUST BE FIRST */
uplpgsql_CastExprHashEntry *cast_centry; /* link to matching expr entry */
/* ExprState is valid only when cast_lxid matches current LXID */
ExprState *cast_exprstate; /* expression's eval tree */
bool cast_in_use; /* true while we're executing eval tree */
LocalTransactionId cast_lxid;
} uplpgsql_CastHashEntry;
static HTAB *cast_expr_hash = NULL;
static HTAB *shared_cast_hash = NULL;
/*
* LOOP_RC_PROCESSING encapsulates common logic for looping statements to
* handle return/exit/continue result codes from the loop body statement(s).
* It's meant to be used like this:
*
* int rc = UPLPGSQL_RC_OK;
* for (...)
* {
* ...
* rc = exec_stmts(estate, stmt->body);
* LOOP_RC_PROCESSING(stmt->label, break);
* ...
* }
* return rc;
*
* If execution of the loop should terminate, LOOP_RC_PROCESSING will execute
* "exit_action" (typically a "break" or "goto"), after updating "rc" to the
* value the current statement should return. If execution should continue,
* LOOP_RC_PROCESSING will do nothing except reset "rc" to UPLPGSQL_RC_OK.
*
* estate and rc are implicit arguments to the macro.
* estate->exitlabel is examined and possibly updated.
*/
#define LOOP_RC_PROCESSING(looplabel, exit_action) \
if (rc == UPLPGSQL_RC_RETURN) \
{ \
/* RETURN, so propagate RC_RETURN out */ \
exit_action; \
} \
else if (rc == UPLPGSQL_RC_EXIT) \
{ \
if (estate->exitlabel == NULL) \
{ \
/* unlabeled EXIT terminates this loop */ \
rc = UPLPGSQL_RC_OK; \
exit_action; \
} \
else if ((looplabel) != NULL && \
strcmp(looplabel, estate->exitlabel) == 0) \
{ \
/* labeled EXIT matching this loop, so terminate loop */ \
estate->exitlabel = NULL; \
rc = UPLPGSQL_RC_OK; \
exit_action; \
} \
else \
{ \
/* non-matching labeled EXIT, propagate RC_EXIT out */ \
exit_action; \
} \
} \
else if (rc == UPLPGSQL_RC_CONTINUE) \
{ \
if (estate->exitlabel == NULL) \
{ \
/* unlabeled CONTINUE matches this loop, so continue in loop */ \
rc = UPLPGSQL_RC_OK; \
} \
else if ((looplabel) != NULL && \
strcmp(looplabel, estate->exitlabel) == 0) \
{ \
/* labeled CONTINUE matching this loop, so continue in loop */ \
estate->exitlabel = NULL; \
rc = UPLPGSQL_RC_OK; \
} \
else \
{ \
/* non-matching labeled CONTINUE, propagate RC_CONTINUE out */ \
exit_action; \
} \
} \
else \
Assert(rc == UPLPGSQL_RC_OK)
/* State struct for count_param_references */
typedef struct count_param_references_context
{
int paramid;
int count;
Param *last_param;
} count_param_references_context;
/************************************************************
* Local function forward declarations
************************************************************/
static void coerce_function_result_tuple(UPLpgSQL_execstate *estate,
TupleDesc tupdesc);
static void uplpgsql_exec_error_callback(void *arg);
static void copy_uplpgsql_datums(UPLpgSQL_execstate *estate,
UPLpgSQL_function *func);
static void uplpgsql_fulfill_promise(UPLpgSQL_execstate *estate,
UPLpgSQL_var *var);
MemoryContext get_stmt_mcontext(UPLpgSQL_execstate *estate);
static void push_stmt_mcontext(UPLpgSQL_execstate *estate);
void pop_stmt_mcontext(UPLpgSQL_execstate *estate);
static int exec_toplevel_block(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_block *block);
int exec_stmt_block(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_block *block);
static int exec_stmts(UPLpgSQL_execstate *estate,
List *stmts);
static int exec_stmt_assign(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_assign *stmt);
int exec_stmt_perform(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_perform *stmt);
int exec_stmt_call(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_call *stmt);
int exec_stmt_getdiag(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_getdiag *stmt);
static int exec_stmt_if(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_if *stmt);
static int exec_stmt_case(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_case *stmt);
static int exec_stmt_loop(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_loop *stmt);
static int exec_stmt_while(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_while *stmt);
int exec_stmt_fori(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_fori *stmt);
static int exec_stmt_fors(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_fors *stmt);
static int exec_stmt_forc(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_forc *stmt);
int exec_stmt_foreach_a(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_foreach_a *stmt);
int exec_stmt_open(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_open *stmt);
int exec_stmt_fetch(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_fetch *stmt);
int exec_stmt_close(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_close *stmt);
static int exec_stmt_exit(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_exit *stmt);
int exec_stmt_return(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_return *stmt);
int exec_stmt_return_next(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_return_next *stmt);
int exec_stmt_return_query(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_return_query *stmt);
int exec_stmt_raise(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_raise *stmt);
int exec_stmt_assert(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_assert *stmt);
int exec_stmt_execsql(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_execsql *stmt);
int exec_stmt_dynexecute(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_dynexecute *stmt);
int exec_stmt_dynfors(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_dynfors *stmt);
int exec_stmt_commit(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_commit *stmt);
int exec_stmt_rollback(UPLpgSQL_execstate *estate,
UPLpgSQL_stmt_rollback *stmt);
void uplpgsql_estate_setup(UPLpgSQL_execstate *estate,
UPLpgSQL_function *func,
ReturnSetInfo *rsi,
EState *simple_eval_estate,
ResourceOwner simple_eval_resowner);
void exec_eval_cleanup(UPLpgSQL_execstate *estate);
static void exec_prepare_plan(UPLpgSQL_execstate *estate,
UPLpgSQL_expr *expr, int cursorOptions);
static void exec_simple_check_plan(UPLpgSQL_execstate *estate, UPLpgSQL_expr *expr);
static bool exec_is_simple_query(UPLpgSQL_expr *expr);
static void exec_save_simple_expr(UPLpgSQL_expr *expr, CachedPlan *cplan);
static void exec_check_rw_parameter(UPLpgSQL_expr *expr, int paramid);
static bool count_param_references(Node *node,
count_param_references_context *context);
static void exec_check_assignable(UPLpgSQL_execstate *estate, int dno);
static bool exec_eval_simple_expr(UPLpgSQL_execstate *estate,
UPLpgSQL_expr *expr,
Datum *result,
bool *isNull,
Oid *rettype,
int32 *rettypmod);
void exec_assign_expr(UPLpgSQL_execstate *estate,
UPLpgSQL_datum *target,
UPLpgSQL_expr *expr);
static void exec_assign_c_string(UPLpgSQL_execstate *estate,
UPLpgSQL_datum *target,
const char *str);
void exec_assign_value(UPLpgSQL_execstate *estate,
UPLpgSQL_datum *target,
Datum value, bool isNull,
Oid valtype, int32 valtypmod);
void exec_eval_datum(UPLpgSQL_execstate *estate,
UPLpgSQL_datum *datum,
Oid *typeid,
int32 *typetypmod,
Datum *value,
bool *isnull);
int exec_eval_integer(UPLpgSQL_execstate *estate,
UPLpgSQL_expr *expr,
bool *isNull);
bool exec_eval_boolean(UPLpgSQL_execstate *estate,
UPLpgSQL_expr *expr,
bool *isNull);
Datum exec_eval_expr(UPLpgSQL_execstate *estate,
UPLpgSQL_expr *expr,
bool *isNull,
Oid *rettype,
int32 *rettypmod);
int exec_run_select(UPLpgSQL_execstate *estate,
UPLpgSQL_expr *expr, long maxtuples, Portal *portalP);
static int exec_for_query(UPLpgSQL_execstate *estate, UPLpgSQL_stmt_forq *stmt,
Portal portal, bool prefetch_ok);
static ParamListInfo setup_param_list(UPLpgSQL_execstate *estate,
UPLpgSQL_expr *expr);
static ParamExternData *uplpgsql_param_fetch(ParamListInfo params,
int paramid, bool speculative,
ParamExternData *prm);
static void uplpgsql_param_compile(ParamListInfo params, Param *param,
ExprState *state,
Datum *resv, bool *resnull);
static void uplpgsql_param_eval_var_check(ExprState *state, ExprEvalStep *op,
ExprContext *econtext);
static void uplpgsql_param_eval_var_transfer(ExprState *state, ExprEvalStep *op,
ExprContext *econtext);
static void uplpgsql_param_eval_var(ExprState *state, ExprEvalStep *op,
ExprContext *econtext);
static void uplpgsql_param_eval_var_ro(ExprState *state, ExprEvalStep *op,
ExprContext *econtext);
static void uplpgsql_param_eval_recfield(ExprState *state, ExprEvalStep *op,
ExprContext *econtext);
static void uplpgsql_param_eval_generic(ExprState *state, ExprEvalStep *op,
ExprContext *econtext);
static void uplpgsql_param_eval_generic_ro(ExprState *state, ExprEvalStep *op,
ExprContext *econtext);
void exec_move_row(UPLpgSQL_execstate *estate,
UPLpgSQL_variable *target,
HeapTuple tup, TupleDesc tupdesc);
static void revalidate_rectypeid(UPLpgSQL_rec *rec);
static ExpandedRecordHeader *make_expanded_record_for_rec(UPLpgSQL_execstate *estate,
UPLpgSQL_rec *rec,
TupleDesc srctupdesc,
ExpandedRecordHeader *srcerh);
static void exec_move_row_from_fields(UPLpgSQL_execstate *estate,
UPLpgSQL_variable *target,
ExpandedRecordHeader *newerh,
Datum *values, bool *nulls,
TupleDesc tupdesc);
static bool compatible_tupdescs(TupleDesc src_tupdesc, TupleDesc dst_tupdesc);
static HeapTuple make_tuple_from_row(UPLpgSQL_execstate *estate,
UPLpgSQL_row *row,
TupleDesc tupdesc);
static TupleDesc deconstruct_composite_datum(Datum value,
HeapTupleData *tmptup);
static void exec_move_row_from_datum(UPLpgSQL_execstate *estate,
UPLpgSQL_variable *target,
Datum value);
static void instantiate_empty_record_variable(UPLpgSQL_execstate *estate,
UPLpgSQL_rec *rec);
static char *convert_value_to_string(UPLpgSQL_execstate *estate,
Datum value, Oid valtype);
static inline Datum exec_cast_value(UPLpgSQL_execstate *estate,
Datum value, bool *isnull,
Oid valtype, int32 valtypmod,
Oid reqtype, int32 reqtypmod);
static Datum do_cast_value(UPLpgSQL_execstate *estate,
Datum value, bool *isnull,
Oid valtype, int32 valtypmod,
Oid reqtype, int32 reqtypmod);
static uplpgsql_CastHashEntry *get_cast_hashentry(UPLpgSQL_execstate *estate,
Oid srctype, int32 srctypmod,
Oid dsttype, int32 dsttypmod);
static void exec_init_tuple_store(UPLpgSQL_execstate *estate);
void exec_set_found(UPLpgSQL_execstate *estate, bool state);
void uplpgsql_create_econtext(UPLpgSQL_execstate *estate);
void uplpgsql_destroy_econtext(UPLpgSQL_execstate *estate);
void assign_simple_var(UPLpgSQL_execstate *estate, UPLpgSQL_var *var,
Datum newvalue, bool isnull, bool freeable);
void assign_text_var(UPLpgSQL_execstate *estate, UPLpgSQL_var *var,
const char *str);
static void assign_record_var(UPLpgSQL_execstate *estate, UPLpgSQL_rec *rec,
ExpandedRecordHeader *erh);
static ParamListInfo exec_eval_using_params(UPLpgSQL_execstate *estate,
List *params);
Portal exec_dynquery_with_params(UPLpgSQL_execstate *estate,
UPLpgSQL_expr *dynquery, List *params,
const char *portalname, int cursorOptions);
static char *format_expr_params(UPLpgSQL_execstate *estate,
const UPLpgSQL_expr *expr);
static char *format_preparedparamsdata(UPLpgSQL_execstate *estate,
ParamListInfo paramLI);
static UPLpgSQL_variable *make_callstmt_target(UPLpgSQL_execstate *estate,
UPLpgSQL_expr *expr);
/* ----------
* uplpgsql_exec_function Called by the call handler for
* function execution.
*
* This is also used to execute inline code blocks (DO blocks). The only
* difference that this code is aware of is that for a DO block, we want
* to use a private simple_eval_estate and a private simple_eval_resowner,
* which are created and passed in by the caller. For regular functions,
* pass NULL, which implies using shared_simple_eval_estate and
* shared_simple_eval_resowner. (When using a private simple_eval_estate,
* we must also use a private cast hashtable, but that's taken care of
* within uplpgsql_estate_setup.)
* procedure_resowner is a resowner that will survive for the duration
* of execution of this function/procedure. It is needed only if we
* are doing non-atomic execution and there are CALL or DO statements
* in the function; otherwise it can be NULL. We use it to hold refcounts
* on the CALL/DO statements' plans.
* ----------
*/
Datum
uplpgsql_exec_function(UPLpgSQL_function *func, FunctionCallInfo fcinfo,
EState *simple_eval_estate,
ResourceOwner simple_eval_resowner,
ResourceOwner procedure_resowner,
bool atomic)
{
UPLpgSQL_execstate estate;
ErrorContextCallback plerrcontext;
int i;
int rc;
/*
* Setup the execution state
*/
uplpgsql_estate_setup(&estate, func, (ReturnSetInfo *) fcinfo->resultinfo,
simple_eval_estate, simple_eval_resowner);
estate.procedure_resowner = procedure_resowner;
estate.atomic = atomic;
/*
* Setup error traceback support for ereport()
*/
plerrcontext.callback = uplpgsql_exec_error_callback;
plerrcontext.arg = &estate;
plerrcontext.previous = error_context_stack;
error_context_stack = &plerrcontext;
/*
* Make local execution copies of all the datums
*/
estate.err_text = gettext_noop("during initialization of execution state");
copy_uplpgsql_datums(&estate, func);
/*
* Store the actual call argument values into the appropriate variables
*/
estate.err_text = gettext_noop("while storing call arguments into local variables");
for (i = 0; i < func->fn_nargs; i++)
{
int n = func->fn_argvarnos[i];
switch (estate.datums[n]->dtype)
{
case UPLPGSQL_DTYPE_VAR:
{
UPLpgSQL_var *var = (UPLpgSQL_var *) estate.datums[n];
assign_simple_var(&estate, var,
fcinfo->args[i].value,
fcinfo->args[i].isnull,
false);
/*
* If it's a varlena type, check to see if we received a
* R/W expanded-object pointer. If so, we can commandeer
* the object rather than having to copy it. If passed a
* R/O expanded pointer, just keep it as the value of the
* variable for the moment. (We can change it to R/W if
* the variable gets modified, but that may very well
* never happen.)
*
* Also, force any flat array value to be stored in
* expanded form in our local variable, in hopes of
* improving efficiency of uses of the variable. (This is
* a hack, really: why only arrays? Need more thought
* about which cases are likely to win. See also
* typisarray-specific heuristic in exec_assign_value.)
*/
if (!var->isnull && var->datatype->typlen == -1)
{
if (VARATT_IS_EXTERNAL_EXPANDED_RW(DatumGetPointer(var->value)))
{
/* take ownership of R/W object */
assign_simple_var(&estate, var,
TransferExpandedObject(var->value,
estate.datum_context),
false,
true);
}
else if (VARATT_IS_EXTERNAL_EXPANDED_RO(DatumGetPointer(var->value)))
{
/* R/O pointer, keep it as-is until assigned to */
}
else if (var->datatype->typisarray)
{
/* flat array, so force to expanded form */
assign_simple_var(&estate, var,
expand_array(var->value,
estate.datum_context,
NULL),
false,
true);
}
}
}
break;
case UPLPGSQL_DTYPE_REC:
{
UPLpgSQL_rec *rec = (UPLpgSQL_rec *) estate.datums[n];
if (!fcinfo->args[i].isnull)
{
/* Assign row value from composite datum */
exec_move_row_from_datum(&estate,
(UPLpgSQL_variable *) rec,
fcinfo->args[i].value);
}
else
{
/* If arg is null, set variable to null */
exec_move_row(&estate, (UPLpgSQL_variable *) rec,
NULL, NULL);
}
/* clean up after exec_move_row() */
exec_eval_cleanup(&estate);
}
break;
default:
/* Anything else should not be an argument variable */
elog(ERROR, "unrecognized dtype: %d", func->datums[i]->dtype);
}
}
estate.err_text = gettext_noop("during function entry");
/*
* Set the magic variable FOUND to false
*/
exec_set_found(&estate, false);
/*
* Let the instrumentation plugin peek at this function
*/
if (*uplpgsql_plugin_ptr && (*uplpgsql_plugin_ptr)->func_beg)
((*uplpgsql_plugin_ptr)->func_beg) (&estate, func);
/*
* Now call the toplevel block of statements
*/
estate.err_text = NULL;
rc = exec_toplevel_block(&estate, func->action);
if (rc != UPLPGSQL_RC_RETURN)
{
estate.err_text = NULL;
ereport(ERROR,
(errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
errmsg("control reached end of function without RETURN")));
}
/*
* We got a return value - process it
*/
estate.err_text = gettext_noop("while casting return value to function's return type");
fcinfo->isnull = estate.retisnull;
if (estate.retisset)
{
ReturnSetInfo *rsi = estate.rsi;
/* Check caller can handle a set result */
if (!rsi || !IsA(rsi, ReturnSetInfo))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("set-valued function called in context that cannot accept a set")));
if (!(rsi->allowedModes & SFRM_Materialize))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("materialize mode required, but it is not allowed in this context")));
rsi->returnMode = SFRM_Materialize;
/* If we produced any tuples, send back the result */
if (estate.tuple_store)
{
MemoryContext oldcxt;
rsi->setResult = estate.tuple_store;
oldcxt = MemoryContextSwitchTo(estate.tuple_store_cxt);
rsi->setDesc = CreateTupleDescCopy(estate.tuple_store_desc);
MemoryContextSwitchTo(oldcxt);
}
estate.retval = (Datum) 0;
fcinfo->isnull = true;
}
else if (!estate.retisnull)
{
/*
* Cast result value to function's declared result type, and copy it
* out to the upper executor memory context. We must treat tuple
* results specially in order to deal with cases like rowtypes
* involving dropped columns.
*/
if (estate.retistuple)
{
/* Don't need coercion if rowtype is known to match */
if (func->fn_rettype == estate.rettype &&
func->fn_rettype != RECORDOID)
{
/*
* Copy the tuple result into upper executor memory context.
* However, if we have a R/W expanded datum, we can just
* transfer its ownership out to the upper context.
*/
estate.retval = SPI_datumTransfer(estate.retval,
false,
-1);
}
else
{
/*
* Need to look up the expected result type. XXX would be
* better to cache the tupdesc instead of repeating
* get_call_result_type(), but the only easy place to save it
* is in the UPLpgSQL_function struct, and that's too
* long-lived: composite types could change during the
* existence of a UPLpgSQL_function.
*/
Oid resultTypeId;
TupleDesc tupdesc;
switch (get_call_result_type(fcinfo, &resultTypeId, &tupdesc))
{
case TYPEFUNC_COMPOSITE:
/* got the expected result rowtype, now coerce it */
coerce_function_result_tuple(&estate, tupdesc);
break;
case TYPEFUNC_COMPOSITE_DOMAIN:
/* got the expected result rowtype, now coerce it */
coerce_function_result_tuple(&estate, tupdesc);
/* and check domain constraints */
/* XXX allowing caching here would be good, too */
domain_check(estate.retval, false, resultTypeId,
NULL, NULL);
break;
case TYPEFUNC_RECORD:
/*
* Failed to determine actual type of RECORD. We
* could raise an error here, but what this means in
* practice is that the caller is expecting any old
* generic rowtype, so we don't really need to be
* restrictive. Pass back the generated result as-is.
*/
estate.retval = SPI_datumTransfer(estate.retval,
false,
-1);
break;
default:
/* shouldn't get here if retistuple is true ... */
elog(ERROR, "return type must be a row type");
break;
}
}
}
else
{
/* Scalar case: use exec_cast_value */
estate.retval = exec_cast_value(&estate,
estate.retval,
&fcinfo->isnull,
estate.rettype,
-1,
func->fn_rettype,
-1);
/*
* If the function's return type isn't by value, copy the value
* into upper executor memory context. However, if we have a R/W
* expanded datum, we can just transfer its ownership out to the
* upper executor context.
*/
if (!fcinfo->isnull && !func->fn_retbyval)
estate.retval = SPI_datumTransfer(estate.retval,
false,
func->fn_rettyplen);
}
}
else
{
/*
* We're returning a NULL, which normally requires no conversion work
* regardless of datatypes. But, if we are casting it to a domain
* return type, we'd better check that the domain's constraints pass.
*/
if (func->fn_retisdomain)
estate.retval = exec_cast_value(&estate,
estate.retval,
&fcinfo->isnull,
estate.rettype,
-1,
func->fn_rettype,
-1);
}
estate.err_text = gettext_noop("during function exit");
/*
* Let the instrumentation plugin peek at this function
*/
if (*uplpgsql_plugin_ptr && (*uplpgsql_plugin_ptr)->func_end)
((*uplpgsql_plugin_ptr)->func_end) (&estate, func);
/* Clean up any leftover temporary memory */
uplpgsql_destroy_econtext(&estate);
exec_eval_cleanup(&estate);
/* stmt_mcontext will be destroyed when function's main context is */
/*
* Pop the error context stack
*/
error_context_stack = plerrcontext.previous;
/*
* Return the function's result
*/
return estate.retval;
}
/* ----------------------------------------------------------------
* uplpgsql_exec_function_jit
*
* Execute a PL/pgSQL function via its JIT-compiled native code.
* This mirrors uplpgsql_exec_function() exactly — same estate setup,
* argument handling, and return-value processing — but calls the
* JIT'd function instead of exec_toplevel_block().
* ----------------------------------------------------------------
*/
Datum
uplpgsql_exec_function_jit(UPLpgSQL_function *func, FunctionCallInfo fcinfo,
EState *simple_eval_estate,
ResourceOwner simple_eval_resowner,
ResourceOwner procedure_resowner,
bool atomic,
uplpgsql_jit_func jit_func)
{
UPLpgSQL_execstate estate;
UPLpgSQL_exec_state jit_estate;
ErrorContextCallback plerrcontext;
int i;
int rc;
/*
* Setup the execution state
*/
uplpgsql_estate_setup(&estate, func, (ReturnSetInfo *) fcinfo->resultinfo,
simple_eval_estate, simple_eval_resowner);
estate.procedure_resowner = procedure_resowner;
estate.atomic = atomic;
/*
* Setup error traceback support for ereport()
*/
plerrcontext.callback = uplpgsql_exec_error_callback;
plerrcontext.arg = &estate;
plerrcontext.previous = error_context_stack;
error_context_stack = &plerrcontext;
/*
* Make local execution copies of all the datums
*/
estate.err_text = gettext_noop("during initialization of execution state");
copy_uplpgsql_datums(&estate, func);
/*
* Store the actual call argument values into the appropriate variables
*/
estate.err_text = gettext_noop("while storing call arguments into local variables");
for (i = 0; i < func->fn_nargs; i++)
{
int n = func->fn_argvarnos[i];
switch (estate.datums[n]->dtype)
{
case UPLPGSQL_DTYPE_VAR:
{
UPLpgSQL_var *var = (UPLpgSQL_var *) estate.datums[n];
assign_simple_var(&estate, var,
fcinfo->args[i].value,
fcinfo->args[i].isnull,
false);
if (!var->isnull && var->datatype->typlen == -1)
{
if (VARATT_IS_EXTERNAL_EXPANDED_RW(DatumGetPointer(var->value)))
{
assign_simple_var(&estate, var,
TransferExpandedObject(var->value,
estate.datum_context),
false,
true);
}
else if (VARATT_IS_EXTERNAL_EXPANDED_RO(DatumGetPointer(var->value)))
{
/* R/O pointer, keep as-is until assigned to */
}
else if (var->datatype->typisarray)
{
assign_simple_var(&estate, var,
expand_array(var->value,
estate.datum_context,
NULL),
false,
true);
}
}
}
break;
case UPLPGSQL_DTYPE_REC:
{
UPLpgSQL_rec *rec = (UPLpgSQL_rec *) estate.datums[n];
if (!fcinfo->args[i].isnull)
{
exec_move_row_from_datum(&estate,
(UPLpgSQL_variable *) rec,
fcinfo->args[i].value);
}
else
{
exec_move_row(&estate, (UPLpgSQL_variable *) rec,
NULL, NULL);
}
exec_eval_cleanup(&estate);
}
break;
default:
elog(ERROR, "unrecognized dtype: %d", func->datums[i]->dtype);
}
}
estate.err_text = gettext_noop("during function entry");
/*
* Set the magic variable FOUND to false
*/
exec_set_found(&estate, false);
/*
* Let the instrumentation plugin peek at this function
*/
if (*uplpgsql_plugin_ptr && (*uplpgsql_plugin_ptr)->func_beg)
((*uplpgsql_plugin_ptr)->func_beg) (&estate, func);
/*
* SQL/PSM: If the function's top-level block declares a SQLSTATE
* variable, activate it so runtime helpers (FETCH etc.) can update it.
*/
if (func->action->sqlstate_varno >= 0)
estate.sqlstate_varno = func->action->sqlstate_varno;
/*
* Set up JIT execution state and call the native function
*/
estate.err_text = NULL;
memset(&jit_estate, 0, sizeof(jit_estate));
jit_estate.uplpgsql_estate = &estate;
rc = jit_func(&jit_estate);
/*
* Return values are set in estate by exec_stmt_return (called via
* uplpgsql_rt_exec_return). No need to copy from jit_estate since
* the runtime helper writes directly to the PL/pgSQL estate.
*
* Clear err_stmt to match exec_stmt_block's behavior: the block is
* complete, so error context should no longer reference a statement.
* Without this, errors during return value casting would incorrectly
* include the RETURN statement's line number.
*/
estate.err_stmt = NULL;
if (rc != UPLPGSQL_RC_RETURN)
{
estate.err_text = NULL;
ereport(ERROR,
(errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
errmsg("control reached end of function without RETURN")));
}
/*
* We got a return value - process it
*/
estate.err_text = gettext_noop("while casting return value to function's return type");