1 /**********************************************************************
2 * plpython.c - python as a procedural language for PostgreSQL
4 * $PostgreSQL: pgsql/src/pl/plpython/plpython.c,v 1.104 2007/11/15 21:14:46 momjian Exp $
6 *********************************************************************
9 #if defined(_MSC_VER) && defined(_DEBUG)
10 /* Python uses #pragma to bring in a non-default libpython on VC++ if
11 * _DEBUG is defined */
13 /* Also hide away errcode, since we load Python.h before postgres.h */
14 #define errcode __msvc_errcode
18 #elif defined (_MSC_VER)
19 #define errcode __msvc_errcode
27 * Py_ssize_t compat for Python <= 2.4
29 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
30 typedef int Py_ssize_t;
32 #define PY_SSIZE_T_MAX INT_MAX
33 #define PY_SSIZE_T_MIN INT_MIN
37 * PyBool_FromLong is supported from 2.3.
39 #if PY_VERSION_HEX < 0x02030000
40 #define PyBool_FromLong(x) PyInt_FromLong(x)
50 /* postgreSQL stuff */
51 #include "access/heapam.h"
52 #include "catalog/pg_proc.h"
53 #include "catalog/pg_type.h"
54 #include "commands/trigger.h"
55 #include "executor/spi.h"
58 #include "nodes/makefuncs.h"
59 #include "parser/parse_type.h"
60 #include "tcop/tcopprot.h"
61 #include "utils/builtins.h"
62 #include "utils/lsyscache.h"
63 #include "utils/memutils.h"
64 #include "utils/syscache.h"
65 #include "utils/typcache.h"
72 /* convert Postgresql Datum or tuple into a PyObject.
73 * input to Python. Tuples are converted to dictionary
77 typedef PyObject *(*PLyDatumToObFunc) (const char *);
79 typedef struct PLyDatumToOb
81 PLyDatumToObFunc func;
87 typedef struct PLyTupleToOb
93 typedef union PLyTypeInput
99 /* convert PyObject to a Postgresql Datum or tuple.
102 typedef struct PLyObToDatum
104 FmgrInfo typfunc; /* The type's input function */
105 Oid typoid; /* The OID of the type */
110 typedef struct PLyObToTuple
116 typedef union PLyTypeOutput
122 /* all we need to move Postgresql data to Python objects,
125 typedef struct PLyTypeInfo
132 * is_rowtype can be: -1 not known yet (initial state) 0 scalar datatype
133 * 1 rowtype 2 rowtype, but I/O functions not set up yet
138 /* cached procedure data */
139 typedef struct PLyProcedure
141 char *proname; /* SQL name of procedure */
142 char *pyname; /* Python name of procedure */
143 TransactionId fn_xmin;
144 ItemPointerData fn_tid;
146 PLyTypeInfo result; /* also used to store info for trigger tuple
148 bool is_setof; /* true, if procedure returns result set */
149 PyObject *setof; /* contents of result set. */
150 char **argnames; /* Argument names */
151 PLyTypeInfo args[FUNC_MAX_ARGS];
153 PyObject *code; /* compiled procedure code */
154 PyObject *statics; /* data saved across calls, local scope */
155 PyObject *globals; /* data saved across calls, global scope */
156 PyObject *me; /* PyCObject containing pointer to this
162 typedef struct PLyPlanObject
165 void *plan; /* return of an SPI_saveplan */
172 typedef struct PLyResultObject
175 /* HeapTuple *tuples; */
176 PyObject * nrows; /* number of rows returned by query */
177 PyObject *rows; /* data rows, or None if no data returned */
178 PyObject *status; /* query status, SPI_OK_*, or SPI_ERR_* */
182 /* function declarations */
184 /* Two exported functions: first is the magic telling Postgresql
185 * what function call interface it implements. Second is for
186 * initialization of the interpreter during library load.
188 Datum plpython_call_handler(PG_FUNCTION_ARGS);
191 PG_FUNCTION_INFO_V1(plpython_call_handler);
193 /* most of the remaining of the declarations, all static */
195 /* these should only be called once at the first call
196 * of plpython_call_handler. initialize the python interpreter
199 static void PLy_init_interp(void);
200 static void PLy_init_plpy(void);
202 /* call PyErr_SetString with a vprint interface */
204 PLy_exception_set(PyObject *, const char *,...)
205 __attribute__((format(printf, 2, 3)));
207 /* Get the innermost python procedure called from the backend */
208 static char *PLy_procedure_name(PLyProcedure *);
210 /* some utility functions */
211 static void PLy_elog(int, const char *,...);
212 static char *PLy_traceback(int *);
213 static char *PLy_vprintf(const char *fmt, va_list ap);
214 static char *PLy_printf(const char *fmt,...);
216 static void *PLy_malloc(size_t);
217 static void *PLy_realloc(void *, size_t);
218 static char *PLy_strdup(const char *);
219 static void PLy_free(void *);
221 /* sub handlers for functions and triggers */
222 static Datum PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure *);
223 static HeapTuple PLy_trigger_handler(FunctionCallInfo fcinfo, PLyProcedure *);
225 static PyObject *PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *);
226 static void PLy_function_delete_args(PLyProcedure *);
227 static PyObject *PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *,
229 static HeapTuple PLy_modify_tuple(PLyProcedure *, PyObject *,
230 TriggerData *, HeapTuple);
232 static PyObject *PLy_procedure_call(PLyProcedure *, char *, PyObject *);
234 static PLyProcedure *PLy_procedure_get(FunctionCallInfo fcinfo,
237 static PLyProcedure *PLy_procedure_create(FunctionCallInfo fcinfo,
239 HeapTuple procTup, char *key);
241 static void PLy_procedure_compile(PLyProcedure *, const char *);
242 static char *PLy_procedure_munge_source(const char *, const char *);
243 static void PLy_procedure_delete(PLyProcedure *);
245 static void PLy_typeinfo_init(PLyTypeInfo *);
246 static void PLy_typeinfo_dealloc(PLyTypeInfo *);
247 static void PLy_output_datum_func(PLyTypeInfo *, HeapTuple);
248 static void PLy_output_datum_func2(PLyObToDatum *, HeapTuple);
249 static void PLy_input_datum_func(PLyTypeInfo *, Oid, HeapTuple);
250 static void PLy_input_datum_func2(PLyDatumToOb *, Oid, HeapTuple);
251 static void PLy_output_tuple_funcs(PLyTypeInfo *, TupleDesc);
252 static void PLy_input_tuple_funcs(PLyTypeInfo *, TupleDesc);
254 /* conversion functions */
255 static PyObject *PLyDict_FromTuple(PLyTypeInfo *, HeapTuple, TupleDesc);
256 static PyObject *PLyBool_FromString(const char *);
257 static PyObject *PLyFloat_FromString(const char *);
258 static PyObject *PLyInt_FromString(const char *);
259 static PyObject *PLyLong_FromString(const char *);
260 static PyObject *PLyString_FromString(const char *);
262 static HeapTuple PLyMapping_ToTuple(PLyTypeInfo *, PyObject *);
263 static HeapTuple PLySequence_ToTuple(PLyTypeInfo *, PyObject *);
264 static HeapTuple PLyObject_ToTuple(PLyTypeInfo *, PyObject *);
267 * Currently active plpython function
269 static PLyProcedure *PLy_curr_procedure = NULL;
272 * When a callback from Python into PG incurs an error, we temporarily store
273 * the error information here, and return NULL to the Python interpreter.
274 * Any further callback attempts immediately fail, and when the Python
275 * interpreter returns to the calling function, we re-throw the error (even if
276 * Python thinks it trapped the error and doesn't return NULL). Eventually
277 * this ought to be improved to let Python code really truly trap the error,
278 * but that's more of a change from the pre-8.0 semantics than I have time for
279 * now --- it will only be possible if the callback query is executed inside a
282 static ErrorData *PLy_error_in_progress = NULL;
284 static PyObject *PLy_interp_globals = NULL;
285 static PyObject *PLy_interp_safe_globals = NULL;
286 static PyObject *PLy_procedure_cache = NULL;
288 /* Python exceptions */
289 static PyObject *PLy_exc_error = NULL;
290 static PyObject *PLy_exc_fatal = NULL;
291 static PyObject *PLy_exc_spi_error = NULL;
293 /* some globals for the python module */
294 static char PLy_plan_doc[] = {
295 "Store a PostgreSQL plan"
298 static char PLy_result_doc[] = {
299 "Results of a PostgreSQL query"
304 * the function definitions
308 * This routine is a crock, and so is everyplace that calls it. The problem
309 * is that the cached form of plpython functions/queries is allocated permanently
310 * (mostly via malloc()) and never released until backend exit. Subsidiary
311 * data structures such as fmgr info records therefore must live forever
312 * as well. A better implementation would store all this stuff in a per-
313 * function memory context that could be reclaimed at need. In the meantime,
314 * fmgr_info_cxt must be called specifying TopMemoryContext so that whatever
315 * it might allocate, and whatever the eventual function might allocate using
316 * fn_mcxt, will live forever too.
319 perm_fmgr_info(Oid functionId, FmgrInfo *finfo)
321 fmgr_info_cxt(functionId, finfo, TopMemoryContext);
325 plpython_call_handler(PG_FUNCTION_ARGS)
328 PLyProcedure *save_curr_proc;
329 PLyProcedure *volatile proc = NULL;
331 if (SPI_connect() != SPI_OK_CONNECT)
332 elog(ERROR, "could not connect to SPI manager");
334 save_curr_proc = PLy_curr_procedure;
338 if (CALLED_AS_TRIGGER(fcinfo))
340 TriggerData *tdata = (TriggerData *) fcinfo->context;
343 proc = PLy_procedure_get(fcinfo,
344 RelationGetRelid(tdata->tg_relation));
345 PLy_curr_procedure = proc;
346 trv = PLy_trigger_handler(fcinfo, proc);
347 retval = PointerGetDatum(trv);
351 proc = PLy_procedure_get(fcinfo, InvalidOid);
352 PLy_curr_procedure = proc;
353 retval = PLy_function_handler(fcinfo, proc);
358 PLy_curr_procedure = save_curr_proc;
361 /* note: Py_DECREF needs braces around it, as of 2003/08 */
369 PLy_curr_procedure = save_curr_proc;
376 /* trigger and function sub handlers
378 * the python function is expected to return Py_None if the tuple is
379 * acceptable and unmodified. Otherwise it should return a PyString
380 * object who's value is SKIP, or MODIFY. SKIP means don't perform
381 * this action. MODIFY means the tuple has been modified, so update
382 * tuple and perform action. SKIP and MODIFY assume the trigger fires
383 * BEFORE the event and is ROW level. postgres expects the function
384 * to take no arguments and return an argument of type trigger.
387 PLy_trigger_handler(FunctionCallInfo fcinfo, PLyProcedure * proc)
390 PyObject *volatile plargs = NULL;
391 PyObject *volatile plrv = NULL;
395 plargs = PLy_trigger_build_args(fcinfo, proc, &rv);
396 plrv = PLy_procedure_call(proc, "TD", plargs);
398 Assert(plrv != NULL);
399 Assert(!PLy_error_in_progress);
402 * Disconnect from SPI manager
404 if (SPI_finish() != SPI_OK_FINISH)
405 elog(ERROR, "SPI_finish failed");
408 * return of None means we're happy with the tuple
414 if (!PyString_Check(plrv))
416 (errcode(ERRCODE_DATA_EXCEPTION),
417 errmsg("unexpected return value from trigger procedure"),
418 errdetail("Expected None or a String.")));
420 srv = PyString_AsString(plrv);
421 if (pg_strcasecmp(srv, "SKIP") == 0)
423 else if (pg_strcasecmp(srv, "MODIFY") == 0)
425 TriggerData *tdata = (TriggerData *) fcinfo->context;
427 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event) ||
428 TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
429 rv = PLy_modify_tuple(proc, plargs, tdata, rv);
431 elog(WARNING, "ignoring modified tuple in DELETE trigger");
433 else if (pg_strcasecmp(srv, "OK") != 0)
436 * accept "OK" as an alternative to None; otherwise, raise an
440 (errcode(ERRCODE_DATA_EXCEPTION),
441 errmsg("unexpected return value from trigger procedure"),
442 errdetail("Expected None, \"OK\", \"SKIP\", or \"MODIFY\".")));
462 PLy_modify_tuple(PLyProcedure * proc, PyObject * pltd, TriggerData *tdata,
465 PyObject *volatile plntup;
466 PyObject *volatile plkeys;
467 PyObject *volatile platt;
468 PyObject *volatile plval;
469 PyObject *volatile plstr;
475 int *volatile modattrs;
476 Datum *volatile modvalues;
477 char *volatile modnulls;
480 plntup = plkeys = platt = plval = plstr = NULL;
487 if ((plntup = PyDict_GetItemString(pltd, "new")) == NULL)
488 elog(ERROR, "TD[\"new\"] deleted, cannot modify tuple");
489 if (!PyDict_Check(plntup))
490 elog(ERROR, "TD[\"new\"] is not a dictionary object");
493 plkeys = PyDict_Keys(plntup);
494 natts = PyList_Size(plkeys);
496 modattrs = (int *) palloc(natts * sizeof(int));
497 modvalues = (Datum *) palloc(natts * sizeof(Datum));
498 modnulls = (char *) palloc(natts * sizeof(char));
500 tupdesc = tdata->tg_relation->rd_att;
502 for (i = 0; i < natts; i++)
506 platt = PyList_GetItem(plkeys, i);
507 if (!PyString_Check(platt))
508 elog(ERROR, "attribute name is not a string");
509 attn = SPI_fnumber(tupdesc, PyString_AsString(platt));
510 if (attn == SPI_ERROR_NOATTRIBUTE)
511 elog(ERROR, "invalid attribute \"%s\" in tuple",
512 PyString_AsString(platt));
515 plval = PyDict_GetItem(plntup, platt);
517 elog(FATAL, "python interpreter is probably corrupted");
523 if (tupdesc->attrs[atti]->attisdropped)
525 modvalues[i] = (Datum) 0;
528 else if (plval != Py_None)
530 plstr = PyObject_Str(plval);
532 PLy_elog(ERROR, "function \"%s\" could not modify tuple",
534 src = PyString_AsString(plstr);
537 InputFunctionCall(&proc->result.out.r.atts[atti].typfunc,
539 proc->result.out.r.atts[atti].typioparam,
540 tupdesc->attrs[atti]->atttypmod);
549 InputFunctionCall(&proc->result.out.r.atts[atti].typfunc,
551 proc->result.out.r.atts[atti].typioparam,
552 tupdesc->attrs[atti]->atttypmod);
560 rtup = SPI_modifytuple(tdata->tg_relation, otup, natts,
561 modattrs, modvalues, modnulls);
563 elog(ERROR, "SPI_modifytuple failed -- error %d", SPI_result);
594 PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure * proc, HeapTuple *rv)
596 TriggerData *tdata = (TriggerData *) fcinfo->context;
607 PyObject *volatile pltdata = NULL;
612 pltdata = PyDict_New();
614 PLy_elog(ERROR, "could not build arguments for trigger procedure");
616 pltname = PyString_FromString(tdata->tg_trigger->tgname);
617 PyDict_SetItemString(pltdata, "name", pltname);
620 stroid = DatumGetCString(DirectFunctionCall1(oidout,
621 ObjectIdGetDatum(tdata->tg_relation->rd_id)));
622 pltrelid = PyString_FromString(stroid);
623 PyDict_SetItemString(pltdata, "relid", pltrelid);
627 stroid = SPI_getrelname(tdata->tg_relation);
628 plttablename = PyString_FromString(stroid);
629 PyDict_SetItemString(pltdata, "table_name", plttablename);
630 Py_DECREF(plttablename);
633 stroid = SPI_getnspname(tdata->tg_relation);
634 plttableschema = PyString_FromString(stroid);
635 PyDict_SetItemString(pltdata, "table_schema", plttableschema);
636 Py_DECREF(plttableschema);
640 if (TRIGGER_FIRED_BEFORE(tdata->tg_event))
641 pltwhen = PyString_FromString("BEFORE");
642 else if (TRIGGER_FIRED_AFTER(tdata->tg_event))
643 pltwhen = PyString_FromString("AFTER");
646 elog(ERROR, "unrecognized WHEN tg_event: %u", tdata->tg_event);
647 pltwhen = NULL; /* keep compiler quiet */
649 PyDict_SetItemString(pltdata, "when", pltwhen);
652 if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
654 pltlevel = PyString_FromString("ROW");
655 PyDict_SetItemString(pltdata, "level", pltlevel);
658 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
660 pltevent = PyString_FromString("INSERT");
662 PyDict_SetItemString(pltdata, "old", Py_None);
663 pytnew = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
664 tdata->tg_relation->rd_att);
665 PyDict_SetItemString(pltdata, "new", pytnew);
667 *rv = tdata->tg_trigtuple;
669 else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
671 pltevent = PyString_FromString("DELETE");
673 PyDict_SetItemString(pltdata, "new", Py_None);
674 pytold = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
675 tdata->tg_relation->rd_att);
676 PyDict_SetItemString(pltdata, "old", pytold);
678 *rv = tdata->tg_trigtuple;
680 else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
682 pltevent = PyString_FromString("UPDATE");
684 pytnew = PLyDict_FromTuple(&(proc->result), tdata->tg_newtuple,
685 tdata->tg_relation->rd_att);
686 PyDict_SetItemString(pltdata, "new", pytnew);
688 pytold = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
689 tdata->tg_relation->rd_att);
690 PyDict_SetItemString(pltdata, "old", pytold);
692 *rv = tdata->tg_newtuple;
696 elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
697 pltevent = NULL; /* keep compiler quiet */
700 PyDict_SetItemString(pltdata, "event", pltevent);
703 else if (TRIGGER_FIRED_FOR_STATEMENT(tdata->tg_event))
705 pltlevel = PyString_FromString("STATEMENT");
706 PyDict_SetItemString(pltdata, "level", pltlevel);
709 PyDict_SetItemString(pltdata, "old", Py_None);
710 PyDict_SetItemString(pltdata, "new", Py_None);
713 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
714 pltevent = PyString_FromString("INSERT");
715 else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
716 pltevent = PyString_FromString("DELETE");
717 else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
718 pltevent = PyString_FromString("UPDATE");
721 elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
722 pltevent = NULL; /* keep compiler quiet */
725 PyDict_SetItemString(pltdata, "event", pltevent);
729 elog(ERROR, "unrecognized LEVEL tg_event: %u", tdata->tg_event);
731 if (tdata->tg_trigger->tgnargs)
739 pltargs = PyList_New(tdata->tg_trigger->tgnargs);
740 for (i = 0; i < tdata->tg_trigger->tgnargs; i++)
742 pltarg = PyString_FromString(tdata->tg_trigger->tgargs[i]);
745 * stolen, don't Py_DECREF
747 PyList_SetItem(pltargs, i, pltarg);
755 PyDict_SetItemString(pltdata, "args", pltargs);
770 /* function handler and friends */
772 PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure * proc)
775 PyObject *volatile plargs = NULL;
776 PyObject *volatile plrv = NULL;
777 PyObject *volatile plrv_so = NULL;
782 if (!proc->is_setof || proc->setof == NULL)
784 /* Simple type returning function or first time for SETOF function */
785 plargs = PLy_function_build_args(fcinfo, proc);
786 plrv = PLy_procedure_call(proc, "args", plargs);
790 * SETOF function parameters will be deleted when last row is
793 PLy_function_delete_args(proc);
794 Assert(plrv != NULL);
795 Assert(!PLy_error_in_progress);
799 * Disconnect from SPI manager and then create the return values datum
800 * (if the input function does a palloc for it this must not be
801 * allocated in the SPI memory context because SPI_finish would free
804 if (SPI_finish() != SPI_OK_FINISH)
805 elog(ERROR, "SPI_finish failed");
809 bool has_error = false;
810 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
812 if (proc->setof == NULL)
814 /* first time -- do checks and setup */
815 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
816 (rsi->allowedModes & SFRM_ValuePerCall) == 0)
819 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
820 errmsg("only value per call is allowed")));
822 rsi->returnMode = SFRM_ValuePerCall;
824 /* Make iterator out of returned object */
825 proc->setof = PyObject_GetIter(plrv);
829 if (proc->setof == NULL)
831 (errcode(ERRCODE_DATATYPE_MISMATCH),
832 errmsg("returned object cannot be iterated"),
833 errdetail("SETOF must be returned as iterable object")));
836 /* Fetch next from iterator */
837 plrv = PyIter_Next(proc->setof);
839 rsi->isDone = ExprMultipleResult;
842 rsi->isDone = ExprEndResult;
843 has_error = PyErr_Occurred() != NULL;
846 if (rsi->isDone == ExprEndResult)
848 /* Iterator is exhausted or error happened */
849 Py_DECREF(proc->setof);
856 PLy_function_delete_args(proc);
860 (errcode(ERRCODE_DATA_EXCEPTION),
861 errmsg("error fetching next item from iterator")));
863 fcinfo->isnull = true;
869 * If the function is declared to return void, the Python return value
870 * must be None. For void-returning functions, we also treat a None
871 * return value as a special "void datum" rather than NULL (as is the
872 * case for non-void-returning functions).
874 if (proc->result.out.d.typoid == VOIDOID)
878 (errcode(ERRCODE_DATATYPE_MISMATCH),
879 errmsg("invalid return value from plpython function"),
880 errdetail("Functions returning type \"void\" must return None.")));
882 fcinfo->isnull = false;
885 else if (plrv == Py_None)
887 fcinfo->isnull = true;
888 if (proc->result.is_rowtype < 1)
889 rv = InputFunctionCall(&proc->result.out.d.typfunc,
891 proc->result.out.d.typioparam,
897 else if (proc->result.is_rowtype >= 1)
899 HeapTuple tuple = NULL;
901 if (PySequence_Check(plrv))
902 /* composite type as sequence (tuple, list etc) */
903 tuple = PLySequence_ToTuple(&proc->result, plrv);
904 else if (PyMapping_Check(plrv))
905 /* composite type as mapping (currently only dict) */
906 tuple = PLyMapping_ToTuple(&proc->result, plrv);
908 /* returned as smth, must provide method __getattr__(name) */
909 tuple = PLyObject_ToTuple(&proc->result, plrv);
913 fcinfo->isnull = false;
914 rv = HeapTupleGetDatum(tuple);
918 fcinfo->isnull = true;
924 fcinfo->isnull = false;
925 plrv_so = PyObject_Str(plrv);
927 PLy_elog(ERROR, "function \"%s\" could not create return value", proc->proname);
928 plrv_sc = PyString_AsString(plrv_so);
929 rv = InputFunctionCall(&proc->result.out.d.typfunc,
931 proc->result.out.d.typioparam,
953 PLy_procedure_call(PLyProcedure * proc, char *kargs, PyObject * vargs)
957 PyDict_SetItemString(proc->globals, kargs, vargs);
958 rv = PyEval_EvalCode((PyCodeObject *) proc->code,
959 proc->globals, proc->globals);
962 * If there was an error in a PG callback, propagate that no matter what
963 * Python claims about its success.
965 if (PLy_error_in_progress)
967 ErrorData *edata = PLy_error_in_progress;
969 PLy_error_in_progress = NULL;
973 if (rv == NULL || PyErr_Occurred())
976 PLy_elog(ERROR, "function \"%s\" failed", proc->proname);
983 PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure * proc)
985 PyObject *volatile arg = NULL;
986 PyObject *volatile args = NULL;
991 args = PyList_New(proc->nargs);
992 for (i = 0; i < proc->nargs; i++)
994 if (proc->args[i].is_rowtype > 0)
996 if (fcinfo->argnull[i])
1004 HeapTupleData tmptup;
1006 td = DatumGetHeapTupleHeader(fcinfo->arg[i]);
1007 /* Extract rowtype info and find a tupdesc */
1008 tupType = HeapTupleHeaderGetTypeId(td);
1009 tupTypmod = HeapTupleHeaderGetTypMod(td);
1010 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
1012 /* Set up I/O funcs if not done yet */
1013 if (proc->args[i].is_rowtype != 1)
1014 PLy_input_tuple_funcs(&(proc->args[i]), tupdesc);
1016 /* Build a temporary HeapTuple control structure */
1017 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
1020 arg = PLyDict_FromTuple(&(proc->args[i]), &tmptup, tupdesc);
1021 ReleaseTupleDesc(tupdesc);
1026 if (fcinfo->argnull[i])
1032 ct = OutputFunctionCall(&(proc->args[i].in.d.typfunc),
1034 arg = (proc->args[i].in.d.func) (ct);
1045 if (PyList_SetItem(args, i, arg) == -1 ||
1047 PyDict_SetItemString(proc->globals, proc->argnames[i], arg) == -1))
1048 PLy_elog(ERROR, "problem setting up arguments for \"%s\"", proc->proname);
1066 PLy_function_delete_args(PLyProcedure * proc)
1070 if (!proc->argnames)
1073 for (i = 0; i < proc->nargs; i++)
1074 PyDict_DelItemString(proc->globals, proc->argnames[i]);
1079 * PLyProcedure functions
1082 /* PLy_procedure_get: returns a cached PLyProcedure, or creates, stores and
1083 * returns a new PLyProcedure. fcinfo is the call info, tgreloid is the
1084 * relation OID when calling a trigger, or InvalidOid (zero) for ordinary
1087 static PLyProcedure *
1088 PLy_procedure_get(FunctionCallInfo fcinfo, Oid tgreloid)
1094 PLyProcedure *proc = NULL;
1097 fn_oid = fcinfo->flinfo->fn_oid;
1098 procTup = SearchSysCache(PROCOID,
1099 ObjectIdGetDatum(fn_oid),
1101 if (!HeapTupleIsValid(procTup))
1102 elog(ERROR, "cache lookup failed for function %u", fn_oid);
1104 rv = snprintf(key, sizeof(key), "%u_%u", fn_oid, tgreloid);
1105 if (rv >= sizeof(key) || rv < 0)
1106 elog(ERROR, "key too long");
1108 plproc = PyDict_GetItemString(PLy_procedure_cache, key);
1113 if (!PyCObject_Check(plproc))
1114 elog(FATAL, "expected a PyCObject, didn't get one");
1116 proc = PyCObject_AsVoidPtr(plproc);
1117 if (proc->me != plproc)
1118 elog(FATAL, "proc->me != plproc");
1119 /* did we find an up-to-date cache entry? */
1120 if (proc->fn_xmin != HeapTupleHeaderGetXmin(procTup->t_data) ||
1121 !ItemPointerEquals(&proc->fn_tid, &procTup->t_self))
1129 proc = PLy_procedure_create(fcinfo, tgreloid, procTup, key);
1131 ReleaseSysCache(procTup);
1136 static PLyProcedure *
1137 PLy_procedure_create(FunctionCallInfo fcinfo, Oid tgreloid,
1138 HeapTuple procTup, char *key)
1140 char procName[NAMEDATALEN + 256];
1141 Form_pg_proc procStruct;
1142 PLyProcedure *volatile proc;
1143 char *volatile procSource = NULL;
1152 procStruct = (Form_pg_proc) GETSTRUCT(procTup);
1154 if (OidIsValid(tgreloid))
1155 rv = snprintf(procName, sizeof(procName),
1156 "__plpython_procedure_%s_%u_trigger_%u",
1157 NameStr(procStruct->proname),
1158 fcinfo->flinfo->fn_oid,
1161 rv = snprintf(procName, sizeof(procName),
1162 "__plpython_procedure_%s_%u",
1163 NameStr(procStruct->proname),
1164 fcinfo->flinfo->fn_oid);
1165 if (rv >= sizeof(procName) || rv < 0)
1166 elog(ERROR, "procedure name would overrun buffer");
1168 proc = PLy_malloc(sizeof(PLyProcedure));
1169 proc->proname = PLy_strdup(NameStr(procStruct->proname));
1170 proc->pyname = PLy_strdup(procName);
1171 proc->fn_xmin = HeapTupleHeaderGetXmin(procTup->t_data);
1172 proc->fn_tid = procTup->t_self;
1173 /* Remember if function is STABLE/IMMUTABLE */
1175 (procStruct->provolatile != PROVOLATILE_VOLATILE);
1176 PLy_typeinfo_init(&proc->result);
1177 for (i = 0; i < FUNC_MAX_ARGS; i++)
1178 PLy_typeinfo_init(&proc->args[i]);
1180 proc->code = proc->statics = NULL;
1181 proc->globals = proc->me = NULL;
1182 proc->is_setof = procStruct->proretset;
1184 proc->argnames = NULL;
1189 * get information required for output conversion of the return value,
1190 * but only if this isn't a trigger.
1192 if (!CALLED_AS_TRIGGER(fcinfo))
1194 HeapTuple rvTypeTup;
1195 Form_pg_type rvTypeStruct;
1197 rvTypeTup = SearchSysCache(TYPEOID,
1198 ObjectIdGetDatum(procStruct->prorettype),
1200 if (!HeapTupleIsValid(rvTypeTup))
1201 elog(ERROR, "cache lookup failed for type %u",
1202 procStruct->prorettype);
1203 rvTypeStruct = (Form_pg_type) GETSTRUCT(rvTypeTup);
1205 /* Disallow pseudotype result, except for void */
1206 if (rvTypeStruct->typtype == TYPTYPE_PSEUDO &&
1207 procStruct->prorettype != VOIDOID)
1209 if (procStruct->prorettype == TRIGGEROID)
1211 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1212 errmsg("trigger functions can only be called as triggers")));
1215 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1216 errmsg("plpython functions cannot return type %s",
1217 format_type_be(procStruct->prorettype))));
1220 if (rvTypeStruct->typtype == TYPTYPE_COMPOSITE)
1223 * Tuple: set up later, during first call to
1224 * PLy_function_handler
1226 proc->result.out.d.typoid = procStruct->prorettype;
1227 proc->result.is_rowtype = 2;
1230 PLy_output_datum_func(&proc->result, rvTypeTup);
1232 ReleaseSysCache(rvTypeTup);
1237 * input/output conversion for trigger tuples. use the result
1238 * TypeInfo variable to store the tuple conversion info.
1240 TriggerData *tdata = (TriggerData *) fcinfo->context;
1242 PLy_input_tuple_funcs(&(proc->result), tdata->tg_relation->rd_att);
1243 PLy_output_tuple_funcs(&(proc->result), tdata->tg_relation->rd_att);
1247 * now get information required for input conversion of the
1248 * procedure's arguments.
1250 proc->nargs = fcinfo->nargs;
1253 argnames = SysCacheGetAttr(PROCOID, procTup, Anum_pg_proc_proargnames, &isnull);
1256 deconstruct_array(DatumGetArrayTypeP(argnames), TEXTOID, -1, false, 'i',
1257 &elems, NULL, &nelems);
1258 if (nelems != proc->nargs)
1260 "proargnames must have the same number of elements "
1261 "as the function has arguments");
1262 proc->argnames = (char **) PLy_malloc(sizeof(char *) * proc->nargs);
1263 memset(proc->argnames, 0, sizeof(char *) * proc->nargs);
1266 for (i = 0; i < fcinfo->nargs; i++)
1268 HeapTuple argTypeTup;
1269 Form_pg_type argTypeStruct;
1271 argTypeTup = SearchSysCache(TYPEOID,
1272 ObjectIdGetDatum(procStruct->proargtypes.values[i]),
1274 if (!HeapTupleIsValid(argTypeTup))
1275 elog(ERROR, "cache lookup failed for type %u",
1276 procStruct->proargtypes.values[i]);
1277 argTypeStruct = (Form_pg_type) GETSTRUCT(argTypeTup);
1279 /* Disallow pseudotype argument */
1280 if (argTypeStruct->typtype == TYPTYPE_PSEUDO)
1282 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1283 errmsg("plpython functions cannot take type %s",
1284 format_type_be(procStruct->proargtypes.values[i]))));
1286 if (argTypeStruct->typtype != TYPTYPE_COMPOSITE)
1287 PLy_input_datum_func(&(proc->args[i]),
1288 procStruct->proargtypes.values[i],
1291 proc->args[i].is_rowtype = 2; /* still need to set I/O funcs */
1293 ReleaseSysCache(argTypeTup);
1295 /* Fetch argument name */
1297 proc->argnames[i] = PLy_strdup(DatumGetCString(DirectFunctionCall1(textout, elems[i])));
1301 * get the text of the function.
1303 prosrcdatum = SysCacheGetAttr(PROCOID, procTup,
1304 Anum_pg_proc_prosrc, &isnull);
1306 elog(ERROR, "null prosrc");
1307 procSource = DatumGetCString(DirectFunctionCall1(textout,
1310 PLy_procedure_compile(proc, procSource);
1314 proc->me = PyCObject_FromVoidPtr(proc, NULL);
1315 PyDict_SetItemString(PLy_procedure_cache, key, proc->me);
1319 PLy_procedure_delete(proc);
1331 PLy_procedure_compile(PLyProcedure * proc, const char *src)
1333 PyObject *crv = NULL;
1336 proc->globals = PyDict_Copy(PLy_interp_globals);
1339 * SD is private preserved data between calls. GD is global data shared by
1342 proc->statics = PyDict_New();
1343 PyDict_SetItemString(proc->globals, "SD", proc->statics);
1346 * insert the function code into the interpreter
1348 msrc = PLy_procedure_munge_source(proc->pyname, src);
1349 crv = PyRun_String(msrc, Py_file_input, proc->globals, NULL);
1352 if (crv != NULL && (!PyErr_Occurred()))
1355 char call[NAMEDATALEN + 256];
1360 * compile a call to the function
1362 clen = snprintf(call, sizeof(call), "%s()", proc->pyname);
1363 if (clen < 0 || clen >= sizeof(call))
1364 elog(ERROR, "string would overflow buffer");
1365 proc->code = Py_CompileString(call, "<string>", Py_eval_input);
1366 if (proc->code != NULL && (!PyErr_Occurred()))
1372 PLy_elog(ERROR, "could not compile function \"%s\"", proc->proname);
1376 PLy_procedure_munge_source(const char *name, const char *src)
1385 * room for function source and the def statement
1387 mlen = (strlen(src) * 2) + strlen(name) + 16;
1389 mrc = PLy_malloc(mlen);
1390 plen = snprintf(mrc, mlen, "def %s():\n\t", name);
1391 Assert(plen >= 0 && plen < mlen);
1398 if (*sp == '\r' && *(sp + 1) == '\n')
1401 if (*sp == '\n' || *sp == '\r')
1414 if (mp > (mrc + mlen))
1415 elog(FATAL, "buffer overrun in PLy_munge_source");
1421 PLy_procedure_delete(PLyProcedure * proc)
1425 Py_XDECREF(proc->code);
1426 Py_XDECREF(proc->statics);
1427 Py_XDECREF(proc->globals);
1428 Py_XDECREF(proc->me);
1430 PLy_free(proc->proname);
1432 PLy_free(proc->pyname);
1433 for (i = 0; i < proc->nargs; i++)
1435 if (proc->args[i].is_rowtype == 1)
1437 if (proc->args[i].in.r.atts)
1438 PLy_free(proc->args[i].in.r.atts);
1439 if (proc->args[i].out.r.atts)
1440 PLy_free(proc->args[i].out.r.atts);
1442 if (proc->argnames && proc->argnames[i])
1443 PLy_free(proc->argnames[i]);
1446 PLy_free(proc->argnames);
1449 /* conversion functions. remember output from python is
1450 * input to postgresql, and vis versa.
1453 PLy_input_tuple_funcs(PLyTypeInfo * arg, TupleDesc desc)
1457 if (arg->is_rowtype == 0)
1458 elog(ERROR, "PLyTypeInfo struct is initialized for a Datum");
1460 arg->is_rowtype = 1;
1461 arg->in.r.natts = desc->natts;
1462 arg->in.r.atts = PLy_malloc(desc->natts * sizeof(PLyDatumToOb));
1464 for (i = 0; i < desc->natts; i++)
1468 if (desc->attrs[i]->attisdropped)
1471 typeTup = SearchSysCache(TYPEOID,
1472 ObjectIdGetDatum(desc->attrs[i]->atttypid),
1474 if (!HeapTupleIsValid(typeTup))
1475 elog(ERROR, "cache lookup failed for type %u",
1476 desc->attrs[i]->atttypid);
1478 PLy_input_datum_func2(&(arg->in.r.atts[i]),
1479 desc->attrs[i]->atttypid,
1482 ReleaseSysCache(typeTup);
1487 PLy_output_tuple_funcs(PLyTypeInfo * arg, TupleDesc desc)
1491 if (arg->is_rowtype == 0)
1492 elog(ERROR, "PLyTypeInfo struct is initialized for a Datum");
1494 arg->is_rowtype = 1;
1495 arg->out.r.natts = desc->natts;
1496 arg->out.r.atts = PLy_malloc(desc->natts * sizeof(PLyDatumToOb));
1498 for (i = 0; i < desc->natts; i++)
1502 if (desc->attrs[i]->attisdropped)
1505 typeTup = SearchSysCache(TYPEOID,
1506 ObjectIdGetDatum(desc->attrs[i]->atttypid),
1508 if (!HeapTupleIsValid(typeTup))
1509 elog(ERROR, "cache lookup failed for type %u",
1510 desc->attrs[i]->atttypid);
1512 PLy_output_datum_func2(&(arg->out.r.atts[i]), typeTup);
1514 ReleaseSysCache(typeTup);
1519 PLy_output_datum_func(PLyTypeInfo * arg, HeapTuple typeTup)
1521 if (arg->is_rowtype > 0)
1522 elog(ERROR, "PLyTypeInfo struct is initialized for a Tuple");
1523 arg->is_rowtype = 0;
1524 PLy_output_datum_func2(&(arg->out.d), typeTup);
1528 PLy_output_datum_func2(PLyObToDatum * arg, HeapTuple typeTup)
1530 Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1532 perm_fmgr_info(typeStruct->typinput, &arg->typfunc);
1533 arg->typoid = HeapTupleGetOid(typeTup);
1534 arg->typioparam = getTypeIOParam(typeTup);
1535 arg->typbyval = typeStruct->typbyval;
1539 PLy_input_datum_func(PLyTypeInfo * arg, Oid typeOid, HeapTuple typeTup)
1541 if (arg->is_rowtype > 0)
1542 elog(ERROR, "PLyTypeInfo struct is initialized for Tuple");
1543 arg->is_rowtype = 0;
1544 PLy_input_datum_func2(&(arg->in.d), typeOid, typeTup);
1548 PLy_input_datum_func2(PLyDatumToOb * arg, Oid typeOid, HeapTuple typeTup)
1550 Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1552 /* Get the type's conversion information */
1553 perm_fmgr_info(typeStruct->typoutput, &arg->typfunc);
1554 arg->typioparam = getTypeIOParam(typeTup);
1555 arg->typbyval = typeStruct->typbyval;
1557 /* Determine which kind of Python object we will convert to */
1561 arg->func = PLyBool_FromString;
1566 arg->func = PLyFloat_FromString;
1570 arg->func = PLyInt_FromString;
1573 arg->func = PLyLong_FromString;
1576 arg->func = PLyString_FromString;
1582 PLy_typeinfo_init(PLyTypeInfo * arg)
1584 arg->is_rowtype = -1;
1585 arg->in.r.natts = arg->out.r.natts = 0;
1586 arg->in.r.atts = NULL;
1587 arg->out.r.atts = NULL;
1591 PLy_typeinfo_dealloc(PLyTypeInfo * arg)
1593 if (arg->is_rowtype == 1)
1596 PLy_free(arg->in.r.atts);
1597 if (arg->out.r.atts)
1598 PLy_free(arg->out.r.atts);
1602 /* assumes that a bool is always returned as a 't' or 'f' */
1604 PLyBool_FromString(const char *src)
1607 * We would like to use Py_RETURN_TRUE and Py_RETURN_FALSE here for
1608 * generating SQL from trigger functions, but those are only supported in
1609 * Python >= 2.3, and we support older versions.
1610 * http://docs.python.org/api/boolObjects.html
1613 return PyBool_FromLong(1);
1614 return PyBool_FromLong(0);
1618 PLyFloat_FromString(const char *src)
1624 v = strtod(src, &eptr);
1625 if (*eptr != '\0' || errno)
1627 return PyFloat_FromDouble(v);
1631 PLyInt_FromString(const char *src)
1637 v = strtol(src, &eptr, 0);
1638 if (*eptr != '\0' || errno)
1640 return PyInt_FromLong(v);
1644 PLyLong_FromString(const char *src)
1646 return PyLong_FromString((char *) src, NULL, 0);
1650 PLyString_FromString(const char *src)
1652 return PyString_FromString(src);
1656 PLyDict_FromTuple(PLyTypeInfo * info, HeapTuple tuple, TupleDesc desc)
1658 PyObject *volatile dict;
1661 if (info->is_rowtype != 1)
1662 elog(ERROR, "PLyTypeInfo structure describes a datum");
1664 dict = PyDict_New();
1666 PLy_elog(ERROR, "could not create tuple dictionary");
1670 for (i = 0; i < info->in.r.natts; i++)
1678 if (desc->attrs[i]->attisdropped)
1681 key = NameStr(desc->attrs[i]->attname);
1682 vattr = heap_getattr(tuple, (i + 1), desc, &is_null);
1684 if (is_null || info->in.r.atts[i].func == NULL)
1685 PyDict_SetItemString(dict, key, Py_None);
1688 vsrc = OutputFunctionCall(&info->in.r.atts[i].typfunc,
1692 * no exceptions allowed
1694 value = info->in.r.atts[i].func(vsrc);
1696 PyDict_SetItemString(dict, key, value);
1713 PLyMapping_ToTuple(PLyTypeInfo * info, PyObject * mapping)
1721 Assert(PyMapping_Check(mapping));
1723 desc = lookup_rowtype_tupdesc(info->out.d.typoid, -1);
1724 if (info->is_rowtype == 2)
1725 PLy_output_tuple_funcs(info, desc);
1726 Assert(info->is_rowtype == 1);
1729 values = palloc(sizeof(Datum) * desc->natts);
1730 nulls = palloc(sizeof(char) * desc->natts);
1731 for (i = 0; i < desc->natts; ++i)
1734 PyObject *volatile value,
1737 key = NameStr(desc->attrs[i]->attname);
1741 value = PyMapping_GetItemString(mapping, key);
1742 if (value == Py_None)
1744 values[i] = (Datum) NULL;
1751 so = PyObject_Str(value);
1753 PLy_elog(ERROR, "cannot convert mapping type");
1754 valuestr = PyString_AsString(so);
1756 values[i] = InputFunctionCall(&info->out.r.atts[i].typfunc
1758 ,info->out.r.atts[i].typioparam
1766 (errcode(ERRCODE_UNDEFINED_COLUMN),
1767 errmsg("no mapping found with key \"%s\"", key),
1768 errhint("to return null in specific column, "
1769 "add value None to map with key named after column")));
1783 tuple = heap_formtuple(desc, values, nulls);
1784 ReleaseTupleDesc(desc);
1793 PLySequence_ToTuple(PLyTypeInfo * info, PyObject * sequence)
1801 Assert(PySequence_Check(sequence));
1804 * Check that sequence length is exactly same as PG tuple's. We actually
1805 * can ignore exceeding items or assume missing ones as null but to avoid
1806 * plpython developer's errors we are strict here
1808 desc = lookup_rowtype_tupdesc(info->out.d.typoid, -1);
1809 if (PySequence_Length(sequence) != desc->natts)
1811 (errcode(ERRCODE_DATATYPE_MISMATCH),
1812 errmsg("returned sequence's length must be same as tuple's length")));
1814 if (info->is_rowtype == 2)
1815 PLy_output_tuple_funcs(info, desc);
1816 Assert(info->is_rowtype == 1);
1819 values = palloc(sizeof(Datum) * desc->natts);
1820 nulls = palloc(sizeof(char) * desc->natts);
1821 for (i = 0; i < desc->natts; ++i)
1823 PyObject *volatile value,
1829 value = PySequence_GetItem(sequence, i);
1831 if (value == Py_None)
1833 values[i] = (Datum) NULL;
1840 so = PyObject_Str(value);
1842 PLy_elog(ERROR, "cannot convert sequence type");
1843 valuestr = PyString_AsString(so);
1844 values[i] = InputFunctionCall(&info->out.r.atts[i].typfunc
1846 ,info->out.r.atts[i].typioparam
1865 tuple = heap_formtuple(desc, values, nulls);
1866 ReleaseTupleDesc(desc);
1875 PLyObject_ToTuple(PLyTypeInfo * info, PyObject * object)
1883 desc = lookup_rowtype_tupdesc(info->out.d.typoid, -1);
1884 if (info->is_rowtype == 2)
1885 PLy_output_tuple_funcs(info, desc);
1886 Assert(info->is_rowtype == 1);
1889 values = palloc(sizeof(Datum) * desc->natts);
1890 nulls = palloc(sizeof(char) * desc->natts);
1891 for (i = 0; i < desc->natts; ++i)
1894 PyObject *volatile value,
1897 key = NameStr(desc->attrs[i]->attname);
1901 value = PyObject_GetAttrString(object, key);
1902 if (value == Py_None)
1904 values[i] = (Datum) NULL;
1911 so = PyObject_Str(value);
1913 PLy_elog(ERROR, "cannot convert object type");
1914 valuestr = PyString_AsString(so);
1915 values[i] = InputFunctionCall(&info->out.r.atts[i].typfunc
1917 ,info->out.r.atts[i].typioparam
1925 (errcode(ERRCODE_UNDEFINED_COLUMN),
1926 errmsg("no attribute named \"%s\"", key),
1927 errhint("to return null in specific column, "
1928 "let returned object to have attribute named "
1929 "after column with value None")));
1943 tuple = heap_formtuple(desc, values, nulls);
1944 ReleaseTupleDesc(desc);
1952 /* initialization, some python variables function declared here */
1954 /* interface to postgresql elog */
1955 static PyObject *PLy_debug(PyObject *, PyObject *);
1956 static PyObject *PLy_log(PyObject *, PyObject *);
1957 static PyObject *PLy_info(PyObject *, PyObject *);
1958 static PyObject *PLy_notice(PyObject *, PyObject *);
1959 static PyObject *PLy_warning(PyObject *, PyObject *);
1960 static PyObject *PLy_error(PyObject *, PyObject *);
1961 static PyObject *PLy_fatal(PyObject *, PyObject *);
1963 /* PLyPlanObject, PLyResultObject and SPI interface */
1964 #define is_PLyPlanObject(x) ((x)->ob_type == &PLy_PlanType)
1965 static PyObject *PLy_plan_new(void);
1966 static void PLy_plan_dealloc(PyObject *);
1967 static PyObject *PLy_plan_getattr(PyObject *, char *);
1968 static PyObject *PLy_plan_status(PyObject *, PyObject *);
1970 static PyObject *PLy_result_new(void);
1971 static void PLy_result_dealloc(PyObject *);
1972 static PyObject *PLy_result_getattr(PyObject *, char *);
1973 static PyObject *PLy_result_nrows(PyObject *, PyObject *);
1974 static PyObject *PLy_result_status(PyObject *, PyObject *);
1975 static Py_ssize_t PLy_result_length(PyObject *);
1976 static PyObject *PLy_result_item(PyObject *, Py_ssize_t);
1977 static PyObject *PLy_result_slice(PyObject *, Py_ssize_t, Py_ssize_t);
1978 static int PLy_result_ass_item(PyObject *, Py_ssize_t, PyObject *);
1979 static int PLy_result_ass_slice(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
1982 static PyObject *PLy_spi_prepare(PyObject *, PyObject *);
1983 static PyObject *PLy_spi_execute(PyObject *, PyObject *);
1984 static PyObject *PLy_spi_execute_query(char *query, long limit);
1985 static PyObject *PLy_spi_execute_plan(PyObject *, PyObject *, long);
1986 static PyObject *PLy_spi_execute_fetch_result(SPITupleTable *, int, int);
1989 static PyTypeObject PLy_PlanType = {
1990 PyObject_HEAD_INIT(NULL)
1992 "PLyPlan", /* tp_name */
1993 sizeof(PLyPlanObject), /* tp_size */
1994 0, /* tp_itemsize */
1999 PLy_plan_dealloc, /* tp_dealloc */
2001 PLy_plan_getattr, /* tp_getattr */
2005 0, /* tp_as_number */
2006 0, /* tp_as_sequence */
2007 0, /* tp_as_mapping */
2011 0, /* tp_getattro */
2012 0, /* tp_setattro */
2013 0, /* tp_as_buffer */
2014 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2015 PLy_plan_doc, /* tp_doc */
2018 static PyMethodDef PLy_plan_methods[] = {
2019 {"status", PLy_plan_status, METH_VARARGS, NULL},
2020 {NULL, NULL, 0, NULL}
2023 static PySequenceMethods PLy_result_as_sequence = {
2024 PLy_result_length, /* sq_length */
2025 NULL, /* sq_concat */
2026 NULL, /* sq_repeat */
2027 PLy_result_item, /* sq_item */
2028 PLy_result_slice, /* sq_slice */
2029 PLy_result_ass_item, /* sq_ass_item */
2030 PLy_result_ass_slice, /* sq_ass_slice */
2033 static PyTypeObject PLy_ResultType = {
2034 PyObject_HEAD_INIT(NULL)
2036 "PLyResult", /* tp_name */
2037 sizeof(PLyResultObject), /* tp_size */
2038 0, /* tp_itemsize */
2043 PLy_result_dealloc, /* tp_dealloc */
2045 PLy_result_getattr, /* tp_getattr */
2049 0, /* tp_as_number */
2050 &PLy_result_as_sequence, /* tp_as_sequence */
2051 0, /* tp_as_mapping */
2055 0, /* tp_getattro */
2056 0, /* tp_setattro */
2057 0, /* tp_as_buffer */
2058 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2059 PLy_result_doc, /* tp_doc */
2062 static PyMethodDef PLy_result_methods[] = {
2063 {"nrows", PLy_result_nrows, METH_VARARGS, NULL},
2064 {"status", PLy_result_status, METH_VARARGS, NULL},
2065 {NULL, NULL, 0, NULL}
2068 static PyMethodDef PLy_methods[] = {
2072 {"debug", PLy_debug, METH_VARARGS, NULL},
2073 {"log", PLy_log, METH_VARARGS, NULL},
2074 {"info", PLy_info, METH_VARARGS, NULL},
2075 {"notice", PLy_notice, METH_VARARGS, NULL},
2076 {"warning", PLy_warning, METH_VARARGS, NULL},
2077 {"error", PLy_error, METH_VARARGS, NULL},
2078 {"fatal", PLy_fatal, METH_VARARGS, NULL},
2081 * create a stored plan
2083 {"prepare", PLy_spi_prepare, METH_VARARGS, NULL},
2086 * execute a plan or query
2088 {"execute", PLy_spi_execute, METH_VARARGS, NULL},
2090 {NULL, NULL, 0, NULL}
2094 /* plan object methods */
2100 if ((ob = PyObject_NEW(PLyPlanObject, &PLy_PlanType)) == NULL)
2108 return (PyObject *) ob;
2113 PLy_plan_dealloc(PyObject * arg)
2115 PLyPlanObject *ob = (PLyPlanObject *) arg;
2118 SPI_freeplan(ob->plan);
2120 PLy_free(ob->types);
2125 for (i = 0; i < ob->nargs; i++)
2126 PLy_typeinfo_dealloc(&ob->args[i]);
2130 arg->ob_type->tp_free(arg);
2135 PLy_plan_getattr(PyObject * self, char *name)
2137 return Py_FindMethod(PLy_plan_methods, self, name);
2141 PLy_plan_status(PyObject * self, PyObject * args)
2143 if (PyArg_ParseTuple(args, ""))
2147 /* return PyInt_FromLong(self->status); */
2149 PyErr_SetString(PLy_exc_error, "plan.status() takes no arguments");
2155 /* result object methods */
2158 PLy_result_new(void)
2160 PLyResultObject *ob;
2162 if ((ob = PyObject_NEW(PLyResultObject, &PLy_ResultType)) == NULL)
2165 /* ob->tuples = NULL; */
2168 ob->status = Py_None;
2169 ob->nrows = PyInt_FromLong(-1);
2170 ob->rows = PyList_New(0);
2172 return (PyObject *) ob;
2176 PLy_result_dealloc(PyObject * arg)
2178 PLyResultObject *ob = (PLyResultObject *) arg;
2180 Py_XDECREF(ob->nrows);
2181 Py_XDECREF(ob->rows);
2182 Py_XDECREF(ob->status);
2184 arg->ob_type->tp_free(arg);
2188 PLy_result_getattr(PyObject * self, char *name)
2190 return Py_FindMethod(PLy_result_methods, self, name);
2194 PLy_result_nrows(PyObject * self, PyObject * args)
2196 PLyResultObject *ob = (PLyResultObject *) self;
2198 Py_INCREF(ob->nrows);
2203 PLy_result_status(PyObject * self, PyObject * args)
2205 PLyResultObject *ob = (PLyResultObject *) self;
2207 Py_INCREF(ob->status);
2212 PLy_result_length(PyObject * arg)
2214 PLyResultObject *ob = (PLyResultObject *) arg;
2216 return PyList_Size(ob->rows);
2220 PLy_result_item(PyObject * arg, Py_ssize_t idx)
2223 PLyResultObject *ob = (PLyResultObject *) arg;
2225 rv = PyList_GetItem(ob->rows, idx);
2232 PLy_result_ass_item(PyObject * arg, Py_ssize_t idx, PyObject * item)
2235 PLyResultObject *ob = (PLyResultObject *) arg;
2238 rv = PyList_SetItem(ob->rows, idx, item);
2243 PLy_result_slice(PyObject * arg, Py_ssize_t lidx, Py_ssize_t hidx)
2246 PLyResultObject *ob = (PLyResultObject *) arg;
2248 rv = PyList_GetSlice(ob->rows, lidx, hidx);
2256 PLy_result_ass_slice(PyObject * arg, Py_ssize_t lidx, Py_ssize_t hidx, PyObject * slice)
2259 PLyResultObject *ob = (PLyResultObject *) arg;
2261 rv = PyList_SetSlice(ob->rows, lidx, hidx, slice);
2267 PLy_spi_prepare(PyObject * self, PyObject * args)
2269 PLyPlanObject *plan;
2270 PyObject *list = NULL;
2271 PyObject *volatile optr = NULL;
2274 MemoryContext oldcontext;
2276 /* Can't execute more if we have an unhandled error */
2277 if (PLy_error_in_progress)
2279 PyErr_SetString(PLy_exc_error, "Transaction aborted.");
2283 if (!PyArg_ParseTuple(args, "s|O", &query, &list))
2285 PyErr_SetString(PLy_exc_spi_error,
2286 "Invalid arguments for plpy.prepare()");
2290 if (list && (!PySequence_Check(list)))
2292 PyErr_SetString(PLy_exc_spi_error,
2293 "Second argument in plpy.prepare() must be a sequence");
2297 if ((plan = (PLyPlanObject *) PLy_plan_new()) == NULL)
2300 oldcontext = CurrentMemoryContext;
2308 nargs = PySequence_Length(list);
2311 plan->nargs = nargs;
2312 plan->types = PLy_malloc(sizeof(Oid) * nargs);
2313 plan->values = PLy_malloc(sizeof(Datum) * nargs);
2314 plan->args = PLy_malloc(sizeof(PLyTypeInfo) * nargs);
2317 * the other loop might throw an exception, if PLyTypeInfo
2318 * member isn't properly initialized the Py_DECREF(plan) will
2321 for (i = 0; i < nargs; i++)
2323 PLy_typeinfo_init(&plan->args[i]);
2324 plan->values[i] = PointerGetDatum(NULL);
2327 for (i = 0; i < nargs; i++)
2333 Form_pg_type typeStruct;
2335 optr = PySequence_GetItem(list, i);
2336 if (!PyString_Check(optr))
2337 elog(ERROR, "Type names must be strings.");
2338 sptr = PyString_AsString(optr);
2340 /********************************************************
2341 * Resolve argument type names and then look them up by
2342 * oid in the system cache, and remember the required
2343 *information for input conversion.
2344 ********************************************************/
2346 parseTypeString(sptr, &typeId, &typmod);
2348 typeTup = SearchSysCache(TYPEOID,
2349 ObjectIdGetDatum(typeId),
2351 if (!HeapTupleIsValid(typeTup))
2352 elog(ERROR, "cache lookup failed for type %u", typeId);
2355 optr = NULL; /* this is important */
2357 plan->types[i] = typeId;
2358 typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
2359 if (typeStruct->typtype != TYPTYPE_COMPOSITE)
2360 PLy_output_datum_func(&plan->args[i], typeTup);
2362 elog(ERROR, "tuples not handled in plpy.prepare, yet.");
2363 ReleaseSysCache(typeTup);
2368 plan->plan = SPI_prepare(query, plan->nargs, plan->types);
2369 if (plan->plan == NULL)
2370 elog(ERROR, "SPI_prepare failed: %s",
2371 SPI_result_code_string(SPI_result));
2373 /* transfer plan from procCxt to topCxt */
2374 tmpplan = plan->plan;
2375 plan->plan = SPI_saveplan(tmpplan);
2376 SPI_freeplan(tmpplan);
2377 if (plan->plan == NULL)
2378 elog(ERROR, "SPI_saveplan failed: %s",
2379 SPI_result_code_string(SPI_result));
2383 MemoryContextSwitchTo(oldcontext);
2384 PLy_error_in_progress = CopyErrorData();
2388 if (!PyErr_Occurred())
2389 PyErr_SetString(PLy_exc_spi_error,
2390 "Unknown error in PLy_spi_prepare");
2391 /* XXX this oughta be replaced with errcontext mechanism */
2392 PLy_elog(WARNING, "in function %s:",
2393 PLy_procedure_name(PLy_curr_procedure));
2398 return (PyObject *) plan;
2401 /* execute(query="select * from foo", limit=5)
2402 * execute(plan=plan, values=(foo, bar), limit=5)
2405 PLy_spi_execute(PyObject * self, PyObject * args)
2409 PyObject *list = NULL;
2412 /* Can't execute more if we have an unhandled error */
2413 if (PLy_error_in_progress)
2415 PyErr_SetString(PLy_exc_error, "Transaction aborted.");
2419 if (PyArg_ParseTuple(args, "s|l", &query, &limit))
2420 return PLy_spi_execute_query(query, limit);
2424 if (PyArg_ParseTuple(args, "O|Ol", &plan, &list, &limit) &&
2425 is_PLyPlanObject(plan))
2426 return PLy_spi_execute_plan(plan, list, limit);
2428 PyErr_SetString(PLy_exc_error, "Expected a query or plan.");
2433 PLy_spi_execute_plan(PyObject * ob, PyObject * list, long limit)
2438 PLyPlanObject *plan;
2439 MemoryContext oldcontext;
2443 if (!PySequence_Check(list) || PyString_Check(list))
2445 char *msg = "plpy.execute() takes a sequence as its second argument";
2447 PyErr_SetString(PLy_exc_spi_error, msg);
2450 nargs = PySequence_Length(list);
2455 plan = (PLyPlanObject *) ob;
2457 if (nargs != plan->nargs)
2460 PyObject *so = PyObject_Str(list);
2463 PLy_elog(ERROR, "function \"%s\" could not execute plan",
2464 PLy_procedure_name(PLy_curr_procedure));
2465 sv = PyString_AsString(so);
2466 PLy_exception_set(PLy_exc_spi_error,
2467 "Expected sequence of %d arguments, got %d. %s",
2468 plan->nargs, nargs, sv);
2474 oldcontext = CurrentMemoryContext;
2477 char *nulls = palloc(nargs * sizeof(char));
2480 for (j = 0; j < nargs; j++)
2485 elem = PySequence_GetItem(list, j);
2486 if (elem != Py_None)
2488 so = PyObject_Str(elem);
2490 PLy_elog(ERROR, "function \"%s\" could not execute plan",
2491 PLy_procedure_name(PLy_curr_procedure));
2496 char *sv = PyString_AsString(so);
2499 InputFunctionCall(&(plan->args[j].out.d.typfunc),
2501 plan->args[j].out.d.typioparam,
2518 InputFunctionCall(&(plan->args[j].out.d.typfunc),
2520 plan->args[j].out.d.typioparam,
2526 rv = SPI_execute_plan(plan->plan, plan->values, nulls,
2527 PLy_curr_procedure->fn_readonly, limit);
2535 MemoryContextSwitchTo(oldcontext);
2536 PLy_error_in_progress = CopyErrorData();
2540 * cleanup plan->values array
2542 for (k = 0; k < nargs; k++)
2544 if (!plan->args[k].out.d.typbyval &&
2545 (plan->values[k] != PointerGetDatum(NULL)))
2547 pfree(DatumGetPointer(plan->values[k]));
2548 plan->values[k] = PointerGetDatum(NULL);
2552 if (!PyErr_Occurred())
2553 PyErr_SetString(PLy_exc_error,
2554 "Unknown error in PLy_spi_execute_plan");
2555 /* XXX this oughta be replaced with errcontext mechanism */
2556 PLy_elog(WARNING, "in function %s:",
2557 PLy_procedure_name(PLy_curr_procedure));
2562 for (i = 0; i < nargs; i++)
2564 if (!plan->args[i].out.d.typbyval &&
2565 (plan->values[i] != PointerGetDatum(NULL)))
2567 pfree(DatumGetPointer(plan->values[i]));
2568 plan->values[i] = PointerGetDatum(NULL);
2574 PLy_exception_set(PLy_exc_spi_error,
2575 "SPI_execute_plan failed: %s",
2576 SPI_result_code_string(rv));
2580 return PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
2584 PLy_spi_execute_query(char *query, long limit)
2587 MemoryContext oldcontext;
2589 oldcontext = CurrentMemoryContext;
2592 rv = SPI_execute(query, PLy_curr_procedure->fn_readonly, limit);
2596 MemoryContextSwitchTo(oldcontext);
2597 PLy_error_in_progress = CopyErrorData();
2599 if (!PyErr_Occurred())
2600 PyErr_SetString(PLy_exc_spi_error,
2601 "Unknown error in PLy_spi_execute_query");
2602 /* XXX this oughta be replaced with errcontext mechanism */
2603 PLy_elog(WARNING, "in function %s:",
2604 PLy_procedure_name(PLy_curr_procedure));
2611 PLy_exception_set(PLy_exc_spi_error,
2612 "SPI_execute failed: %s",
2613 SPI_result_code_string(rv));
2617 return PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
2621 PLy_spi_execute_fetch_result(SPITupleTable *tuptable, int rows, int status)
2623 PLyResultObject *result;
2624 MemoryContext oldcontext;
2626 result = (PLyResultObject *) PLy_result_new();
2627 Py_DECREF(result->status);
2628 result->status = PyInt_FromLong(status);
2630 if (status > 0 && tuptable == NULL)
2632 Py_DECREF(result->nrows);
2633 result->nrows = PyInt_FromLong(rows);
2635 else if (status > 0 && tuptable != NULL)
2640 Py_DECREF(result->nrows);
2641 result->nrows = PyInt_FromLong(rows);
2642 PLy_typeinfo_init(&args);
2644 oldcontext = CurrentMemoryContext;
2649 Py_DECREF(result->rows);
2650 result->rows = PyList_New(rows);
2652 PLy_input_tuple_funcs(&args, tuptable->tupdesc);
2653 for (i = 0; i < rows; i++)
2655 PyObject *row = PLyDict_FromTuple(&args, tuptable->vals[i],
2658 PyList_SetItem(result->rows, i, row);
2660 PLy_typeinfo_dealloc(&args);
2662 SPI_freetuptable(tuptable);
2667 MemoryContextSwitchTo(oldcontext);
2668 PLy_error_in_progress = CopyErrorData();
2670 if (!PyErr_Occurred())
2671 PyErr_SetString(PLy_exc_error,
2672 "Unknown error in PLy_spi_execute_fetch_result");
2674 PLy_typeinfo_dealloc(&args);
2680 return (PyObject *) result;
2685 * language handler and interpreter initialization
2689 * _PG_init() - library load-time initialization
2691 * DO NOT make this static nor change its name!
2696 /* Be sure we do initialization only once (should be redundant now) */
2697 static bool inited = false;
2705 if (PyErr_Occurred())
2706 PLy_elog(FATAL, "untrapped error in initialization");
2707 PLy_procedure_cache = PyDict_New();
2708 if (PLy_procedure_cache == NULL)
2709 PLy_elog(ERROR, "could not create procedure cache");
2715 PLy_init_interp(void)
2719 mainmod = PyImport_AddModule("__main__");
2720 if (mainmod == NULL || PyErr_Occurred())
2721 PLy_elog(ERROR, "could not import \"__main__\" module.");
2723 PLy_interp_globals = PyModule_GetDict(mainmod);
2724 PLy_interp_safe_globals = PyDict_New();
2725 PyDict_SetItemString(PLy_interp_globals, "GD", PLy_interp_safe_globals);
2727 if (PLy_interp_globals == NULL || PyErr_Occurred())
2728 PLy_elog(ERROR, "could not initialize globals");
2741 * initialize plpy module
2743 if (PyType_Ready(&PLy_PlanType) < 0)
2744 elog(ERROR, "could not init PLy_PlanType");
2745 if (PyType_Ready(&PLy_ResultType) < 0)
2746 elog(ERROR, "could not init PLy_ResultType");
2748 plpy = Py_InitModule("plpy", PLy_methods);
2749 plpy_dict = PyModule_GetDict(plpy);
2751 /* PyDict_SetItemString(plpy, "PlanType", (PyObject *) &PLy_PlanType); */
2753 PLy_exc_error = PyErr_NewException("plpy.Error", NULL, NULL);
2754 PLy_exc_fatal = PyErr_NewException("plpy.Fatal", NULL, NULL);
2755 PLy_exc_spi_error = PyErr_NewException("plpy.SPIError", NULL, NULL);
2756 PyDict_SetItemString(plpy_dict, "Error", PLy_exc_error);
2757 PyDict_SetItemString(plpy_dict, "Fatal", PLy_exc_fatal);
2758 PyDict_SetItemString(plpy_dict, "SPIError", PLy_exc_spi_error);
2761 * initialize main module, and add plpy
2763 main_mod = PyImport_AddModule("__main__");
2764 main_dict = PyModule_GetDict(main_mod);
2765 plpy_mod = PyImport_AddModule("plpy");
2766 PyDict_SetItemString(main_dict, "plpy", plpy_mod);
2767 if (PyErr_Occurred())
2768 elog(ERROR, "could not init plpy");
2771 /* the python interface to the elog function
2772 * don't confuse these with PLy_elog
2774 static PyObject *PLy_output(volatile int, PyObject *, PyObject *);
2777 PLy_debug(PyObject * self, PyObject * args)
2779 return PLy_output(DEBUG2, self, args);
2783 PLy_log(PyObject * self, PyObject * args)
2785 return PLy_output(LOG, self, args);
2789 PLy_info(PyObject * self, PyObject * args)
2791 return PLy_output(INFO, self, args);
2795 PLy_notice(PyObject * self, PyObject * args)
2797 return PLy_output(NOTICE, self, args);
2801 PLy_warning(PyObject * self, PyObject * args)
2803 return PLy_output(WARNING, self, args);
2807 PLy_error(PyObject * self, PyObject * args)
2809 return PLy_output(ERROR, self, args);
2813 PLy_fatal(PyObject * self, PyObject * args)
2815 return PLy_output(FATAL, self, args);
2820 PLy_output(volatile int level, PyObject * self, PyObject * args)
2824 MemoryContext oldcontext;
2826 so = PyObject_Str(args);
2827 if (so == NULL || ((sv = PyString_AsString(so)) == NULL))
2830 sv = "could not parse error message in `plpy.elog'";
2833 oldcontext = CurrentMemoryContext;
2836 elog(level, "%s", sv);
2840 MemoryContextSwitchTo(oldcontext);
2841 PLy_error_in_progress = CopyErrorData();
2846 * returning NULL here causes the python interpreter to bail. when
2847 * control passes back to PLy_procedure_call, we check for PG
2848 * exceptions and re-throw the error.
2850 PyErr_SetString(PLy_exc_error, sv);
2858 * return a legal object so the interpreter will continue on its merry way
2866 * Get the name of the last procedure called by the backend (the
2867 * innermost, if a plpython procedure call calls the backend and the
2868 * backend calls another plpython procedure).
2870 * NB: this returns the SQL name, not the internal Python procedure name
2873 PLy_procedure_name(PLyProcedure * proc)
2876 return "<unknown procedure>";
2877 return proc->proname;
2880 /* output a python traceback/exception via the postgresql elog
2881 * function. not pretty.
2884 PLy_exception_set(PyObject * exc, const char *fmt,...)
2890 vsnprintf(buf, sizeof(buf), fmt, ap);
2893 PyErr_SetString(exc, buf);
2896 /* Emit a PG error or notice, together with any available info about the
2897 * current Python error. This should be used to propagate Python errors
2901 PLy_elog(int elevel, const char *fmt,...)
2908 xmsg = PLy_traceback(&xlevel);
2911 emsg = PLy_vprintf(fmt, ap);
2917 (errmsg("plpython: %s", emsg),
2918 (xmsg) ? errdetail("%s", xmsg) : 0));
2935 PLy_traceback(int *xlevel)
2947 * get the current exception
2949 PyErr_Fetch(&e, &v, &tb);
2952 * oops, no exception, return
2960 PyErr_NormalizeException(&e, &v, &tb);
2963 eob = PyObject_Str(e);
2964 if (v && ((vob = PyObject_Str(v)) != NULL))
2965 vstr = PyString_AsString(vob);
2970 * I'm not sure what to do if eob is NULL here -- we can't call PLy_elog
2971 * because that function calls us, so we could end up with infinite
2972 * recursion. I'm not even sure if eob could be NULL here -- would an
2973 * Assert() be more appropriate?
2975 estr = eob ? PyString_AsString(eob) : "Unknown Exception";
2976 xstr = PLy_printf("%s: %s", estr, vstr);
2983 * intuit an appropriate error level based on the exception type
2985 if (PLy_exc_error && PyErr_GivenExceptionMatches(e, PLy_exc_error))
2987 else if (PLy_exc_fatal && PyErr_GivenExceptionMatches(e, PLy_exc_fatal))
2997 PLy_printf(const char *fmt,...)
3003 emsg = PLy_vprintf(fmt, ap);
3009 PLy_vprintf(const char *fmt, va_list ap)
3016 blen = strlen(fmt) * 2;
3019 buf = PLy_malloc(blen * sizeof(char));
3023 bchar = vsnprintf(buf, blen, fmt, ap);
3024 if (bchar > 0 && bchar < blen)
3032 buf = PLy_realloc(buf, blen);
3038 /* python module code */
3040 /* some dumb utility functions */
3042 PLy_malloc(size_t bytes)
3044 void *ptr = malloc(bytes);
3048 (errcode(ERRCODE_OUT_OF_MEMORY),
3049 errmsg("out of memory")));
3054 PLy_realloc(void *optr, size_t bytes)
3056 void *nptr = realloc(optr, bytes);
3060 (errcode(ERRCODE_OUT_OF_MEMORY),
3061 errmsg("out of memory")));
3066 PLy_strdup(const char *str)
3071 len = strlen(str) + 1;
3072 result = PLy_malloc(len);
3073 memcpy(result, str, len);
3078 /* define this away */