1 /**********************************************************************
2 * pltcl.c - PostgreSQL support for Tcl as
3 * procedural language (PL)
7 **********************************************************************/
16 #include "access/htup_details.h"
17 #include "access/xact.h"
18 #include "catalog/objectaccess.h"
19 #include "catalog/pg_proc.h"
20 #include "catalog/pg_type.h"
21 #include "commands/event_trigger.h"
22 #include "commands/trigger.h"
23 #include "executor/spi.h"
26 #include "mb/pg_wchar.h"
27 #include "miscadmin.h"
28 #include "nodes/makefuncs.h"
29 #include "parser/parse_func.h"
30 #include "parser/parse_type.h"
32 #include "tcop/tcopprot.h"
33 #include "utils/builtins.h"
34 #include "utils/lsyscache.h"
35 #include "utils/memutils.h"
36 #include "utils/regproc.h"
37 #include "utils/rel.h"
38 #include "utils/syscache.h"
39 #include "utils/typcache.h"
44 #define HAVE_TCL_VERSION(maj,min) \
45 ((TCL_MAJOR_VERSION > maj) || \
46 (TCL_MAJOR_VERSION == maj && TCL_MINOR_VERSION >= min))
48 /* Insist on Tcl >= 8.4 */
49 #if !HAVE_TCL_VERSION(8,4)
50 #error PostgreSQL only supports Tcl 8.4 or later.
53 /* Hack to deal with Tcl 8.6 const-ification without losing compatibility */
58 /* define our text domain for translations */
60 #define TEXTDOMAIN PG_TEXTDOMAIN("pltcl")
64 * Support for converting between UTF8 (which is what all strings going into
65 * or out of Tcl should be) and the database encoding.
67 * If you just use utf_u2e() or utf_e2u() directly, they will leak some
68 * palloc'd space when doing a conversion. This is not worth worrying about
69 * if it only happens, say, once per PL/Tcl function call. If it does seem
70 * worth worrying about, use the wrapper macros.
74 utf_u2e(const char *src)
76 return pg_any_to_server(src, strlen(src), PG_UTF8);
80 utf_e2u(const char *src)
82 return pg_server_to_any(src, strlen(src), PG_UTF8);
87 const char *_pltcl_utf_src = NULL; \
88 char *_pltcl_utf_dst = NULL
91 if (_pltcl_utf_src != (const char *) _pltcl_utf_dst) \
92 pfree(_pltcl_utf_dst); \
96 (_pltcl_utf_dst = utf_u2e(_pltcl_utf_src = (x)))
99 (_pltcl_utf_dst = utf_e2u(_pltcl_utf_src = (x)))
102 /**********************************************************************
103 * Information associated with a Tcl interpreter. We have one interpreter
104 * that is used for all pltclu (untrusted) functions. For pltcl (trusted)
105 * functions, there is a separate interpreter for each effective SQL userid.
106 * (This is needed to ensure that an unprivileged user can't inject Tcl code
107 * that'll be executed with the privileges of some other SQL user.)
109 * The pltcl_interp_desc structs are kept in a Postgres hash table indexed
110 * by userid OID, with OID 0 used for the single untrusted interpreter.
111 **********************************************************************/
112 typedef struct pltcl_interp_desc
114 Oid user_id; /* Hash key (must be first!) */
115 Tcl_Interp *interp; /* The interpreter */
116 Tcl_HashTable query_hash; /* pltcl_query_desc structs */
120 /**********************************************************************
121 * The information we cache about loaded procedures
123 * The pltcl_proc_desc struct itself, as well as all subsidiary data,
124 * is stored in the memory context identified by the fn_cxt field.
125 * We can reclaim all the data by deleting that context, and should do so
126 * when the fn_refcount goes to zero. (But note that we do not bother
127 * trying to clean up Tcl's copy of the procedure definition: it's Tcl's
128 * problem to manage its memory when we replace a proc definition. We do
129 * not clean up pltcl_proc_descs when a pg_proc row is deleted, only when
130 * it is updated, and the same policy applies to Tcl's copy as well.)
132 * Note that the data in this struct is shared across all active calls;
133 * nothing except the fn_refcount should be changed by a call instance.
134 **********************************************************************/
135 typedef struct pltcl_proc_desc
137 char *user_proname; /* user's name (from pg_proc.proname) */
138 char *internal_proname; /* Tcl name (based on function OID) */
139 MemoryContext fn_cxt; /* memory context for this procedure */
140 unsigned long fn_refcount; /* number of active references */
141 TransactionId fn_xmin; /* xmin of pg_proc row */
142 ItemPointerData fn_tid; /* TID of pg_proc row */
143 bool fn_readonly; /* is function readonly? */
144 bool lanpltrusted; /* is it pltcl (vs. pltclu)? */
145 pltcl_interp_desc *interp_desc; /* interpreter to use */
146 Oid result_typid; /* OID of fn's result type */
147 FmgrInfo result_in_func; /* input function for fn's result type */
148 Oid result_typioparam; /* param to pass to same */
149 bool fn_retisset; /* true if function returns a set */
150 bool fn_retistuple; /* true if function returns composite */
151 bool fn_retisdomain; /* true if function returns domain */
152 void *domain_info; /* opaque cache for domain checks */
153 int nargs; /* number of arguments */
154 /* these arrays have nargs entries: */
155 FmgrInfo *arg_out_func; /* output fns for arg types */
156 bool *arg_is_rowtype; /* is each arg composite? */
160 /**********************************************************************
161 * The information we cache about prepared and saved plans
162 **********************************************************************/
163 typedef struct pltcl_query_desc
169 FmgrInfo *arginfuncs;
174 /**********************************************************************
175 * For speedy lookup, we maintain a hash table mapping from
176 * function OID + trigger flag + user OID to pltcl_proc_desc pointers.
177 * The reason the pltcl_proc_desc struct isn't directly part of the hash
178 * entry is to simplify recovery from errors during compile_pltcl_function.
180 * Note: if the same function is called by multiple userIDs within a session,
181 * there will be a separate pltcl_proc_desc entry for each userID in the case
182 * of pltcl functions, but only one entry for pltclu functions, because we
183 * set user_id = 0 for that case.
184 **********************************************************************/
185 typedef struct pltcl_proc_key
187 Oid proc_id; /* Function OID */
190 * is_trigger is really a bool, but declare as Oid to ensure this struct
191 * contains no padding
193 Oid is_trigger; /* is it a trigger function? */
194 Oid user_id; /* User calling the function, or 0 */
197 typedef struct pltcl_proc_ptr
199 pltcl_proc_key proc_key; /* Hash key (must be first!) */
200 pltcl_proc_desc *proc_ptr;
204 /**********************************************************************
206 **********************************************************************/
207 typedef struct pltcl_call_state
209 /* Call info struct, or NULL in a trigger */
210 FunctionCallInfo fcinfo;
212 /* Trigger data, if we're in a normal (not event) trigger; else NULL */
213 TriggerData *trigdata;
215 /* Function we're executing (NULL if not yet identified) */
216 pltcl_proc_desc *prodesc;
219 * Information for SRFs and functions returning composite types.
220 * ret_tupdesc and attinmeta are set up if either fn_retistuple or
221 * fn_retisset, since even a scalar-returning SRF needs a tuplestore.
223 TupleDesc ret_tupdesc; /* return rowtype, if retistuple or retisset */
224 AttInMetadata *attinmeta; /* metadata for building tuples of that type */
226 ReturnSetInfo *rsi; /* passed-in ReturnSetInfo, if any */
227 Tuplestorestate *tuple_store; /* SRFs accumulate result here */
228 MemoryContext tuple_store_cxt; /* context and resowner for tuplestore */
229 ResourceOwner tuple_store_owner;
233 /**********************************************************************
235 **********************************************************************/
236 static char *pltcl_start_proc = NULL;
237 static char *pltclu_start_proc = NULL;
238 static bool pltcl_pm_init_done = false;
239 static Tcl_Interp *pltcl_hold_interp = NULL;
240 static HTAB *pltcl_interp_htab = NULL;
241 static HTAB *pltcl_proc_htab = NULL;
243 /* this is saved and restored by pltcl_handler */
244 static pltcl_call_state *pltcl_current_call_state = NULL;
246 /**********************************************************************
247 * Lookup table for SQLSTATE condition names
248 **********************************************************************/
253 } TclExceptionNameMap;
255 static const TclExceptionNameMap exception_name_map[] = {
256 #include "pltclerrcodes.h" /* pgrminclude ignore */
260 /**********************************************************************
261 * Forward declarations
262 **********************************************************************/
265 static void pltcl_init_interp(pltcl_interp_desc *interp_desc,
266 Oid prolang, bool pltrusted);
267 static pltcl_interp_desc *pltcl_fetch_interp(Oid prolang, bool pltrusted);
268 static void call_pltcl_start_proc(Oid prolang, bool pltrusted);
269 static void start_proc_error_callback(void *arg);
271 static Datum pltcl_handler(PG_FUNCTION_ARGS, bool pltrusted);
273 static Datum pltcl_func_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state,
275 static HeapTuple pltcl_trigger_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state,
277 static void pltcl_event_trigger_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state,
280 static void throw_tcl_error(Tcl_Interp *interp, const char *proname);
282 static pltcl_proc_desc *compile_pltcl_function(Oid fn_oid, Oid tgreloid,
283 bool is_event_trigger,
286 static int pltcl_elog(ClientData cdata, Tcl_Interp *interp,
287 int objc, Tcl_Obj *const objv[]);
288 static void pltcl_construct_errorCode(Tcl_Interp *interp, ErrorData *edata);
289 static const char *pltcl_get_condition_name(int sqlstate);
290 static int pltcl_quote(ClientData cdata, Tcl_Interp *interp,
291 int objc, Tcl_Obj *const objv[]);
292 static int pltcl_argisnull(ClientData cdata, Tcl_Interp *interp,
293 int objc, Tcl_Obj *const objv[]);
294 static int pltcl_returnnull(ClientData cdata, Tcl_Interp *interp,
295 int objc, Tcl_Obj *const objv[]);
296 static int pltcl_returnnext(ClientData cdata, Tcl_Interp *interp,
297 int objc, Tcl_Obj *const objv[]);
298 static int pltcl_SPI_execute(ClientData cdata, Tcl_Interp *interp,
299 int objc, Tcl_Obj *const objv[]);
300 static int pltcl_process_SPI_result(Tcl_Interp *interp,
301 const char *arrayname,
304 SPITupleTable *tuptable,
306 static int pltcl_SPI_prepare(ClientData cdata, Tcl_Interp *interp,
307 int objc, Tcl_Obj *const objv[]);
308 static int pltcl_SPI_execute_plan(ClientData cdata, Tcl_Interp *interp,
309 int objc, Tcl_Obj *const objv[]);
310 static int pltcl_SPI_lastoid(ClientData cdata, Tcl_Interp *interp,
311 int objc, Tcl_Obj *const objv[]);
312 static int pltcl_subtransaction(ClientData cdata, Tcl_Interp *interp,
313 int objc, Tcl_Obj *const objv[]);
314 static int pltcl_commit(ClientData cdata, Tcl_Interp *interp,
315 int objc, Tcl_Obj *const objv[]);
316 static int pltcl_rollback(ClientData cdata, Tcl_Interp *interp,
317 int objc, Tcl_Obj *const objv[]);
319 static void pltcl_subtrans_begin(MemoryContext oldcontext,
320 ResourceOwner oldowner);
321 static void pltcl_subtrans_commit(MemoryContext oldcontext,
322 ResourceOwner oldowner);
323 static void pltcl_subtrans_abort(Tcl_Interp *interp,
324 MemoryContext oldcontext,
325 ResourceOwner oldowner);
327 static void pltcl_set_tuple_values(Tcl_Interp *interp, const char *arrayname,
328 uint64 tupno, HeapTuple tuple, TupleDesc tupdesc);
329 static Tcl_Obj *pltcl_build_tuple_argument(HeapTuple tuple, TupleDesc tupdesc);
330 static HeapTuple pltcl_build_tuple_result(Tcl_Interp *interp,
331 Tcl_Obj **kvObjv, int kvObjc,
332 pltcl_call_state *call_state);
333 static void pltcl_init_tuple_store(pltcl_call_state *call_state);
337 * Hack to override Tcl's builtin Notifier subsystem. This prevents the
338 * backend from becoming multithreaded, which breaks all sorts of things.
339 * That happens in the default version of Tcl_InitNotifier if the TCL library
340 * has been compiled with multithreading support (i.e. when TCL_THREADS is
341 * defined under Unix, and in all cases under Windows).
342 * It's okay to disable the notifier because we never enter the Tcl event loop
343 * from Postgres, so the notifier capabilities are initialized, but never
344 * used. Only InitNotifier and DeleteFileHandler ever seem to get called
345 * within Postgres, but we implement all the functions for completeness.
348 pltcl_InitNotifier(void)
350 static int fakeThreadKey; /* To give valid address for ClientData */
352 return (ClientData) &(fakeThreadKey);
356 pltcl_FinalizeNotifier(ClientData clientData)
361 pltcl_SetTimer(CONST86 Tcl_Time *timePtr)
366 pltcl_AlertNotifier(ClientData clientData)
371 pltcl_CreateFileHandler(int fd, int mask,
372 Tcl_FileProc *proc, ClientData clientData)
377 pltcl_DeleteFileHandler(int fd)
382 pltcl_ServiceModeHook(int mode)
387 pltcl_WaitForEvent(CONST86 Tcl_Time *timePtr)
394 * _PG_init() - library load-time initialization
396 * DO NOT make this static nor change its name!
398 * The work done here must be safe to do in the postmaster process,
399 * in case the pltcl library is preloaded in the postmaster.
404 Tcl_NotifierProcs notifier;
407 /* Be sure we do initialization only once (should be redundant now) */
408 if (pltcl_pm_init_done)
411 pg_bindtextdomain(TEXTDOMAIN);
414 /* Required on win32 to prevent error loading init.tcl */
415 Tcl_FindExecutable("");
419 * Override the functions in the Notifier subsystem. See comments above.
421 notifier.setTimerProc = pltcl_SetTimer;
422 notifier.waitForEventProc = pltcl_WaitForEvent;
423 notifier.createFileHandlerProc = pltcl_CreateFileHandler;
424 notifier.deleteFileHandlerProc = pltcl_DeleteFileHandler;
425 notifier.initNotifierProc = pltcl_InitNotifier;
426 notifier.finalizeNotifierProc = pltcl_FinalizeNotifier;
427 notifier.alertNotifierProc = pltcl_AlertNotifier;
428 notifier.serviceModeHookProc = pltcl_ServiceModeHook;
429 Tcl_SetNotifier(¬ifier);
431 /************************************************************
432 * Create the dummy hold interpreter to prevent close of
433 * stdout and stderr on DeleteInterp
434 ************************************************************/
435 if ((pltcl_hold_interp = Tcl_CreateInterp()) == NULL)
436 elog(ERROR, "could not create master Tcl interpreter");
437 if (Tcl_Init(pltcl_hold_interp) == TCL_ERROR)
438 elog(ERROR, "could not initialize master Tcl interpreter");
440 /************************************************************
441 * Create the hash table for working interpreters
442 ************************************************************/
443 memset(&hash_ctl, 0, sizeof(hash_ctl));
444 hash_ctl.keysize = sizeof(Oid);
445 hash_ctl.entrysize = sizeof(pltcl_interp_desc);
446 pltcl_interp_htab = hash_create("PL/Tcl interpreters",
449 HASH_ELEM | HASH_BLOBS);
451 /************************************************************
452 * Create the hash table for function lookup
453 ************************************************************/
454 memset(&hash_ctl, 0, sizeof(hash_ctl));
455 hash_ctl.keysize = sizeof(pltcl_proc_key);
456 hash_ctl.entrysize = sizeof(pltcl_proc_ptr);
457 pltcl_proc_htab = hash_create("PL/Tcl functions",
460 HASH_ELEM | HASH_BLOBS);
462 /************************************************************
463 * Define PL/Tcl's custom GUCs
464 ************************************************************/
465 DefineCustomStringVariable("pltcl.start_proc",
466 gettext_noop("PL/Tcl function to call once when pltcl is first used."),
472 DefineCustomStringVariable("pltclu.start_proc",
473 gettext_noop("PL/TclU function to call once when pltclu is first used."),
480 pltcl_pm_init_done = true;
483 /**********************************************************************
484 * pltcl_init_interp() - initialize a new Tcl interpreter
485 **********************************************************************/
487 pltcl_init_interp(pltcl_interp_desc *interp_desc, Oid prolang, bool pltrusted)
492 /************************************************************
493 * Create the Tcl interpreter as a slave of pltcl_hold_interp.
494 * Note: Tcl automatically does Tcl_Init in the untrusted case,
495 * and it's not wanted in the trusted case.
496 ************************************************************/
497 snprintf(interpname, sizeof(interpname), "slave_%u", interp_desc->user_id);
498 if ((interp = Tcl_CreateSlave(pltcl_hold_interp, interpname,
499 pltrusted ? 1 : 0)) == NULL)
500 elog(ERROR, "could not create slave Tcl interpreter");
502 /************************************************************
503 * Initialize the query hash table associated with interpreter
504 ************************************************************/
505 Tcl_InitHashTable(&interp_desc->query_hash, TCL_STRING_KEYS);
507 /************************************************************
508 * Install the commands for SPI support in the interpreter
509 ************************************************************/
510 Tcl_CreateObjCommand(interp, "elog",
511 pltcl_elog, NULL, NULL);
512 Tcl_CreateObjCommand(interp, "quote",
513 pltcl_quote, NULL, NULL);
514 Tcl_CreateObjCommand(interp, "argisnull",
515 pltcl_argisnull, NULL, NULL);
516 Tcl_CreateObjCommand(interp, "return_null",
517 pltcl_returnnull, NULL, NULL);
518 Tcl_CreateObjCommand(interp, "return_next",
519 pltcl_returnnext, NULL, NULL);
520 Tcl_CreateObjCommand(interp, "spi_exec",
521 pltcl_SPI_execute, NULL, NULL);
522 Tcl_CreateObjCommand(interp, "spi_prepare",
523 pltcl_SPI_prepare, NULL, NULL);
524 Tcl_CreateObjCommand(interp, "spi_execp",
525 pltcl_SPI_execute_plan, NULL, NULL);
526 Tcl_CreateObjCommand(interp, "spi_lastoid",
527 pltcl_SPI_lastoid, NULL, NULL);
528 Tcl_CreateObjCommand(interp, "subtransaction",
529 pltcl_subtransaction, NULL, NULL);
530 Tcl_CreateObjCommand(interp, "commit",
531 pltcl_commit, NULL, NULL);
532 Tcl_CreateObjCommand(interp, "rollback",
533 pltcl_rollback, NULL, NULL);
535 /************************************************************
536 * Call the appropriate start_proc, if there is one.
538 * We must set interp_desc->interp before the call, else the start_proc
539 * won't find the interpreter it's supposed to use. But, if the
540 * start_proc fails, we want to abandon use of the interpreter.
541 ************************************************************/
544 interp_desc->interp = interp;
545 call_pltcl_start_proc(prolang, pltrusted);
549 interp_desc->interp = NULL;
550 Tcl_DeleteInterp(interp);
556 /**********************************************************************
557 * pltcl_fetch_interp() - fetch the Tcl interpreter to use for a function
559 * This also takes care of any on-first-use initialization required.
560 **********************************************************************/
561 static pltcl_interp_desc *
562 pltcl_fetch_interp(Oid prolang, bool pltrusted)
565 pltcl_interp_desc *interp_desc;
568 /* Find or create the interpreter hashtable entry for this userid */
570 user_id = GetUserId();
572 user_id = InvalidOid;
574 interp_desc = hash_search(pltcl_interp_htab, &user_id,
578 interp_desc->interp = NULL;
580 /* If we haven't yet successfully made an interpreter, try to do that */
581 if (!interp_desc->interp)
582 pltcl_init_interp(interp_desc, prolang, pltrusted);
588 /**********************************************************************
589 * call_pltcl_start_proc() - Call user-defined initialization proc, if any
590 **********************************************************************/
592 call_pltcl_start_proc(Oid prolang, bool pltrusted)
596 ErrorContextCallback errcallback;
598 Oid fargtypes[1]; /* dummy */
601 Form_pg_proc procStruct;
604 FunctionCallInfoData fcinfo;
605 PgStat_FunctionCallUsage fcusage;
607 /* select appropriate GUC */
608 start_proc = pltrusted ? pltcl_start_proc : pltclu_start_proc;
609 gucname = pltrusted ? "pltcl.start_proc" : "pltclu.start_proc";
611 /* Nothing to do if it's empty or unset */
612 if (start_proc == NULL || start_proc[0] == '\0')
615 /* Set up errcontext callback to make errors more helpful */
616 errcallback.callback = start_proc_error_callback;
617 errcallback.arg = (void *) gucname;
618 errcallback.previous = error_context_stack;
619 error_context_stack = &errcallback;
621 /* Parse possibly-qualified identifier and look up the function */
622 namelist = stringToQualifiedNameList(start_proc);
623 procOid = LookupFuncName(namelist, 0, fargtypes, false);
625 /* Current user must have permission to call function */
626 aclresult = pg_proc_aclcheck(procOid, GetUserId(), ACL_EXECUTE);
627 if (aclresult != ACLCHECK_OK)
628 aclcheck_error(aclresult, OBJECT_FUNCTION, start_proc);
630 /* Get the function's pg_proc entry */
631 procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(procOid));
632 if (!HeapTupleIsValid(procTup))
633 elog(ERROR, "cache lookup failed for function %u", procOid);
634 procStruct = (Form_pg_proc) GETSTRUCT(procTup);
636 /* It must be same language as the function we're currently calling */
637 if (procStruct->prolang != prolang)
639 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
640 errmsg("function \"%s\" is in the wrong language",
644 * It must not be SECURITY DEFINER, either. This together with the
645 * language match check ensures that the function will execute in the same
646 * Tcl interpreter we just finished initializing.
648 if (procStruct->prosecdef)
650 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
651 errmsg("function \"%s\" must not be SECURITY DEFINER",
655 ReleaseSysCache(procTup);
658 * Call the function using the normal SQL function call mechanism. We
659 * could perhaps cheat and jump directly to pltcl_handler(), but it seems
660 * better to do it this way so that the call is exposed to, eg, call
661 * statistics collection.
663 InvokeFunctionExecuteHook(procOid);
664 fmgr_info(procOid, &finfo);
665 InitFunctionCallInfoData(fcinfo, &finfo,
667 InvalidOid, NULL, NULL);
668 pgstat_init_function_usage(&fcinfo, &fcusage);
669 (void) FunctionCallInvoke(&fcinfo);
670 pgstat_end_function_usage(&fcusage, true);
672 /* Pop the error context stack */
673 error_context_stack = errcallback.previous;
677 * Error context callback for errors occurring during start_proc processing.
680 start_proc_error_callback(void *arg)
682 const char *gucname = (const char *) arg;
684 /* translator: %s is "pltcl.start_proc" or "pltclu.start_proc" */
685 errcontext("processing %s parameter", gucname);
689 /**********************************************************************
690 * pltcl_call_handler - This is the only visible function
691 * of the PL interpreter. The PostgreSQL
692 * function manager and trigger manager
693 * call this function for execution of
695 **********************************************************************/
696 PG_FUNCTION_INFO_V1(pltcl_call_handler);
698 /* keep non-static */
700 pltcl_call_handler(PG_FUNCTION_ARGS)
702 return pltcl_handler(fcinfo, true);
706 * Alternative handler for unsafe functions
708 PG_FUNCTION_INFO_V1(pltclu_call_handler);
710 /* keep non-static */
712 pltclu_call_handler(PG_FUNCTION_ARGS)
714 return pltcl_handler(fcinfo, false);
718 /**********************************************************************
719 * pltcl_handler() - Handler for function and trigger calls, for
720 * both trusted and untrusted interpreters.
721 **********************************************************************/
723 pltcl_handler(PG_FUNCTION_ARGS, bool pltrusted)
726 pltcl_call_state current_call_state;
727 pltcl_call_state *save_call_state;
730 * Initialize current_call_state to nulls/zeroes; in particular, set its
731 * prodesc pointer to null. Anything that sets it non-null should
732 * increase the prodesc's fn_refcount at the same time. We'll decrease
733 * the refcount, and then delete the prodesc if it's no longer referenced,
734 * on the way out of this function. This ensures that prodescs live as
735 * long as needed even if somebody replaces the originating pg_proc row
736 * while they're executing.
738 memset(¤t_call_state, 0, sizeof(current_call_state));
741 * Ensure that static pointer is saved/restored properly
743 save_call_state = pltcl_current_call_state;
744 pltcl_current_call_state = ¤t_call_state;
749 * Determine if called as function or trigger and call appropriate
752 if (CALLED_AS_TRIGGER(fcinfo))
754 /* invoke the trigger handler */
755 retval = PointerGetDatum(pltcl_trigger_handler(fcinfo,
759 else if (CALLED_AS_EVENT_TRIGGER(fcinfo))
761 /* invoke the event trigger handler */
762 pltcl_event_trigger_handler(fcinfo, ¤t_call_state, pltrusted);
767 /* invoke the regular function handler */
768 current_call_state.fcinfo = fcinfo;
769 retval = pltcl_func_handler(fcinfo, ¤t_call_state, pltrusted);
774 /* Restore static pointer, then clean up the prodesc refcount if any */
775 pltcl_current_call_state = save_call_state;
776 if (current_call_state.prodesc != NULL)
778 Assert(current_call_state.prodesc->fn_refcount > 0);
779 if (--current_call_state.prodesc->fn_refcount == 0)
780 MemoryContextDelete(current_call_state.prodesc->fn_cxt);
786 /* Restore static pointer, then clean up the prodesc refcount if any */
787 /* (We're being paranoid in case an error is thrown in context deletion) */
788 pltcl_current_call_state = save_call_state;
789 if (current_call_state.prodesc != NULL)
791 Assert(current_call_state.prodesc->fn_refcount > 0);
792 if (--current_call_state.prodesc->fn_refcount == 0)
793 MemoryContextDelete(current_call_state.prodesc->fn_cxt);
800 /**********************************************************************
801 * pltcl_func_handler() - Handler for regular function calls
802 **********************************************************************/
804 pltcl_func_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state,
808 pltcl_proc_desc *prodesc;
809 Tcl_Interp *volatile interp;
815 nonatomic = fcinfo->context &&
816 IsA(fcinfo->context, CallContext) &&
817 !castNode(CallContext, fcinfo->context)->atomic;
819 /* Connect to SPI manager */
820 if (SPI_connect_ext(nonatomic ? SPI_OPT_NONATOMIC : 0) != SPI_OK_CONNECT)
821 elog(ERROR, "could not connect to SPI manager");
823 /* Find or compile the function */
824 prodesc = compile_pltcl_function(fcinfo->flinfo->fn_oid, InvalidOid,
827 call_state->prodesc = prodesc;
828 prodesc->fn_refcount++;
830 interp = prodesc->interp_desc->interp;
833 * If we're a SRF, check caller can handle materialize mode, and save
834 * relevant info into call_state. We must ensure that the returned
835 * tuplestore is owned by the caller's context, even if we first create it
836 * inside a subtransaction.
838 if (prodesc->fn_retisset)
840 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
842 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
843 (rsi->allowedModes & SFRM_Materialize) == 0)
845 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
846 errmsg("set-valued function called in context that cannot accept a set")));
848 call_state->rsi = rsi;
849 call_state->tuple_store_cxt = rsi->econtext->ecxt_per_query_memory;
850 call_state->tuple_store_owner = CurrentResourceOwner;
853 /************************************************************
854 * Create the tcl command to call the internal
855 * proc in the Tcl interpreter
856 ************************************************************/
857 tcl_cmd = Tcl_NewObj();
858 Tcl_ListObjAppendElement(NULL, tcl_cmd,
859 Tcl_NewStringObj(prodesc->internal_proname, -1));
861 /* We hold a refcount on tcl_cmd just to be sure it stays around */
862 Tcl_IncrRefCount(tcl_cmd);
864 /************************************************************
865 * Add all call arguments to the command
866 ************************************************************/
869 for (i = 0; i < prodesc->nargs; i++)
871 if (prodesc->arg_is_rowtype[i])
873 /**************************************************
874 * For tuple values, add a list for 'array set ...'
875 **************************************************/
876 if (fcinfo->argnull[i])
877 Tcl_ListObjAppendElement(NULL, tcl_cmd, Tcl_NewObj());
884 HeapTupleData tmptup;
887 td = DatumGetHeapTupleHeader(fcinfo->arg[i]);
888 /* Extract rowtype info and find a tupdesc */
889 tupType = HeapTupleHeaderGetTypeId(td);
890 tupTypmod = HeapTupleHeaderGetTypMod(td);
891 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
892 /* Build a temporary HeapTuple control structure */
893 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
896 list_tmp = pltcl_build_tuple_argument(&tmptup, tupdesc);
897 Tcl_ListObjAppendElement(NULL, tcl_cmd, list_tmp);
899 ReleaseTupleDesc(tupdesc);
904 /**************************************************
905 * Single values are added as string element
906 * of their external representation
907 **************************************************/
908 if (fcinfo->argnull[i])
909 Tcl_ListObjAppendElement(NULL, tcl_cmd, Tcl_NewObj());
914 tmp = OutputFunctionCall(&prodesc->arg_out_func[i],
917 Tcl_ListObjAppendElement(NULL, tcl_cmd,
918 Tcl_NewStringObj(UTF_E2U(tmp), -1));
927 /* Release refcount to free tcl_cmd */
928 Tcl_DecrRefCount(tcl_cmd);
933 /************************************************************
934 * Call the Tcl function
936 * We assume no PG error can be thrown directly from this call.
937 ************************************************************/
938 tcl_rc = Tcl_EvalObjEx(interp, tcl_cmd, (TCL_EVAL_DIRECT | TCL_EVAL_GLOBAL));
940 /* Release refcount to free tcl_cmd (and all subsidiary objects) */
941 Tcl_DecrRefCount(tcl_cmd);
943 /************************************************************
944 * Check for errors reported by Tcl.
945 ************************************************************/
946 if (tcl_rc != TCL_OK)
947 throw_tcl_error(interp, prodesc->user_proname);
949 /************************************************************
950 * Disconnect from SPI manager and then create the return
951 * value datum (if the input function does a palloc for it
952 * this must not be allocated in the SPI memory context
953 * because SPI_finish would free it). But don't try to call
954 * the result_in_func if we've been told to return a NULL;
955 * the Tcl result may not be a valid value of the result type
957 ************************************************************/
958 if (SPI_finish() != SPI_OK_FINISH)
959 elog(ERROR, "SPI_finish() failed");
961 if (prodesc->fn_retisset)
963 ReturnSetInfo *rsi = call_state->rsi;
965 /* We already checked this is OK */
966 rsi->returnMode = SFRM_Materialize;
968 /* If we produced any tuples, send back the result */
969 if (call_state->tuple_store)
971 rsi->setResult = call_state->tuple_store;
972 if (call_state->ret_tupdesc)
974 MemoryContext oldcxt;
976 oldcxt = MemoryContextSwitchTo(call_state->tuple_store_cxt);
977 rsi->setDesc = CreateTupleDescCopy(call_state->ret_tupdesc);
978 MemoryContextSwitchTo(oldcxt);
982 fcinfo->isnull = true;
984 else if (fcinfo->isnull)
986 retval = InputFunctionCall(&prodesc->result_in_func,
988 prodesc->result_typioparam,
991 else if (prodesc->fn_retistuple)
996 Tcl_Obj **resultObjv;
1000 * Set up data about result type. XXX it's tempting to consider
1001 * caching this in the prodesc, in the common case where the rowtype
1002 * is determined by the function not the calling query. But we'd have
1003 * to be able to deal with ADD/DROP/ALTER COLUMN events when the
1004 * result type is a named composite type, so it's not exactly trivial.
1005 * Maybe worth improving someday.
1007 switch (get_call_result_type(fcinfo, NULL, &td))
1009 case TYPEFUNC_COMPOSITE:
1012 case TYPEFUNC_COMPOSITE_DOMAIN:
1013 Assert(prodesc->fn_retisdomain);
1015 case TYPEFUNC_RECORD:
1016 /* failed to determine actual type of RECORD */
1018 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1019 errmsg("function returning record called in context "
1020 "that cannot accept type record")));
1023 /* result type isn't composite? */
1024 elog(ERROR, "return type must be a row type");
1028 Assert(!call_state->ret_tupdesc);
1029 Assert(!call_state->attinmeta);
1030 call_state->ret_tupdesc = td;
1031 call_state->attinmeta = TupleDescGetAttInMetadata(td);
1033 /* Convert function result to tuple */
1034 resultObj = Tcl_GetObjResult(interp);
1035 if (Tcl_ListObjGetElements(interp, resultObj, &resultObjc, &resultObjv) == TCL_ERROR)
1036 throw_tcl_error(interp, prodesc->user_proname);
1038 tup = pltcl_build_tuple_result(interp, resultObjv, resultObjc,
1040 retval = HeapTupleGetDatum(tup);
1043 retval = InputFunctionCall(&prodesc->result_in_func,
1044 utf_u2e(Tcl_GetStringResult(interp)),
1045 prodesc->result_typioparam,
1052 /**********************************************************************
1053 * pltcl_trigger_handler() - Handler for trigger calls
1054 **********************************************************************/
1056 pltcl_trigger_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state,
1059 pltcl_proc_desc *prodesc;
1060 Tcl_Interp *volatile interp;
1061 TriggerData *trigdata = (TriggerData *) fcinfo->context;
1064 volatile HeapTuple rettup;
1066 Tcl_Obj *tcl_trigtup;
1067 Tcl_Obj *tcl_newtup;
1072 Tcl_Obj **result_Objv;
1073 int rc PG_USED_FOR_ASSERTS_ONLY;
1075 call_state->trigdata = trigdata;
1077 /* Connect to SPI manager */
1078 if (SPI_connect() != SPI_OK_CONNECT)
1079 elog(ERROR, "could not connect to SPI manager");
1081 /* Make transition tables visible to this SPI connection */
1082 rc = SPI_register_trigger_data(trigdata);
1085 /* Find or compile the function */
1086 prodesc = compile_pltcl_function(fcinfo->flinfo->fn_oid,
1087 RelationGetRelid(trigdata->tg_relation),
1088 false, /* not an event trigger */
1091 call_state->prodesc = prodesc;
1092 prodesc->fn_refcount++;
1094 interp = prodesc->interp_desc->interp;
1096 tupdesc = RelationGetDescr(trigdata->tg_relation);
1098 /************************************************************
1099 * Create the tcl command to call the internal
1100 * proc in the interpreter
1101 ************************************************************/
1102 tcl_cmd = Tcl_NewObj();
1103 Tcl_IncrRefCount(tcl_cmd);
1107 /* The procedure name (note this is all ASCII, so no utf_e2u) */
1108 Tcl_ListObjAppendElement(NULL, tcl_cmd,
1109 Tcl_NewStringObj(prodesc->internal_proname, -1));
1111 /* The trigger name for argument TG_name */
1112 Tcl_ListObjAppendElement(NULL, tcl_cmd,
1113 Tcl_NewStringObj(utf_e2u(trigdata->tg_trigger->tgname), -1));
1115 /* The oid of the trigger relation for argument TG_relid */
1116 /* Consider not converting to a string for more performance? */
1117 stroid = DatumGetCString(DirectFunctionCall1(oidout,
1118 ObjectIdGetDatum(trigdata->tg_relation->rd_id)));
1119 Tcl_ListObjAppendElement(NULL, tcl_cmd,
1120 Tcl_NewStringObj(stroid, -1));
1123 /* The name of the table the trigger is acting on: TG_table_name */
1124 stroid = SPI_getrelname(trigdata->tg_relation);
1125 Tcl_ListObjAppendElement(NULL, tcl_cmd,
1126 Tcl_NewStringObj(utf_e2u(stroid), -1));
1129 /* The schema of the table the trigger is acting on: TG_table_schema */
1130 stroid = SPI_getnspname(trigdata->tg_relation);
1131 Tcl_ListObjAppendElement(NULL, tcl_cmd,
1132 Tcl_NewStringObj(utf_e2u(stroid), -1));
1135 /* A list of attribute names for argument TG_relatts */
1136 tcl_trigtup = Tcl_NewObj();
1137 Tcl_ListObjAppendElement(NULL, tcl_trigtup, Tcl_NewObj());
1138 for (i = 0; i < tupdesc->natts; i++)
1140 Form_pg_attribute att = TupleDescAttr(tupdesc, i);
1142 if (att->attisdropped)
1143 Tcl_ListObjAppendElement(NULL, tcl_trigtup, Tcl_NewObj());
1145 Tcl_ListObjAppendElement(NULL, tcl_trigtup,
1146 Tcl_NewStringObj(utf_e2u(NameStr(att->attname)), -1));
1148 Tcl_ListObjAppendElement(NULL, tcl_cmd, tcl_trigtup);
1150 /* The when part of the event for TG_when */
1151 if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
1152 Tcl_ListObjAppendElement(NULL, tcl_cmd,
1153 Tcl_NewStringObj("BEFORE", -1));
1154 else if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
1155 Tcl_ListObjAppendElement(NULL, tcl_cmd,
1156 Tcl_NewStringObj("AFTER", -1));
1157 else if (TRIGGER_FIRED_INSTEAD(trigdata->tg_event))
1158 Tcl_ListObjAppendElement(NULL, tcl_cmd,
1159 Tcl_NewStringObj("INSTEAD OF", -1));
1161 elog(ERROR, "unrecognized WHEN tg_event: %u", trigdata->tg_event);
1163 /* The level part of the event for TG_level */
1164 if (TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
1166 Tcl_ListObjAppendElement(NULL, tcl_cmd,
1167 Tcl_NewStringObj("ROW", -1));
1169 /* Build the data list for the trigtuple */
1170 tcl_trigtup = pltcl_build_tuple_argument(trigdata->tg_trigtuple,
1174 * Now the command part of the event for TG_op and data for NEW
1177 if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
1179 Tcl_ListObjAppendElement(NULL, tcl_cmd,
1180 Tcl_NewStringObj("INSERT", -1));
1182 Tcl_ListObjAppendElement(NULL, tcl_cmd, tcl_trigtup);
1183 Tcl_ListObjAppendElement(NULL, tcl_cmd, Tcl_NewObj());
1185 rettup = trigdata->tg_trigtuple;
1187 else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
1189 Tcl_ListObjAppendElement(NULL, tcl_cmd,
1190 Tcl_NewStringObj("DELETE", -1));
1192 Tcl_ListObjAppendElement(NULL, tcl_cmd, Tcl_NewObj());
1193 Tcl_ListObjAppendElement(NULL, tcl_cmd, tcl_trigtup);
1195 rettup = trigdata->tg_trigtuple;
1197 else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
1199 Tcl_ListObjAppendElement(NULL, tcl_cmd,
1200 Tcl_NewStringObj("UPDATE", -1));
1202 tcl_newtup = pltcl_build_tuple_argument(trigdata->tg_newtuple,
1205 Tcl_ListObjAppendElement(NULL, tcl_cmd, tcl_newtup);
1206 Tcl_ListObjAppendElement(NULL, tcl_cmd, tcl_trigtup);
1208 rettup = trigdata->tg_newtuple;
1211 elog(ERROR, "unrecognized OP tg_event: %u", trigdata->tg_event);
1213 else if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
1215 Tcl_ListObjAppendElement(NULL, tcl_cmd,
1216 Tcl_NewStringObj("STATEMENT", -1));
1218 if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
1219 Tcl_ListObjAppendElement(NULL, tcl_cmd,
1220 Tcl_NewStringObj("INSERT", -1));
1221 else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
1222 Tcl_ListObjAppendElement(NULL, tcl_cmd,
1223 Tcl_NewStringObj("DELETE", -1));
1224 else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
1225 Tcl_ListObjAppendElement(NULL, tcl_cmd,
1226 Tcl_NewStringObj("UPDATE", -1));
1227 else if (TRIGGER_FIRED_BY_TRUNCATE(trigdata->tg_event))
1228 Tcl_ListObjAppendElement(NULL, tcl_cmd,
1229 Tcl_NewStringObj("TRUNCATE", -1));
1231 elog(ERROR, "unrecognized OP tg_event: %u", trigdata->tg_event);
1233 Tcl_ListObjAppendElement(NULL, tcl_cmd, Tcl_NewObj());
1234 Tcl_ListObjAppendElement(NULL, tcl_cmd, Tcl_NewObj());
1236 rettup = (HeapTuple) NULL;
1239 elog(ERROR, "unrecognized LEVEL tg_event: %u", trigdata->tg_event);
1241 /* Finally append the arguments from CREATE TRIGGER */
1242 for (i = 0; i < trigdata->tg_trigger->tgnargs; i++)
1243 Tcl_ListObjAppendElement(NULL, tcl_cmd,
1244 Tcl_NewStringObj(utf_e2u(trigdata->tg_trigger->tgargs[i]), -1));
1249 Tcl_DecrRefCount(tcl_cmd);
1254 /************************************************************
1255 * Call the Tcl function
1257 * We assume no PG error can be thrown directly from this call.
1258 ************************************************************/
1259 tcl_rc = Tcl_EvalObjEx(interp, tcl_cmd, (TCL_EVAL_DIRECT | TCL_EVAL_GLOBAL));
1261 /* Release refcount to free tcl_cmd (and all subsidiary objects) */
1262 Tcl_DecrRefCount(tcl_cmd);
1264 /************************************************************
1265 * Check for errors reported by Tcl.
1266 ************************************************************/
1267 if (tcl_rc != TCL_OK)
1268 throw_tcl_error(interp, prodesc->user_proname);
1270 /************************************************************
1271 * Exit SPI environment.
1272 ************************************************************/
1273 if (SPI_finish() != SPI_OK_FINISH)
1274 elog(ERROR, "SPI_finish() failed");
1276 /************************************************************
1277 * The return value from the procedure might be one of
1278 * the magic strings OK or SKIP, or a list from array get.
1279 * We can check for OK or SKIP without worrying about encoding.
1280 ************************************************************/
1281 result = Tcl_GetStringResult(interp);
1283 if (strcmp(result, "OK") == 0)
1285 if (strcmp(result, "SKIP") == 0)
1286 return (HeapTuple) NULL;
1288 /************************************************************
1289 * Otherwise, the return value should be a column name/value list
1290 * specifying the modified tuple to return.
1291 ************************************************************/
1292 if (Tcl_ListObjGetElements(interp, Tcl_GetObjResult(interp),
1293 &result_Objc, &result_Objv) != TCL_OK)
1295 (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
1296 errmsg("could not split return value from trigger: %s",
1297 utf_u2e(Tcl_GetStringResult(interp)))));
1299 /* Convert function result to tuple */
1300 rettup = pltcl_build_tuple_result(interp, result_Objv, result_Objc,
1306 /**********************************************************************
1307 * pltcl_event_trigger_handler() - Handler for event trigger calls
1308 **********************************************************************/
1310 pltcl_event_trigger_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state,
1313 pltcl_proc_desc *prodesc;
1314 Tcl_Interp *volatile interp;
1315 EventTriggerData *tdata = (EventTriggerData *) fcinfo->context;
1319 /* Connect to SPI manager */
1320 if (SPI_connect() != SPI_OK_CONNECT)
1321 elog(ERROR, "could not connect to SPI manager");
1323 /* Find or compile the function */
1324 prodesc = compile_pltcl_function(fcinfo->flinfo->fn_oid,
1325 InvalidOid, true, pltrusted);
1327 call_state->prodesc = prodesc;
1328 prodesc->fn_refcount++;
1330 interp = prodesc->interp_desc->interp;
1332 /* Create the tcl command and call the internal proc */
1333 tcl_cmd = Tcl_NewObj();
1334 Tcl_IncrRefCount(tcl_cmd);
1335 Tcl_ListObjAppendElement(NULL, tcl_cmd,
1336 Tcl_NewStringObj(prodesc->internal_proname, -1));
1337 Tcl_ListObjAppendElement(NULL, tcl_cmd,
1338 Tcl_NewStringObj(utf_e2u(tdata->event), -1));
1339 Tcl_ListObjAppendElement(NULL, tcl_cmd,
1340 Tcl_NewStringObj(utf_e2u(tdata->tag), -1));
1342 tcl_rc = Tcl_EvalObjEx(interp, tcl_cmd, (TCL_EVAL_DIRECT | TCL_EVAL_GLOBAL));
1344 /* Release refcount to free tcl_cmd (and all subsidiary objects) */
1345 Tcl_DecrRefCount(tcl_cmd);
1347 /* Check for errors reported by Tcl. */
1348 if (tcl_rc != TCL_OK)
1349 throw_tcl_error(interp, prodesc->user_proname);
1351 if (SPI_finish() != SPI_OK_FINISH)
1352 elog(ERROR, "SPI_finish() failed");
1356 /**********************************************************************
1357 * throw_tcl_error - ereport an error returned from the Tcl interpreter
1358 **********************************************************************/
1360 throw_tcl_error(Tcl_Interp *interp, const char *proname)
1363 * Caution is needed here because Tcl_GetVar could overwrite the
1364 * interpreter result (even though it's not really supposed to), and we
1365 * can't control the order of evaluation of ereport arguments. Hence, make
1366 * real sure we have our own copy of the result string before invoking
1372 emsg = pstrdup(utf_u2e(Tcl_GetStringResult(interp)));
1373 econtext = utf_u2e(Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY));
1375 (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
1377 errcontext("%s\nin PL/Tcl function \"%s\"",
1378 econtext, proname)));
1382 /**********************************************************************
1383 * compile_pltcl_function - compile (or hopefully just look up) function
1385 * tgreloid is the OID of the relation when compiling a trigger, or zero
1386 * (InvalidOid) when compiling a plain function.
1387 **********************************************************************/
1388 static pltcl_proc_desc *
1389 compile_pltcl_function(Oid fn_oid, Oid tgreloid,
1390 bool is_event_trigger, bool pltrusted)
1393 Form_pg_proc procStruct;
1394 pltcl_proc_key proc_key;
1395 pltcl_proc_ptr *proc_ptr;
1397 pltcl_proc_desc *prodesc;
1398 pltcl_proc_desc *old_prodesc;
1399 volatile MemoryContext proc_cxt = NULL;
1400 Tcl_DString proc_internal_def;
1401 Tcl_DString proc_internal_body;
1403 /* We'll need the pg_proc tuple in any case... */
1404 procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(fn_oid));
1405 if (!HeapTupleIsValid(procTup))
1406 elog(ERROR, "cache lookup failed for function %u", fn_oid);
1407 procStruct = (Form_pg_proc) GETSTRUCT(procTup);
1410 * Look up function in pltcl_proc_htab; if it's not there, create an entry
1411 * and set the entry's proc_ptr to NULL.
1413 proc_key.proc_id = fn_oid;
1414 proc_key.is_trigger = OidIsValid(tgreloid);
1415 proc_key.user_id = pltrusted ? GetUserId() : InvalidOid;
1417 proc_ptr = hash_search(pltcl_proc_htab, &proc_key,
1421 proc_ptr->proc_ptr = NULL;
1423 prodesc = proc_ptr->proc_ptr;
1425 /************************************************************
1426 * If it's present, must check whether it's still up to date.
1427 * This is needed because CREATE OR REPLACE FUNCTION can modify the
1428 * function's pg_proc entry without changing its OID.
1429 ************************************************************/
1430 if (prodesc != NULL &&
1431 prodesc->fn_xmin == HeapTupleHeaderGetRawXmin(procTup->t_data) &&
1432 ItemPointerEquals(&prodesc->fn_tid, &procTup->t_self))
1434 /* It's still up-to-date, so we can use it */
1435 ReleaseSysCache(procTup);
1439 /************************************************************
1440 * If we haven't found it in the hashtable, we analyze
1441 * the functions arguments and returntype and store
1442 * the in-/out-functions in the prodesc block and create
1443 * a new hashtable entry for it.
1445 * Then we load the procedure into the Tcl interpreter.
1446 ************************************************************/
1447 Tcl_DStringInit(&proc_internal_def);
1448 Tcl_DStringInit(&proc_internal_body);
1451 bool is_trigger = OidIsValid(tgreloid);
1452 char internal_proname[128];
1454 Form_pg_type typeStruct;
1455 char proc_internal_args[33 * FUNC_MAX_ARGS];
1463 MemoryContext oldcontext;
1465 /************************************************************
1466 * Build our internal proc name from the function's Oid. Append
1467 * "_trigger" when appropriate to ensure the normal and trigger
1468 * cases are kept separate. Note name must be all-ASCII.
1469 ************************************************************/
1470 if (is_event_trigger)
1471 snprintf(internal_proname, sizeof(internal_proname),
1472 "__PLTcl_proc_%u_evttrigger", fn_oid);
1473 else if (is_trigger)
1474 snprintf(internal_proname, sizeof(internal_proname),
1475 "__PLTcl_proc_%u_trigger", fn_oid);
1477 snprintf(internal_proname, sizeof(internal_proname),
1478 "__PLTcl_proc_%u", fn_oid);
1480 /************************************************************
1481 * Allocate a context that will hold all PG data for the procedure.
1482 ************************************************************/
1483 proc_cxt = AllocSetContextCreate(TopMemoryContext,
1485 ALLOCSET_SMALL_SIZES);
1487 /************************************************************
1488 * Allocate and fill a new procedure description block.
1489 * struct prodesc and subsidiary data must all live in proc_cxt.
1490 ************************************************************/
1491 oldcontext = MemoryContextSwitchTo(proc_cxt);
1492 prodesc = (pltcl_proc_desc *) palloc0(sizeof(pltcl_proc_desc));
1493 prodesc->user_proname = pstrdup(NameStr(procStruct->proname));
1494 MemoryContextSetIdentifier(proc_cxt, prodesc->user_proname);
1495 prodesc->internal_proname = pstrdup(internal_proname);
1496 prodesc->fn_cxt = proc_cxt;
1497 prodesc->fn_refcount = 0;
1498 prodesc->fn_xmin = HeapTupleHeaderGetRawXmin(procTup->t_data);
1499 prodesc->fn_tid = procTup->t_self;
1500 prodesc->nargs = procStruct->pronargs;
1501 prodesc->arg_out_func = (FmgrInfo *) palloc0(prodesc->nargs * sizeof(FmgrInfo));
1502 prodesc->arg_is_rowtype = (bool *) palloc0(prodesc->nargs * sizeof(bool));
1503 MemoryContextSwitchTo(oldcontext);
1505 /* Remember if function is STABLE/IMMUTABLE */
1506 prodesc->fn_readonly =
1507 (procStruct->provolatile != PROVOLATILE_VOLATILE);
1508 /* And whether it is trusted */
1509 prodesc->lanpltrusted = pltrusted;
1511 /************************************************************
1512 * Identify the interpreter to use for the function
1513 ************************************************************/
1514 prodesc->interp_desc = pltcl_fetch_interp(procStruct->prolang,
1515 prodesc->lanpltrusted);
1516 interp = prodesc->interp_desc->interp;
1518 /************************************************************
1519 * Get the required information for input conversion of the
1521 ************************************************************/
1522 if (!is_trigger && !is_event_trigger)
1524 Oid rettype = procStruct->prorettype;
1526 typeTup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(rettype));
1527 if (!HeapTupleIsValid(typeTup))
1528 elog(ERROR, "cache lookup failed for type %u", rettype);
1529 typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1531 /* Disallow pseudotype result, except VOID and RECORD */
1532 if (typeStruct->typtype == TYPTYPE_PSEUDO)
1534 if (rettype == VOIDOID ||
1535 rettype == RECORDOID)
1537 else if (rettype == TRIGGEROID ||
1538 rettype == EVTTRIGGEROID)
1540 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1541 errmsg("trigger functions can only be called as triggers")));
1544 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1545 errmsg("PL/Tcl functions cannot return type %s",
1546 format_type_be(rettype))));
1549 prodesc->result_typid = rettype;
1550 fmgr_info_cxt(typeStruct->typinput,
1551 &(prodesc->result_in_func),
1553 prodesc->result_typioparam = getTypeIOParam(typeTup);
1555 prodesc->fn_retisset = procStruct->proretset;
1556 prodesc->fn_retistuple = type_is_rowtype(rettype);
1557 prodesc->fn_retisdomain = (typeStruct->typtype == TYPTYPE_DOMAIN);
1558 prodesc->domain_info = NULL;
1560 ReleaseSysCache(typeTup);
1563 /************************************************************
1564 * Get the required information for output conversion
1565 * of all procedure arguments, and set up argument naming info.
1566 ************************************************************/
1567 if (!is_trigger && !is_event_trigger)
1569 proc_internal_args[0] = '\0';
1570 for (i = 0; i < prodesc->nargs; i++)
1572 Oid argtype = procStruct->proargtypes.values[i];
1574 typeTup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(argtype));
1575 if (!HeapTupleIsValid(typeTup))
1576 elog(ERROR, "cache lookup failed for type %u", argtype);
1577 typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1579 /* Disallow pseudotype argument, except RECORD */
1580 if (typeStruct->typtype == TYPTYPE_PSEUDO &&
1581 argtype != RECORDOID)
1583 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1584 errmsg("PL/Tcl functions cannot accept type %s",
1585 format_type_be(argtype))));
1587 if (type_is_rowtype(argtype))
1589 prodesc->arg_is_rowtype[i] = true;
1590 snprintf(buf, sizeof(buf), "__PLTcl_Tup_%d", i + 1);
1594 prodesc->arg_is_rowtype[i] = false;
1595 fmgr_info_cxt(typeStruct->typoutput,
1596 &(prodesc->arg_out_func[i]),
1598 snprintf(buf, sizeof(buf), "%d", i + 1);
1602 strcat(proc_internal_args, " ");
1603 strcat(proc_internal_args, buf);
1605 ReleaseSysCache(typeTup);
1608 else if (is_trigger)
1610 /* trigger procedure has fixed args */
1611 strcpy(proc_internal_args,
1612 "TG_name TG_relid TG_table_name TG_table_schema TG_relatts TG_when TG_level TG_op __PLTcl_Tup_NEW __PLTcl_Tup_OLD args");
1614 else if (is_event_trigger)
1616 /* event trigger procedure has fixed args */
1617 strcpy(proc_internal_args, "TG_event TG_tag");
1620 /************************************************************
1621 * Create the tcl command to define the internal
1624 * Leave this code as DString - performance is not critical here,
1625 * and we don't want to duplicate the knowledge of the Tcl quoting
1626 * rules that's embedded in Tcl_DStringAppendElement.
1627 ************************************************************/
1628 Tcl_DStringAppendElement(&proc_internal_def, "proc");
1629 Tcl_DStringAppendElement(&proc_internal_def, internal_proname);
1630 Tcl_DStringAppendElement(&proc_internal_def, proc_internal_args);
1632 /************************************************************
1633 * prefix procedure body with
1634 * upvar #0 <internal_procname> GD
1635 * and with appropriate setting of arguments
1636 ************************************************************/
1637 Tcl_DStringAppend(&proc_internal_body, "upvar #0 ", -1);
1638 Tcl_DStringAppend(&proc_internal_body, internal_proname, -1);
1639 Tcl_DStringAppend(&proc_internal_body, " GD\n", -1);
1642 Tcl_DStringAppend(&proc_internal_body,
1643 "array set NEW $__PLTcl_Tup_NEW\n", -1);
1644 Tcl_DStringAppend(&proc_internal_body,
1645 "array set OLD $__PLTcl_Tup_OLD\n", -1);
1646 Tcl_DStringAppend(&proc_internal_body,
1649 "foreach v $args {\n"
1653 "unset i v\n\n", -1);
1655 else if (is_event_trigger)
1657 /* no argument support for event triggers */
1661 for (i = 0; i < prodesc->nargs; i++)
1663 if (prodesc->arg_is_rowtype[i])
1665 snprintf(buf, sizeof(buf),
1666 "array set %d $__PLTcl_Tup_%d\n",
1668 Tcl_DStringAppend(&proc_internal_body, buf, -1);
1673 /************************************************************
1674 * Add user's function definition to proc body
1675 ************************************************************/
1676 prosrcdatum = SysCacheGetAttr(PROCOID, procTup,
1677 Anum_pg_proc_prosrc, &isnull);
1679 elog(ERROR, "null prosrc");
1680 proc_source = TextDatumGetCString(prosrcdatum);
1682 Tcl_DStringAppend(&proc_internal_body, UTF_E2U(proc_source), -1);
1685 Tcl_DStringAppendElement(&proc_internal_def,
1686 Tcl_DStringValue(&proc_internal_body));
1688 /************************************************************
1689 * Create the procedure in the interpreter
1690 ************************************************************/
1691 tcl_rc = Tcl_EvalEx(interp,
1692 Tcl_DStringValue(&proc_internal_def),
1693 Tcl_DStringLength(&proc_internal_def),
1695 if (tcl_rc != TCL_OK)
1697 (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
1698 errmsg("could not create internal procedure \"%s\": %s",
1700 utf_u2e(Tcl_GetStringResult(interp)))));
1705 * If we failed anywhere above, clean up whatever got allocated. It
1706 * should all be in the proc_cxt, except for the DStrings.
1709 MemoryContextDelete(proc_cxt);
1710 Tcl_DStringFree(&proc_internal_def);
1711 Tcl_DStringFree(&proc_internal_body);
1717 * Install the new proc description block in the hashtable, incrementing
1718 * its refcount (the hashtable link counts as a reference). Then, if
1719 * there was a previous definition of the function, decrement that one's
1720 * refcount, and delete it if no longer referenced. The order of
1721 * operations here is important: if something goes wrong during the
1722 * MemoryContextDelete, leaking some memory for the old definition is OK,
1723 * but we don't want to corrupt the live hashtable entry. (Likewise,
1724 * freeing the DStrings is pretty low priority if that happens.)
1726 old_prodesc = proc_ptr->proc_ptr;
1728 proc_ptr->proc_ptr = prodesc;
1729 prodesc->fn_refcount++;
1731 if (old_prodesc != NULL)
1733 Assert(old_prodesc->fn_refcount > 0);
1734 if (--old_prodesc->fn_refcount == 0)
1735 MemoryContextDelete(old_prodesc->fn_cxt);
1738 Tcl_DStringFree(&proc_internal_def);
1739 Tcl_DStringFree(&proc_internal_body);
1741 ReleaseSysCache(procTup);
1747 /**********************************************************************
1748 * pltcl_elog() - elog() support for PLTcl
1749 **********************************************************************/
1751 pltcl_elog(ClientData cdata, Tcl_Interp *interp,
1752 int objc, Tcl_Obj *const objv[])
1755 MemoryContext oldcontext;
1758 static const char *logpriorities[] = {
1759 "DEBUG", "LOG", "INFO", "NOTICE",
1760 "WARNING", "ERROR", "FATAL", (const char *) NULL
1763 static const int loglevels[] = {
1764 DEBUG2, LOG, INFO, NOTICE,
1765 WARNING, ERROR, FATAL
1770 Tcl_WrongNumArgs(interp, 1, objv, "level msg");
1774 if (Tcl_GetIndexFromObj(interp, objv[1], logpriorities, "priority",
1775 TCL_EXACT, &priIndex) != TCL_OK)
1778 level = loglevels[priIndex];
1783 * We just pass the error back to Tcl. If it's not caught, it'll
1784 * eventually get converted to a PG error when we reach the call
1787 Tcl_SetObjResult(interp, objv[2]);
1792 * For non-error messages, just pass 'em to ereport(). We do not expect
1793 * that this will fail, but just on the off chance it does, report the
1794 * error back to Tcl. Note we are assuming that ereport() can't have any
1795 * internal failures that are so bad as to require a transaction abort.
1797 * This path is also used for FATAL errors, which aren't going to come
1798 * back to us at all.
1800 oldcontext = CurrentMemoryContext;
1805 (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
1806 errmsg("%s", UTF_U2E(Tcl_GetString(objv[2])))));
1813 /* Must reset elog.c's state */
1814 MemoryContextSwitchTo(oldcontext);
1815 edata = CopyErrorData();
1818 /* Pass the error data to Tcl */
1819 pltcl_construct_errorCode(interp, edata);
1821 Tcl_SetObjResult(interp, Tcl_NewStringObj(UTF_E2U(edata->message), -1));
1823 FreeErrorData(edata);
1833 /**********************************************************************
1834 * pltcl_construct_errorCode() - construct a Tcl errorCode
1835 * list with detailed information from the PostgreSQL server
1836 **********************************************************************/
1838 pltcl_construct_errorCode(Tcl_Interp *interp, ErrorData *edata)
1840 Tcl_Obj *obj = Tcl_NewObj();
1842 Tcl_ListObjAppendElement(interp, obj,
1843 Tcl_NewStringObj("POSTGRES", -1));
1844 Tcl_ListObjAppendElement(interp, obj,
1845 Tcl_NewStringObj(PG_VERSION, -1));
1846 Tcl_ListObjAppendElement(interp, obj,
1847 Tcl_NewStringObj("SQLSTATE", -1));
1848 Tcl_ListObjAppendElement(interp, obj,
1849 Tcl_NewStringObj(unpack_sql_state(edata->sqlerrcode), -1));
1850 Tcl_ListObjAppendElement(interp, obj,
1851 Tcl_NewStringObj("condition", -1));
1852 Tcl_ListObjAppendElement(interp, obj,
1853 Tcl_NewStringObj(pltcl_get_condition_name(edata->sqlerrcode), -1));
1854 Tcl_ListObjAppendElement(interp, obj,
1855 Tcl_NewStringObj("message", -1));
1857 Tcl_ListObjAppendElement(interp, obj,
1858 Tcl_NewStringObj(UTF_E2U(edata->message), -1));
1862 Tcl_ListObjAppendElement(interp, obj,
1863 Tcl_NewStringObj("detail", -1));
1865 Tcl_ListObjAppendElement(interp, obj,
1866 Tcl_NewStringObj(UTF_E2U(edata->detail), -1));
1871 Tcl_ListObjAppendElement(interp, obj,
1872 Tcl_NewStringObj("hint", -1));
1874 Tcl_ListObjAppendElement(interp, obj,
1875 Tcl_NewStringObj(UTF_E2U(edata->hint), -1));
1880 Tcl_ListObjAppendElement(interp, obj,
1881 Tcl_NewStringObj("context", -1));
1883 Tcl_ListObjAppendElement(interp, obj,
1884 Tcl_NewStringObj(UTF_E2U(edata->context), -1));
1887 if (edata->schema_name)
1889 Tcl_ListObjAppendElement(interp, obj,
1890 Tcl_NewStringObj("schema", -1));
1892 Tcl_ListObjAppendElement(interp, obj,
1893 Tcl_NewStringObj(UTF_E2U(edata->schema_name), -1));
1896 if (edata->table_name)
1898 Tcl_ListObjAppendElement(interp, obj,
1899 Tcl_NewStringObj("table", -1));
1901 Tcl_ListObjAppendElement(interp, obj,
1902 Tcl_NewStringObj(UTF_E2U(edata->table_name), -1));
1905 if (edata->column_name)
1907 Tcl_ListObjAppendElement(interp, obj,
1908 Tcl_NewStringObj("column", -1));
1910 Tcl_ListObjAppendElement(interp, obj,
1911 Tcl_NewStringObj(UTF_E2U(edata->column_name), -1));
1914 if (edata->datatype_name)
1916 Tcl_ListObjAppendElement(interp, obj,
1917 Tcl_NewStringObj("datatype", -1));
1919 Tcl_ListObjAppendElement(interp, obj,
1920 Tcl_NewStringObj(UTF_E2U(edata->datatype_name), -1));
1923 if (edata->constraint_name)
1925 Tcl_ListObjAppendElement(interp, obj,
1926 Tcl_NewStringObj("constraint", -1));
1928 Tcl_ListObjAppendElement(interp, obj,
1929 Tcl_NewStringObj(UTF_E2U(edata->constraint_name), -1));
1932 /* cursorpos is never interesting here; report internal query/pos */
1933 if (edata->internalquery)
1935 Tcl_ListObjAppendElement(interp, obj,
1936 Tcl_NewStringObj("statement", -1));
1938 Tcl_ListObjAppendElement(interp, obj,
1939 Tcl_NewStringObj(UTF_E2U(edata->internalquery), -1));
1942 if (edata->internalpos > 0)
1944 Tcl_ListObjAppendElement(interp, obj,
1945 Tcl_NewStringObj("cursor_position", -1));
1946 Tcl_ListObjAppendElement(interp, obj,
1947 Tcl_NewIntObj(edata->internalpos));
1949 if (edata->filename)
1951 Tcl_ListObjAppendElement(interp, obj,
1952 Tcl_NewStringObj("filename", -1));
1954 Tcl_ListObjAppendElement(interp, obj,
1955 Tcl_NewStringObj(UTF_E2U(edata->filename), -1));
1958 if (edata->lineno > 0)
1960 Tcl_ListObjAppendElement(interp, obj,
1961 Tcl_NewStringObj("lineno", -1));
1962 Tcl_ListObjAppendElement(interp, obj,
1963 Tcl_NewIntObj(edata->lineno));
1965 if (edata->funcname)
1967 Tcl_ListObjAppendElement(interp, obj,
1968 Tcl_NewStringObj("funcname", -1));
1970 Tcl_ListObjAppendElement(interp, obj,
1971 Tcl_NewStringObj(UTF_E2U(edata->funcname), -1));
1975 Tcl_SetObjErrorCode(interp, obj);
1979 /**********************************************************************
1980 * pltcl_get_condition_name() - find name for SQLSTATE
1981 **********************************************************************/
1983 pltcl_get_condition_name(int sqlstate)
1987 for (i = 0; exception_name_map[i].label != NULL; i++)
1989 if (exception_name_map[i].sqlerrstate == sqlstate)
1990 return exception_name_map[i].label;
1992 return "unrecognized_sqlstate";
1996 /**********************************************************************
1997 * pltcl_quote() - quote literal strings that are to
1998 * be used in SPI_execute query strings
1999 **********************************************************************/
2001 pltcl_quote(ClientData cdata, Tcl_Interp *interp,
2002 int objc, Tcl_Obj *const objv[])
2009 /************************************************************
2011 ************************************************************/
2014 Tcl_WrongNumArgs(interp, 1, objv, "string");
2018 /************************************************************
2019 * Allocate space for the maximum the string can
2020 * grow to and initialize pointers
2021 ************************************************************/
2022 cp1 = Tcl_GetStringFromObj(objv[1], &length);
2023 tmp = palloc(length * 2 + 1);
2026 /************************************************************
2027 * Walk through string and double every quote and backslash
2028 ************************************************************/
2041 /************************************************************
2042 * Terminate the string and set it as result
2043 ************************************************************/
2045 Tcl_SetObjResult(interp, Tcl_NewStringObj(tmp, -1));
2051 /**********************************************************************
2052 * pltcl_argisnull() - determine if a specific argument is NULL
2053 **********************************************************************/
2055 pltcl_argisnull(ClientData cdata, Tcl_Interp *interp,
2056 int objc, Tcl_Obj *const objv[])
2059 FunctionCallInfo fcinfo = pltcl_current_call_state->fcinfo;
2061 /************************************************************
2063 ************************************************************/
2066 Tcl_WrongNumArgs(interp, 1, objv, "argno");
2070 /************************************************************
2071 * Check that we're called as a normal function
2072 ************************************************************/
2075 Tcl_SetObjResult(interp,
2076 Tcl_NewStringObj("argisnull cannot be used in triggers", -1));
2080 /************************************************************
2081 * Get the argument number
2082 ************************************************************/
2083 if (Tcl_GetIntFromObj(interp, objv[1], &argno) != TCL_OK)
2086 /************************************************************
2087 * Check that the argno is valid
2088 ************************************************************/
2090 if (argno < 0 || argno >= fcinfo->nargs)
2092 Tcl_SetObjResult(interp,
2093 Tcl_NewStringObj("argno out of range", -1));
2097 /************************************************************
2098 * Get the requested NULL state
2099 ************************************************************/
2100 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(PG_ARGISNULL(argno)));
2105 /**********************************************************************
2106 * pltcl_returnnull() - Cause a NULL return from the current function
2107 **********************************************************************/
2109 pltcl_returnnull(ClientData cdata, Tcl_Interp *interp,
2110 int objc, Tcl_Obj *const objv[])
2112 FunctionCallInfo fcinfo = pltcl_current_call_state->fcinfo;
2114 /************************************************************
2116 ************************************************************/
2119 Tcl_WrongNumArgs(interp, 1, objv, "");
2123 /************************************************************
2124 * Check that we're called as a normal function
2125 ************************************************************/
2128 Tcl_SetObjResult(interp,
2129 Tcl_NewStringObj("return_null cannot be used in triggers", -1));
2133 /************************************************************
2134 * Set the NULL return flag and cause Tcl to return from the
2136 ************************************************************/
2137 fcinfo->isnull = true;
2143 /**********************************************************************
2144 * pltcl_returnnext() - Add a row to the result tuplestore in a SRF.
2145 **********************************************************************/
2147 pltcl_returnnext(ClientData cdata, Tcl_Interp *interp,
2148 int objc, Tcl_Obj *const objv[])
2150 pltcl_call_state *call_state = pltcl_current_call_state;
2151 FunctionCallInfo fcinfo = call_state->fcinfo;
2152 pltcl_proc_desc *prodesc = call_state->prodesc;
2153 MemoryContext oldcontext = CurrentMemoryContext;
2154 ResourceOwner oldowner = CurrentResourceOwner;
2155 volatile int result = TCL_OK;
2158 * Check that we're called as a set-returning function
2162 Tcl_SetObjResult(interp,
2163 Tcl_NewStringObj("return_next cannot be used in triggers", -1));
2167 if (!prodesc->fn_retisset)
2169 Tcl_SetObjResult(interp,
2170 Tcl_NewStringObj("return_next cannot be used in non-set-returning functions", -1));
2179 Tcl_WrongNumArgs(interp, 1, objv, "result");
2184 * The rest might throw elog(ERROR), so must run in a subtransaction.
2186 * A small advantage of using a subtransaction is that it provides a
2187 * short-lived memory context for free, so we needn't worry about leaking
2188 * memory here. To use that context, call BeginInternalSubTransaction
2189 * directly instead of going through pltcl_subtrans_begin.
2191 BeginInternalSubTransaction(NULL);
2194 /* Set up tuple store if first output row */
2195 if (call_state->tuple_store == NULL)
2196 pltcl_init_tuple_store(call_state);
2198 if (prodesc->fn_retistuple)
2203 /* result should be a list, so break it down */
2204 if (Tcl_ListObjGetElements(interp, objv[1], &rowObjc, &rowObjv) == TCL_ERROR)
2210 tuple = pltcl_build_tuple_result(interp, rowObjv, rowObjc,
2212 tuplestore_puttuple(call_state->tuple_store, tuple);
2218 bool isNull = false;
2220 /* for paranoia's sake, check that tupdesc has exactly one column */
2221 if (call_state->ret_tupdesc->natts != 1)
2222 elog(ERROR, "wrong result type supplied in return_next");
2224 retval = InputFunctionCall(&prodesc->result_in_func,
2225 utf_u2e((char *) Tcl_GetString(objv[1])),
2226 prodesc->result_typioparam,
2228 tuplestore_putvalues(call_state->tuple_store, call_state->ret_tupdesc,
2232 pltcl_subtrans_commit(oldcontext, oldowner);
2236 pltcl_subtrans_abort(interp, oldcontext, oldowner);
2246 * Support for running SPI operations inside subtransactions
2248 * Intended usage pattern is:
2250 * MemoryContext oldcontext = CurrentMemoryContext;
2251 * ResourceOwner oldowner = CurrentResourceOwner;
2254 * pltcl_subtrans_begin(oldcontext, oldowner);
2257 * do something risky;
2258 * pltcl_subtrans_commit(oldcontext, oldowner);
2262 * pltcl_subtrans_abort(interp, oldcontext, oldowner);
2270 pltcl_subtrans_begin(MemoryContext oldcontext, ResourceOwner oldowner)
2272 BeginInternalSubTransaction(NULL);
2274 /* Want to run inside function's memory context */
2275 MemoryContextSwitchTo(oldcontext);
2279 pltcl_subtrans_commit(MemoryContext oldcontext, ResourceOwner oldowner)
2281 /* Commit the inner transaction, return to outer xact context */
2282 ReleaseCurrentSubTransaction();
2283 MemoryContextSwitchTo(oldcontext);
2284 CurrentResourceOwner = oldowner;
2288 pltcl_subtrans_abort(Tcl_Interp *interp,
2289 MemoryContext oldcontext, ResourceOwner oldowner)
2293 /* Save error info */
2294 MemoryContextSwitchTo(oldcontext);
2295 edata = CopyErrorData();
2298 /* Abort the inner transaction */
2299 RollbackAndReleaseCurrentSubTransaction();
2300 MemoryContextSwitchTo(oldcontext);
2301 CurrentResourceOwner = oldowner;
2303 /* Pass the error data to Tcl */
2304 pltcl_construct_errorCode(interp, edata);
2306 Tcl_SetObjResult(interp, Tcl_NewStringObj(UTF_E2U(edata->message), -1));
2308 FreeErrorData(edata);
2312 /**********************************************************************
2313 * pltcl_SPI_execute() - The builtin SPI_execute command
2314 * for the Tcl interpreter
2315 **********************************************************************/
2317 pltcl_SPI_execute(ClientData cdata, Tcl_Interp *interp,
2318 int objc, Tcl_Obj *const objv[])
2326 const char *volatile arrayname = NULL;
2327 Tcl_Obj *volatile loop_body = NULL;
2328 MemoryContext oldcontext = CurrentMemoryContext;
2329 ResourceOwner oldowner = CurrentResourceOwner;
2333 OPT_ARRAY, OPT_COUNT
2336 static const char *options[] = {
2337 "-array", "-count", (const char *) NULL
2340 /************************************************************
2341 * Check the call syntax and get the options
2342 ************************************************************/
2345 Tcl_WrongNumArgs(interp, 1, objv,
2346 "?-count n? ?-array name? query ?loop body?");
2353 if (Tcl_GetIndexFromObj(NULL, objv[i], options, NULL,
2354 TCL_EXACT, &optIndex) != TCL_OK)
2359 Tcl_SetObjResult(interp,
2360 Tcl_NewStringObj("missing argument to -count or -array", -1));
2364 switch ((enum options) optIndex)
2367 arrayname = Tcl_GetString(objv[i++]);
2371 if (Tcl_GetIntFromObj(interp, objv[i++], &count) != TCL_OK)
2378 if (query_idx >= objc || query_idx + 2 < objc)
2380 Tcl_WrongNumArgs(interp, query_idx - 1, objv, "query ?loop body?");
2384 if (query_idx + 1 < objc)
2385 loop_body = objv[query_idx + 1];
2387 /************************************************************
2388 * Execute the query inside a sub-transaction, so we can cope with
2390 ************************************************************/
2392 pltcl_subtrans_begin(oldcontext, oldowner);
2397 spi_rc = SPI_execute(UTF_U2E(Tcl_GetString(objv[query_idx])),
2398 pltcl_current_call_state->prodesc->fn_readonly, count);
2401 my_rc = pltcl_process_SPI_result(interp,
2408 pltcl_subtrans_commit(oldcontext, oldowner);
2412 pltcl_subtrans_abort(interp, oldcontext, oldowner);
2421 * Process the result from SPI_execute or SPI_execute_plan
2423 * Shared code between pltcl_SPI_execute and pltcl_SPI_execute_plan
2426 pltcl_process_SPI_result(Tcl_Interp *interp,
2427 const char *arrayname,
2430 SPITupleTable *tuptable,
2440 case SPI_OK_SELINTO:
2444 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(ntuples));
2447 case SPI_OK_UTILITY:
2448 case SPI_OK_REWRITTEN:
2449 if (tuptable == NULL)
2451 Tcl_SetObjResult(interp, Tcl_NewIntObj(0));
2454 /* fall through for utility returning tuples */
2458 case SPI_OK_INSERT_RETURNING:
2459 case SPI_OK_DELETE_RETURNING:
2460 case SPI_OK_UPDATE_RETURNING:
2463 * Process the tuples we got
2465 tuples = tuptable->vals;
2466 tupdesc = tuptable->tupdesc;
2468 if (loop_body == NULL)
2471 * If there is no loop body given, just set the variables from
2472 * the first tuple (if any)
2475 pltcl_set_tuple_values(interp, arrayname, 0,
2476 tuples[0], tupdesc);
2481 * There is a loop body - process all tuples and evaluate the
2486 for (i = 0; i < ntuples; i++)
2488 pltcl_set_tuple_values(interp, arrayname, i,
2489 tuples[i], tupdesc);
2491 loop_rc = Tcl_EvalObjEx(interp, loop_body, 0);
2493 if (loop_rc == TCL_OK)
2495 if (loop_rc == TCL_CONTINUE)
2497 if (loop_rc == TCL_RETURN)
2502 if (loop_rc == TCL_BREAK)
2509 if (my_rc == TCL_OK)
2511 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(ntuples));
2516 Tcl_AppendResult(interp, "pltcl: SPI_execute failed: ",
2517 SPI_result_code_string(spi_rc), NULL);
2522 SPI_freetuptable(tuptable);
2528 /**********************************************************************
2529 * pltcl_SPI_prepare() - Builtin support for prepared plans
2530 * The Tcl command SPI_prepare
2531 * always saves the plan using
2532 * SPI_keepplan and returns a key for
2533 * access. There is no chance to prepare
2534 * and not save the plan currently.
2535 **********************************************************************/
2537 pltcl_SPI_prepare(ClientData cdata, Tcl_Interp *interp,
2538 int objc, Tcl_Obj *const objv[])
2540 volatile MemoryContext plan_cxt = NULL;
2543 pltcl_query_desc *qdesc;
2545 Tcl_HashEntry *hashent;
2547 Tcl_HashTable *query_hash;
2548 MemoryContext oldcontext = CurrentMemoryContext;
2549 ResourceOwner oldowner = CurrentResourceOwner;
2551 /************************************************************
2552 * Check the call syntax
2553 ************************************************************/
2556 Tcl_WrongNumArgs(interp, 1, objv, "query argtypes");
2560 /************************************************************
2561 * Split the argument type list
2562 ************************************************************/
2563 if (Tcl_ListObjGetElements(interp, objv[2], &nargs, &argsObj) != TCL_OK)
2566 /************************************************************
2567 * Allocate the new querydesc structure
2569 * struct qdesc and subsidiary data all live in plan_cxt. Note that if the
2570 * function is recompiled for whatever reason, permanent memory leaks
2571 * occur. FIXME someday.
2572 ************************************************************/
2573 plan_cxt = AllocSetContextCreate(TopMemoryContext,
2574 "PL/Tcl spi_prepare query",
2575 ALLOCSET_SMALL_SIZES);
2576 MemoryContextSwitchTo(plan_cxt);
2577 qdesc = (pltcl_query_desc *) palloc0(sizeof(pltcl_query_desc));
2578 snprintf(qdesc->qname, sizeof(qdesc->qname), "%p", qdesc);
2579 qdesc->nargs = nargs;
2580 qdesc->argtypes = (Oid *) palloc(nargs * sizeof(Oid));
2581 qdesc->arginfuncs = (FmgrInfo *) palloc(nargs * sizeof(FmgrInfo));
2582 qdesc->argtypioparams = (Oid *) palloc(nargs * sizeof(Oid));
2583 MemoryContextSwitchTo(oldcontext);
2585 /************************************************************
2586 * Execute the prepare inside a sub-transaction, so we can cope with
2588 ************************************************************/
2590 pltcl_subtrans_begin(oldcontext, oldowner);
2594 /************************************************************
2595 * Resolve argument type names and then look them up by oid
2596 * in the system cache, and remember the required information
2597 * for input conversion.
2598 ************************************************************/
2599 for (i = 0; i < nargs; i++)
2606 parseTypeString(Tcl_GetString(argsObj[i]), &typId, &typmod, false);
2608 getTypeInputInfo(typId, &typInput, &typIOParam);
2610 qdesc->argtypes[i] = typId;
2611 fmgr_info_cxt(typInput, &(qdesc->arginfuncs[i]), plan_cxt);
2612 qdesc->argtypioparams[i] = typIOParam;
2615 /************************************************************
2616 * Prepare the plan and check for errors
2617 ************************************************************/
2619 qdesc->plan = SPI_prepare(UTF_U2E(Tcl_GetString(objv[1])),
2620 nargs, qdesc->argtypes);
2623 if (qdesc->plan == NULL)
2624 elog(ERROR, "SPI_prepare() failed");
2626 /************************************************************
2627 * Save the plan into permanent memory (right now it's in the
2628 * SPI procCxt, which will go away at function end).
2629 ************************************************************/
2630 if (SPI_keepplan(qdesc->plan))
2631 elog(ERROR, "SPI_keepplan() failed");
2633 pltcl_subtrans_commit(oldcontext, oldowner);
2637 pltcl_subtrans_abort(interp, oldcontext, oldowner);
2639 MemoryContextDelete(plan_cxt);
2645 /************************************************************
2646 * Insert a hashtable entry for the plan and return
2647 * the key to the caller
2648 ************************************************************/
2649 query_hash = &pltcl_current_call_state->prodesc->interp_desc->query_hash;
2651 hashent = Tcl_CreateHashEntry(query_hash, qdesc->qname, &hashnew);
2652 Tcl_SetHashValue(hashent, (ClientData) qdesc);
2654 /* qname is ASCII, so no need for encoding conversion */
2655 Tcl_SetObjResult(interp, Tcl_NewStringObj(qdesc->qname, -1));
2660 /**********************************************************************
2661 * pltcl_SPI_execute_plan() - Execute a prepared plan
2662 **********************************************************************/
2664 pltcl_SPI_execute_plan(ClientData cdata, Tcl_Interp *interp,
2665 int objc, Tcl_Obj *const objv[])
2672 Tcl_HashEntry *hashent;
2673 pltcl_query_desc *qdesc;
2674 const char *nulls = NULL;
2675 const char *arrayname = NULL;
2676 Tcl_Obj *loop_body = NULL;
2679 Tcl_Obj **callObjv = NULL;
2681 MemoryContext oldcontext = CurrentMemoryContext;
2682 ResourceOwner oldowner = CurrentResourceOwner;
2683 Tcl_HashTable *query_hash;
2687 OPT_ARRAY, OPT_COUNT, OPT_NULLS
2690 static const char *options[] = {
2691 "-array", "-count", "-nulls", (const char *) NULL
2694 /************************************************************
2695 * Get the options and check syntax
2696 ************************************************************/
2700 if (Tcl_GetIndexFromObj(NULL, objv[i], options, NULL,
2701 TCL_EXACT, &optIndex) != TCL_OK)
2706 Tcl_SetObjResult(interp,
2707 Tcl_NewStringObj("missing argument to -array, -count or -nulls", -1));
2711 switch ((enum options) optIndex)
2714 arrayname = Tcl_GetString(objv[i++]);
2718 if (Tcl_GetIntFromObj(interp, objv[i++], &count) != TCL_OK)
2723 nulls = Tcl_GetString(objv[i++]);
2728 /************************************************************
2729 * Get the prepared plan descriptor by its key
2730 ************************************************************/
2733 Tcl_SetObjResult(interp,
2734 Tcl_NewStringObj("missing argument to -count or -array", -1));
2738 query_hash = &pltcl_current_call_state->prodesc->interp_desc->query_hash;
2740 hashent = Tcl_FindHashEntry(query_hash, Tcl_GetString(objv[i]));
2741 if (hashent == NULL)
2743 Tcl_AppendResult(interp, "invalid queryid '", Tcl_GetString(objv[i]), "'", NULL);
2746 qdesc = (pltcl_query_desc *) Tcl_GetHashValue(hashent);
2749 /************************************************************
2750 * If a nulls string is given, check for correct length
2751 ************************************************************/
2754 if (strlen(nulls) != qdesc->nargs)
2756 Tcl_SetObjResult(interp,
2758 "length of nulls string doesn't match number of arguments",
2764 /************************************************************
2765 * If there was a argtype list on preparation, we need
2766 * an argument value list now
2767 ************************************************************/
2768 if (qdesc->nargs > 0)
2772 Tcl_SetObjResult(interp,
2774 "argument list length doesn't match number of arguments for query"
2779 /************************************************************
2780 * Split the argument values
2781 ************************************************************/
2782 if (Tcl_ListObjGetElements(interp, objv[i++], &callObjc, &callObjv) != TCL_OK)
2785 /************************************************************
2786 * Check that the number of arguments matches
2787 ************************************************************/
2788 if (callObjc != qdesc->nargs)
2790 Tcl_SetObjResult(interp,
2792 "argument list length doesn't match number of arguments for query"
2800 /************************************************************
2801 * Get loop body if present
2802 ************************************************************/
2804 loop_body = objv[i++];
2808 Tcl_WrongNumArgs(interp, 1, objv,
2809 "?-count n? ?-array name? ?-nulls string? "
2810 "query ?args? ?loop body?");
2814 /************************************************************
2815 * Execute the plan inside a sub-transaction, so we can cope with
2817 ************************************************************/
2819 pltcl_subtrans_begin(oldcontext, oldowner);
2823 /************************************************************
2824 * Setup the value array for SPI_execute_plan() using
2825 * the type specific input functions
2826 ************************************************************/
2827 argvalues = (Datum *) palloc(callObjc * sizeof(Datum));
2829 for (j = 0; j < callObjc; j++)
2831 if (nulls && nulls[j] == 'n')
2833 argvalues[j] = InputFunctionCall(&qdesc->arginfuncs[j],
2835 qdesc->argtypioparams[j],
2841 argvalues[j] = InputFunctionCall(&qdesc->arginfuncs[j],
2842 UTF_U2E(Tcl_GetString(callObjv[j])),
2843 qdesc->argtypioparams[j],
2849 /************************************************************
2851 ************************************************************/
2852 spi_rc = SPI_execute_plan(qdesc->plan, argvalues, nulls,
2853 pltcl_current_call_state->prodesc->fn_readonly,
2856 my_rc = pltcl_process_SPI_result(interp,
2863 pltcl_subtrans_commit(oldcontext, oldowner);
2867 pltcl_subtrans_abort(interp, oldcontext, oldowner);
2876 /**********************************************************************
2877 * pltcl_SPI_lastoid() - return the last oid. To
2878 * be used after insert queries
2879 **********************************************************************/
2881 pltcl_SPI_lastoid(ClientData cdata, Tcl_Interp *interp,
2882 int objc, Tcl_Obj *const objv[])
2889 Tcl_WrongNumArgs(interp, 1, objv, "");
2893 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(SPI_lastoid));
2898 /**********************************************************************
2899 * pltcl_subtransaction() - Execute some Tcl code in a subtransaction
2901 * The subtransaction is aborted if the Tcl code fragment returns TCL_ERROR,
2902 * otherwise it's subcommitted.
2903 **********************************************************************/
2905 pltcl_subtransaction(ClientData cdata, Tcl_Interp *interp,
2906 int objc, Tcl_Obj *const objv[])
2908 MemoryContext oldcontext = CurrentMemoryContext;
2909 ResourceOwner oldowner = CurrentResourceOwner;
2914 Tcl_WrongNumArgs(interp, 1, objv, "command");
2919 * Note: we don't use pltcl_subtrans_begin and friends here because we
2920 * don't want the error handling in pltcl_subtrans_abort. But otherwise
2921 * the processing should be about the same as in those functions.
2923 BeginInternalSubTransaction(NULL);
2924 MemoryContextSwitchTo(oldcontext);
2926 retcode = Tcl_EvalObjEx(interp, objv[1], 0);
2928 if (retcode == TCL_ERROR)
2930 /* Rollback the subtransaction */
2931 RollbackAndReleaseCurrentSubTransaction();
2935 /* Commit the subtransaction */
2936 ReleaseCurrentSubTransaction();
2939 /* In either case, restore previous memory context and resource owner */
2940 MemoryContextSwitchTo(oldcontext);
2941 CurrentResourceOwner = oldowner;
2947 /**********************************************************************
2950 * Commit the transaction and start a new one.
2951 **********************************************************************/
2953 pltcl_commit(ClientData cdata, Tcl_Interp *interp,
2954 int objc, Tcl_Obj *const objv[])
2956 MemoryContext oldcontext = CurrentMemoryContext;
2961 SPI_start_transaction();
2967 /* Save error info */
2968 MemoryContextSwitchTo(oldcontext);
2969 edata = CopyErrorData();
2972 /* Pass the error data to Tcl */
2973 pltcl_construct_errorCode(interp, edata);
2975 Tcl_SetObjResult(interp, Tcl_NewStringObj(UTF_E2U(edata->message), -1));
2977 FreeErrorData(edata);
2987 /**********************************************************************
2990 * Abort the transaction and start a new one.
2991 **********************************************************************/
2993 pltcl_rollback(ClientData cdata, Tcl_Interp *interp,
2994 int objc, Tcl_Obj *const objv[])
2996 MemoryContext oldcontext = CurrentMemoryContext;
3001 SPI_start_transaction();
3007 /* Save error info */
3008 MemoryContextSwitchTo(oldcontext);
3009 edata = CopyErrorData();
3012 /* Pass the error data to Tcl */
3013 pltcl_construct_errorCode(interp, edata);
3015 Tcl_SetObjResult(interp, Tcl_NewStringObj(UTF_E2U(edata->message), -1));
3017 FreeErrorData(edata);
3027 /**********************************************************************
3028 * pltcl_set_tuple_values() - Set variables for all attributes
3031 * Note: arrayname is presumed to be UTF8; it usually came from Tcl
3032 **********************************************************************/
3034 pltcl_set_tuple_values(Tcl_Interp *interp, const char *arrayname,
3035 uint64 tupno, HeapTuple tuple, TupleDesc tupdesc)
3041 const char *attname;
3044 const char **arrptr;
3045 const char **nameptr;
3046 const char *nullname = NULL;
3048 /************************************************************
3049 * Prepare pointers for Tcl_SetVar2() below
3050 ************************************************************/
3051 if (arrayname == NULL)
3054 nameptr = &nullname;
3058 arrptr = &arrayname;
3062 * When outputting to an array, fill the ".tupno" element with the
3063 * current tuple number. This will be overridden below if ".tupno" is
3064 * in use as an actual field name in the rowtype.
3066 Tcl_SetVar2Ex(interp, arrayname, ".tupno", Tcl_NewWideIntObj(tupno), 0);
3069 for (i = 0; i < tupdesc->natts; i++)
3071 Form_pg_attribute att = TupleDescAttr(tupdesc, i);
3073 /* ignore dropped attributes */
3074 if (att->attisdropped)
3077 /************************************************************
3078 * Get the attribute name
3079 ************************************************************/
3081 attname = pstrdup(UTF_E2U(NameStr(att->attname)));
3084 /************************************************************
3085 * Get the attributes value
3086 ************************************************************/
3087 attr = heap_getattr(tuple, i + 1, tupdesc, &isnull);
3089 /************************************************************
3090 * If there is a value, set the variable
3093 * Hmmm - Null attributes will cause functions to
3094 * crash if they don't expect them - need something
3096 ************************************************************/
3099 getTypeOutputInfo(att->atttypid, &typoutput, &typisvarlena);
3100 outputstr = OidOutputFunctionCall(typoutput, attr);
3102 Tcl_SetVar2Ex(interp, *arrptr, *nameptr,
3103 Tcl_NewStringObj(UTF_E2U(outputstr), -1), 0);
3108 Tcl_UnsetVar2(interp, *arrptr, *nameptr, 0);
3110 pfree((char *) attname);
3115 /**********************************************************************
3116 * pltcl_build_tuple_argument() - Build a list object usable for 'array set'
3117 * from all attributes of a given tuple
3118 **********************************************************************/
3120 pltcl_build_tuple_argument(HeapTuple tuple, TupleDesc tupdesc)
3122 Tcl_Obj *retobj = Tcl_NewObj();
3131 for (i = 0; i < tupdesc->natts; i++)
3133 Form_pg_attribute att = TupleDescAttr(tupdesc, i);
3135 /* ignore dropped attributes */
3136 if (att->attisdropped)
3139 /************************************************************
3140 * Get the attribute name
3141 ************************************************************/
3142 attname = NameStr(att->attname);
3144 /************************************************************
3145 * Get the attributes value
3146 ************************************************************/
3147 attr = heap_getattr(tuple, i + 1, tupdesc, &isnull);
3149 /************************************************************
3150 * If there is a value, append the attribute name and the
3153 * Hmmm - Null attributes will cause functions to
3154 * crash if they don't expect them - need something
3156 ************************************************************/
3159 getTypeOutputInfo(att->atttypid,
3160 &typoutput, &typisvarlena);
3161 outputstr = OidOutputFunctionCall(typoutput, attr);
3163 Tcl_ListObjAppendElement(NULL, retobj,
3164 Tcl_NewStringObj(UTF_E2U(attname), -1));
3167 Tcl_ListObjAppendElement(NULL, retobj,
3168 Tcl_NewStringObj(UTF_E2U(outputstr), -1));
3177 /**********************************************************************
3178 * pltcl_build_tuple_result() - Build a tuple of function's result rowtype
3179 * from a Tcl list of column names and values
3181 * In a trigger function, we build a tuple of the trigger table's rowtype.
3183 * Note: this function leaks memory. Even if we made it clean up its own
3184 * mess, there's no way to prevent the datatype input functions it calls
3185 * from leaking. Run it in a short-lived context, unless we're about to
3186 * exit the procedure anyway.
3187 **********************************************************************/
3189 pltcl_build_tuple_result(Tcl_Interp *interp, Tcl_Obj **kvObjv, int kvObjc,
3190 pltcl_call_state *call_state)
3194 AttInMetadata *attinmeta;
3198 if (call_state->ret_tupdesc)
3200 tupdesc = call_state->ret_tupdesc;
3201 attinmeta = call_state->attinmeta;
3203 else if (call_state->trigdata)
3205 tupdesc = RelationGetDescr(call_state->trigdata->tg_relation);
3206 attinmeta = TupleDescGetAttInMetadata(tupdesc);
3210 elog(ERROR, "PL/Tcl function does not return a tuple");
3211 tupdesc = NULL; /* keep compiler quiet */
3215 values = (char **) palloc0(tupdesc->natts * sizeof(char *));
3217 if (kvObjc % 2 != 0)
3219 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3220 errmsg("column name/value list must have even number of elements")));
3222 for (i = 0; i < kvObjc; i += 2)
3224 char *fieldName = utf_u2e(Tcl_GetString(kvObjv[i]));
3225 int attn = SPI_fnumber(tupdesc, fieldName);
3228 * We silently ignore ".tupno", if it's present but doesn't match any
3229 * actual output column. This allows direct use of a row returned by
3230 * pltcl_set_tuple_values().
3232 if (attn == SPI_ERROR_NOATTRIBUTE)
3234 if (strcmp(fieldName, ".tupno") == 0)
3237 (errcode(ERRCODE_UNDEFINED_COLUMN),
3238 errmsg("column name/value list contains nonexistent column name \"%s\"",
3244 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3245 errmsg("cannot set system attribute \"%s\"",
3248 values[attn - 1] = utf_u2e(Tcl_GetString(kvObjv[i + 1]));
3251 tuple = BuildTupleFromCStrings(attinmeta, values);
3253 /* if result type is domain-over-composite, check domain constraints */
3254 if (call_state->prodesc->fn_retisdomain)
3255 domain_check(HeapTupleGetDatum(tuple), false,
3256 call_state->prodesc->result_typid,
3257 &call_state->prodesc->domain_info,
3258 call_state->prodesc->fn_cxt);
3263 /**********************************************************************
3264 * pltcl_init_tuple_store() - Initialize the result tuplestore for a SRF
3265 **********************************************************************/
3267 pltcl_init_tuple_store(pltcl_call_state *call_state)
3269 ReturnSetInfo *rsi = call_state->rsi;
3270 MemoryContext oldcxt;
3271 ResourceOwner oldowner;
3273 /* Should be in a SRF */
3275 /* Should be first time through */
3276 Assert(!call_state->tuple_store);
3277 Assert(!call_state->attinmeta);
3279 /* We expect caller to provide an appropriate result tupdesc */
3280 Assert(rsi->expectedDesc);
3281 call_state->ret_tupdesc = rsi->expectedDesc;
3284 * Switch to the right memory context and resource owner for storing the
3285 * tuplestore. If we're within a subtransaction opened for an exception
3286 * block, for example, we must still create the tuplestore in the resource
3287 * owner that was active when this function was entered, and not in the
3288 * subtransaction's resource owner.
3290 oldcxt = MemoryContextSwitchTo(call_state->tuple_store_cxt);
3291 oldowner = CurrentResourceOwner;
3292 CurrentResourceOwner = call_state->tuple_store_owner;
3294 call_state->tuple_store =
3295 tuplestore_begin_heap(rsi->allowedModes & SFRM_Materialize_Random,
3298 /* Build attinmeta in this context, too */
3299 call_state->attinmeta = TupleDescGetAttInMetadata(call_state->ret_tupdesc);
3301 CurrentResourceOwner = oldowner;
3302 MemoryContextSwitchTo(oldcxt);