1 /**********************************************************************
2 * plpython.c - python as a procedural language for PostgreSQL
4 * $PostgreSQL: pgsql/src/pl/plpython/plpython.c,v 1.101 2007/05/31 15:13:05 petere 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;
31 #define PY_SSIZE_T_MAX INT_MAX
32 #define PY_SSIZE_T_MIN INT_MIN
36 * PyBool_FromLong is supported from 2.3.
38 #if PY_VERSION_HEX < 0x02030000
39 #define PyBool_FromLong(x) PyInt_FromLong(x)
49 /* postgreSQL stuff */
50 #include "access/heapam.h"
51 #include "catalog/pg_proc.h"
52 #include "catalog/pg_type.h"
53 #include "commands/trigger.h"
54 #include "executor/spi.h"
57 #include "nodes/makefuncs.h"
58 #include "parser/parse_type.h"
59 #include "tcop/tcopprot.h"
60 #include "utils/builtins.h"
61 #include "utils/lsyscache.h"
62 #include "utils/memutils.h"
63 #include "utils/syscache.h"
64 #include "utils/typcache.h"
71 /* convert Postgresql Datum or tuple into a PyObject.
72 * input to Python. Tuples are converted to dictionary
76 typedef PyObject *(*PLyDatumToObFunc) (const char *);
78 typedef struct PLyDatumToOb
80 PLyDatumToObFunc func;
86 typedef struct PLyTupleToOb
92 typedef union PLyTypeInput
98 /* convert PyObject to a Postgresql Datum or tuple.
101 typedef struct PLyObToDatum
103 FmgrInfo typfunc; /* The type's input function */
104 Oid typoid; /* The OID of the type */
109 typedef struct PLyObToTuple
115 typedef union PLyTypeOutput
121 /* all we need to move Postgresql data to Python objects,
124 typedef struct PLyTypeInfo
131 * is_rowtype can be: -1 not known yet (initial state) 0 scalar datatype
132 * 1 rowtype 2 rowtype, but I/O functions not set up yet
137 /* cached procedure data */
138 typedef struct PLyProcedure
140 char *proname; /* SQL name of procedure */
141 char *pyname; /* Python name of procedure */
142 TransactionId fn_xmin;
143 ItemPointerData fn_tid;
145 PLyTypeInfo result; /* also used to store info for trigger tuple
147 bool is_setof; /* true, if procedure returns result set */
148 PyObject *setof; /* contents of result set. */
149 char **argnames; /* Argument names */
150 PLyTypeInfo args[FUNC_MAX_ARGS];
152 PyObject *code; /* compiled procedure code */
153 PyObject *statics; /* data saved across calls, local scope */
154 PyObject *globals; /* data saved across calls, global scope */
155 PyObject *me; /* PyCObject containing pointer to this
161 typedef struct PLyPlanObject
164 void *plan; /* return of an SPI_saveplan */
171 typedef struct PLyResultObject
174 /* HeapTuple *tuples; */
175 PyObject * nrows; /* number of rows returned by query */
176 PyObject *rows; /* data rows, or None if no data returned */
177 PyObject *status; /* query status, SPI_OK_*, or SPI_ERR_* */
181 /* function declarations */
183 /* Two exported functions: first is the magic telling Postgresql
184 * what function call interface it implements. Second is for
185 * initialization of the interpreter during library load.
187 Datum plpython_call_handler(PG_FUNCTION_ARGS);
190 PG_FUNCTION_INFO_V1(plpython_call_handler);
192 /* most of the remaining of the declarations, all static */
194 /* these should only be called once at the first call
195 * of plpython_call_handler. initialize the python interpreter
198 static void PLy_init_interp(void);
199 static void PLy_init_plpy(void);
201 /* call PyErr_SetString with a vprint interface */
203 PLy_exception_set(PyObject *, const char *,...)
204 __attribute__((format(printf, 2, 3)));
206 /* Get the innermost python procedure called from the backend */
207 static char *PLy_procedure_name(PLyProcedure *);
209 /* some utility functions */
210 static void PLy_elog(int, const char *,...);
211 static char *PLy_traceback(int *);
212 static char *PLy_vprintf(const char *fmt, va_list ap);
213 static char *PLy_printf(const char *fmt,...);
215 static void *PLy_malloc(size_t);
216 static void *PLy_realloc(void *, size_t);
217 static char *PLy_strdup(const char *);
218 static void PLy_free(void *);
220 /* sub handlers for functions and triggers */
221 static Datum PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure *);
222 static HeapTuple PLy_trigger_handler(FunctionCallInfo fcinfo, PLyProcedure *);
224 static PyObject *PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *);
225 static void PLy_function_delete_args(PLyProcedure *);
226 static PyObject *PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *,
228 static HeapTuple PLy_modify_tuple(PLyProcedure *, PyObject *,
229 TriggerData *, HeapTuple);
231 static PyObject *PLy_procedure_call(PLyProcedure *, char *, PyObject *);
233 static PLyProcedure *PLy_procedure_get(FunctionCallInfo fcinfo,
236 static PLyProcedure *PLy_procedure_create(FunctionCallInfo fcinfo,
238 HeapTuple procTup, char *key);
240 static void PLy_procedure_compile(PLyProcedure *, const char *);
241 static char *PLy_procedure_munge_source(const char *, const char *);
242 static void PLy_procedure_delete(PLyProcedure *);
244 static void PLy_typeinfo_init(PLyTypeInfo *);
245 static void PLy_typeinfo_dealloc(PLyTypeInfo *);
246 static void PLy_output_datum_func(PLyTypeInfo *, HeapTuple);
247 static void PLy_output_datum_func2(PLyObToDatum *, HeapTuple);
248 static void PLy_input_datum_func(PLyTypeInfo *, Oid, HeapTuple);
249 static void PLy_input_datum_func2(PLyDatumToOb *, Oid, HeapTuple);
250 static void PLy_output_tuple_funcs(PLyTypeInfo *, TupleDesc);
251 static void PLy_input_tuple_funcs(PLyTypeInfo *, TupleDesc);
253 /* conversion functions */
254 static PyObject *PLyDict_FromTuple(PLyTypeInfo *, HeapTuple, TupleDesc);
255 static PyObject *PLyBool_FromString(const char *);
256 static PyObject *PLyFloat_FromString(const char *);
257 static PyObject *PLyInt_FromString(const char *);
258 static PyObject *PLyLong_FromString(const char *);
259 static PyObject *PLyString_FromString(const char *);
261 static HeapTuple PLyMapping_ToTuple(PLyTypeInfo *, PyObject *);
262 static HeapTuple PLySequence_ToTuple(PLyTypeInfo *, PyObject *);
263 static HeapTuple PLyObject_ToTuple(PLyTypeInfo *, PyObject *);
266 * Currently active plpython function
268 static PLyProcedure *PLy_curr_procedure = NULL;
271 * When a callback from Python into PG incurs an error, we temporarily store
272 * the error information here, and return NULL to the Python interpreter.
273 * Any further callback attempts immediately fail, and when the Python
274 * interpreter returns to the calling function, we re-throw the error (even if
275 * Python thinks it trapped the error and doesn't return NULL). Eventually
276 * this ought to be improved to let Python code really truly trap the error,
277 * but that's more of a change from the pre-8.0 semantics than I have time for
278 * now --- it will only be possible if the callback query is executed inside a
281 static ErrorData *PLy_error_in_progress = NULL;
283 static PyObject *PLy_interp_globals = NULL;
284 static PyObject *PLy_interp_safe_globals = NULL;
285 static PyObject *PLy_procedure_cache = NULL;
287 /* Python exceptions */
288 static PyObject *PLy_exc_error = NULL;
289 static PyObject *PLy_exc_fatal = NULL;
290 static PyObject *PLy_exc_spi_error = NULL;
292 /* some globals for the python module */
293 static char PLy_plan_doc[] = {
294 "Store a PostgreSQL plan"
297 static char PLy_result_doc[] = {
298 "Results of a PostgreSQL query"
303 * the function definitions
307 * This routine is a crock, and so is everyplace that calls it. The problem
308 * is that the cached form of plpython functions/queries is allocated permanently
309 * (mostly via malloc()) and never released until backend exit. Subsidiary
310 * data structures such as fmgr info records therefore must live forever
311 * as well. A better implementation would store all this stuff in a per-
312 * function memory context that could be reclaimed at need. In the meantime,
313 * fmgr_info_cxt must be called specifying TopMemoryContext so that whatever
314 * it might allocate, and whatever the eventual function might allocate using
315 * fn_mcxt, will live forever too.
318 perm_fmgr_info(Oid functionId, FmgrInfo *finfo)
320 fmgr_info_cxt(functionId, finfo, TopMemoryContext);
324 plpython_call_handler(PG_FUNCTION_ARGS)
327 PLyProcedure *save_curr_proc;
328 PLyProcedure *volatile proc = NULL;
330 if (SPI_connect() != SPI_OK_CONNECT)
331 elog(ERROR, "could not connect to SPI manager");
333 save_curr_proc = PLy_curr_procedure;
337 if (CALLED_AS_TRIGGER(fcinfo))
339 TriggerData *tdata = (TriggerData *) fcinfo->context;
342 proc = PLy_procedure_get(fcinfo,
343 RelationGetRelid(tdata->tg_relation));
344 PLy_curr_procedure = proc;
345 trv = PLy_trigger_handler(fcinfo, proc);
346 retval = PointerGetDatum(trv);
350 proc = PLy_procedure_get(fcinfo, InvalidOid);
351 PLy_curr_procedure = proc;
352 retval = PLy_function_handler(fcinfo, proc);
357 PLy_curr_procedure = save_curr_proc;
360 /* note: Py_DECREF needs braces around it, as of 2003/08 */
368 PLy_curr_procedure = save_curr_proc;
375 /* trigger and function sub handlers
377 * the python function is expected to return Py_None if the tuple is
378 * acceptable and unmodified. Otherwise it should return a PyString
379 * object who's value is SKIP, or MODIFY. SKIP means don't perform
380 * this action. MODIFY means the tuple has been modified, so update
381 * tuple and perform action. SKIP and MODIFY assume the trigger fires
382 * BEFORE the event and is ROW level. postgres expects the function
383 * to take no arguments and return an argument of type trigger.
386 PLy_trigger_handler(FunctionCallInfo fcinfo, PLyProcedure * proc)
389 PyObject *volatile plargs = NULL;
390 PyObject *volatile plrv = NULL;
394 plargs = PLy_trigger_build_args(fcinfo, proc, &rv);
395 plrv = PLy_procedure_call(proc, "TD", plargs);
397 Assert(plrv != NULL);
398 Assert(!PLy_error_in_progress);
401 * Disconnect from SPI manager
403 if (SPI_finish() != SPI_OK_FINISH)
404 elog(ERROR, "SPI_finish failed");
407 * return of None means we're happy with the tuple
413 if (!PyString_Check(plrv))
415 (errcode(ERRCODE_DATA_EXCEPTION),
416 errmsg("unexpected return value from trigger procedure"),
417 errdetail("Expected None or a String.")));
419 srv = PyString_AsString(plrv);
420 if (pg_strcasecmp(srv, "SKIP") == 0)
422 else if (pg_strcasecmp(srv, "MODIFY") == 0)
424 TriggerData *tdata = (TriggerData *) fcinfo->context;
426 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event) ||
427 TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
428 rv = PLy_modify_tuple(proc, plargs, tdata, rv);
430 elog(WARNING, "ignoring modified tuple in DELETE trigger");
432 else if (pg_strcasecmp(srv, "OK") != 0)
435 * accept "OK" as an alternative to None; otherwise, raise an
439 (errcode(ERRCODE_DATA_EXCEPTION),
440 errmsg("unexpected return value from trigger procedure"),
441 errdetail("Expected None, \"OK\", \"SKIP\", or \"MODIFY\".")));
461 PLy_modify_tuple(PLyProcedure * proc, PyObject * pltd, TriggerData *tdata,
464 PyObject *volatile plntup;
465 PyObject *volatile plkeys;
466 PyObject *volatile platt;
467 PyObject *volatile plval;
468 PyObject *volatile plstr;
474 int *volatile modattrs;
475 Datum *volatile modvalues;
476 char *volatile modnulls;
479 plntup = plkeys = platt = plval = plstr = NULL;
486 if ((plntup = PyDict_GetItemString(pltd, "new")) == NULL)
487 elog(ERROR, "TD[\"new\"] deleted, cannot modify tuple");
488 if (!PyDict_Check(plntup))
489 elog(ERROR, "TD[\"new\"] is not a dictionary object");
492 plkeys = PyDict_Keys(plntup);
493 natts = PyList_Size(plkeys);
495 modattrs = (int *) palloc(natts * sizeof(int));
496 modvalues = (Datum *) palloc(natts * sizeof(Datum));
497 modnulls = (char *) palloc(natts * sizeof(char));
499 tupdesc = tdata->tg_relation->rd_att;
501 for (i = 0; i < natts; i++)
505 platt = PyList_GetItem(plkeys, i);
506 if (!PyString_Check(platt))
507 elog(ERROR, "attribute name is not a string");
508 attn = SPI_fnumber(tupdesc, PyString_AsString(platt));
509 if (attn == SPI_ERROR_NOATTRIBUTE)
510 elog(ERROR, "invalid attribute \"%s\" in tuple",
511 PyString_AsString(platt));
514 plval = PyDict_GetItem(plntup, platt);
516 elog(FATAL, "python interpreter is probably corrupted");
522 if (tupdesc->attrs[atti]->attisdropped)
524 modvalues[i] = (Datum) 0;
527 else if (plval != Py_None)
529 plstr = PyObject_Str(plval);
531 PLy_elog(ERROR, "function \"%s\" could not modify tuple",
533 src = PyString_AsString(plstr);
536 InputFunctionCall(&proc->result.out.r.atts[atti].typfunc,
538 proc->result.out.r.atts[atti].typioparam,
539 tupdesc->attrs[atti]->atttypmod);
548 InputFunctionCall(&proc->result.out.r.atts[atti].typfunc,
550 proc->result.out.r.atts[atti].typioparam,
551 tupdesc->attrs[atti]->atttypmod);
559 rtup = SPI_modifytuple(tdata->tg_relation, otup, natts,
560 modattrs, modvalues, modnulls);
562 elog(ERROR, "SPI_modifytuple failed -- error %d", SPI_result);
593 PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure * proc, HeapTuple *rv)
595 TriggerData *tdata = (TriggerData *) fcinfo->context;
606 PyObject *volatile pltdata = NULL;
611 pltdata = PyDict_New();
613 PLy_elog(ERROR, "could not build arguments for trigger procedure");
615 pltname = PyString_FromString(tdata->tg_trigger->tgname);
616 PyDict_SetItemString(pltdata, "name", pltname);
619 stroid = DatumGetCString(DirectFunctionCall1(oidout,
620 ObjectIdGetDatum(tdata->tg_relation->rd_id)));
621 pltrelid = PyString_FromString(stroid);
622 PyDict_SetItemString(pltdata, "relid", pltrelid);
626 stroid = SPI_getrelname(tdata->tg_relation);
627 plttablename = PyString_FromString(stroid);
628 PyDict_SetItemString(pltdata, "table_name", plttablename);
629 Py_DECREF(plttablename);
632 stroid = SPI_getnspname(tdata->tg_relation);
633 plttableschema = PyString_FromString(stroid);
634 PyDict_SetItemString(pltdata, "table_schema", plttableschema);
635 Py_DECREF(plttableschema);
639 if (TRIGGER_FIRED_BEFORE(tdata->tg_event))
640 pltwhen = PyString_FromString("BEFORE");
641 else if (TRIGGER_FIRED_AFTER(tdata->tg_event))
642 pltwhen = PyString_FromString("AFTER");
645 elog(ERROR, "unrecognized WHEN tg_event: %u", tdata->tg_event);
646 pltwhen = NULL; /* keep compiler quiet */
648 PyDict_SetItemString(pltdata, "when", pltwhen);
651 if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
653 pltlevel = PyString_FromString("ROW");
654 PyDict_SetItemString(pltdata, "level", pltlevel);
657 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
659 pltevent = PyString_FromString("INSERT");
661 PyDict_SetItemString(pltdata, "old", Py_None);
662 pytnew = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
663 tdata->tg_relation->rd_att);
664 PyDict_SetItemString(pltdata, "new", pytnew);
666 *rv = tdata->tg_trigtuple;
668 else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
670 pltevent = PyString_FromString("DELETE");
672 PyDict_SetItemString(pltdata, "new", Py_None);
673 pytold = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
674 tdata->tg_relation->rd_att);
675 PyDict_SetItemString(pltdata, "old", pytold);
677 *rv = tdata->tg_trigtuple;
679 else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
681 pltevent = PyString_FromString("UPDATE");
683 pytnew = PLyDict_FromTuple(&(proc->result), tdata->tg_newtuple,
684 tdata->tg_relation->rd_att);
685 PyDict_SetItemString(pltdata, "new", pytnew);
687 pytold = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
688 tdata->tg_relation->rd_att);
689 PyDict_SetItemString(pltdata, "old", pytold);
691 *rv = tdata->tg_newtuple;
695 elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
696 pltevent = NULL; /* keep compiler quiet */
699 PyDict_SetItemString(pltdata, "event", pltevent);
702 else if (TRIGGER_FIRED_FOR_STATEMENT(tdata->tg_event))
704 pltlevel = PyString_FromString("STATEMENT");
705 PyDict_SetItemString(pltdata, "level", pltlevel);
708 PyDict_SetItemString(pltdata, "old", Py_None);
709 PyDict_SetItemString(pltdata, "new", Py_None);
712 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
713 pltevent = PyString_FromString("INSERT");
714 else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
715 pltevent = PyString_FromString("DELETE");
716 else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
717 pltevent = PyString_FromString("UPDATE");
720 elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
721 pltevent = NULL; /* keep compiler quiet */
724 PyDict_SetItemString(pltdata, "event", pltevent);
728 elog(ERROR, "unrecognized LEVEL tg_event: %u", tdata->tg_event);
730 if (tdata->tg_trigger->tgnargs)
738 pltargs = PyList_New(tdata->tg_trigger->tgnargs);
739 for (i = 0; i < tdata->tg_trigger->tgnargs; i++)
741 pltarg = PyString_FromString(tdata->tg_trigger->tgargs[i]);
744 * stolen, don't Py_DECREF
746 PyList_SetItem(pltargs, i, pltarg);
754 PyDict_SetItemString(pltdata, "args", pltargs);
769 /* function handler and friends */
771 PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure * proc)
774 PyObject *volatile plargs = NULL;
775 PyObject *volatile plrv = NULL;
776 PyObject *volatile plrv_so = NULL;
781 if (!proc->is_setof || proc->setof == NULL)
783 /* Simple type returning function or first time for SETOF function */
784 plargs = PLy_function_build_args(fcinfo, proc);
785 plrv = PLy_procedure_call(proc, "args", plargs);
789 * SETOF function parameters will be deleted when last row is
792 PLy_function_delete_args(proc);
793 Assert(plrv != NULL);
794 Assert(!PLy_error_in_progress);
798 * Disconnect from SPI manager and then create the return values datum
799 * (if the input function does a palloc for it this must not be
800 * allocated in the SPI memory context because SPI_finish would free
803 if (SPI_finish() != SPI_OK_FINISH)
804 elog(ERROR, "SPI_finish failed");
808 bool has_error = false;
809 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
811 if (proc->setof == NULL)
813 /* first time -- do checks and setup */
814 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
815 (rsi->allowedModes & SFRM_ValuePerCall) == 0)
818 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
819 errmsg("only value per call is allowed")));
821 rsi->returnMode = SFRM_ValuePerCall;
823 /* Make iterator out of returned object */
824 proc->setof = PyObject_GetIter(plrv);
828 if (proc->setof == NULL)
830 (errcode(ERRCODE_DATATYPE_MISMATCH),
831 errmsg("returned object cannot be iterated"),
832 errdetail("SETOF must be returned as iterable object")));
835 /* Fetch next from iterator */
836 plrv = PyIter_Next(proc->setof);
838 rsi->isDone = ExprMultipleResult;
841 rsi->isDone = ExprEndResult;
842 has_error = PyErr_Occurred() != NULL;
845 if (rsi->isDone == ExprEndResult)
847 /* Iterator is exhausted or error happened */
848 Py_DECREF(proc->setof);
855 PLy_function_delete_args(proc);
859 (errcode(ERRCODE_DATA_EXCEPTION),
860 errmsg("error fetching next item from iterator")));
862 fcinfo->isnull = true;
868 * If the function is declared to return void, the Python return value
869 * must be None. For void-returning functions, we also treat a None
870 * return value as a special "void datum" rather than NULL (as is the
871 * case for non-void-returning functions).
873 if (proc->result.out.d.typoid == VOIDOID)
877 (errcode(ERRCODE_DATATYPE_MISMATCH),
878 errmsg("invalid return value from plpython function"),
879 errdetail("Functions returning type \"void\" must return None.")));
881 fcinfo->isnull = false;
884 else if (plrv == Py_None)
886 fcinfo->isnull = true;
887 if (proc->result.is_rowtype < 1)
888 rv = InputFunctionCall(&proc->result.out.d.typfunc,
890 proc->result.out.d.typioparam,
896 else if (proc->result.is_rowtype >= 1)
898 HeapTuple tuple = NULL;
900 if (PySequence_Check(plrv))
901 /* composite type as sequence (tuple, list etc) */
902 tuple = PLySequence_ToTuple(&proc->result, plrv);
903 else if (PyMapping_Check(plrv))
904 /* composite type as mapping (currently only dict) */
905 tuple = PLyMapping_ToTuple(&proc->result, plrv);
907 /* returned as smth, must provide method __getattr__(name) */
908 tuple = PLyObject_ToTuple(&proc->result, plrv);
912 fcinfo->isnull = false;
913 rv = HeapTupleGetDatum(tuple);
917 fcinfo->isnull = true;
923 fcinfo->isnull = false;
924 plrv_so = PyObject_Str(plrv);
926 PLy_elog(ERROR, "function \"%s\" could not create return value", proc->proname);
927 plrv_sc = PyString_AsString(plrv_so);
928 rv = InputFunctionCall(&proc->result.out.d.typfunc,
930 proc->result.out.d.typioparam,
952 PLy_procedure_call(PLyProcedure * proc, char *kargs, PyObject * vargs)
956 PyDict_SetItemString(proc->globals, kargs, vargs);
957 rv = PyEval_EvalCode((PyCodeObject *) proc->code,
958 proc->globals, proc->globals);
961 * If there was an error in a PG callback, propagate that no matter what
962 * Python claims about its success.
964 if (PLy_error_in_progress)
966 ErrorData *edata = PLy_error_in_progress;
968 PLy_error_in_progress = NULL;
972 if (rv == NULL || PyErr_Occurred())
975 PLy_elog(ERROR, "function \"%s\" failed", proc->proname);
982 PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure * proc)
984 PyObject *volatile arg = NULL;
985 PyObject *volatile args = NULL;
990 args = PyList_New(proc->nargs);
991 for (i = 0; i < proc->nargs; i++)
993 if (proc->args[i].is_rowtype > 0)
995 if (fcinfo->argnull[i])
1003 HeapTupleData tmptup;
1005 td = DatumGetHeapTupleHeader(fcinfo->arg[i]);
1006 /* Extract rowtype info and find a tupdesc */
1007 tupType = HeapTupleHeaderGetTypeId(td);
1008 tupTypmod = HeapTupleHeaderGetTypMod(td);
1009 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
1011 /* Set up I/O funcs if not done yet */
1012 if (proc->args[i].is_rowtype != 1)
1013 PLy_input_tuple_funcs(&(proc->args[i]), tupdesc);
1015 /* Build a temporary HeapTuple control structure */
1016 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
1019 arg = PLyDict_FromTuple(&(proc->args[i]), &tmptup, tupdesc);
1020 ReleaseTupleDesc(tupdesc);
1025 if (fcinfo->argnull[i])
1031 ct = OutputFunctionCall(&(proc->args[i].in.d.typfunc),
1033 arg = (proc->args[i].in.d.func) (ct);
1044 if (PyList_SetItem(args, i, arg) == -1 ||
1046 PyDict_SetItemString(proc->globals, proc->argnames[i], arg) == -1))
1047 PLy_elog(ERROR, "problem setting up arguments for \"%s\"", proc->proname);
1065 PLy_function_delete_args(PLyProcedure * proc)
1069 if (!proc->argnames)
1072 for (i = 0; i < proc->nargs; i++)
1073 PyDict_DelItemString(proc->globals, proc->argnames[i]);
1078 * PLyProcedure functions
1081 /* PLy_procedure_get: returns a cached PLyProcedure, or creates, stores and
1082 * returns a new PLyProcedure. fcinfo is the call info, tgreloid is the
1083 * relation OID when calling a trigger, or InvalidOid (zero) for ordinary
1086 static PLyProcedure *
1087 PLy_procedure_get(FunctionCallInfo fcinfo, Oid tgreloid)
1093 PLyProcedure *proc = NULL;
1096 fn_oid = fcinfo->flinfo->fn_oid;
1097 procTup = SearchSysCache(PROCOID,
1098 ObjectIdGetDatum(fn_oid),
1100 if (!HeapTupleIsValid(procTup))
1101 elog(ERROR, "cache lookup failed for function %u", fn_oid);
1103 rv = snprintf(key, sizeof(key), "%u_%u", fn_oid, tgreloid);
1104 if (rv >= sizeof(key) || rv < 0)
1105 elog(ERROR, "key too long");
1107 plproc = PyDict_GetItemString(PLy_procedure_cache, key);
1112 if (!PyCObject_Check(plproc))
1113 elog(FATAL, "expected a PyCObject, didn't get one");
1115 proc = PyCObject_AsVoidPtr(plproc);
1116 if (proc->me != plproc)
1117 elog(FATAL, "proc->me != plproc");
1118 /* did we find an up-to-date cache entry? */
1119 if (proc->fn_xmin != HeapTupleHeaderGetXmin(procTup->t_data) ||
1120 !ItemPointerEquals(&proc->fn_tid, &procTup->t_self))
1128 proc = PLy_procedure_create(fcinfo, tgreloid, procTup, key);
1130 ReleaseSysCache(procTup);
1135 static PLyProcedure *
1136 PLy_procedure_create(FunctionCallInfo fcinfo, Oid tgreloid,
1137 HeapTuple procTup, char *key)
1139 char procName[NAMEDATALEN + 256];
1140 Form_pg_proc procStruct;
1141 PLyProcedure *volatile proc;
1142 char *volatile procSource = NULL;
1151 procStruct = (Form_pg_proc) GETSTRUCT(procTup);
1153 if (OidIsValid(tgreloid))
1154 rv = snprintf(procName, sizeof(procName),
1155 "__plpython_procedure_%s_%u_trigger_%u",
1156 NameStr(procStruct->proname),
1157 fcinfo->flinfo->fn_oid,
1160 rv = snprintf(procName, sizeof(procName),
1161 "__plpython_procedure_%s_%u",
1162 NameStr(procStruct->proname),
1163 fcinfo->flinfo->fn_oid);
1164 if (rv >= sizeof(procName) || rv < 0)
1165 elog(ERROR, "procedure name would overrun buffer");
1167 proc = PLy_malloc(sizeof(PLyProcedure));
1168 proc->proname = PLy_strdup(NameStr(procStruct->proname));
1169 proc->pyname = PLy_strdup(procName);
1170 proc->fn_xmin = HeapTupleHeaderGetXmin(procTup->t_data);
1171 proc->fn_tid = procTup->t_self;
1172 /* Remember if function is STABLE/IMMUTABLE */
1174 (procStruct->provolatile != PROVOLATILE_VOLATILE);
1175 PLy_typeinfo_init(&proc->result);
1176 for (i = 0; i < FUNC_MAX_ARGS; i++)
1177 PLy_typeinfo_init(&proc->args[i]);
1179 proc->code = proc->statics = NULL;
1180 proc->globals = proc->me = NULL;
1181 proc->is_setof = procStruct->proretset;
1183 proc->argnames = NULL;
1188 * get information required for output conversion of the return value,
1189 * but only if this isn't a trigger.
1191 if (!CALLED_AS_TRIGGER(fcinfo))
1193 HeapTuple rvTypeTup;
1194 Form_pg_type rvTypeStruct;
1196 rvTypeTup = SearchSysCache(TYPEOID,
1197 ObjectIdGetDatum(procStruct->prorettype),
1199 if (!HeapTupleIsValid(rvTypeTup))
1200 elog(ERROR, "cache lookup failed for type %u",
1201 procStruct->prorettype);
1202 rvTypeStruct = (Form_pg_type) GETSTRUCT(rvTypeTup);
1204 /* Disallow pseudotype result, except for void */
1205 if (rvTypeStruct->typtype == TYPTYPE_PSEUDO &&
1206 procStruct->prorettype != VOIDOID)
1208 if (procStruct->prorettype == TRIGGEROID)
1210 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1211 errmsg("trigger functions can only be called as triggers")));
1214 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1215 errmsg("plpython functions cannot return type %s",
1216 format_type_be(procStruct->prorettype))));
1219 if (rvTypeStruct->typtype == TYPTYPE_COMPOSITE)
1222 * Tuple: set up later, during first call to
1223 * PLy_function_handler
1225 proc->result.out.d.typoid = procStruct->prorettype;
1226 proc->result.is_rowtype = 2;
1229 PLy_output_datum_func(&proc->result, rvTypeTup);
1231 ReleaseSysCache(rvTypeTup);
1236 * input/output conversion for trigger tuples. use the result
1237 * TypeInfo variable to store the tuple conversion info.
1239 TriggerData *tdata = (TriggerData *) fcinfo->context;
1241 PLy_input_tuple_funcs(&(proc->result), tdata->tg_relation->rd_att);
1242 PLy_output_tuple_funcs(&(proc->result), tdata->tg_relation->rd_att);
1246 * now get information required for input conversion of the
1247 * procedure's arguments.
1249 proc->nargs = fcinfo->nargs;
1252 argnames = SysCacheGetAttr(PROCOID, procTup, Anum_pg_proc_proargnames, &isnull);
1255 deconstruct_array(DatumGetArrayTypeP(argnames), TEXTOID, -1, false, 'i',
1256 &elems, NULL, &nelems);
1257 if (nelems != proc->nargs)
1259 "proargnames must have the same number of elements "
1260 "as the function has arguments");
1261 proc->argnames = (char **) PLy_malloc(sizeof(char *) * proc->nargs);
1264 for (i = 0; i < fcinfo->nargs; i++)
1266 HeapTuple argTypeTup;
1267 Form_pg_type argTypeStruct;
1269 argTypeTup = SearchSysCache(TYPEOID,
1270 ObjectIdGetDatum(procStruct->proargtypes.values[i]),
1272 if (!HeapTupleIsValid(argTypeTup))
1273 elog(ERROR, "cache lookup failed for type %u",
1274 procStruct->proargtypes.values[i]);
1275 argTypeStruct = (Form_pg_type) GETSTRUCT(argTypeTup);
1277 /* Disallow pseudotype argument */
1278 if (argTypeStruct->typtype == TYPTYPE_PSEUDO)
1280 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1281 errmsg("plpython functions cannot take type %s",
1282 format_type_be(procStruct->proargtypes.values[i]))));
1284 if (argTypeStruct->typtype != TYPTYPE_COMPOSITE)
1285 PLy_input_datum_func(&(proc->args[i]),
1286 procStruct->proargtypes.values[i],
1289 proc->args[i].is_rowtype = 2; /* still need to set I/O funcs */
1291 ReleaseSysCache(argTypeTup);
1293 /* Fetch argument name */
1295 proc->argnames[i] = PLy_strdup(DatumGetCString(DirectFunctionCall1(textout, elems[i])));
1299 * get the text of the function.
1301 prosrcdatum = SysCacheGetAttr(PROCOID, procTup,
1302 Anum_pg_proc_prosrc, &isnull);
1304 elog(ERROR, "null prosrc");
1305 procSource = DatumGetCString(DirectFunctionCall1(textout,
1308 PLy_procedure_compile(proc, procSource);
1312 proc->me = PyCObject_FromVoidPtr(proc, NULL);
1313 PyDict_SetItemString(PLy_procedure_cache, key, proc->me);
1317 PLy_procedure_delete(proc);
1329 PLy_procedure_compile(PLyProcedure * proc, const char *src)
1331 PyObject *crv = NULL;
1334 proc->globals = PyDict_Copy(PLy_interp_globals);
1337 * SD is private preserved data between calls. GD is global data shared by
1340 proc->statics = PyDict_New();
1341 PyDict_SetItemString(proc->globals, "SD", proc->statics);
1344 * insert the function code into the interpreter
1346 msrc = PLy_procedure_munge_source(proc->pyname, src);
1347 crv = PyRun_String(msrc, Py_file_input, proc->globals, NULL);
1350 if (crv != NULL && (!PyErr_Occurred()))
1353 char call[NAMEDATALEN + 256];
1358 * compile a call to the function
1360 clen = snprintf(call, sizeof(call), "%s()", proc->pyname);
1361 if (clen < 0 || clen >= sizeof(call))
1362 elog(ERROR, "string would overflow buffer");
1363 proc->code = Py_CompileString(call, "<string>", Py_eval_input);
1364 if (proc->code != NULL && (!PyErr_Occurred()))
1370 PLy_elog(ERROR, "could not compile function \"%s\"", proc->proname);
1374 PLy_procedure_munge_source(const char *name, const char *src)
1383 * room for function source and the def statement
1385 mlen = (strlen(src) * 2) + strlen(name) + 16;
1387 mrc = PLy_malloc(mlen);
1388 plen = snprintf(mrc, mlen, "def %s():\n\t", name);
1389 Assert(plen >= 0 && plen < mlen);
1396 if (*sp == '\r' && *(sp + 1) == '\n')
1399 if (*sp == '\n' || *sp == '\r')
1412 if (mp > (mrc + mlen))
1413 elog(FATAL, "buffer overrun in PLy_munge_source");
1419 PLy_procedure_delete(PLyProcedure * proc)
1423 Py_XDECREF(proc->code);
1424 Py_XDECREF(proc->statics);
1425 Py_XDECREF(proc->globals);
1426 Py_XDECREF(proc->me);
1428 PLy_free(proc->proname);
1430 PLy_free(proc->pyname);
1431 for (i = 0; i < proc->nargs; i++)
1433 if (proc->args[i].is_rowtype == 1)
1435 if (proc->args[i].in.r.atts)
1436 PLy_free(proc->args[i].in.r.atts);
1437 if (proc->args[i].out.r.atts)
1438 PLy_free(proc->args[i].out.r.atts);
1440 if (proc->argnames && proc->argnames[i])
1441 PLy_free(proc->argnames[i]);
1444 PLy_free(proc->argnames);
1447 /* conversion functions. remember output from python is
1448 * input to postgresql, and vis versa.
1451 PLy_input_tuple_funcs(PLyTypeInfo * arg, TupleDesc desc)
1455 if (arg->is_rowtype == 0)
1456 elog(ERROR, "PLyTypeInfo struct is initialized for a Datum");
1458 arg->is_rowtype = 1;
1459 arg->in.r.natts = desc->natts;
1460 arg->in.r.atts = PLy_malloc(desc->natts * sizeof(PLyDatumToOb));
1462 for (i = 0; i < desc->natts; i++)
1466 if (desc->attrs[i]->attisdropped)
1469 typeTup = SearchSysCache(TYPEOID,
1470 ObjectIdGetDatum(desc->attrs[i]->atttypid),
1472 if (!HeapTupleIsValid(typeTup))
1473 elog(ERROR, "cache lookup failed for type %u",
1474 desc->attrs[i]->atttypid);
1476 PLy_input_datum_func2(&(arg->in.r.atts[i]),
1477 desc->attrs[i]->atttypid,
1480 ReleaseSysCache(typeTup);
1485 PLy_output_tuple_funcs(PLyTypeInfo * arg, TupleDesc desc)
1489 if (arg->is_rowtype == 0)
1490 elog(ERROR, "PLyTypeInfo struct is initialized for a Datum");
1492 arg->is_rowtype = 1;
1493 arg->out.r.natts = desc->natts;
1494 arg->out.r.atts = PLy_malloc(desc->natts * sizeof(PLyDatumToOb));
1496 for (i = 0; i < desc->natts; i++)
1500 if (desc->attrs[i]->attisdropped)
1503 typeTup = SearchSysCache(TYPEOID,
1504 ObjectIdGetDatum(desc->attrs[i]->atttypid),
1506 if (!HeapTupleIsValid(typeTup))
1507 elog(ERROR, "cache lookup failed for type %u",
1508 desc->attrs[i]->atttypid);
1510 PLy_output_datum_func2(&(arg->out.r.atts[i]), typeTup);
1512 ReleaseSysCache(typeTup);
1517 PLy_output_datum_func(PLyTypeInfo * arg, HeapTuple typeTup)
1519 if (arg->is_rowtype > 0)
1520 elog(ERROR, "PLyTypeInfo struct is initialized for a Tuple");
1521 arg->is_rowtype = 0;
1522 PLy_output_datum_func2(&(arg->out.d), typeTup);
1526 PLy_output_datum_func2(PLyObToDatum * arg, HeapTuple typeTup)
1528 Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1530 perm_fmgr_info(typeStruct->typinput, &arg->typfunc);
1531 arg->typoid = HeapTupleGetOid(typeTup);
1532 arg->typioparam = getTypeIOParam(typeTup);
1533 arg->typbyval = typeStruct->typbyval;
1537 PLy_input_datum_func(PLyTypeInfo * arg, Oid typeOid, HeapTuple typeTup)
1539 if (arg->is_rowtype > 0)
1540 elog(ERROR, "PLyTypeInfo struct is initialized for Tuple");
1541 arg->is_rowtype = 0;
1542 PLy_input_datum_func2(&(arg->in.d), typeOid, typeTup);
1546 PLy_input_datum_func2(PLyDatumToOb * arg, Oid typeOid, HeapTuple typeTup)
1548 Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1550 /* Get the type's conversion information */
1551 perm_fmgr_info(typeStruct->typoutput, &arg->typfunc);
1552 arg->typioparam = getTypeIOParam(typeTup);
1553 arg->typbyval = typeStruct->typbyval;
1555 /* Determine which kind of Python object we will convert to */
1559 arg->func = PLyBool_FromString;
1564 arg->func = PLyFloat_FromString;
1568 arg->func = PLyInt_FromString;
1571 arg->func = PLyLong_FromString;
1574 arg->func = PLyString_FromString;
1580 PLy_typeinfo_init(PLyTypeInfo * arg)
1582 arg->is_rowtype = -1;
1583 arg->in.r.natts = arg->out.r.natts = 0;
1584 arg->in.r.atts = NULL;
1585 arg->out.r.atts = NULL;
1589 PLy_typeinfo_dealloc(PLyTypeInfo * arg)
1591 if (arg->is_rowtype == 1)
1594 PLy_free(arg->in.r.atts);
1595 if (arg->out.r.atts)
1596 PLy_free(arg->out.r.atts);
1600 /* assumes that a bool is always returned as a 't' or 'f' */
1602 PLyBool_FromString(const char *src)
1605 * We would like to use Py_RETURN_TRUE and Py_RETURN_FALSE here for
1606 * generating SQL from trigger functions, but those are only
1607 * supported in Python >= 2.3, and we support older
1608 * versions. http://docs.python.org/api/boolObjects.html
1611 return PyBool_FromLong(1);
1612 return PyBool_FromLong(0);
1616 PLyFloat_FromString(const char *src)
1622 v = strtod(src, &eptr);
1623 if (*eptr != '\0' || errno)
1625 return PyFloat_FromDouble(v);
1629 PLyInt_FromString(const char *src)
1635 v = strtol(src, &eptr, 0);
1636 if (*eptr != '\0' || errno)
1638 return PyInt_FromLong(v);
1642 PLyLong_FromString(const char *src)
1644 return PyLong_FromString((char *) src, NULL, 0);
1648 PLyString_FromString(const char *src)
1650 return PyString_FromString(src);
1654 PLyDict_FromTuple(PLyTypeInfo * info, HeapTuple tuple, TupleDesc desc)
1656 PyObject *volatile dict;
1659 if (info->is_rowtype != 1)
1660 elog(ERROR, "PLyTypeInfo structure describes a datum");
1662 dict = PyDict_New();
1664 PLy_elog(ERROR, "could not create tuple dictionary");
1668 for (i = 0; i < info->in.r.natts; i++)
1676 if (desc->attrs[i]->attisdropped)
1679 key = NameStr(desc->attrs[i]->attname);
1680 vattr = heap_getattr(tuple, (i + 1), desc, &is_null);
1682 if (is_null || info->in.r.atts[i].func == NULL)
1683 PyDict_SetItemString(dict, key, Py_None);
1686 vsrc = OutputFunctionCall(&info->in.r.atts[i].typfunc,
1690 * no exceptions allowed
1692 value = info->in.r.atts[i].func(vsrc);
1694 PyDict_SetItemString(dict, key, value);
1711 PLyMapping_ToTuple(PLyTypeInfo * info, PyObject * mapping)
1719 Assert(PyMapping_Check(mapping));
1721 desc = lookup_rowtype_tupdesc(info->out.d.typoid, -1);
1722 if (info->is_rowtype == 2)
1723 PLy_output_tuple_funcs(info, desc);
1724 Assert(info->is_rowtype == 1);
1727 values = palloc(sizeof(Datum) * desc->natts);
1728 nulls = palloc(sizeof(char) * desc->natts);
1729 for (i = 0; i < desc->natts; ++i)
1735 key = NameStr(desc->attrs[i]->attname);
1739 value = PyMapping_GetItemString(mapping, key);
1740 if (value == Py_None)
1742 values[i] = (Datum) NULL;
1749 so = PyObject_Str(value);
1751 PLy_elog(ERROR, "cannot convert mapping type");
1752 valuestr = PyString_AsString(so);
1754 values[i] = InputFunctionCall(&info->out.r.atts[i].typfunc
1756 ,info->out.r.atts[i].typioparam
1764 (errcode(ERRCODE_UNDEFINED_COLUMN),
1765 errmsg("no mapping found with key \"%s\"", key),
1766 errhint("to return null in specific column, "
1767 "add value None to map with key named after column")));
1781 tuple = heap_formtuple(desc, values, nulls);
1782 ReleaseTupleDesc(desc);
1791 PLySequence_ToTuple(PLyTypeInfo * info, PyObject * sequence)
1799 Assert(PySequence_Check(sequence));
1802 * Check that sequence length is exactly same as PG tuple's. We actually
1803 * can ignore exceeding items or assume missing ones as null but to avoid
1804 * plpython developer's errors we are strict here
1806 desc = lookup_rowtype_tupdesc(info->out.d.typoid, -1);
1807 if (PySequence_Length(sequence) != desc->natts)
1809 (errcode(ERRCODE_DATATYPE_MISMATCH),
1810 errmsg("returned sequence's length must be same as tuple's length")));
1812 if (info->is_rowtype == 2)
1813 PLy_output_tuple_funcs(info, desc);
1814 Assert(info->is_rowtype == 1);
1817 values = palloc(sizeof(Datum) * desc->natts);
1818 nulls = palloc(sizeof(char) * desc->natts);
1819 for (i = 0; i < desc->natts; ++i)
1827 value = PySequence_GetItem(sequence, i);
1829 if (value == Py_None)
1831 values[i] = (Datum) NULL;
1838 so = PyObject_Str(value);
1840 PLy_elog(ERROR, "cannot convert sequence type");
1841 valuestr = PyString_AsString(so);
1842 values[i] = InputFunctionCall(&info->out.r.atts[i].typfunc
1844 ,info->out.r.atts[i].typioparam
1863 tuple = heap_formtuple(desc, values, nulls);
1864 ReleaseTupleDesc(desc);
1873 PLyObject_ToTuple(PLyTypeInfo * info, PyObject * object)
1881 desc = lookup_rowtype_tupdesc(info->out.d.typoid, -1);
1882 if (info->is_rowtype == 2)
1883 PLy_output_tuple_funcs(info, desc);
1884 Assert(info->is_rowtype == 1);
1887 values = palloc(sizeof(Datum) * desc->natts);
1888 nulls = palloc(sizeof(char) * desc->natts);
1889 for (i = 0; i < desc->natts; ++i)
1895 key = NameStr(desc->attrs[i]->attname);
1899 value = PyObject_GetAttrString(object, key);
1900 if (value == Py_None)
1902 values[i] = (Datum) NULL;
1909 so = PyObject_Str(value);
1911 PLy_elog(ERROR, "cannot convert object type");
1912 valuestr = PyString_AsString(so);
1913 values[i] = InputFunctionCall(&info->out.r.atts[i].typfunc
1915 ,info->out.r.atts[i].typioparam
1923 (errcode(ERRCODE_UNDEFINED_COLUMN),
1924 errmsg("no attribute named \"%s\"", key),
1925 errhint("to return null in specific column, "
1926 "let returned object to have attribute named "
1927 "after column with value None")));
1941 tuple = heap_formtuple(desc, values, nulls);
1942 ReleaseTupleDesc(desc);
1950 /* initialization, some python variables function declared here */
1952 /* interface to postgresql elog */
1953 static PyObject *PLy_debug(PyObject *, PyObject *);
1954 static PyObject *PLy_log(PyObject *, PyObject *);
1955 static PyObject *PLy_info(PyObject *, PyObject *);
1956 static PyObject *PLy_notice(PyObject *, PyObject *);
1957 static PyObject *PLy_warning(PyObject *, PyObject *);
1958 static PyObject *PLy_error(PyObject *, PyObject *);
1959 static PyObject *PLy_fatal(PyObject *, PyObject *);
1961 /* PLyPlanObject, PLyResultObject and SPI interface */
1962 #define is_PLyPlanObject(x) ((x)->ob_type == &PLy_PlanType)
1963 static PyObject *PLy_plan_new(void);
1964 static void PLy_plan_dealloc(PyObject *);
1965 static PyObject *PLy_plan_getattr(PyObject *, char *);
1966 static PyObject *PLy_plan_status(PyObject *, PyObject *);
1968 static PyObject *PLy_result_new(void);
1969 static void PLy_result_dealloc(PyObject *);
1970 static PyObject *PLy_result_getattr(PyObject *, char *);
1971 static PyObject *PLy_result_nrows(PyObject *, PyObject *);
1972 static PyObject *PLy_result_status(PyObject *, PyObject *);
1973 static Py_ssize_t PLy_result_length(PyObject *);
1974 static PyObject *PLy_result_item(PyObject *, Py_ssize_t);
1975 static PyObject *PLy_result_slice(PyObject *, Py_ssize_t, Py_ssize_t);
1976 static int PLy_result_ass_item(PyObject *, Py_ssize_t, PyObject *);
1977 static int PLy_result_ass_slice(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
1980 static PyObject *PLy_spi_prepare(PyObject *, PyObject *);
1981 static PyObject *PLy_spi_execute(PyObject *, PyObject *);
1982 static PyObject *PLy_spi_execute_query(char *query, long limit);
1983 static PyObject *PLy_spi_execute_plan(PyObject *, PyObject *, long);
1984 static PyObject *PLy_spi_execute_fetch_result(SPITupleTable *, int, int);
1987 static PyTypeObject PLy_PlanType = {
1988 PyObject_HEAD_INIT(NULL)
1990 "PLyPlan", /* tp_name */
1991 sizeof(PLyPlanObject), /* tp_size */
1992 0, /* tp_itemsize */
1997 PLy_plan_dealloc, /* tp_dealloc */
1999 PLy_plan_getattr, /* tp_getattr */
2003 0, /* tp_as_number */
2004 0, /* tp_as_sequence */
2005 0, /* tp_as_mapping */
2009 0, /* tp_getattro */
2010 0, /* tp_setattro */
2011 0, /* tp_as_buffer */
2012 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2013 PLy_plan_doc, /* tp_doc */
2016 static PyMethodDef PLy_plan_methods[] = {
2017 {"status", PLy_plan_status, METH_VARARGS, NULL},
2018 {NULL, NULL, 0, NULL}
2021 static PySequenceMethods PLy_result_as_sequence = {
2022 PLy_result_length, /* sq_length */
2023 NULL, /* sq_concat */
2024 NULL, /* sq_repeat */
2025 PLy_result_item, /* sq_item */
2026 PLy_result_slice, /* sq_slice */
2027 PLy_result_ass_item, /* sq_ass_item */
2028 PLy_result_ass_slice, /* sq_ass_slice */
2031 static PyTypeObject PLy_ResultType = {
2032 PyObject_HEAD_INIT(NULL)
2034 "PLyResult", /* tp_name */
2035 sizeof(PLyResultObject), /* tp_size */
2036 0, /* tp_itemsize */
2041 PLy_result_dealloc, /* tp_dealloc */
2043 PLy_result_getattr, /* tp_getattr */
2047 0, /* tp_as_number */
2048 &PLy_result_as_sequence, /* tp_as_sequence */
2049 0, /* tp_as_mapping */
2053 0, /* tp_getattro */
2054 0, /* tp_setattro */
2055 0, /* tp_as_buffer */
2056 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2057 PLy_result_doc, /* tp_doc */
2060 static PyMethodDef PLy_result_methods[] = {
2061 {"nrows", PLy_result_nrows, METH_VARARGS, NULL},
2062 {"status", PLy_result_status, METH_VARARGS, NULL},
2063 {NULL, NULL, 0, NULL}
2066 static PyMethodDef PLy_methods[] = {
2070 {"debug", PLy_debug, METH_VARARGS, NULL},
2071 {"log", PLy_log, METH_VARARGS, NULL},
2072 {"info", PLy_info, METH_VARARGS, NULL},
2073 {"notice", PLy_notice, METH_VARARGS, NULL},
2074 {"warning", PLy_warning, METH_VARARGS, NULL},
2075 {"error", PLy_error, METH_VARARGS, NULL},
2076 {"fatal", PLy_fatal, METH_VARARGS, NULL},
2079 * create a stored plan
2081 {"prepare", PLy_spi_prepare, METH_VARARGS, NULL},
2084 * execute a plan or query
2086 {"execute", PLy_spi_execute, METH_VARARGS, NULL},
2088 {NULL, NULL, 0, NULL}
2092 /* plan object methods */
2098 if ((ob = PyObject_NEW(PLyPlanObject, &PLy_PlanType)) == NULL)
2106 return (PyObject *) ob;
2111 PLy_plan_dealloc(PyObject * arg)
2113 PLyPlanObject *ob = (PLyPlanObject *) arg;
2116 SPI_freeplan(ob->plan);
2118 PLy_free(ob->types);
2123 for (i = 0; i < ob->nargs; i++)
2124 PLy_typeinfo_dealloc(&ob->args[i]);
2128 arg->ob_type->tp_free(arg);
2133 PLy_plan_getattr(PyObject * self, char *name)
2135 return Py_FindMethod(PLy_plan_methods, self, name);
2139 PLy_plan_status(PyObject * self, PyObject * args)
2141 if (PyArg_ParseTuple(args, ""))
2145 /* return PyInt_FromLong(self->status); */
2147 PyErr_SetString(PLy_exc_error, "plan.status() takes no arguments");
2153 /* result object methods */
2156 PLy_result_new(void)
2158 PLyResultObject *ob;
2160 if ((ob = PyObject_NEW(PLyResultObject, &PLy_ResultType)) == NULL)
2163 /* ob->tuples = NULL; */
2166 ob->status = Py_None;
2167 ob->nrows = PyInt_FromLong(-1);
2168 ob->rows = PyList_New(0);
2170 return (PyObject *) ob;
2174 PLy_result_dealloc(PyObject * arg)
2176 PLyResultObject *ob = (PLyResultObject *) arg;
2178 Py_XDECREF(ob->nrows);
2179 Py_XDECREF(ob->rows);
2180 Py_XDECREF(ob->status);
2182 arg->ob_type->tp_free(arg);
2186 PLy_result_getattr(PyObject * self, char *name)
2188 return Py_FindMethod(PLy_result_methods, self, name);
2192 PLy_result_nrows(PyObject * self, PyObject * args)
2194 PLyResultObject *ob = (PLyResultObject *) self;
2196 Py_INCREF(ob->nrows);
2201 PLy_result_status(PyObject * self, PyObject * args)
2203 PLyResultObject *ob = (PLyResultObject *) self;
2205 Py_INCREF(ob->status);
2210 PLy_result_length(PyObject * arg)
2212 PLyResultObject *ob = (PLyResultObject *) arg;
2214 return PyList_Size(ob->rows);
2218 PLy_result_item(PyObject * arg, Py_ssize_t idx)
2221 PLyResultObject *ob = (PLyResultObject *) arg;
2223 rv = PyList_GetItem(ob->rows, idx);
2230 PLy_result_ass_item(PyObject * arg, Py_ssize_t idx, PyObject * item)
2233 PLyResultObject *ob = (PLyResultObject *) arg;
2236 rv = PyList_SetItem(ob->rows, idx, item);
2241 PLy_result_slice(PyObject * arg, Py_ssize_t lidx, Py_ssize_t hidx)
2244 PLyResultObject *ob = (PLyResultObject *) arg;
2246 rv = PyList_GetSlice(ob->rows, lidx, hidx);
2254 PLy_result_ass_slice(PyObject * arg, Py_ssize_t lidx, Py_ssize_t hidx, PyObject * slice)
2257 PLyResultObject *ob = (PLyResultObject *) arg;
2259 rv = PyList_SetSlice(ob->rows, lidx, hidx, slice);
2265 PLy_spi_prepare(PyObject * self, PyObject * args)
2267 PLyPlanObject *plan;
2268 PyObject *list = NULL;
2269 PyObject *volatile optr = NULL;
2272 MemoryContext oldcontext;
2274 /* Can't execute more if we have an unhandled error */
2275 if (PLy_error_in_progress)
2277 PyErr_SetString(PLy_exc_error, "Transaction aborted.");
2281 if (!PyArg_ParseTuple(args, "s|O", &query, &list))
2283 PyErr_SetString(PLy_exc_spi_error,
2284 "Invalid arguments for plpy.prepare()");
2288 if (list && (!PySequence_Check(list)))
2290 PyErr_SetString(PLy_exc_spi_error,
2291 "Second argument in plpy.prepare() must be a sequence");
2295 if ((plan = (PLyPlanObject *) PLy_plan_new()) == NULL)
2298 oldcontext = CurrentMemoryContext;
2306 nargs = PySequence_Length(list);
2309 plan->nargs = nargs;
2310 plan->types = PLy_malloc(sizeof(Oid) * nargs);
2311 plan->values = PLy_malloc(sizeof(Datum) * nargs);
2312 plan->args = PLy_malloc(sizeof(PLyTypeInfo) * nargs);
2315 * the other loop might throw an exception, if PLyTypeInfo
2316 * member isn't properly initialized the Py_DECREF(plan) will
2319 for (i = 0; i < nargs; i++)
2321 PLy_typeinfo_init(&plan->args[i]);
2322 plan->values[i] = PointerGetDatum(NULL);
2325 for (i = 0; i < nargs; i++)
2331 Form_pg_type typeStruct;
2333 optr = PySequence_GetItem(list, i);
2334 if (!PyString_Check(optr))
2335 elog(ERROR, "Type names must be strings.");
2336 sptr = PyString_AsString(optr);
2338 /********************************************************
2339 * Resolve argument type names and then look them up by
2340 * oid in the system cache, and remember the required
2341 *information for input conversion.
2342 ********************************************************/
2344 parseTypeString(sptr, &typeId, &typmod);
2346 typeTup = SearchSysCache(TYPEOID,
2347 ObjectIdGetDatum(typeId),
2349 if (!HeapTupleIsValid(typeTup))
2350 elog(ERROR, "cache lookup failed for type %u", typeId);
2353 optr = NULL; /* this is important */
2355 plan->types[i] = typeId;
2356 typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
2357 if (typeStruct->typtype != TYPTYPE_COMPOSITE)
2358 PLy_output_datum_func(&plan->args[i], typeTup);
2360 elog(ERROR, "tuples not handled in plpy.prepare, yet.");
2361 ReleaseSysCache(typeTup);
2366 plan->plan = SPI_prepare(query, plan->nargs, plan->types);
2367 if (plan->plan == NULL)
2368 elog(ERROR, "SPI_prepare failed: %s",
2369 SPI_result_code_string(SPI_result));
2371 /* transfer plan from procCxt to topCxt */
2372 tmpplan = plan->plan;
2373 plan->plan = SPI_saveplan(tmpplan);
2374 SPI_freeplan(tmpplan);
2375 if (plan->plan == NULL)
2376 elog(ERROR, "SPI_saveplan failed: %s",
2377 SPI_result_code_string(SPI_result));
2381 MemoryContextSwitchTo(oldcontext);
2382 PLy_error_in_progress = CopyErrorData();
2386 if (!PyErr_Occurred())
2387 PyErr_SetString(PLy_exc_spi_error,
2388 "Unknown error in PLy_spi_prepare");
2389 /* XXX this oughta be replaced with errcontext mechanism */
2390 PLy_elog(WARNING, "in function %s:",
2391 PLy_procedure_name(PLy_curr_procedure));
2396 return (PyObject *) plan;
2399 /* execute(query="select * from foo", limit=5)
2400 * execute(plan=plan, values=(foo, bar), limit=5)
2403 PLy_spi_execute(PyObject * self, PyObject * args)
2407 PyObject *list = NULL;
2410 /* Can't execute more if we have an unhandled error */
2411 if (PLy_error_in_progress)
2413 PyErr_SetString(PLy_exc_error, "Transaction aborted.");
2417 if (PyArg_ParseTuple(args, "s|l", &query, &limit))
2418 return PLy_spi_execute_query(query, limit);
2422 if (PyArg_ParseTuple(args, "O|Ol", &plan, &list, &limit) &&
2423 is_PLyPlanObject(plan))
2424 return PLy_spi_execute_plan(plan, list, limit);
2426 PyErr_SetString(PLy_exc_error, "Expected a query or plan.");
2431 PLy_spi_execute_plan(PyObject * ob, PyObject * list, long limit)
2436 PLyPlanObject *plan;
2437 MemoryContext oldcontext;
2441 if (!PySequence_Check(list) || PyString_Check(list))
2443 char *msg = "plpy.execute() takes a sequence as its second argument";
2445 PyErr_SetString(PLy_exc_spi_error, msg);
2448 nargs = PySequence_Length(list);
2453 plan = (PLyPlanObject *) ob;
2455 if (nargs != plan->nargs)
2458 PyObject *so = PyObject_Str(list);
2461 PLy_elog(ERROR, "function \"%s\" could not execute plan",
2462 PLy_procedure_name(PLy_curr_procedure));
2463 sv = PyString_AsString(so);
2464 PLy_exception_set(PLy_exc_spi_error,
2465 "Expected sequence of %d arguments, got %d. %s",
2466 plan->nargs, nargs, sv);
2472 oldcontext = CurrentMemoryContext;
2475 char *nulls = palloc(nargs * sizeof(char));
2477 for (i = 0; i < nargs; i++)
2482 elem = PySequence_GetItem(list, i);
2483 if (elem != Py_None)
2485 so = PyObject_Str(elem);
2487 PLy_elog(ERROR, "function \"%s\" could not execute plan",
2488 PLy_procedure_name(PLy_curr_procedure));
2493 char *sv = PyString_AsString(so);
2496 InputFunctionCall(&(plan->args[i].out.d.typfunc),
2498 plan->args[i].out.d.typioparam,
2515 InputFunctionCall(&(plan->args[i].out.d.typfunc),
2517 plan->args[i].out.d.typioparam,
2523 rv = SPI_execute_plan(plan->plan, plan->values, nulls,
2524 PLy_curr_procedure->fn_readonly, limit);
2530 MemoryContextSwitchTo(oldcontext);
2531 PLy_error_in_progress = CopyErrorData();
2535 * cleanup plan->values array
2537 for (i = 0; i < nargs; i++)
2539 if (!plan->args[i].out.d.typbyval &&
2540 (plan->values[i] != PointerGetDatum(NULL)))
2542 pfree(DatumGetPointer(plan->values[i]));
2543 plan->values[i] = PointerGetDatum(NULL);
2547 if (!PyErr_Occurred())
2548 PyErr_SetString(PLy_exc_error,
2549 "Unknown error in PLy_spi_execute_plan");
2550 /* XXX this oughta be replaced with errcontext mechanism */
2551 PLy_elog(WARNING, "in function %s:",
2552 PLy_procedure_name(PLy_curr_procedure));
2557 for (i = 0; i < nargs; i++)
2559 if (!plan->args[i].out.d.typbyval &&
2560 (plan->values[i] != PointerGetDatum(NULL)))
2562 pfree(DatumGetPointer(plan->values[i]));
2563 plan->values[i] = PointerGetDatum(NULL);
2569 PLy_exception_set(PLy_exc_spi_error,
2570 "SPI_execute_plan failed: %s",
2571 SPI_result_code_string(rv));
2575 return PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
2579 PLy_spi_execute_query(char *query, long limit)
2582 MemoryContext oldcontext;
2584 oldcontext = CurrentMemoryContext;
2587 rv = SPI_execute(query, PLy_curr_procedure->fn_readonly, limit);
2591 MemoryContextSwitchTo(oldcontext);
2592 PLy_error_in_progress = CopyErrorData();
2594 if (!PyErr_Occurred())
2595 PyErr_SetString(PLy_exc_spi_error,
2596 "Unknown error in PLy_spi_execute_query");
2597 /* XXX this oughta be replaced with errcontext mechanism */
2598 PLy_elog(WARNING, "in function %s:",
2599 PLy_procedure_name(PLy_curr_procedure));
2606 PLy_exception_set(PLy_exc_spi_error,
2607 "SPI_execute failed: %s",
2608 SPI_result_code_string(rv));
2612 return PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
2616 PLy_spi_execute_fetch_result(SPITupleTable *tuptable, int rows, int status)
2618 PLyResultObject *result;
2619 MemoryContext oldcontext;
2621 result = (PLyResultObject *) PLy_result_new();
2622 Py_DECREF(result->status);
2623 result->status = PyInt_FromLong(status);
2625 if (status > 0 && tuptable == NULL)
2627 Py_DECREF(result->nrows);
2628 result->nrows = PyInt_FromLong(rows);
2630 else if (status > 0 && tuptable != NULL)
2635 Py_DECREF(result->nrows);
2636 result->nrows = PyInt_FromLong(rows);
2637 PLy_typeinfo_init(&args);
2639 oldcontext = CurrentMemoryContext;
2644 Py_DECREF(result->rows);
2645 result->rows = PyList_New(rows);
2647 PLy_input_tuple_funcs(&args, tuptable->tupdesc);
2648 for (i = 0; i < rows; i++)
2650 PyObject *row = PLyDict_FromTuple(&args, tuptable->vals[i],
2653 PyList_SetItem(result->rows, i, row);
2655 PLy_typeinfo_dealloc(&args);
2657 SPI_freetuptable(tuptable);
2662 MemoryContextSwitchTo(oldcontext);
2663 PLy_error_in_progress = CopyErrorData();
2665 if (!PyErr_Occurred())
2666 PyErr_SetString(PLy_exc_error,
2667 "Unknown error in PLy_spi_execute_fetch_result");
2669 PLy_typeinfo_dealloc(&args);
2675 return (PyObject *) result;
2680 * language handler and interpreter initialization
2684 * _PG_init() - library load-time initialization
2686 * DO NOT make this static nor change its name!
2691 /* Be sure we do initialization only once (should be redundant now) */
2692 static bool inited = false;
2700 if (PyErr_Occurred())
2701 PLy_elog(FATAL, "untrapped error in initialization");
2702 PLy_procedure_cache = PyDict_New();
2703 if (PLy_procedure_cache == NULL)
2704 PLy_elog(ERROR, "could not create procedure cache");
2710 PLy_init_interp(void)
2714 mainmod = PyImport_AddModule("__main__");
2715 if (mainmod == NULL || PyErr_Occurred())
2716 PLy_elog(ERROR, "could not import \"__main__\" module.");
2718 PLy_interp_globals = PyModule_GetDict(mainmod);
2719 PLy_interp_safe_globals = PyDict_New();
2720 PyDict_SetItemString(PLy_interp_globals, "GD", PLy_interp_safe_globals);
2722 if (PLy_interp_globals == NULL || PyErr_Occurred())
2723 PLy_elog(ERROR, "could not initialize globals");
2736 * initialize plpy module
2738 if (PyType_Ready(&PLy_PlanType) < 0)
2739 elog(ERROR, "could not init PLy_PlanType");
2740 if (PyType_Ready(&PLy_ResultType) < 0)
2741 elog(ERROR, "could not init PLy_ResultType");
2743 plpy = Py_InitModule("plpy", PLy_methods);
2744 plpy_dict = PyModule_GetDict(plpy);
2746 /* PyDict_SetItemString(plpy, "PlanType", (PyObject *) &PLy_PlanType); */
2748 PLy_exc_error = PyErr_NewException("plpy.Error", NULL, NULL);
2749 PLy_exc_fatal = PyErr_NewException("plpy.Fatal", NULL, NULL);
2750 PLy_exc_spi_error = PyErr_NewException("plpy.SPIError", NULL, NULL);
2751 PyDict_SetItemString(plpy_dict, "Error", PLy_exc_error);
2752 PyDict_SetItemString(plpy_dict, "Fatal", PLy_exc_fatal);
2753 PyDict_SetItemString(plpy_dict, "SPIError", PLy_exc_spi_error);
2756 * initialize main module, and add plpy
2758 main_mod = PyImport_AddModule("__main__");
2759 main_dict = PyModule_GetDict(main_mod);
2760 plpy_mod = PyImport_AddModule("plpy");
2761 PyDict_SetItemString(main_dict, "plpy", plpy_mod);
2762 if (PyErr_Occurred())
2763 elog(ERROR, "could not init plpy");
2766 /* the python interface to the elog function
2767 * don't confuse these with PLy_elog
2769 static PyObject *PLy_output(volatile int, PyObject *, PyObject *);
2772 PLy_debug(PyObject * self, PyObject * args)
2774 return PLy_output(DEBUG2, self, args);
2778 PLy_log(PyObject * self, PyObject * args)
2780 return PLy_output(LOG, self, args);
2784 PLy_info(PyObject * self, PyObject * args)
2786 return PLy_output(INFO, self, args);
2790 PLy_notice(PyObject * self, PyObject * args)
2792 return PLy_output(NOTICE, self, args);
2796 PLy_warning(PyObject * self, PyObject * args)
2798 return PLy_output(WARNING, self, args);
2802 PLy_error(PyObject * self, PyObject * args)
2804 return PLy_output(ERROR, self, args);
2808 PLy_fatal(PyObject * self, PyObject * args)
2810 return PLy_output(FATAL, self, args);
2815 PLy_output(volatile int level, PyObject * self, PyObject * args)
2819 MemoryContext oldcontext;
2821 so = PyObject_Str(args);
2822 if (so == NULL || ((sv = PyString_AsString(so)) == NULL))
2825 sv = "could not parse error message in `plpy.elog'";
2828 oldcontext = CurrentMemoryContext;
2831 elog(level, "%s", sv);
2835 MemoryContextSwitchTo(oldcontext);
2836 PLy_error_in_progress = CopyErrorData();
2841 * returning NULL here causes the python interpreter to bail. when
2842 * control passes back to PLy_procedure_call, we check for PG
2843 * exceptions and re-throw the error.
2845 PyErr_SetString(PLy_exc_error, sv);
2853 * return a legal object so the interpreter will continue on its merry way
2861 * Get the name of the last procedure called by the backend (the
2862 * innermost, if a plpython procedure call calls the backend and the
2863 * backend calls another plpython procedure).
2865 * NB: this returns the SQL name, not the internal Python procedure name
2868 PLy_procedure_name(PLyProcedure * proc)
2871 return "<unknown procedure>";
2872 return proc->proname;
2875 /* output a python traceback/exception via the postgresql elog
2876 * function. not pretty.
2879 PLy_exception_set(PyObject * exc, const char *fmt,...)
2885 vsnprintf(buf, sizeof(buf), fmt, ap);
2888 PyErr_SetString(exc, buf);
2891 /* Emit a PG error or notice, together with any available info about the
2892 * current Python error. This should be used to propagate Python errors
2896 PLy_elog(int elevel, const char *fmt,...)
2903 xmsg = PLy_traceback(&xlevel);
2906 emsg = PLy_vprintf(fmt, ap);
2912 (errmsg("plpython: %s", emsg),
2913 (xmsg) ? errdetail("%s", xmsg) : 0));
2930 PLy_traceback(int *xlevel)
2942 * get the current exception
2944 PyErr_Fetch(&e, &v, &tb);
2947 * oops, no exception, return
2955 PyErr_NormalizeException(&e, &v, &tb);
2958 eob = PyObject_Str(e);
2959 if (v && ((vob = PyObject_Str(v)) != NULL))
2960 vstr = PyString_AsString(vob);
2965 * I'm not sure what to do if eob is NULL here -- we can't call PLy_elog
2966 * because that function calls us, so we could end up with infinite
2967 * recursion. I'm not even sure if eob could be NULL here -- would an
2968 * Assert() be more appropriate?
2970 estr = eob ? PyString_AsString(eob) : "Unknown Exception";
2971 xstr = PLy_printf("%s: %s", estr, vstr);
2978 * intuit an appropriate error level based on the exception type
2980 if (PLy_exc_error && PyErr_GivenExceptionMatches(e, PLy_exc_error))
2982 else if (PLy_exc_fatal && PyErr_GivenExceptionMatches(e, PLy_exc_fatal))
2992 PLy_printf(const char *fmt,...)
2998 emsg = PLy_vprintf(fmt, ap);
3004 PLy_vprintf(const char *fmt, va_list ap)
3011 blen = strlen(fmt) * 2;
3014 buf = PLy_malloc(blen * sizeof(char));
3018 bchar = vsnprintf(buf, blen, fmt, ap);
3019 if (bchar > 0 && bchar < blen)
3027 buf = PLy_realloc(buf, blen);
3033 /* python module code */
3035 /* some dumb utility functions */
3037 PLy_malloc(size_t bytes)
3039 void *ptr = malloc(bytes);
3043 (errcode(ERRCODE_OUT_OF_MEMORY),
3044 errmsg("out of memory")));
3049 PLy_realloc(void *optr, size_t bytes)
3051 void *nptr = realloc(optr, bytes);
3055 (errcode(ERRCODE_OUT_OF_MEMORY),
3056 errmsg("out of memory")));
3061 PLy_strdup(const char *str)
3066 len = strlen(str) + 1;
3067 result = PLy_malloc(len);
3068 memcpy(result, str, len);
3073 /* define this away */