PostgreSQL Source Code  git master
pltcl.c
Go to the documentation of this file.
1 /**********************************************************************
2  * pltcl.c - PostgreSQL support for Tcl as
3  * procedural language (PL)
4  *
5  * src/pl/tcl/pltcl.c
6  *
7  **********************************************************************/
8 
9 #include "postgres.h"
10 
11 #include <tcl.h>
12 
13 #include <unistd.h>
14 #include <fcntl.h>
15 
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"
24 #include "fmgr.h"
25 #include "funcapi.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"
31 #include "pgstat.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"
40 
41 
43 
44 #define HAVE_TCL_VERSION(maj,min) \
45  ((TCL_MAJOR_VERSION > maj) || \
46  (TCL_MAJOR_VERSION == maj && TCL_MINOR_VERSION >= min))
47 
48 /* Insist on Tcl >= 8.4 */
49 #if !HAVE_TCL_VERSION(8,4)
50 #error PostgreSQL only supports Tcl 8.4 or later.
51 #endif
52 
53 /* Hack to deal with Tcl 8.6 const-ification without losing compatibility */
54 #ifndef CONST86
55 #define CONST86
56 #endif
57 
58 /* define our text domain for translations */
59 #undef TEXTDOMAIN
60 #define TEXTDOMAIN PG_TEXTDOMAIN("pltcl")
61 
62 
63 /*
64  * Support for converting between UTF8 (which is what all strings going into
65  * or out of Tcl should be) and the database encoding.
66  *
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.
71  */
72 
73 static inline char *
74 utf_u2e(const char *src)
75 {
76  return pg_any_to_server(src, strlen(src), PG_UTF8);
77 }
78 
79 static inline char *
80 utf_e2u(const char *src)
81 {
82  return pg_server_to_any(src, strlen(src), PG_UTF8);
83 }
84 
85 #define UTF_BEGIN \
86  do { \
87  const char *_pltcl_utf_src = NULL; \
88  char *_pltcl_utf_dst = NULL
89 
90 #define UTF_END \
91  if (_pltcl_utf_src != (const char *) _pltcl_utf_dst) \
92  pfree(_pltcl_utf_dst); \
93  } while (0)
94 
95 #define UTF_U2E(x) \
96  (_pltcl_utf_dst = utf_u2e(_pltcl_utf_src = (x)))
97 
98 #define UTF_E2U(x) \
99  (_pltcl_utf_dst = utf_e2u(_pltcl_utf_src = (x)))
100 
101 
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.)
108  *
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
113 {
114  Oid user_id; /* Hash key (must be first!) */
115  Tcl_Interp *interp; /* The interpreter */
116  Tcl_HashTable query_hash; /* pltcl_query_desc structs */
118 
119 
120 /**********************************************************************
121  * The information we cache about loaded procedures
122  *
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.)
131  *
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
136 {
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? */
158 
159 
160 /**********************************************************************
161  * The information we cache about prepared and saved plans
162  **********************************************************************/
163 typedef struct pltcl_query_desc
164 {
165  char qname[20];
167  int nargs;
172 
173 
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.
179  *
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
186 {
187  Oid proc_id; /* Function OID */
188 
189  /*
190  * is_trigger is really a bool, but declare as Oid to ensure this struct
191  * contains no padding
192  */
193  Oid is_trigger; /* is it a trigger function? */
194  Oid user_id; /* User calling the function, or 0 */
196 
197 typedef struct pltcl_proc_ptr
198 {
199  pltcl_proc_key proc_key; /* Hash key (must be first!) */
202 
203 
204 /**********************************************************************
205  * Per-call state
206  **********************************************************************/
207 typedef struct pltcl_call_state
208 {
209  /* Call info struct, or NULL in a trigger */
211 
212  /* Trigger data, if we're in a normal (not event) trigger; else NULL */
214 
215  /* Function we're executing (NULL if not yet identified) */
217 
218  /*
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.
222  */
223  TupleDesc ret_tupdesc; /* return rowtype, if retistuple or retisset */
224  AttInMetadata *attinmeta; /* metadata for building tuples of that type */
225 
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 */
231 
232 
233 /**********************************************************************
234  * Global data
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;
242 
243 /* this is saved and restored by pltcl_handler */
245 
246 /**********************************************************************
247  * Lookup table for SQLSTATE condition names
248  **********************************************************************/
249 typedef struct
250 {
251  const char *label;
254 
256 #include "pltclerrcodes.h" /* pgrminclude ignore */
257  {NULL, 0}
258 };
259 
260 /**********************************************************************
261  * Forward declarations
262  **********************************************************************/
263 void _PG_init(void);
264 
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);
270 
271 static Datum pltcl_handler(PG_FUNCTION_ARGS, bool pltrusted);
272 
274  bool pltrusted);
276  bool pltrusted);
278  bool pltrusted);
279 
280 static void throw_tcl_error(Tcl_Interp *interp, const char *proname);
281 
282 static pltcl_proc_desc *compile_pltcl_function(Oid fn_oid, Oid tgreloid,
283  bool is_event_trigger,
284  bool pltrusted);
285 
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,
302  Tcl_Obj *loop_body,
303  int spi_rc,
304  SPITupleTable *tuptable,
305  uint64 ntuples);
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_subtransaction(ClientData cdata, Tcl_Interp *interp,
311  int objc, Tcl_Obj *const objv[]);
312 static int pltcl_commit(ClientData cdata, Tcl_Interp *interp,
313  int objc, Tcl_Obj *const objv[]);
314 static int pltcl_rollback(ClientData cdata, Tcl_Interp *interp,
315  int objc, Tcl_Obj *const objv[]);
316 
317 static void pltcl_subtrans_begin(MemoryContext oldcontext,
318  ResourceOwner oldowner);
319 static void pltcl_subtrans_commit(MemoryContext oldcontext,
320  ResourceOwner oldowner);
321 static void pltcl_subtrans_abort(Tcl_Interp *interp,
322  MemoryContext oldcontext,
323  ResourceOwner oldowner);
324 
325 static void pltcl_set_tuple_values(Tcl_Interp *interp, const char *arrayname,
326  uint64 tupno, HeapTuple tuple, TupleDesc tupdesc);
327 static Tcl_Obj *pltcl_build_tuple_argument(HeapTuple tuple, TupleDesc tupdesc, bool include_generated);
328 static HeapTuple pltcl_build_tuple_result(Tcl_Interp *interp,
329  Tcl_Obj **kvObjv, int kvObjc,
330  pltcl_call_state *call_state);
331 static void pltcl_init_tuple_store(pltcl_call_state *call_state);
332 
333 
334 /*
335  * Hack to override Tcl's builtin Notifier subsystem. This prevents the
336  * backend from becoming multithreaded, which breaks all sorts of things.
337  * That happens in the default version of Tcl_InitNotifier if the TCL library
338  * has been compiled with multithreading support (i.e. when TCL_THREADS is
339  * defined under Unix, and in all cases under Windows).
340  * It's okay to disable the notifier because we never enter the Tcl event loop
341  * from Postgres, so the notifier capabilities are initialized, but never
342  * used. Only InitNotifier and DeleteFileHandler ever seem to get called
343  * within Postgres, but we implement all the functions for completeness.
344  */
345 static ClientData
347 {
348  static int fakeThreadKey; /* To give valid address for ClientData */
349 
350  return (ClientData) &(fakeThreadKey);
351 }
352 
353 static void
354 pltcl_FinalizeNotifier(ClientData clientData)
355 {
356 }
357 
358 static void
359 pltcl_SetTimer(CONST86 Tcl_Time *timePtr)
360 {
361 }
362 
363 static void
364 pltcl_AlertNotifier(ClientData clientData)
365 {
366 }
367 
368 static void
370  Tcl_FileProc *proc, ClientData clientData)
371 {
372 }
373 
374 static void
376 {
377 }
378 
379 static void
381 {
382 }
383 
384 static int
385 pltcl_WaitForEvent(CONST86 Tcl_Time *timePtr)
386 {
387  return 0;
388 }
389 
390 
391 /*
392  * _PG_init() - library load-time initialization
393  *
394  * DO NOT make this static nor change its name!
395  *
396  * The work done here must be safe to do in the postmaster process,
397  * in case the pltcl library is preloaded in the postmaster.
398  */
399 void
400 _PG_init(void)
401 {
402  Tcl_NotifierProcs notifier;
403  HASHCTL hash_ctl;
404 
405  /* Be sure we do initialization only once (should be redundant now) */
406  if (pltcl_pm_init_done)
407  return;
408 
410 
411 #ifdef WIN32
412  /* Required on win32 to prevent error loading init.tcl */
413  Tcl_FindExecutable("");
414 #endif
415 
416  /*
417  * Override the functions in the Notifier subsystem. See comments above.
418  */
419  notifier.setTimerProc = pltcl_SetTimer;
420  notifier.waitForEventProc = pltcl_WaitForEvent;
421  notifier.createFileHandlerProc = pltcl_CreateFileHandler;
422  notifier.deleteFileHandlerProc = pltcl_DeleteFileHandler;
423  notifier.initNotifierProc = pltcl_InitNotifier;
424  notifier.finalizeNotifierProc = pltcl_FinalizeNotifier;
425  notifier.alertNotifierProc = pltcl_AlertNotifier;
426  notifier.serviceModeHookProc = pltcl_ServiceModeHook;
427  Tcl_SetNotifier(&notifier);
428 
429  /************************************************************
430  * Create the dummy hold interpreter to prevent close of
431  * stdout and stderr on DeleteInterp
432  ************************************************************/
433  if ((pltcl_hold_interp = Tcl_CreateInterp()) == NULL)
434  elog(ERROR, "could not create master Tcl interpreter");
435  if (Tcl_Init(pltcl_hold_interp) == TCL_ERROR)
436  elog(ERROR, "could not initialize master Tcl interpreter");
437 
438  /************************************************************
439  * Create the hash table for working interpreters
440  ************************************************************/
441  memset(&hash_ctl, 0, sizeof(hash_ctl));
442  hash_ctl.keysize = sizeof(Oid);
443  hash_ctl.entrysize = sizeof(pltcl_interp_desc);
444  pltcl_interp_htab = hash_create("PL/Tcl interpreters",
445  8,
446  &hash_ctl,
448 
449  /************************************************************
450  * Create the hash table for function lookup
451  ************************************************************/
452  memset(&hash_ctl, 0, sizeof(hash_ctl));
453  hash_ctl.keysize = sizeof(pltcl_proc_key);
454  hash_ctl.entrysize = sizeof(pltcl_proc_ptr);
455  pltcl_proc_htab = hash_create("PL/Tcl functions",
456  100,
457  &hash_ctl,
459 
460  /************************************************************
461  * Define PL/Tcl's custom GUCs
462  ************************************************************/
463  DefineCustomStringVariable("pltcl.start_proc",
464  gettext_noop("PL/Tcl function to call once when pltcl is first used."),
465  NULL,
467  NULL,
468  PGC_SUSET, 0,
469  NULL, NULL, NULL);
470  DefineCustomStringVariable("pltclu.start_proc",
471  gettext_noop("PL/TclU function to call once when pltclu is first used."),
472  NULL,
474  NULL,
475  PGC_SUSET, 0,
476  NULL, NULL, NULL);
477 
478  pltcl_pm_init_done = true;
479 }
480 
481 /**********************************************************************
482  * pltcl_init_interp() - initialize a new Tcl interpreter
483  **********************************************************************/
484 static void
485 pltcl_init_interp(pltcl_interp_desc *interp_desc, Oid prolang, bool pltrusted)
486 {
487  Tcl_Interp *interp;
488  char interpname[32];
489 
490  /************************************************************
491  * Create the Tcl interpreter as a slave of pltcl_hold_interp.
492  * Note: Tcl automatically does Tcl_Init in the untrusted case,
493  * and it's not wanted in the trusted case.
494  ************************************************************/
495  snprintf(interpname, sizeof(interpname), "slave_%u", interp_desc->user_id);
496  if ((interp = Tcl_CreateSlave(pltcl_hold_interp, interpname,
497  pltrusted ? 1 : 0)) == NULL)
498  elog(ERROR, "could not create slave Tcl interpreter");
499 
500  /************************************************************
501  * Initialize the query hash table associated with interpreter
502  ************************************************************/
503  Tcl_InitHashTable(&interp_desc->query_hash, TCL_STRING_KEYS);
504 
505  /************************************************************
506  * Install the commands for SPI support in the interpreter
507  ************************************************************/
508  Tcl_CreateObjCommand(interp, "elog",
509  pltcl_elog, NULL, NULL);
510  Tcl_CreateObjCommand(interp, "quote",
511  pltcl_quote, NULL, NULL);
512  Tcl_CreateObjCommand(interp, "argisnull",
513  pltcl_argisnull, NULL, NULL);
514  Tcl_CreateObjCommand(interp, "return_null",
515  pltcl_returnnull, NULL, NULL);
516  Tcl_CreateObjCommand(interp, "return_next",
517  pltcl_returnnext, NULL, NULL);
518  Tcl_CreateObjCommand(interp, "spi_exec",
519  pltcl_SPI_execute, NULL, NULL);
520  Tcl_CreateObjCommand(interp, "spi_prepare",
521  pltcl_SPI_prepare, NULL, NULL);
522  Tcl_CreateObjCommand(interp, "spi_execp",
523  pltcl_SPI_execute_plan, NULL, NULL);
524  Tcl_CreateObjCommand(interp, "subtransaction",
525  pltcl_subtransaction, NULL, NULL);
526  Tcl_CreateObjCommand(interp, "commit",
527  pltcl_commit, NULL, NULL);
528  Tcl_CreateObjCommand(interp, "rollback",
529  pltcl_rollback, NULL, NULL);
530 
531  /************************************************************
532  * Call the appropriate start_proc, if there is one.
533  *
534  * We must set interp_desc->interp before the call, else the start_proc
535  * won't find the interpreter it's supposed to use. But, if the
536  * start_proc fails, we want to abandon use of the interpreter.
537  ************************************************************/
538  PG_TRY();
539  {
540  interp_desc->interp = interp;
541  call_pltcl_start_proc(prolang, pltrusted);
542  }
543  PG_CATCH();
544  {
545  interp_desc->interp = NULL;
546  Tcl_DeleteInterp(interp);
547  PG_RE_THROW();
548  }
549  PG_END_TRY();
550 }
551 
552 /**********************************************************************
553  * pltcl_fetch_interp() - fetch the Tcl interpreter to use for a function
554  *
555  * This also takes care of any on-first-use initialization required.
556  **********************************************************************/
557 static pltcl_interp_desc *
558 pltcl_fetch_interp(Oid prolang, bool pltrusted)
559 {
560  Oid user_id;
561  pltcl_interp_desc *interp_desc;
562  bool found;
563 
564  /* Find or create the interpreter hashtable entry for this userid */
565  if (pltrusted)
566  user_id = GetUserId();
567  else
568  user_id = InvalidOid;
569 
570  interp_desc = hash_search(pltcl_interp_htab, &user_id,
571  HASH_ENTER,
572  &found);
573  if (!found)
574  interp_desc->interp = NULL;
575 
576  /* If we haven't yet successfully made an interpreter, try to do that */
577  if (!interp_desc->interp)
578  pltcl_init_interp(interp_desc, prolang, pltrusted);
579 
580  return interp_desc;
581 }
582 
583 
584 /**********************************************************************
585  * call_pltcl_start_proc() - Call user-defined initialization proc, if any
586  **********************************************************************/
587 static void
588 call_pltcl_start_proc(Oid prolang, bool pltrusted)
589 {
590  LOCAL_FCINFO(fcinfo, 0);
591  char *start_proc;
592  const char *gucname;
593  ErrorContextCallback errcallback;
594  List *namelist;
595  Oid fargtypes[1]; /* dummy */
596  Oid procOid;
597  HeapTuple procTup;
598  Form_pg_proc procStruct;
599  AclResult aclresult;
600  FmgrInfo finfo;
601  PgStat_FunctionCallUsage fcusage;
602 
603  /* select appropriate GUC */
604  start_proc = pltrusted ? pltcl_start_proc : pltclu_start_proc;
605  gucname = pltrusted ? "pltcl.start_proc" : "pltclu.start_proc";
606 
607  /* Nothing to do if it's empty or unset */
608  if (start_proc == NULL || start_proc[0] == '\0')
609  return;
610 
611  /* Set up errcontext callback to make errors more helpful */
612  errcallback.callback = start_proc_error_callback;
613  errcallback.arg = unconstify(char *, gucname);
614  errcallback.previous = error_context_stack;
615  error_context_stack = &errcallback;
616 
617  /* Parse possibly-qualified identifier and look up the function */
618  namelist = stringToQualifiedNameList(start_proc);
619  procOid = LookupFuncName(namelist, 0, fargtypes, false);
620 
621  /* Current user must have permission to call function */
622  aclresult = pg_proc_aclcheck(procOid, GetUserId(), ACL_EXECUTE);
623  if (aclresult != ACLCHECK_OK)
624  aclcheck_error(aclresult, OBJECT_FUNCTION, start_proc);
625 
626  /* Get the function's pg_proc entry */
627  procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(procOid));
628  if (!HeapTupleIsValid(procTup))
629  elog(ERROR, "cache lookup failed for function %u", procOid);
630  procStruct = (Form_pg_proc) GETSTRUCT(procTup);
631 
632  /* It must be same language as the function we're currently calling */
633  if (procStruct->prolang != prolang)
634  ereport(ERROR,
635  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
636  errmsg("function \"%s\" is in the wrong language",
637  start_proc)));
638 
639  /*
640  * It must not be SECURITY DEFINER, either. This together with the
641  * language match check ensures that the function will execute in the same
642  * Tcl interpreter we just finished initializing.
643  */
644  if (procStruct->prosecdef)
645  ereport(ERROR,
646  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
647  errmsg("function \"%s\" must not be SECURITY DEFINER",
648  start_proc)));
649 
650  /* A-OK */
651  ReleaseSysCache(procTup);
652 
653  /*
654  * Call the function using the normal SQL function call mechanism. We
655  * could perhaps cheat and jump directly to pltcl_handler(), but it seems
656  * better to do it this way so that the call is exposed to, eg, call
657  * statistics collection.
658  */
659  InvokeFunctionExecuteHook(procOid);
660  fmgr_info(procOid, &finfo);
661  InitFunctionCallInfoData(*fcinfo, &finfo,
662  0,
663  InvalidOid, NULL, NULL);
664  pgstat_init_function_usage(fcinfo, &fcusage);
665  (void) FunctionCallInvoke(fcinfo);
666  pgstat_end_function_usage(&fcusage, true);
667 
668  /* Pop the error context stack */
669  error_context_stack = errcallback.previous;
670 }
671 
672 /*
673  * Error context callback for errors occurring during start_proc processing.
674  */
675 static void
677 {
678  const char *gucname = (const char *) arg;
679 
680  /* translator: %s is "pltcl.start_proc" or "pltclu.start_proc" */
681  errcontext("processing %s parameter", gucname);
682 }
683 
684 
685 /**********************************************************************
686  * pltcl_call_handler - This is the only visible function
687  * of the PL interpreter. The PostgreSQL
688  * function manager and trigger manager
689  * call this function for execution of
690  * PL/Tcl procedures.
691  **********************************************************************/
693 
694 /* keep non-static */
695 Datum
697 {
698  return pltcl_handler(fcinfo, true);
699 }
700 
701 /*
702  * Alternative handler for unsafe functions
703  */
705 
706 /* keep non-static */
707 Datum
709 {
710  return pltcl_handler(fcinfo, false);
711 }
712 
713 
714 /**********************************************************************
715  * pltcl_handler() - Handler for function and trigger calls, for
716  * both trusted and untrusted interpreters.
717  **********************************************************************/
718 static Datum
720 {
721  Datum retval;
722  pltcl_call_state current_call_state;
723  pltcl_call_state *save_call_state;
724 
725  /*
726  * Initialize current_call_state to nulls/zeroes; in particular, set its
727  * prodesc pointer to null. Anything that sets it non-null should
728  * increase the prodesc's fn_refcount at the same time. We'll decrease
729  * the refcount, and then delete the prodesc if it's no longer referenced,
730  * on the way out of this function. This ensures that prodescs live as
731  * long as needed even if somebody replaces the originating pg_proc row
732  * while they're executing.
733  */
734  memset(&current_call_state, 0, sizeof(current_call_state));
735 
736  /*
737  * Ensure that static pointer is saved/restored properly
738  */
739  save_call_state = pltcl_current_call_state;
740  pltcl_current_call_state = &current_call_state;
741 
742  PG_TRY();
743  {
744  /*
745  * Determine if called as function or trigger and call appropriate
746  * subhandler
747  */
748  if (CALLED_AS_TRIGGER(fcinfo))
749  {
750  /* invoke the trigger handler */
751  retval = PointerGetDatum(pltcl_trigger_handler(fcinfo,
752  &current_call_state,
753  pltrusted));
754  }
755  else if (CALLED_AS_EVENT_TRIGGER(fcinfo))
756  {
757  /* invoke the event trigger handler */
758  pltcl_event_trigger_handler(fcinfo, &current_call_state, pltrusted);
759  retval = (Datum) 0;
760  }
761  else
762  {
763  /* invoke the regular function handler */
764  current_call_state.fcinfo = fcinfo;
765  retval = pltcl_func_handler(fcinfo, &current_call_state, pltrusted);
766  }
767  }
768  PG_CATCH();
769  {
770  /* Restore static pointer, then clean up the prodesc refcount if any */
771  pltcl_current_call_state = save_call_state;
772  if (current_call_state.prodesc != NULL)
773  {
774  Assert(current_call_state.prodesc->fn_refcount > 0);
775  if (--current_call_state.prodesc->fn_refcount == 0)
776  MemoryContextDelete(current_call_state.prodesc->fn_cxt);
777  }
778  PG_RE_THROW();
779  }
780  PG_END_TRY();
781 
782  /* Restore static pointer, then clean up the prodesc refcount if any */
783  /* (We're being paranoid in case an error is thrown in context deletion) */
784  pltcl_current_call_state = save_call_state;
785  if (current_call_state.prodesc != NULL)
786  {
787  Assert(current_call_state.prodesc->fn_refcount > 0);
788  if (--current_call_state.prodesc->fn_refcount == 0)
789  MemoryContextDelete(current_call_state.prodesc->fn_cxt);
790  }
791 
792  return retval;
793 }
794 
795 
796 /**********************************************************************
797  * pltcl_func_handler() - Handler for regular function calls
798  **********************************************************************/
799 static Datum
801  bool pltrusted)
802 {
803  bool nonatomic;
804  pltcl_proc_desc *prodesc;
805  Tcl_Interp *volatile interp;
806  Tcl_Obj *tcl_cmd;
807  int i;
808  int tcl_rc;
809  Datum retval;
810 
811  nonatomic = fcinfo->context &&
812  IsA(fcinfo->context, CallContext) &&
813  !castNode(CallContext, fcinfo->context)->atomic;
814 
815  /* Connect to SPI manager */
816  if (SPI_connect_ext(nonatomic ? SPI_OPT_NONATOMIC : 0) != SPI_OK_CONNECT)
817  elog(ERROR, "could not connect to SPI manager");
818 
819  /* Find or compile the function */
820  prodesc = compile_pltcl_function(fcinfo->flinfo->fn_oid, InvalidOid,
821  false, pltrusted);
822 
823  call_state->prodesc = prodesc;
824  prodesc->fn_refcount++;
825 
826  interp = prodesc->interp_desc->interp;
827 
828  /*
829  * If we're a SRF, check caller can handle materialize mode, and save
830  * relevant info into call_state. We must ensure that the returned
831  * tuplestore is owned by the caller's context, even if we first create it
832  * inside a subtransaction.
833  */
834  if (prodesc->fn_retisset)
835  {
836  ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
837 
838  if (!rsi || !IsA(rsi, ReturnSetInfo) ||
839  (rsi->allowedModes & SFRM_Materialize) == 0)
840  ereport(ERROR,
841  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
842  errmsg("set-valued function called in context that cannot accept a set")));
843 
844  call_state->rsi = rsi;
845  call_state->tuple_store_cxt = rsi->econtext->ecxt_per_query_memory;
847  }
848 
849  /************************************************************
850  * Create the tcl command to call the internal
851  * proc in the Tcl interpreter
852  ************************************************************/
853  tcl_cmd = Tcl_NewObj();
854  Tcl_ListObjAppendElement(NULL, tcl_cmd,
855  Tcl_NewStringObj(prodesc->internal_proname, -1));
856 
857  /* We hold a refcount on tcl_cmd just to be sure it stays around */
858  Tcl_IncrRefCount(tcl_cmd);
859 
860  /************************************************************
861  * Add all call arguments to the command
862  ************************************************************/
863  PG_TRY();
864  {
865  for (i = 0; i < prodesc->nargs; i++)
866  {
867  if (prodesc->arg_is_rowtype[i])
868  {
869  /**************************************************
870  * For tuple values, add a list for 'array set ...'
871  **************************************************/
872  if (fcinfo->args[i].isnull)
873  Tcl_ListObjAppendElement(NULL, tcl_cmd, Tcl_NewObj());
874  else
875  {
876  HeapTupleHeader td;
877  Oid tupType;
878  int32 tupTypmod;
879  TupleDesc tupdesc;
880  HeapTupleData tmptup;
881  Tcl_Obj *list_tmp;
882 
883  td = DatumGetHeapTupleHeader(fcinfo->args[i].value);
884  /* Extract rowtype info and find a tupdesc */
885  tupType = HeapTupleHeaderGetTypeId(td);
886  tupTypmod = HeapTupleHeaderGetTypMod(td);
887  tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
888  /* Build a temporary HeapTuple control structure */
890  tmptup.t_data = td;
891 
892  list_tmp = pltcl_build_tuple_argument(&tmptup, tupdesc, true);
893  Tcl_ListObjAppendElement(NULL, tcl_cmd, list_tmp);
894 
895  ReleaseTupleDesc(tupdesc);
896  }
897  }
898  else
899  {
900  /**************************************************
901  * Single values are added as string element
902  * of their external representation
903  **************************************************/
904  if (fcinfo->args[i].isnull)
905  Tcl_ListObjAppendElement(NULL, tcl_cmd, Tcl_NewObj());
906  else
907  {
908  char *tmp;
909 
910  tmp = OutputFunctionCall(&prodesc->arg_out_func[i],
911  fcinfo->args[i].value);
912  UTF_BEGIN;
913  Tcl_ListObjAppendElement(NULL, tcl_cmd,
914  Tcl_NewStringObj(UTF_E2U(tmp), -1));
915  UTF_END;
916  pfree(tmp);
917  }
918  }
919  }
920  }
921  PG_CATCH();
922  {
923  /* Release refcount to free tcl_cmd */
924  Tcl_DecrRefCount(tcl_cmd);
925  PG_RE_THROW();
926  }
927  PG_END_TRY();
928 
929  /************************************************************
930  * Call the Tcl function
931  *
932  * We assume no PG error can be thrown directly from this call.
933  ************************************************************/
934  tcl_rc = Tcl_EvalObjEx(interp, tcl_cmd, (TCL_EVAL_DIRECT | TCL_EVAL_GLOBAL));
935 
936  /* Release refcount to free tcl_cmd (and all subsidiary objects) */
937  Tcl_DecrRefCount(tcl_cmd);
938 
939  /************************************************************
940  * Check for errors reported by Tcl.
941  ************************************************************/
942  if (tcl_rc != TCL_OK)
943  throw_tcl_error(interp, prodesc->user_proname);
944 
945  /************************************************************
946  * Disconnect from SPI manager and then create the return
947  * value datum (if the input function does a palloc for it
948  * this must not be allocated in the SPI memory context
949  * because SPI_finish would free it). But don't try to call
950  * the result_in_func if we've been told to return a NULL;
951  * the Tcl result may not be a valid value of the result type
952  * in that case.
953  ************************************************************/
954  if (SPI_finish() != SPI_OK_FINISH)
955  elog(ERROR, "SPI_finish() failed");
956 
957  if (prodesc->fn_retisset)
958  {
959  ReturnSetInfo *rsi = call_state->rsi;
960 
961  /* We already checked this is OK */
963 
964  /* If we produced any tuples, send back the result */
965  if (call_state->tuple_store)
966  {
967  rsi->setResult = call_state->tuple_store;
968  if (call_state->ret_tupdesc)
969  {
970  MemoryContext oldcxt;
971 
972  oldcxt = MemoryContextSwitchTo(call_state->tuple_store_cxt);
973  rsi->setDesc = CreateTupleDescCopy(call_state->ret_tupdesc);
974  MemoryContextSwitchTo(oldcxt);
975  }
976  }
977  retval = (Datum) 0;
978  fcinfo->isnull = true;
979  }
980  else if (fcinfo->isnull)
981  {
982  retval = InputFunctionCall(&prodesc->result_in_func,
983  NULL,
984  prodesc->result_typioparam,
985  -1);
986  }
987  else if (prodesc->fn_retistuple)
988  {
989  TupleDesc td;
990  HeapTuple tup;
991  Tcl_Obj *resultObj;
992  Tcl_Obj **resultObjv;
993  int resultObjc;
994 
995  /*
996  * Set up data about result type. XXX it's tempting to consider
997  * caching this in the prodesc, in the common case where the rowtype
998  * is determined by the function not the calling query. But we'd have
999  * to be able to deal with ADD/DROP/ALTER COLUMN events when the
1000  * result type is a named composite type, so it's not exactly trivial.
1001  * Maybe worth improving someday.
1002  */
1003  switch (get_call_result_type(fcinfo, NULL, &td))
1004  {
1005  case TYPEFUNC_COMPOSITE:
1006  /* success */
1007  break;
1009  Assert(prodesc->fn_retisdomain);
1010  break;
1011  case TYPEFUNC_RECORD:
1012  /* failed to determine actual type of RECORD */
1013  ereport(ERROR,
1014  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1015  errmsg("function returning record called in context "
1016  "that cannot accept type record")));
1017  break;
1018  default:
1019  /* result type isn't composite? */
1020  elog(ERROR, "return type must be a row type");
1021  break;
1022  }
1023 
1024  Assert(!call_state->ret_tupdesc);
1025  Assert(!call_state->attinmeta);
1026  call_state->ret_tupdesc = td;
1027  call_state->attinmeta = TupleDescGetAttInMetadata(td);
1028 
1029  /* Convert function result to tuple */
1030  resultObj = Tcl_GetObjResult(interp);
1031  if (Tcl_ListObjGetElements(interp, resultObj, &resultObjc, &resultObjv) == TCL_ERROR)
1032  throw_tcl_error(interp, prodesc->user_proname);
1033 
1034  tup = pltcl_build_tuple_result(interp, resultObjv, resultObjc,
1035  call_state);
1036  retval = HeapTupleGetDatum(tup);
1037  }
1038  else
1039  retval = InputFunctionCall(&prodesc->result_in_func,
1040  utf_u2e(Tcl_GetStringResult(interp)),
1041  prodesc->result_typioparam,
1042  -1);
1043 
1044  return retval;
1045 }
1046 
1047 
1048 /**********************************************************************
1049  * pltcl_trigger_handler() - Handler for trigger calls
1050  **********************************************************************/
1051 static HeapTuple
1053  bool pltrusted)
1054 {
1055  pltcl_proc_desc *prodesc;
1056  Tcl_Interp *volatile interp;
1057  TriggerData *trigdata = (TriggerData *) fcinfo->context;
1058  char *stroid;
1059  TupleDesc tupdesc;
1060  volatile HeapTuple rettup;
1061  Tcl_Obj *tcl_cmd;
1062  Tcl_Obj *tcl_trigtup;
1063  int tcl_rc;
1064  int i;
1065  const char *result;
1066  int result_Objc;
1067  Tcl_Obj **result_Objv;
1068  int rc PG_USED_FOR_ASSERTS_ONLY;
1069 
1070  call_state->trigdata = trigdata;
1071 
1072  /* Connect to SPI manager */
1073  if (SPI_connect() != SPI_OK_CONNECT)
1074  elog(ERROR, "could not connect to SPI manager");
1075 
1076  /* Make transition tables visible to this SPI connection */
1077  rc = SPI_register_trigger_data(trigdata);
1078  Assert(rc >= 0);
1079 
1080  /* Find or compile the function */
1081  prodesc = compile_pltcl_function(fcinfo->flinfo->fn_oid,
1082  RelationGetRelid(trigdata->tg_relation),
1083  false, /* not an event trigger */
1084  pltrusted);
1085 
1086  call_state->prodesc = prodesc;
1087  prodesc->fn_refcount++;
1088 
1089  interp = prodesc->interp_desc->interp;
1090 
1091  tupdesc = RelationGetDescr(trigdata->tg_relation);
1092 
1093  /************************************************************
1094  * Create the tcl command to call the internal
1095  * proc in the interpreter
1096  ************************************************************/
1097  tcl_cmd = Tcl_NewObj();
1098  Tcl_IncrRefCount(tcl_cmd);
1099 
1100  PG_TRY();
1101  {
1102  /* The procedure name (note this is all ASCII, so no utf_e2u) */
1103  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1104  Tcl_NewStringObj(prodesc->internal_proname, -1));
1105 
1106  /* The trigger name for argument TG_name */
1107  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1108  Tcl_NewStringObj(utf_e2u(trigdata->tg_trigger->tgname), -1));
1109 
1110  /* The oid of the trigger relation for argument TG_relid */
1111  /* Consider not converting to a string for more performance? */
1113  ObjectIdGetDatum(trigdata->tg_relation->rd_id)));
1114  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1115  Tcl_NewStringObj(stroid, -1));
1116  pfree(stroid);
1117 
1118  /* The name of the table the trigger is acting on: TG_table_name */
1119  stroid = SPI_getrelname(trigdata->tg_relation);
1120  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1121  Tcl_NewStringObj(utf_e2u(stroid), -1));
1122  pfree(stroid);
1123 
1124  /* The schema of the table the trigger is acting on: TG_table_schema */
1125  stroid = SPI_getnspname(trigdata->tg_relation);
1126  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1127  Tcl_NewStringObj(utf_e2u(stroid), -1));
1128  pfree(stroid);
1129 
1130  /* A list of attribute names for argument TG_relatts */
1131  tcl_trigtup = Tcl_NewObj();
1132  Tcl_ListObjAppendElement(NULL, tcl_trigtup, Tcl_NewObj());
1133  for (i = 0; i < tupdesc->natts; i++)
1134  {
1135  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
1136 
1137  if (att->attisdropped)
1138  Tcl_ListObjAppendElement(NULL, tcl_trigtup, Tcl_NewObj());
1139  else
1140  Tcl_ListObjAppendElement(NULL, tcl_trigtup,
1141  Tcl_NewStringObj(utf_e2u(NameStr(att->attname)), -1));
1142  }
1143  Tcl_ListObjAppendElement(NULL, tcl_cmd, tcl_trigtup);
1144 
1145  /* The when part of the event for TG_when */
1146  if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
1147  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1148  Tcl_NewStringObj("BEFORE", -1));
1149  else if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
1150  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1151  Tcl_NewStringObj("AFTER", -1));
1152  else if (TRIGGER_FIRED_INSTEAD(trigdata->tg_event))
1153  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1154  Tcl_NewStringObj("INSTEAD OF", -1));
1155  else
1156  elog(ERROR, "unrecognized WHEN tg_event: %u", trigdata->tg_event);
1157 
1158  /* The level part of the event for TG_level */
1159  if (TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
1160  {
1161  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1162  Tcl_NewStringObj("ROW", -1));
1163 
1164  /*
1165  * Now the command part of the event for TG_op and data for NEW
1166  * and OLD
1167  *
1168  * Note: In BEFORE trigger, stored generated columns are not
1169  * computed yet, so don't make them accessible in NEW row.
1170  */
1171  if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
1172  {
1173  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1174  Tcl_NewStringObj("INSERT", -1));
1175 
1176  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1178  tupdesc,
1179  !TRIGGER_FIRED_BEFORE(trigdata->tg_event)));
1180  Tcl_ListObjAppendElement(NULL, tcl_cmd, Tcl_NewObj());
1181 
1182  rettup = trigdata->tg_trigtuple;
1183  }
1184  else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
1185  {
1186  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1187  Tcl_NewStringObj("DELETE", -1));
1188 
1189  Tcl_ListObjAppendElement(NULL, tcl_cmd, Tcl_NewObj());
1190  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1192  tupdesc,
1193  true));
1194 
1195  rettup = trigdata->tg_trigtuple;
1196  }
1197  else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
1198  {
1199  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1200  Tcl_NewStringObj("UPDATE", -1));
1201 
1202  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1204  tupdesc,
1205  !TRIGGER_FIRED_BEFORE(trigdata->tg_event)));
1206  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1208  tupdesc,
1209  true));
1210 
1211  rettup = trigdata->tg_newtuple;
1212  }
1213  else
1214  elog(ERROR, "unrecognized OP tg_event: %u", trigdata->tg_event);
1215  }
1216  else if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
1217  {
1218  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1219  Tcl_NewStringObj("STATEMENT", -1));
1220 
1221  if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
1222  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1223  Tcl_NewStringObj("INSERT", -1));
1224  else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
1225  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1226  Tcl_NewStringObj("DELETE", -1));
1227  else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
1228  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1229  Tcl_NewStringObj("UPDATE", -1));
1230  else if (TRIGGER_FIRED_BY_TRUNCATE(trigdata->tg_event))
1231  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1232  Tcl_NewStringObj("TRUNCATE", -1));
1233  else
1234  elog(ERROR, "unrecognized OP tg_event: %u", trigdata->tg_event);
1235 
1236  Tcl_ListObjAppendElement(NULL, tcl_cmd, Tcl_NewObj());
1237  Tcl_ListObjAppendElement(NULL, tcl_cmd, Tcl_NewObj());
1238 
1239  rettup = (HeapTuple) NULL;
1240  }
1241  else
1242  elog(ERROR, "unrecognized LEVEL tg_event: %u", trigdata->tg_event);
1243 
1244  /* Finally append the arguments from CREATE TRIGGER */
1245  for (i = 0; i < trigdata->tg_trigger->tgnargs; i++)
1246  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1247  Tcl_NewStringObj(utf_e2u(trigdata->tg_trigger->tgargs[i]), -1));
1248 
1249  }
1250  PG_CATCH();
1251  {
1252  Tcl_DecrRefCount(tcl_cmd);
1253  PG_RE_THROW();
1254  }
1255  PG_END_TRY();
1256 
1257  /************************************************************
1258  * Call the Tcl function
1259  *
1260  * We assume no PG error can be thrown directly from this call.
1261  ************************************************************/
1262  tcl_rc = Tcl_EvalObjEx(interp, tcl_cmd, (TCL_EVAL_DIRECT | TCL_EVAL_GLOBAL));
1263 
1264  /* Release refcount to free tcl_cmd (and all subsidiary objects) */
1265  Tcl_DecrRefCount(tcl_cmd);
1266 
1267  /************************************************************
1268  * Check for errors reported by Tcl.
1269  ************************************************************/
1270  if (tcl_rc != TCL_OK)
1271  throw_tcl_error(interp, prodesc->user_proname);
1272 
1273  /************************************************************
1274  * Exit SPI environment.
1275  ************************************************************/
1276  if (SPI_finish() != SPI_OK_FINISH)
1277  elog(ERROR, "SPI_finish() failed");
1278 
1279  /************************************************************
1280  * The return value from the procedure might be one of
1281  * the magic strings OK or SKIP, or a list from array get.
1282  * We can check for OK or SKIP without worrying about encoding.
1283  ************************************************************/
1284  result = Tcl_GetStringResult(interp);
1285 
1286  if (strcmp(result, "OK") == 0)
1287  return rettup;
1288  if (strcmp(result, "SKIP") == 0)
1289  return (HeapTuple) NULL;
1290 
1291  /************************************************************
1292  * Otherwise, the return value should be a column name/value list
1293  * specifying the modified tuple to return.
1294  ************************************************************/
1295  if (Tcl_ListObjGetElements(interp, Tcl_GetObjResult(interp),
1296  &result_Objc, &result_Objv) != TCL_OK)
1297  ereport(ERROR,
1298  (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
1299  errmsg("could not split return value from trigger: %s",
1300  utf_u2e(Tcl_GetStringResult(interp)))));
1301 
1302  /* Convert function result to tuple */
1303  rettup = pltcl_build_tuple_result(interp, result_Objv, result_Objc,
1304  call_state);
1305 
1306  return rettup;
1307 }
1308 
1309 /**********************************************************************
1310  * pltcl_event_trigger_handler() - Handler for event trigger calls
1311  **********************************************************************/
1312 static void
1314  bool pltrusted)
1315 {
1316  pltcl_proc_desc *prodesc;
1317  Tcl_Interp *volatile interp;
1318  EventTriggerData *tdata = (EventTriggerData *) fcinfo->context;
1319  Tcl_Obj *tcl_cmd;
1320  int tcl_rc;
1321 
1322  /* Connect to SPI manager */
1323  if (SPI_connect() != SPI_OK_CONNECT)
1324  elog(ERROR, "could not connect to SPI manager");
1325 
1326  /* Find or compile the function */
1327  prodesc = compile_pltcl_function(fcinfo->flinfo->fn_oid,
1328  InvalidOid, true, pltrusted);
1329 
1330  call_state->prodesc = prodesc;
1331  prodesc->fn_refcount++;
1332 
1333  interp = prodesc->interp_desc->interp;
1334 
1335  /* Create the tcl command and call the internal proc */
1336  tcl_cmd = Tcl_NewObj();
1337  Tcl_IncrRefCount(tcl_cmd);
1338  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1339  Tcl_NewStringObj(prodesc->internal_proname, -1));
1340  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1341  Tcl_NewStringObj(utf_e2u(tdata->event), -1));
1342  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1343  Tcl_NewStringObj(utf_e2u(tdata->tag), -1));
1344 
1345  tcl_rc = Tcl_EvalObjEx(interp, tcl_cmd, (TCL_EVAL_DIRECT | TCL_EVAL_GLOBAL));
1346 
1347  /* Release refcount to free tcl_cmd (and all subsidiary objects) */
1348  Tcl_DecrRefCount(tcl_cmd);
1349 
1350  /* Check for errors reported by Tcl. */
1351  if (tcl_rc != TCL_OK)
1352  throw_tcl_error(interp, prodesc->user_proname);
1353 
1354  if (SPI_finish() != SPI_OK_FINISH)
1355  elog(ERROR, "SPI_finish() failed");
1356 }
1357 
1358 
1359 /**********************************************************************
1360  * throw_tcl_error - ereport an error returned from the Tcl interpreter
1361  **********************************************************************/
1362 static void
1363 throw_tcl_error(Tcl_Interp *interp, const char *proname)
1364 {
1365  /*
1366  * Caution is needed here because Tcl_GetVar could overwrite the
1367  * interpreter result (even though it's not really supposed to), and we
1368  * can't control the order of evaluation of ereport arguments. Hence, make
1369  * real sure we have our own copy of the result string before invoking
1370  * Tcl_GetVar.
1371  */
1372  char *emsg;
1373  char *econtext;
1374 
1375  emsg = pstrdup(utf_u2e(Tcl_GetStringResult(interp)));
1376  econtext = utf_u2e(Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY));
1377  ereport(ERROR,
1378  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
1379  errmsg("%s", emsg),
1380  errcontext("%s\nin PL/Tcl function \"%s\"",
1381  econtext, proname)));
1382 }
1383 
1384 
1385 /**********************************************************************
1386  * compile_pltcl_function - compile (or hopefully just look up) function
1387  *
1388  * tgreloid is the OID of the relation when compiling a trigger, or zero
1389  * (InvalidOid) when compiling a plain function.
1390  **********************************************************************/
1391 static pltcl_proc_desc *
1392 compile_pltcl_function(Oid fn_oid, Oid tgreloid,
1393  bool is_event_trigger, bool pltrusted)
1394 {
1395  HeapTuple procTup;
1396  Form_pg_proc procStruct;
1397  pltcl_proc_key proc_key;
1398  pltcl_proc_ptr *proc_ptr;
1399  bool found;
1400  pltcl_proc_desc *prodesc;
1401  pltcl_proc_desc *old_prodesc;
1402  volatile MemoryContext proc_cxt = NULL;
1403  Tcl_DString proc_internal_def;
1404  Tcl_DString proc_internal_body;
1405 
1406  /* We'll need the pg_proc tuple in any case... */
1407  procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(fn_oid));
1408  if (!HeapTupleIsValid(procTup))
1409  elog(ERROR, "cache lookup failed for function %u", fn_oid);
1410  procStruct = (Form_pg_proc) GETSTRUCT(procTup);
1411 
1412  /*
1413  * Look up function in pltcl_proc_htab; if it's not there, create an entry
1414  * and set the entry's proc_ptr to NULL.
1415  */
1416  proc_key.proc_id = fn_oid;
1417  proc_key.is_trigger = OidIsValid(tgreloid);
1418  proc_key.user_id = pltrusted ? GetUserId() : InvalidOid;
1419 
1420  proc_ptr = hash_search(pltcl_proc_htab, &proc_key,
1421  HASH_ENTER,
1422  &found);
1423  if (!found)
1424  proc_ptr->proc_ptr = NULL;
1425 
1426  prodesc = proc_ptr->proc_ptr;
1427 
1428  /************************************************************
1429  * If it's present, must check whether it's still up to date.
1430  * This is needed because CREATE OR REPLACE FUNCTION can modify the
1431  * function's pg_proc entry without changing its OID.
1432  ************************************************************/
1433  if (prodesc != NULL &&
1434  prodesc->fn_xmin == HeapTupleHeaderGetRawXmin(procTup->t_data) &&
1435  ItemPointerEquals(&prodesc->fn_tid, &procTup->t_self))
1436  {
1437  /* It's still up-to-date, so we can use it */
1438  ReleaseSysCache(procTup);
1439  return prodesc;
1440  }
1441 
1442  /************************************************************
1443  * If we haven't found it in the hashtable, we analyze
1444  * the functions arguments and returntype and store
1445  * the in-/out-functions in the prodesc block and create
1446  * a new hashtable entry for it.
1447  *
1448  * Then we load the procedure into the Tcl interpreter.
1449  ************************************************************/
1450  Tcl_DStringInit(&proc_internal_def);
1451  Tcl_DStringInit(&proc_internal_body);
1452  PG_TRY();
1453  {
1454  bool is_trigger = OidIsValid(tgreloid);
1455  char internal_proname[128];
1456  HeapTuple typeTup;
1457  Form_pg_type typeStruct;
1458  char proc_internal_args[33 * FUNC_MAX_ARGS];
1459  Datum prosrcdatum;
1460  bool isnull;
1461  char *proc_source;
1462  char buf[48];
1463  Tcl_Interp *interp;
1464  int i;
1465  int tcl_rc;
1466  MemoryContext oldcontext;
1467 
1468  /************************************************************
1469  * Build our internal proc name from the function's Oid. Append
1470  * "_trigger" when appropriate to ensure the normal and trigger
1471  * cases are kept separate. Note name must be all-ASCII.
1472  ************************************************************/
1473  if (is_event_trigger)
1474  snprintf(internal_proname, sizeof(internal_proname),
1475  "__PLTcl_proc_%u_evttrigger", fn_oid);
1476  else if (is_trigger)
1477  snprintf(internal_proname, sizeof(internal_proname),
1478  "__PLTcl_proc_%u_trigger", fn_oid);
1479  else
1480  snprintf(internal_proname, sizeof(internal_proname),
1481  "__PLTcl_proc_%u", fn_oid);
1482 
1483  /************************************************************
1484  * Allocate a context that will hold all PG data for the procedure.
1485  ************************************************************/
1487  "PL/Tcl function",
1489 
1490  /************************************************************
1491  * Allocate and fill a new procedure description block.
1492  * struct prodesc and subsidiary data must all live in proc_cxt.
1493  ************************************************************/
1494  oldcontext = MemoryContextSwitchTo(proc_cxt);
1495  prodesc = (pltcl_proc_desc *) palloc0(sizeof(pltcl_proc_desc));
1496  prodesc->user_proname = pstrdup(NameStr(procStruct->proname));
1497  MemoryContextSetIdentifier(proc_cxt, prodesc->user_proname);
1498  prodesc->internal_proname = pstrdup(internal_proname);
1499  prodesc->fn_cxt = proc_cxt;
1500  prodesc->fn_refcount = 0;
1501  prodesc->fn_xmin = HeapTupleHeaderGetRawXmin(procTup->t_data);
1502  prodesc->fn_tid = procTup->t_self;
1503  prodesc->nargs = procStruct->pronargs;
1504  prodesc->arg_out_func = (FmgrInfo *) palloc0(prodesc->nargs * sizeof(FmgrInfo));
1505  prodesc->arg_is_rowtype = (bool *) palloc0(prodesc->nargs * sizeof(bool));
1506  MemoryContextSwitchTo(oldcontext);
1507 
1508  /* Remember if function is STABLE/IMMUTABLE */
1509  prodesc->fn_readonly =
1510  (procStruct->provolatile != PROVOLATILE_VOLATILE);
1511  /* And whether it is trusted */
1512  prodesc->lanpltrusted = pltrusted;
1513 
1514  /************************************************************
1515  * Identify the interpreter to use for the function
1516  ************************************************************/
1517  prodesc->interp_desc = pltcl_fetch_interp(procStruct->prolang,
1518  prodesc->lanpltrusted);
1519  interp = prodesc->interp_desc->interp;
1520 
1521  /************************************************************
1522  * Get the required information for input conversion of the
1523  * return value.
1524  ************************************************************/
1525  if (!is_trigger && !is_event_trigger)
1526  {
1527  Oid rettype = procStruct->prorettype;
1528 
1529  typeTup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(rettype));
1530  if (!HeapTupleIsValid(typeTup))
1531  elog(ERROR, "cache lookup failed for type %u", rettype);
1532  typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1533 
1534  /* Disallow pseudotype result, except VOID and RECORD */
1535  if (typeStruct->typtype == TYPTYPE_PSEUDO)
1536  {
1537  if (rettype == VOIDOID ||
1538  rettype == RECORDOID)
1539  /* okay */ ;
1540  else if (rettype == TRIGGEROID ||
1541  rettype == EVTTRIGGEROID)
1542  ereport(ERROR,
1543  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1544  errmsg("trigger functions can only be called as triggers")));
1545  else
1546  ereport(ERROR,
1547  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1548  errmsg("PL/Tcl functions cannot return type %s",
1549  format_type_be(rettype))));
1550  }
1551 
1552  prodesc->result_typid = rettype;
1553  fmgr_info_cxt(typeStruct->typinput,
1554  &(prodesc->result_in_func),
1555  proc_cxt);
1556  prodesc->result_typioparam = getTypeIOParam(typeTup);
1557 
1558  prodesc->fn_retisset = procStruct->proretset;
1559  prodesc->fn_retistuple = type_is_rowtype(rettype);
1560  prodesc->fn_retisdomain = (typeStruct->typtype == TYPTYPE_DOMAIN);
1561  prodesc->domain_info = NULL;
1562 
1563  ReleaseSysCache(typeTup);
1564  }
1565 
1566  /************************************************************
1567  * Get the required information for output conversion
1568  * of all procedure arguments, and set up argument naming info.
1569  ************************************************************/
1570  if (!is_trigger && !is_event_trigger)
1571  {
1572  proc_internal_args[0] = '\0';
1573  for (i = 0; i < prodesc->nargs; i++)
1574  {
1575  Oid argtype = procStruct->proargtypes.values[i];
1576 
1577  typeTup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(argtype));
1578  if (!HeapTupleIsValid(typeTup))
1579  elog(ERROR, "cache lookup failed for type %u", argtype);
1580  typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1581 
1582  /* Disallow pseudotype argument, except RECORD */
1583  if (typeStruct->typtype == TYPTYPE_PSEUDO &&
1584  argtype != RECORDOID)
1585  ereport(ERROR,
1586  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1587  errmsg("PL/Tcl functions cannot accept type %s",
1588  format_type_be(argtype))));
1589 
1590  if (type_is_rowtype(argtype))
1591  {
1592  prodesc->arg_is_rowtype[i] = true;
1593  snprintf(buf, sizeof(buf), "__PLTcl_Tup_%d", i + 1);
1594  }
1595  else
1596  {
1597  prodesc->arg_is_rowtype[i] = false;
1598  fmgr_info_cxt(typeStruct->typoutput,
1599  &(prodesc->arg_out_func[i]),
1600  proc_cxt);
1601  snprintf(buf, sizeof(buf), "%d", i + 1);
1602  }
1603 
1604  if (i > 0)
1605  strcat(proc_internal_args, " ");
1606  strcat(proc_internal_args, buf);
1607 
1608  ReleaseSysCache(typeTup);
1609  }
1610  }
1611  else if (is_trigger)
1612  {
1613  /* trigger procedure has fixed args */
1614  strcpy(proc_internal_args,
1615  "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");
1616  }
1617  else if (is_event_trigger)
1618  {
1619  /* event trigger procedure has fixed args */
1620  strcpy(proc_internal_args, "TG_event TG_tag");
1621  }
1622 
1623  /************************************************************
1624  * Create the tcl command to define the internal
1625  * procedure
1626  *
1627  * Leave this code as DString - performance is not critical here,
1628  * and we don't want to duplicate the knowledge of the Tcl quoting
1629  * rules that's embedded in Tcl_DStringAppendElement.
1630  ************************************************************/
1631  Tcl_DStringAppendElement(&proc_internal_def, "proc");
1632  Tcl_DStringAppendElement(&proc_internal_def, internal_proname);
1633  Tcl_DStringAppendElement(&proc_internal_def, proc_internal_args);
1634 
1635  /************************************************************
1636  * prefix procedure body with
1637  * upvar #0 <internal_proname> GD
1638  * and with appropriate setting of arguments
1639  ************************************************************/
1640  Tcl_DStringAppend(&proc_internal_body, "upvar #0 ", -1);
1641  Tcl_DStringAppend(&proc_internal_body, internal_proname, -1);
1642  Tcl_DStringAppend(&proc_internal_body, " GD\n", -1);
1643  if (is_trigger)
1644  {
1645  Tcl_DStringAppend(&proc_internal_body,
1646  "array set NEW $__PLTcl_Tup_NEW\n", -1);
1647  Tcl_DStringAppend(&proc_internal_body,
1648  "array set OLD $__PLTcl_Tup_OLD\n", -1);
1649  Tcl_DStringAppend(&proc_internal_body,
1650  "set i 0\n"
1651  "set v 0\n"
1652  "foreach v $args {\n"
1653  " incr i\n"
1654  " set $i $v\n"
1655  "}\n"
1656  "unset i v\n\n", -1);
1657  }
1658  else if (is_event_trigger)
1659  {
1660  /* no argument support for event triggers */
1661  }
1662  else
1663  {
1664  for (i = 0; i < prodesc->nargs; i++)
1665  {
1666  if (prodesc->arg_is_rowtype[i])
1667  {
1668  snprintf(buf, sizeof(buf),
1669  "array set %d $__PLTcl_Tup_%d\n",
1670  i + 1, i + 1);
1671  Tcl_DStringAppend(&proc_internal_body, buf, -1);
1672  }
1673  }
1674  }
1675 
1676  /************************************************************
1677  * Add user's function definition to proc body
1678  ************************************************************/
1679  prosrcdatum = SysCacheGetAttr(PROCOID, procTup,
1680  Anum_pg_proc_prosrc, &isnull);
1681  if (isnull)
1682  elog(ERROR, "null prosrc");
1683  proc_source = TextDatumGetCString(prosrcdatum);
1684  UTF_BEGIN;
1685  Tcl_DStringAppend(&proc_internal_body, UTF_E2U(proc_source), -1);
1686  UTF_END;
1687  pfree(proc_source);
1688  Tcl_DStringAppendElement(&proc_internal_def,
1689  Tcl_DStringValue(&proc_internal_body));
1690 
1691  /************************************************************
1692  * Create the procedure in the interpreter
1693  ************************************************************/
1694  tcl_rc = Tcl_EvalEx(interp,
1695  Tcl_DStringValue(&proc_internal_def),
1696  Tcl_DStringLength(&proc_internal_def),
1697  TCL_EVAL_GLOBAL);
1698  if (tcl_rc != TCL_OK)
1699  ereport(ERROR,
1700  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
1701  errmsg("could not create internal procedure \"%s\": %s",
1702  internal_proname,
1703  utf_u2e(Tcl_GetStringResult(interp)))));
1704  }
1705  PG_CATCH();
1706  {
1707  /*
1708  * If we failed anywhere above, clean up whatever got allocated. It
1709  * should all be in the proc_cxt, except for the DStrings.
1710  */
1711  if (proc_cxt)
1712  MemoryContextDelete(proc_cxt);
1713  Tcl_DStringFree(&proc_internal_def);
1714  Tcl_DStringFree(&proc_internal_body);
1715  PG_RE_THROW();
1716  }
1717  PG_END_TRY();
1718 
1719  /*
1720  * Install the new proc description block in the hashtable, incrementing
1721  * its refcount (the hashtable link counts as a reference). Then, if
1722  * there was a previous definition of the function, decrement that one's
1723  * refcount, and delete it if no longer referenced. The order of
1724  * operations here is important: if something goes wrong during the
1725  * MemoryContextDelete, leaking some memory for the old definition is OK,
1726  * but we don't want to corrupt the live hashtable entry. (Likewise,
1727  * freeing the DStrings is pretty low priority if that happens.)
1728  */
1729  old_prodesc = proc_ptr->proc_ptr;
1730 
1731  proc_ptr->proc_ptr = prodesc;
1732  prodesc->fn_refcount++;
1733 
1734  if (old_prodesc != NULL)
1735  {
1736  Assert(old_prodesc->fn_refcount > 0);
1737  if (--old_prodesc->fn_refcount == 0)
1738  MemoryContextDelete(old_prodesc->fn_cxt);
1739  }
1740 
1741  Tcl_DStringFree(&proc_internal_def);
1742  Tcl_DStringFree(&proc_internal_body);
1743 
1744  ReleaseSysCache(procTup);
1745 
1746  return prodesc;
1747 }
1748 
1749 
1750 /**********************************************************************
1751  * pltcl_elog() - elog() support for PLTcl
1752  **********************************************************************/
1753 static int
1754 pltcl_elog(ClientData cdata, Tcl_Interp *interp,
1755  int objc, Tcl_Obj *const objv[])
1756 {
1757  volatile int level;
1758  MemoryContext oldcontext;
1759  int priIndex;
1760 
1761  static const char *logpriorities[] = {
1762  "DEBUG", "LOG", "INFO", "NOTICE",
1763  "WARNING", "ERROR", "FATAL", (const char *) NULL
1764  };
1765 
1766  static const int loglevels[] = {
1767  DEBUG2, LOG, INFO, NOTICE,
1768  WARNING, ERROR, FATAL
1769  };
1770 
1771  if (objc != 3)
1772  {
1773  Tcl_WrongNumArgs(interp, 1, objv, "level msg");
1774  return TCL_ERROR;
1775  }
1776 
1777  if (Tcl_GetIndexFromObj(interp, objv[1], logpriorities, "priority",
1778  TCL_EXACT, &priIndex) != TCL_OK)
1779  return TCL_ERROR;
1780 
1781  level = loglevels[priIndex];
1782 
1783  if (level == ERROR)
1784  {
1785  /*
1786  * We just pass the error back to Tcl. If it's not caught, it'll
1787  * eventually get converted to a PG error when we reach the call
1788  * handler.
1789  */
1790  Tcl_SetObjResult(interp, objv[2]);
1791  return TCL_ERROR;
1792  }
1793 
1794  /*
1795  * For non-error messages, just pass 'em to ereport(). We do not expect
1796  * that this will fail, but just on the off chance it does, report the
1797  * error back to Tcl. Note we are assuming that ereport() can't have any
1798  * internal failures that are so bad as to require a transaction abort.
1799  *
1800  * This path is also used for FATAL errors, which aren't going to come
1801  * back to us at all.
1802  */
1803  oldcontext = CurrentMemoryContext;
1804  PG_TRY();
1805  {
1806  UTF_BEGIN;
1807  ereport(level,
1808  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
1809  errmsg("%s", UTF_U2E(Tcl_GetString(objv[2])))));
1810  UTF_END;
1811  }
1812  PG_CATCH();
1813  {
1814  ErrorData *edata;
1815 
1816  /* Must reset elog.c's state */
1817  MemoryContextSwitchTo(oldcontext);
1818  edata = CopyErrorData();
1819  FlushErrorState();
1820 
1821  /* Pass the error data to Tcl */
1822  pltcl_construct_errorCode(interp, edata);
1823  UTF_BEGIN;
1824  Tcl_SetObjResult(interp, Tcl_NewStringObj(UTF_E2U(edata->message), -1));
1825  UTF_END;
1826  FreeErrorData(edata);
1827 
1828  return TCL_ERROR;
1829  }
1830  PG_END_TRY();
1831 
1832  return TCL_OK;
1833 }
1834 
1835 
1836 /**********************************************************************
1837  * pltcl_construct_errorCode() - construct a Tcl errorCode
1838  * list with detailed information from the PostgreSQL server
1839  **********************************************************************/
1840 static void
1842 {
1843  Tcl_Obj *obj = Tcl_NewObj();
1844 
1845  Tcl_ListObjAppendElement(interp, obj,
1846  Tcl_NewStringObj("POSTGRES", -1));
1847  Tcl_ListObjAppendElement(interp, obj,
1848  Tcl_NewStringObj(PG_VERSION, -1));
1849  Tcl_ListObjAppendElement(interp, obj,
1850  Tcl_NewStringObj("SQLSTATE", -1));
1851  Tcl_ListObjAppendElement(interp, obj,
1852  Tcl_NewStringObj(unpack_sql_state(edata->sqlerrcode), -1));
1853  Tcl_ListObjAppendElement(interp, obj,
1854  Tcl_NewStringObj("condition", -1));
1855  Tcl_ListObjAppendElement(interp, obj,
1856  Tcl_NewStringObj(pltcl_get_condition_name(edata->sqlerrcode), -1));
1857  Tcl_ListObjAppendElement(interp, obj,
1858  Tcl_NewStringObj("message", -1));
1859  UTF_BEGIN;
1860  Tcl_ListObjAppendElement(interp, obj,
1861  Tcl_NewStringObj(UTF_E2U(edata->message), -1));
1862  UTF_END;
1863  if (edata->detail)
1864  {
1865  Tcl_ListObjAppendElement(interp, obj,
1866  Tcl_NewStringObj("detail", -1));
1867  UTF_BEGIN;
1868  Tcl_ListObjAppendElement(interp, obj,
1869  Tcl_NewStringObj(UTF_E2U(edata->detail), -1));
1870  UTF_END;
1871  }
1872  if (edata->hint)
1873  {
1874  Tcl_ListObjAppendElement(interp, obj,
1875  Tcl_NewStringObj("hint", -1));
1876  UTF_BEGIN;
1877  Tcl_ListObjAppendElement(interp, obj,
1878  Tcl_NewStringObj(UTF_E2U(edata->hint), -1));
1879  UTF_END;
1880  }
1881  if (edata->context)
1882  {
1883  Tcl_ListObjAppendElement(interp, obj,
1884  Tcl_NewStringObj("context", -1));
1885  UTF_BEGIN;
1886  Tcl_ListObjAppendElement(interp, obj,
1887  Tcl_NewStringObj(UTF_E2U(edata->context), -1));
1888  UTF_END;
1889  }
1890  if (edata->schema_name)
1891  {
1892  Tcl_ListObjAppendElement(interp, obj,
1893  Tcl_NewStringObj("schema", -1));
1894  UTF_BEGIN;
1895  Tcl_ListObjAppendElement(interp, obj,
1896  Tcl_NewStringObj(UTF_E2U(edata->schema_name), -1));
1897  UTF_END;
1898  }
1899  if (edata->table_name)
1900  {
1901  Tcl_ListObjAppendElement(interp, obj,
1902  Tcl_NewStringObj("table", -1));
1903  UTF_BEGIN;
1904  Tcl_ListObjAppendElement(interp, obj,
1905  Tcl_NewStringObj(UTF_E2U(edata->table_name), -1));
1906  UTF_END;
1907  }
1908  if (edata->column_name)
1909  {
1910  Tcl_ListObjAppendElement(interp, obj,
1911  Tcl_NewStringObj("column", -1));
1912  UTF_BEGIN;
1913  Tcl_ListObjAppendElement(interp, obj,
1914  Tcl_NewStringObj(UTF_E2U(edata->column_name), -1));
1915  UTF_END;
1916  }
1917  if (edata->datatype_name)
1918  {
1919  Tcl_ListObjAppendElement(interp, obj,
1920  Tcl_NewStringObj("datatype", -1));
1921  UTF_BEGIN;
1922  Tcl_ListObjAppendElement(interp, obj,
1923  Tcl_NewStringObj(UTF_E2U(edata->datatype_name), -1));
1924  UTF_END;
1925  }
1926  if (edata->constraint_name)
1927  {
1928  Tcl_ListObjAppendElement(interp, obj,
1929  Tcl_NewStringObj("constraint", -1));
1930  UTF_BEGIN;
1931  Tcl_ListObjAppendElement(interp, obj,
1932  Tcl_NewStringObj(UTF_E2U(edata->constraint_name), -1));
1933  UTF_END;
1934  }
1935  /* cursorpos is never interesting here; report internal query/pos */
1936  if (edata->internalquery)
1937  {
1938  Tcl_ListObjAppendElement(interp, obj,
1939  Tcl_NewStringObj("statement", -1));
1940  UTF_BEGIN;
1941  Tcl_ListObjAppendElement(interp, obj,
1942  Tcl_NewStringObj(UTF_E2U(edata->internalquery), -1));
1943  UTF_END;
1944  }
1945  if (edata->internalpos > 0)
1946  {
1947  Tcl_ListObjAppendElement(interp, obj,
1948  Tcl_NewStringObj("cursor_position", -1));
1949  Tcl_ListObjAppendElement(interp, obj,
1950  Tcl_NewIntObj(edata->internalpos));
1951  }
1952  if (edata->filename)
1953  {
1954  Tcl_ListObjAppendElement(interp, obj,
1955  Tcl_NewStringObj("filename", -1));
1956  UTF_BEGIN;
1957  Tcl_ListObjAppendElement(interp, obj,
1958  Tcl_NewStringObj(UTF_E2U(edata->filename), -1));
1959  UTF_END;
1960  }
1961  if (edata->lineno > 0)
1962  {
1963  Tcl_ListObjAppendElement(interp, obj,
1964  Tcl_NewStringObj("lineno", -1));
1965  Tcl_ListObjAppendElement(interp, obj,
1966  Tcl_NewIntObj(edata->lineno));
1967  }
1968  if (edata->funcname)
1969  {
1970  Tcl_ListObjAppendElement(interp, obj,
1971  Tcl_NewStringObj("funcname", -1));
1972  UTF_BEGIN;
1973  Tcl_ListObjAppendElement(interp, obj,
1974  Tcl_NewStringObj(UTF_E2U(edata->funcname), -1));
1975  UTF_END;
1976  }
1977 
1978  Tcl_SetObjErrorCode(interp, obj);
1979 }
1980 
1981 
1982 /**********************************************************************
1983  * pltcl_get_condition_name() - find name for SQLSTATE
1984  **********************************************************************/
1985 static const char *
1987 {
1988  int i;
1989 
1990  for (i = 0; exception_name_map[i].label != NULL; i++)
1991  {
1992  if (exception_name_map[i].sqlerrstate == sqlstate)
1993  return exception_name_map[i].label;
1994  }
1995  return "unrecognized_sqlstate";
1996 }
1997 
1998 
1999 /**********************************************************************
2000  * pltcl_quote() - quote literal strings that are to
2001  * be used in SPI_execute query strings
2002  **********************************************************************/
2003 static int
2004 pltcl_quote(ClientData cdata, Tcl_Interp *interp,
2005  int objc, Tcl_Obj *const objv[])
2006 {
2007  char *tmp;
2008  const char *cp1;
2009  char *cp2;
2010  int length;
2011 
2012  /************************************************************
2013  * Check call syntax
2014  ************************************************************/
2015  if (objc != 2)
2016  {
2017  Tcl_WrongNumArgs(interp, 1, objv, "string");
2018  return TCL_ERROR;
2019  }
2020 
2021  /************************************************************
2022  * Allocate space for the maximum the string can
2023  * grow to and initialize pointers
2024  ************************************************************/
2025  cp1 = Tcl_GetStringFromObj(objv[1], &length);
2026  tmp = palloc(length * 2 + 1);
2027  cp2 = tmp;
2028 
2029  /************************************************************
2030  * Walk through string and double every quote and backslash
2031  ************************************************************/
2032  while (*cp1)
2033  {
2034  if (*cp1 == '\'')
2035  *cp2++ = '\'';
2036  else
2037  {
2038  if (*cp1 == '\\')
2039  *cp2++ = '\\';
2040  }
2041  *cp2++ = *cp1++;
2042  }
2043 
2044  /************************************************************
2045  * Terminate the string and set it as result
2046  ************************************************************/
2047  *cp2 = '\0';
2048  Tcl_SetObjResult(interp, Tcl_NewStringObj(tmp, -1));
2049  pfree(tmp);
2050  return TCL_OK;
2051 }
2052 
2053 
2054 /**********************************************************************
2055  * pltcl_argisnull() - determine if a specific argument is NULL
2056  **********************************************************************/
2057 static int
2058 pltcl_argisnull(ClientData cdata, Tcl_Interp *interp,
2059  int objc, Tcl_Obj *const objv[])
2060 {
2061  int argno;
2062  FunctionCallInfo fcinfo = pltcl_current_call_state->fcinfo;
2063 
2064  /************************************************************
2065  * Check call syntax
2066  ************************************************************/
2067  if (objc != 2)
2068  {
2069  Tcl_WrongNumArgs(interp, 1, objv, "argno");
2070  return TCL_ERROR;
2071  }
2072 
2073  /************************************************************
2074  * Check that we're called as a normal function
2075  ************************************************************/
2076  if (fcinfo == NULL)
2077  {
2078  Tcl_SetObjResult(interp,
2079  Tcl_NewStringObj("argisnull cannot be used in triggers", -1));
2080  return TCL_ERROR;
2081  }
2082 
2083  /************************************************************
2084  * Get the argument number
2085  ************************************************************/
2086  if (Tcl_GetIntFromObj(interp, objv[1], &argno) != TCL_OK)
2087  return TCL_ERROR;
2088 
2089  /************************************************************
2090  * Check that the argno is valid
2091  ************************************************************/
2092  argno--;
2093  if (argno < 0 || argno >= fcinfo->nargs)
2094  {
2095  Tcl_SetObjResult(interp,
2096  Tcl_NewStringObj("argno out of range", -1));
2097  return TCL_ERROR;
2098  }
2099 
2100  /************************************************************
2101  * Get the requested NULL state
2102  ************************************************************/
2103  Tcl_SetObjResult(interp, Tcl_NewBooleanObj(PG_ARGISNULL(argno)));
2104  return TCL_OK;
2105 }
2106 
2107 
2108 /**********************************************************************
2109  * pltcl_returnnull() - Cause a NULL return from the current function
2110  **********************************************************************/
2111 static int
2112 pltcl_returnnull(ClientData cdata, Tcl_Interp *interp,
2113  int objc, Tcl_Obj *const objv[])
2114 {
2115  FunctionCallInfo fcinfo = pltcl_current_call_state->fcinfo;
2116 
2117  /************************************************************
2118  * Check call syntax
2119  ************************************************************/
2120  if (objc != 1)
2121  {
2122  Tcl_WrongNumArgs(interp, 1, objv, "");
2123  return TCL_ERROR;
2124  }
2125 
2126  /************************************************************
2127  * Check that we're called as a normal function
2128  ************************************************************/
2129  if (fcinfo == NULL)
2130  {
2131  Tcl_SetObjResult(interp,
2132  Tcl_NewStringObj("return_null cannot be used in triggers", -1));
2133  return TCL_ERROR;
2134  }
2135 
2136  /************************************************************
2137  * Set the NULL return flag and cause Tcl to return from the
2138  * procedure.
2139  ************************************************************/
2140  fcinfo->isnull = true;
2141 
2142  return TCL_RETURN;
2143 }
2144 
2145 
2146 /**********************************************************************
2147  * pltcl_returnnext() - Add a row to the result tuplestore in a SRF.
2148  **********************************************************************/
2149 static int
2150 pltcl_returnnext(ClientData cdata, Tcl_Interp *interp,
2151  int objc, Tcl_Obj *const objv[])
2152 {
2154  FunctionCallInfo fcinfo = call_state->fcinfo;
2155  pltcl_proc_desc *prodesc = call_state->prodesc;
2156  MemoryContext oldcontext = CurrentMemoryContext;
2158  volatile int result = TCL_OK;
2159 
2160  /*
2161  * Check that we're called as a set-returning function
2162  */
2163  if (fcinfo == NULL)
2164  {
2165  Tcl_SetObjResult(interp,
2166  Tcl_NewStringObj("return_next cannot be used in triggers", -1));
2167  return TCL_ERROR;
2168  }
2169 
2170  if (!prodesc->fn_retisset)
2171  {
2172  Tcl_SetObjResult(interp,
2173  Tcl_NewStringObj("return_next cannot be used in non-set-returning functions", -1));
2174  return TCL_ERROR;
2175  }
2176 
2177  /*
2178  * Check call syntax
2179  */
2180  if (objc != 2)
2181  {
2182  Tcl_WrongNumArgs(interp, 1, objv, "result");
2183  return TCL_ERROR;
2184  }
2185 
2186  /*
2187  * The rest might throw elog(ERROR), so must run in a subtransaction.
2188  *
2189  * A small advantage of using a subtransaction is that it provides a
2190  * short-lived memory context for free, so we needn't worry about leaking
2191  * memory here. To use that context, call BeginInternalSubTransaction
2192  * directly instead of going through pltcl_subtrans_begin.
2193  */
2195  PG_TRY();
2196  {
2197  /* Set up tuple store if first output row */
2198  if (call_state->tuple_store == NULL)
2199  pltcl_init_tuple_store(call_state);
2200 
2201  if (prodesc->fn_retistuple)
2202  {
2203  Tcl_Obj **rowObjv;
2204  int rowObjc;
2205 
2206  /* result should be a list, so break it down */
2207  if (Tcl_ListObjGetElements(interp, objv[1], &rowObjc, &rowObjv) == TCL_ERROR)
2208  result = TCL_ERROR;
2209  else
2210  {
2211  HeapTuple tuple;
2212 
2213  tuple = pltcl_build_tuple_result(interp, rowObjv, rowObjc,
2214  call_state);
2215  tuplestore_puttuple(call_state->tuple_store, tuple);
2216  }
2217  }
2218  else
2219  {
2220  Datum retval;
2221  bool isNull = false;
2222 
2223  /* for paranoia's sake, check that tupdesc has exactly one column */
2224  if (call_state->ret_tupdesc->natts != 1)
2225  elog(ERROR, "wrong result type supplied in return_next");
2226 
2227  retval = InputFunctionCall(&prodesc->result_in_func,
2228  utf_u2e((char *) Tcl_GetString(objv[1])),
2229  prodesc->result_typioparam,
2230  -1);
2231  tuplestore_putvalues(call_state->tuple_store, call_state->ret_tupdesc,
2232  &retval, &isNull);
2233  }
2234 
2235  pltcl_subtrans_commit(oldcontext, oldowner);
2236  }
2237  PG_CATCH();
2238  {
2239  pltcl_subtrans_abort(interp, oldcontext, oldowner);
2240  return TCL_ERROR;
2241  }
2242  PG_END_TRY();
2243 
2244  return result;
2245 }
2246 
2247 
2248 /*----------
2249  * Support for running SPI operations inside subtransactions
2250  *
2251  * Intended usage pattern is:
2252  *
2253  * MemoryContext oldcontext = CurrentMemoryContext;
2254  * ResourceOwner oldowner = CurrentResourceOwner;
2255  *
2256  * ...
2257  * pltcl_subtrans_begin(oldcontext, oldowner);
2258  * PG_TRY();
2259  * {
2260  * do something risky;
2261  * pltcl_subtrans_commit(oldcontext, oldowner);
2262  * }
2263  * PG_CATCH();
2264  * {
2265  * pltcl_subtrans_abort(interp, oldcontext, oldowner);
2266  * return TCL_ERROR;
2267  * }
2268  * PG_END_TRY();
2269  * return TCL_OK;
2270  *----------
2271  */
2272 static void
2274 {
2276 
2277  /* Want to run inside function's memory context */
2278  MemoryContextSwitchTo(oldcontext);
2279 }
2280 
2281 static void
2283 {
2284  /* Commit the inner transaction, return to outer xact context */
2286  MemoryContextSwitchTo(oldcontext);
2287  CurrentResourceOwner = oldowner;
2288 }
2289 
2290 static void
2292  MemoryContext oldcontext, ResourceOwner oldowner)
2293 {
2294  ErrorData *edata;
2295 
2296  /* Save error info */
2297  MemoryContextSwitchTo(oldcontext);
2298  edata = CopyErrorData();
2299  FlushErrorState();
2300 
2301  /* Abort the inner transaction */
2303  MemoryContextSwitchTo(oldcontext);
2304  CurrentResourceOwner = oldowner;
2305 
2306  /* Pass the error data to Tcl */
2307  pltcl_construct_errorCode(interp, edata);
2308  UTF_BEGIN;
2309  Tcl_SetObjResult(interp, Tcl_NewStringObj(UTF_E2U(edata->message), -1));
2310  UTF_END;
2311  FreeErrorData(edata);
2312 }
2313 
2314 
2315 /**********************************************************************
2316  * pltcl_SPI_execute() - The builtin SPI_execute command
2317  * for the Tcl interpreter
2318  **********************************************************************/
2319 static int
2320 pltcl_SPI_execute(ClientData cdata, Tcl_Interp *interp,
2321  int objc, Tcl_Obj *const objv[])
2322 {
2323  int my_rc;
2324  int spi_rc;
2325  int query_idx;
2326  int i;
2327  int optIndex;
2328  int count = 0;
2329  const char *volatile arrayname = NULL;
2330  Tcl_Obj *volatile loop_body = NULL;
2331  MemoryContext oldcontext = CurrentMemoryContext;
2333 
2334  enum options
2335  {
2336  OPT_ARRAY, OPT_COUNT
2337  };
2338 
2339  static const char *options[] = {
2340  "-array", "-count", (const char *) NULL
2341  };
2342 
2343  /************************************************************
2344  * Check the call syntax and get the options
2345  ************************************************************/
2346  if (objc < 2)
2347  {
2348  Tcl_WrongNumArgs(interp, 1, objv,
2349  "?-count n? ?-array name? query ?loop body?");
2350  return TCL_ERROR;
2351  }
2352 
2353  i = 1;
2354  while (i < objc)
2355  {
2356  if (Tcl_GetIndexFromObj(NULL, objv[i], options, NULL,
2357  TCL_EXACT, &optIndex) != TCL_OK)
2358  break;
2359 
2360  if (++i >= objc)
2361  {
2362  Tcl_SetObjResult(interp,
2363  Tcl_NewStringObj("missing argument to -count or -array", -1));
2364  return TCL_ERROR;
2365  }
2366 
2367  switch ((enum options) optIndex)
2368  {
2369  case OPT_ARRAY:
2370  arrayname = Tcl_GetString(objv[i++]);
2371  break;
2372 
2373  case OPT_COUNT:
2374  if (Tcl_GetIntFromObj(interp, objv[i++], &count) != TCL_OK)
2375  return TCL_ERROR;
2376  break;
2377  }
2378  }
2379 
2380  query_idx = i;
2381  if (query_idx >= objc || query_idx + 2 < objc)
2382  {
2383  Tcl_WrongNumArgs(interp, query_idx - 1, objv, "query ?loop body?");
2384  return TCL_ERROR;
2385  }
2386 
2387  if (query_idx + 1 < objc)
2388  loop_body = objv[query_idx + 1];
2389 
2390  /************************************************************
2391  * Execute the query inside a sub-transaction, so we can cope with
2392  * errors sanely
2393  ************************************************************/
2394 
2395  pltcl_subtrans_begin(oldcontext, oldowner);
2396 
2397  PG_TRY();
2398  {
2399  UTF_BEGIN;
2400  spi_rc = SPI_execute(UTF_U2E(Tcl_GetString(objv[query_idx])),
2401  pltcl_current_call_state->prodesc->fn_readonly, count);
2402  UTF_END;
2403 
2404  my_rc = pltcl_process_SPI_result(interp,
2405  arrayname,
2406  loop_body,
2407  spi_rc,
2408  SPI_tuptable,
2409  SPI_processed);
2410 
2411  pltcl_subtrans_commit(oldcontext, oldowner);
2412  }
2413  PG_CATCH();
2414  {
2415  pltcl_subtrans_abort(interp, oldcontext, oldowner);
2416  return TCL_ERROR;
2417  }
2418  PG_END_TRY();
2419 
2420  return my_rc;
2421 }
2422 
2423 /*
2424  * Process the result from SPI_execute or SPI_execute_plan
2425  *
2426  * Shared code between pltcl_SPI_execute and pltcl_SPI_execute_plan
2427  */
2428 static int
2430  const char *arrayname,
2431  Tcl_Obj *loop_body,
2432  int spi_rc,
2433  SPITupleTable *tuptable,
2434  uint64 ntuples)
2435 {
2436  int my_rc = TCL_OK;
2437  int loop_rc;
2438  HeapTuple *tuples;
2439  TupleDesc tupdesc;
2440 
2441  switch (spi_rc)
2442  {
2443  case SPI_OK_SELINTO:
2444  case SPI_OK_INSERT:
2445  case SPI_OK_DELETE:
2446  case SPI_OK_UPDATE:
2447  Tcl_SetObjResult(interp, Tcl_NewWideIntObj(ntuples));
2448  break;
2449 
2450  case SPI_OK_UTILITY:
2451  case SPI_OK_REWRITTEN:
2452  if (tuptable == NULL)
2453  {
2454  Tcl_SetObjResult(interp, Tcl_NewIntObj(0));
2455  break;
2456  }
2457  /* fall through for utility returning tuples */
2458  /* FALLTHROUGH */
2459 
2460  case SPI_OK_SELECT:
2464 
2465  /*
2466  * Process the tuples we got
2467  */
2468  tuples = tuptable->vals;
2469  tupdesc = tuptable->tupdesc;
2470 
2471  if (loop_body == NULL)
2472  {
2473  /*
2474  * If there is no loop body given, just set the variables from
2475  * the first tuple (if any)
2476  */
2477  if (ntuples > 0)
2478  pltcl_set_tuple_values(interp, arrayname, 0,
2479  tuples[0], tupdesc);
2480  }
2481  else
2482  {
2483  /*
2484  * There is a loop body - process all tuples and evaluate the
2485  * body on each
2486  */
2487  uint64 i;
2488 
2489  for (i = 0; i < ntuples; i++)
2490  {
2491  pltcl_set_tuple_values(interp, arrayname, i,
2492  tuples[i], tupdesc);
2493 
2494  loop_rc = Tcl_EvalObjEx(interp, loop_body, 0);
2495 
2496  if (loop_rc == TCL_OK)
2497  continue;
2498  if (loop_rc == TCL_CONTINUE)
2499  continue;
2500  if (loop_rc == TCL_RETURN)
2501  {
2502  my_rc = TCL_RETURN;
2503  break;
2504  }
2505  if (loop_rc == TCL_BREAK)
2506  break;
2507  my_rc = TCL_ERROR;
2508  break;
2509  }
2510  }
2511 
2512  if (my_rc == TCL_OK)
2513  {
2514  Tcl_SetObjResult(interp, Tcl_NewWideIntObj(ntuples));
2515  }
2516  break;
2517 
2518  default:
2519  Tcl_AppendResult(interp, "pltcl: SPI_execute failed: ",
2520  SPI_result_code_string(spi_rc), NULL);
2521  my_rc = TCL_ERROR;
2522  break;
2523  }
2524 
2525  SPI_freetuptable(tuptable);
2526 
2527  return my_rc;
2528 }
2529 
2530 
2531 /**********************************************************************
2532  * pltcl_SPI_prepare() - Builtin support for prepared plans
2533  * The Tcl command SPI_prepare
2534  * always saves the plan using
2535  * SPI_keepplan and returns a key for
2536  * access. There is no chance to prepare
2537  * and not save the plan currently.
2538  **********************************************************************/
2539 static int
2540 pltcl_SPI_prepare(ClientData cdata, Tcl_Interp *interp,
2541  int objc, Tcl_Obj *const objv[])
2542 {
2543  volatile MemoryContext plan_cxt = NULL;
2544  int nargs;
2545  Tcl_Obj **argsObj;
2546  pltcl_query_desc *qdesc;
2547  int i;
2548  Tcl_HashEntry *hashent;
2549  int hashnew;
2550  Tcl_HashTable *query_hash;
2551  MemoryContext oldcontext = CurrentMemoryContext;
2553 
2554  /************************************************************
2555  * Check the call syntax
2556  ************************************************************/
2557  if (objc != 3)
2558  {
2559  Tcl_WrongNumArgs(interp, 1, objv, "query argtypes");
2560  return TCL_ERROR;
2561  }
2562 
2563  /************************************************************
2564  * Split the argument type list
2565  ************************************************************/
2566  if (Tcl_ListObjGetElements(interp, objv[2], &nargs, &argsObj) != TCL_OK)
2567  return TCL_ERROR;
2568 
2569  /************************************************************
2570  * Allocate the new querydesc structure
2571  *
2572  * struct qdesc and subsidiary data all live in plan_cxt. Note that if the
2573  * function is recompiled for whatever reason, permanent memory leaks
2574  * occur. FIXME someday.
2575  ************************************************************/
2577  "PL/Tcl spi_prepare query",
2579  MemoryContextSwitchTo(plan_cxt);
2580  qdesc = (pltcl_query_desc *) palloc0(sizeof(pltcl_query_desc));
2581  snprintf(qdesc->qname, sizeof(qdesc->qname), "%p", qdesc);
2582  qdesc->nargs = nargs;
2583  qdesc->argtypes = (Oid *) palloc(nargs * sizeof(Oid));
2584  qdesc->arginfuncs = (FmgrInfo *) palloc(nargs * sizeof(FmgrInfo));
2585  qdesc->argtypioparams = (Oid *) palloc(nargs * sizeof(Oid));
2586  MemoryContextSwitchTo(oldcontext);
2587 
2588  /************************************************************
2589  * Execute the prepare inside a sub-transaction, so we can cope with
2590  * errors sanely
2591  ************************************************************/
2592 
2593  pltcl_subtrans_begin(oldcontext, oldowner);
2594 
2595  PG_TRY();
2596  {
2597  /************************************************************
2598  * Resolve argument type names and then look them up by oid
2599  * in the system cache, and remember the required information
2600  * for input conversion.
2601  ************************************************************/
2602  for (i = 0; i < nargs; i++)
2603  {
2604  Oid typId,
2605  typInput,
2606  typIOParam;
2607  int32 typmod;
2608 
2609  parseTypeString(Tcl_GetString(argsObj[i]), &typId, &typmod, false);
2610 
2611  getTypeInputInfo(typId, &typInput, &typIOParam);
2612 
2613  qdesc->argtypes[i] = typId;
2614  fmgr_info_cxt(typInput, &(qdesc->arginfuncs[i]), plan_cxt);
2615  qdesc->argtypioparams[i] = typIOParam;
2616  }
2617 
2618  /************************************************************
2619  * Prepare the plan and check for errors
2620  ************************************************************/
2621  UTF_BEGIN;
2622  qdesc->plan = SPI_prepare(UTF_U2E(Tcl_GetString(objv[1])),
2623  nargs, qdesc->argtypes);
2624  UTF_END;
2625 
2626  if (qdesc->plan == NULL)
2627  elog(ERROR, "SPI_prepare() failed");
2628 
2629  /************************************************************
2630  * Save the plan into permanent memory (right now it's in the
2631  * SPI procCxt, which will go away at function end).
2632  ************************************************************/
2633  if (SPI_keepplan(qdesc->plan))
2634  elog(ERROR, "SPI_keepplan() failed");
2635 
2636  pltcl_subtrans_commit(oldcontext, oldowner);
2637  }
2638  PG_CATCH();
2639  {
2640  pltcl_subtrans_abort(interp, oldcontext, oldowner);
2641 
2642  MemoryContextDelete(plan_cxt);
2643 
2644  return TCL_ERROR;
2645  }
2646  PG_END_TRY();
2647 
2648  /************************************************************
2649  * Insert a hashtable entry for the plan and return
2650  * the key to the caller
2651  ************************************************************/
2652  query_hash = &pltcl_current_call_state->prodesc->interp_desc->query_hash;
2653 
2654  hashent = Tcl_CreateHashEntry(query_hash, qdesc->qname, &hashnew);
2655  Tcl_SetHashValue(hashent, (ClientData) qdesc);
2656 
2657  /* qname is ASCII, so no need for encoding conversion */
2658  Tcl_SetObjResult(interp, Tcl_NewStringObj(qdesc->qname, -1));
2659  return TCL_OK;
2660 }
2661 
2662 
2663 /**********************************************************************
2664  * pltcl_SPI_execute_plan() - Execute a prepared plan
2665  **********************************************************************/
2666 static int
2667 pltcl_SPI_execute_plan(ClientData cdata, Tcl_Interp *interp,
2668  int objc, Tcl_Obj *const objv[])
2669 {
2670  int my_rc;
2671  int spi_rc;
2672  int i;
2673  int j;
2674  int optIndex;
2675  Tcl_HashEntry *hashent;
2676  pltcl_query_desc *qdesc;
2677  const char *nulls = NULL;
2678  const char *arrayname = NULL;
2679  Tcl_Obj *loop_body = NULL;
2680  int count = 0;
2681  int callObjc;
2682  Tcl_Obj **callObjv = NULL;
2683  Datum *argvalues;
2684  MemoryContext oldcontext = CurrentMemoryContext;
2686  Tcl_HashTable *query_hash;
2687 
2688  enum options
2689  {
2690  OPT_ARRAY, OPT_COUNT, OPT_NULLS
2691  };
2692 
2693  static const char *options[] = {
2694  "-array", "-count", "-nulls", (const char *) NULL
2695  };
2696 
2697  /************************************************************
2698  * Get the options and check syntax
2699  ************************************************************/
2700  i = 1;
2701  while (i < objc)
2702  {
2703  if (Tcl_GetIndexFromObj(NULL, objv[i], options, NULL,
2704  TCL_EXACT, &optIndex) != TCL_OK)
2705  break;
2706 
2707  if (++i >= objc)
2708  {
2709  Tcl_SetObjResult(interp,
2710  Tcl_NewStringObj("missing argument to -array, -count or -nulls", -1));
2711  return TCL_ERROR;
2712  }
2713 
2714  switch ((enum options) optIndex)
2715  {
2716  case OPT_ARRAY:
2717  arrayname = Tcl_GetString(objv[i++]);
2718  break;
2719 
2720  case OPT_COUNT:
2721  if (Tcl_GetIntFromObj(interp, objv[i++], &count) != TCL_OK)
2722  return TCL_ERROR;
2723  break;
2724 
2725  case OPT_NULLS:
2726  nulls = Tcl_GetString(objv[i++]);
2727  break;
2728  }
2729  }
2730 
2731  /************************************************************
2732  * Get the prepared plan descriptor by its key
2733  ************************************************************/
2734  if (i >= objc)
2735  {
2736  Tcl_SetObjResult(interp,
2737  Tcl_NewStringObj("missing argument to -count or -array", -1));
2738  return TCL_ERROR;
2739  }
2740 
2741  query_hash = &pltcl_current_call_state->prodesc->interp_desc->query_hash;
2742 
2743  hashent = Tcl_FindHashEntry(query_hash, Tcl_GetString(objv[i]));
2744  if (hashent == NULL)
2745  {
2746  Tcl_AppendResult(interp, "invalid queryid '", Tcl_GetString(objv[i]), "'", NULL);
2747  return TCL_ERROR;
2748  }
2749  qdesc = (pltcl_query_desc *) Tcl_GetHashValue(hashent);
2750  i++;
2751 
2752  /************************************************************
2753  * If a nulls string is given, check for correct length
2754  ************************************************************/
2755  if (nulls != NULL)
2756  {
2757  if (strlen(nulls) != qdesc->nargs)
2758  {
2759  Tcl_SetObjResult(interp,
2760  Tcl_NewStringObj(
2761  "length of nulls string doesn't match number of arguments",
2762  -1));
2763  return TCL_ERROR;
2764  }
2765  }
2766 
2767  /************************************************************
2768  * If there was an argtype list on preparation, we need
2769  * an argument value list now
2770  ************************************************************/
2771  if (qdesc->nargs > 0)
2772  {
2773  if (i >= objc)
2774  {
2775  Tcl_SetObjResult(interp,
2776  Tcl_NewStringObj(
2777  "argument list length doesn't match number of arguments for query"
2778  ,-1));
2779  return TCL_ERROR;
2780  }
2781 
2782  /************************************************************
2783  * Split the argument values
2784  ************************************************************/
2785  if (Tcl_ListObjGetElements(interp, objv[i++], &callObjc, &callObjv) != TCL_OK)
2786  return TCL_ERROR;
2787 
2788  /************************************************************
2789  * Check that the number of arguments matches
2790  ************************************************************/
2791  if (callObjc != qdesc->nargs)
2792  {
2793  Tcl_SetObjResult(interp,
2794  Tcl_NewStringObj(
2795  "argument list length doesn't match number of arguments for query"
2796  ,-1));
2797  return TCL_ERROR;
2798  }
2799  }
2800  else
2801  callObjc = 0;
2802 
2803  /************************************************************
2804  * Get loop body if present
2805  ************************************************************/
2806  if (i < objc)
2807  loop_body = objv[i++];
2808 
2809  if (i != objc)
2810  {
2811  Tcl_WrongNumArgs(interp, 1, objv,
2812  "?-count n? ?-array name? ?-nulls string? "
2813  "query ?args? ?loop body?");
2814  return TCL_ERROR;
2815  }
2816 
2817  /************************************************************
2818  * Execute the plan inside a sub-transaction, so we can cope with
2819  * errors sanely
2820  ************************************************************/
2821 
2822  pltcl_subtrans_begin(oldcontext, oldowner);
2823 
2824  PG_TRY();
2825  {
2826  /************************************************************
2827  * Setup the value array for SPI_execute_plan() using
2828  * the type specific input functions
2829  ************************************************************/
2830  argvalues = (Datum *) palloc(callObjc * sizeof(Datum));
2831 
2832  for (j = 0; j < callObjc; j++)
2833  {
2834  if (nulls && nulls[j] == 'n')
2835  {
2836  argvalues[j] = InputFunctionCall(&qdesc->arginfuncs[j],
2837  NULL,
2838  qdesc->argtypioparams[j],
2839  -1);
2840  }
2841  else
2842  {
2843  UTF_BEGIN;
2844  argvalues[j] = InputFunctionCall(&qdesc->arginfuncs[j],
2845  UTF_U2E(Tcl_GetString(callObjv[j])),
2846  qdesc->argtypioparams[j],
2847  -1);
2848  UTF_END;
2849  }
2850  }
2851 
2852  /************************************************************
2853  * Execute the plan
2854  ************************************************************/
2855  spi_rc = SPI_execute_plan(qdesc->plan, argvalues, nulls,
2856  pltcl_current_call_state->prodesc->fn_readonly,
2857  count);
2858 
2859  my_rc = pltcl_process_SPI_result(interp,
2860  arrayname,
2861  loop_body,
2862  spi_rc,
2863  SPI_tuptable,
2864  SPI_processed);
2865 
2866  pltcl_subtrans_commit(oldcontext, oldowner);
2867  }
2868  PG_CATCH();
2869  {
2870  pltcl_subtrans_abort(interp, oldcontext, oldowner);
2871  return TCL_ERROR;
2872  }
2873  PG_END_TRY();
2874 
2875  return my_rc;
2876 }
2877 
2878 
2879 /**********************************************************************
2880  * pltcl_subtransaction() - Execute some Tcl code in a subtransaction
2881  *
2882  * The subtransaction is aborted if the Tcl code fragment returns TCL_ERROR,
2883  * otherwise it's subcommitted.
2884  **********************************************************************/
2885 static int
2886 pltcl_subtransaction(ClientData cdata, Tcl_Interp *interp,
2887  int objc, Tcl_Obj *const objv[])
2888 {
2889  MemoryContext oldcontext = CurrentMemoryContext;
2891  int retcode;
2892 
2893  if (objc != 2)
2894  {
2895  Tcl_WrongNumArgs(interp, 1, objv, "command");
2896  return TCL_ERROR;
2897  }
2898 
2899  /*
2900  * Note: we don't use pltcl_subtrans_begin and friends here because we
2901  * don't want the error handling in pltcl_subtrans_abort. But otherwise
2902  * the processing should be about the same as in those functions.
2903  */
2905  MemoryContextSwitchTo(oldcontext);
2906 
2907  retcode = Tcl_EvalObjEx(interp, objv[1], 0);
2908 
2909  if (retcode == TCL_ERROR)
2910  {
2911  /* Rollback the subtransaction */
2913  }
2914  else
2915  {
2916  /* Commit the subtransaction */
2918  }
2919 
2920  /* In either case, restore previous memory context and resource owner */
2921  MemoryContextSwitchTo(oldcontext);
2922  CurrentResourceOwner = oldowner;
2923 
2924  return retcode;
2925 }
2926 
2927 
2928 /**********************************************************************
2929  * pltcl_commit()
2930  *
2931  * Commit the transaction and start a new one.
2932  **********************************************************************/
2933 static int
2934 pltcl_commit(ClientData cdata, Tcl_Interp *interp,
2935  int objc, Tcl_Obj *const objv[])
2936 {
2937  MemoryContext oldcontext = CurrentMemoryContext;
2938 
2939  PG_TRY();
2940  {
2941  SPI_commit();
2943  }
2944  PG_CATCH();
2945  {
2946  ErrorData *edata;
2947 
2948  /* Save error info */
2949  MemoryContextSwitchTo(oldcontext);
2950  edata = CopyErrorData();
2951  FlushErrorState();
2952 
2953  /* Pass the error data to Tcl */
2954  pltcl_construct_errorCode(interp, edata);
2955  UTF_BEGIN;
2956  Tcl_SetObjResult(interp, Tcl_NewStringObj(UTF_E2U(edata->message), -1));
2957  UTF_END;
2958  FreeErrorData(edata);
2959 
2960  return TCL_ERROR;
2961  }
2962  PG_END_TRY();
2963 
2964  return TCL_OK;
2965 }
2966 
2967 
2968 /**********************************************************************
2969  * pltcl_rollback()
2970  *
2971  * Abort the transaction and start a new one.
2972  **********************************************************************/
2973 static int
2974 pltcl_rollback(ClientData cdata, Tcl_Interp *interp,
2975  int objc, Tcl_Obj *const objv[])
2976 {
2977  MemoryContext oldcontext = CurrentMemoryContext;
2978 
2979  PG_TRY();
2980  {
2981  SPI_rollback();
2983  }
2984  PG_CATCH();
2985  {
2986  ErrorData *edata;
2987 
2988  /* Save error info */
2989  MemoryContextSwitchTo(oldcontext);
2990  edata = CopyErrorData();
2991  FlushErrorState();
2992 
2993  /* Pass the error data to Tcl */
2994  pltcl_construct_errorCode(interp, edata);
2995  UTF_BEGIN;
2996  Tcl_SetObjResult(interp, Tcl_NewStringObj(UTF_E2U(edata->message), -1));
2997  UTF_END;
2998  FreeErrorData(edata);
2999 
3000  return TCL_ERROR;
3001  }
3002  PG_END_TRY();
3003 
3004  return TCL_OK;
3005 }
3006 
3007 
3008 /**********************************************************************
3009  * pltcl_set_tuple_values() - Set variables for all attributes
3010  * of a given tuple
3011  *
3012  * Note: arrayname is presumed to be UTF8; it usually came from Tcl
3013  **********************************************************************/
3014 static void
3015 pltcl_set_tuple_values(Tcl_Interp *interp, const char *arrayname,
3016  uint64 tupno, HeapTuple tuple, TupleDesc tupdesc)
3017 {
3018  int i;
3019  char *outputstr;
3020  Datum attr;
3021  bool isnull;
3022  const char *attname;
3023  Oid typoutput;
3024  bool typisvarlena;
3025  const char **arrptr;
3026  const char **nameptr;
3027  const char *nullname = NULL;
3028 
3029  /************************************************************
3030  * Prepare pointers for Tcl_SetVar2Ex() below
3031  ************************************************************/
3032  if (arrayname == NULL)
3033  {
3034  arrptr = &attname;
3035  nameptr = &nullname;
3036  }
3037  else
3038  {
3039  arrptr = &arrayname;
3040  nameptr = &attname;
3041 
3042  /*
3043  * When outputting to an array, fill the ".tupno" element with the
3044  * current tuple number. This will be overridden below if ".tupno" is
3045  * in use as an actual field name in the rowtype.
3046  */
3047  Tcl_SetVar2Ex(interp, arrayname, ".tupno", Tcl_NewWideIntObj(tupno), 0);
3048  }
3049 
3050  for (i = 0; i < tupdesc->natts; i++)
3051  {
3052  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
3053 
3054  /* ignore dropped attributes */
3055  if (att->attisdropped)
3056  continue;
3057 
3058  /************************************************************
3059  * Get the attribute name
3060  ************************************************************/
3061  UTF_BEGIN;
3062  attname = pstrdup(UTF_E2U(NameStr(att->attname)));
3063  UTF_END;
3064 
3065  /************************************************************
3066  * Get the attributes value
3067  ************************************************************/
3068  attr = heap_getattr(tuple, i + 1, tupdesc, &isnull);
3069 
3070  /************************************************************
3071  * If there is a value, set the variable
3072  * If not, unset it
3073  *
3074  * Hmmm - Null attributes will cause functions to
3075  * crash if they don't expect them - need something
3076  * smarter here.
3077  ************************************************************/
3078  if (!isnull)
3079  {
3080  getTypeOutputInfo(att->atttypid, &typoutput, &typisvarlena);
3081  outputstr = OidOutputFunctionCall(typoutput, attr);
3082  UTF_BEGIN;
3083  Tcl_SetVar2Ex(interp, *arrptr, *nameptr,
3084  Tcl_NewStringObj(UTF_E2U(outputstr), -1), 0);
3085  UTF_END;
3086  pfree(outputstr);
3087  }
3088  else
3089  Tcl_UnsetVar2(interp, *arrptr, *nameptr, 0);
3090 
3091  pfree(unconstify(char *, attname));
3092  }
3093 }
3094 
3095 
3096 /**********************************************************************
3097  * pltcl_build_tuple_argument() - Build a list object usable for 'array set'
3098  * from all attributes of a given tuple
3099  **********************************************************************/
3100 static Tcl_Obj *
3101 pltcl_build_tuple_argument(HeapTuple tuple, TupleDesc tupdesc, bool include_generated)
3102 {
3103  Tcl_Obj *retobj = Tcl_NewObj();
3104  int i;
3105  char *outputstr;
3106  Datum attr;
3107  bool isnull;
3108  char *attname;
3109  Oid typoutput;
3110  bool typisvarlena;
3111 
3112  for (i = 0; i < tupdesc->natts; i++)
3113  {
3114  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
3115 
3116  /* ignore dropped attributes */
3117  if (att->attisdropped)
3118  continue;
3119 
3120  if (att->attgenerated)
3121  {
3122  /* don't include unless requested */
3123  if (!include_generated)
3124  continue;
3125  }
3126 
3127  /************************************************************
3128  * Get the attribute name
3129  ************************************************************/
3130  attname = NameStr(att->attname);
3131 
3132  /************************************************************
3133  * Get the attributes value
3134  ************************************************************/
3135  attr = heap_getattr(tuple, i + 1, tupdesc, &isnull);
3136 
3137  /************************************************************
3138  * If there is a value, append the attribute name and the
3139  * value to the list
3140  *
3141  * Hmmm - Null attributes will cause functions to
3142  * crash if they don't expect them - need something
3143  * smarter here.
3144  ************************************************************/
3145  if (!isnull)
3146  {
3147  getTypeOutputInfo(att->atttypid,
3148  &typoutput, &typisvarlena);
3149  outputstr = OidOutputFunctionCall(typoutput, attr);
3150  UTF_BEGIN;
3151  Tcl_ListObjAppendElement(NULL, retobj,
3152  Tcl_NewStringObj(UTF_E2U(attname), -1));
3153  UTF_END;
3154  UTF_BEGIN;
3155  Tcl_ListObjAppendElement(NULL, retobj,
3156  Tcl_NewStringObj(UTF_E2U(outputstr), -1));
3157  UTF_END;
3158  pfree(outputstr);
3159  }
3160  }
3161 
3162  return retobj;
3163 }
3164 
3165 /**********************************************************************
3166  * pltcl_build_tuple_result() - Build a tuple of function's result rowtype
3167  * from a Tcl list of column names and values
3168  *
3169  * In a trigger function, we build a tuple of the trigger table's rowtype.
3170  *
3171  * Note: this function leaks memory. Even if we made it clean up its own
3172  * mess, there's no way to prevent the datatype input functions it calls
3173  * from leaking. Run it in a short-lived context, unless we're about to
3174  * exit the procedure anyway.
3175  **********************************************************************/
3176 static HeapTuple
3177 pltcl_build_tuple_result(Tcl_Interp *interp, Tcl_Obj **kvObjv, int kvObjc,
3178  pltcl_call_state *call_state)
3179 {
3180  HeapTuple tuple;
3181  TupleDesc tupdesc;
3182  AttInMetadata *attinmeta;
3183  char **values;
3184  int i;
3185 
3186  if (call_state->ret_tupdesc)
3187  {
3188  tupdesc = call_state->ret_tupdesc;
3189  attinmeta = call_state->attinmeta;
3190  }
3191  else if (call_state->trigdata)
3192  {
3193  tupdesc = RelationGetDescr(call_state->trigdata->tg_relation);
3194  attinmeta = TupleDescGetAttInMetadata(tupdesc);
3195  }
3196  else
3197  {
3198  elog(ERROR, "PL/Tcl function does not return a tuple");
3199  tupdesc = NULL; /* keep compiler quiet */
3200  attinmeta = NULL;
3201  }
3202 
3203  values = (char **) palloc0(tupdesc->natts * sizeof(char *));
3204 
3205  if (kvObjc % 2 != 0)
3206  ereport(ERROR,
3207  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3208  errmsg("column name/value list must have even number of elements")));
3209 
3210  for (i = 0; i < kvObjc; i += 2)
3211  {
3212  char *fieldName = utf_u2e(Tcl_GetString(kvObjv[i]));
3213  int attn = SPI_fnumber(tupdesc, fieldName);
3214 
3215  /*
3216  * We silently ignore ".tupno", if it's present but doesn't match any
3217  * actual output column. This allows direct use of a row returned by
3218  * pltcl_set_tuple_values().
3219  */
3220  if (attn == SPI_ERROR_NOATTRIBUTE)
3221  {
3222  if (strcmp(fieldName, ".tupno") == 0)
3223  continue;
3224  ereport(ERROR,
3225  (errcode(ERRCODE_UNDEFINED_COLUMN),
3226  errmsg("column name/value list contains nonexistent column name \"%s\"",
3227  fieldName)));
3228  }
3229 
3230  if (attn <= 0)
3231  ereport(ERROR,
3232  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3233  errmsg("cannot set system attribute \"%s\"",
3234  fieldName)));
3235 
3236  if (TupleDescAttr(tupdesc, attn - 1)->attgenerated)
3237  ereport(ERROR,
3238  (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
3239  errmsg("cannot set generated column \"%s\"",
3240  fieldName)));
3241 
3242  values[attn - 1] = utf_u2e(Tcl_GetString(kvObjv[i + 1]));
3243  }
3244 
3245  tuple = BuildTupleFromCStrings(attinmeta, values);
3246 
3247  /* if result type is domain-over-composite, check domain constraints */
3248  if (call_state->prodesc->fn_retisdomain)
3249  domain_check(HeapTupleGetDatum(tuple), false,
3250  call_state->prodesc->result_typid,
3251  &call_state->prodesc->domain_info,
3252  call_state->prodesc->fn_cxt);
3253 
3254  return tuple;
3255 }
3256 
3257 /**********************************************************************
3258  * pltcl_init_tuple_store() - Initialize the result tuplestore for a SRF
3259  **********************************************************************/
3260 static void
3262 {
3263  ReturnSetInfo *rsi = call_state->rsi;
3264  MemoryContext oldcxt;
3265  ResourceOwner oldowner;
3266 
3267  /* Should be in a SRF */
3268  Assert(rsi);
3269  /* Should be first time through */
3270  Assert(!call_state->tuple_store);
3271  Assert(!call_state->attinmeta);
3272 
3273  /* We expect caller to provide an appropriate result tupdesc */
3274  Assert(rsi->expectedDesc);
3275  call_state->ret_tupdesc = rsi->expectedDesc;
3276 
3277  /*
3278  * Switch to the right memory context and resource owner for storing the
3279  * tuplestore. If we're within a subtransaction opened for an exception
3280  * block, for example, we must still create the tuplestore in the resource
3281  * owner that was active when this function was entered, and not in the
3282  * subtransaction's resource owner.
3283  */
3284  oldcxt = MemoryContextSwitchTo(call_state->tuple_store_cxt);
3285  oldowner = CurrentResourceOwner;
3287 
3288  call_state->tuple_store =
3290  false, work_mem);
3291 
3292  /* Build attinmeta in this context, too */
3293  call_state->attinmeta = TupleDescGetAttInMetadata(call_state->ret_tupdesc);
3294 
3295  CurrentResourceOwner = oldowner;
3296  MemoryContextSwitchTo(oldcxt);
3297 }
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:951
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
bool lanpltrusted
Definition: pltcl.c:144
char * schema_name
Definition: elog.h:366
#define SPI_OK_CONNECT
Definition: spi.h:53
PG_MODULE_MAGIC
Definition: pltcl.c:42
static void pltcl_subtrans_begin(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: pltcl.c:2273
static PgChecksumMode mode
Definition: pg_checksums.c:61
static int pltcl_rollback(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
Definition: pltcl.c:2974
static void pltcl_init_interp(pltcl_interp_desc *interp_desc, Oid prolang, bool pltrusted)
Definition: pltcl.c:485
Definition: fmgr.h:56
#define CALLED_AS_EVENT_TRIGGER(fcinfo)
Definition: event_trigger.h:39
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:110
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
#define AllocSetContextCreate
Definition: memutils.h:170
HeapTupleData * HeapTuple
Definition: htup.h:71
bool fn_retistuple
Definition: pltcl.c:150
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:196
NameData proname
Definition: pg_proc.h:36
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2674
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define TEXTDOMAIN
Definition: pltcl.c:60
struct pltcl_proc_ptr pltcl_proc_ptr
static void pltcl_subtrans_abort(Tcl_Interp *interp, MemoryContext oldcontext, ResourceOwner oldowner)
Definition: pltcl.c:2291
#define HASH_ELEM
Definition: hsearch.h:87
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1652
uint32 TransactionId
Definition: c.h:507
int sqlerrcode
Definition: elog.h:359
#define RelationGetDescr(relation)
Definition: rel.h:445
int SPI_connect_ext(int options)
Definition: spi.c:95
Oid GetUserId(void)
Definition: miscinit.c:380
int SPI_connect(void)
Definition: spi.c:89
#define SPI_OK_DELETE_RETURNING
Definition: spi.h:64
#define castNode(_type_, nodeptr)
Definition: nodes.h:594
static int pltcl_argisnull(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
Definition: pltcl.c:2058
static void pltcl_init_tuple_store(pltcl_call_state *call_state)
Definition: pltcl.c:3261
ErrorData * CopyErrorData(void)
Definition: elog.c:1484
FmgrInfo result_in_func
Definition: pltcl.c:147
#define PointerGetDatum(X)
Definition: postgres.h:556
const char * funcname
Definition: elog.h:356
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define SPI_OK_DELETE
Definition: spi.h:60
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
char * pstrdup(const char *in)
Definition: mcxt.c:1186
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:674
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:202
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4428
char * unpack_sql_state(int sql_state)
Definition: elog.c:2831
int SPI_finish(void)
Definition: spi.c:176
struct pltcl_proc_key pltcl_proc_key
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static void pltcl_DeleteFileHandler(int fd)
Definition: pltcl.c:375
char * user_proname
Definition: pltcl.c:137
void * domain_info
Definition: pltcl.c:152
static int pltcl_commit(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
Definition: pltcl.c:2934
int lineno
Definition: elog.h:355
Size entrysize
Definition: hsearch.h:73
#define gettext_noop(x)
Definition: c.h:1117
SPITupleTable * SPI_tuptable
Definition: spi.c:46
static const char * pltcl_get_condition_name(int sqlstate)
Definition: pltcl.c:1986
int errcode(int sqlerrcode)
Definition: elog.c:570
static void pltcl_ServiceModeHook(int mode)
Definition: pltcl.c:380
#define INFO
Definition: elog.h:33
char * format_type_be(Oid type_oid)
Definition: format_type.c:326
Datum pltcl_call_handler(PG_FUNCTION_ARGS)
Definition: pltcl.c:696
Oid proc_id
Definition: pltcl.c:187
const char * tag
Definition: event_trigger.h:28
char * internalquery
Definition: elog.h:373
Oid * argtypes
Definition: pltcl.c:168
struct pltcl_interp_desc pltcl_interp_desc
static void pltcl_subtrans_commit(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: pltcl.c:2282
FmgrInfo * arginfuncs
Definition: pltcl.c:169
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:617
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
#define LOG
Definition: elog.h:26
unsigned int Oid
Definition: postgres_ext.h:31
static int pltcl_elog(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
Definition: pltcl.c:1754
HeapTuple * vals
Definition: spi.h:26
void SPI_rollback(void)
Definition: spi.c:333
#define TRIGGER_FIRED_AFTER(event)
Definition: trigger.h:137
Datum oidout(PG_FUNCTION_ARGS)
Definition: oid.c:127
static void pltcl_CreateFileHandler(int fd, int mask, Tcl_FileProc *proc, ClientData clientData)
Definition: pltcl.c:369
void(* callback)(void *arg)
Definition: elog.h:254
FunctionCallInfo fcinfo
Definition: pltcl.c:210
struct ErrorContextCallback * previous
Definition: elog.h:253
#define OidIsValid(objectId)
Definition: c.h:638
static int pltcl_subtransaction(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
Definition: pltcl.c:2886
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:289
static int fd(const char *x, int i)
Definition: preproc-init.c:105
static void throw_tcl_error(Tcl_Interp *interp, const char *proname)
Definition: pltcl.c:1363
ResourceOwner tuple_store_owner
Definition: pltcl.c:229
void FlushErrorState(void)
Definition: elog.c:1574
static void pltcl_AlertNotifier(ClientData clientData)
Definition: pltcl.c:364
uint64 SPI_processed
Definition: spi.c:45
#define TRIGGER_FIRED_FOR_STATEMENT(event)
Definition: trigger.h:131
static bool pltcl_pm_init_done
Definition: pltcl.c:238
static char * pltcl_start_proc
Definition: pltcl.c:236
static Datum pltcl_func_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state, bool pltrusted)
Definition: pltcl.c:800
HeapTuple tg_trigtuple
Definition: trigger.h:35
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:654
signed int int32
Definition: c.h:346
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2112
static int pltcl_SPI_prepare(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
Definition: pltcl.c:2540
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1575
bool fn_retisdomain
Definition: pltcl.c:151
static pltcl_call_state * pltcl_current_call_state
Definition: pltcl.c:244
HeapTupleHeader t_data
Definition: htup.h:68
static int pltcl_quote(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
Definition: pltcl.c:2004
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:468
#define FUNC_MAX_ARGS
char qname[20]
Definition: pltcl.c:165
struct pltcl_call_state pltcl_call_state
static void pltcl_event_trigger_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state, bool pltrusted)
Definition: pltcl.c:1313
Definition: dynahash.c:208
static int pltcl_WaitForEvent(CONST86 Tcl_Time *timePtr)
Definition: pltcl.c:385
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3353
TupleDesc expectedDesc
Definition: execnodes.h:302
static int pltcl_SPI_execute_plan(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
Definition: pltcl.c:2667
void pfree(void *pointer)
Definition: mcxt.c:1056
#define TRIGGER_FIRED_BY_TRUNCATE(event)
Definition: trigger.h:125
AttInMetadata * attinmeta
Definition: pltcl.c:224
void FreeErrorData(ErrorData *edata)
Definition: elog.c:1538
#define UTF_E2U(x)
Definition: pltcl.c:98
struct pltcl_proc_desc pltcl_proc_desc
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
NameData attname
Definition: pg_attribute.h:40
static ClientData pltcl_InitNotifier(void)
Definition: pltcl.c:346
FmgrInfo * arg_out_func
Definition: pltcl.c:155
#define DatumGetCString(X)
Definition: postgres.h:566
void pgstat_init_function_usage(FunctionCallInfo fcinfo, PgStat_FunctionCallUsage *fcu)
Definition: pgstat.c:1622
static void call_pltcl_start_proc(Oid prolang, bool pltrusted)
Definition: pltcl.c:588
const char * filename
Definition: elog.h:354
#define SPI_OPT_NONATOMIC
Definition: spi.h:71
char * tgname
Definition: reltrigger.h:27
#define SPI_OK_INSERT_RETURNING
Definition: spi.h:63
#define FATAL
Definition: elog.h:52
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:124
Oid LookupFuncName(List *funcname, int nargs, const Oid *argtypes, bool missing_ok)
Definition: parse_func.c:2100
const char * event
Definition: event_trigger.h:26
Definition: guc.h:75
PG_FUNCTION_INFO_V1(pltcl_call_handler)
ReturnSetInfo * rsi
Definition: pltcl.c:226
ItemPointerData t_self
Definition: htup.h:65
pltcl_interp_desc * interp_desc
Definition: pltcl.c:145
int SPI_execute_plan(SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only, long tcount)
Definition: spi.c:531
static int pltcl_process_SPI_result(Tcl_Interp *interp, const char *arrayname, Tcl_Obj *loop_body, int spi_rc, SPITupleTable *tuptable, uint64 ntuples)
Definition: pltcl.c:2429
static Datum pltcl_handler(PG_FUNCTION_ARGS, bool pltrusted)
Definition: pltcl.c:719
const char * SPI_result_code_string(int code)
Definition: spi.c:1705
SPIPlanPtr plan
Definition: pltcl.c:166
void tuplestore_puttuple(Tuplestorestate *state, HeapTuple tuple)
Definition: tuplestore.c:730
#define DEBUG2
Definition: elog.h:24
uint32 t_len
Definition: htup.h:64
static void pltcl_SetTimer(CONST86 Tcl_Time *timePtr)
Definition: pltcl.c:359
char * table_name
Definition: elog.h:367
#define FunctionCallInvoke(fcinfo)
Definition: fmgr.h:167
int SPI_keepplan(SPIPlanPtr plan)
Definition: spi.c:752
static char * buf
Definition: pg_test_fsync.c:68
static void pltcl_construct_errorCode(Tcl_Interp *interp, ErrorData *edata)
Definition: pltcl.c:1841
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4462
#define UTF_END
Definition: pltcl.c:90
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:47
static Tcl_Interp * pltcl_hold_interp
Definition: pltcl.c:239
int internalpos
Definition: elog.h:372
#define SPI_OK_UTILITY
Definition: spi.h:56
struct pltcl_query_desc pltcl_query_desc
static char * utf_u2e(const char *src)
Definition: pltcl.c:74
#define SPI_OK_UPDATE_RETURNING
Definition: spi.h:65
ItemPointerData fn_tid
Definition: pltcl.c:142
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#define SPI_OK_REWRITTEN
Definition: spi.h:66
unsigned long fn_refcount
Definition: pltcl.c:140
Tcl_Interp * interp
Definition: pltcl.c:115
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2433
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:134
MemoryContext tuple_store_cxt
Definition: pltcl.c:228
Tuplestorestate * tuple_store
Definition: pltcl.c:227
#define CONST86
Definition: pltcl.c:55
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2641
#define ereport(elevel, rest)
Definition: elog.h:141
static const TclExceptionNameMap exception_name_map[]
Definition: pltcl.c:255
int SPI_register_trigger_data(TriggerData *tdata)
Definition: spi.c:2934
MemoryContext TopMemoryContext
Definition: mcxt.c:44
Oid rd_id
Definition: rel.h:85
pltcl_proc_key proc_key
Definition: pltcl.c:199
#define InvokeFunctionExecuteHook(objectId)
Definition: objectaccess.h:179
static HeapTuple pltcl_trigger_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state, bool pltrusted)
Definition: pltcl.c:1052
#define SPI_OK_SELINTO
Definition: spi.h:58
bool fn_readonly
Definition: pltcl.c:143
TupleDesc ret_tupdesc
Definition: pltcl.c:223
void SPI_commit(void)
Definition: spi.c:278
#define WARNING
Definition: elog.h:40
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:762
#define unconstify(underlying_type, expr)
Definition: c.h:1163
void SPI_freetuptable(SPITupleTable *tuptable)
Definition: spi.c:1162
char ** tgargs
Definition: reltrigger.h:40
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1124
char * datatype_name
Definition: elog.h:369
#define TRIGGER_FIRED_BY_DELETE(event)
Definition: trigger.h:119
char * detail
Definition: elog.h:361
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
#define HASH_BLOBS
Definition: hsearch.h:88
const char * label
Definition: pltcl.c:251
static pltcl_proc_desc * compile_pltcl_function(Oid fn_oid, Oid tgreloid, bool is_event_trigger, bool pltrusted)
Definition: pltcl.c:1392
#define TextDatumGetCString(d)
Definition: builtins.h:84
void * palloc0(Size size)
Definition: mcxt.c:980
AclResult
Definition: acl.h:177
void DefineCustomStringVariable(const char *name, const char *short_desc, const char *long_desc, char **valueAddr, const char *bootValue, GucContext context, int flags, GucStringCheckHook check_hook, GucStringAssignHook assign_hook, GucShowHook show_hook)
Definition: guc.c:8659
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:316
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
#define UTF_U2E(x)
Definition: pltcl.c:95
static char * utf_e2u(const char *src)
Definition: pltcl.c:80
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1385
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2063
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:458
Datum pltclu_call_handler(PG_FUNCTION_ARGS)
Definition: pltcl.c:708
Size keysize
Definition: hsearch.h:72
int work_mem
Definition: globals.c:121
TupleDesc tupdesc
Definition: spi.h:25
void domain_check(Datum value, bool isnull, Oid domainType, void **extra, MemoryContext mcxt)
Definition: domains.c:327
Oid result_typioparam
Definition: pltcl.c:148
Trigger * tg_trigger
Definition: trigger.h:37
HeapTuple tg_newtuple
Definition: trigger.h:36
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:134
void parseTypeString(const char *str, Oid *typeid_p, int32 *typmod_p, bool missing_ok)
Definition: parse_type.c:833
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1531
#define SPI_OK_SELECT
Definition: spi.h:57
char * SPI_getrelname(Relation rel)
Definition: spi.c:1102
#define InvalidOid
Definition: postgres_ext.h:36
char * column_name
Definition: elog.h:368
int allowedModes
Definition: execnodes.h:303
static HeapTuple pltcl_build_tuple_result(Tcl_Interp *interp, Tcl_Obj **kvObjv, int kvObjc, pltcl_call_state *call_state)
Definition: pltcl.c:3177
#define NOTICE
Definition: elog.h:37
#define LOCAL_FCINFO(name, nargs)
Definition: fmgr.h:110
#define UTF_BEGIN
Definition: pltcl.c:85
char * internal_proname
Definition: pltcl.c:138
SetFunctionReturnMode returnMode
Definition: execnodes.h:305
#define PG_CATCH()
Definition: elog.h:310
#define PG_ARGISNULL(n)
Definition: fmgr.h:204
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:25
#define Assert(condition)
Definition: c.h:732
pltcl_proc_desc * proc_ptr
Definition: pltcl.c:200
pltcl_proc_desc * prodesc
Definition: pltcl.c:216
Oid is_trigger
Definition: pltcl.c:193
char * hint
Definition: elog.h:363
TriggerEvent tg_event
Definition: trigger.h:33
void BeginInternalSubTransaction(const char *name)
Definition: xact.c:4357
char * SPI_getnspname(Relation rel)
Definition: spi.c:1108
static int pltcl_returnnext(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
Definition: pltcl.c:2150
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:150
void _PG_init(void)
Definition: pltcl.c:400
FormData_pg_type * Form_pg_type
Definition: pg_type.h:251
#define SPI_OK_FINISH
Definition: spi.h:54
void MemoryContextSetIdentifier(MemoryContext context, const char *id)
Definition: mcxt.c:329
#define HeapTupleHeaderGetRawXmin(tup)
Definition: htup_details.h:308
Oid * argtypioparams
Definition: pltcl.c:170
#define PG_RE_THROW()
Definition: elog.h:331
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:221
char * context
Definition: elog.h:364
static pltcl_interp_desc * pltcl_fetch_interp(Oid prolang, bool pltrusted)
Definition: pltcl.c:558
static void start_proc_error_callback(void *arg)
Definition: pltcl.c:676
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:231
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:29
Tuplestorestate * setResult
Definition: execnodes.h:308
void pgstat_end_function_usage(PgStat_FunctionCallUsage *fcu, bool finalize)
Definition: pgstat.c:1694
TransactionId fn_xmin
Definition: pltcl.c:141
static HTAB * pltcl_proc_htab
Definition: pltcl.c:241
Oid result_typid
Definition: pltcl.c:146
#define TRIGGER_FIRED_BEFORE(event)
Definition: trigger.h:134
static void pltcl_FinalizeNotifier(ClientData clientData)
Definition: pltcl.c:354
static Datum values[MAXATTR]
Definition: bootstrap.c:167
#define TRIGGER_FIRED_INSTEAD(event)
Definition: trigger.h:140
ExprContext * econtext
Definition: execnodes.h:301
List * stringToQualifiedNameList(const char *string)
Definition: regproc.c:1687
#define TRIGGER_FIRED_BY_INSERT(event)
Definition: trigger.h:116
MemoryContext fn_cxt
Definition: pltcl.c:139
bool fn_retisset
Definition: pltcl.c:149
TupleDesc setDesc
Definition: execnodes.h:309
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1655
Oid user_id
Definition: pltcl.c:194
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define SPI_OK_UPDATE
Definition: spi.h:61
bool * arg_is_rowtype
Definition: pltcl.c:156
#define ACL_EXECUTE
Definition: parsenodes.h:81
#define elog(elevel,...)
Definition: elog.h:226
AclResult pg_proc_aclcheck(Oid proc_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4655
int i
Oid getTypeIOParam(HeapTuple typeTuple)
Definition: lsyscache.c:2081
void pg_bindtextdomain(const char *domain)
Definition: miscinit.c:1607
static HTAB * pltcl_interp_htab
Definition: pltcl.c:240
int16 tgnargs
Definition: reltrigger.h:37
#define NameStr(name)
Definition: c.h:609
#define errcontext
Definition: elog.h:183
void * arg
#define PG_FUNCTION_ARGS
Definition: fmgr.h:188
static char * pltclu_start_proc
Definition: pltcl.c:237
void SPI_start_transaction(void)
Definition: spi.c:212
#define SPI_OK_INSERT
Definition: spi.h:59
TriggerData * trigdata
Definition: pltcl.c:213
static int pltcl_returnnull(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
Definition: pltcl.c:2112
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:123
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:581
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:122
#define PG_TRY()
Definition: elog.h:301
#define TRIGGER_FIRED_FOR_ROW(event)
Definition: trigger.h:128
char * constraint_name
Definition: elog.h:370
static void pltcl_set_tuple_values(Tcl_Interp *interp, const char *arrayname, uint64 tupno, HeapTuple tuple, TupleDesc tupdesc)
Definition: pltcl.c:3015
Definition: pg_list.h:50
#define snprintf
Definition: port.h:192
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition: trigger.h:122
#define RelationGetRelid(relation)
Definition: rel.h:419
#define PG_END_TRY()
Definition: elog.h:317
static Tcl_Obj * pltcl_build_tuple_argument(HeapTuple tuple, TupleDesc tupdesc, bool include_generated)
Definition: pltcl.c:3101
char * message
Definition: elog.h:360
Tcl_HashTable query_hash
Definition: pltcl.c:116
static int pltcl_SPI_execute(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
Definition: pltcl.c:2320
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:496
Relation tg_relation
Definition: trigger.h:34
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:452