1 /**********************************************************************
2 * plpython.c - python as a procedural language for PostgreSQL
4 * $PostgreSQL: pgsql/src/pl/plpython/plpython.c,v 1.78 2006/04/27 01:05:05 momjian Exp $
6 *********************************************************************
16 /* postgreSQL stuff */
17 #include "access/heapam.h"
18 #include "catalog/pg_proc.h"
19 #include "catalog/pg_type.h"
20 #include "commands/trigger.h"
21 #include "executor/spi.h"
24 #include "nodes/makefuncs.h"
25 #include "parser/parse_type.h"
26 #include "tcop/tcopprot.h"
27 #include "utils/builtins.h"
28 #include "utils/lsyscache.h"
29 #include "utils/memutils.h"
30 #include "utils/syscache.h"
31 #include "utils/typcache.h"
36 /* convert Postgresql Datum or tuple into a PyObject.
37 * input to Python. Tuples are converted to dictionary
41 typedef PyObject *(*PLyDatumToObFunc) (const char *);
43 typedef struct PLyDatumToOb
45 PLyDatumToObFunc func;
51 typedef struct PLyTupleToOb
57 typedef union PLyTypeInput
63 /* convert PyObject to a Postgresql Datum or tuple.
66 typedef struct PLyObToDatum
68 FmgrInfo typfunc; /* The type's input function */
69 Oid typoid; /* The OID of the type */
74 typedef struct PLyObToTuple
80 typedef union PLyTypeOutput
86 /* all we need to move Postgresql data to Python objects,
89 typedef struct PLyTypeInfo
96 * is_rowtype can be: -1 not known yet (initial state) 0 scalar datatype
97 * 1 rowtype 2 rowtype, but I/O functions not set up yet
102 /* cached procedure data */
103 typedef struct PLyProcedure
105 char *proname; /* SQL name of procedure */
106 char *pyname; /* Python name of procedure */
107 TransactionId fn_xmin;
110 PLyTypeInfo result; /* also used to store info for trigger tuple
112 bool is_setof; /* true, if procedure returns result set */
113 PyObject *setof; /* contents of result set. */
114 int setof_count; /* numbef of items to return in result set */
115 int setof_current; /* current item in result set */
116 char **argnames; /* Argument names */
117 PLyTypeInfo args[FUNC_MAX_ARGS];
119 PyObject *code; /* compiled procedure code */
120 PyObject *statics; /* data saved across calls, local scope */
121 PyObject *globals; /* data saved across calls, global scope */
122 PyObject *me; /* PyCObject containing pointer to this
128 typedef struct PLyPlanObject
131 void *plan; /* return of an SPI_saveplan */
138 typedef struct PLyResultObject
141 /* HeapTuple *tuples; */
142 PyObject *nrows; /* number of rows returned by query */
143 PyObject *rows; /* data rows, or None if no data returned */
144 PyObject *status; /* query status, SPI_OK_*, or SPI_ERR_* */
148 /* function declarations */
150 /* Two exported functions: first is the magic telling Postgresql
151 * what function call interface it implements. Second allows
152 * preinitialization of the interpreter during postmaster startup.
154 Datum plpython_call_handler(PG_FUNCTION_ARGS);
155 void plpython_init(void);
157 PG_FUNCTION_INFO_V1(plpython_call_handler);
159 /* most of the remaining of the declarations, all static */
161 /* these should only be called once at the first call
162 * of plpython_call_handler. initialize the python interpreter
165 static void PLy_init_all(void);
166 static void PLy_init_interp(void);
167 static void PLy_init_plpy(void);
169 /* call PyErr_SetString with a vprint interface */
171 PLy_exception_set(PyObject *, const char *,...)
172 __attribute__((format(printf, 2, 3)));
174 /* Get the innermost python procedure called from the backend */
175 static char *PLy_procedure_name(PLyProcedure *);
177 /* some utility functions */
178 static void PLy_elog(int, const char *,...);
179 static char *PLy_traceback(int *);
180 static char *PLy_vprintf(const char *fmt, va_list ap);
181 static char *PLy_printf(const char *fmt,...);
183 static void *PLy_malloc(size_t);
184 static void *PLy_realloc(void *, size_t);
185 static char *PLy_strdup(const char *);
186 static void PLy_free(void *);
188 /* sub handlers for functions and triggers */
189 static Datum PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure *);
190 static HeapTuple PLy_trigger_handler(FunctionCallInfo fcinfo, PLyProcedure *);
192 static PyObject *PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *);
193 static void PLy_function_delete_args(PLyProcedure *);
194 static PyObject *PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *,
196 static HeapTuple PLy_modify_tuple(PLyProcedure *, PyObject *,
197 TriggerData *, HeapTuple);
199 static PyObject *PLy_procedure_call(PLyProcedure *, char *, PyObject *);
201 static PLyProcedure *PLy_procedure_get(FunctionCallInfo fcinfo,
204 static PLyProcedure *PLy_procedure_create(FunctionCallInfo fcinfo,
206 HeapTuple procTup, char *key);
208 static void PLy_procedure_compile(PLyProcedure *, const char *);
209 static char *PLy_procedure_munge_source(const char *, const char *);
210 static void PLy_procedure_delete(PLyProcedure *);
212 static void PLy_typeinfo_init(PLyTypeInfo *);
213 static void PLy_typeinfo_dealloc(PLyTypeInfo *);
214 static void PLy_output_datum_func(PLyTypeInfo *, HeapTuple);
215 static void PLy_output_datum_func2(PLyObToDatum *, HeapTuple);
216 static void PLy_input_datum_func(PLyTypeInfo *, Oid, HeapTuple);
217 static void PLy_input_datum_func2(PLyDatumToOb *, Oid, HeapTuple);
218 static void PLy_output_tuple_funcs(PLyTypeInfo *, TupleDesc);
219 static void PLy_input_tuple_funcs(PLyTypeInfo *, TupleDesc);
221 /* conversion functions */
222 static PyObject *PLyDict_FromTuple(PLyTypeInfo *, HeapTuple, TupleDesc);
223 static PyObject *PLyBool_FromString(const char *);
224 static PyObject *PLyFloat_FromString(const char *);
225 static PyObject *PLyInt_FromString(const char *);
226 static PyObject *PLyLong_FromString(const char *);
227 static PyObject *PLyString_FromString(const char *);
228 static HeapTuple PLyDict_ToTuple(PLyTypeInfo *, PyObject *);
232 static bool PLy_first_call = true;
235 * Currently active plpython function
237 static PLyProcedure *PLy_curr_procedure = NULL;
240 * When a callback from Python into PG incurs an error, we temporarily store
241 * the error information here, and return NULL to the Python interpreter.
242 * Any further callback attempts immediately fail, and when the Python
243 * interpreter returns to the calling function, we re-throw the error (even if
244 * Python thinks it trapped the error and doesn't return NULL). Eventually
245 * this ought to be improved to let Python code really truly trap the error,
246 * but that's more of a change from the pre-8.0 semantics than I have time for
247 * now --- it will only be possible if the callback query is executed inside a
250 static ErrorData *PLy_error_in_progress = NULL;
252 static PyObject *PLy_interp_globals = NULL;
253 static PyObject *PLy_interp_safe_globals = NULL;
254 static PyObject *PLy_procedure_cache = NULL;
256 /* Python exceptions */
257 static PyObject *PLy_exc_error = NULL;
258 static PyObject *PLy_exc_fatal = NULL;
259 static PyObject *PLy_exc_spi_error = NULL;
261 /* some globals for the python module */
262 static char PLy_plan_doc[] = {
263 "Store a PostgreSQL plan"
266 static char PLy_result_doc[] = {
267 "Results of a PostgreSQL query"
272 * the function definitions
276 * This routine is a crock, and so is everyplace that calls it. The problem
277 * is that the cached form of plpython functions/queries is allocated permanently
278 * (mostly via malloc()) and never released until backend exit. Subsidiary
279 * data structures such as fmgr info records therefore must live forever
280 * as well. A better implementation would store all this stuff in a per-
281 * function memory context that could be reclaimed at need. In the meantime,
282 * fmgr_info_cxt must be called specifying TopMemoryContext so that whatever
283 * it might allocate, and whatever the eventual function might allocate using
284 * fn_mcxt, will live forever too.
287 perm_fmgr_info(Oid functionId, FmgrInfo *finfo)
289 fmgr_info_cxt(functionId, finfo, TopMemoryContext);
293 plpython_call_handler(PG_FUNCTION_ARGS)
296 PLyProcedure *save_curr_proc;
297 PLyProcedure *volatile proc = NULL;
301 if (SPI_connect() != SPI_OK_CONNECT)
302 elog(ERROR, "could not connect to SPI manager");
304 save_curr_proc = PLy_curr_procedure;
308 if (CALLED_AS_TRIGGER(fcinfo))
310 TriggerData *tdata = (TriggerData *) fcinfo->context;
313 proc = PLy_procedure_get(fcinfo,
314 RelationGetRelid(tdata->tg_relation));
315 PLy_curr_procedure = proc;
316 trv = PLy_trigger_handler(fcinfo, proc);
317 retval = PointerGetDatum(trv);
321 proc = PLy_procedure_get(fcinfo, InvalidOid);
322 PLy_curr_procedure = proc;
323 retval = PLy_function_handler(fcinfo, proc);
328 PLy_curr_procedure = save_curr_proc;
331 /* note: Py_DECREF needs braces around it, as of 2003/08 */
339 PLy_curr_procedure = save_curr_proc;
346 /* trigger and function sub handlers
348 * the python function is expected to return Py_None if the tuple is
349 * acceptable and unmodified. Otherwise it should return a PyString
350 * object who's value is SKIP, or MODIFY. SKIP means don't perform
351 * this action. MODIFY means the tuple has been modified, so update
352 * tuple and perform action. SKIP and MODIFY assume the trigger fires
353 * BEFORE the event and is ROW level. postgres expects the function
354 * to take no arguments and return an argument of type trigger.
357 PLy_trigger_handler(FunctionCallInfo fcinfo, PLyProcedure * proc)
360 PyObject *volatile plargs = NULL;
361 PyObject *volatile plrv = NULL;
365 plargs = PLy_trigger_build_args(fcinfo, proc, &rv);
366 plrv = PLy_procedure_call(proc, "TD", plargs);
368 Assert(plrv != NULL);
369 Assert(!PLy_error_in_progress);
372 * Disconnect from SPI manager
374 if (SPI_finish() != SPI_OK_FINISH)
375 elog(ERROR, "SPI_finish failed");
378 * return of None means we're happy with the tuple
384 if (!PyString_Check(plrv))
386 (errcode(ERRCODE_DATA_EXCEPTION),
387 errmsg("unexpected return value from trigger procedure"),
388 errdetail("Expected None or a String.")));
390 srv = PyString_AsString(plrv);
391 if (pg_strcasecmp(srv, "SKIP") == 0)
393 else if (pg_strcasecmp(srv, "MODIFY") == 0)
395 TriggerData *tdata = (TriggerData *) fcinfo->context;
397 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event) ||
398 TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
399 rv = PLy_modify_tuple(proc, plargs, tdata, rv);
401 elog(WARNING, "ignoring modified tuple in DELETE trigger");
403 else if (pg_strcasecmp(srv, "OK") != 0)
406 * accept "OK" as an alternative to None; otherwise,
410 (errcode(ERRCODE_DATA_EXCEPTION),
411 errmsg("unexpected return value from trigger procedure"),
412 errdetail("Expected None, \"OK\", \"SKIP\", or \"MODIFY\".")));
432 PLy_modify_tuple(PLyProcedure * proc, PyObject * pltd, TriggerData *tdata,
435 PyObject *volatile plntup;
436 PyObject *volatile plkeys;
437 PyObject *volatile platt;
438 PyObject *volatile plval;
439 PyObject *volatile plstr;
445 int *volatile modattrs;
446 Datum *volatile modvalues;
447 char *volatile modnulls;
450 plntup = plkeys = platt = plval = plstr = NULL;
457 if ((plntup = PyDict_GetItemString(pltd, "new")) == NULL)
458 elog(ERROR, "TD[\"new\"] deleted, unable to modify tuple");
459 if (!PyDict_Check(plntup))
460 elog(ERROR, "TD[\"new\"] is not a dictionary object");
463 plkeys = PyDict_Keys(plntup);
464 natts = PyList_Size(plkeys);
466 modattrs = (int *) palloc(natts * sizeof(int));
467 modvalues = (Datum *) palloc(natts * sizeof(Datum));
468 modnulls = (char *) palloc(natts * sizeof(char));
470 tupdesc = tdata->tg_relation->rd_att;
472 for (i = 0; i < natts; i++)
476 platt = PyList_GetItem(plkeys, i);
477 if (!PyString_Check(platt))
478 elog(ERROR, "attribute name is not a string");
479 attn = SPI_fnumber(tupdesc, PyString_AsString(platt));
480 if (attn == SPI_ERROR_NOATTRIBUTE)
481 elog(ERROR, "invalid attribute \"%s\" in tuple",
482 PyString_AsString(platt));
485 plval = PyDict_GetItem(plntup, platt);
487 elog(FATAL, "python interpreter is probably corrupted");
493 if (tupdesc->attrs[atti]->attisdropped)
495 modvalues[i] = (Datum) 0;
498 else if (plval != Py_None)
500 plstr = PyObject_Str(plval);
502 PLy_elog(ERROR, "function \"%s\" could not modify tuple",
504 src = PyString_AsString(plstr);
507 InputFunctionCall(&proc->result.out.r.atts[atti].typfunc,
509 proc->result.out.r.atts[atti].typioparam,
510 tupdesc->attrs[atti]->atttypmod);
519 InputFunctionCall(&proc->result.out.r.atts[atti].typfunc,
521 proc->result.out.r.atts[atti].typioparam,
522 tupdesc->attrs[atti]->atttypmod);
530 rtup = SPI_modifytuple(tdata->tg_relation, otup, natts,
531 modattrs, modvalues, modnulls);
533 elog(ERROR, "SPI_modifytuple failed -- error %d", SPI_result);
564 PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure * proc, HeapTuple *rv)
566 TriggerData *tdata = (TriggerData *) fcinfo->context;
575 PyObject *volatile pltdata = NULL;
580 pltdata = PyDict_New();
582 PLy_elog(ERROR, "could not build arguments for trigger procedure");
584 pltname = PyString_FromString(tdata->tg_trigger->tgname);
585 PyDict_SetItemString(pltdata, "name", pltname);
588 stroid = DatumGetCString(DirectFunctionCall1(oidout,
589 ObjectIdGetDatum(tdata->tg_relation->rd_id)));
590 pltrelid = PyString_FromString(stroid);
591 PyDict_SetItemString(pltdata, "relid", pltrelid);
595 if (TRIGGER_FIRED_BEFORE(tdata->tg_event))
596 pltwhen = PyString_FromString("BEFORE");
597 else if (TRIGGER_FIRED_AFTER(tdata->tg_event))
598 pltwhen = PyString_FromString("AFTER");
601 elog(ERROR, "unrecognized WHEN tg_event: %u", tdata->tg_event);
602 pltwhen = NULL; /* keep compiler quiet */
604 PyDict_SetItemString(pltdata, "when", pltwhen);
607 if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
609 pltlevel = PyString_FromString("ROW");
610 PyDict_SetItemString(pltdata, "level", pltlevel);
613 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
615 pltevent = PyString_FromString("INSERT");
617 PyDict_SetItemString(pltdata, "old", Py_None);
618 pytnew = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
619 tdata->tg_relation->rd_att);
620 PyDict_SetItemString(pltdata, "new", pytnew);
622 *rv = tdata->tg_trigtuple;
624 else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
626 pltevent = PyString_FromString("DELETE");
628 PyDict_SetItemString(pltdata, "new", Py_None);
629 pytold = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
630 tdata->tg_relation->rd_att);
631 PyDict_SetItemString(pltdata, "old", pytold);
633 *rv = tdata->tg_trigtuple;
635 else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
637 pltevent = PyString_FromString("UPDATE");
639 pytnew = PLyDict_FromTuple(&(proc->result), tdata->tg_newtuple,
640 tdata->tg_relation->rd_att);
641 PyDict_SetItemString(pltdata, "new", pytnew);
643 pytold = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
644 tdata->tg_relation->rd_att);
645 PyDict_SetItemString(pltdata, "old", pytold);
647 *rv = tdata->tg_newtuple;
651 elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
652 pltevent = NULL; /* keep compiler quiet */
655 PyDict_SetItemString(pltdata, "event", pltevent);
658 else if (TRIGGER_FIRED_FOR_STATEMENT(tdata->tg_event))
660 pltlevel = PyString_FromString("STATEMENT");
661 PyDict_SetItemString(pltdata, "level", pltlevel);
664 PyDict_SetItemString(pltdata, "old", Py_None);
665 PyDict_SetItemString(pltdata, "new", Py_None);
668 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
669 pltevent = PyString_FromString("INSERT");
670 else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
671 pltevent = PyString_FromString("DELETE");
672 else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
673 pltevent = PyString_FromString("UPDATE");
676 elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
677 pltevent = NULL; /* keep compiler quiet */
680 PyDict_SetItemString(pltdata, "event", pltevent);
684 elog(ERROR, "unrecognized LEVEL tg_event: %u", tdata->tg_event);
686 if (tdata->tg_trigger->tgnargs)
694 pltargs = PyList_New(tdata->tg_trigger->tgnargs);
695 for (i = 0; i < tdata->tg_trigger->tgnargs; i++)
697 pltarg = PyString_FromString(tdata->tg_trigger->tgargs[i]);
700 * stolen, don't Py_DECREF
702 PyList_SetItem(pltargs, i, pltarg);
710 PyDict_SetItemString(pltdata, "args", pltargs);
725 /* function handler and friends */
727 PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure * proc)
730 PyObject *volatile plargs = NULL;
731 PyObject *volatile plrv = NULL;
732 PyObject *volatile plrv_so = NULL;
737 if (!proc->is_setof || proc->setof_count == -1)
739 /* python function not called yet, do it */
740 plargs = PLy_function_build_args(fcinfo, proc);
741 plrv = PLy_procedure_call(proc, "args", plargs);
743 /* SETOF function parameters are deleted when called last row is returned */
744 PLy_function_delete_args(proc);
745 Assert(plrv != NULL);
746 Assert(!PLy_error_in_progress);
750 * Disconnect from SPI manager and then create the return values datum
751 * (if the input function does a palloc for it this must not be
752 * allocated in the SPI memory context because SPI_finish would free
755 if (SPI_finish() != SPI_OK_FINISH)
756 elog(ERROR, "SPI_finish failed");
760 bool is_done = false;
761 ReturnSetInfo *rsi = (ReturnSetInfo *)fcinfo->resultinfo;
763 if (proc->setof_current == -1)
765 /* first time -- do checks and setup */
766 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
767 (rsi->allowedModes & SFRM_ValuePerCall) == 0)
770 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
771 errmsg("only value per call is allowed")));
773 rsi->returnMode = SFRM_ValuePerCall;
775 /* fetch information about returned object */
778 if (PyList_Check(proc->setof))
780 proc->setof_count = PyList_GET_SIZE(proc->setof);
781 else if (PyIter_Check(proc->setof))
782 /* SETOF as iterator, unknown number of items */
783 proc->setof_current = proc->setof_count = 0;
787 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
788 errmsg("SETOF must be returned as list or iterator")));
792 Assert(proc->setof != NULL);
794 /* Fetch next of SETOF */
795 if (PyList_Check(proc->setof))
797 is_done = ++proc->setof_current == proc->setof_count;
799 plrv = PyList_GET_ITEM(proc->setof, proc->setof_current);
801 else if (PyIter_Check(proc->setof))
803 plrv = PyIter_Next(proc->setof);
804 is_done = plrv == NULL;
809 rsi->isDone = ExprMultipleResult;
813 rsi->isDone = ExprEndResult;
814 proc->setof_count = proc->setof_current = -1;
815 Py_DECREF(proc->setof);
822 PLy_function_delete_args(proc);
823 fcinfo->isnull = true;
829 * If the function is declared to return void, the Python
830 * return value must be None. For void-returning functions, we
831 * also treat a None return value as a special "void datum"
832 * rather than NULL (as is the case for non-void-returning
835 if (proc->result.out.d.typoid == VOIDOID)
839 (errcode(ERRCODE_DATATYPE_MISMATCH),
840 errmsg("invalid return value from plpython function"),
841 errdetail("Functions returning type \"void\" must return None.")));
843 fcinfo->isnull = false;
846 else if (plrv == Py_None)
848 fcinfo->isnull = true;
849 rv = InputFunctionCall(&proc->result.out.d.typfunc,
851 proc->result.out.d.typioparam,
854 else if (proc->result.is_rowtype >= 1)
858 /* returning composite type */
859 if (!PyDict_Check(plrv))
860 elog(ERROR, "tuple must be returned as dictionary");
862 tuple = PLyDict_ToTuple(&proc->result, plrv);
865 fcinfo->isnull = false;
866 rv = HeapTupleGetDatum(tuple);
870 fcinfo->isnull = true;
876 fcinfo->isnull = false;
877 plrv_so = PyObject_Str(plrv);
879 PLy_elog(ERROR, "function \"%s\" could not create return value", proc->proname);
880 plrv_sc = PyString_AsString(plrv_so);
881 rv = InputFunctionCall(&proc->result.out.d.typfunc,
883 proc->result.out.d.typioparam,
905 PLy_procedure_call(PLyProcedure * proc, char *kargs, PyObject * vargs)
909 PyDict_SetItemString(proc->globals, kargs, vargs);
910 rv = PyEval_EvalCode((PyCodeObject *) proc->code,
911 proc->globals, proc->globals);
914 * If there was an error in a PG callback, propagate that no matter what
915 * Python claims about its success.
917 if (PLy_error_in_progress)
919 ErrorData *edata = PLy_error_in_progress;
921 PLy_error_in_progress = NULL;
925 if (rv == NULL || PyErr_Occurred())
928 PLy_elog(ERROR, "function \"%s\" failed", proc->proname);
935 PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure * proc)
937 PyObject *volatile arg = NULL;
938 PyObject *volatile args = NULL;
943 args = PyList_New(proc->nargs);
944 for (i = 0; i < proc->nargs; i++)
946 if (proc->args[i].is_rowtype > 0)
948 if (fcinfo->argnull[i])
956 HeapTupleData tmptup;
958 td = DatumGetHeapTupleHeader(fcinfo->arg[i]);
959 /* Extract rowtype info and find a tupdesc */
960 tupType = HeapTupleHeaderGetTypeId(td);
961 tupTypmod = HeapTupleHeaderGetTypMod(td);
962 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
964 /* Set up I/O funcs if not done yet */
965 if (proc->args[i].is_rowtype != 1)
966 PLy_input_tuple_funcs(&(proc->args[i]), tupdesc);
968 /* Build a temporary HeapTuple control structure */
969 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
972 arg = PLyDict_FromTuple(&(proc->args[i]), &tmptup, tupdesc);
977 if (fcinfo->argnull[i])
983 ct = OutputFunctionCall(&(proc->args[i].in.d.typfunc),
985 arg = (proc->args[i].in.d.func) (ct);
997 * FIXME -- error check this
999 PyList_SetItem(args, i, arg);
1000 PyDict_SetItemString(proc->globals, proc->argnames[i], arg);
1018 PLy_function_delete_args(PLyProcedure *proc)
1022 for (i = 0; i < proc->nargs; i++)
1023 PyDict_DelItemString(proc->globals, proc->argnames[i]);
1028 * PLyProcedure functions
1031 /* PLy_procedure_get: returns a cached PLyProcedure, or creates, stores and
1032 * returns a new PLyProcedure. fcinfo is the call info, tgreloid is the
1033 * relation OID when calling a trigger, or InvalidOid (zero) for ordinary
1036 static PLyProcedure *
1037 PLy_procedure_get(FunctionCallInfo fcinfo, Oid tgreloid)
1043 PLyProcedure *proc = NULL;
1046 fn_oid = fcinfo->flinfo->fn_oid;
1047 procTup = SearchSysCache(PROCOID,
1048 ObjectIdGetDatum(fn_oid),
1050 if (!HeapTupleIsValid(procTup))
1051 elog(ERROR, "cache lookup failed for function %u", fn_oid);
1053 rv = snprintf(key, sizeof(key), "%u_%u", fn_oid, tgreloid);
1054 if (rv >= sizeof(key) || rv < 0)
1055 elog(ERROR, "key too long");
1057 plproc = PyDict_GetItemString(PLy_procedure_cache, key);
1062 if (!PyCObject_Check(plproc))
1063 elog(FATAL, "expected a PyCObject, didn't get one");
1065 proc = PyCObject_AsVoidPtr(plproc);
1066 if (proc->me != plproc)
1067 elog(FATAL, "proc->me != plproc");
1068 /* did we find an up-to-date cache entry? */
1069 if (proc->fn_xmin != HeapTupleHeaderGetXmin(procTup->t_data) ||
1070 proc->fn_cmin != HeapTupleHeaderGetCmin(procTup->t_data))
1078 proc = PLy_procedure_create(fcinfo, tgreloid, procTup, key);
1080 ReleaseSysCache(procTup);
1085 static PLyProcedure *
1086 PLy_procedure_create(FunctionCallInfo fcinfo, Oid tgreloid,
1087 HeapTuple procTup, char *key)
1089 char procName[NAMEDATALEN + 256];
1090 Form_pg_proc procStruct;
1091 PLyProcedure *volatile proc;
1092 char *volatile procSource = NULL;
1101 procStruct = (Form_pg_proc) GETSTRUCT(procTup);
1103 if (OidIsValid(tgreloid))
1104 rv = snprintf(procName, sizeof(procName),
1105 "__plpython_procedure_%s_%u_trigger_%u",
1106 NameStr(procStruct->proname),
1107 fcinfo->flinfo->fn_oid,
1110 rv = snprintf(procName, sizeof(procName),
1111 "__plpython_procedure_%s_%u",
1112 NameStr(procStruct->proname),
1113 fcinfo->flinfo->fn_oid);
1114 if (rv >= sizeof(procName) || rv < 0)
1115 elog(ERROR, "procedure name would overrun buffer");
1117 proc = PLy_malloc(sizeof(PLyProcedure));
1118 proc->proname = PLy_strdup(NameStr(procStruct->proname));
1119 proc->pyname = PLy_strdup(procName);
1120 proc->fn_xmin = HeapTupleHeaderGetXmin(procTup->t_data);
1121 proc->fn_cmin = HeapTupleHeaderGetCmin(procTup->t_data);
1122 /* Remember if function is STABLE/IMMUTABLE */
1124 (procStruct->provolatile != PROVOLATILE_VOLATILE);
1125 PLy_typeinfo_init(&proc->result);
1126 for (i = 0; i < FUNC_MAX_ARGS; i++)
1127 PLy_typeinfo_init(&proc->args[i]);
1129 proc->code = proc->statics = NULL;
1130 proc->globals = proc->me = NULL;
1131 proc->is_setof = procStruct->proretset;
1133 proc->setof_count = proc->setof_current = -1;
1134 proc->argnames = NULL;
1139 * get information required for output conversion of the return value,
1140 * but only if this isn't a trigger.
1142 if (!CALLED_AS_TRIGGER(fcinfo))
1144 HeapTuple rvTypeTup;
1145 Form_pg_type rvTypeStruct;
1147 rvTypeTup = SearchSysCache(TYPEOID,
1148 ObjectIdGetDatum(procStruct->prorettype),
1150 if (!HeapTupleIsValid(rvTypeTup))
1151 elog(ERROR, "cache lookup failed for type %u",
1152 procStruct->prorettype);
1153 rvTypeStruct = (Form_pg_type) GETSTRUCT(rvTypeTup);
1155 /* Disallow pseudotype result, except for void */
1156 if (rvTypeStruct->typtype == 'p' &&
1157 procStruct->prorettype != VOIDOID)
1159 if (procStruct->prorettype == TRIGGEROID)
1161 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1162 errmsg("trigger functions may only be called as triggers")));
1165 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1166 errmsg("plpython functions cannot return type %s",
1167 format_type_be(procStruct->prorettype))));
1170 if (rvTypeStruct->typtype == 'c')
1172 /* Tuple: set up later, during first call to PLy_function_handler */
1173 proc->result.out.d.typoid = procStruct->prorettype;
1174 proc->result.is_rowtype = 2;
1177 PLy_output_datum_func(&proc->result, rvTypeTup);
1179 ReleaseSysCache(rvTypeTup);
1184 * input/output conversion for trigger tuples. use the result
1185 * TypeInfo variable to store the tuple conversion info.
1187 TriggerData *tdata = (TriggerData *) fcinfo->context;
1189 PLy_input_tuple_funcs(&(proc->result), tdata->tg_relation->rd_att);
1190 PLy_output_tuple_funcs(&(proc->result), tdata->tg_relation->rd_att);
1194 * now get information required for input conversion of the procedure's
1197 proc->nargs = fcinfo->nargs;
1198 proc->argnames = NULL;
1201 argnames = SysCacheGetAttr(PROCOID, procTup, Anum_pg_proc_proargnames, &isnull);
1204 deconstruct_array(DatumGetArrayTypeP(argnames), TEXTOID, -1, false, 'i',
1205 &elems, NULL, &nelems);
1206 if (nelems != proc->nargs)
1208 "proargnames must have the same number of elements "
1209 "as the function has arguments");
1210 proc->argnames = (char **) PLy_malloc(sizeof(char *)*proc->nargs);
1213 for (i = 0; i < fcinfo->nargs; i++)
1215 HeapTuple argTypeTup;
1216 Form_pg_type argTypeStruct;
1218 argTypeTup = SearchSysCache(TYPEOID,
1219 ObjectIdGetDatum(procStruct->proargtypes.values[i]),
1221 if (!HeapTupleIsValid(argTypeTup))
1222 elog(ERROR, "cache lookup failed for type %u",
1223 procStruct->proargtypes.values[i]);
1224 argTypeStruct = (Form_pg_type) GETSTRUCT(argTypeTup);
1226 /* Disallow pseudotype argument */
1227 if (argTypeStruct->typtype == 'p')
1229 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1230 errmsg("plpython functions cannot take type %s",
1231 format_type_be(procStruct->proargtypes.values[i]))));
1233 if (argTypeStruct->typtype != 'c')
1234 PLy_input_datum_func(&(proc->args[i]),
1235 procStruct->proargtypes.values[i],
1238 proc->args[i].is_rowtype = 2; /* still need to set I/O funcs */
1240 ReleaseSysCache(argTypeTup);
1242 /* Fetch argument name */
1244 proc->argnames[i] = PLy_strdup(DatumGetCString(DirectFunctionCall1(textout, elems[i])));
1248 * get the text of the function.
1250 prosrcdatum = SysCacheGetAttr(PROCOID, procTup,
1251 Anum_pg_proc_prosrc, &isnull);
1253 elog(ERROR, "null prosrc");
1254 procSource = DatumGetCString(DirectFunctionCall1(textout,
1257 PLy_procedure_compile(proc, procSource);
1261 proc->me = PyCObject_FromVoidPtr(proc, NULL);
1262 PyDict_SetItemString(PLy_procedure_cache, key, proc->me);
1266 PLy_procedure_delete(proc);
1278 PLy_procedure_compile(PLyProcedure * proc, const char *src)
1280 PyObject *crv = NULL;
1283 proc->globals = PyDict_Copy(PLy_interp_globals);
1286 * SD is private preserved data between calls. GD is global data
1287 * shared by all functions
1289 proc->statics = PyDict_New();
1290 PyDict_SetItemString(proc->globals, "SD", proc->statics);
1293 * insert the function code into the interpreter
1295 msrc = PLy_procedure_munge_source(proc->pyname, src);
1296 crv = PyRun_String(msrc, Py_file_input, proc->globals, NULL);
1299 if (crv != NULL && (!PyErr_Occurred()))
1302 char call[NAMEDATALEN + 256];
1307 * compile a call to the function
1309 clen = snprintf(call, sizeof(call), "%s()", proc->pyname);
1310 if (clen < 0 || clen >= sizeof(call))
1311 elog(ERROR, "string would overflow buffer");
1312 proc->code = Py_CompileString(call, "<string>", Py_eval_input);
1313 if (proc->code != NULL && (!PyErr_Occurred()))
1319 PLy_elog(ERROR, "could not compile function \"%s\"", proc->proname);
1323 PLy_procedure_munge_source(const char *name, const char *src)
1332 * room for function source and the def statement
1334 mlen = (strlen(src) * 2) + strlen(name) + 16;
1336 mrc = PLy_malloc(mlen);
1337 plen = snprintf(mrc, mlen, "def %s():\n\t", name);
1338 Assert(plen >= 0 && plen < mlen);
1345 if (*sp == '\r' && *(sp + 1) == '\n')
1348 if (*sp == '\n' || *sp == '\r')
1361 if (mp > (mrc + mlen))
1362 elog(FATAL, "buffer overrun in PLy_munge_source");
1368 PLy_procedure_delete(PLyProcedure * proc)
1372 Py_XDECREF(proc->code);
1373 Py_XDECREF(proc->statics);
1374 Py_XDECREF(proc->globals);
1375 Py_XDECREF(proc->me);
1377 PLy_free(proc->proname);
1379 PLy_free(proc->pyname);
1380 for (i = 0; i < proc->nargs; i++)
1382 if (proc->args[i].is_rowtype == 1)
1384 if (proc->args[i].in.r.atts)
1385 PLy_free(proc->args[i].in.r.atts);
1386 if (proc->args[i].out.r.atts)
1387 PLy_free(proc->args[i].out.r.atts);
1389 if (proc->argnames && proc->argnames[i])
1390 PLy_free(proc->argnames[i]);
1393 PLy_free(proc->argnames);
1396 /* conversion functions. remember output from python is
1397 * input to postgresql, and vis versa.
1400 PLy_input_tuple_funcs(PLyTypeInfo * arg, TupleDesc desc)
1404 if (arg->is_rowtype == 0)
1405 elog(ERROR, "PLyTypeInfo struct is initialized for a Datum");
1407 arg->is_rowtype = 1;
1408 arg->in.r.natts = desc->natts;
1409 arg->in.r.atts = PLy_malloc(desc->natts * sizeof(PLyDatumToOb));
1411 for (i = 0; i < desc->natts; i++)
1415 if (desc->attrs[i]->attisdropped)
1418 typeTup = SearchSysCache(TYPEOID,
1419 ObjectIdGetDatum(desc->attrs[i]->atttypid),
1421 if (!HeapTupleIsValid(typeTup))
1422 elog(ERROR, "cache lookup failed for type %u",
1423 desc->attrs[i]->atttypid);
1425 PLy_input_datum_func2(&(arg->in.r.atts[i]),
1426 desc->attrs[i]->atttypid,
1429 ReleaseSysCache(typeTup);
1434 PLy_output_tuple_funcs(PLyTypeInfo * arg, TupleDesc desc)
1438 if (arg->is_rowtype == 0)
1439 elog(ERROR, "PLyTypeInfo struct is initialized for a Datum");
1441 arg->is_rowtype = 1;
1442 arg->out.r.natts = desc->natts;
1443 arg->out.r.atts = PLy_malloc(desc->natts * sizeof(PLyDatumToOb));
1445 for (i = 0; i < desc->natts; i++)
1449 if (desc->attrs[i]->attisdropped)
1452 typeTup = SearchSysCache(TYPEOID,
1453 ObjectIdGetDatum(desc->attrs[i]->atttypid),
1455 if (!HeapTupleIsValid(typeTup))
1456 elog(ERROR, "cache lookup failed for type %u",
1457 desc->attrs[i]->atttypid);
1459 PLy_output_datum_func2(&(arg->out.r.atts[i]), typeTup);
1461 ReleaseSysCache(typeTup);
1466 PLy_output_datum_func(PLyTypeInfo * arg, HeapTuple typeTup)
1468 if (arg->is_rowtype > 0)
1469 elog(ERROR, "PLyTypeInfo struct is initialized for a Tuple");
1470 arg->is_rowtype = 0;
1471 PLy_output_datum_func2(&(arg->out.d), typeTup);
1475 PLy_output_datum_func2(PLyObToDatum * arg, HeapTuple typeTup)
1477 Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1479 perm_fmgr_info(typeStruct->typinput, &arg->typfunc);
1480 arg->typoid = HeapTupleGetOid(typeTup);
1481 arg->typioparam = getTypeIOParam(typeTup);
1482 arg->typbyval = typeStruct->typbyval;
1486 PLy_input_datum_func(PLyTypeInfo * arg, Oid typeOid, HeapTuple typeTup)
1488 if (arg->is_rowtype > 0)
1489 elog(ERROR, "PLyTypeInfo struct is initialized for Tuple");
1490 arg->is_rowtype = 0;
1491 PLy_input_datum_func2(&(arg->in.d), typeOid, typeTup);
1495 PLy_input_datum_func2(PLyDatumToOb * arg, Oid typeOid, HeapTuple typeTup)
1497 Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1499 /* Get the type's conversion information */
1500 perm_fmgr_info(typeStruct->typoutput, &arg->typfunc);
1501 arg->typioparam = getTypeIOParam(typeTup);
1502 arg->typbyval = typeStruct->typbyval;
1504 /* Determine which kind of Python object we will convert to */
1508 arg->func = PLyBool_FromString;
1513 arg->func = PLyFloat_FromString;
1517 arg->func = PLyInt_FromString;
1520 arg->func = PLyLong_FromString;
1523 arg->func = PLyString_FromString;
1529 PLy_typeinfo_init(PLyTypeInfo * arg)
1531 arg->is_rowtype = -1;
1532 arg->in.r.natts = arg->out.r.natts = 0;
1533 arg->in.r.atts = NULL;
1534 arg->out.r.atts = NULL;
1538 PLy_typeinfo_dealloc(PLyTypeInfo * arg)
1540 if (arg->is_rowtype == 1)
1543 PLy_free(arg->in.r.atts);
1544 if (arg->out.r.atts)
1545 PLy_free(arg->out.r.atts);
1549 /* assumes that a bool is always returned as a 't' or 'f' */
1551 PLyBool_FromString(const char *src)
1554 return PyInt_FromLong(1);
1555 return PyInt_FromLong(0);
1559 PLyFloat_FromString(const char *src)
1565 v = strtod(src, &eptr);
1566 if (*eptr != '\0' || errno)
1568 return PyFloat_FromDouble(v);
1572 PLyInt_FromString(const char *src)
1578 v = strtol(src, &eptr, 0);
1579 if (*eptr != '\0' || errno)
1581 return PyInt_FromLong(v);
1585 PLyLong_FromString(const char *src)
1587 return PyLong_FromString((char *) src, NULL, 0);
1591 PLyString_FromString(const char *src)
1593 return PyString_FromString(src);
1597 PLyDict_FromTuple(PLyTypeInfo * info, HeapTuple tuple, TupleDesc desc)
1599 PyObject *volatile dict;
1602 if (info->is_rowtype != 1)
1603 elog(ERROR, "PLyTypeInfo structure describes a datum");
1605 dict = PyDict_New();
1607 PLy_elog(ERROR, "could not create tuple dictionary");
1611 for (i = 0; i < info->in.r.natts; i++)
1619 if (desc->attrs[i]->attisdropped)
1622 key = NameStr(desc->attrs[i]->attname);
1623 vattr = heap_getattr(tuple, (i + 1), desc, &is_null);
1625 if (is_null || info->in.r.atts[i].func == NULL)
1626 PyDict_SetItemString(dict, key, Py_None);
1629 vsrc = OutputFunctionCall(&info->in.r.atts[i].typfunc,
1633 * no exceptions allowed
1635 value = info->in.r.atts[i].func(vsrc);
1637 PyDict_SetItemString(dict, key, value);
1654 PLyDict_ToTuple(PLyTypeInfo *info, PyObject *dict)
1662 desc = CreateTupleDescCopy(lookup_rowtype_tupdesc(info->out.d.typoid, -1));
1664 /* Set up tuple type, if neccessary */
1665 if (info->is_rowtype == 2)
1667 PLy_output_tuple_funcs(info, desc);
1668 info->is_rowtype = 1;
1670 Assert(info->is_rowtype == 1);
1673 values = palloc(sizeof(Datum)*desc->natts);
1674 nulls = palloc(sizeof(char)*desc->natts);
1675 for (i = 0; i < desc->natts; ++i)
1681 key = NameStr(desc->attrs[i]->attname);
1685 value = PyDict_GetItemString(dict, key);
1686 if (value != Py_None && value != NULL)
1690 so = PyObject_Str(value);
1691 valuestr = PyString_AsString(so);
1692 values[i] = InputFunctionCall(&info->out.r.atts[i].typfunc
1694 , info->out.r.atts[i].typioparam
1703 values[i] = (Datum) NULL;
1716 tuple = heap_formtuple(desc, values, nulls);
1717 FreeTupleDesc(desc);
1724 /* initialization, some python variables function declared here */
1726 /* interface to postgresql elog */
1727 static PyObject *PLy_debug(PyObject *, PyObject *);
1728 static PyObject *PLy_log(PyObject *, PyObject *);
1729 static PyObject *PLy_info(PyObject *, PyObject *);
1730 static PyObject *PLy_notice(PyObject *, PyObject *);
1731 static PyObject *PLy_warning(PyObject *, PyObject *);
1732 static PyObject *PLy_error(PyObject *, PyObject *);
1733 static PyObject *PLy_fatal(PyObject *, PyObject *);
1735 /* PLyPlanObject, PLyResultObject and SPI interface */
1736 #define is_PLyPlanObject(x) ((x)->ob_type == &PLy_PlanType)
1737 static PyObject *PLy_plan_new(void);
1738 static void PLy_plan_dealloc(PyObject *);
1739 static PyObject *PLy_plan_getattr(PyObject *, char *);
1740 static PyObject *PLy_plan_status(PyObject *, PyObject *);
1742 static PyObject *PLy_result_new(void);
1743 static void PLy_result_dealloc(PyObject *);
1744 static PyObject *PLy_result_getattr(PyObject *, char *);
1745 static PyObject *PLy_result_nrows(PyObject *, PyObject *);
1746 static PyObject *PLy_result_status(PyObject *, PyObject *);
1747 static int PLy_result_length(PyObject *);
1748 static PyObject *PLy_result_item(PyObject *, int);
1749 static PyObject *PLy_result_slice(PyObject *, int, int);
1750 static int PLy_result_ass_item(PyObject *, int, PyObject *);
1751 static int PLy_result_ass_slice(PyObject *, int, int, PyObject *);
1754 static PyObject *PLy_spi_prepare(PyObject *, PyObject *);
1755 static PyObject *PLy_spi_execute(PyObject *, PyObject *);
1756 static PyObject *PLy_spi_execute_query(char *query, long limit);
1757 static PyObject *PLy_spi_execute_plan(PyObject *, PyObject *, long);
1758 static PyObject *PLy_spi_execute_fetch_result(SPITupleTable *, int, int);
1761 static PyTypeObject PLy_PlanType = {
1762 PyObject_HEAD_INIT(NULL)
1764 "PLyPlan", /* tp_name */
1765 sizeof(PLyPlanObject), /* tp_size */
1766 0, /* tp_itemsize */
1771 (destructor) PLy_plan_dealloc, /* tp_dealloc */
1773 (getattrfunc) PLy_plan_getattr, /* tp_getattr */
1777 0, /* tp_as_number */
1778 0, /* tp_as_sequence */
1779 0, /* tp_as_mapping */
1783 0, /* tp_getattro */
1784 0, /* tp_setattro */
1785 0, /* tp_as_buffer */
1787 PLy_plan_doc, /* tp_doc */
1790 static PyMethodDef PLy_plan_methods[] = {
1791 {"status", PLy_plan_status, METH_VARARGS, NULL},
1792 {NULL, NULL, 0, NULL}
1796 static PySequenceMethods PLy_result_as_sequence = {
1797 (inquiry) PLy_result_length, /* sq_length */
1798 (binaryfunc) 0, /* sq_concat */
1799 (intargfunc) 0, /* sq_repeat */
1800 (intargfunc) PLy_result_item, /* sq_item */
1801 (intintargfunc) PLy_result_slice, /* sq_slice */
1802 (intobjargproc) PLy_result_ass_item, /* sq_ass_item */
1803 (intintobjargproc) PLy_result_ass_slice, /* sq_ass_slice */
1806 static PyTypeObject PLy_ResultType = {
1807 PyObject_HEAD_INIT(NULL)
1809 "PLyResult", /* tp_name */
1810 sizeof(PLyResultObject), /* tp_size */
1811 0, /* tp_itemsize */
1816 (destructor) PLy_result_dealloc, /* tp_dealloc */
1818 (getattrfunc) PLy_result_getattr, /* tp_getattr */
1822 0, /* tp_as_number */
1823 &PLy_result_as_sequence, /* tp_as_sequence */
1824 0, /* tp_as_mapping */
1828 0, /* tp_getattro */
1829 0, /* tp_setattro */
1830 0, /* tp_as_buffer */
1832 PLy_result_doc, /* tp_doc */
1835 static PyMethodDef PLy_result_methods[] = {
1836 {"nrows", PLy_result_nrows, METH_VARARGS, NULL},
1837 {"status", PLy_result_status, METH_VARARGS, NULL},
1838 {NULL, NULL, 0, NULL}
1841 static PyMethodDef PLy_methods[] = {
1845 {"debug", PLy_debug, METH_VARARGS, NULL},
1846 {"log", PLy_log, METH_VARARGS, NULL},
1847 {"info", PLy_info, METH_VARARGS, NULL},
1848 {"notice", PLy_notice, METH_VARARGS, NULL},
1849 {"warning", PLy_warning, METH_VARARGS, NULL},
1850 {"error", PLy_error, METH_VARARGS, NULL},
1851 {"fatal", PLy_fatal, METH_VARARGS, NULL},
1854 * create a stored plan
1856 {"prepare", PLy_spi_prepare, METH_VARARGS, NULL},
1859 * execute a plan or query
1861 {"execute", PLy_spi_execute, METH_VARARGS, NULL},
1863 {NULL, NULL, 0, NULL}
1867 /* plan object methods */
1873 if ((ob = PyObject_NEW(PLyPlanObject, &PLy_PlanType)) == NULL)
1881 return (PyObject *) ob;
1886 PLy_plan_dealloc(PyObject * arg)
1888 PLyPlanObject *ob = (PLyPlanObject *) arg;
1891 SPI_freeplan(ob->plan);
1893 PLy_free(ob->types);
1898 for (i = 0; i < ob->nargs; i++)
1899 PLy_typeinfo_dealloc(&ob->args[i]);
1908 PLy_plan_getattr(PyObject * self, char *name)
1910 return Py_FindMethod(PLy_plan_methods, self, name);
1914 PLy_plan_status(PyObject * self, PyObject * args)
1916 if (PyArg_ParseTuple(args, ""))
1920 /* return PyInt_FromLong(self->status); */
1922 PyErr_SetString(PLy_exc_error, "plan.status() takes no arguments");
1928 /* result object methods */
1931 PLy_result_new(void)
1933 PLyResultObject *ob;
1935 if ((ob = PyObject_NEW(PLyResultObject, &PLy_ResultType)) == NULL)
1938 /* ob->tuples = NULL; */
1941 ob->status = Py_None;
1942 ob->nrows = PyInt_FromLong(-1);
1943 ob->rows = PyList_New(0);
1945 return (PyObject *) ob;
1949 PLy_result_dealloc(PyObject * arg)
1951 PLyResultObject *ob = (PLyResultObject *) arg;
1953 Py_XDECREF(ob->nrows);
1954 Py_XDECREF(ob->rows);
1955 Py_XDECREF(ob->status);
1961 PLy_result_getattr(PyObject * self, char *name)
1963 return Py_FindMethod(PLy_result_methods, self, name);
1967 PLy_result_nrows(PyObject * self, PyObject * args)
1969 PLyResultObject *ob = (PLyResultObject *) self;
1971 Py_INCREF(ob->nrows);
1976 PLy_result_status(PyObject * self, PyObject * args)
1978 PLyResultObject *ob = (PLyResultObject *) self;
1980 Py_INCREF(ob->status);
1985 PLy_result_length(PyObject * arg)
1987 PLyResultObject *ob = (PLyResultObject *) arg;
1989 return PyList_Size(ob->rows);
1993 PLy_result_item(PyObject * arg, int idx)
1996 PLyResultObject *ob = (PLyResultObject *) arg;
1998 rv = PyList_GetItem(ob->rows, idx);
2005 PLy_result_ass_item(PyObject * arg, int idx, PyObject * item)
2008 PLyResultObject *ob = (PLyResultObject *) arg;
2011 rv = PyList_SetItem(ob->rows, idx, item);
2016 PLy_result_slice(PyObject * arg, int lidx, int hidx)
2019 PLyResultObject *ob = (PLyResultObject *) arg;
2021 rv = PyList_GetSlice(ob->rows, lidx, hidx);
2029 PLy_result_ass_slice(PyObject * arg, int lidx, int hidx, PyObject * slice)
2032 PLyResultObject *ob = (PLyResultObject *) arg;
2034 rv = PyList_SetSlice(ob->rows, lidx, hidx, slice);
2040 PLy_spi_prepare(PyObject * self, PyObject * args)
2042 PLyPlanObject *plan;
2043 PyObject *list = NULL;
2044 PyObject *volatile optr = NULL;
2047 MemoryContext oldcontext;
2049 /* Can't execute more if we have an unhandled error */
2050 if (PLy_error_in_progress)
2052 PyErr_SetString(PLy_exc_error, "Transaction aborted.");
2056 if (!PyArg_ParseTuple(args, "s|O", &query, &list))
2058 PyErr_SetString(PLy_exc_spi_error,
2059 "Invalid arguments for plpy.prepare()");
2063 if (list && (!PySequence_Check(list)))
2065 PyErr_SetString(PLy_exc_spi_error,
2066 "Second argument in plpy.prepare() must be a sequence");
2070 if ((plan = (PLyPlanObject *) PLy_plan_new()) == NULL)
2073 oldcontext = CurrentMemoryContext;
2081 nargs = PySequence_Length(list);
2084 plan->nargs = nargs;
2085 plan->types = PLy_malloc(sizeof(Oid) * nargs);
2086 plan->values = PLy_malloc(sizeof(Datum) * nargs);
2087 plan->args = PLy_malloc(sizeof(PLyTypeInfo) * nargs);
2090 * the other loop might throw an exception, if PLyTypeInfo
2091 * member isn't properly initialized the Py_DECREF(plan) will
2094 for (i = 0; i < nargs; i++)
2096 PLy_typeinfo_init(&plan->args[i]);
2097 plan->values[i] = PointerGetDatum(NULL);
2100 for (i = 0; i < nargs; i++)
2105 Form_pg_type typeStruct;
2107 optr = PySequence_GetItem(list, i);
2108 if (!PyString_Check(optr))
2109 elog(ERROR, "Type names must be strings.");
2110 sptr = PyString_AsString(optr);
2113 * Parse possibly-qualified type name and look it up in
2116 names = stringToQualifiedNameList(sptr,
2118 typeTup = typenameType(NULL,
2119 makeTypeNameFromNameList(names));
2121 optr = NULL; /* this is important */
2123 plan->types[i] = HeapTupleGetOid(typeTup);
2124 typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
2125 if (typeStruct->typtype != 'c')
2126 PLy_output_datum_func(&plan->args[i], typeTup);
2128 elog(ERROR, "tuples not handled in plpy.prepare, yet.");
2129 ReleaseSysCache(typeTup);
2134 plan->plan = SPI_prepare(query, plan->nargs, plan->types);
2135 if (plan->plan == NULL)
2136 elog(ERROR, "SPI_prepare failed: %s",
2137 SPI_result_code_string(SPI_result));
2139 /* transfer plan from procCxt to topCxt */
2140 tmpplan = plan->plan;
2141 plan->plan = SPI_saveplan(tmpplan);
2142 SPI_freeplan(tmpplan);
2143 if (plan->plan == NULL)
2144 elog(ERROR, "SPI_saveplan failed: %s",
2145 SPI_result_code_string(SPI_result));
2149 MemoryContextSwitchTo(oldcontext);
2150 PLy_error_in_progress = CopyErrorData();
2154 if (!PyErr_Occurred())
2155 PyErr_SetString(PLy_exc_spi_error,
2156 "Unknown error in PLy_spi_prepare");
2157 /* XXX this oughta be replaced with errcontext mechanism */
2158 PLy_elog(WARNING, "in function %s:",
2159 PLy_procedure_name(PLy_curr_procedure));
2164 return (PyObject *) plan;
2167 /* execute(query="select * from foo", limit=5)
2168 * execute(plan=plan, values=(foo, bar), limit=5)
2171 PLy_spi_execute(PyObject * self, PyObject * args)
2175 PyObject *list = NULL;
2178 /* Can't execute more if we have an unhandled error */
2179 if (PLy_error_in_progress)
2181 PyErr_SetString(PLy_exc_error, "Transaction aborted.");
2185 if (PyArg_ParseTuple(args, "s|l", &query, &limit))
2186 return PLy_spi_execute_query(query, limit);
2190 if (PyArg_ParseTuple(args, "O|Ol", &plan, &list, &limit) &&
2191 is_PLyPlanObject(plan))
2192 return PLy_spi_execute_plan(plan, list, limit);
2194 PyErr_SetString(PLy_exc_error, "Expected a query or plan.");
2199 PLy_spi_execute_plan(PyObject * ob, PyObject * list, long limit)
2204 PLyPlanObject *plan;
2205 MemoryContext oldcontext;
2209 if (!PySequence_Check(list) || PyString_Check(list))
2211 char *msg = "plpy.execute() takes a sequence as its second argument";
2213 PyErr_SetString(PLy_exc_spi_error, msg);
2216 nargs = PySequence_Length(list);
2221 plan = (PLyPlanObject *) ob;
2223 if (nargs != plan->nargs)
2226 PyObject *so = PyObject_Str(list);
2229 PLy_elog(ERROR, "function \"%s\" could not execute plan",
2230 PLy_procedure_name(PLy_curr_procedure));
2231 sv = PyString_AsString(so);
2232 PLy_exception_set(PLy_exc_spi_error,
2233 "Expected sequence of %d arguments, got %d. %s",
2234 plan->nargs, nargs, sv);
2240 oldcontext = CurrentMemoryContext;
2243 char *nulls = palloc(nargs * sizeof(char));
2245 for (i = 0; i < nargs; i++)
2250 elem = PySequence_GetItem(list, i);
2251 if (elem != Py_None)
2253 so = PyObject_Str(elem);
2255 PLy_elog(ERROR, "function \"%s\" could not execute plan",
2256 PLy_procedure_name(PLy_curr_procedure));
2261 char *sv = PyString_AsString(so);
2264 InputFunctionCall(&(plan->args[i].out.d.typfunc),
2266 plan->args[i].out.d.typioparam,
2283 InputFunctionCall(&(plan->args[i].out.d.typfunc),
2285 plan->args[i].out.d.typioparam,
2291 rv = SPI_execute_plan(plan->plan, plan->values, nulls,
2292 PLy_curr_procedure->fn_readonly, limit);
2298 MemoryContextSwitchTo(oldcontext);
2299 PLy_error_in_progress = CopyErrorData();
2303 * cleanup plan->values array
2305 for (i = 0; i < nargs; i++)
2307 if (!plan->args[i].out.d.typbyval &&
2308 (plan->values[i] != PointerGetDatum(NULL)))
2310 pfree(DatumGetPointer(plan->values[i]));
2311 plan->values[i] = PointerGetDatum(NULL);
2315 if (!PyErr_Occurred())
2316 PyErr_SetString(PLy_exc_error,
2317 "Unknown error in PLy_spi_execute_plan");
2318 /* XXX this oughta be replaced with errcontext mechanism */
2319 PLy_elog(WARNING, "in function %s:",
2320 PLy_procedure_name(PLy_curr_procedure));
2325 for (i = 0; i < nargs; i++)
2327 if (!plan->args[i].out.d.typbyval &&
2328 (plan->values[i] != PointerGetDatum(NULL)))
2330 pfree(DatumGetPointer(plan->values[i]));
2331 plan->values[i] = PointerGetDatum(NULL);
2337 PLy_exception_set(PLy_exc_spi_error,
2338 "SPI_execute_plan failed: %s",
2339 SPI_result_code_string(rv));
2343 return PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
2347 PLy_spi_execute_query(char *query, long limit)
2350 MemoryContext oldcontext;
2352 oldcontext = CurrentMemoryContext;
2355 rv = SPI_execute(query, PLy_curr_procedure->fn_readonly, limit);
2359 MemoryContextSwitchTo(oldcontext);
2360 PLy_error_in_progress = CopyErrorData();
2362 if (!PyErr_Occurred())
2363 PyErr_SetString(PLy_exc_spi_error,
2364 "Unknown error in PLy_spi_execute_query");
2365 /* XXX this oughta be replaced with errcontext mechanism */
2366 PLy_elog(WARNING, "in function %s:",
2367 PLy_procedure_name(PLy_curr_procedure));
2374 PLy_exception_set(PLy_exc_spi_error,
2375 "SPI_execute failed: %s",
2376 SPI_result_code_string(rv));
2380 return PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
2384 PLy_spi_execute_fetch_result(SPITupleTable *tuptable, int rows, int status)
2386 PLyResultObject *result;
2387 MemoryContext oldcontext;
2389 result = (PLyResultObject *) PLy_result_new();
2390 Py_DECREF(result->status);
2391 result->status = PyInt_FromLong(status);
2393 if (status == SPI_OK_UTILITY)
2395 Py_DECREF(result->nrows);
2396 result->nrows = PyInt_FromLong(0);
2398 else if (status != SPI_OK_SELECT)
2400 Py_DECREF(result->nrows);
2401 result->nrows = PyInt_FromLong(rows);
2408 PLy_typeinfo_init(&args);
2409 Py_DECREF(result->nrows);
2410 result->nrows = PyInt_FromLong(rows);
2412 oldcontext = CurrentMemoryContext;
2417 Py_DECREF(result->rows);
2418 result->rows = PyList_New(rows);
2420 PLy_input_tuple_funcs(&args, tuptable->tupdesc);
2421 for (i = 0; i < rows; i++)
2423 PyObject *row = PLyDict_FromTuple(&args, tuptable->vals[i],
2426 PyList_SetItem(result->rows, i, row);
2428 PLy_typeinfo_dealloc(&args);
2430 SPI_freetuptable(tuptable);
2435 MemoryContextSwitchTo(oldcontext);
2436 PLy_error_in_progress = CopyErrorData();
2438 if (!PyErr_Occurred())
2439 PyErr_SetString(PLy_exc_error,
2440 "Unknown error in PLy_spi_execute_fetch_result");
2442 PLy_typeinfo_dealloc(&args);
2448 return (PyObject *) result;
2453 * language handler and interpreter initialization
2457 * plpython_init() - Initialize everything that can be
2458 * safely initialized during postmaster
2461 * DO NOT make this static --- it has to be callable by preload
2466 static volatile bool init_active = false;
2468 /* Do initialization only once */
2469 if (!PLy_first_call)
2473 elog(FATAL, "initialization of language module failed");
2479 if (PyErr_Occurred())
2480 PLy_elog(FATAL, "untrapped error in initialization");
2481 PLy_procedure_cache = PyDict_New();
2482 if (PLy_procedure_cache == NULL)
2483 PLy_elog(ERROR, "could not create procedure cache");
2485 PLy_first_call = false;
2491 /* Execute postmaster-startup safe initialization */
2496 * Any other initialization that must be done each time a new backend
2497 * starts -- currently none
2502 PLy_init_interp(void)
2506 mainmod = PyImport_AddModule("__main__");
2507 if (mainmod == NULL || PyErr_Occurred())
2508 PLy_elog(ERROR, "could not import \"__main__\" module.");
2510 PLy_interp_globals = PyModule_GetDict(mainmod);
2511 PLy_interp_safe_globals = PyDict_New();
2512 PyDict_SetItemString(PLy_interp_globals, "GD", PLy_interp_safe_globals);
2514 if (PLy_interp_globals == NULL || PyErr_Occurred())
2515 PLy_elog(ERROR, "could not initialize globals");
2528 * initialize plpy module
2530 PLy_PlanType.ob_type = PLy_ResultType.ob_type = &PyType_Type;
2531 plpy = Py_InitModule("plpy", PLy_methods);
2532 plpy_dict = PyModule_GetDict(plpy);
2534 /* PyDict_SetItemString(plpy, "PlanType", (PyObject *) &PLy_PlanType); */
2536 PLy_exc_error = PyErr_NewException("plpy.Error", NULL, NULL);
2537 PLy_exc_fatal = PyErr_NewException("plpy.Fatal", NULL, NULL);
2538 PLy_exc_spi_error = PyErr_NewException("plpy.SPIError", NULL, NULL);
2539 PyDict_SetItemString(plpy_dict, "Error", PLy_exc_error);
2540 PyDict_SetItemString(plpy_dict, "Fatal", PLy_exc_fatal);
2541 PyDict_SetItemString(plpy_dict, "SPIError", PLy_exc_spi_error);
2544 * initialize main module, and add plpy
2546 main_mod = PyImport_AddModule("__main__");
2547 main_dict = PyModule_GetDict(main_mod);
2548 plpy_mod = PyImport_AddModule("plpy");
2549 PyDict_SetItemString(main_dict, "plpy", plpy_mod);
2550 if (PyErr_Occurred())
2551 elog(ERROR, "could not init plpy");
2554 /* the python interface to the elog function
2555 * don't confuse these with PLy_elog
2557 static PyObject *PLy_output(int, PyObject *, PyObject *);
2560 PLy_debug(PyObject * self, PyObject * args)
2562 return PLy_output(DEBUG2, self, args);
2566 PLy_log(PyObject * self, PyObject * args)
2568 return PLy_output(LOG, self, args);
2572 PLy_info(PyObject * self, PyObject * args)
2574 return PLy_output(INFO, self, args);
2578 PLy_notice(PyObject * self, PyObject * args)
2580 return PLy_output(NOTICE, self, args);
2584 PLy_warning(PyObject * self, PyObject * args)
2586 return PLy_output(WARNING, self, args);
2590 PLy_error(PyObject * self, PyObject * args)
2592 return PLy_output(ERROR, self, args);
2596 PLy_fatal(PyObject * self, PyObject * args)
2598 return PLy_output(FATAL, self, args);
2603 PLy_output(volatile int level, PyObject * self, PyObject * args)
2607 MemoryContext oldcontext;
2609 so = PyObject_Str(args);
2610 if (so == NULL || ((sv = PyString_AsString(so)) == NULL))
2613 sv = "Unable to parse error message in `plpy.elog'";
2616 oldcontext = CurrentMemoryContext;
2619 elog(level, "%s", sv);
2623 MemoryContextSwitchTo(oldcontext);
2624 PLy_error_in_progress = CopyErrorData();
2629 * returning NULL here causes the python interpreter to bail. when
2630 * control passes back to PLy_procedure_call, we check for PG
2631 * exceptions and re-throw the error.
2633 PyErr_SetString(PLy_exc_error, sv);
2641 * return a legal object so the interpreter will continue on its merry way
2649 * Get the name of the last procedure called by the backend (the
2650 * innermost, if a plpython procedure call calls the backend and the
2651 * backend calls another plpython procedure).
2653 * NB: this returns the SQL name, not the internal Python procedure name
2656 PLy_procedure_name(PLyProcedure * proc)
2659 return "<unknown procedure>";
2660 return proc->proname;
2663 /* output a python traceback/exception via the postgresql elog
2664 * function. not pretty.
2667 PLy_exception_set(PyObject * exc, const char *fmt,...)
2673 vsnprintf(buf, sizeof(buf), fmt, ap);
2676 PyErr_SetString(exc, buf);
2679 /* Emit a PG error or notice, together with any available info about the
2680 * current Python error. This should be used to propagate Python errors
2684 PLy_elog(int elevel, const char *fmt,...)
2691 xmsg = PLy_traceback(&xlevel);
2694 emsg = PLy_vprintf(fmt, ap);
2700 (errmsg("plpython: %s", emsg),
2701 (xmsg) ? errdetail("%s", xmsg) : 0));
2718 PLy_traceback(int *xlevel)
2730 * get the current exception
2732 PyErr_Fetch(&e, &v, &tb);
2735 * oops, no exception, return
2743 PyErr_NormalizeException(&e, &v, &tb);
2746 eob = PyObject_Str(e);
2747 if (v && ((vob = PyObject_Str(v)) != NULL))
2748 vstr = PyString_AsString(vob);
2753 * I'm not sure what to do if eob is NULL here -- we can't call PLy_elog
2754 * because that function calls us, so we could end up with infinite
2755 * recursion. I'm not even sure if eob could be NULL here -- would an
2756 * Assert() be more appropriate?
2758 estr = eob ? PyString_AsString(eob) : "Unknown Exception";
2759 xstr = PLy_printf("%s: %s", estr, vstr);
2766 * intuit an appropriate error level based on the exception type
2768 if (PLy_exc_error && PyErr_GivenExceptionMatches(e, PLy_exc_error))
2770 else if (PLy_exc_fatal && PyErr_GivenExceptionMatches(e, PLy_exc_fatal))
2780 PLy_printf(const char *fmt,...)
2786 emsg = PLy_vprintf(fmt, ap);
2792 PLy_vprintf(const char *fmt, va_list ap)
2799 blen = strlen(fmt) * 2;
2802 buf = PLy_malloc(blen * sizeof(char));
2806 bchar = vsnprintf(buf, blen, fmt, ap);
2807 if (bchar > 0 && bchar < blen)
2815 buf = PLy_realloc(buf, blen);
2821 /* python module code */
2823 /* some dumb utility functions */
2825 PLy_malloc(size_t bytes)
2827 void *ptr = malloc(bytes);
2831 (errcode(ERRCODE_OUT_OF_MEMORY),
2832 errmsg("out of memory")));
2837 PLy_realloc(void *optr, size_t bytes)
2839 void *nptr = realloc(optr, bytes);
2843 (errcode(ERRCODE_OUT_OF_MEMORY),
2844 errmsg("out of memory")));
2849 PLy_strdup(const char *str)
2854 len = strlen(str) + 1;
2855 result = PLy_malloc(len);
2856 memcpy(result, str, len);
2861 /* define this away */
2867 /* vim: set noexpandtab nosmarttab shiftwidth=8 cinoptions=l1j1: */