forked from nextgres/uplpgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuplpgsql_handler.c
More file actions
737 lines (656 loc) · 21.4 KB
/
Copy pathuplpgsql_handler.c
File metadata and controls
737 lines (656 loc) · 21.4 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
/*-------------------------------------------------------------------------
*
* uplpgsql_handler.c
* PostgreSQL language handler entry points for uplpgsql.
*
* This file is the main entry point for all uplpgsql execution.
* It contains:
*
* _PG_init() — Module initialization: sets up LLVM, cache,
* GUCs, and transaction callbacks.
*
* uplpgsql_call_handler() — Main execution entry for functions and triggers.
* Compiles the function (get AST via forked parser),
* checks the JIT cache, optionally JIT-compiles,
* then executes via native code or interpreter.
*
* uplpgsql_inline_handler() — Executes DO blocks (always interpreted since
* they run once and JIT overhead would dominate).
*
* uplpgsql_validator() — CREATE FUNCTION validation via test-compilation.
*
* The call_handler implements the three-state cache flow:
* 1. Cache lookup: HIT → use JIT'd code, SKIP → use interpreter
* 2. Cache MISS → evaluate heuristic (uplpgsql_should_jit)
* 3. If should JIT → compile, cache result; else cache skip marker
* 4. JIT compilation failures are caught and fall back to interpreter
*
*
* Copyright (c) 2003-2014, Jonah H. Harris <[email protected]>
* Copyright (c) 2014-2026, NEXTGRES, LLC. <[email protected]>
* All rights reserved.
*
* 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
*-------------------------------------------------------------------------
*/
#include "upl_common.h"
#include "access/xact.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "commands/event_trigger.h"
#include "commands/trigger.h"
#include "funcapi.h"
#include "executor/executor.h"
#include "nodes/parsenodes.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/plancache.h"
#include "utils/resowner.h"
#include "utils/syscache.h"
#include "utils/varlena.h"
/* Extension version, as reported by PG_MODULE_MAGIC_EXT and \dx */
#define UPLPGSQL_VERSION "1.0"
PG_MODULE_MAGIC_EXT(
.name = "uplpgsql",
.version = UPLPGSQL_VERSION
);
/*
* Forked PL/pgSQL global variables (originally in pl_handler.c).
*
* These mirror the original PL/pgSQL GUCs but under the "uplpgsql." prefix.
* They are referenced by the forked parser/executor code (upl_comp.c,
* upl_exec.c, etc.) which was mechanically renamed from plpgsql_* to
* uplpgsql_*.
*/
static const struct config_enum_entry variable_conflict_options[] = {
{"error", UPLPGSQL_RESOLVE_ERROR, false},
{"use_variable", UPLPGSQL_RESOLVE_VARIABLE, false},
{"use_column", UPLPGSQL_RESOLVE_COLUMN, false},
{NULL, 0, false}
};
int uplpgsql_variable_conflict = UPLPGSQL_RESOLVE_ERROR;
bool uplpgsql_print_strict_params = false;
bool uplpgsql_check_asserts = true;
static char *uplpgsql_extra_warnings_string = NULL;
static char *uplpgsql_extra_errors_string = NULL;
int uplpgsql_extra_warnings;
int uplpgsql_extra_errors;
/* Plugin hook pointer */
UPLpgSQL_plugin **uplpgsql_plugin_ptr = NULL;
/*
* uplpgsql-specific GUCs (not in standard PL/pgSQL).
*
* enable_jit_heuristic: When true, use cost/benefit heuristic to skip JIT for
* functions unlikely to benefit. When false (default),
* JIT-compile every function.
* log_compilation: Log a message when a function is JIT compiled
* dump_ir: Dump the generated LLVM IR to the server log (debugging)
*/
static bool uplpgsql_log_compilation = false;
bool uplpgsql_dump_ir = false;
bool uplpgsql_enable_jit_heuristic = false;
/* Forward declarations for GUC hooks */
static bool uplpgsql_extra_checks_check_hook(char **newvalue, void **extra,
GucSource source);
static void uplpgsql_extra_warnings_assign_hook(const char *newvalue, void *extra);
static void uplpgsql_extra_errors_assign_hook(const char *newvalue, void *extra);
/*
* Cache staleness check callback for upl_cache_lookup().
*
* Returns true if the cached lang_func pointer matches the current one,
* meaning the JIT'd code's embedded AST pointers are still valid.
*/
static bool
uplpgsql_cache_check(void *cached_lang_func, void *current_lang_func)
{
return (cached_lang_func == current_lang_func);
}
/* Function declarations */
PG_FUNCTION_INFO_V1(uplpgsql_call_handler);
PG_FUNCTION_INFO_V1(uplpgsql_inline_handler);
PG_FUNCTION_INFO_V1(uplpgsql_validator);
/*
* _PG_init - module load callback
*/
void
_PG_init(void)
{
static bool inited = false;
if (inited)
return;
/* Initialize LLVM (core engine) */
upl_llvm_init();
/* Initialize function cache (core engine) */
upl_cache_init();
/*
* Register forked PL/pgSQL GUCs under "uplpgsql." prefix
*/
DefineCustomEnumVariable("uplpgsql.variable_conflict",
"Sets handling of conflicts between UPL/pgSQL variable names and table column names.",
NULL,
&uplpgsql_variable_conflict,
UPLPGSQL_RESOLVE_ERROR,
variable_conflict_options,
PGC_SUSET, 0,
NULL, NULL, NULL);
DefineCustomBoolVariable("uplpgsql.print_strict_params",
"Print information about parameters in the DETAIL part of the error messages generated on INTO ... STRICT failures.",
NULL,
&uplpgsql_print_strict_params,
false,
PGC_USERSET, 0,
NULL, NULL, NULL);
DefineCustomBoolVariable("uplpgsql.check_asserts",
"Perform checks given in ASSERT statements.",
NULL,
&uplpgsql_check_asserts,
true,
PGC_USERSET, 0,
NULL, NULL, NULL);
DefineCustomStringVariable("uplpgsql.extra_warnings",
"List of programming constructs that should produce a warning.",
NULL,
&uplpgsql_extra_warnings_string,
"none",
PGC_USERSET, GUC_LIST_INPUT,
uplpgsql_extra_checks_check_hook,
uplpgsql_extra_warnings_assign_hook,
NULL);
DefineCustomStringVariable("uplpgsql.extra_errors",
"List of programming constructs that should produce an error.",
NULL,
&uplpgsql_extra_errors_string,
"none",
PGC_USERSET, GUC_LIST_INPUT,
uplpgsql_extra_checks_check_hook,
uplpgsql_extra_errors_assign_hook,
NULL);
/* uplpgsql-specific GUCs */
DefineCustomBoolVariable("uplpgsql.log_compilation",
"Log when functions are JIT compiled.",
NULL,
&uplpgsql_log_compilation,
false,
PGC_USERSET, 0,
NULL, NULL, NULL);
DefineCustomBoolVariable("uplpgsql.dump_ir",
"Dump LLVM IR to server log.",
NULL,
&uplpgsql_dump_ir,
false,
PGC_USERSET, 0,
NULL, NULL, NULL);
DefineCustomBoolVariable("uplpgsql.enable_jit_heuristic",
"Use cost/benefit heuristic to skip JIT for functions unlikely to benefit. "
"When off (default), all functions are JIT compiled.",
NULL,
&uplpgsql_enable_jit_heuristic,
false,
PGC_USERSET, 0,
NULL, NULL, NULL);
/*
* Reserve the prefix only after every uplpgsql.* GUC is defined. Doing it
* earlier discards the placeholders for the ones defined below, so a value
* SET before the module was first loaded in a session was thrown away with
* "invalid configuration parameter name".
*/
MarkGUCPrefixReserved("uplpgsql");
/* Register transaction callbacks for cleanup */
RegisterXactCallback(uplpgsql_xact_cb, NULL);
RegisterSubXactCallback(uplpgsql_subxact_cb, NULL);
/* Set up rendezvous point with optional instrumentation plugin */
uplpgsql_plugin_ptr = (UPLpgSQL_plugin **)
find_rendezvous_variable("UPLpgSQL_plugin");
inited = true;
}
/*
* GUC check/assign hooks for extra_warnings/extra_errors
*/
static bool
uplpgsql_extra_checks_check_hook(char **newvalue, void **extra, GucSource source)
{
char *rawstring;
List *elemlist;
ListCell *l;
int extrachecks = 0;
int *myextra;
if (pg_strcasecmp(*newvalue, "all") == 0)
extrachecks = UPLPGSQL_XCHECK_ALL;
else if (pg_strcasecmp(*newvalue, "none") == 0)
extrachecks = UPLPGSQL_XCHECK_NONE;
else
{
rawstring = pstrdup(*newvalue);
if (!SplitIdentifierString(rawstring, ',', &elemlist))
{
GUC_check_errdetail("List syntax is invalid.");
pfree(rawstring);
list_free(elemlist);
return false;
}
foreach(l, elemlist)
{
char *tok = (char *) lfirst(l);
if (pg_strcasecmp(tok, "shadowed_variables") == 0)
extrachecks |= UPLPGSQL_XCHECK_SHADOWVAR;
else if (pg_strcasecmp(tok, "too_many_rows") == 0)
extrachecks |= UPLPGSQL_XCHECK_TOOMANYROWS;
else if (pg_strcasecmp(tok, "strict_multi_assignment") == 0)
extrachecks |= UPLPGSQL_XCHECK_STRICTMULTIASSIGNMENT;
else if (pg_strcasecmp(tok, "all") == 0 || pg_strcasecmp(tok, "none") == 0)
{
GUC_check_errdetail("Key word \"%s\" cannot be combined with other key words.", tok);
pfree(rawstring);
list_free(elemlist);
return false;
}
else
{
GUC_check_errdetail("Unrecognized key word: \"%s\".", tok);
pfree(rawstring);
list_free(elemlist);
return false;
}
}
pfree(rawstring);
list_free(elemlist);
}
myextra = (int *) guc_malloc(LOG, sizeof(int));
if (!myextra)
return false;
*myextra = extrachecks;
*extra = myextra;
return true;
}
static void
uplpgsql_extra_warnings_assign_hook(const char *newvalue, void *extra)
{
uplpgsql_extra_warnings = *((int *) extra);
}
static void
uplpgsql_extra_errors_assign_hook(const char *newvalue, void *extra)
{
uplpgsql_extra_errors = *((int *) extra);
}
/*
* uplpgsql_call_handler - main entry point for function/trigger execution
*
* For regular functions and triggers, we attempt JIT compilation on first
* call and execute the native code. On subsequent calls, the cached JIT'd
* function is used directly. Falls back to the interpreter for event
* triggers (JIT support for those is future work).
*/
Datum
uplpgsql_call_handler(PG_FUNCTION_ARGS)
{
bool nonatomic;
UPLpgSQL_function *func;
UPLpgSQL_func *jitfunc;
UPLpgSQL_execstate *save_cur_estate;
ResourceOwner procedure_resowner;
volatile Datum retval = (Datum) 0;
int rc;
bool isTrigger;
bool isEventTrigger;
nonatomic = fcinfo->context &&
IsA(fcinfo->context, CallContext) &&
!castNode(CallContext, fcinfo->context)->atomic;
isTrigger = CALLED_AS_TRIGGER(fcinfo);
isEventTrigger = CALLED_AS_EVENT_TRIGGER(fcinfo);
/*
* Connect to SPI manager
*/
SPI_connect_ext(nonatomic ? SPI_OPT_NONATOMIC : 0);
/*
* Compile the function (get AST via our forked parser).
*/
/*
* forValidator is false here: we are executing, not validating. It must
* not be confused with the trigger flags — those reach do_compile()
* through fcinfo's context, which is already set up by the caller.
* Passing the trigger flag compiled every trigger in validator mode on
* every fire, which ran the extra syntax checks at execution time and,
* worse, applied uplpgsql.extra_warnings/extra_errors to a running
* trigger — so a DML statement could fail purely because a GUC was set.
*/
func = uplpgsql_compile(fcinfo, false);
/* Must save and restore prior value of cur_estate */
save_cur_estate = func->cur_estate;
/* Mark the function as busy, so it can't be deleted from under us */
func->cfunc.use_count++;
/*
* If we'll need a procedure-lifespan resowner to execute any CALL or DO
* statements, create it now. Since this resowner is not tied to any
* parent, failing to free it would result in process-lifespan leaks.
* Therefore, be very wary of adding any code between here and the PG_TRY
* block.
*/
procedure_resowner =
(nonatomic && func->requires_procedure_resowner) ?
ResourceOwnerCreate(NULL, "UPL/pgSQL procedure resources") : NULL;
/*
* For regular functions and triggers, try the JIT path.
* Event triggers still use the interpreter.
*/
jitfunc = NULL;
if (!isEventTrigger)
{
int cache_status;
UPL_func *upl_jitfunc;
cache_status = upl_cache_lookup(func->fn_oid,
func->cfunc.fn_xmin,
func->cfunc.fn_tid,
func,
uplpgsql_cache_check,
&upl_jitfunc);
if (cache_status == UPL_CACHE_MISS)
{
/*
* First call for this function version. By default we JIT
* everything. When uplpgsql.enable_jit_heuristic is on, run
* the cost/benefit scorer to decide.
*/
if (!uplpgsql_enable_jit_heuristic || uplpgsql_should_jit(func))
{
MemoryContext oldcontext = CurrentMemoryContext;
ResourceOwner oldowner = CurrentResourceOwner;
/*
* Compile inside an internal subtransaction.
*
* Compilation can elog(ERROR) — an unsupported construct, an
* expression whose plan will not prepare, an LLVM verification
* failure — and we intend to catch that and fall back to the
* interpreter. Catching an arbitrary error and carrying on is
* only safe when a subtransaction restores the memory context,
* resource owner, buffer pins and syscache references;
* otherwise the "graceful fallback" leaks whatever the failed
* compile was holding. That is not hypothetical: a simple
* CASE over a non-integer value fails to plan at compile time
* and leaked syscache references on every such function.
*/
BeginInternalSubTransaction(NULL);
MemoryContextSwitchTo(oldcontext);
PG_TRY();
{
jitfunc = uplpgsql_compile_function(func);
upl_cache_store(func->fn_oid,
func->cfunc.fn_xmin,
func->cfunc.fn_tid,
func,
(UPL_func *) jitfunc);
/* Success — commit the subtransaction. */
ReleaseCurrentSubTransaction();
MemoryContextSwitchTo(oldcontext);
CurrentResourceOwner = oldowner;
if (uplpgsql_log_compilation)
elog(LOG, "uplpgsql: JIT compiled function %s (oid %u)",
func->fn_signature, func->fn_oid);
}
PG_CATCH();
{
/* Report the failure, then roll the subtransaction back. */
MemoryContextSwitchTo(oldcontext);
EmitErrorReport();
FlushErrorState();
RollbackAndReleaseCurrentSubTransaction();
MemoryContextSwitchTo(oldcontext);
CurrentResourceOwner = oldowner;
jitfunc = NULL;
if (uplpgsql_log_compilation)
elog(LOG, "uplpgsql: JIT compilation failed for %s, "
"using interpreter",
func->fn_signature);
}
PG_END_TRY();
}
else
{
/* Heuristic says skip — cache that decision */
upl_cache_store_skip(func->fn_oid,
func->cfunc.fn_xmin,
func->cfunc.fn_tid,
func);
jitfunc = NULL;
if (uplpgsql_log_compilation)
elog(LOG, "uplpgsql: skipping JIT for %s (heuristic)",
func->fn_signature);
}
}
else if (cache_status == UPL_CACHE_SKIP)
{
jitfunc = NULL;
}
else
{
/* UPL_CACHE_HIT: cast back to PL/pgSQL-specific type */
jitfunc = (UPLpgSQL_func *) upl_jitfunc;
}
}
PG_TRY();
{
if (isTrigger)
{
if (jitfunc != NULL)
{
retval = PointerGetDatum(
uplpgsql_exec_trigger_jit(func,
(TriggerData *) fcinfo->context,
jitfunc->jit_func));
}
else
{
retval = PointerGetDatum(
uplpgsql_exec_trigger(func,
(TriggerData *) fcinfo->context));
}
}
else if (isEventTrigger)
{
uplpgsql_exec_event_trigger(func,
(EventTriggerData *) fcinfo->context);
/* there's no return value in this case */
}
else
{
if (jitfunc != NULL)
{
/* Execute via JIT'd native code */
retval = uplpgsql_exec_function_jit(func, fcinfo,
NULL, NULL,
procedure_resowner,
!nonatomic,
jitfunc->jit_func);
}
else
{
/* Fallback to interpreter */
retval = uplpgsql_exec_function(func, fcinfo,
NULL, NULL,
procedure_resowner,
!nonatomic);
}
}
}
PG_FINALLY();
{
/* Decrement use-count, restore cur_estate */
func->cfunc.use_count--;
func->cur_estate = save_cur_estate;
/* Be sure to release the procedure resowner if any */
if (procedure_resowner)
{
ReleaseAllPlanCacheRefsInOwner(procedure_resowner);
ResourceOwnerDelete(procedure_resowner);
}
}
PG_END_TRY();
/*
* Disconnect from SPI manager
*/
if ((rc = SPI_finish()) != SPI_OK_FINISH)
elog(ERROR, "SPI_finish failed: %s", SPI_result_code_string(rc));
return retval;
}
/*
* uplpgsql_inline_handler - DO block execution
*
* Executes anonymous code blocks (DO $$ ... $$ LANGUAGE uplpgsql).
* DO blocks are always interpreted (not JIT'd) since they run once and
* compilation overhead would exceed any benefit.
*
* This mirrors plpgsql_inline_handler() — sets up a private EState and
* resowner for simple-expression execution that survive COMMIT/ROLLBACK.
*/
Datum
uplpgsql_inline_handler(PG_FUNCTION_ARGS)
{
LOCAL_FCINFO(fake_fcinfo, 0);
InlineCodeBlock *codeblock = castNode(InlineCodeBlock,
DatumGetPointer(PG_GETARG_DATUM(0)));
UPLpgSQL_function *func;
FmgrInfo flinfo;
EState *simple_eval_estate;
ResourceOwner simple_eval_resowner;
Datum retval;
int rc;
/*
* Connect to SPI manager
*/
SPI_connect_ext(codeblock->atomic ? 0 : SPI_OPT_NONATOMIC);
/* Compile the anonymous code block */
func = uplpgsql_compile_inline(codeblock->source_text);
/* Mark the function as busy, just pro forma */
func->cfunc.use_count++;
/*
* Set up a fake fcinfo with just enough info to satisfy
* uplpgsql_exec_function().
*/
MemSet(fake_fcinfo, 0, SizeForFunctionCallInfo(0));
MemSet(&flinfo, 0, sizeof(flinfo));
fake_fcinfo->flinfo = &flinfo;
flinfo.fn_oid = InvalidOid;
flinfo.fn_mcxt = CurrentMemoryContext;
/*
* Create a private EState and resowner for simple-expression execution.
* These must survive any COMMIT/ROLLBACK the DO block executes.
*/
simple_eval_estate = CreateExecutorState();
simple_eval_resowner =
ResourceOwnerCreate(NULL, "UPL/pgSQL DO block simple expressions");
/* Execute the DO block via the interpreter (not JIT'd) */
PG_TRY();
{
retval = uplpgsql_exec_function(func, fake_fcinfo,
simple_eval_estate,
simple_eval_resowner,
simple_eval_resowner,
codeblock->atomic);
}
PG_CATCH();
{
/* Clean up on failure */
uplpgsql_subxact_cb(SUBXACT_EVENT_ABORT_SUB,
GetCurrentSubTransactionId(),
0, NULL);
FreeExecutorState(simple_eval_estate);
ReleaseAllPlanCacheRefsInOwner(simple_eval_resowner);
ResourceOwnerDelete(simple_eval_resowner);
func->cfunc.use_count--;
uplpgsql_free_function_memory(func);
PG_RE_THROW();
}
PG_END_TRY();
/* Clean up */
FreeExecutorState(simple_eval_estate);
ReleaseAllPlanCacheRefsInOwner(simple_eval_resowner);
ResourceOwnerDelete(simple_eval_resowner);
func->cfunc.use_count--;
uplpgsql_free_function_memory(func);
/*
* Disconnect from SPI manager
*/
if ((rc = SPI_finish()) != SPI_OK_FINISH)
elog(ERROR, "SPI_finish failed: %s", SPI_result_code_string(rc));
return retval;
}
/*
* uplpgsql_validator - CREATE FUNCTION validation
*
* Validates by test-compiling the function with PL/pgSQL's parser.
* Mirrors uplpgsql_validator() logic since that function is not callable
* as a C function from extensions.
*/
Datum
uplpgsql_validator(PG_FUNCTION_ARGS)
{
Oid funcoid = PG_GETARG_OID(0);
HeapTuple tuple;
Form_pg_proc proc;
bool is_trigger = false;
bool is_event_trigger = false;
if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid, funcoid))
PG_RETURN_VOID();
/* Get the function's pg_proc entry */
tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
if (!HeapTupleIsValid(tuple))
elog(ERROR, "cache lookup failed for function %u", funcoid);
proc = (Form_pg_proc) GETSTRUCT(tuple);
if (proc->prorettype == TRIGGEROID)
is_trigger = true;
else if (proc->prorettype == EVENT_TRIGGEROID)
is_event_trigger = true;
ReleaseSysCache(tuple);
/* Postpone body checks if !check_function_bodies */
if (check_function_bodies)
{
LOCAL_FCINFO(fake_fcinfo, 0);
FmgrInfo flinfo;
int rc;
TriggerData trigdata;
EventTriggerData etrigdata;
SPI_connect();
MemSet(fake_fcinfo, 0, SizeForFunctionCallInfo(0));
MemSet(&flinfo, 0, sizeof(flinfo));
fake_fcinfo->flinfo = &flinfo;
flinfo.fn_oid = funcoid;
flinfo.fn_mcxt = CurrentMemoryContext;
if (is_trigger)
{
MemSet(&trigdata, 0, sizeof(trigdata));
trigdata.type = T_TriggerData;
fake_fcinfo->context = (Node *) &trigdata;
}
else if (is_event_trigger)
{
MemSet(&etrigdata, 0, sizeof(etrigdata));
etrigdata.type = T_EventTriggerData;
fake_fcinfo->context = (Node *) &etrigdata;
}
/*
* Test-compile the function.
*
* forValidator must be true — we are the validator. It is what
* enables the extra syntax checks, the extra_warnings/extra_errors
* checks, polymorphic return-type resolution, and source-position
* reporting in compile errors; do_compile() zeroes all of that when
* it is false. The trigger flags are conveyed via fake_fcinfo's
* context, set above, not through this argument.
*/
uplpgsql_compile(fake_fcinfo, true);
if ((rc = SPI_finish()) != SPI_OK_FINISH)
elog(ERROR, "SPI_finish failed: %s", SPI_result_code_string(rc));
}
PG_RETURN_VOID();
}