PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pltcl.c File Reference
#include "postgres.h"
#include <tcl.h>
#include <unistd.h>
#include <fcntl.h>
#include "access/htup_details.h"
#include "access/xact.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "commands/event_trigger.h"
#include "commands/trigger.h"
#include "executor/spi.h"
#include "fmgr.h"
#include "funcapi.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "parser/parse_func.h"
#include "parser/parse_type.h"
#include "pgstat.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/regproc.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
#include "pltclerrcodes.h"
Include dependency graph for pltcl.c:

Go to the source code of this file.

Data Structures

struct  pltcl_interp_desc
 
struct  pltcl_proc_desc
 
struct  pltcl_query_desc
 
struct  pltcl_proc_key
 
struct  pltcl_proc_ptr
 
struct  pltcl_call_state
 
struct  TclExceptionNameMap
 

Macros

#define HAVE_TCL_VERSION(maj, min)
 
#define CONST86
 
#define TEXTDOMAIN   PG_TEXTDOMAIN("pltcl")
 
#define UTF_BEGIN
 
#define UTF_END
 
#define UTF_U2E(x)   (_pltcl_utf_dst = utf_u2e(_pltcl_utf_src = (x)))
 
#define UTF_E2U(x)   (_pltcl_utf_dst = utf_e2u(_pltcl_utf_src = (x)))
 

Typedefs

typedef struct pltcl_interp_desc pltcl_interp_desc
 
typedef struct pltcl_proc_desc pltcl_proc_desc
 
typedef struct pltcl_query_desc pltcl_query_desc
 
typedef struct pltcl_proc_key pltcl_proc_key
 
typedef struct pltcl_proc_ptr pltcl_proc_ptr
 
typedef struct pltcl_call_state pltcl_call_state
 

Functions

static char * utf_u2e (const char *src)
 
static char * utf_e2u (const char *src)
 
void _PG_init (void)
 
static void pltcl_init_interp (pltcl_interp_desc *interp_desc, Oid prolang, bool pltrusted)
 
static pltcl_interp_descpltcl_fetch_interp (Oid prolang, bool pltrusted)
 
static void call_pltcl_start_proc (Oid prolang, bool pltrusted)
 
static void start_proc_error_callback (void *arg)
 
static Datum pltcl_handler (PG_FUNCTION_ARGS, bool pltrusted)
 
static Datum pltcl_func_handler (PG_FUNCTION_ARGS, pltcl_call_state *call_state, bool pltrusted)
 
static HeapTuple pltcl_trigger_handler (PG_FUNCTION_ARGS, pltcl_call_state *call_state, bool pltrusted)
 
static void pltcl_event_trigger_handler (PG_FUNCTION_ARGS, pltcl_call_state *call_state, bool pltrusted)
 
static void throw_tcl_error (Tcl_Interp *interp, const char *proname)
 
static pltcl_proc_desccompile_pltcl_function (Oid fn_oid, Oid tgreloid, bool is_event_trigger, bool pltrusted)
 
static int pltcl_elog (ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
 
static void pltcl_construct_errorCode (Tcl_Interp *interp, ErrorData *edata)
 
static const char * pltcl_get_condition_name (int sqlstate)
 
static int pltcl_quote (ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
 
static int pltcl_argisnull (ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
 
static int pltcl_returnnull (ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
 
static int pltcl_returnnext (ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
 
static int pltcl_SPI_execute (ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
 
static int pltcl_process_SPI_result (Tcl_Interp *interp, const char *arrayname, Tcl_Obj *loop_body, int spi_rc, SPITupleTable *tuptable, uint64 ntuples)
 
static int pltcl_SPI_prepare (ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
 
static int pltcl_SPI_execute_plan (ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
 
static int pltcl_SPI_lastoid (ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
 
static int pltcl_subtransaction (ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
 
static void pltcl_subtrans_begin (MemoryContext oldcontext, ResourceOwner oldowner)
 
static void pltcl_subtrans_commit (MemoryContext oldcontext, ResourceOwner oldowner)
 
static void pltcl_subtrans_abort (Tcl_Interp *interp, MemoryContext oldcontext, ResourceOwner oldowner)
 
static void pltcl_set_tuple_values (Tcl_Interp *interp, const char *arrayname, uint64 tupno, HeapTuple tuple, TupleDesc tupdesc)
 
static Tcl_Obj * pltcl_build_tuple_argument (HeapTuple tuple, TupleDesc tupdesc)
 
static HeapTuple pltcl_build_tuple_result (Tcl_Interp *interp, Tcl_Obj **kvObjv, int kvObjc, pltcl_call_state *call_state)
 
static void pltcl_init_tuple_store (pltcl_call_state *call_state)
 
static ClientData pltcl_InitNotifier (void)
 
static void pltcl_FinalizeNotifier (ClientData clientData)
 
static void pltcl_SetTimer (CONST86 Tcl_Time *timePtr)
 
static void pltcl_AlertNotifier (ClientData clientData)
 
static void pltcl_CreateFileHandler (int fd, int mask, Tcl_FileProc *proc, ClientData clientData)
 
static void pltcl_DeleteFileHandler (int fd)
 
static void pltcl_ServiceModeHook (int mode)
 
static int pltcl_WaitForEvent (CONST86 Tcl_Time *timePtr)
 
 PG_FUNCTION_INFO_V1 (pltcl_call_handler)
 
Datum pltcl_call_handler (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (pltclu_call_handler)
 
Datum pltclu_call_handler (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 
static char * pltcl_start_proc = NULL
 
static char * pltclu_start_proc = NULL
 
static bool pltcl_pm_init_done = false
 
static Tcl_Interp * pltcl_hold_interp = NULL
 
static HTABpltcl_interp_htab = NULL
 
static HTABpltcl_proc_htab = NULL
 
static pltcl_call_statepltcl_current_call_state = NULL
 
static const TclExceptionNameMap exception_name_map []
 

Macro Definition Documentation

#define CONST86

Definition at line 55 of file pltcl.c.

#define HAVE_TCL_VERSION (   maj,
  min 
)
Value:
((TCL_MAJOR_VERSION > maj) || \
(TCL_MAJOR_VERSION == maj && TCL_MINOR_VERSION >= min))

Definition at line 44 of file pltcl.c.

#define TEXTDOMAIN   PG_TEXTDOMAIN("pltcl")

Definition at line 60 of file pltcl.c.

Referenced by _PG_init().

#define UTF_BEGIN
#define UTF_E2U (   x)    (_pltcl_utf_dst = utf_e2u(_pltcl_utf_src = (x)))
#define UTF_END
Value:
if (_pltcl_utf_src != (const char *) _pltcl_utf_dst) \
pfree(_pltcl_utf_dst); \
} while (0)
void pfree(void *pointer)
Definition: mcxt.c:950

Definition at line 90 of file pltcl.c.

Referenced by compile_pltcl_function(), pltcl_build_tuple_argument(), pltcl_construct_errorCode(), pltcl_elog(), pltcl_func_handler(), pltcl_set_tuple_values(), pltcl_SPI_execute(), pltcl_SPI_execute_plan(), pltcl_SPI_prepare(), and pltcl_subtrans_abort().

#define UTF_U2E (   x)    (_pltcl_utf_dst = utf_u2e(_pltcl_utf_src = (x)))

Definition at line 95 of file pltcl.c.

Referenced by pltcl_elog(), pltcl_SPI_execute(), pltcl_SPI_execute_plan(), and pltcl_SPI_prepare().

Typedef Documentation

Function Documentation

void _PG_init ( void  )

Definition at line 395 of file pltcl.c.

References DefineCustomStringVariable(), elog, HASHCTL::entrysize, ERROR, gettext_noop, HASH_BLOBS, hash_create(), HASH_ELEM, HASHCTL::keysize, NULL, pg_bindtextdomain(), PGC_SUSET, pltcl_AlertNotifier(), pltcl_CreateFileHandler(), pltcl_DeleteFileHandler(), pltcl_FinalizeNotifier(), pltcl_hold_interp, pltcl_InitNotifier(), pltcl_pm_init_done, pltcl_ServiceModeHook(), pltcl_SetTimer(), pltcl_start_proc, pltcl_WaitForEvent(), pltclu_start_proc, and TEXTDOMAIN.

396 {
397  Tcl_NotifierProcs notifier;
398  HASHCTL hash_ctl;
399 
400  /* Be sure we do initialization only once (should be redundant now) */
401  if (pltcl_pm_init_done)
402  return;
403 
405 
406 #ifdef WIN32
407  /* Required on win32 to prevent error loading init.tcl */
408  Tcl_FindExecutable("");
409 #endif
410 
411  /*
412  * Override the functions in the Notifier subsystem. See comments above.
413  */
414  notifier.setTimerProc = pltcl_SetTimer;
415  notifier.waitForEventProc = pltcl_WaitForEvent;
416  notifier.createFileHandlerProc = pltcl_CreateFileHandler;
417  notifier.deleteFileHandlerProc = pltcl_DeleteFileHandler;
418  notifier.initNotifierProc = pltcl_InitNotifier;
419  notifier.finalizeNotifierProc = pltcl_FinalizeNotifier;
420  notifier.alertNotifierProc = pltcl_AlertNotifier;
421  notifier.serviceModeHookProc = pltcl_ServiceModeHook;
422  Tcl_SetNotifier(&notifier);
423 
424  /************************************************************
425  * Create the dummy hold interpreter to prevent close of
426  * stdout and stderr on DeleteInterp
427  ************************************************************/
428  if ((pltcl_hold_interp = Tcl_CreateInterp()) == NULL)
429  elog(ERROR, "could not create master Tcl interpreter");
430  if (Tcl_Init(pltcl_hold_interp) == TCL_ERROR)
431  elog(ERROR, "could not initialize master Tcl interpreter");
432 
433  /************************************************************
434  * Create the hash table for working interpreters
435  ************************************************************/
436  memset(&hash_ctl, 0, sizeof(hash_ctl));
437  hash_ctl.keysize = sizeof(Oid);
438  hash_ctl.entrysize = sizeof(pltcl_interp_desc);
439  pltcl_interp_htab = hash_create("PL/Tcl interpreters",
440  8,
441  &hash_ctl,
443 
444  /************************************************************
445  * Create the hash table for function lookup
446  ************************************************************/
447  memset(&hash_ctl, 0, sizeof(hash_ctl));
448  hash_ctl.keysize = sizeof(pltcl_proc_key);
449  hash_ctl.entrysize = sizeof(pltcl_proc_ptr);
450  pltcl_proc_htab = hash_create("PL/Tcl functions",
451  100,
452  &hash_ctl,
454 
455  /************************************************************
456  * Define PL/Tcl's custom GUCs
457  ************************************************************/
458  DefineCustomStringVariable("pltcl.start_proc",
459  gettext_noop("PL/Tcl function to call once when pltcl is first used."),
460  NULL,
462  NULL,
463  PGC_SUSET, 0,
464  NULL, NULL, NULL);
465  DefineCustomStringVariable("pltclu.start_proc",
466  gettext_noop("PL/TclU function to call once when pltclu is first used."),
467  NULL,
469  NULL,
470  PGC_SUSET, 0,
471  NULL, NULL, NULL);
472 
473  pltcl_pm_init_done = true;
474 }
#define TEXTDOMAIN
Definition: pltcl.c:60
struct pltcl_proc_ptr pltcl_proc_ptr
#define HASH_ELEM
Definition: hsearch.h:87
struct pltcl_proc_key pltcl_proc_key
static void pltcl_DeleteFileHandler(int fd)
Definition: pltcl.c:370
Size entrysize
Definition: hsearch.h:73
#define gettext_noop(x)
Definition: c.h:139
static void pltcl_ServiceModeHook(int mode)
Definition: pltcl.c:375
struct pltcl_interp_desc pltcl_interp_desc
unsigned int Oid
Definition: postgres_ext.h:31
static void pltcl_CreateFileHandler(int fd, int mask, Tcl_FileProc *proc, ClientData clientData)
Definition: pltcl.c:364
static void pltcl_AlertNotifier(ClientData clientData)
Definition: pltcl.c:359
static bool pltcl_pm_init_done
Definition: pltcl.c:235
static char * pltcl_start_proc
Definition: pltcl.c:233
static int pltcl_WaitForEvent(CONST86 Tcl_Time *timePtr)
Definition: pltcl.c:380
#define ERROR
Definition: elog.h:43
static ClientData pltcl_InitNotifier(void)
Definition: pltcl.c:341
Definition: guc.h:75
static void pltcl_SetTimer(CONST86 Tcl_Time *timePtr)
Definition: pltcl.c:354
static Tcl_Interp * pltcl_hold_interp
Definition: pltcl.c:236
#define HASH_BLOBS
Definition: hsearch.h:88
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:7815
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:301
Size keysize
Definition: hsearch.h:72
#define NULL
Definition: c.h:229
static HTAB * pltcl_proc_htab
Definition: pltcl.c:238
static void pltcl_FinalizeNotifier(ClientData clientData)
Definition: pltcl.c:349
void pg_bindtextdomain(const char *domain)
Definition: miscinit.c:1502
static HTAB * pltcl_interp_htab
Definition: pltcl.c:237
static char * pltclu_start_proc
Definition: pltcl.c:234
#define elog
Definition: elog.h:219
static void call_pltcl_start_proc ( Oid  prolang,
bool  pltrusted 
)
static

Definition at line 581 of file pltcl.c.

References ACL_EXECUTE, ACL_KIND_PROC, aclcheck_error(), ACLCHECK_OK, ErrorContextCallback::arg, ErrorContextCallback::callback, elog, ereport, errcode(), errmsg(), ERROR, error_context_stack, fmgr_info(), FunctionCallInvoke, GETSTRUCT, GetUserId(), HeapTupleIsValid, InitFunctionCallInfoData, InvalidOid, InvokeFunctionExecuteHook, LookupFuncName(), NULL, ObjectIdGetDatum, pg_proc_aclcheck(), pgstat_end_function_usage(), pgstat_init_function_usage(), pltcl_start_proc, pltclu_start_proc, ErrorContextCallback::previous, PROCOID, ReleaseSysCache(), SearchSysCache1, start_proc_error_callback(), and stringToQualifiedNameList().

Referenced by pltcl_init_interp().

582 {
583  char *start_proc;
584  const char *gucname;
585  ErrorContextCallback errcallback;
586  List *namelist;
587  Oid fargtypes[1]; /* dummy */
588  Oid procOid;
589  HeapTuple procTup;
590  Form_pg_proc procStruct;
591  AclResult aclresult;
592  FmgrInfo finfo;
593  FunctionCallInfoData fcinfo;
594  PgStat_FunctionCallUsage fcusage;
595 
596  /* select appropriate GUC */
597  start_proc = pltrusted ? pltcl_start_proc : pltclu_start_proc;
598  gucname = pltrusted ? "pltcl.start_proc" : "pltclu.start_proc";
599 
600  /* Nothing to do if it's empty or unset */
601  if (start_proc == NULL || start_proc[0] == '\0')
602  return;
603 
604  /* Set up errcontext callback to make errors more helpful */
605  errcallback.callback = start_proc_error_callback;
606  errcallback.arg = (void *) gucname;
607  errcallback.previous = error_context_stack;
608  error_context_stack = &errcallback;
609 
610  /* Parse possibly-qualified identifier and look up the function */
611  namelist = stringToQualifiedNameList(start_proc);
612  procOid = LookupFuncName(namelist, 0, fargtypes, false);
613 
614  /* Current user must have permission to call function */
615  aclresult = pg_proc_aclcheck(procOid, GetUserId(), ACL_EXECUTE);
616  if (aclresult != ACLCHECK_OK)
617  aclcheck_error(aclresult, ACL_KIND_PROC, start_proc);
618 
619  /* Get the function's pg_proc entry */
620  procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(procOid));
621  if (!HeapTupleIsValid(procTup))
622  elog(ERROR, "cache lookup failed for function %u", procOid);
623  procStruct = (Form_pg_proc) GETSTRUCT(procTup);
624 
625  /* It must be same language as the function we're currently calling */
626  if (procStruct->prolang != prolang)
627  ereport(ERROR,
628  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
629  errmsg("function \"%s\" is in the wrong language",
630  start_proc)));
631 
632  /*
633  * It must not be SECURITY DEFINER, either. This together with the
634  * language match check ensures that the function will execute in the same
635  * Tcl interpreter we just finished initializing.
636  */
637  if (procStruct->prosecdef)
638  ereport(ERROR,
639  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
640  errmsg("function \"%s\" must not be SECURITY DEFINER",
641  start_proc)));
642 
643  /* A-OK */
644  ReleaseSysCache(procTup);
645 
646  /*
647  * Call the function using the normal SQL function call mechanism. We
648  * could perhaps cheat and jump directly to pltcl_handler(), but it seems
649  * better to do it this way so that the call is exposed to, eg, call
650  * statistics collection.
651  */
652  InvokeFunctionExecuteHook(procOid);
653  fmgr_info(procOid, &finfo);
654  InitFunctionCallInfoData(fcinfo, &finfo,
655  0,
656  InvalidOid, NULL, NULL);
657  pgstat_init_function_usage(&fcinfo, &fcusage);
658  (void) FunctionCallInvoke(&fcinfo);
659  pgstat_end_function_usage(&fcusage, true);
660 
661  /* Pop the error context stack */
662  error_context_stack = errcallback.previous;
663 }
Definition: fmgr.h:56
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Oid GetUserId(void)
Definition: miscinit.c:283
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
struct ErrorContextCallback * previous
Definition: elog.h:238
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:156
static char * pltcl_start_proc
Definition: pltcl.c:233
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:127
#define FunctionCallInvoke(fcinfo)
Definition: fmgr.h:137
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3399
#define ereport(elevel, rest)
Definition: elog.h:122
#define InvokeFunctionExecuteHook(objectId)
Definition: objectaccess.h:179
AclResult
Definition: acl.h:170
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:120
static void start_proc_error_callback(void *arg)
Definition: pltcl.c:669
void pgstat_end_function_usage(PgStat_FunctionCallUsage *fcu, bool finalize)
Definition: pgstat.c:1653
Oid LookupFuncName(List *funcname, int nargs, const Oid *argtypes, bool noError)
Definition: parse_func.c:1929
void pgstat_init_function_usage(FunctionCallInfoData *fcinfo, PgStat_FunctionCallUsage *fcu)
Definition: pgstat.c:1581
List * stringToQualifiedNameList(const char *string)
Definition: regproc.c:1687
void(* callback)(void *arg)
Definition: elog.h:239
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define ACL_EXECUTE
Definition: parsenodes.h:79
AclResult pg_proc_aclcheck(Oid proc_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4446
static char * pltclu_start_proc
Definition: pltcl.c:234
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
static pltcl_proc_desc * compile_pltcl_function ( Oid  fn_oid,
Oid  tgreloid,
bool  is_event_trigger,
bool  pltrusted 
)
static

Definition at line 1356 of file pltcl.c.

References ALLOCSET_SMALL_SIZES, AllocSetContextCreate(), Anum_pg_proc_prosrc, pltcl_proc_desc::arg_is_rowtype, pltcl_proc_desc::arg_out_func, Assert, buf, elog, ereport, errcode(), errmsg(), ERROR, EVTTRIGGEROID, fmgr_info_cxt(), pltcl_proc_desc::fn_cxt, pltcl_proc_desc::fn_readonly, pltcl_proc_desc::fn_refcount, pltcl_proc_desc::fn_retisset, pltcl_proc_desc::fn_retistuple, pltcl_proc_desc::fn_tid, pltcl_proc_desc::fn_xmin, format_type_be(), FUNC_MAX_ARGS, GETSTRUCT, getTypeIOParam(), GetUserId(), HASH_ENTER, hash_search(), HeapTupleHeaderGetRawXmin, HeapTupleIsValid, i, pltcl_proc_desc::internal_proname, pltcl_interp_desc::interp, pltcl_proc_desc::interp_desc, InvalidOid, pltcl_proc_key::is_trigger, ItemPointerEquals(), pltcl_proc_desc::lanpltrusted, MemoryContextDelete(), MemoryContextSwitchTo(), NameStr, pltcl_proc_desc::nargs, NULL, ObjectIdGetDatum, OidIsValid, palloc0(), pfree(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, pltcl_fetch_interp(), pltcl_proc_key::proc_id, pltcl_proc_ptr::proc_ptr, PROCOID, PROVOLATILE_VOLATILE, pstrdup(), RECORDOID, ReleaseSysCache(), pltcl_proc_desc::result_in_func, pltcl_proc_desc::result_typioparam, SearchSysCache1, snprintf(), SysCacheGetAttr(), HeapTupleData::t_data, HeapTupleData::t_self, TextDatumGetCString, TopMemoryContext, TRIGGEROID, TYPEOID, TYPTYPE_COMPOSITE, TYPTYPE_PSEUDO, pltcl_proc_key::user_id, pltcl_proc_desc::user_proname, UTF_BEGIN, UTF_E2U, UTF_END, utf_u2e(), and VOIDOID.

Referenced by pltcl_event_trigger_handler(), pltcl_func_handler(), and pltcl_trigger_handler().

1358 {
1359  HeapTuple procTup;
1360  Form_pg_proc procStruct;
1361  pltcl_proc_key proc_key;
1362  pltcl_proc_ptr *proc_ptr;
1363  bool found;
1364  pltcl_proc_desc *prodesc;
1365  pltcl_proc_desc *old_prodesc;
1366  volatile MemoryContext proc_cxt = NULL;
1367  Tcl_DString proc_internal_def;
1368  Tcl_DString proc_internal_body;
1369 
1370  /* We'll need the pg_proc tuple in any case... */
1371  procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(fn_oid));
1372  if (!HeapTupleIsValid(procTup))
1373  elog(ERROR, "cache lookup failed for function %u", fn_oid);
1374  procStruct = (Form_pg_proc) GETSTRUCT(procTup);
1375 
1376  /*
1377  * Look up function in pltcl_proc_htab; if it's not there, create an entry
1378  * and set the entry's proc_ptr to NULL.
1379  */
1380  proc_key.proc_id = fn_oid;
1381  proc_key.is_trigger = OidIsValid(tgreloid);
1382  proc_key.user_id = pltrusted ? GetUserId() : InvalidOid;
1383 
1384  proc_ptr = hash_search(pltcl_proc_htab, &proc_key,
1385  HASH_ENTER,
1386  &found);
1387  if (!found)
1388  proc_ptr->proc_ptr = NULL;
1389 
1390  prodesc = proc_ptr->proc_ptr;
1391 
1392  /************************************************************
1393  * If it's present, must check whether it's still up to date.
1394  * This is needed because CREATE OR REPLACE FUNCTION can modify the
1395  * function's pg_proc entry without changing its OID.
1396  ************************************************************/
1397  if (prodesc != NULL &&
1398  prodesc->fn_xmin == HeapTupleHeaderGetRawXmin(procTup->t_data) &&
1399  ItemPointerEquals(&prodesc->fn_tid, &procTup->t_self))
1400  {
1401  /* It's still up-to-date, so we can use it */
1402  ReleaseSysCache(procTup);
1403  return prodesc;
1404  }
1405 
1406  /************************************************************
1407  * If we haven't found it in the hashtable, we analyze
1408  * the functions arguments and returntype and store
1409  * the in-/out-functions in the prodesc block and create
1410  * a new hashtable entry for it.
1411  *
1412  * Then we load the procedure into the Tcl interpreter.
1413  ************************************************************/
1414  Tcl_DStringInit(&proc_internal_def);
1415  Tcl_DStringInit(&proc_internal_body);
1416  PG_TRY();
1417  {
1418  bool is_trigger = OidIsValid(tgreloid);
1419  char internal_proname[128];
1420  HeapTuple typeTup;
1421  Form_pg_type typeStruct;
1422  char proc_internal_args[33 * FUNC_MAX_ARGS];
1423  Datum prosrcdatum;
1424  bool isnull;
1425  char *proc_source;
1426  char buf[32];
1427  Tcl_Interp *interp;
1428  int i;
1429  int tcl_rc;
1430  MemoryContext oldcontext;
1431 
1432  /************************************************************
1433  * Build our internal proc name from the function's Oid. Append
1434  * "_trigger" when appropriate to ensure the normal and trigger
1435  * cases are kept separate. Note name must be all-ASCII.
1436  ************************************************************/
1437  if (is_event_trigger)
1438  snprintf(internal_proname, sizeof(internal_proname),
1439  "__PLTcl_proc_%u_evttrigger", fn_oid);
1440  else if (is_trigger)
1441  snprintf(internal_proname, sizeof(internal_proname),
1442  "__PLTcl_proc_%u_trigger", fn_oid);
1443  else
1444  snprintf(internal_proname, sizeof(internal_proname),
1445  "__PLTcl_proc_%u", fn_oid);
1446 
1447  /************************************************************
1448  * Allocate a context that will hold all PG data for the procedure.
1449  * We use the internal proc name as the context name.
1450  ************************************************************/
1452  internal_proname,
1454 
1455  /************************************************************
1456  * Allocate and fill a new procedure description block.
1457  * struct prodesc and subsidiary data must all live in proc_cxt.
1458  ************************************************************/
1459  oldcontext = MemoryContextSwitchTo(proc_cxt);
1460  prodesc = (pltcl_proc_desc *) palloc0(sizeof(pltcl_proc_desc));
1461  prodesc->user_proname = pstrdup(NameStr(procStruct->proname));
1462  prodesc->internal_proname = pstrdup(internal_proname);
1463  prodesc->fn_cxt = proc_cxt;
1464  prodesc->fn_refcount = 0;
1465  prodesc->fn_xmin = HeapTupleHeaderGetRawXmin(procTup->t_data);
1466  prodesc->fn_tid = procTup->t_self;
1467  prodesc->nargs = procStruct->pronargs;
1468  prodesc->arg_out_func = (FmgrInfo *) palloc0(prodesc->nargs * sizeof(FmgrInfo));
1469  prodesc->arg_is_rowtype = (bool *) palloc0(prodesc->nargs * sizeof(bool));
1470  MemoryContextSwitchTo(oldcontext);
1471 
1472  /* Remember if function is STABLE/IMMUTABLE */
1473  prodesc->fn_readonly =
1474  (procStruct->provolatile != PROVOLATILE_VOLATILE);
1475  /* And whether it is trusted */
1476  prodesc->lanpltrusted = pltrusted;
1477 
1478  /************************************************************
1479  * Identify the interpreter to use for the function
1480  ************************************************************/
1481  prodesc->interp_desc = pltcl_fetch_interp(procStruct->prolang,
1482  prodesc->lanpltrusted);
1483  interp = prodesc->interp_desc->interp;
1484 
1485  /************************************************************
1486  * Get the required information for input conversion of the
1487  * return value.
1488  ************************************************************/
1489  if (!is_trigger && !is_event_trigger)
1490  {
1491  typeTup =
1493  ObjectIdGetDatum(procStruct->prorettype));
1494  if (!HeapTupleIsValid(typeTup))
1495  elog(ERROR, "cache lookup failed for type %u",
1496  procStruct->prorettype);
1497  typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1498 
1499  /* Disallow pseudotype result, except VOID and RECORD */
1500  if (typeStruct->typtype == TYPTYPE_PSEUDO)
1501  {
1502  if (procStruct->prorettype == VOIDOID ||
1503  procStruct->prorettype == RECORDOID)
1504  /* okay */ ;
1505  else if (procStruct->prorettype == TRIGGEROID ||
1506  procStruct->prorettype == EVTTRIGGEROID)
1507  ereport(ERROR,
1508  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1509  errmsg("trigger functions can only be called as triggers")));
1510  else
1511  ereport(ERROR,
1512  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1513  errmsg("PL/Tcl functions cannot return type %s",
1514  format_type_be(procStruct->prorettype))));
1515  }
1516 
1517  fmgr_info_cxt(typeStruct->typinput,
1518  &(prodesc->result_in_func),
1519  proc_cxt);
1520  prodesc->result_typioparam = getTypeIOParam(typeTup);
1521 
1522  prodesc->fn_retisset = procStruct->proretset;
1523  prodesc->fn_retistuple = (procStruct->prorettype == RECORDOID ||
1524  typeStruct->typtype == TYPTYPE_COMPOSITE);
1525 
1526  ReleaseSysCache(typeTup);
1527  }
1528 
1529  /************************************************************
1530  * Get the required information for output conversion
1531  * of all procedure arguments, and set up argument naming info.
1532  ************************************************************/
1533  if (!is_trigger && !is_event_trigger)
1534  {
1535  proc_internal_args[0] = '\0';
1536  for (i = 0; i < prodesc->nargs; i++)
1537  {
1538  typeTup = SearchSysCache1(TYPEOID,
1539  ObjectIdGetDatum(procStruct->proargtypes.values[i]));
1540  if (!HeapTupleIsValid(typeTup))
1541  elog(ERROR, "cache lookup failed for type %u",
1542  procStruct->proargtypes.values[i]);
1543  typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1544 
1545  /* Disallow pseudotype argument */
1546  if (typeStruct->typtype == TYPTYPE_PSEUDO)
1547  ereport(ERROR,
1548  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1549  errmsg("PL/Tcl functions cannot accept type %s",
1550  format_type_be(procStruct->proargtypes.values[i]))));
1551 
1552  if (typeStruct->typtype == TYPTYPE_COMPOSITE)
1553  {
1554  prodesc->arg_is_rowtype[i] = true;
1555  snprintf(buf, sizeof(buf), "__PLTcl_Tup_%d", i + 1);
1556  }
1557  else
1558  {
1559  prodesc->arg_is_rowtype[i] = false;
1560  fmgr_info_cxt(typeStruct->typoutput,
1561  &(prodesc->arg_out_func[i]),
1562  proc_cxt);
1563  snprintf(buf, sizeof(buf), "%d", i + 1);
1564  }
1565 
1566  if (i > 0)
1567  strcat(proc_internal_args, " ");
1568  strcat(proc_internal_args, buf);
1569 
1570  ReleaseSysCache(typeTup);
1571  }
1572  }
1573  else if (is_trigger)
1574  {
1575  /* trigger procedure has fixed args */
1576  strcpy(proc_internal_args,
1577  "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");
1578  }
1579  else if (is_event_trigger)
1580  {
1581  /* event trigger procedure has fixed args */
1582  strcpy(proc_internal_args, "TG_event TG_tag");
1583  }
1584 
1585  /************************************************************
1586  * Create the tcl command to define the internal
1587  * procedure
1588  *
1589  * Leave this code as DString - performance is not critical here,
1590  * and we don't want to duplicate the knowledge of the Tcl quoting
1591  * rules that's embedded in Tcl_DStringAppendElement.
1592  ************************************************************/
1593  Tcl_DStringAppendElement(&proc_internal_def, "proc");
1594  Tcl_DStringAppendElement(&proc_internal_def, internal_proname);
1595  Tcl_DStringAppendElement(&proc_internal_def, proc_internal_args);
1596 
1597  /************************************************************
1598  * prefix procedure body with
1599  * upvar #0 <internal_procname> GD
1600  * and with appropriate setting of arguments
1601  ************************************************************/
1602  Tcl_DStringAppend(&proc_internal_body, "upvar #0 ", -1);
1603  Tcl_DStringAppend(&proc_internal_body, internal_proname, -1);
1604  Tcl_DStringAppend(&proc_internal_body, " GD\n", -1);
1605  if (is_trigger)
1606  {
1607  Tcl_DStringAppend(&proc_internal_body,
1608  "array set NEW $__PLTcl_Tup_NEW\n", -1);
1609  Tcl_DStringAppend(&proc_internal_body,
1610  "array set OLD $__PLTcl_Tup_OLD\n", -1);
1611  Tcl_DStringAppend(&proc_internal_body,
1612  "set i 0\n"
1613  "set v 0\n"
1614  "foreach v $args {\n"
1615  " incr i\n"
1616  " set $i $v\n"
1617  "}\n"
1618  "unset i v\n\n", -1);
1619  }
1620  else if (is_event_trigger)
1621  {
1622  /* no argument support for event triggers */
1623  }
1624  else
1625  {
1626  for (i = 0; i < prodesc->nargs; i++)
1627  {
1628  if (prodesc->arg_is_rowtype[i])
1629  {
1630  snprintf(buf, sizeof(buf),
1631  "array set %d $__PLTcl_Tup_%d\n",
1632  i + 1, i + 1);
1633  Tcl_DStringAppend(&proc_internal_body, buf, -1);
1634  }
1635  }
1636  }
1637 
1638  /************************************************************
1639  * Add user's function definition to proc body
1640  ************************************************************/
1641  prosrcdatum = SysCacheGetAttr(PROCOID, procTup,
1642  Anum_pg_proc_prosrc, &isnull);
1643  if (isnull)
1644  elog(ERROR, "null prosrc");
1645  proc_source = TextDatumGetCString(prosrcdatum);
1646  UTF_BEGIN;
1647  Tcl_DStringAppend(&proc_internal_body, UTF_E2U(proc_source), -1);
1648  UTF_END;
1649  pfree(proc_source);
1650  Tcl_DStringAppendElement(&proc_internal_def,
1651  Tcl_DStringValue(&proc_internal_body));
1652 
1653  /************************************************************
1654  * Create the procedure in the interpreter
1655  ************************************************************/
1656  tcl_rc = Tcl_EvalEx(interp,
1657  Tcl_DStringValue(&proc_internal_def),
1658  Tcl_DStringLength(&proc_internal_def),
1659  TCL_EVAL_GLOBAL);
1660  if (tcl_rc != TCL_OK)
1661  ereport(ERROR,
1662  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
1663  errmsg("could not create internal procedure \"%s\": %s",
1664  internal_proname,
1665  utf_u2e(Tcl_GetStringResult(interp)))));
1666  }
1667  PG_CATCH();
1668  {
1669  /*
1670  * If we failed anywhere above, clean up whatever got allocated. It
1671  * should all be in the proc_cxt, except for the DStrings.
1672  */
1673  if (proc_cxt)
1674  MemoryContextDelete(proc_cxt);
1675  Tcl_DStringFree(&proc_internal_def);
1676  Tcl_DStringFree(&proc_internal_body);
1677  PG_RE_THROW();
1678  }
1679  PG_END_TRY();
1680 
1681  /*
1682  * Install the new proc description block in the hashtable, incrementing
1683  * its refcount (the hashtable link counts as a reference). Then, if
1684  * there was a previous definition of the function, decrement that one's
1685  * refcount, and delete it if no longer referenced. The order of
1686  * operations here is important: if something goes wrong during the
1687  * MemoryContextDelete, leaking some memory for the old definition is OK,
1688  * but we don't want to corrupt the live hashtable entry. (Likewise,
1689  * freeing the DStrings is pretty low priority if that happens.)
1690  */
1691  old_prodesc = proc_ptr->proc_ptr;
1692 
1693  proc_ptr->proc_ptr = prodesc;
1694  prodesc->fn_refcount++;
1695 
1696  if (old_prodesc != NULL)
1697  {
1698  Assert(old_prodesc->fn_refcount > 0);
1699  if (--old_prodesc->fn_refcount == 0)
1700  MemoryContextDelete(old_prodesc->fn_cxt);
1701  }
1702 
1703  Tcl_DStringFree(&proc_internal_def);
1704  Tcl_DStringFree(&proc_internal_body);
1705 
1706  ReleaseSysCache(procTup);
1707 
1708  return prodesc;
1709 }
bool lanpltrusted
Definition: pltcl.c:144
Definition: fmgr.h:56
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
bool fn_retistuple
Definition: pltcl.c:149
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Oid GetUserId(void)
Definition: miscinit.c:283
#define TYPTYPE_COMPOSITE
Definition: pg_type.h:721
FmgrInfo result_in_func
Definition: pltcl.c:146
char * pstrdup(const char *in)
Definition: mcxt.c:1077
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:175
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
char * user_proname
Definition: pltcl.c:137
#define Anum_pg_proc_prosrc
Definition: pg_proc.h:115
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
Oid proc_id
Definition: pltcl.c:184
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
FormData_pg_type * Form_pg_type
Definition: pg_type.h:233
#define OidIsValid(objectId)
Definition: c.h:538
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:156
HeapTupleHeader t_data
Definition: htup.h:67
#define FUNC_MAX_ARGS
void pfree(void *pointer)
Definition: mcxt.c:950
#define VOIDOID
Definition: pg_type.h:690
#define UTF_E2U(x)
Definition: pltcl.c:98
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
FmgrInfo * arg_out_func
Definition: pltcl.c:152
#define TRIGGEROID
Definition: pg_type.h:692
ItemPointerData t_self
Definition: htup.h:65
pltcl_interp_desc * interp_desc
Definition: pltcl.c:145
static char * buf
Definition: pg_test_fsync.c:66
#define UTF_END
Definition: pltcl.c:90
static char * utf_u2e(const char *src)
Definition: pltcl.c:74
ItemPointerData fn_tid
Definition: pltcl.c:142
#define RECORDOID
Definition: pg_type.h:680
unsigned long fn_refcount
Definition: pltcl.c:140
Tcl_Interp * interp
Definition: pltcl.c:115
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:137
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext TopMemoryContext
Definition: mcxt.c:43
#define PROVOLATILE_VOLATILE
Definition: pg_proc.h:5488
bool fn_readonly
Definition: pltcl.c:143
#define TextDatumGetCString(d)
Definition: builtins.h:92
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
void * palloc0(Size size)
Definition: mcxt.c:878
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1279
Oid result_typioparam
Definition: pltcl.c:147
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define InvalidOid
Definition: postgres_ext.h:36
#define UTF_BEGIN
Definition: pltcl.c:85
char * internal_proname
Definition: pltcl.c:138
#define PG_CATCH()
Definition: elog.h:293
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define EVTTRIGGEROID
Definition: pg_type.h:694
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
pltcl_proc_desc * proc_ptr
Definition: pltcl.c:197
Oid is_trigger
Definition: pltcl.c:190
#define HeapTupleHeaderGetRawXmin(tup)
Definition: htup_details.h:302
#define PG_RE_THROW()
Definition: elog.h:314
static pltcl_interp_desc * pltcl_fetch_interp(Oid prolang, bool pltrusted)
Definition: pltcl.c:551
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:29
#define TYPTYPE_PSEUDO
Definition: pg_type.h:724
TransactionId fn_xmin
Definition: pltcl.c:141
static HTAB * pltcl_proc_htab
Definition: pltcl.c:238
MemoryContext fn_cxt
Definition: pltcl.c:139
bool fn_retisset
Definition: pltcl.c:148
Oid user_id
Definition: pltcl.c:191
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool * arg_is_rowtype
Definition: pltcl.c:153
int i
Oid getTypeIOParam(HeapTuple typeTuple)
Definition: lsyscache.c:2053
#define NameStr(name)
Definition: c.h:499
#define elog
Definition: elog.h:219
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
PG_FUNCTION_INFO_V1 ( pltcl_call_handler  )
PG_FUNCTION_INFO_V1 ( pltclu_call_handler  )
static void pltcl_AlertNotifier ( ClientData  clientData)
static

Definition at line 359 of file pltcl.c.

Referenced by _PG_init().

360 {
361 }
static int pltcl_argisnull ( ClientData  cdata,
Tcl_Interp *  interp,
int  objc,
Tcl_Obj *const  objv[] 
)
static

Definition at line 2020 of file pltcl.c.

References pltcl_call_state::fcinfo, FunctionCallInfoData::nargs, NULL, and PG_ARGISNULL.

Referenced by pltcl_init_interp().

2022 {
2023  int argno;
2025 
2026  /************************************************************
2027  * Check call syntax
2028  ************************************************************/
2029  if (objc != 2)
2030  {
2031  Tcl_WrongNumArgs(interp, 1, objv, "argno");
2032  return TCL_ERROR;
2033  }
2034 
2035  /************************************************************
2036  * Check that we're called as a normal function
2037  ************************************************************/
2038  if (fcinfo == NULL)
2039  {
2040  Tcl_SetObjResult(interp,
2041  Tcl_NewStringObj("argisnull cannot be used in triggers", -1));
2042  return TCL_ERROR;
2043  }
2044 
2045  /************************************************************
2046  * Get the argument number
2047  ************************************************************/
2048  if (Tcl_GetIntFromObj(interp, objv[1], &argno) != TCL_OK)
2049  return TCL_ERROR;
2050 
2051  /************************************************************
2052  * Check that the argno is valid
2053  ************************************************************/
2054  argno--;
2055  if (argno < 0 || argno >= fcinfo->nargs)
2056  {
2057  Tcl_SetObjResult(interp,
2058  Tcl_NewStringObj("argno out of range", -1));
2059  return TCL_ERROR;
2060  }
2061 
2062  /************************************************************
2063  * Get the requested NULL state
2064  ************************************************************/
2065  Tcl_SetObjResult(interp, Tcl_NewBooleanObj(PG_ARGISNULL(argno)));
2066  return TCL_OK;
2067 }
FunctionCallInfo fcinfo
Definition: pltcl.c:207
static pltcl_call_state * pltcl_current_call_state
Definition: pltcl.c:241
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define NULL
Definition: c.h:229
static Tcl_Obj * pltcl_build_tuple_argument ( HeapTuple  tuple,
TupleDesc  tupdesc 
)
static

Definition at line 3003 of file pltcl.c.

References tupleDesc::attrs, getTypeOutputInfo(), heap_getattr, i, NameStr, tupleDesc::natts, NULL, OidOutputFunctionCall(), pfree(), UTF_BEGIN, UTF_E2U, and UTF_END.

Referenced by pltcl_func_handler(), and pltcl_trigger_handler().

3004 {
3005  Tcl_Obj *retobj = Tcl_NewObj();
3006  int i;
3007  char *outputstr;
3008  Datum attr;
3009  bool isnull;
3010  char *attname;
3011  Oid typoutput;
3012  bool typisvarlena;
3013 
3014  for (i = 0; i < tupdesc->natts; i++)
3015  {
3016  /* ignore dropped attributes */
3017  if (tupdesc->attrs[i]->attisdropped)
3018  continue;
3019 
3020  /************************************************************
3021  * Get the attribute name
3022  ************************************************************/
3023  attname = NameStr(tupdesc->attrs[i]->attname);
3024 
3025  /************************************************************
3026  * Get the attributes value
3027  ************************************************************/
3028  attr = heap_getattr(tuple, i + 1, tupdesc, &isnull);
3029 
3030  /************************************************************
3031  * If there is a value, append the attribute name and the
3032  * value to the list
3033  *
3034  * Hmmm - Null attributes will cause functions to
3035  * crash if they don't expect them - need something
3036  * smarter here.
3037  ************************************************************/
3038  if (!isnull)
3039  {
3040  getTypeOutputInfo(tupdesc->attrs[i]->atttypid,
3041  &typoutput, &typisvarlena);
3042  outputstr = OidOutputFunctionCall(typoutput, attr);
3043  UTF_BEGIN;
3044  Tcl_ListObjAppendElement(NULL, retobj,
3045  Tcl_NewStringObj(UTF_E2U(attname), -1));
3046  UTF_END;
3047  UTF_BEGIN;
3048  Tcl_ListObjAppendElement(NULL, retobj,
3049  Tcl_NewStringObj(UTF_E2U(outputstr), -1));
3050  UTF_END;
3051  pfree(outputstr);
3052  }
3053  }
3054 
3055  return retobj;
3056 }
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2632
Form_pg_attribute * attrs
Definition: tupdesc.h:74
unsigned int Oid
Definition: postgres_ext.h:31
int natts
Definition: tupdesc.h:73
void pfree(void *pointer)
Definition: mcxt.c:950
#define UTF_E2U(x)
Definition: pltcl.c:98
#define UTF_END
Definition: pltcl.c:90
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:769
uintptr_t Datum
Definition: postgres.h:372
#define UTF_BEGIN
Definition: pltcl.c:85
#define NULL
Definition: c.h:229
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1747
int i
#define NameStr(name)
Definition: c.h:499
static HeapTuple pltcl_build_tuple_result ( Tcl_Interp *  interp,
Tcl_Obj **  kvObjv,
int  kvObjc,
pltcl_call_state call_state 
)
static

Definition at line 3070 of file pltcl.c.

References pltcl_call_state::attinmeta, BuildTupleFromCStrings(), elog, ereport, errcode(), errmsg(), ERROR, i, tupleDesc::natts, NULL, palloc0(), RelationGetDescr, pltcl_call_state::ret_tupdesc, SPI_ERROR_NOATTRIBUTE, SPI_fnumber(), TriggerData::tg_relation, pltcl_call_state::trigdata, TupleDescGetAttInMetadata(), utf_u2e(), and values.

Referenced by pltcl_func_handler(), pltcl_returnnext(), and pltcl_trigger_handler().

3072 {
3073  TupleDesc tupdesc;
3074  AttInMetadata *attinmeta;
3075  char **values;
3076  int i;
3077 
3078  if (call_state->ret_tupdesc)
3079  {
3080  tupdesc = call_state->ret_tupdesc;
3081  attinmeta = call_state->attinmeta;
3082  }
3083  else if (call_state->trigdata)
3084  {
3085  tupdesc = RelationGetDescr(call_state->trigdata->tg_relation);
3086  attinmeta = TupleDescGetAttInMetadata(tupdesc);
3087  }
3088  else
3089  {
3090  elog(ERROR, "PL/Tcl function does not return a tuple");
3091  tupdesc = NULL; /* keep compiler quiet */
3092  attinmeta = NULL;
3093  }
3094 
3095  values = (char **) palloc0(tupdesc->natts * sizeof(char *));
3096 
3097  if (kvObjc % 2 != 0)
3098  ereport(ERROR,
3099  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3100  errmsg("column name/value list must have even number of elements")));
3101 
3102  for (i = 0; i < kvObjc; i += 2)
3103  {
3104  char *fieldName = utf_u2e(Tcl_GetString(kvObjv[i]));
3105  int attn = SPI_fnumber(tupdesc, fieldName);
3106 
3107  /*
3108  * We silently ignore ".tupno", if it's present but doesn't match any
3109  * actual output column. This allows direct use of a row returned by
3110  * pltcl_set_tuple_values().
3111  */
3112  if (attn == SPI_ERROR_NOATTRIBUTE)
3113  {
3114  if (strcmp(fieldName, ".tupno") == 0)
3115  continue;
3116  ereport(ERROR,
3117  (errcode(ERRCODE_UNDEFINED_COLUMN),
3118  errmsg("column name/value list contains nonexistent column name \"%s\"",
3119  fieldName)));
3120  }
3121 
3122  if (attn <= 0)
3123  ereport(ERROR,
3124  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3125  errmsg("cannot set system attribute \"%s\"",
3126  fieldName)));
3127 
3128  values[attn - 1] = utf_u2e(Tcl_GetString(kvObjv[i + 1]));
3129  }
3130 
3131  return BuildTupleFromCStrings(attinmeta, values);
3132 }
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:761
#define RelationGetDescr(relation)
Definition: rel.h:428
int errcode(int sqlerrcode)
Definition: elog.c:575
int natts
Definition: tupdesc.h:73
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:1115
AttInMetadata * attinmeta
Definition: pltcl.c:221
#define ERROR
Definition: elog.h:43
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:44
static char * utf_u2e(const char *src)
Definition: pltcl.c:74
#define ereport(elevel, rest)
Definition: elog.h:122
TupleDesc ret_tupdesc
Definition: pltcl.c:220
void * palloc0(Size size)
Definition: mcxt.c:878
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:1068
#define NULL
Definition: c.h:229
static Datum values[MAXATTR]
Definition: bootstrap.c:163
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define elog
Definition: elog.h:219
TriggerData * trigdata
Definition: pltcl.c:210
Relation tg_relation
Definition: trigger.h:34
Datum pltcl_call_handler ( PG_FUNCTION_ARGS  )

Definition at line 689 of file pltcl.c.

References pltcl_handler().

690 {
691  return pltcl_handler(fcinfo, true);
692 }
static Datum pltcl_handler(PG_FUNCTION_ARGS, bool pltrusted)
Definition: pltcl.c:712
static void pltcl_construct_errorCode ( Tcl_Interp *  interp,
ErrorData edata 
)
static

Definition at line 1803 of file pltcl.c.

References ErrorData::column_name, ErrorData::constraint_name, ErrorData::context, ErrorData::datatype_name, ErrorData::detail, ErrorData::filename, ErrorData::funcname, ErrorData::hint, ErrorData::internalpos, ErrorData::internalquery, ErrorData::lineno, ErrorData::message, pltcl_get_condition_name(), ErrorData::schema_name, ErrorData::sqlerrcode, ErrorData::table_name, unpack_sql_state(), UTF_BEGIN, UTF_E2U, and UTF_END.

Referenced by pltcl_elog(), and pltcl_subtrans_abort().

1804 {
1805  Tcl_Obj *obj = Tcl_NewObj();
1806 
1807  Tcl_ListObjAppendElement(interp, obj,
1808  Tcl_NewStringObj("POSTGRES", -1));
1809  Tcl_ListObjAppendElement(interp, obj,
1810  Tcl_NewStringObj(PG_VERSION, -1));
1811  Tcl_ListObjAppendElement(interp, obj,
1812  Tcl_NewStringObj("SQLSTATE", -1));
1813  Tcl_ListObjAppendElement(interp, obj,
1814  Tcl_NewStringObj(unpack_sql_state(edata->sqlerrcode), -1));
1815  Tcl_ListObjAppendElement(interp, obj,
1816  Tcl_NewStringObj("condition", -1));
1817  Tcl_ListObjAppendElement(interp, obj,
1818  Tcl_NewStringObj(pltcl_get_condition_name(edata->sqlerrcode), -1));
1819  Tcl_ListObjAppendElement(interp, obj,
1820  Tcl_NewStringObj("message", -1));
1821  UTF_BEGIN;
1822  Tcl_ListObjAppendElement(interp, obj,
1823  Tcl_NewStringObj(UTF_E2U(edata->message), -1));
1824  UTF_END;
1825  if (edata->detail)
1826  {
1827  Tcl_ListObjAppendElement(interp, obj,
1828  Tcl_NewStringObj("detail", -1));
1829  UTF_BEGIN;
1830  Tcl_ListObjAppendElement(interp, obj,
1831  Tcl_NewStringObj(UTF_E2U(edata->detail), -1));
1832  UTF_END;
1833  }
1834  if (edata->hint)
1835  {
1836  Tcl_ListObjAppendElement(interp, obj,
1837  Tcl_NewStringObj("hint", -1));
1838  UTF_BEGIN;
1839  Tcl_ListObjAppendElement(interp, obj,
1840  Tcl_NewStringObj(UTF_E2U(edata->hint), -1));
1841  UTF_END;
1842  }
1843  if (edata->context)
1844  {
1845  Tcl_ListObjAppendElement(interp, obj,
1846  Tcl_NewStringObj("context", -1));
1847  UTF_BEGIN;
1848  Tcl_ListObjAppendElement(interp, obj,
1849  Tcl_NewStringObj(UTF_E2U(edata->context), -1));
1850  UTF_END;
1851  }
1852  if (edata->schema_name)
1853  {
1854  Tcl_ListObjAppendElement(interp, obj,
1855  Tcl_NewStringObj("schema", -1));
1856  UTF_BEGIN;
1857  Tcl_ListObjAppendElement(interp, obj,
1858  Tcl_NewStringObj(UTF_E2U(edata->schema_name), -1));
1859  UTF_END;
1860  }
1861  if (edata->table_name)
1862  {
1863  Tcl_ListObjAppendElement(interp, obj,
1864  Tcl_NewStringObj("table", -1));
1865  UTF_BEGIN;
1866  Tcl_ListObjAppendElement(interp, obj,
1867  Tcl_NewStringObj(UTF_E2U(edata->table_name), -1));
1868  UTF_END;
1869  }
1870  if (edata->column_name)
1871  {
1872  Tcl_ListObjAppendElement(interp, obj,
1873  Tcl_NewStringObj("column", -1));
1874  UTF_BEGIN;
1875  Tcl_ListObjAppendElement(interp, obj,
1876  Tcl_NewStringObj(UTF_E2U(edata->column_name), -1));
1877  UTF_END;
1878  }
1879  if (edata->datatype_name)
1880  {
1881  Tcl_ListObjAppendElement(interp, obj,
1882  Tcl_NewStringObj("datatype", -1));
1883  UTF_BEGIN;
1884  Tcl_ListObjAppendElement(interp, obj,
1885  Tcl_NewStringObj(UTF_E2U(edata->datatype_name), -1));
1886  UTF_END;
1887  }
1888  if (edata->constraint_name)
1889  {
1890  Tcl_ListObjAppendElement(interp, obj,
1891  Tcl_NewStringObj("constraint", -1));
1892  UTF_BEGIN;
1893  Tcl_ListObjAppendElement(interp, obj,
1894  Tcl_NewStringObj(UTF_E2U(edata->constraint_name), -1));
1895  UTF_END;
1896  }
1897  /* cursorpos is never interesting here; report internal query/pos */
1898  if (edata->internalquery)
1899  {
1900  Tcl_ListObjAppendElement(interp, obj,
1901  Tcl_NewStringObj("statement", -1));
1902  UTF_BEGIN;
1903  Tcl_ListObjAppendElement(interp, obj,
1904  Tcl_NewStringObj(UTF_E2U(edata->internalquery), -1));
1905  UTF_END;
1906  }
1907  if (edata->internalpos > 0)
1908  {
1909  Tcl_ListObjAppendElement(interp, obj,
1910  Tcl_NewStringObj("cursor_position", -1));
1911  Tcl_ListObjAppendElement(interp, obj,
1912  Tcl_NewIntObj(edata->internalpos));
1913  }
1914  if (edata->filename)
1915  {
1916  Tcl_ListObjAppendElement(interp, obj,
1917  Tcl_NewStringObj("filename", -1));
1918  UTF_BEGIN;
1919  Tcl_ListObjAppendElement(interp, obj,
1920  Tcl_NewStringObj(UTF_E2U(edata->filename), -1));
1921  UTF_END;
1922  }
1923  if (edata->lineno > 0)
1924  {
1925  Tcl_ListObjAppendElement(interp, obj,
1926  Tcl_NewStringObj("lineno", -1));
1927  Tcl_ListObjAppendElement(interp, obj,
1928  Tcl_NewIntObj(edata->lineno));
1929  }
1930  if (edata->funcname)
1931  {
1932  Tcl_ListObjAppendElement(interp, obj,
1933  Tcl_NewStringObj("funcname", -1));
1934  UTF_BEGIN;
1935  Tcl_ListObjAppendElement(interp, obj,
1936  Tcl_NewStringObj(UTF_E2U(edata->funcname), -1));
1937  UTF_END;
1938  }
1939 
1940  Tcl_SetObjErrorCode(interp, obj);
1941 }
char * schema_name
Definition: elog.h:349
int sqlerrcode
Definition: elog.h:342
const char * funcname
Definition: elog.h:339
char * unpack_sql_state(int sql_state)
Definition: elog.c:2844
int lineno
Definition: elog.h:338
static const char * pltcl_get_condition_name(int sqlstate)
Definition: pltcl.c:1948
char * internalquery
Definition: elog.h:356
#define UTF_E2U(x)
Definition: pltcl.c:98
const char * filename
Definition: elog.h:337
char * table_name
Definition: elog.h:350
#define UTF_END
Definition: pltcl.c:90
int internalpos
Definition: elog.h:355
char * datatype_name
Definition: elog.h:352
char * detail
Definition: elog.h:344
char * column_name
Definition: elog.h:351
#define UTF_BEGIN
Definition: pltcl.c:85
char * hint
Definition: elog.h:346
char * context
Definition: elog.h:347
char * constraint_name
Definition: elog.h:353
char * message
Definition: elog.h:343
static void pltcl_CreateFileHandler ( int  fd,
int  mask,
Tcl_FileProc *  proc,
ClientData  clientData 
)
static

Definition at line 364 of file pltcl.c.

Referenced by _PG_init().

366 {
367 }
static void pltcl_DeleteFileHandler ( int  fd)
static

Definition at line 370 of file pltcl.c.

Referenced by _PG_init().

371 {
372 }
static int pltcl_elog ( ClientData  cdata,
Tcl_Interp *  interp,
int  objc,
Tcl_Obj *const  objv[] 
)
static

Definition at line 1716 of file pltcl.c.

References CopyErrorData(), CurrentMemoryContext, DEBUG2, ereport, errcode(), errmsg(), ERROR, FATAL, FlushErrorState(), FreeErrorData(), INFO, LOG, MemoryContextSwitchTo(), ErrorData::message, NOTICE, NULL, PG_CATCH, PG_END_TRY, PG_TRY, pltcl_construct_errorCode(), UTF_BEGIN, UTF_E2U, UTF_END, UTF_U2E, and WARNING.

Referenced by pltcl_init_interp().

1718 {
1719  volatile int level;
1720  MemoryContext oldcontext;
1721  int priIndex;
1722 
1723  static const char *logpriorities[] = {
1724  "DEBUG", "LOG", "INFO", "NOTICE",
1725  "WARNING", "ERROR", "FATAL", (const char *) NULL
1726  };
1727 
1728  static const int loglevels[] = {
1729  DEBUG2, LOG, INFO, NOTICE,
1730  WARNING, ERROR, FATAL
1731  };
1732 
1733  if (objc != 3)
1734  {
1735  Tcl_WrongNumArgs(interp, 1, objv, "level msg");
1736  return TCL_ERROR;
1737  }
1738 
1739  if (Tcl_GetIndexFromObj(interp, objv[1], logpriorities, "priority",
1740  TCL_EXACT, &priIndex) != TCL_OK)
1741  return TCL_ERROR;
1742 
1743  level = loglevels[priIndex];
1744 
1745  if (level == ERROR)
1746  {
1747  /*
1748  * We just pass the error back to Tcl. If it's not caught, it'll
1749  * eventually get converted to a PG error when we reach the call
1750  * handler.
1751  */
1752  Tcl_SetObjResult(interp, objv[2]);
1753  return TCL_ERROR;
1754  }
1755 
1756  /*
1757  * For non-error messages, just pass 'em to ereport(). We do not expect
1758  * that this will fail, but just on the off chance it does, report the
1759  * error back to Tcl. Note we are assuming that ereport() can't have any
1760  * internal failures that are so bad as to require a transaction abort.
1761  *
1762  * This path is also used for FATAL errors, which aren't going to come
1763  * back to us at all.
1764  */
1765  oldcontext = CurrentMemoryContext;
1766  PG_TRY();
1767  {
1768  UTF_BEGIN;
1769  ereport(level,
1770  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
1771  errmsg("%s", UTF_U2E(Tcl_GetString(objv[2])))));
1772  UTF_END;
1773  }
1774  PG_CATCH();
1775  {
1776  ErrorData *edata;
1777 
1778  /* Must reset elog.c's state */
1779  MemoryContextSwitchTo(oldcontext);
1780  edata = CopyErrorData();
1781  FlushErrorState();
1782 
1783  /* Pass the error data to Tcl */
1784  pltcl_construct_errorCode(interp, edata);
1785  UTF_BEGIN;
1786  Tcl_SetObjResult(interp, Tcl_NewStringObj(UTF_E2U(edata->message), -1));
1787  UTF_END;
1788  FreeErrorData(edata);
1789 
1790  return TCL_ERROR;
1791  }
1792  PG_END_TRY();
1793 
1794  return TCL_OK;
1795 }
ErrorData * CopyErrorData(void)
Definition: elog.c:1497
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
#define INFO
Definition: elog.h:33
#define LOG
Definition: elog.h:26
void FlushErrorState(void)
Definition: elog.c:1587
void FreeErrorData(ErrorData *edata)
Definition: elog.c:1551
#define UTF_E2U(x)
Definition: pltcl.c:98
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
#define DEBUG2
Definition: elog.h:24
static void pltcl_construct_errorCode(Tcl_Interp *interp, ErrorData *edata)
Definition: pltcl.c:1803
#define UTF_END
Definition: pltcl.c:90
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define ereport(elevel, rest)
Definition: elog.h:122
#define WARNING
Definition: elog.h:40
#define UTF_U2E(x)
Definition: pltcl.c:95
#define NOTICE
Definition: elog.h:37
#define UTF_BEGIN
Definition: pltcl.c:85
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:229
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
char * message
Definition: elog.h:343
static void pltcl_event_trigger_handler ( PG_FUNCTION_ARGS  ,
pltcl_call_state call_state,
bool  pltrusted 
)
static

Definition at line 1277 of file pltcl.c.

References compile_pltcl_function(), elog, ERROR, EventTriggerData::event, pltcl_proc_desc::fn_refcount, pltcl_proc_desc::internal_proname, pltcl_interp_desc::interp, pltcl_proc_desc::interp_desc, InvalidOid, NULL, pltcl_call_state::prodesc, SPI_connect(), SPI_finish(), SPI_OK_CONNECT, SPI_OK_FINISH, EventTriggerData::tag, throw_tcl_error(), pltcl_proc_desc::user_proname, and utf_e2u().

Referenced by pltcl_handler().

1279 {
1280  pltcl_proc_desc *prodesc;
1281  Tcl_Interp *volatile interp;
1282  EventTriggerData *tdata = (EventTriggerData *) fcinfo->context;
1283  Tcl_Obj *tcl_cmd;
1284  int tcl_rc;
1285 
1286  /* Connect to SPI manager */
1287  if (SPI_connect() != SPI_OK_CONNECT)
1288  elog(ERROR, "could not connect to SPI manager");
1289 
1290  /* Find or compile the function */
1291  prodesc = compile_pltcl_function(fcinfo->flinfo->fn_oid,
1292  InvalidOid, true, pltrusted);
1293 
1294  call_state->prodesc = prodesc;
1295  prodesc->fn_refcount++;
1296 
1297  interp = prodesc->interp_desc->interp;
1298 
1299  /* Create the tcl command and call the internal proc */
1300  tcl_cmd = Tcl_NewObj();
1301  Tcl_IncrRefCount(tcl_cmd);
1302  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1303  Tcl_NewStringObj(prodesc->internal_proname, -1));
1304  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1305  Tcl_NewStringObj(utf_e2u(tdata->event), -1));
1306  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1307  Tcl_NewStringObj(utf_e2u(tdata->tag), -1));
1308 
1309  tcl_rc = Tcl_EvalObjEx(interp, tcl_cmd, (TCL_EVAL_DIRECT | TCL_EVAL_GLOBAL));
1310 
1311  /* Release refcount to free tcl_cmd (and all subsidiary objects) */
1312  Tcl_DecrRefCount(tcl_cmd);
1313 
1314  /* Check for errors reported by Tcl. */
1315  if (tcl_rc != TCL_OK)
1316  throw_tcl_error(interp, prodesc->user_proname);
1317 
1318  if (SPI_finish() != SPI_OK_FINISH)
1319  elog(ERROR, "SPI_finish() failed");
1320 }
#define SPI_OK_CONNECT
Definition: spi.h:50
int SPI_connect(void)
Definition: spi.c:84
int SPI_finish(void)
Definition: spi.c:148
char * user_proname
Definition: pltcl.c:137
const char * tag
Definition: event_trigger.h:28
static void throw_tcl_error(Tcl_Interp *interp, const char *proname)
Definition: pltcl.c:1327
#define ERROR
Definition: elog.h:43
const char * event
Definition: event_trigger.h:26
pltcl_interp_desc * interp_desc
Definition: pltcl.c:145
unsigned long fn_refcount
Definition: pltcl.c:140
Tcl_Interp * interp
Definition: pltcl.c:115
static pltcl_proc_desc * compile_pltcl_function(Oid fn_oid, Oid tgreloid, bool is_event_trigger, bool pltrusted)
Definition: pltcl.c:1356
static char * utf_e2u(const char *src)
Definition: pltcl.c:80
#define InvalidOid
Definition: postgres_ext.h:36
char * internal_proname
Definition: pltcl.c:138
#define NULL
Definition: c.h:229
pltcl_proc_desc * prodesc
Definition: pltcl.c:213
#define SPI_OK_FINISH
Definition: spi.h:51
#define elog
Definition: elog.h:219
static pltcl_interp_desc * pltcl_fetch_interp ( Oid  prolang,
bool  pltrusted 
)
static

Definition at line 551 of file pltcl.c.

References GetUserId(), HASH_ENTER, hash_search(), pltcl_interp_desc::interp, InvalidOid, NULL, and pltcl_init_interp().

Referenced by compile_pltcl_function().

552 {
553  Oid user_id;
554  pltcl_interp_desc *interp_desc;
555  bool found;
556 
557  /* Find or create the interpreter hashtable entry for this userid */
558  if (pltrusted)
559  user_id = GetUserId();
560  else
561  user_id = InvalidOid;
562 
563  interp_desc = hash_search(pltcl_interp_htab, &user_id,
564  HASH_ENTER,
565  &found);
566  if (!found)
567  interp_desc->interp = NULL;
568 
569  /* If we haven't yet successfully made an interpreter, try to do that */
570  if (!interp_desc->interp)
571  pltcl_init_interp(interp_desc, prolang, pltrusted);
572 
573  return interp_desc;
574 }
static void pltcl_init_interp(pltcl_interp_desc *interp_desc, Oid prolang, bool pltrusted)
Definition: pltcl.c:480
Oid GetUserId(void)
Definition: miscinit.c:283
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
unsigned int Oid
Definition: postgres_ext.h:31
Tcl_Interp * interp
Definition: pltcl.c:115
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:229
static HTAB * pltcl_interp_htab
Definition: pltcl.c:237
static void pltcl_FinalizeNotifier ( ClientData  clientData)
static

Definition at line 349 of file pltcl.c.

Referenced by _PG_init().

350 {
351 }
static Datum pltcl_func_handler ( PG_FUNCTION_ARGS  ,
pltcl_call_state call_state,
bool  pltrusted 
)
static

Definition at line 793 of file pltcl.c.

References ReturnSetInfo::allowedModes, pltcl_proc_desc::arg_is_rowtype, pltcl_proc_desc::arg_out_func, Assert, pltcl_call_state::attinmeta, compile_pltcl_function(), CreateTupleDescCopy(), CurrentResourceOwner, DatumGetHeapTupleHeader, ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, elog, ereport, errcode(), errmsg(), ERROR, pltcl_proc_desc::fn_refcount, pltcl_proc_desc::fn_retisset, pltcl_proc_desc::fn_retistuple, get_call_result_type(), HeapTupleGetDatum, HeapTupleHeaderGetDatumLength, HeapTupleHeaderGetTypeId, HeapTupleHeaderGetTypMod, i, InputFunctionCall(), pltcl_proc_desc::internal_proname, pltcl_interp_desc::interp, pltcl_proc_desc::interp_desc, InvalidOid, IsA, lookup_rowtype_tupdesc(), MemoryContextSwitchTo(), pltcl_proc_desc::nargs, NULL, OutputFunctionCall(), pfree(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, pltcl_build_tuple_argument(), pltcl_build_tuple_result(), pltcl_call_state::prodesc, ReleaseTupleDesc, pltcl_proc_desc::result_in_func, pltcl_proc_desc::result_typioparam, pltcl_call_state::ret_tupdesc, ReturnSetInfo::returnMode, pltcl_call_state::rsi, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, SPI_connect(), SPI_finish(), SPI_OK_CONNECT, SPI_OK_FINISH, HeapTupleData::t_data, HeapTupleData::t_len, throw_tcl_error(), pltcl_call_state::tuple_store, pltcl_call_state::tuple_store_cxt, pltcl_call_state::tuple_store_owner, TupleDescGetAttInMetadata(), TYPEFUNC_COMPOSITE, pltcl_proc_desc::user_proname, UTF_BEGIN, UTF_E2U, UTF_END, and utf_u2e().

Referenced by pltcl_handler().

795 {
796  pltcl_proc_desc *prodesc;
797  Tcl_Interp *volatile interp;
798  Tcl_Obj *tcl_cmd;
799  int i;
800  int tcl_rc;
801  Datum retval;
802 
803  /* Connect to SPI manager */
804  if (SPI_connect() != SPI_OK_CONNECT)
805  elog(ERROR, "could not connect to SPI manager");
806 
807  /* Find or compile the function */
808  prodesc = compile_pltcl_function(fcinfo->flinfo->fn_oid, InvalidOid,
809  false, pltrusted);
810 
811  call_state->prodesc = prodesc;
812  prodesc->fn_refcount++;
813 
814  interp = prodesc->interp_desc->interp;
815 
816  /*
817  * If we're a SRF, check caller can handle materialize mode, and save
818  * relevant info into call_state. We must ensure that the returned
819  * tuplestore is owned by the caller's context, even if we first create it
820  * inside a subtransaction.
821  */
822  if (prodesc->fn_retisset)
823  {
824  ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
825 
826  if (!rsi || !IsA(rsi, ReturnSetInfo) ||
827  (rsi->allowedModes & SFRM_Materialize) == 0)
828  ereport(ERROR,
829  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
830  errmsg("set-valued function called in context that cannot accept a set")));
831 
832  call_state->rsi = rsi;
833  call_state->tuple_store_cxt = rsi->econtext->ecxt_per_query_memory;
835  }
836 
837  /************************************************************
838  * Create the tcl command to call the internal
839  * proc in the Tcl interpreter
840  ************************************************************/
841  tcl_cmd = Tcl_NewObj();
842  Tcl_ListObjAppendElement(NULL, tcl_cmd,
843  Tcl_NewStringObj(prodesc->internal_proname, -1));
844 
845  /* We hold a refcount on tcl_cmd just to be sure it stays around */
846  Tcl_IncrRefCount(tcl_cmd);
847 
848  /************************************************************
849  * Add all call arguments to the command
850  ************************************************************/
851  PG_TRY();
852  {
853  for (i = 0; i < prodesc->nargs; i++)
854  {
855  if (prodesc->arg_is_rowtype[i])
856  {
857  /**************************************************
858  * For tuple values, add a list for 'array set ...'
859  **************************************************/
860  if (fcinfo->argnull[i])
861  Tcl_ListObjAppendElement(NULL, tcl_cmd, Tcl_NewObj());
862  else
863  {
864  HeapTupleHeader td;
865  Oid tupType;
866  int32 tupTypmod;
867  TupleDesc tupdesc;
868  HeapTupleData tmptup;
869  Tcl_Obj *list_tmp;
870 
871  td = DatumGetHeapTupleHeader(fcinfo->arg[i]);
872  /* Extract rowtype info and find a tupdesc */
873  tupType = HeapTupleHeaderGetTypeId(td);
874  tupTypmod = HeapTupleHeaderGetTypMod(td);
875  tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
876  /* Build a temporary HeapTuple control structure */
878  tmptup.t_data = td;
879 
880  list_tmp = pltcl_build_tuple_argument(&tmptup, tupdesc);
881  Tcl_ListObjAppendElement(NULL, tcl_cmd, list_tmp);
882 
883  ReleaseTupleDesc(tupdesc);
884  }
885  }
886  else
887  {
888  /**************************************************
889  * Single values are added as string element
890  * of their external representation
891  **************************************************/
892  if (fcinfo->argnull[i])
893  Tcl_ListObjAppendElement(NULL, tcl_cmd, Tcl_NewObj());
894  else
895  {
896  char *tmp;
897 
898  tmp = OutputFunctionCall(&prodesc->arg_out_func[i],
899  fcinfo->arg[i]);
900  UTF_BEGIN;
901  Tcl_ListObjAppendElement(NULL, tcl_cmd,
902  Tcl_NewStringObj(UTF_E2U(tmp), -1));
903  UTF_END;
904  pfree(tmp);
905  }
906  }
907  }
908  }
909  PG_CATCH();
910  {
911  /* Release refcount to free tcl_cmd */
912  Tcl_DecrRefCount(tcl_cmd);
913  PG_RE_THROW();
914  }
915  PG_END_TRY();
916 
917  /************************************************************
918  * Call the Tcl function
919  *
920  * We assume no PG error can be thrown directly from this call.
921  ************************************************************/
922  tcl_rc = Tcl_EvalObjEx(interp, tcl_cmd, (TCL_EVAL_DIRECT | TCL_EVAL_GLOBAL));
923 
924  /* Release refcount to free tcl_cmd (and all subsidiary objects) */
925  Tcl_DecrRefCount(tcl_cmd);
926 
927  /************************************************************
928  * Check for errors reported by Tcl.
929  ************************************************************/
930  if (tcl_rc != TCL_OK)
931  throw_tcl_error(interp, prodesc->user_proname);
932 
933  /************************************************************
934  * Disconnect from SPI manager and then create the return
935  * value datum (if the input function does a palloc for it
936  * this must not be allocated in the SPI memory context
937  * because SPI_finish would free it). But don't try to call
938  * the result_in_func if we've been told to return a NULL;
939  * the Tcl result may not be a valid value of the result type
940  * in that case.
941  ************************************************************/
942  if (SPI_finish() != SPI_OK_FINISH)
943  elog(ERROR, "SPI_finish() failed");
944 
945  if (prodesc->fn_retisset)
946  {
947  ReturnSetInfo *rsi = call_state->rsi;
948 
949  /* We already checked this is OK */
951 
952  /* If we produced any tuples, send back the result */
953  if (call_state->tuple_store)
954  {
955  rsi->setResult = call_state->tuple_store;
956  if (call_state->ret_tupdesc)
957  {
958  MemoryContext oldcxt;
959 
960  oldcxt = MemoryContextSwitchTo(call_state->tuple_store_cxt);
961  rsi->setDesc = CreateTupleDescCopy(call_state->ret_tupdesc);
962  MemoryContextSwitchTo(oldcxt);
963  }
964  }
965  retval = (Datum) 0;
966  fcinfo->isnull = true;
967  }
968  else if (fcinfo->isnull)
969  {
970  retval = InputFunctionCall(&prodesc->result_in_func,
971  NULL,
972  prodesc->result_typioparam,
973  -1);
974  }
975  else if (prodesc->fn_retistuple)
976  {
977  TupleDesc td;
978  HeapTuple tup;
979  Tcl_Obj *resultObj;
980  Tcl_Obj **resultObjv;
981  int resultObjc;
982 
983  /*
984  * Set up data about result type. XXX it's tempting to consider
985  * caching this in the prodesc, in the common case where the rowtype
986  * is determined by the function not the calling query. But we'd have
987  * to be able to deal with ADD/DROP/ALTER COLUMN events when the
988  * result type is a named composite type, so it's not exactly trivial.
989  * Maybe worth improving someday.
990  */
991  if (get_call_result_type(fcinfo, NULL, &td) != TYPEFUNC_COMPOSITE)
992  ereport(ERROR,
993  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
994  errmsg("function returning record called in context "
995  "that cannot accept type record")));
996 
997  Assert(!call_state->ret_tupdesc);
998  Assert(!call_state->attinmeta);
999  call_state->ret_tupdesc = td;
1000  call_state->attinmeta = TupleDescGetAttInMetadata(td);
1001 
1002  /* Convert function result to tuple */
1003  resultObj = Tcl_GetObjResult(interp);
1004  if (Tcl_ListObjGetElements(interp, resultObj, &resultObjc, &resultObjv) == TCL_ERROR)
1005  throw_tcl_error(interp, prodesc->user_proname);
1006 
1007  tup = pltcl_build_tuple_result(interp, resultObjv, resultObjc,
1008  call_state);
1009  retval = HeapTupleGetDatum(tup);
1010  }
1011  else
1012  retval = InputFunctionCall(&prodesc->result_in_func,
1013  utf_u2e(Tcl_GetStringResult(interp)),
1014  prodesc->result_typioparam,
1015  -1);
1016 
1017  return retval;
1018 }
#define SPI_OK_CONNECT
Definition: spi.h:50
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:141
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
bool fn_retistuple
Definition: pltcl.c:149
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1257
int SPI_connect(void)
Definition: spi.c:84
FmgrInfo result_in_func
Definition: pltcl.c:146
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
int SPI_finish(void)
Definition: spi.c:148
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
char * user_proname
Definition: pltcl.c:137
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:259
static void throw_tcl_error(Tcl_Interp *interp, const char *proname)
Definition: pltcl.c:1327
ResourceOwner tuple_store_owner
Definition: pltcl.c:226
signed int int32
Definition: c.h:256
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1667
HeapTupleHeader t_data
Definition: htup.h:67
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:455
void pfree(void *pointer)
Definition: mcxt.c:950
AttInMetadata * attinmeta
Definition: pltcl.c:221
#define UTF_E2U(x)
Definition: pltcl.c:98
#define ERROR
Definition: elog.h:43
FmgrInfo * arg_out_func
Definition: pltcl.c:152
ReturnSetInfo * rsi
Definition: pltcl.c:223
pltcl_interp_desc * interp_desc
Definition: pltcl.c:145
uint32 t_len
Definition: htup.h:64
#define UTF_END
Definition: pltcl.c:90
static char * utf_u2e(const char *src)
Definition: pltcl.c:74
unsigned long fn_refcount
Definition: pltcl.c:140
Tcl_Interp * interp
Definition: pltcl.c:115
MemoryContext tuple_store_cxt
Definition: pltcl.c:225
Tuplestorestate * tuple_store
Definition: pltcl.c:224
#define ereport(elevel, rest)
Definition: elog.h:122
TupleDesc ret_tupdesc
Definition: pltcl.c:220
static Tcl_Obj * pltcl_build_tuple_argument(HeapTuple tuple, TupleDesc tupdesc)
Definition: pltcl.c:3003
static pltcl_proc_desc * compile_pltcl_function(Oid fn_oid, Oid tgreloid, bool is_event_trigger, bool pltrusted)
Definition: pltcl.c:1356
uintptr_t Datum
Definition: postgres.h:372
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:1068
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:445
Oid result_typioparam
Definition: pltcl.c:147
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1623
#define InvalidOid
Definition: postgres_ext.h:36
int allowedModes
Definition: execnodes.h:268
static HeapTuple pltcl_build_tuple_result(Tcl_Interp *interp, Tcl_Obj **kvObjv, int kvObjc, pltcl_call_state *call_state)
Definition: pltcl.c:3070
#define UTF_BEGIN
Definition: pltcl.c:85
char * internal_proname
Definition: pltcl.c:138
SetFunctionReturnMode returnMode
Definition: execnodes.h:270
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
pltcl_proc_desc * prodesc
Definition: pltcl.c:213
#define SPI_OK_FINISH
Definition: spi.h:51
#define PG_RE_THROW()
Definition: elog.h:314
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:202
Tuplestorestate * setResult
Definition: execnodes.h:273
ExprContext * econtext
Definition: execnodes.h:266
bool fn_retisset
Definition: pltcl.c:148
TupleDesc setDesc
Definition: execnodes.h:274
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool * arg_is_rowtype
Definition: pltcl.c:153
int i
#define elog
Definition: elog.h:219
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:107
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:439
static const char * pltcl_get_condition_name ( int  sqlstate)
static

Definition at line 1948 of file pltcl.c.

References i, TclExceptionNameMap::label, and NULL.

Referenced by pltcl_construct_errorCode().

1949 {
1950  int i;
1951 
1952  for (i = 0; exception_name_map[i].label != NULL; i++)
1953  {
1954  if (exception_name_map[i].sqlerrstate == sqlstate)
1955  return exception_name_map[i].label;
1956  }
1957  return "unrecognized_sqlstate";
1958 }
static const TclExceptionNameMap exception_name_map[]
Definition: pltcl.c:252
const char * label
Definition: pltcl.c:248
#define NULL
Definition: c.h:229
int i
static Datum pltcl_handler ( PG_FUNCTION_ARGS  ,
bool  pltrusted 
)
static

Definition at line 712 of file pltcl.c.

References Assert, CALLED_AS_EVENT_TRIGGER, CALLED_AS_TRIGGER, pltcl_call_state::fcinfo, pltcl_proc_desc::fn_cxt, pltcl_proc_desc::fn_refcount, MemoryContextDelete(), NULL, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, pltcl_current_call_state, pltcl_event_trigger_handler(), pltcl_func_handler(), pltcl_trigger_handler(), PointerGetDatum, and pltcl_call_state::prodesc.

Referenced by pltcl_call_handler(), and pltclu_call_handler().

713 {
714  Datum retval;
715  pltcl_call_state current_call_state;
716  pltcl_call_state *save_call_state;
717 
718  /*
719  * Initialize current_call_state to nulls/zeroes; in particular, set its
720  * prodesc pointer to null. Anything that sets it non-null should
721  * increase the prodesc's fn_refcount at the same time. We'll decrease
722  * the refcount, and then delete the prodesc if it's no longer referenced,
723  * on the way out of this function. This ensures that prodescs live as
724  * long as needed even if somebody replaces the originating pg_proc row
725  * while they're executing.
726  */
727  memset(&current_call_state, 0, sizeof(current_call_state));
728 
729  /*
730  * Ensure that static pointer is saved/restored properly
731  */
732  save_call_state = pltcl_current_call_state;
733  pltcl_current_call_state = &current_call_state;
734 
735  PG_TRY();
736  {
737  /*
738  * Determine if called as function or trigger and call appropriate
739  * subhandler
740  */
741  if (CALLED_AS_TRIGGER(fcinfo))
742  {
743  /* invoke the trigger handler */
744  retval = PointerGetDatum(pltcl_trigger_handler(fcinfo,
745  &current_call_state,
746  pltrusted));
747  }
748  else if (CALLED_AS_EVENT_TRIGGER(fcinfo))
749  {
750  /* invoke the event trigger handler */
751  pltcl_event_trigger_handler(fcinfo, &current_call_state, pltrusted);
752  retval = (Datum) 0;
753  }
754  else
755  {
756  /* invoke the regular function handler */
757  current_call_state.fcinfo = fcinfo;
758  retval = pltcl_func_handler(fcinfo, &current_call_state, pltrusted);
759  }
760  }
761  PG_CATCH();
762  {
763  /* Restore static pointer, then clean up the prodesc refcount if any */
764  pltcl_current_call_state = save_call_state;
765  if (current_call_state.prodesc != NULL)
766  {
767  Assert(current_call_state.prodesc->fn_refcount > 0);
768  if (--current_call_state.prodesc->fn_refcount == 0)
769  MemoryContextDelete(current_call_state.prodesc->fn_cxt);
770  }
771  PG_RE_THROW();
772  }
773  PG_END_TRY();
774 
775  /* Restore static pointer, then clean up the prodesc refcount if any */
776  /* (We're being paranoid in case an error is thrown in context deletion) */
777  pltcl_current_call_state = save_call_state;
778  if (current_call_state.prodesc != NULL)
779  {
780  Assert(current_call_state.prodesc->fn_refcount > 0);
781  if (--current_call_state.prodesc->fn_refcount == 0)
782  MemoryContextDelete(current_call_state.prodesc->fn_cxt);
783  }
784 
785  return retval;
786 }
#define CALLED_AS_EVENT_TRIGGER(fcinfo)
Definition: event_trigger.h:40
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
#define PointerGetDatum(X)
Definition: postgres.h:562
FunctionCallInfo fcinfo
Definition: pltcl.c:207
static Datum pltcl_func_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state, bool pltrusted)
Definition: pltcl.c:793
static pltcl_call_state * pltcl_current_call_state
Definition: pltcl.c:241
static void pltcl_event_trigger_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state, bool pltrusted)
Definition: pltcl.c:1277
unsigned long fn_refcount
Definition: pltcl.c:140
static HeapTuple pltcl_trigger_handler(PG_FUNCTION_ARGS, pltcl_call_state *call_state, bool pltrusted)
Definition: pltcl.c:1025
uintptr_t Datum
Definition: postgres.h:372
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:229
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:25
#define Assert(condition)
Definition: c.h:675
pltcl_proc_desc * prodesc
Definition: pltcl.c:213
#define PG_RE_THROW()
Definition: elog.h:314
MemoryContext fn_cxt
Definition: pltcl.c:139
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
static void pltcl_init_interp ( pltcl_interp_desc interp_desc,
Oid  prolang,
bool  pltrusted 
)
static

Definition at line 480 of file pltcl.c.

References call_pltcl_start_proc(), elog, ERROR, pltcl_interp_desc::interp, NULL, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, pltcl_argisnull(), pltcl_elog(), pltcl_hold_interp, pltcl_quote(), pltcl_returnnext(), pltcl_returnnull(), pltcl_SPI_execute(), pltcl_SPI_execute_plan(), pltcl_SPI_lastoid(), pltcl_SPI_prepare(), pltcl_subtransaction(), pltcl_interp_desc::query_hash, snprintf(), and pltcl_interp_desc::user_id.

Referenced by pltcl_fetch_interp().

481 {
482  Tcl_Interp *interp;
483  char interpname[32];
484 
485  /************************************************************
486  * Create the Tcl interpreter as a slave of pltcl_hold_interp.
487  * Note: Tcl automatically does Tcl_Init in the untrusted case,
488  * and it's not wanted in the trusted case.
489  ************************************************************/
490  snprintf(interpname, sizeof(interpname), "slave_%u", interp_desc->user_id);
491  if ((interp = Tcl_CreateSlave(pltcl_hold_interp, interpname,
492  pltrusted ? 1 : 0)) == NULL)
493  elog(ERROR, "could not create slave Tcl interpreter");
494 
495  /************************************************************
496  * Initialize the query hash table associated with interpreter
497  ************************************************************/
498  Tcl_InitHashTable(&interp_desc->query_hash, TCL_STRING_KEYS);
499 
500  /************************************************************
501  * Install the commands for SPI support in the interpreter
502  ************************************************************/
503  Tcl_CreateObjCommand(interp, "elog",
504  pltcl_elog, NULL, NULL);
505  Tcl_CreateObjCommand(interp, "quote",
506  pltcl_quote, NULL, NULL);
507  Tcl_CreateObjCommand(interp, "argisnull",
509  Tcl_CreateObjCommand(interp, "return_null",
511  Tcl_CreateObjCommand(interp, "return_next",
513  Tcl_CreateObjCommand(interp, "spi_exec",
515  Tcl_CreateObjCommand(interp, "spi_prepare",
517  Tcl_CreateObjCommand(interp, "spi_execp",
519  Tcl_CreateObjCommand(interp, "spi_lastoid",
521  Tcl_CreateObjCommand(interp, "subtransaction",
523 
524  /************************************************************
525  * Call the appropriate start_proc, if there is one.
526  *
527  * We must set interp_desc->interp before the call, else the start_proc
528  * won't find the interpreter it's supposed to use. But, if the
529  * start_proc fails, we want to abandon use of the interpreter.
530  ************************************************************/
531  PG_TRY();
532  {
533  interp_desc->interp = interp;
534  call_pltcl_start_proc(prolang, pltrusted);
535  }
536  PG_CATCH();
537  {
538  interp_desc->interp = NULL;
539  Tcl_DeleteInterp(interp);
540  PG_RE_THROW();
541  }
542  PG_END_TRY();
543 }
static int pltcl_argisnull(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
Definition: pltcl.c:2020
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static int pltcl_elog(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
Definition: pltcl.c:1716
static int pltcl_subtransaction(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
Definition: pltcl.c:2869
static int pltcl_SPI_prepare(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
Definition: pltcl.c:2501
static int pltcl_quote(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
Definition: pltcl.c:1966
static int pltcl_SPI_execute_plan(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
Definition: pltcl.c:2628
#define ERROR
Definition: elog.h:43
static void call_pltcl_start_proc(Oid prolang, bool pltrusted)
Definition: pltcl.c:581
static Tcl_Interp * pltcl_hold_interp
Definition: pltcl.c:236
Tcl_Interp * interp
Definition: pltcl.c:115
static int pltcl_SPI_lastoid(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
Definition: pltcl.c:2845
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:229
static int pltcl_returnnext(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
Definition: pltcl.c:2112
#define PG_RE_THROW()
Definition: elog.h:314
#define elog
Definition: elog.h:219
static int pltcl_returnnull(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
Definition: pltcl.c:2074
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
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:2282
static void pltcl_init_tuple_store ( pltcl_call_state call_state)
static

Definition at line 3138 of file pltcl.c.

References ReturnSetInfo::allowedModes, Assert, pltcl_call_state::attinmeta, CurrentResourceOwner, ReturnSetInfo::expectedDesc, MemoryContextSwitchTo(), pltcl_call_state::ret_tupdesc, pltcl_call_state::rsi, SFRM_Materialize_Random, pltcl_call_state::tuple_store, pltcl_call_state::tuple_store_cxt, pltcl_call_state::tuple_store_owner, TupleDescGetAttInMetadata(), tuplestore_begin_heap(), and work_mem.

Referenced by pltcl_returnnext().

3139 {
3140  ReturnSetInfo *rsi = call_state->rsi;
3141  MemoryContext oldcxt;
3142  ResourceOwner oldowner;
3143 
3144  /* Should be in a SRF */
3145  Assert(rsi);
3146  /* Should be first time through */
3147  Assert(!call_state->tuple_store);
3148  Assert(!call_state->attinmeta);
3149 
3150  /* We expect caller to provide an appropriate result tupdesc */
3151  Assert(rsi->expectedDesc);
3152  call_state->ret_tupdesc = rsi->expectedDesc;
3153 
3154  /*
3155  * Switch to the right memory context and resource owner for storing the
3156  * tuplestore. If we're within a subtransaction opened for an exception
3157  * block, for example, we must still create the tuplestore in the resource
3158  * owner that was active when this function was entered, and not in the
3159  * subtransaction's resource owner.
3160  */
3161  oldcxt = MemoryContextSwitchTo(call_state->tuple_store_cxt);
3162  oldowner = CurrentResourceOwner;
3164 
3165  call_state->tuple_store =
3167  false, work_mem);
3168 
3169  /* Build attinmeta in this context, too */
3170  call_state->attinmeta = TupleDescGetAttInMetadata(call_state->ret_tupdesc);
3171 
3172  CurrentResourceOwner = oldowner;
3173  MemoryContextSwitchTo(oldcxt);
3174 }
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
ResourceOwner tuple_store_owner
Definition: pltcl.c:226
TupleDesc expectedDesc
Definition: execnodes.h:267
AttInMetadata * attinmeta
Definition: pltcl.c:221
ReturnSetInfo * rsi
Definition: pltcl.c:223
MemoryContext tuple_store_cxt
Definition: pltcl.c:225
Tuplestorestate * tuple_store
Definition: pltcl.c:224
TupleDesc ret_tupdesc
Definition: pltcl.c:220
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:1068
int work_mem
Definition: globals.c:113
int allowedModes
Definition: execnodes.h:268
#define Assert(condition)
Definition: c.h:675
static ClientData pltcl_InitNotifier ( void  )
static

Definition at line 341 of file pltcl.c.

Referenced by _PG_init().

342 {
343  static int fakeThreadKey; /* To give valid address for ClientData */
344 
345  return (ClientData) &(fakeThreadKey);
346 }
static int pltcl_process_SPI_result ( Tcl_Interp *  interp,
const char *  arrayname,
Tcl_Obj *  loop_body,
int  spi_rc,
SPITupleTable tuptable,
uint64  ntuples 
)
static

Definition at line 2391 of file pltcl.c.

References i, NULL, pltcl_set_tuple_values(), SPI_freetuptable(), SPI_OK_DELETE, SPI_OK_DELETE_RETURNING, SPI_OK_INSERT, SPI_OK_INSERT_RETURNING, SPI_OK_REWRITTEN, SPI_OK_SELECT, SPI_OK_SELINTO, SPI_OK_UPDATE, SPI_OK_UPDATE_RETURNING, SPI_OK_UTILITY, SPI_result_code_string(), SPITupleTable::tupdesc, and SPITupleTable::vals.

Referenced by pltcl_SPI_execute(), and pltcl_SPI_execute_plan().

2397 {
2398  int my_rc = TCL_OK;
2399  int loop_rc;
2400  HeapTuple *tuples;
2401  TupleDesc tupdesc;
2402 
2403  switch (spi_rc)
2404  {
2405  case SPI_OK_SELINTO:
2406  case SPI_OK_INSERT:
2407  case SPI_OK_DELETE:
2408  case SPI_OK_UPDATE:
2409  Tcl_SetObjResult(interp, Tcl_NewWideIntObj(ntuples));
2410  break;
2411 
2412  case SPI_OK_UTILITY:
2413  case SPI_OK_REWRITTEN:
2414  if (tuptable == NULL)
2415  {
2416  Tcl_SetObjResult(interp, Tcl_NewIntObj(0));
2417  break;
2418  }
2419  /* FALL THRU for utility returning tuples */
2420 
2421  case SPI_OK_SELECT:
2425 
2426  /*
2427  * Process the tuples we got
2428  */
2429  tuples = tuptable->vals;
2430  tupdesc = tuptable->tupdesc;
2431 
2432  if (loop_body == NULL)
2433  {
2434  /*
2435  * If there is no loop body given, just set the variables from
2436  * the first tuple (if any)
2437  */
2438  if (ntuples > 0)
2439  pltcl_set_tuple_values(interp, arrayname, 0,
2440  tuples[0], tupdesc);
2441  }
2442  else
2443  {
2444  /*
2445  * There is a loop body - process all tuples and evaluate the
2446  * body on each
2447  */
2448  uint64 i;
2449 
2450  for (i = 0; i < ntuples; i++)
2451  {
2452  pltcl_set_tuple_values(interp, arrayname, i,
2453  tuples[i], tupdesc);
2454 
2455  loop_rc = Tcl_EvalObjEx(interp, loop_body, 0);
2456 
2457  if (loop_rc == TCL_OK)
2458  continue;
2459  if (loop_rc == TCL_CONTINUE)
2460  continue;
2461  if (loop_rc == TCL_RETURN)
2462  {
2463  my_rc = TCL_RETURN;
2464  break;
2465  }
2466  if (loop_rc == TCL_BREAK)
2467  break;
2468  my_rc = TCL_ERROR;
2469  break;
2470  }
2471  }
2472 
2473  if (my_rc == TCL_OK)
2474  {
2475  Tcl_SetObjResult(interp, Tcl_NewWideIntObj(ntuples));
2476  }
2477  break;
2478 
2479  default:
2480  Tcl_AppendResult(interp, "pltcl: SPI_execute failed: ",
2481  SPI_result_code_string(spi_rc), NULL);
2482  my_rc = TCL_ERROR;
2483  break;
2484  }
2485 
2486  SPI_freetuptable(tuptable);
2487 
2488  return my_rc;
2489 }
#define SPI_OK_DELETE_RETURNING
Definition: spi.h:61
#define SPI_OK_DELETE
Definition: spi.h:57
HeapTuple * vals
Definition: spi.h:28
#define SPI_OK_INSERT_RETURNING
Definition: spi.h:60
const char * SPI_result_code_string(int code)
Definition: spi.c:1513
#define SPI_OK_UTILITY
Definition: spi.h:53
#define SPI_OK_UPDATE_RETURNING
Definition: spi.h:62
#define SPI_OK_REWRITTEN
Definition: spi.h:63
#define SPI_OK_SELINTO
Definition: spi.h:55
void SPI_freetuptable(SPITupleTable *tuptable)
Definition: spi.c:970
TupleDesc tupdesc
Definition: spi.h:27
#define SPI_OK_SELECT
Definition: spi.h:54
#define NULL
Definition: c.h:229
#define SPI_OK_UPDATE
Definition: spi.h:58
int i
#define SPI_OK_INSERT
Definition: spi.h:56
static void pltcl_set_tuple_values(Tcl_Interp *interp, const char *arrayname, uint64 tupno, HeapTuple tuple, TupleDesc tupdesc)
Definition: pltcl.c:2918
static int pltcl_quote ( ClientData  cdata,
Tcl_Interp *  interp,
int  objc,
Tcl_Obj *const  objv[] 
)
static

Definition at line 1966 of file pltcl.c.

References length(), palloc(), and pfree().

Referenced by pltcl_init_interp().

1968 {
1969  char *tmp;
1970  const char *cp1;
1971  char *cp2;
1972  int length;
1973 
1974  /************************************************************
1975  * Check call syntax
1976  ************************************************************/
1977  if (objc != 2)
1978  {
1979  Tcl_WrongNumArgs(interp, 1, objv, "string");
1980  return TCL_ERROR;
1981  }
1982 
1983  /************************************************************
1984  * Allocate space for the maximum the string can
1985  * grow to and initialize pointers
1986  ************************************************************/
1987  cp1 = Tcl_GetStringFromObj(objv[1], &length);
1988  tmp = palloc(length * 2 + 1);
1989  cp2 = tmp;
1990 
1991  /************************************************************
1992  * Walk through string and double every quote and backslash
1993  ************************************************************/
1994  while (*cp1)
1995  {
1996  if (*cp1 == '\'')
1997  *cp2++ = '\'';
1998  else
1999  {
2000  if (*cp1 == '\\')
2001  *cp2++ = '\\';
2002  }
2003  *cp2++ = *cp1++;
2004  }
2005 
2006  /************************************************************
2007  * Terminate the string and set it as result
2008  ************************************************************/
2009  *cp2 = '\0';
2010  Tcl_SetObjResult(interp, Tcl_NewStringObj(tmp, -1));
2011  pfree(tmp);
2012  return TCL_OK;
2013 }
int length(const List *list)
Definition: list.c:1271
void pfree(void *pointer)
Definition: mcxt.c:950
void * palloc(Size size)
Definition: mcxt.c:849
static int pltcl_returnnext ( ClientData  cdata,
Tcl_Interp *  interp,
int  objc,
Tcl_Obj *const  objv[] 
)
static

Definition at line 2112 of file pltcl.c.

References BeginInternalSubTransaction(), CurrentMemoryContext, CurrentResourceOwner, elog, ERROR, pltcl_call_state::fcinfo, pltcl_proc_desc::fn_retisset, pltcl_proc_desc::fn_retistuple, InputFunctionCall(), tupleDesc::natts, NULL, PG_CATCH, PG_END_TRY, PG_TRY, pltcl_build_tuple_result(), pltcl_current_call_state, pltcl_init_tuple_store(), pltcl_subtrans_abort(), pltcl_subtrans_commit(), pltcl_call_state::prodesc, result, pltcl_proc_desc::result_in_func, pltcl_proc_desc::result_typioparam, pltcl_call_state::ret_tupdesc, pltcl_call_state::tuple_store, tuplestore_puttuple(), tuplestore_putvalues(), and utf_u2e().

Referenced by pltcl_init_interp().

2114 {
2116  FunctionCallInfo fcinfo = call_state->fcinfo;
2117  pltcl_proc_desc *prodesc = call_state->prodesc;
2118  MemoryContext oldcontext = CurrentMemoryContext;
2120  volatile int result = TCL_OK;
2121 
2122  /*
2123  * Check that we're called as a set-returning function
2124  */
2125  if (fcinfo == NULL)
2126  {
2127  Tcl_SetObjResult(interp,
2128  Tcl_NewStringObj("return_next cannot be used in triggers", -1));
2129  return TCL_ERROR;
2130  }
2131 
2132  if (!prodesc->fn_retisset)
2133  {
2134  Tcl_SetObjResult(interp,
2135  Tcl_NewStringObj("return_next cannot be used in non-set-returning functions", -1));
2136  return TCL_ERROR;
2137  }
2138 
2139  /*
2140  * Check call syntax
2141  */
2142  if (objc != 2)
2143  {
2144  Tcl_WrongNumArgs(interp, 1, objv, "result");
2145  return TCL_ERROR;
2146  }
2147 
2148  /*
2149  * The rest might throw elog(ERROR), so must run in a subtransaction.
2150  *
2151  * A small advantage of using a subtransaction is that it provides a
2152  * short-lived memory context for free, so we needn't worry about leaking
2153  * memory here. To use that context, call BeginInternalSubTransaction
2154  * directly instead of going through pltcl_subtrans_begin.
2155  */
2157  PG_TRY();
2158  {
2159  /* Set up tuple store if first output row */
2160  if (call_state->tuple_store == NULL)
2161  pltcl_init_tuple_store(call_state);
2162 
2163  if (prodesc->fn_retistuple)
2164  {
2165  Tcl_Obj **rowObjv;
2166  int rowObjc;
2167 
2168  /* result should be a list, so break it down */
2169  if (Tcl_ListObjGetElements(interp, objv[1], &rowObjc, &rowObjv) == TCL_ERROR)
2170  result = TCL_ERROR;
2171  else
2172  {
2173  HeapTuple tuple;
2174 
2175  tuple = pltcl_build_tuple_result(interp, rowObjv, rowObjc,
2176  call_state);
2177  tuplestore_puttuple(call_state->tuple_store, tuple);
2178  }
2179  }
2180  else
2181  {
2182  Datum retval;
2183  bool isNull = false;
2184 
2185  /* for paranoia's sake, check that tupdesc has exactly one column */
2186  if (call_state->ret_tupdesc->natts != 1)
2187  elog(ERROR, "wrong result type supplied in return_next");
2188 
2189  retval = InputFunctionCall(&prodesc->result_in_func,
2190  utf_u2e((char *) Tcl_GetString(objv[1])),
2191  prodesc->result_typioparam,
2192  -1);
2193  tuplestore_putvalues(call_state->tuple_store, call_state->ret_tupdesc,
2194  &retval, &isNull);
2195  }
2196 
2197  pltcl_subtrans_commit(oldcontext, oldowner);
2198  }
2199  PG_CATCH();
2200  {
2201  pltcl_subtrans_abort(interp, oldcontext, oldowner);
2202  return TCL_ERROR;
2203  }
2204  PG_END_TRY();
2205 
2206  return result;
2207 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
bool fn_retistuple
Definition: pltcl.c:149
static void pltcl_subtrans_abort(Tcl_Interp *interp, MemoryContext oldcontext, ResourceOwner oldowner)
Definition: pltcl.c:2253
static void pltcl_init_tuple_store(pltcl_call_state *call_state)
Definition: pltcl.c:3138
FmgrInfo result_in_func
Definition: pltcl.c:146
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
return result
Definition: formatting.c:1633
static void pltcl_subtrans_commit(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: pltcl.c:2244
FunctionCallInfo fcinfo
Definition: pltcl.c:207
int natts
Definition: tupdesc.h:73
static pltcl_call_state * pltcl_current_call_state
Definition: pltcl.c:241
#define ERROR
Definition: elog.h:43
void tuplestore_puttuple(Tuplestorestate *state, HeapTuple tuple)
Definition: tuplestore.c:730
static char * utf_u2e(const char *src)
Definition: pltcl.c:74
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
Tuplestorestate * tuple_store
Definition: pltcl.c:224
TupleDesc ret_tupdesc
Definition: pltcl.c:220
uintptr_t Datum
Definition: postgres.h:372
Oid result_typioparam
Definition: pltcl.c:147
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1623
static HeapTuple pltcl_build_tuple_result(Tcl_Interp *interp, Tcl_Obj **kvObjv, int kvObjc, pltcl_call_state *call_state)
Definition: pltcl.c:3070
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:229
pltcl_proc_desc * prodesc
Definition: pltcl.c:213
void BeginInternalSubTransaction(char *name)
Definition: xact.c:4054
bool fn_retisset
Definition: pltcl.c:148
#define elog
Definition: elog.h:219
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
static int pltcl_returnnull ( ClientData  cdata,
Tcl_Interp *  interp,
int  objc,
Tcl_Obj *const  objv[] 
)
static

Definition at line 2074 of file pltcl.c.

References pltcl_call_state::fcinfo, FunctionCallInfoData::isnull, and NULL.

Referenced by pltcl_init_interp().

2076 {
2078 
2079  /************************************************************
2080  * Check call syntax
2081  ************************************************************/
2082  if (objc != 1)
2083  {
2084  Tcl_WrongNumArgs(interp, 1, objv, "");
2085  return TCL_ERROR;
2086  }
2087 
2088  /************************************************************
2089  * Check that we're called as a normal function
2090  ************************************************************/
2091  if (fcinfo == NULL)
2092  {
2093  Tcl_SetObjResult(interp,
2094  Tcl_NewStringObj("return_null cannot be used in triggers", -1));
2095  return TCL_ERROR;
2096  }
2097 
2098  /************************************************************
2099  * Set the NULL return flag and cause Tcl to return from the
2100  * procedure.
2101  ************************************************************/
2102  fcinfo->isnull = true;
2103 
2104  return TCL_RETURN;
2105 }
FunctionCallInfo fcinfo
Definition: pltcl.c:207
static pltcl_call_state * pltcl_current_call_state
Definition: pltcl.c:241
#define NULL
Definition: c.h:229
static void pltcl_ServiceModeHook ( int  mode)
static

Definition at line 375 of file pltcl.c.

Referenced by _PG_init().

376 {
377 }
static void pltcl_set_tuple_values ( Tcl_Interp *  interp,
const char *  arrayname,
uint64  tupno,
HeapTuple  tuple,
TupleDesc  tupdesc 
)
static

Definition at line 2918 of file pltcl.c.

References tupleDesc::attrs, getTypeOutputInfo(), heap_getattr, i, NameStr, tupleDesc::natts, NULL, OidOutputFunctionCall(), pfree(), pstrdup(), UTF_BEGIN, UTF_E2U, and UTF_END.

Referenced by pltcl_process_SPI_result().

2920 {
2921  int i;
2922  char *outputstr;
2923  Datum attr;
2924  bool isnull;
2925  const char *attname;
2926  Oid typoutput;
2927  bool typisvarlena;
2928  const char **arrptr;
2929  const char **nameptr;
2930  const char *nullname = NULL;
2931 
2932  /************************************************************
2933  * Prepare pointers for Tcl_SetVar2() below
2934  ************************************************************/
2935  if (arrayname == NULL)
2936  {
2937  arrptr = &attname;
2938  nameptr = &nullname;
2939  }
2940  else
2941  {
2942  arrptr = &arrayname;
2943  nameptr = &attname;
2944 
2945  /*
2946  * When outputting to an array, fill the ".tupno" element with the
2947  * current tuple number. This will be overridden below if ".tupno" is
2948  * in use as an actual field name in the rowtype.
2949  */
2950  Tcl_SetVar2Ex(interp, arrayname, ".tupno", Tcl_NewWideIntObj(tupno), 0);
2951  }
2952 
2953  for (i = 0; i < tupdesc->natts; i++)
2954  {
2955  /* ignore dropped attributes */
2956  if (tupdesc->attrs[i]->attisdropped)
2957  continue;
2958 
2959  /************************************************************
2960  * Get the attribute name
2961  ************************************************************/
2962  UTF_BEGIN;
2963  attname = pstrdup(UTF_E2U(NameStr(tupdesc->attrs[i]->attname)));
2964  UTF_END;
2965 
2966  /************************************************************
2967  * Get the attributes value
2968  ************************************************************/
2969  attr = heap_getattr(tuple, i + 1, tupdesc, &isnull);
2970 
2971  /************************************************************
2972  * If there is a value, set the variable
2973  * If not, unset it
2974  *
2975  * Hmmm - Null attributes will cause functions to
2976  * crash if they don't expect them - need something
2977  * smarter here.
2978  ************************************************************/
2979  if (!isnull)
2980  {
2981  getTypeOutputInfo(tupdesc->attrs[i]->atttypid,
2982  &typoutput, &typisvarlena);
2983  outputstr = OidOutputFunctionCall(typoutput, attr);
2984  UTF_BEGIN;
2985  Tcl_SetVar2Ex(interp, *arrptr, *nameptr,
2986  Tcl_NewStringObj(UTF_E2U(outputstr), -1), 0);
2987  UTF_END;
2988  pfree(outputstr);
2989  }
2990  else
2991  Tcl_UnsetVar2(interp, *arrptr, *nameptr, 0);
2992 
2993  pfree((char *) attname);
2994  }
2995 }
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2632
char * pstrdup(const char *in)
Definition: mcxt.c:1077
Form_pg_attribute * attrs
Definition: tupdesc.h:74
unsigned int Oid
Definition: postgres_ext.h:31
int natts
Definition: tupdesc.h:73
void pfree(void *pointer)
Definition: mcxt.c:950
#define UTF_E2U(x)
Definition: pltcl.c:98
#define UTF_END
Definition: pltcl.c:90
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:769
uintptr_t Datum
Definition: postgres.h:372
#define UTF_BEGIN
Definition: pltcl.c:85
#define NULL
Definition: c.h:229
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1747
int i
#define NameStr(name)
Definition: c.h:499
static void pltcl_SetTimer ( CONST86 Tcl_Time *  timePtr)
static

Definition at line 354 of file pltcl.c.

Referenced by _PG_init().

355 {
356 }
static int pltcl_SPI_execute ( ClientData  cdata,
Tcl_Interp *  interp,
int  objc,
Tcl_Obj *const  objv[] 
)
static

Definition at line 2282 of file pltcl.c.

References CurrentMemoryContext, CurrentResourceOwner, pltcl_proc_desc::fn_readonly, i, NULL, PG_CATCH, PG_END_TRY, PG_TRY, pltcl_process_SPI_result(), pltcl_subtrans_abort(), pltcl_subtrans_begin(), pltcl_subtrans_commit(), pltcl_call_state::prodesc, SPI_execute(), SPI_processed, SPI_tuptable, UTF_BEGIN, UTF_END, and UTF_U2E.

Referenced by pltcl_init_interp().

2284 {
2285  int my_rc;
2286  int spi_rc;
2287  int query_idx;
2288  int i;
2289  int optIndex;
2290  int count = 0;
2291  const char *volatile arrayname = NULL;
2292  Tcl_Obj *volatile loop_body = NULL;
2293  MemoryContext oldcontext = CurrentMemoryContext;
2295 
2296  enum options
2297  {
2298  OPT_ARRAY, OPT_COUNT
2299  };
2300 
2301  static const char *options[] = {
2302  "-array", "-count", (const char *) NULL
2303  };
2304 
2305  /************************************************************
2306  * Check the call syntax and get the options
2307  ************************************************************/
2308  if (objc < 2)
2309  {
2310  Tcl_WrongNumArgs(interp, 1, objv,
2311  "?-count n? ?-array name? query ?loop body?");
2312  return TCL_ERROR;
2313  }
2314 
2315  i = 1;
2316  while (i < objc)
2317  {
2318  if (Tcl_GetIndexFromObj(NULL, objv[i], options, NULL,
2319  TCL_EXACT, &optIndex) != TCL_OK)
2320  break;
2321 
2322  if (++i >= objc)
2323  {
2324  Tcl_SetObjResult(interp,
2325  Tcl_NewStringObj("missing argument to -count or -array", -1));
2326  return TCL_ERROR;
2327  }
2328 
2329  switch ((enum options) optIndex)
2330  {
2331  case OPT_ARRAY:
2332  arrayname = Tcl_GetString(objv[i++]);
2333  break;
2334 
2335  case OPT_COUNT:
2336  if (Tcl_GetIntFromObj(interp, objv[i++], &count) != TCL_OK)
2337  return TCL_ERROR;
2338  break;
2339  }
2340  }
2341 
2342  query_idx = i;
2343  if (query_idx >= objc || query_idx + 2 < objc)
2344  {
2345  Tcl_WrongNumArgs(interp, query_idx - 1, objv, "query ?loop body?");
2346  return TCL_ERROR;
2347  }
2348 
2349  if (query_idx + 1 < objc)
2350  loop_body = objv[query_idx + 1];
2351 
2352  /************************************************************
2353  * Execute the query inside a sub-transaction, so we can cope with
2354  * errors sanely
2355  ************************************************************/
2356 
2357  pltcl_subtrans_begin(oldcontext, oldowner);
2358 
2359  PG_TRY();
2360  {
2361  UTF_BEGIN;
2362  spi_rc = SPI_execute(UTF_U2E(Tcl_GetString(objv[query_idx])),
2364  UTF_END;
2365 
2366  my_rc = pltcl_process_SPI_result(interp,
2367  arrayname,
2368  loop_body,
2369  spi_rc,
2370  SPI_tuptable,
2371  SPI_processed);
2372 
2373  pltcl_subtrans_commit(oldcontext, oldowner);
2374  }
2375  PG_CATCH();
2376  {
2377  pltcl_subtrans_abort(interp, oldcontext, oldowner);
2378  return TCL_ERROR;
2379  }
2380  PG_END_TRY();
2381 
2382  return my_rc;
2383 }
static void pltcl_subtrans_begin(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: pltcl.c:2235
static void pltcl_subtrans_abort(Tcl_Interp *interp, MemoryContext oldcontext, ResourceOwner oldowner)
Definition: pltcl.c:2253
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
SPITupleTable * SPI_tuptable
Definition: spi.c:41
static void pltcl_subtrans_commit(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: pltcl.c:2244
uint64 SPI_processed
Definition: spi.c:39
static pltcl_call_state * pltcl_current_call_state
Definition: pltcl.c:241
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:2391
#define UTF_END
Definition: pltcl.c:90
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
bool fn_readonly
Definition: pltcl.c:143
#define UTF_U2E(x)
Definition: pltcl.c:95
#define UTF_BEGIN
Definition: pltcl.c:85
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:229
pltcl_proc_desc * prodesc
Definition: pltcl.c:213
int i
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:304
static int pltcl_SPI_execute_plan ( ClientData  cdata,
Tcl_Interp *  interp,
int  objc,
Tcl_Obj *const  objv[] 
)
static

Definition at line 2628 of file pltcl.c.

References pltcl_query_desc::arginfuncs, pltcl_query_desc::argtypioparams, CurrentMemoryContext, CurrentResourceOwner, pltcl_proc_desc::fn_readonly, i, InputFunctionCall(), pltcl_proc_desc::interp_desc, pltcl_query_desc::nargs, NULL, palloc(), PG_CATCH, PG_END_TRY, PG_TRY, pltcl_query_desc::plan, pltcl_process_SPI_result(), pltcl_subtrans_abort(), pltcl_subtrans_begin(), pltcl_subtrans_commit(), pltcl_call_state::prodesc, pltcl_interp_desc::query_hash, SPI_execute_plan(), SPI_processed, SPI_tuptable, UTF_BEGIN, UTF_END, and UTF_U2E.

Referenced by pltcl_init_interp().

2630 {
2631  int my_rc;
2632  int spi_rc;
2633  int i;
2634  int j;
2635  int optIndex;
2636  Tcl_HashEntry *hashent;
2637  pltcl_query_desc *qdesc;
2638  const char *nulls = NULL;
2639  const char *arrayname = NULL;
2640  Tcl_Obj *loop_body = NULL;
2641  int count = 0;
2642  int callObjc;
2643  Tcl_Obj **callObjv = NULL;
2644  Datum *argvalues;
2645  MemoryContext oldcontext = CurrentMemoryContext;
2647  Tcl_HashTable *query_hash;
2648 
2649  enum options
2650  {
2651  OPT_ARRAY, OPT_COUNT, OPT_NULLS
2652  };
2653 
2654  static const char *options[] = {
2655  "-array", "-count", "-nulls", (const char *) NULL
2656  };
2657 
2658  /************************************************************
2659  * Get the options and check syntax
2660  ************************************************************/
2661  i = 1;
2662  while (i < objc)
2663  {
2664  if (Tcl_GetIndexFromObj(NULL, objv[i], options, NULL,
2665  TCL_EXACT, &optIndex) != TCL_OK)
2666  break;
2667 
2668  if (++i >= objc)
2669  {
2670  Tcl_SetObjResult(interp,
2671  Tcl_NewStringObj("missing argument to -array, -count or -nulls", -1));
2672  return TCL_ERROR;
2673  }
2674 
2675  switch ((enum options) optIndex)
2676  {
2677  case OPT_ARRAY:
2678  arrayname = Tcl_GetString(objv[i++]);
2679  break;
2680 
2681  case OPT_COUNT:
2682  if (Tcl_GetIntFromObj(interp, objv[i++], &count) != TCL_OK)
2683  return TCL_ERROR;
2684  break;
2685 
2686  case OPT_NULLS:
2687  nulls = Tcl_GetString(objv[i++]);
2688  break;
2689  }
2690  }
2691 
2692  /************************************************************
2693  * Get the prepared plan descriptor by its key
2694  ************************************************************/
2695  if (i >= objc)
2696  {
2697  Tcl_SetObjResult(interp,
2698  Tcl_NewStringObj("missing argument to -count or -array", -1));
2699  return TCL_ERROR;
2700  }
2701 
2703 
2704  hashent = Tcl_FindHashEntry(query_hash, Tcl_GetString(objv[i]));
2705  if (hashent == NULL)
2706  {
2707  Tcl_AppendResult(interp, "invalid queryid '", Tcl_GetString(objv[i]), "'", NULL);
2708  return TCL_ERROR;
2709  }
2710  qdesc = (pltcl_query_desc *) Tcl_GetHashValue(hashent);
2711  i++;
2712 
2713  /************************************************************
2714  * If a nulls string is given, check for correct length
2715  ************************************************************/
2716  if (nulls != NULL)
2717  {
2718  if (strlen(nulls) != qdesc->nargs)
2719  {
2720  Tcl_SetObjResult(interp,
2721  Tcl_NewStringObj(
2722  "length of nulls string doesn't match number of arguments",
2723  -1));
2724  return TCL_ERROR;
2725  }
2726  }
2727 
2728  /************************************************************
2729  * If there was a argtype list on preparation, we need
2730  * an argument value list now
2731  ************************************************************/
2732  if (qdesc->nargs > 0)
2733  {
2734  if (i >= objc)
2735  {
2736  Tcl_SetObjResult(interp,
2737  Tcl_NewStringObj(
2738  "argument list length doesn't match number of arguments for query"
2739  ,-1));
2740  return TCL_ERROR;
2741  }
2742 
2743  /************************************************************
2744  * Split the argument values
2745  ************************************************************/
2746  if (Tcl_ListObjGetElements(interp, objv[i++], &callObjc, &callObjv) != TCL_OK)
2747  return TCL_ERROR;
2748 
2749  /************************************************************
2750  * Check that the number of arguments matches
2751  ************************************************************/
2752  if (callObjc != qdesc->nargs)
2753  {
2754  Tcl_SetObjResult(interp,
2755  Tcl_NewStringObj(
2756  "argument list length doesn't match number of arguments for query"
2757  ,-1));
2758  return TCL_ERROR;
2759  }
2760  }
2761  else
2762  callObjc = 0;
2763 
2764  /************************************************************
2765  * Get loop body if present
2766  ************************************************************/
2767  if (i < objc)
2768  loop_body = objv[i++];
2769 
2770  if (i != objc)
2771  {
2772  Tcl_WrongNumArgs(interp, 1, objv,
2773  "?-count n? ?-array name? ?-nulls string? "
2774  "query ?args? ?loop body?");
2775  return TCL_ERROR;
2776  }
2777 
2778  /************************************************************
2779  * Execute the plan inside a sub-transaction, so we can cope with
2780  * errors sanely
2781  ************************************************************/
2782 
2783  pltcl_subtrans_begin(oldcontext, oldowner);
2784 
2785  PG_TRY();
2786  {
2787  /************************************************************
2788  * Setup the value array for SPI_execute_plan() using
2789  * the type specific input functions
2790  ************************************************************/
2791  argvalues = (Datum *) palloc(callObjc * sizeof(Datum));
2792 
2793  for (j = 0; j < callObjc; j++)
2794  {
2795  if (nulls && nulls[j] == 'n')
2796  {
2797  argvalues[j] = InputFunctionCall(&qdesc->arginfuncs[j],
2798  NULL,
2799  qdesc->argtypioparams[j],
2800  -1);
2801  }
2802  else
2803  {
2804  UTF_BEGIN;
2805  argvalues[j] = InputFunctionCall(&qdesc->arginfuncs[j],
2806  UTF_U2E(Tcl_GetString(callObjv[j])),
2807  qdesc->argtypioparams[j],
2808  -1);
2809  UTF_END;
2810  }
2811  }
2812 
2813  /************************************************************
2814  * Execute the plan
2815  ************************************************************/
2816  spi_rc = SPI_execute_plan(qdesc->plan, argvalues, nulls,
2818  count);
2819 
2820  my_rc = pltcl_process_SPI_result(interp,
2821  arrayname,
2822  loop_body,
2823  spi_rc,
2824  SPI_tuptable,
2825  SPI_processed);
2826 
2827  pltcl_subtrans_commit(oldcontext, oldowner);
2828  }
2829  PG_CATCH();
2830  {
2831  pltcl_subtrans_abort(interp, oldcontext, oldowner);
2832  return TCL_ERROR;
2833  }
2834  PG_END_TRY();
2835 
2836  return my_rc;
2837 }
static void pltcl_subtrans_begin(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: pltcl.c:2235
static void pltcl_subtrans_abort(Tcl_Interp *interp, MemoryContext oldcontext, ResourceOwner oldowner)
Definition: pltcl.c:2253
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
SPITupleTable * SPI_tuptable
Definition: spi.c:41
static void pltcl_subtrans_commit(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: pltcl.c:2244
FmgrInfo * arginfuncs
Definition: pltcl.c:166
uint64 SPI_processed
Definition: spi.c:39
static pltcl_call_state * pltcl_current_call_state
Definition: pltcl.c:241
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:339
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:2391
SPIPlanPtr plan
Definition: pltcl.c:163
#define UTF_END
Definition: pltcl.c:90
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
bool fn_readonly
Definition: pltcl.c:143
uintptr_t Datum
Definition: postgres.h:372
#define UTF_U2E(x)
Definition: pltcl.c:95
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1623
#define UTF_BEGIN
Definition: pltcl.c:85
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:229
pltcl_proc_desc * prodesc
Definition: pltcl.c:213
Oid * argtypioparams
Definition: pltcl.c:167
void * palloc(Size size)
Definition: mcxt.c:849
int i
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
Tcl_HashTable query_hash
Definition: pltcl.c:116
static int pltcl_SPI_lastoid ( ClientData  cdata,
Tcl_Interp *  interp,
int  objc,
Tcl_Obj *const  objv[] 
)
static

Definition at line 2845 of file pltcl.c.

References SPI_lastoid.

Referenced by pltcl_init_interp().

2847 {
2848  /*
2849  * Check call syntax
2850  */
2851  if (objc != 1)
2852  {
2853  Tcl_WrongNumArgs(interp, 1, objv, "");
2854  return TCL_ERROR;
2855  }
2856 
2857  Tcl_SetObjResult(interp, Tcl_NewWideIntObj(SPI_lastoid));
2858  return TCL_OK;
2859 }
Oid SPI_lastoid
Definition: spi.c:40
static int pltcl_SPI_prepare ( ClientData  cdata,
Tcl_Interp *  interp,
int  objc,
Tcl_Obj *const  objv[] 
)
static

Definition at line 2501 of file pltcl.c.

References ALLOCSET_SMALL_SIZES, AllocSetContextCreate(), pltcl_query_desc::arginfuncs, pltcl_query_desc::argtypes, pltcl_query_desc::argtypioparams, CurrentMemoryContext, CurrentResourceOwner, elog, ERROR, fmgr_info_cxt(), getTypeInputInfo(), i, pltcl_proc_desc::interp_desc, MemoryContextDelete(), MemoryContextSwitchTo(), pltcl_query_desc::nargs, NULL, palloc(), palloc0(), parseTypeString(), PG_CATCH, PG_END_TRY, PG_TRY, pltcl_query_desc::plan, pltcl_subtrans_abort(), pltcl_subtrans_begin(), pltcl_subtrans_commit(), pltcl_call_state::prodesc, pltcl_query_desc::qname, pltcl_interp_desc::query_hash, snprintf(), SPI_keepplan(), SPI_prepare(), TopMemoryContext, UTF_BEGIN, UTF_END, and UTF_U2E.

Referenced by pltcl_init_interp().

2503 {
2504  volatile MemoryContext plan_cxt = NULL;
2505  int nargs;
2506  Tcl_Obj **argsObj;
2507  pltcl_query_desc *qdesc;
2508  int i;
2509  Tcl_HashEntry *hashent;
2510  int hashnew;
2511  Tcl_HashTable *query_hash;
2512  MemoryContext oldcontext = CurrentMemoryContext;
2514 
2515  /************************************************************
2516  * Check the call syntax
2517  ************************************************************/
2518  if (objc != 3)
2519  {
2520  Tcl_WrongNumArgs(interp, 1, objv, "query argtypes");
2521  return TCL_ERROR;
2522  }
2523 
2524  /************************************************************
2525  * Split the argument type list
2526  ************************************************************/
2527  if (Tcl_ListObjGetElements(interp, objv[2], &nargs, &argsObj) != TCL_OK)
2528  return TCL_ERROR;
2529 
2530  /************************************************************
2531  * Allocate the new querydesc structure
2532  *
2533  * struct qdesc and subsidiary data all live in plan_cxt. Note that if the
2534  * function is recompiled for whatever reason, permanent memory leaks
2535  * occur. FIXME someday.
2536  ************************************************************/
2538  "PL/Tcl spi_prepare query",
2540  MemoryContextSwitchTo(plan_cxt);
2541  qdesc = (pltcl_query_desc *) palloc0(sizeof(pltcl_query_desc));
2542  snprintf(qdesc->qname, sizeof(qdesc->qname), "%p", qdesc);
2543  qdesc->nargs = nargs;
2544  qdesc->argtypes = (Oid *) palloc(nargs * sizeof(Oid));
2545  qdesc->arginfuncs = (FmgrInfo *) palloc(nargs * sizeof(FmgrInfo));
2546  qdesc->argtypioparams = (Oid *) palloc(nargs * sizeof(Oid));
2547  MemoryContextSwitchTo(oldcontext);
2548 
2549  /************************************************************
2550  * Execute the prepare inside a sub-transaction, so we can cope with
2551  * errors sanely
2552  ************************************************************/
2553 
2554  pltcl_subtrans_begin(oldcontext, oldowner);
2555 
2556  PG_TRY();
2557  {
2558  /************************************************************
2559  * Resolve argument type names and then look them up by oid
2560  * in the system cache, and remember the required information
2561  * for input conversion.
2562  ************************************************************/
2563  for (i = 0; i < nargs; i++)
2564  {
2565  Oid typId,
2566  typInput,
2567  typIOParam;
2568  int32 typmod;
2569 
2570  parseTypeString(Tcl_GetString(argsObj[i]), &typId, &typmod, false);
2571 
2572  getTypeInputInfo(typId, &typInput, &typIOParam);
2573 
2574  qdesc->argtypes[i] = typId;
2575  fmgr_info_cxt(typInput, &(qdesc->arginfuncs[i]), plan_cxt);
2576  qdesc->argtypioparams[i] = typIOParam;
2577  }
2578 
2579  /************************************************************
2580  * Prepare the plan and check for errors
2581  ************************************************************/
2582  UTF_BEGIN;
2583  qdesc->plan = SPI_prepare(UTF_U2E(Tcl_GetString(objv[1])),
2584  nargs, qdesc->argtypes);
2585  UTF_END;
2586 
2587  if (qdesc->plan == NULL)
2588  elog(ERROR, "SPI_prepare() failed");
2589 
2590  /************************************************************
2591  * Save the plan into permanent memory (right now it's in the
2592  * SPI procCxt, which will go away at function end).
2593  ************************************************************/
2594  if (SPI_keepplan(qdesc->plan))
2595  elog(ERROR, "SPI_keepplan() failed");
2596 
2597  pltcl_subtrans_commit(oldcontext, oldowner);
2598  }
2599  PG_CATCH();
2600  {
2601  pltcl_subtrans_abort(interp, oldcontext, oldowner);
2602 
2603  MemoryContextDelete(plan_cxt);
2604 
2605  return TCL_ERROR;
2606  }
2607  PG_END_TRY();
2608 
2609  /************************************************************
2610  * Insert a hashtable entry for the plan and return
2611  * the key to the caller
2612  ************************************************************/
2614 
2615  hashent = Tcl_CreateHashEntry(query_hash, qdesc->qname, &hashnew);
2616  Tcl_SetHashValue(hashent, (ClientData) qdesc);
2617 
2618  /* qname is ASCII, so no need for encoding conversion */
2619  Tcl_SetObjResult(interp, Tcl_NewStringObj(qdesc->qname, -1));
2620  return TCL_OK;
2621 }
static void pltcl_subtrans_begin(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: pltcl.c:2235
Definition: fmgr.h:56
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
static void pltcl_subtrans_abort(Tcl_Interp *interp, MemoryContext oldcontext, ResourceOwner oldowner)
Definition: pltcl.c:2253
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:482
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:175
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
Oid * argtypes
Definition: pltcl.c:165
static void pltcl_subtrans_commit(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: pltcl.c:2244
FmgrInfo * arginfuncs
Definition: pltcl.c:166
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:256
static pltcl_call_state * pltcl_current_call_state
Definition: pltcl.c:241
char qname[20]
Definition: pltcl.c:162
#define ERROR
Definition: elog.h:43
pltcl_interp_desc * interp_desc
Definition: pltcl.c:145
SPIPlanPtr plan
Definition: pltcl.c:163
int SPI_keepplan(SPIPlanPtr plan)
Definition: spi.c:560
#define UTF_END
Definition: pltcl.c:90
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:137
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2599
MemoryContext TopMemoryContext
Definition: mcxt.c:43
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
void * palloc0(Size size)
Definition: mcxt.c:878
#define UTF_U2E(x)
Definition: pltcl.c:95
void parseTypeString(const char *str, Oid *typeid_p, int32 *typmod_p, bool missing_ok)
Definition: parse_type.c:781
#define UTF_BEGIN
Definition: pltcl.c:85
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:229
pltcl_proc_desc * prodesc
Definition: pltcl.c:213
Oid * argtypioparams
Definition: pltcl.c:167
void * palloc(Size size)
Definition: mcxt.c:849
int i
#define elog
Definition: elog.h:219
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
Tcl_HashTable query_hash
Definition: pltcl.c:116
static void pltcl_subtrans_abort ( Tcl_Interp *  interp,
MemoryContext  oldcontext,
ResourceOwner  oldowner 
)
static

Definition at line 2253 of file pltcl.c.

References CopyErrorData(), CurrentResourceOwner, FlushErrorState(), FreeErrorData(), MemoryContextSwitchTo(), ErrorData::message, pltcl_construct_errorCode(), RollbackAndReleaseCurrentSubTransaction(), UTF_BEGIN, UTF_E2U, and UTF_END.

Referenced by pltcl_returnnext(), pltcl_SPI_execute(), pltcl_SPI_execute_plan(), and pltcl_SPI_prepare().

2255 {
2256  ErrorData *edata;
2257 
2258  /* Save error info */
2259  MemoryContextSwitchTo(oldcontext);
2260  edata = CopyErrorData();
2261  FlushErrorState();
2262 
2263  /* Abort the inner transaction */
2265  MemoryContextSwitchTo(oldcontext);
2266  CurrentResourceOwner = oldowner;
2267 
2268  /* Pass the error data to Tcl */
2269  pltcl_construct_errorCode(interp, edata);
2270  UTF_BEGIN;
2271  Tcl_SetObjResult(interp, Tcl_NewStringObj(UTF_E2U(edata->message), -1));
2272  UTF_END;
2273  FreeErrorData(edata);
2274 }
ErrorData * CopyErrorData(void)
Definition: elog.c:1497
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void FlushErrorState(void)
Definition: elog.c:1587
void FreeErrorData(ErrorData *edata)
Definition: elog.c:1551
#define UTF_E2U(x)
Definition: pltcl.c:98
static void pltcl_construct_errorCode(Tcl_Interp *interp, ErrorData *edata)
Definition: pltcl.c:1803
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4158
#define UTF_END
Definition: pltcl.c:90
#define UTF_BEGIN
Definition: pltcl.c:85
char * message
Definition: elog.h:343
static void pltcl_subtrans_begin ( MemoryContext  oldcontext,
ResourceOwner  oldowner 
)
static

Definition at line 2235 of file pltcl.c.

References BeginInternalSubTransaction(), MemoryContextSwitchTo(), and NULL.

Referenced by pltcl_SPI_execute(), pltcl_SPI_execute_plan(), and pltcl_SPI_prepare().

2236 {
2238 
2239  /* Want to run inside function's memory context */
2240  MemoryContextSwitchTo(oldcontext);
2241 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define NULL
Definition: c.h:229
void BeginInternalSubTransaction(char *name)
Definition: xact.c:4054
static void pltcl_subtrans_commit ( MemoryContext  oldcontext,
ResourceOwner  oldowner 
)
static

Definition at line 2244 of file pltcl.c.

References CurrentResourceOwner, MemoryContextSwitchTo(), and ReleaseCurrentSubTransaction().

Referenced by pltcl_returnnext(), pltcl_SPI_execute(), pltcl_SPI_execute_plan(), and pltcl_SPI_prepare().

2245 {
2246  /* Commit the inner transaction, return to outer xact context */
2248  MemoryContextSwitchTo(oldcontext);
2249  CurrentResourceOwner = oldowner;
2250 }
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4124
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static int pltcl_subtransaction ( ClientData  cdata,
Tcl_Interp *  interp,
int  objc,
Tcl_Obj *const  objv[] 
)
static

Definition at line 2869 of file pltcl.c.

References BeginInternalSubTransaction(), CurrentMemoryContext, CurrentResourceOwner, MemoryContextSwitchTo(), NULL, ReleaseCurrentSubTransaction(), and RollbackAndReleaseCurrentSubTransaction().

Referenced by pltcl_init_interp().

2871 {
2872  MemoryContext oldcontext = CurrentMemoryContext;
2874  int retcode;
2875 
2876  if (objc != 2)
2877  {
2878  Tcl_WrongNumArgs(interp, 1, objv, "command");
2879  return TCL_ERROR;
2880  }
2881 
2882  /*
2883  * Note: we don't use pltcl_subtrans_begin and friends here because we
2884  * don't want the error handling in pltcl_subtrans_abort. But otherwise
2885  * the processing should be about the same as in those functions.
2886  */
2888  MemoryContextSwitchTo(oldcontext);
2889 
2890  retcode = Tcl_EvalObjEx(interp, objv[1], 0);
2891 
2892  if (retcode == TCL_ERROR)
2893  {
2894  /* Rollback the subtransaction */
2896  }
2897  else
2898  {
2899  /* Commit the subtransaction */
2901  }
2902 
2903  /* In either case, restore previous memory context and resource owner */
2904  MemoryContextSwitchTo(oldcontext);
2905  CurrentResourceOwner = oldowner;
2906 
2907  return retcode;
2908 }
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4124
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4158
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define NULL
Definition: c.h:229
void BeginInternalSubTransaction(char *name)
Definition: xact.c:4054
static HeapTuple pltcl_trigger_handler ( PG_FUNCTION_ARGS  ,
pltcl_call_state call_state,
bool  pltrusted 
)
static

Definition at line 1025 of file pltcl.c.

References Assert, compile_pltcl_function(), DatumGetCString, DirectFunctionCall1, elog, ereport, errcode(), errmsg(), ERROR, pltcl_proc_desc::fn_refcount, i, pltcl_proc_desc::internal_proname, pltcl_interp_desc::interp, pltcl_proc_desc::interp_desc, NameStr, NULL, ObjectIdGetDatum, oidout(), pfree(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PG_USED_FOR_ASSERTS_ONLY, pltcl_build_tuple_argument(), pltcl_build_tuple_result(), pltcl_call_state::prodesc, RelationData::rd_id, RelationGetDescr, RelationGetRelid, result, SPI_connect(), SPI_finish(), SPI_getnspname(), SPI_getrelname(), SPI_OK_CONNECT, SPI_OK_FINISH, SPI_register_trigger_data(), TriggerData::tg_event, TriggerData::tg_newtuple, TriggerData::tg_relation, TriggerData::tg_trigger, TriggerData::tg_trigtuple, Trigger::tgargs, Trigger::tgname, Trigger::tgnargs, throw_tcl_error(), pltcl_call_state::trigdata, TRIGGER_FIRED_AFTER, TRIGGER_FIRED_BEFORE, TRIGGER_FIRED_BY_DELETE, TRIGGER_FIRED_BY_INSERT, TRIGGER_FIRED_BY_TRUNCATE, TRIGGER_FIRED_BY_UPDATE, TRIGGER_FIRED_FOR_ROW, TRIGGER_FIRED_FOR_STATEMENT, TRIGGER_FIRED_INSTEAD, pltcl_proc_desc::user_proname, utf_e2u(), and utf_u2e().

Referenced by pltcl_handler().

1027 {
1028  pltcl_proc_desc *prodesc;
1029  Tcl_Interp *volatile interp;
1030  TriggerData *trigdata = (TriggerData *) fcinfo->context;
1031  char *stroid;
1032  TupleDesc tupdesc;
1033  volatile HeapTuple rettup;
1034  Tcl_Obj *tcl_cmd;
1035  Tcl_Obj *tcl_trigtup;
1036  Tcl_Obj *tcl_newtup;
1037  int tcl_rc;
1038  int i;
1039  const char *result;
1040  int result_Objc;
1041  Tcl_Obj **result_Objv;
1042  int rc PG_USED_FOR_ASSERTS_ONLY;
1043 
1044  call_state->trigdata = trigdata;
1045 
1046  /* Connect to SPI manager */
1047  if (SPI_connect() != SPI_OK_CONNECT)
1048  elog(ERROR, "could not connect to SPI manager");
1049 
1050  /* Make transition tables visible to this SPI connection */
1051  rc = SPI_register_trigger_data(trigdata);
1052  Assert(rc >= 0);
1053 
1054  /* Find or compile the function */
1055  prodesc = compile_pltcl_function(fcinfo->flinfo->fn_oid,
1056  RelationGetRelid(trigdata->tg_relation),
1057  false, /* not an event trigger */
1058  pltrusted);
1059 
1060  call_state->prodesc = prodesc;
1061  prodesc->fn_refcount++;
1062 
1063  interp = prodesc->interp_desc->interp;
1064 
1065  tupdesc = RelationGetDescr(trigdata->tg_relation);
1066 
1067  /************************************************************
1068  * Create the tcl command to call the internal
1069  * proc in the interpreter
1070  ************************************************************/
1071  tcl_cmd = Tcl_NewObj();
1072  Tcl_IncrRefCount(tcl_cmd);
1073 
1074  PG_TRY();
1075  {
1076  /* The procedure name (note this is all ASCII, so no utf_e2u) */
1077  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1078  Tcl_NewStringObj(prodesc->internal_proname, -1));
1079 
1080  /* The trigger name for argument TG_name */
1081  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1082  Tcl_NewStringObj(utf_e2u(trigdata->tg_trigger->tgname), -1));
1083 
1084  /* The oid of the trigger relation for argument TG_relid */
1085  /* Consider not converting to a string for more performance? */
1087  ObjectIdGetDatum(trigdata->tg_relation->rd_id)));
1088  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1089  Tcl_NewStringObj(stroid, -1));
1090  pfree(stroid);
1091 
1092  /* The name of the table the trigger is acting on: TG_table_name */
1093  stroid = SPI_getrelname(trigdata->tg_relation);
1094  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1095  Tcl_NewStringObj(utf_e2u(stroid), -1));
1096  pfree(stroid);
1097 
1098  /* The schema of the table the trigger is acting on: TG_table_schema */
1099  stroid = SPI_getnspname(trigdata->tg_relation);
1100  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1101  Tcl_NewStringObj(utf_e2u(stroid), -1));
1102  pfree(stroid);
1103 
1104  /* A list of attribute names for argument TG_relatts */
1105  tcl_trigtup = Tcl_NewObj();
1106  Tcl_ListObjAppendElement(NULL, tcl_trigtup, Tcl_NewObj());
1107  for (i = 0; i < tupdesc->natts; i++)
1108  {
1109  if (tupdesc->attrs[i]->attisdropped)
1110  Tcl_ListObjAppendElement(NULL, tcl_trigtup, Tcl_NewObj());
1111  else
1112  Tcl_ListObjAppendElement(NULL, tcl_trigtup,
1113  Tcl_NewStringObj(utf_e2u(NameStr(tupdesc->attrs[i]->attname)), -1));
1114  }
1115  Tcl_ListObjAppendElement(NULL, tcl_cmd, tcl_trigtup);
1116 
1117  /* The when part of the event for TG_when */
1118  if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
1119  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1120  Tcl_NewStringObj("BEFORE", -1));
1121  else if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
1122  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1123  Tcl_NewStringObj("AFTER", -1));
1124  else if (TRIGGER_FIRED_INSTEAD(trigdata->tg_event))
1125  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1126  Tcl_NewStringObj("INSTEAD OF", -1));
1127  else
1128  elog(ERROR, "unrecognized WHEN tg_event: %u", trigdata->tg_event);
1129 
1130  /* The level part of the event for TG_level */
1131  if (TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
1132  {
1133  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1134  Tcl_NewStringObj("ROW", -1));
1135 
1136  /* Build the data list for the trigtuple */
1137  tcl_trigtup = pltcl_build_tuple_argument(trigdata->tg_trigtuple,
1138  tupdesc);
1139 
1140  /*
1141  * Now the command part of the event for TG_op and data for NEW
1142  * and OLD
1143  */
1144  if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
1145  {
1146  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1147  Tcl_NewStringObj("INSERT", -1));
1148 
1149  Tcl_ListObjAppendElement(NULL, tcl_cmd, tcl_trigtup);
1150  Tcl_ListObjAppendElement(NULL, tcl_cmd, Tcl_NewObj());
1151 
1152  rettup = trigdata->tg_trigtuple;
1153  }
1154  else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
1155  {
1156  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1157  Tcl_NewStringObj("DELETE", -1));
1158 
1159  Tcl_ListObjAppendElement(NULL, tcl_cmd, Tcl_NewObj());
1160  Tcl_ListObjAppendElement(NULL, tcl_cmd, tcl_trigtup);
1161 
1162  rettup = trigdata->tg_trigtuple;
1163  }
1164  else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
1165  {
1166  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1167  Tcl_NewStringObj("UPDATE", -1));
1168 
1169  tcl_newtup = pltcl_build_tuple_argument(trigdata->tg_newtuple,
1170  tupdesc);
1171 
1172  Tcl_ListObjAppendElement(NULL, tcl_cmd, tcl_newtup);
1173  Tcl_ListObjAppendElement(NULL, tcl_cmd, tcl_trigtup);
1174 
1175  rettup = trigdata->tg_newtuple;
1176  }
1177  else
1178  elog(ERROR, "unrecognized OP tg_event: %u", trigdata->tg_event);
1179  }
1180  else if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
1181  {
1182  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1183  Tcl_NewStringObj("STATEMENT", -1));
1184 
1185  if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
1186  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1187  Tcl_NewStringObj("INSERT", -1));
1188  else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
1189  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1190  Tcl_NewStringObj("DELETE", -1));
1191  else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
1192  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1193  Tcl_NewStringObj("UPDATE", -1));
1194  else if (TRIGGER_FIRED_BY_TRUNCATE(trigdata->tg_event))
1195  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1196  Tcl_NewStringObj("TRUNCATE", -1));
1197  else
1198  elog(ERROR, "unrecognized OP tg_event: %u", trigdata->tg_event);
1199 
1200  Tcl_ListObjAppendElement(NULL, tcl_cmd, Tcl_NewObj());
1201  Tcl_ListObjAppendElement(NULL, tcl_cmd, Tcl_NewObj());
1202 
1203  rettup = (HeapTuple) NULL;
1204  }
1205  else
1206  elog(ERROR, "unrecognized LEVEL tg_event: %u", trigdata->tg_event);
1207 
1208  /* Finally append the arguments from CREATE TRIGGER */
1209  for (i = 0; i < trigdata->tg_trigger->tgnargs; i++)
1210  Tcl_ListObjAppendElement(NULL, tcl_cmd,
1211  Tcl_NewStringObj(utf_e2u(trigdata->tg_trigger->tgargs[i]), -1));
1212 
1213  }
1214  PG_CATCH();
1215  {
1216  Tcl_DecrRefCount(tcl_cmd);
1217  PG_RE_THROW();
1218  }
1219  PG_END_TRY();
1220 
1221  /************************************************************
1222  * Call the Tcl function
1223  *
1224  * We assume no PG error can be thrown directly from this call.
1225  ************************************************************/
1226  tcl_rc = Tcl_EvalObjEx(interp, tcl_cmd, (TCL_EVAL_DIRECT | TCL_EVAL_GLOBAL));
1227 
1228  /* Release refcount to free tcl_cmd (and all subsidiary objects) */
1229  Tcl_DecrRefCount(tcl_cmd);
1230 
1231  /************************************************************
1232  * Check for errors reported by Tcl.
1233  ************************************************************/
1234  if (tcl_rc != TCL_OK)
1235  throw_tcl_error(interp, prodesc->user_proname);
1236 
1237  /************************************************************
1238  * Exit SPI environment.
1239  ************************************************************/
1240  if (SPI_finish() != SPI_OK_FINISH)
1241  elog(ERROR, "SPI_finish() failed");
1242 
1243  /************************************************************
1244  * The return value from the procedure might be one of
1245  * the magic strings OK or SKIP, or a list from array get.
1246  * We can check for OK or SKIP without worrying about encoding.
1247  ************************************************************/
1248  result = Tcl_GetStringResult(interp);
1249 
1250  if (strcmp(result, "OK") == 0)
1251  return rettup;
1252  if (strcmp(result, "SKIP") == 0)
1253  return (HeapTuple) NULL;
1254 
1255  /************************************************************
1256  * Otherwise, the return value should be a column name/value list
1257  * specifying the modified tuple to return.
1258  ************************************************************/
1259  if (Tcl_ListObjGetElements(interp, Tcl_GetObjResult(interp),
1260  &result_Objc, &result_Objv) != TCL_OK)
1261  ereport(ERROR,
1262  (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
1263  errmsg("could not split return value from trigger: %s",
1264  utf_u2e(Tcl_GetStringResult(interp)))));
1265 
1266  /* Convert function result to tuple */
1267  rettup = pltcl_build_tuple_result(interp, result_Objv, result_Objc,
1268  call_state);
1269 
1270  return rettup;
1271 }
#define SPI_OK_CONNECT
Definition: spi.h:50
HeapTupleData * HeapTuple
Definition: htup.h:70
#define RelationGetDescr(relation)
Definition: rel.h:428
int SPI_connect(void)
Definition: spi.c:84
int SPI_finish(void)
Definition: spi.c:148
char * user_proname
Definition: pltcl.c:137
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1633
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
#define TRIGGER_FIRED_AFTER(event)
Definition: trigger.h:91
Datum oidout(PG_FUNCTION_ARGS)
Definition: oid.c:127
static void throw_tcl_error(Tcl_Interp *interp, const char *proname)
Definition: pltcl.c:1327
#define TRIGGER_FIRED_FOR_STATEMENT(event)
Definition: trigger.h:85
HeapTuple tg_trigtuple
Definition: trigger.h:35
void pfree(void *pointer)
Definition: mcxt.c:950
#define TRIGGER_FIRED_BY_TRUNCATE(event)
Definition: trigger.h:79
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define DatumGetCString(X)
Definition: postgres.h:572
char * tgname
Definition: reltrigger.h:27
pltcl_interp_desc * interp_desc
Definition: pltcl.c:145
static char * utf_u2e(const char *src)
Definition: pltcl.c:74
unsigned long fn_refcount
Definition: pltcl.c:140
Tcl_Interp * interp
Definition: pltcl.c:115
#define ereport(elevel, rest)
Definition: elog.h:122
int SPI_register_trigger_data(TriggerData *tdata)
Definition: spi.c:2730
Oid rd_id
Definition: rel.h:116
static Tcl_Obj * pltcl_build_tuple_argument(HeapTuple tuple, TupleDesc tupdesc)
Definition: pltcl.c:3003
char ** tgargs
Definition: reltrigger.h:40
#define TRIGGER_FIRED_BY_DELETE(event)
Definition: trigger.h:73
static pltcl_proc_desc * compile_pltcl_function(Oid fn_oid, Oid tgreloid, bool is_event_trigger, bool pltrusted)
Definition: pltcl.c:1356
static char * utf_e2u(const char *src)
Definition: pltcl.c:80
Trigger * tg_trigger
Definition: trigger.h:37
HeapTuple tg_newtuple
Definition: trigger.h:36
char * SPI_getrelname(Relation rel)
Definition: spi.c:910
static HeapTuple pltcl_build_tuple_result(Tcl_Interp *interp, Tcl_Obj **kvObjv, int kvObjc, pltcl_call_state *call_state)
Definition: pltcl.c:3070
char * internal_proname
Definition: pltcl.c:138
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
pltcl_proc_desc * prodesc
Definition: pltcl.c:213
TriggerEvent tg_event
Definition: trigger.h:33
char * SPI_getnspname(Relation rel)
Definition: spi.c:916
#define SPI_OK_FINISH
Definition: spi.h:51
#define PG_RE_THROW()
Definition: elog.h:314
#define TRIGGER_FIRED_BEFORE(event)
Definition: trigger.h:88
#define TRIGGER_FIRED_INSTEAD(event)
Definition: trigger.h:94
#define TRIGGER_FIRED_BY_INSERT(event)
Definition: trigger.h:70
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
int16 tgnargs
Definition: reltrigger.h:37
#define NameStr(name)
Definition: c.h:499
#define elog
Definition: elog.h:219
TriggerData * trigdata
Definition: pltcl.c:210
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:990
#define PG_TRY()
Definition: elog.h:284
#define TRIGGER_FIRED_FOR_ROW(event)
Definition: trigger.h:82
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition: trigger.h:76
#define RelationGetRelid(relation)
Definition: rel.h:416
#define PG_END_TRY()
Definition: elog.h:300
Relation tg_relation
Definition: trigger.h:34
static int pltcl_WaitForEvent ( CONST86 Tcl_Time *  timePtr)
static

Definition at line 380 of file pltcl.c.

Referenced by _PG_init().

381 {
382  return 0;
383 }
Datum pltclu_call_handler ( PG_FUNCTION_ARGS  )

Definition at line 701 of file pltcl.c.

References pltcl_handler().

702 {
703  return pltcl_handler(fcinfo, false);
704 }
static Datum pltcl_handler(PG_FUNCTION_ARGS, bool pltrusted)
Definition: pltcl.c:712
static void start_proc_error_callback ( void *  arg)
static

Definition at line 669 of file pltcl.c.

References errcontext.

Referenced by call_pltcl_start_proc().

670 {
671  const char *gucname = (const char *) arg;
672 
673  /* translator: %s is "pltcl.start_proc" or "pltclu.start_proc" */
674  errcontext("processing %s parameter", gucname);
675 }
#define errcontext
Definition: elog.h:164
void * arg
static void throw_tcl_error ( Tcl_Interp *  interp,
const char *  proname 
)
static

Definition at line 1327 of file pltcl.c.

References ereport, errcode(), errcontext, errmsg(), ERROR, pstrdup(), and utf_u2e().

Referenced by pltcl_event_trigger_handler(), pltcl_func_handler(), and pltcl_trigger_handler().

1328 {
1329  /*
1330  * Caution is needed here because Tcl_GetVar could overwrite the
1331  * interpreter result (even though it's not really supposed to), and we
1332  * can't control the order of evaluation of ereport arguments. Hence, make
1333  * real sure we have our own copy of the result string before invoking
1334  * Tcl_GetVar.
1335  */
1336  char *emsg;
1337  char *econtext;
1338 
1339  emsg = pstrdup(utf_u2e(Tcl_GetStringResult(interp)));
1340  econtext = utf_u2e(Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY));
1341  ereport(ERROR,
1342  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
1343  errmsg("%s", emsg),
1344  errcontext("%s\nin PL/Tcl function \"%s\"",
1345  econtext, proname)));
1346 }
char * pstrdup(const char *in)
Definition: mcxt.c:1077
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
static char * utf_u2e(const char *src)
Definition: pltcl.c:74
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define errcontext
Definition: elog.h:164
static char* utf_e2u ( const char *  src)
inlinestatic

Definition at line 80 of file pltcl.c.

References pg_server_to_any(), and PG_UTF8.

Referenced by pltcl_event_trigger_handler(), and pltcl_trigger_handler().

81 {
82  return pg_server_to_any(src, strlen(src), PG_UTF8);
83 }
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:645
static char* utf_u2e ( const char *  src)
inlinestatic

Definition at line 74 of file pltcl.c.

References pg_any_to_server(), and PG_UTF8.

Referenced by compile_pltcl_function(), pltcl_build_tuple_result(), pltcl_func_handler(), pltcl_returnnext(), pltcl_trigger_handler(), and throw_tcl_error().

75 {
76  return pg_any_to_server(src, strlen(src), PG_UTF8);
77 }
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:572

Variable Documentation

const TclExceptionNameMap exception_name_map[]
static
Initial value:
= {
{NULL, 0}
}
#define NULL
Definition: c.h:229

Definition at line 252 of file pltcl.c.

PG_MODULE_MAGIC

Definition at line 42 of file pltcl.c.

pltcl_call_state* pltcl_current_call_state = NULL
static

Definition at line 241 of file pltcl.c.

Referenced by pltcl_handler(), and pltcl_returnnext().

Tcl_Interp* pltcl_hold_interp = NULL
static

Definition at line 236 of file pltcl.c.

Referenced by _PG_init(), and pltcl_init_interp().

HTAB* pltcl_interp_htab = NULL
static

Definition at line 237 of file pltcl.c.

bool pltcl_pm_init_done = false
static

Definition at line 235 of file pltcl.c.

Referenced by _PG_init().

HTAB* pltcl_proc_htab = NULL
static

Definition at line 238 of file pltcl.c.

char* pltcl_start_proc = NULL
static

Definition at line 233 of file pltcl.c.

Referenced by _PG_init(), and call_pltcl_start_proc().

char* pltclu_start_proc = NULL
static

Definition at line 234 of file pltcl.c.

Referenced by _PG_init(), and call_pltcl_start_proc().