Fix obsolete comment regarding FSM truncation.
[PostgreSQL.git] / src / pl / plpython / plpython.c
blob1565c41372fd66c9ea8303df4511c16dddd475a1
1 /**********************************************************************
2 * plpython.c - python as a procedural language for PostgreSQL
4 * $PostgreSQL$
6 *********************************************************************
7 */
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 */
12 #undef _DEBUG
13 /* Also hide away errcode, since we load Python.h before postgres.h */
14 #define errcode __msvc_errcode
15 #include <Python.h>
16 #undef errcode
17 #define _DEBUG
18 #elif defined (_MSC_VER)
19 #define errcode __msvc_errcode
20 #include <Python.h>
21 #undef errcode
22 #else
23 #include <Python.h>
24 #endif
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
34 #endif
37 * PyBool_FromLong is supported from 2.3.
39 #if PY_VERSION_HEX < 0x02030000
40 #define PyBool_FromLong(x) PyInt_FromLong(x)
41 #endif
44 #include "postgres.h"
46 /* system stuff */
47 #include <unistd.h>
48 #include <fcntl.h>
50 /* postgreSQL stuff */
51 #include "catalog/pg_proc.h"
52 #include "catalog/pg_type.h"
53 #include "commands/trigger.h"
54 #include "executor/spi.h"
55 #include "funcapi.h"
56 #include "fmgr.h"
57 #include "miscadmin.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"
67 /* define our text domain for translations */
68 #undef TEXTDOMAIN
69 #define TEXTDOMAIN "plpython"
71 #include <compile.h>
72 #include <eval.h>
74 PG_MODULE_MAGIC;
76 /* convert Postgresql Datum or tuple into a PyObject.
77 * input to Python. Tuples are converted to dictionary
78 * objects.
81 typedef PyObject *(*PLyDatumToObFunc) (const char *);
83 typedef struct PLyDatumToOb
85 PLyDatumToObFunc func;
86 FmgrInfo typfunc; /* The type's output function */
87 Oid typoid; /* The OID of the type */
88 Oid typioparam;
89 bool typbyval;
90 } PLyDatumToOb;
92 typedef struct PLyTupleToOb
94 PLyDatumToOb *atts;
95 int natts;
96 } PLyTupleToOb;
98 typedef union PLyTypeInput
100 PLyDatumToOb d;
101 PLyTupleToOb r;
102 } PLyTypeInput;
104 /* convert PyObject to a Postgresql Datum or tuple.
105 * output from Python
107 typedef struct PLyObToDatum
109 FmgrInfo typfunc; /* The type's input function */
110 Oid typoid; /* The OID of the type */
111 Oid typioparam;
112 bool typbyval;
113 } PLyObToDatum;
115 typedef struct PLyObToTuple
117 PLyObToDatum *atts;
118 int natts;
119 } PLyObToTuple;
121 typedef union PLyTypeOutput
123 PLyObToDatum d;
124 PLyObToTuple r;
125 } PLyTypeOutput;
127 /* all we need to move Postgresql data to Python objects,
128 * and vis versa
130 typedef struct PLyTypeInfo
132 PLyTypeInput in;
133 PLyTypeOutput out;
134 int is_rowtype;
137 * is_rowtype can be: -1 not known yet (initial state) 0 scalar datatype
138 * 1 rowtype 2 rowtype, but I/O functions not set up yet
140 } PLyTypeInfo;
143 /* cached procedure data */
144 typedef struct PLyProcedure
146 char *proname; /* SQL name of procedure */
147 char *pyname; /* Python name of procedure */
148 TransactionId fn_xmin;
149 ItemPointerData fn_tid;
150 bool fn_readonly;
151 PLyTypeInfo result; /* also used to store info for trigger tuple
152 * type */
153 bool is_setof; /* true, if procedure returns result set */
154 PyObject *setof; /* contents of result set. */
155 char **argnames; /* Argument names */
156 PLyTypeInfo args[FUNC_MAX_ARGS];
157 int nargs;
158 PyObject *code; /* compiled procedure code */
159 PyObject *statics; /* data saved across calls, local scope */
160 PyObject *globals; /* data saved across calls, global scope */
161 PyObject *me; /* PyCObject containing pointer to this
162 * PLyProcedure */
163 } PLyProcedure;
166 /* Python objects */
167 typedef struct PLyPlanObject
169 PyObject_HEAD
170 void *plan; /* return of an SPI_saveplan */
171 int nargs;
172 Oid *types;
173 Datum *values;
174 PLyTypeInfo *args;
175 } PLyPlanObject;
177 typedef struct PLyResultObject
179 PyObject_HEAD
180 /* HeapTuple *tuples; */
181 PyObject * nrows; /* number of rows returned by query */
182 PyObject *rows; /* data rows, or None if no data returned */
183 PyObject *status; /* query status, SPI_OK_*, or SPI_ERR_* */
184 } PLyResultObject;
187 /* function declarations */
189 /* Two exported functions: first is the magic telling Postgresql
190 * what function call interface it implements. Second is for
191 * initialization of the interpreter during library load.
193 Datum plpython_call_handler(PG_FUNCTION_ARGS);
194 void _PG_init(void);
196 PG_FUNCTION_INFO_V1(plpython_call_handler);
198 /* most of the remaining of the declarations, all static */
200 /* these should only be called once at the first call
201 * of plpython_call_handler. initialize the python interpreter
202 * and global data.
204 static void PLy_init_interp(void);
205 static void PLy_init_plpy(void);
207 /* call PyErr_SetString with a vprint interface */
208 static void
209 PLy_exception_set(PyObject *, const char *,...)
210 __attribute__((format(printf, 2, 3)));
212 /* Get the innermost python procedure called from the backend */
213 static char *PLy_procedure_name(PLyProcedure *);
215 /* some utility functions */
216 static void PLy_elog(int, const char *,...);
217 static char *PLy_traceback(int *);
219 static void *PLy_malloc(size_t);
220 static void *PLy_malloc0(size_t);
221 static char *PLy_strdup(const char *);
222 static void PLy_free(void *);
224 /* sub handlers for functions and triggers */
225 static Datum PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure *);
226 static HeapTuple PLy_trigger_handler(FunctionCallInfo fcinfo, PLyProcedure *);
228 static PyObject *PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *);
229 static void PLy_function_delete_args(PLyProcedure *);
230 static PyObject *PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *,
231 HeapTuple *);
232 static HeapTuple PLy_modify_tuple(PLyProcedure *, PyObject *,
233 TriggerData *, HeapTuple);
235 static PyObject *PLy_procedure_call(PLyProcedure *, char *, PyObject *);
237 static PLyProcedure *PLy_procedure_get(FunctionCallInfo fcinfo,
238 Oid tgreloid);
240 static PLyProcedure *PLy_procedure_create(HeapTuple procTup, Oid tgreloid,
241 char *key);
243 static void PLy_procedure_compile(PLyProcedure *, const char *);
244 static char *PLy_procedure_munge_source(const char *, const char *);
245 static void PLy_procedure_delete(PLyProcedure *);
247 static void PLy_typeinfo_init(PLyTypeInfo *);
248 static void PLy_typeinfo_dealloc(PLyTypeInfo *);
249 static void PLy_output_datum_func(PLyTypeInfo *, HeapTuple);
250 static void PLy_output_datum_func2(PLyObToDatum *, HeapTuple);
251 static void PLy_input_datum_func(PLyTypeInfo *, Oid, HeapTuple);
252 static void PLy_input_datum_func2(PLyDatumToOb *, Oid, HeapTuple);
253 static void PLy_output_tuple_funcs(PLyTypeInfo *, TupleDesc);
254 static void PLy_input_tuple_funcs(PLyTypeInfo *, TupleDesc);
256 /* conversion functions */
257 static PyObject *PLyDict_FromTuple(PLyTypeInfo *, HeapTuple, TupleDesc);
258 static PyObject *PLyBool_FromString(const char *);
259 static PyObject *PLyFloat_FromString(const char *);
260 static PyObject *PLyInt_FromString(const char *);
261 static PyObject *PLyLong_FromString(const char *);
262 static PyObject *PLyString_FromString(const char *);
264 static HeapTuple PLyMapping_ToTuple(PLyTypeInfo *, PyObject *);
265 static HeapTuple PLySequence_ToTuple(PLyTypeInfo *, PyObject *);
266 static HeapTuple PLyObject_ToTuple(PLyTypeInfo *, PyObject *);
269 * Currently active plpython function
271 static PLyProcedure *PLy_curr_procedure = NULL;
274 * When a callback from Python into PG incurs an error, we temporarily store
275 * the error information here, and return NULL to the Python interpreter.
276 * Any further callback attempts immediately fail, and when the Python
277 * interpreter returns to the calling function, we re-throw the error (even if
278 * Python thinks it trapped the error and doesn't return NULL). Eventually
279 * this ought to be improved to let Python code really truly trap the error,
280 * but that's more of a change from the pre-8.0 semantics than I have time for
281 * now --- it will only be possible if the callback query is executed inside a
282 * subtransaction.
284 static ErrorData *PLy_error_in_progress = NULL;
286 static PyObject *PLy_interp_globals = NULL;
287 static PyObject *PLy_interp_safe_globals = NULL;
288 static PyObject *PLy_procedure_cache = NULL;
290 /* Python exceptions */
291 static PyObject *PLy_exc_error = NULL;
292 static PyObject *PLy_exc_fatal = NULL;
293 static PyObject *PLy_exc_spi_error = NULL;
295 /* some globals for the python module */
296 static char PLy_plan_doc[] = {
297 "Store a PostgreSQL plan"
300 static char PLy_result_doc[] = {
301 "Results of a PostgreSQL query"
306 * the function definitions
310 * This routine is a crock, and so is everyplace that calls it. The problem
311 * is that the cached form of plpython functions/queries is allocated permanently
312 * (mostly via malloc()) and never released until backend exit. Subsidiary
313 * data structures such as fmgr info records therefore must live forever
314 * as well. A better implementation would store all this stuff in a per-
315 * function memory context that could be reclaimed at need. In the meantime,
316 * fmgr_info_cxt must be called specifying TopMemoryContext so that whatever
317 * it might allocate, and whatever the eventual function might allocate using
318 * fn_mcxt, will live forever too.
320 static void
321 perm_fmgr_info(Oid functionId, FmgrInfo *finfo)
323 fmgr_info_cxt(functionId, finfo, TopMemoryContext);
326 Datum
327 plpython_call_handler(PG_FUNCTION_ARGS)
329 Datum retval;
330 PLyProcedure *save_curr_proc;
331 PLyProcedure *volatile proc = NULL;
333 if (SPI_connect() != SPI_OK_CONNECT)
334 elog(ERROR, "could not connect to SPI manager");
336 save_curr_proc = PLy_curr_procedure;
338 PG_TRY();
340 if (CALLED_AS_TRIGGER(fcinfo))
342 TriggerData *tdata = (TriggerData *) fcinfo->context;
343 HeapTuple trv;
345 proc = PLy_procedure_get(fcinfo,
346 RelationGetRelid(tdata->tg_relation));
347 PLy_curr_procedure = proc;
348 trv = PLy_trigger_handler(fcinfo, proc);
349 retval = PointerGetDatum(trv);
351 else
353 proc = PLy_procedure_get(fcinfo, InvalidOid);
354 PLy_curr_procedure = proc;
355 retval = PLy_function_handler(fcinfo, proc);
358 PG_CATCH();
360 PLy_curr_procedure = save_curr_proc;
361 if (proc)
363 /* note: Py_DECREF needs braces around it, as of 2003/08 */
364 Py_DECREF(proc->me);
366 PyErr_Clear();
367 PG_RE_THROW();
369 PG_END_TRY();
371 PLy_curr_procedure = save_curr_proc;
373 Py_DECREF(proc->me);
375 return retval;
378 /* trigger and function sub handlers
380 * the python function is expected to return Py_None if the tuple is
381 * acceptable and unmodified. Otherwise it should return a PyString
382 * object who's value is SKIP, or MODIFY. SKIP means don't perform
383 * this action. MODIFY means the tuple has been modified, so update
384 * tuple and perform action. SKIP and MODIFY assume the trigger fires
385 * BEFORE the event and is ROW level. postgres expects the function
386 * to take no arguments and return an argument of type trigger.
388 static HeapTuple
389 PLy_trigger_handler(FunctionCallInfo fcinfo, PLyProcedure * proc)
391 HeapTuple rv = NULL;
392 PyObject *volatile plargs = NULL;
393 PyObject *volatile plrv = NULL;
395 PG_TRY();
397 plargs = PLy_trigger_build_args(fcinfo, proc, &rv);
398 plrv = PLy_procedure_call(proc, "TD", plargs);
400 Assert(plrv != NULL);
401 Assert(!PLy_error_in_progress);
404 * Disconnect from SPI manager
406 if (SPI_finish() != SPI_OK_FINISH)
407 elog(ERROR, "SPI_finish failed");
410 * return of None means we're happy with the tuple
412 if (plrv != Py_None)
414 char *srv;
416 if (!PyString_Check(plrv))
417 ereport(ERROR,
418 (errcode(ERRCODE_DATA_EXCEPTION),
419 errmsg("unexpected return value from trigger procedure"),
420 errdetail("Expected None or a String.")));
422 srv = PyString_AsString(plrv);
423 if (pg_strcasecmp(srv, "SKIP") == 0)
424 rv = NULL;
425 else if (pg_strcasecmp(srv, "MODIFY") == 0)
427 TriggerData *tdata = (TriggerData *) fcinfo->context;
429 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event) ||
430 TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
431 rv = PLy_modify_tuple(proc, plargs, tdata, rv);
432 else
433 elog(WARNING, "ignoring modified tuple in DELETE trigger");
435 else if (pg_strcasecmp(srv, "OK") != 0)
438 * accept "OK" as an alternative to None; otherwise, raise an
439 * error
441 ereport(ERROR,
442 (errcode(ERRCODE_DATA_EXCEPTION),
443 errmsg("unexpected return value from trigger procedure"),
444 errdetail("Expected None, \"OK\", \"SKIP\", or \"MODIFY\".")));
448 PG_CATCH();
450 Py_XDECREF(plargs);
451 Py_XDECREF(plrv);
453 PG_RE_THROW();
455 PG_END_TRY();
457 Py_DECREF(plargs);
458 Py_DECREF(plrv);
460 return rv;
463 static HeapTuple
464 PLy_modify_tuple(PLyProcedure * proc, PyObject * pltd, TriggerData *tdata,
465 HeapTuple otup)
467 PyObject *volatile plntup;
468 PyObject *volatile plkeys;
469 PyObject *volatile platt;
470 PyObject *volatile plval;
471 PyObject *volatile plstr;
472 HeapTuple rtup;
473 int natts,
475 attn,
476 atti;
477 int *volatile modattrs;
478 Datum *volatile modvalues;
479 char *volatile modnulls;
480 TupleDesc tupdesc;
482 plntup = plkeys = platt = plval = plstr = NULL;
483 modattrs = NULL;
484 modvalues = NULL;
485 modnulls = NULL;
487 PG_TRY();
489 if ((plntup = PyDict_GetItemString(pltd, "new")) == NULL)
490 elog(ERROR, "TD[\"new\"] deleted, cannot modify tuple");
491 if (!PyDict_Check(plntup))
492 elog(ERROR, "TD[\"new\"] is not a dictionary object");
493 Py_INCREF(plntup);
495 plkeys = PyDict_Keys(plntup);
496 natts = PyList_Size(plkeys);
498 modattrs = (int *) palloc(natts * sizeof(int));
499 modvalues = (Datum *) palloc(natts * sizeof(Datum));
500 modnulls = (char *) palloc(natts * sizeof(char));
502 tupdesc = tdata->tg_relation->rd_att;
504 for (i = 0; i < natts; i++)
506 char *src;
508 platt = PyList_GetItem(plkeys, i);
509 if (!PyString_Check(platt))
510 elog(ERROR, "attribute name is not a string");
511 attn = SPI_fnumber(tupdesc, PyString_AsString(platt));
512 if (attn == SPI_ERROR_NOATTRIBUTE)
513 elog(ERROR, "invalid attribute \"%s\" in tuple",
514 PyString_AsString(platt));
515 atti = attn - 1;
517 plval = PyDict_GetItem(plntup, platt);
518 if (plval == NULL)
519 elog(FATAL, "python interpreter is probably corrupted");
521 Py_INCREF(plval);
523 modattrs[i] = attn;
525 if (tupdesc->attrs[atti]->attisdropped)
527 modvalues[i] = (Datum) 0;
528 modnulls[i] = 'n';
530 else if (plval != Py_None)
532 plstr = PyObject_Str(plval);
533 if (!plstr)
534 PLy_elog(ERROR, "function \"%s\" could not modify tuple",
535 proc->proname);
536 src = PyString_AsString(plstr);
538 modvalues[i] =
539 InputFunctionCall(&proc->result.out.r.atts[atti].typfunc,
540 src,
541 proc->result.out.r.atts[atti].typioparam,
542 tupdesc->attrs[atti]->atttypmod);
543 modnulls[i] = ' ';
545 Py_DECREF(plstr);
546 plstr = NULL;
548 else
550 modvalues[i] =
551 InputFunctionCall(&proc->result.out.r.atts[atti].typfunc,
552 NULL,
553 proc->result.out.r.atts[atti].typioparam,
554 tupdesc->attrs[atti]->atttypmod);
555 modnulls[i] = 'n';
558 Py_DECREF(plval);
559 plval = NULL;
562 rtup = SPI_modifytuple(tdata->tg_relation, otup, natts,
563 modattrs, modvalues, modnulls);
564 if (rtup == NULL)
565 elog(ERROR, "SPI_modifytuple failed -- error %d", SPI_result);
567 PG_CATCH();
569 Py_XDECREF(plntup);
570 Py_XDECREF(plkeys);
571 Py_XDECREF(plval);
572 Py_XDECREF(plstr);
574 if (modnulls)
575 pfree(modnulls);
576 if (modvalues)
577 pfree(modvalues);
578 if (modattrs)
579 pfree(modattrs);
581 PG_RE_THROW();
583 PG_END_TRY();
585 Py_DECREF(plntup);
586 Py_DECREF(plkeys);
588 pfree(modattrs);
589 pfree(modvalues);
590 pfree(modnulls);
592 return rtup;
595 static PyObject *
596 PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure * proc, HeapTuple *rv)
598 TriggerData *tdata = (TriggerData *) fcinfo->context;
599 PyObject *pltname,
600 *pltevent,
601 *pltwhen,
602 *pltlevel,
603 *pltrelid,
604 *plttablename,
605 *plttableschema;
606 PyObject *pltargs,
607 *pytnew,
608 *pytold;
609 PyObject *volatile pltdata = NULL;
610 char *stroid;
612 PG_TRY();
614 pltdata = PyDict_New();
615 if (!pltdata)
616 PLy_elog(ERROR, "could not build arguments for trigger procedure");
618 pltname = PyString_FromString(tdata->tg_trigger->tgname);
619 PyDict_SetItemString(pltdata, "name", pltname);
620 Py_DECREF(pltname);
622 stroid = DatumGetCString(DirectFunctionCall1(oidout,
623 ObjectIdGetDatum(tdata->tg_relation->rd_id)));
624 pltrelid = PyString_FromString(stroid);
625 PyDict_SetItemString(pltdata, "relid", pltrelid);
626 Py_DECREF(pltrelid);
627 pfree(stroid);
629 stroid = SPI_getrelname(tdata->tg_relation);
630 plttablename = PyString_FromString(stroid);
631 PyDict_SetItemString(pltdata, "table_name", plttablename);
632 Py_DECREF(plttablename);
633 pfree(stroid);
635 stroid = SPI_getnspname(tdata->tg_relation);
636 plttableschema = PyString_FromString(stroid);
637 PyDict_SetItemString(pltdata, "table_schema", plttableschema);
638 Py_DECREF(plttableschema);
639 pfree(stroid);
642 if (TRIGGER_FIRED_BEFORE(tdata->tg_event))
643 pltwhen = PyString_FromString("BEFORE");
644 else if (TRIGGER_FIRED_AFTER(tdata->tg_event))
645 pltwhen = PyString_FromString("AFTER");
646 else
648 elog(ERROR, "unrecognized WHEN tg_event: %u", tdata->tg_event);
649 pltwhen = NULL; /* keep compiler quiet */
651 PyDict_SetItemString(pltdata, "when", pltwhen);
652 Py_DECREF(pltwhen);
654 if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
656 pltlevel = PyString_FromString("ROW");
657 PyDict_SetItemString(pltdata, "level", pltlevel);
658 Py_DECREF(pltlevel);
660 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
662 pltevent = PyString_FromString("INSERT");
664 PyDict_SetItemString(pltdata, "old", Py_None);
665 pytnew = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
666 tdata->tg_relation->rd_att);
667 PyDict_SetItemString(pltdata, "new", pytnew);
668 Py_DECREF(pytnew);
669 *rv = tdata->tg_trigtuple;
671 else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
673 pltevent = PyString_FromString("DELETE");
675 PyDict_SetItemString(pltdata, "new", Py_None);
676 pytold = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
677 tdata->tg_relation->rd_att);
678 PyDict_SetItemString(pltdata, "old", pytold);
679 Py_DECREF(pytold);
680 *rv = tdata->tg_trigtuple;
682 else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
684 pltevent = PyString_FromString("UPDATE");
686 pytnew = PLyDict_FromTuple(&(proc->result), tdata->tg_newtuple,
687 tdata->tg_relation->rd_att);
688 PyDict_SetItemString(pltdata, "new", pytnew);
689 Py_DECREF(pytnew);
690 pytold = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
691 tdata->tg_relation->rd_att);
692 PyDict_SetItemString(pltdata, "old", pytold);
693 Py_DECREF(pytold);
694 *rv = tdata->tg_newtuple;
696 else
698 elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
699 pltevent = NULL; /* keep compiler quiet */
702 PyDict_SetItemString(pltdata, "event", pltevent);
703 Py_DECREF(pltevent);
705 else if (TRIGGER_FIRED_FOR_STATEMENT(tdata->tg_event))
707 pltlevel = PyString_FromString("STATEMENT");
708 PyDict_SetItemString(pltdata, "level", pltlevel);
709 Py_DECREF(pltlevel);
711 PyDict_SetItemString(pltdata, "old", Py_None);
712 PyDict_SetItemString(pltdata, "new", Py_None);
713 *rv = NULL;
715 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
716 pltevent = PyString_FromString("INSERT");
717 else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
718 pltevent = PyString_FromString("DELETE");
719 else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
720 pltevent = PyString_FromString("UPDATE");
721 else if (TRIGGER_FIRED_BY_TRUNCATE(tdata->tg_event))
722 pltevent = PyString_FromString("TRUNCATE");
723 else
725 elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
726 pltevent = NULL; /* keep compiler quiet */
729 PyDict_SetItemString(pltdata, "event", pltevent);
730 Py_DECREF(pltevent);
732 else
733 elog(ERROR, "unrecognized LEVEL tg_event: %u", tdata->tg_event);
735 if (tdata->tg_trigger->tgnargs)
738 * all strings...
740 int i;
741 PyObject *pltarg;
743 pltargs = PyList_New(tdata->tg_trigger->tgnargs);
744 for (i = 0; i < tdata->tg_trigger->tgnargs; i++)
746 pltarg = PyString_FromString(tdata->tg_trigger->tgargs[i]);
749 * stolen, don't Py_DECREF
751 PyList_SetItem(pltargs, i, pltarg);
754 else
756 Py_INCREF(Py_None);
757 pltargs = Py_None;
759 PyDict_SetItemString(pltdata, "args", pltargs);
760 Py_DECREF(pltargs);
762 PG_CATCH();
764 Py_XDECREF(pltdata);
765 PG_RE_THROW();
767 PG_END_TRY();
769 return pltdata;
774 /* function handler and friends */
775 static Datum
776 PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure * proc)
778 Datum rv;
779 PyObject *volatile plargs = NULL;
780 PyObject *volatile plrv = NULL;
781 PyObject *volatile plrv_so = NULL;
782 char *plrv_sc;
784 PG_TRY();
786 if (!proc->is_setof || proc->setof == NULL)
788 /* Simple type returning function or first time for SETOF function */
789 plargs = PLy_function_build_args(fcinfo, proc);
790 plrv = PLy_procedure_call(proc, "args", plargs);
791 if (!proc->is_setof)
794 * SETOF function parameters will be deleted when last row is
795 * returned
797 PLy_function_delete_args(proc);
798 Assert(plrv != NULL);
799 Assert(!PLy_error_in_progress);
803 * Disconnect from SPI manager and then create the return values datum
804 * (if the input function does a palloc for it this must not be
805 * allocated in the SPI memory context because SPI_finish would free
806 * it).
808 if (SPI_finish() != SPI_OK_FINISH)
809 elog(ERROR, "SPI_finish failed");
811 if (proc->is_setof)
813 bool has_error = false;
814 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
816 if (proc->setof == NULL)
818 /* first time -- do checks and setup */
819 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
820 (rsi->allowedModes & SFRM_ValuePerCall) == 0)
822 ereport(ERROR,
823 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
824 errmsg("only value per call is allowed")));
826 rsi->returnMode = SFRM_ValuePerCall;
828 /* Make iterator out of returned object */
829 proc->setof = PyObject_GetIter(plrv);
830 Py_DECREF(plrv);
831 plrv = NULL;
833 if (proc->setof == NULL)
834 ereport(ERROR,
835 (errcode(ERRCODE_DATATYPE_MISMATCH),
836 errmsg("returned object cannot be iterated"),
837 errdetail("SETOF must be returned as iterable object")));
840 /* Fetch next from iterator */
841 plrv = PyIter_Next(proc->setof);
842 if (plrv)
843 rsi->isDone = ExprMultipleResult;
844 else
846 rsi->isDone = ExprEndResult;
847 has_error = PyErr_Occurred() != NULL;
850 if (rsi->isDone == ExprEndResult)
852 /* Iterator is exhausted or error happened */
853 Py_DECREF(proc->setof);
854 proc->setof = NULL;
856 Py_XDECREF(plargs);
857 Py_XDECREF(plrv);
858 Py_XDECREF(plrv_so);
860 PLy_function_delete_args(proc);
862 if (has_error)
863 ereport(ERROR,
864 (errcode(ERRCODE_DATA_EXCEPTION),
865 errmsg("error fetching next item from iterator")));
867 fcinfo->isnull = true;
868 return (Datum) NULL;
873 * If the function is declared to return void, the Python return value
874 * must be None. For void-returning functions, we also treat a None
875 * return value as a special "void datum" rather than NULL (as is the
876 * case for non-void-returning functions).
878 if (proc->result.out.d.typoid == VOIDOID)
880 if (plrv != Py_None)
881 ereport(ERROR,
882 (errcode(ERRCODE_DATATYPE_MISMATCH),
883 errmsg("invalid return value from plpython function"),
884 errdetail("Functions returning type \"void\" must return None.")));
886 fcinfo->isnull = false;
887 rv = (Datum) 0;
889 else if (plrv == Py_None)
891 fcinfo->isnull = true;
892 if (proc->result.is_rowtype < 1)
893 rv = InputFunctionCall(&proc->result.out.d.typfunc,
894 NULL,
895 proc->result.out.d.typioparam,
896 -1);
897 else
898 /* Tuple as None */
899 rv = (Datum) NULL;
901 else if (proc->result.is_rowtype >= 1)
903 HeapTuple tuple = NULL;
905 if (PySequence_Check(plrv))
906 /* composite type as sequence (tuple, list etc) */
907 tuple = PLySequence_ToTuple(&proc->result, plrv);
908 else if (PyMapping_Check(plrv))
909 /* composite type as mapping (currently only dict) */
910 tuple = PLyMapping_ToTuple(&proc->result, plrv);
911 else
912 /* returned as smth, must provide method __getattr__(name) */
913 tuple = PLyObject_ToTuple(&proc->result, plrv);
915 if (tuple != NULL)
917 fcinfo->isnull = false;
918 rv = HeapTupleGetDatum(tuple);
920 else
922 fcinfo->isnull = true;
923 rv = (Datum) NULL;
926 else
928 fcinfo->isnull = false;
929 plrv_so = PyObject_Str(plrv);
930 if (!plrv_so)
931 PLy_elog(ERROR, "function \"%s\" could not create return value", proc->proname);
932 plrv_sc = PyString_AsString(plrv_so);
933 rv = InputFunctionCall(&proc->result.out.d.typfunc,
934 plrv_sc,
935 proc->result.out.d.typioparam,
936 -1);
939 PG_CATCH();
941 Py_XDECREF(plargs);
942 Py_XDECREF(plrv);
943 Py_XDECREF(plrv_so);
945 PG_RE_THROW();
947 PG_END_TRY();
949 Py_XDECREF(plargs);
950 Py_DECREF(plrv);
951 Py_XDECREF(plrv_so);
953 return rv;
956 static PyObject *
957 PLy_procedure_call(PLyProcedure * proc, char *kargs, PyObject * vargs)
959 PyObject *rv;
961 PyDict_SetItemString(proc->globals, kargs, vargs);
962 rv = PyEval_EvalCode((PyCodeObject *) proc->code,
963 proc->globals, proc->globals);
966 * If there was an error in a PG callback, propagate that no matter what
967 * Python claims about its success.
969 if (PLy_error_in_progress)
971 ErrorData *edata = PLy_error_in_progress;
973 PLy_error_in_progress = NULL;
974 ReThrowError(edata);
977 if (rv == NULL || PyErr_Occurred())
979 Py_XDECREF(rv);
980 PLy_elog(ERROR, "function \"%s\" failed", proc->proname);
983 return rv;
986 static PyObject *
987 PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure * proc)
989 PyObject *volatile arg = NULL;
990 PyObject *volatile args = NULL;
991 int i;
993 PG_TRY();
995 args = PyList_New(proc->nargs);
996 for (i = 0; i < proc->nargs; i++)
998 if (proc->args[i].is_rowtype > 0)
1000 if (fcinfo->argnull[i])
1001 arg = NULL;
1002 else
1004 HeapTupleHeader td;
1005 Oid tupType;
1006 int32 tupTypmod;
1007 TupleDesc tupdesc;
1008 HeapTupleData tmptup;
1010 td = DatumGetHeapTupleHeader(fcinfo->arg[i]);
1011 /* Extract rowtype info and find a tupdesc */
1012 tupType = HeapTupleHeaderGetTypeId(td);
1013 tupTypmod = HeapTupleHeaderGetTypMod(td);
1014 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
1016 /* Set up I/O funcs if not done yet */
1017 if (proc->args[i].is_rowtype != 1)
1018 PLy_input_tuple_funcs(&(proc->args[i]), tupdesc);
1020 /* Build a temporary HeapTuple control structure */
1021 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
1022 tmptup.t_data = td;
1024 arg = PLyDict_FromTuple(&(proc->args[i]), &tmptup, tupdesc);
1025 ReleaseTupleDesc(tupdesc);
1028 else
1030 if (fcinfo->argnull[i])
1031 arg = NULL;
1032 else
1034 char *ct;
1036 ct = OutputFunctionCall(&(proc->args[i].in.d.typfunc),
1037 fcinfo->arg[i]);
1038 arg = (proc->args[i].in.d.func) (ct);
1039 pfree(ct);
1043 if (arg == NULL)
1045 Py_INCREF(Py_None);
1046 arg = Py_None;
1049 if (PyList_SetItem(args, i, arg) == -1 ||
1050 (proc->argnames &&
1051 PyDict_SetItemString(proc->globals, proc->argnames[i], arg) == -1))
1052 PLy_elog(ERROR, "problem setting up arguments for \"%s\"", proc->proname);
1053 arg = NULL;
1056 PG_CATCH();
1058 Py_XDECREF(arg);
1059 Py_XDECREF(args);
1061 PG_RE_THROW();
1063 PG_END_TRY();
1065 return args;
1069 static void
1070 PLy_function_delete_args(PLyProcedure * proc)
1072 int i;
1074 if (!proc->argnames)
1075 return;
1077 for (i = 0; i < proc->nargs; i++)
1078 PyDict_DelItemString(proc->globals, proc->argnames[i]);
1083 * PLyProcedure functions
1086 /* PLy_procedure_get: returns a cached PLyProcedure, or creates, stores and
1087 * returns a new PLyProcedure. fcinfo is the call info, tgreloid is the
1088 * relation OID when calling a trigger, or InvalidOid (zero) for ordinary
1089 * function calls.
1091 static PLyProcedure *
1092 PLy_procedure_get(FunctionCallInfo fcinfo, Oid tgreloid)
1094 Oid fn_oid;
1095 HeapTuple procTup;
1096 char key[128];
1097 PyObject *plproc;
1098 PLyProcedure *proc = NULL;
1099 int rv;
1101 fn_oid = fcinfo->flinfo->fn_oid;
1102 procTup = SearchSysCache(PROCOID,
1103 ObjectIdGetDatum(fn_oid),
1104 0, 0, 0);
1105 if (!HeapTupleIsValid(procTup))
1106 elog(ERROR, "cache lookup failed for function %u", fn_oid);
1108 rv = snprintf(key, sizeof(key), "%u_%u", fn_oid, tgreloid);
1109 if (rv >= sizeof(key) || rv < 0)
1110 elog(ERROR, "key too long");
1112 plproc = PyDict_GetItemString(PLy_procedure_cache, key);
1114 if (plproc != NULL)
1116 Py_INCREF(plproc);
1117 if (!PyCObject_Check(plproc))
1118 elog(FATAL, "expected a PyCObject, didn't get one");
1120 proc = PyCObject_AsVoidPtr(plproc);
1121 if (proc->me != plproc)
1122 elog(FATAL, "proc->me != plproc");
1123 /* did we find an up-to-date cache entry? */
1124 if (proc->fn_xmin != HeapTupleHeaderGetXmin(procTup->t_data) ||
1125 !ItemPointerEquals(&proc->fn_tid, &procTup->t_self))
1127 Py_DECREF(plproc);
1128 proc = NULL;
1132 if (proc == NULL)
1133 proc = PLy_procedure_create(procTup, tgreloid, key);
1135 if (OidIsValid(tgreloid))
1138 * Input/output conversion for trigger tuples. Use the result
1139 * TypeInfo variable to store the tuple conversion info. We
1140 * do this over again on each call to cover the possibility that
1141 * the relation's tupdesc changed since the trigger was last called.
1142 * PLy_input_tuple_funcs and PLy_output_tuple_funcs are responsible
1143 * for not doing repetitive work.
1145 TriggerData *tdata = (TriggerData *) fcinfo->context;
1147 Assert(CALLED_AS_TRIGGER(fcinfo));
1148 PLy_input_tuple_funcs(&(proc->result), tdata->tg_relation->rd_att);
1149 PLy_output_tuple_funcs(&(proc->result), tdata->tg_relation->rd_att);
1152 ReleaseSysCache(procTup);
1154 return proc;
1157 static PLyProcedure *
1158 PLy_procedure_create(HeapTuple procTup, Oid tgreloid, char *key)
1160 char procName[NAMEDATALEN + 256];
1161 Form_pg_proc procStruct;
1162 PLyProcedure *volatile proc;
1163 char *volatile procSource = NULL;
1164 Datum prosrcdatum;
1165 bool isnull;
1166 int i,
1169 procStruct = (Form_pg_proc) GETSTRUCT(procTup);
1171 if (OidIsValid(tgreloid))
1172 rv = snprintf(procName, sizeof(procName),
1173 "__plpython_procedure_%s_%u_trigger_%u",
1174 NameStr(procStruct->proname),
1175 HeapTupleGetOid(procTup),
1176 tgreloid);
1177 else
1178 rv = snprintf(procName, sizeof(procName),
1179 "__plpython_procedure_%s_%u",
1180 NameStr(procStruct->proname),
1181 HeapTupleGetOid(procTup));
1182 if (rv >= sizeof(procName) || rv < 0)
1183 elog(ERROR, "procedure name would overrun buffer");
1185 proc = PLy_malloc(sizeof(PLyProcedure));
1186 proc->proname = PLy_strdup(NameStr(procStruct->proname));
1187 proc->pyname = PLy_strdup(procName);
1188 proc->fn_xmin = HeapTupleHeaderGetXmin(procTup->t_data);
1189 proc->fn_tid = procTup->t_self;
1190 /* Remember if function is STABLE/IMMUTABLE */
1191 proc->fn_readonly =
1192 (procStruct->provolatile != PROVOLATILE_VOLATILE);
1193 PLy_typeinfo_init(&proc->result);
1194 for (i = 0; i < FUNC_MAX_ARGS; i++)
1195 PLy_typeinfo_init(&proc->args[i]);
1196 proc->nargs = 0;
1197 proc->code = proc->statics = NULL;
1198 proc->globals = proc->me = NULL;
1199 proc->is_setof = procStruct->proretset;
1200 proc->setof = NULL;
1201 proc->argnames = NULL;
1203 PG_TRY();
1206 * get information required for output conversion of the return value,
1207 * but only if this isn't a trigger.
1209 if (!OidIsValid(tgreloid))
1211 HeapTuple rvTypeTup;
1212 Form_pg_type rvTypeStruct;
1214 rvTypeTup = SearchSysCache(TYPEOID,
1215 ObjectIdGetDatum(procStruct->prorettype),
1216 0, 0, 0);
1217 if (!HeapTupleIsValid(rvTypeTup))
1218 elog(ERROR, "cache lookup failed for type %u",
1219 procStruct->prorettype);
1220 rvTypeStruct = (Form_pg_type) GETSTRUCT(rvTypeTup);
1222 /* Disallow pseudotype result, except for void */
1223 if (rvTypeStruct->typtype == TYPTYPE_PSEUDO &&
1224 procStruct->prorettype != VOIDOID)
1226 if (procStruct->prorettype == TRIGGEROID)
1227 ereport(ERROR,
1228 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1229 errmsg("trigger functions can only be called as triggers")));
1230 else
1231 ereport(ERROR,
1232 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1233 errmsg("plpython functions cannot return type %s",
1234 format_type_be(procStruct->prorettype))));
1237 if (rvTypeStruct->typtype == TYPTYPE_COMPOSITE)
1240 * Tuple: set up later, during first call to
1241 * PLy_function_handler
1243 proc->result.out.d.typoid = procStruct->prorettype;
1244 proc->result.is_rowtype = 2;
1246 else
1247 PLy_output_datum_func(&proc->result, rvTypeTup);
1249 ReleaseSysCache(rvTypeTup);
1253 * Now get information required for input conversion of the
1254 * procedure's arguments. Note that we ignore output arguments
1255 * here --- since we don't support returning record, and that was
1256 * already checked above, there's no need to worry about multiple
1257 * output arguments.
1259 if (procStruct->pronargs)
1261 Oid *types;
1262 char **names,
1263 *modes;
1264 int i,
1265 pos,
1266 total;
1268 /* extract argument type info from the pg_proc tuple */
1269 total = get_func_arg_info(procTup, &types, &names, &modes);
1271 /* count number of in+inout args into proc->nargs */
1272 if (modes == NULL)
1273 proc->nargs = total;
1274 else
1276 /* proc->nargs was initialized to 0 above */
1277 for (i = 0; i < total; i++)
1279 if (modes[i] != PROARGMODE_OUT &&
1280 modes[i] != PROARGMODE_TABLE)
1281 (proc->nargs)++;
1285 proc->argnames = (char **) PLy_malloc0(sizeof(char *) * proc->nargs);
1286 for (i = pos = 0; i < total; i++)
1288 HeapTuple argTypeTup;
1289 Form_pg_type argTypeStruct;
1291 if (modes &&
1292 (modes[i] == PROARGMODE_OUT ||
1293 modes[i] == PROARGMODE_TABLE))
1294 continue; /* skip OUT arguments */
1296 Assert(types[i] == procStruct->proargtypes.values[pos]);
1298 argTypeTup = SearchSysCache(TYPEOID,
1299 ObjectIdGetDatum(types[i]),
1300 0, 0, 0);
1301 if (!HeapTupleIsValid(argTypeTup))
1302 elog(ERROR, "cache lookup failed for type %u", types[i]);
1303 argTypeStruct = (Form_pg_type) GETSTRUCT(argTypeTup);
1305 /* check argument type is OK, set up I/O function info */
1306 switch (argTypeStruct->typtype)
1308 case TYPTYPE_PSEUDO:
1309 /* Disallow pseudotype argument */
1310 ereport(ERROR,
1311 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1312 errmsg("plpython functions cannot take type %s",
1313 format_type_be(types[i]))));
1314 break;
1315 case TYPTYPE_COMPOSITE:
1316 /* we'll set IO funcs at first call */
1317 proc->args[pos].is_rowtype = 2;
1318 break;
1319 default:
1320 PLy_input_datum_func(&(proc->args[pos]),
1321 types[i],
1322 argTypeTup);
1323 break;
1326 /* get argument name */
1327 proc->argnames[pos] = names ? PLy_strdup(names[i]) : NULL;
1329 ReleaseSysCache(argTypeTup);
1331 pos++;
1336 * get the text of the function.
1338 prosrcdatum = SysCacheGetAttr(PROCOID, procTup,
1339 Anum_pg_proc_prosrc, &isnull);
1340 if (isnull)
1341 elog(ERROR, "null prosrc");
1342 procSource = TextDatumGetCString(prosrcdatum);
1344 PLy_procedure_compile(proc, procSource);
1346 pfree(procSource);
1348 proc->me = PyCObject_FromVoidPtr(proc, NULL);
1349 PyDict_SetItemString(PLy_procedure_cache, key, proc->me);
1351 PG_CATCH();
1353 PLy_procedure_delete(proc);
1354 if (procSource)
1355 pfree(procSource);
1357 PG_RE_THROW();
1359 PG_END_TRY();
1361 return proc;
1364 static void
1365 PLy_procedure_compile(PLyProcedure * proc, const char *src)
1367 PyObject *crv = NULL;
1368 char *msrc;
1370 proc->globals = PyDict_Copy(PLy_interp_globals);
1373 * SD is private preserved data between calls. GD is global data shared by
1374 * all functions
1376 proc->statics = PyDict_New();
1377 PyDict_SetItemString(proc->globals, "SD", proc->statics);
1380 * insert the function code into the interpreter
1382 msrc = PLy_procedure_munge_source(proc->pyname, src);
1383 crv = PyRun_String(msrc, Py_file_input, proc->globals, NULL);
1384 free(msrc);
1386 if (crv != NULL && (!PyErr_Occurred()))
1388 int clen;
1389 char call[NAMEDATALEN + 256];
1391 Py_DECREF(crv);
1394 * compile a call to the function
1396 clen = snprintf(call, sizeof(call), "%s()", proc->pyname);
1397 if (clen < 0 || clen >= sizeof(call))
1398 elog(ERROR, "string would overflow buffer");
1399 proc->code = Py_CompileString(call, "<string>", Py_eval_input);
1400 if (proc->code != NULL && (!PyErr_Occurred()))
1401 return;
1403 else
1404 Py_XDECREF(crv);
1406 PLy_elog(ERROR, "could not compile function \"%s\"", proc->proname);
1409 static char *
1410 PLy_procedure_munge_source(const char *name, const char *src)
1412 char *mrc,
1413 *mp;
1414 const char *sp;
1415 size_t mlen,
1416 plen;
1419 * room for function source and the def statement
1421 mlen = (strlen(src) * 2) + strlen(name) + 16;
1423 mrc = PLy_malloc(mlen);
1424 plen = snprintf(mrc, mlen, "def %s():\n\t", name);
1425 Assert(plen >= 0 && plen < mlen);
1427 sp = src;
1428 mp = mrc + plen;
1430 while (*sp != '\0')
1432 if (*sp == '\r' && *(sp + 1) == '\n')
1433 sp++;
1435 if (*sp == '\n' || *sp == '\r')
1437 *mp++ = '\n';
1438 *mp++ = '\t';
1439 sp++;
1441 else
1442 *mp++ = *sp++;
1444 *mp++ = '\n';
1445 *mp++ = '\n';
1446 *mp = '\0';
1448 if (mp > (mrc + mlen))
1449 elog(FATAL, "buffer overrun in PLy_munge_source");
1451 return mrc;
1454 static void
1455 PLy_procedure_delete(PLyProcedure * proc)
1457 int i;
1459 Py_XDECREF(proc->code);
1460 Py_XDECREF(proc->statics);
1461 Py_XDECREF(proc->globals);
1462 Py_XDECREF(proc->me);
1463 if (proc->proname)
1464 PLy_free(proc->proname);
1465 if (proc->pyname)
1466 PLy_free(proc->pyname);
1467 for (i = 0; i < proc->nargs; i++)
1469 if (proc->args[i].is_rowtype == 1)
1471 if (proc->args[i].in.r.atts)
1472 PLy_free(proc->args[i].in.r.atts);
1473 if (proc->args[i].out.r.atts)
1474 PLy_free(proc->args[i].out.r.atts);
1476 if (proc->argnames && proc->argnames[i])
1477 PLy_free(proc->argnames[i]);
1479 if (proc->argnames)
1480 PLy_free(proc->argnames);
1483 /* conversion functions. remember output from python is
1484 * input to postgresql, and vis versa.
1486 static void
1487 PLy_input_tuple_funcs(PLyTypeInfo * arg, TupleDesc desc)
1489 int i;
1491 if (arg->is_rowtype == 0)
1492 elog(ERROR, "PLyTypeInfo struct is initialized for a Datum");
1493 arg->is_rowtype = 1;
1495 if (arg->in.r.natts != desc->natts)
1497 if (arg->in.r.atts)
1498 PLy_free(arg->in.r.atts);
1499 arg->in.r.natts = desc->natts;
1500 arg->in.r.atts = PLy_malloc0(desc->natts * sizeof(PLyDatumToOb));
1503 for (i = 0; i < desc->natts; i++)
1505 HeapTuple typeTup;
1507 if (desc->attrs[i]->attisdropped)
1508 continue;
1510 if (arg->in.r.atts[i].typoid == desc->attrs[i]->atttypid)
1511 continue; /* already set up this entry */
1513 typeTup = SearchSysCache(TYPEOID,
1514 ObjectIdGetDatum(desc->attrs[i]->atttypid),
1515 0, 0, 0);
1516 if (!HeapTupleIsValid(typeTup))
1517 elog(ERROR, "cache lookup failed for type %u",
1518 desc->attrs[i]->atttypid);
1520 PLy_input_datum_func2(&(arg->in.r.atts[i]),
1521 desc->attrs[i]->atttypid,
1522 typeTup);
1524 ReleaseSysCache(typeTup);
1528 static void
1529 PLy_output_tuple_funcs(PLyTypeInfo * arg, TupleDesc desc)
1531 int i;
1533 if (arg->is_rowtype == 0)
1534 elog(ERROR, "PLyTypeInfo struct is initialized for a Datum");
1535 arg->is_rowtype = 1;
1537 if (arg->out.r.natts != desc->natts)
1539 if (arg->out.r.atts)
1540 PLy_free(arg->out.r.atts);
1541 arg->out.r.natts = desc->natts;
1542 arg->out.r.atts = PLy_malloc0(desc->natts * sizeof(PLyDatumToOb));
1545 for (i = 0; i < desc->natts; i++)
1547 HeapTuple typeTup;
1549 if (desc->attrs[i]->attisdropped)
1550 continue;
1552 if (arg->out.r.atts[i].typoid == desc->attrs[i]->atttypid)
1553 continue; /* already set up this entry */
1555 typeTup = SearchSysCache(TYPEOID,
1556 ObjectIdGetDatum(desc->attrs[i]->atttypid),
1557 0, 0, 0);
1558 if (!HeapTupleIsValid(typeTup))
1559 elog(ERROR, "cache lookup failed for type %u",
1560 desc->attrs[i]->atttypid);
1562 PLy_output_datum_func2(&(arg->out.r.atts[i]), typeTup);
1564 ReleaseSysCache(typeTup);
1568 static void
1569 PLy_output_datum_func(PLyTypeInfo * arg, HeapTuple typeTup)
1571 if (arg->is_rowtype > 0)
1572 elog(ERROR, "PLyTypeInfo struct is initialized for a Tuple");
1573 arg->is_rowtype = 0;
1574 PLy_output_datum_func2(&(arg->out.d), typeTup);
1577 static void
1578 PLy_output_datum_func2(PLyObToDatum * arg, HeapTuple typeTup)
1580 Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1582 perm_fmgr_info(typeStruct->typinput, &arg->typfunc);
1583 arg->typoid = HeapTupleGetOid(typeTup);
1584 arg->typioparam = getTypeIOParam(typeTup);
1585 arg->typbyval = typeStruct->typbyval;
1588 static void
1589 PLy_input_datum_func(PLyTypeInfo * arg, Oid typeOid, HeapTuple typeTup)
1591 if (arg->is_rowtype > 0)
1592 elog(ERROR, "PLyTypeInfo struct is initialized for Tuple");
1593 arg->is_rowtype = 0;
1594 PLy_input_datum_func2(&(arg->in.d), typeOid, typeTup);
1597 static void
1598 PLy_input_datum_func2(PLyDatumToOb * arg, Oid typeOid, HeapTuple typeTup)
1600 Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1602 /* Get the type's conversion information */
1603 perm_fmgr_info(typeStruct->typoutput, &arg->typfunc);
1604 arg->typoid = HeapTupleGetOid(typeTup);
1605 arg->typioparam = getTypeIOParam(typeTup);
1606 arg->typbyval = typeStruct->typbyval;
1608 /* Determine which kind of Python object we will convert to */
1609 switch (typeOid)
1611 case BOOLOID:
1612 arg->func = PLyBool_FromString;
1613 break;
1614 case FLOAT4OID:
1615 case FLOAT8OID:
1616 case NUMERICOID:
1617 arg->func = PLyFloat_FromString;
1618 break;
1619 case INT2OID:
1620 case INT4OID:
1621 arg->func = PLyInt_FromString;
1622 break;
1623 case INT8OID:
1624 arg->func = PLyLong_FromString;
1625 break;
1626 default:
1627 arg->func = PLyString_FromString;
1628 break;
1632 static void
1633 PLy_typeinfo_init(PLyTypeInfo * arg)
1635 arg->is_rowtype = -1;
1636 arg->in.r.natts = arg->out.r.natts = 0;
1637 arg->in.r.atts = NULL;
1638 arg->out.r.atts = NULL;
1641 static void
1642 PLy_typeinfo_dealloc(PLyTypeInfo * arg)
1644 if (arg->is_rowtype == 1)
1646 if (arg->in.r.atts)
1647 PLy_free(arg->in.r.atts);
1648 if (arg->out.r.atts)
1649 PLy_free(arg->out.r.atts);
1653 /* assumes that a bool is always returned as a 't' or 'f' */
1654 static PyObject *
1655 PLyBool_FromString(const char *src)
1658 * We would like to use Py_RETURN_TRUE and Py_RETURN_FALSE here for
1659 * generating SQL from trigger functions, but those are only supported in
1660 * Python >= 2.3, and we support older versions.
1661 * http://docs.python.org/api/boolObjects.html
1663 if (src[0] == 't')
1664 return PyBool_FromLong(1);
1665 return PyBool_FromLong(0);
1668 static PyObject *
1669 PLyFloat_FromString(const char *src)
1671 double v;
1672 char *eptr;
1674 errno = 0;
1675 v = strtod(src, &eptr);
1676 if (*eptr != '\0' || errno)
1677 return NULL;
1678 return PyFloat_FromDouble(v);
1681 static PyObject *
1682 PLyInt_FromString(const char *src)
1684 long v;
1685 char *eptr;
1687 errno = 0;
1688 v = strtol(src, &eptr, 0);
1689 if (*eptr != '\0' || errno)
1690 return NULL;
1691 return PyInt_FromLong(v);
1694 static PyObject *
1695 PLyLong_FromString(const char *src)
1697 return PyLong_FromString((char *) src, NULL, 0);
1700 static PyObject *
1701 PLyString_FromString(const char *src)
1703 return PyString_FromString(src);
1706 static PyObject *
1707 PLyDict_FromTuple(PLyTypeInfo * info, HeapTuple tuple, TupleDesc desc)
1709 PyObject *volatile dict;
1710 int i;
1712 if (info->is_rowtype != 1)
1713 elog(ERROR, "PLyTypeInfo structure describes a datum");
1715 dict = PyDict_New();
1716 if (dict == NULL)
1717 PLy_elog(ERROR, "could not create tuple dictionary");
1719 PG_TRY();
1721 for (i = 0; i < info->in.r.natts; i++)
1723 char *key,
1724 *vsrc;
1725 Datum vattr;
1726 bool is_null;
1727 PyObject *value;
1729 if (desc->attrs[i]->attisdropped)
1730 continue;
1732 key = NameStr(desc->attrs[i]->attname);
1733 vattr = heap_getattr(tuple, (i + 1), desc, &is_null);
1735 if (is_null || info->in.r.atts[i].func == NULL)
1736 PyDict_SetItemString(dict, key, Py_None);
1737 else
1739 vsrc = OutputFunctionCall(&info->in.r.atts[i].typfunc,
1740 vattr);
1743 * no exceptions allowed
1745 value = info->in.r.atts[i].func(vsrc);
1746 pfree(vsrc);
1747 PyDict_SetItemString(dict, key, value);
1748 Py_DECREF(value);
1752 PG_CATCH();
1754 Py_DECREF(dict);
1755 PG_RE_THROW();
1757 PG_END_TRY();
1759 return dict;
1763 static HeapTuple
1764 PLyMapping_ToTuple(PLyTypeInfo * info, PyObject * mapping)
1766 TupleDesc desc;
1767 HeapTuple tuple;
1768 Datum *values;
1769 bool *nulls;
1770 volatile int i;
1772 Assert(PyMapping_Check(mapping));
1774 desc = lookup_rowtype_tupdesc(info->out.d.typoid, -1);
1775 if (info->is_rowtype == 2)
1776 PLy_output_tuple_funcs(info, desc);
1777 Assert(info->is_rowtype == 1);
1779 /* Build tuple */
1780 values = palloc(sizeof(Datum) * desc->natts);
1781 nulls = palloc(sizeof(bool) * desc->natts);
1782 for (i = 0; i < desc->natts; ++i)
1784 char *key;
1785 PyObject *volatile value,
1786 *volatile so;
1788 key = NameStr(desc->attrs[i]->attname);
1789 value = so = NULL;
1790 PG_TRY();
1792 value = PyMapping_GetItemString(mapping, key);
1793 if (value == Py_None)
1795 values[i] = (Datum) NULL;
1796 nulls[i] = true;
1798 else if (value)
1800 char *valuestr;
1802 so = PyObject_Str(value);
1803 if (so == NULL)
1804 PLy_elog(ERROR, "cannot convert mapping type");
1805 valuestr = PyString_AsString(so);
1807 values[i] = InputFunctionCall(&info->out.r.atts[i].typfunc
1808 ,valuestr
1809 ,info->out.r.atts[i].typioparam
1810 ,-1);
1811 Py_DECREF(so);
1812 so = NULL;
1813 nulls[i] = false;
1815 else
1816 ereport(ERROR,
1817 (errcode(ERRCODE_UNDEFINED_COLUMN),
1818 errmsg("no mapping found with key \"%s\"", key),
1819 errhint("to return null in specific column, "
1820 "add value None to map with key named after column")));
1822 Py_XDECREF(value);
1823 value = NULL;
1825 PG_CATCH();
1827 Py_XDECREF(so);
1828 Py_XDECREF(value);
1829 PG_RE_THROW();
1831 PG_END_TRY();
1834 tuple = heap_form_tuple(desc, values, nulls);
1835 ReleaseTupleDesc(desc);
1836 pfree(values);
1837 pfree(nulls);
1839 return tuple;
1843 static HeapTuple
1844 PLySequence_ToTuple(PLyTypeInfo * info, PyObject * sequence)
1846 TupleDesc desc;
1847 HeapTuple tuple;
1848 Datum *values;
1849 bool *nulls;
1850 volatile int i;
1852 Assert(PySequence_Check(sequence));
1855 * Check that sequence length is exactly same as PG tuple's. We actually
1856 * can ignore exceeding items or assume missing ones as null but to avoid
1857 * plpython developer's errors we are strict here
1859 desc = lookup_rowtype_tupdesc(info->out.d.typoid, -1);
1860 if (PySequence_Length(sequence) != desc->natts)
1861 ereport(ERROR,
1862 (errcode(ERRCODE_DATATYPE_MISMATCH),
1863 errmsg("returned sequence's length must be same as tuple's length")));
1865 if (info->is_rowtype == 2)
1866 PLy_output_tuple_funcs(info, desc);
1867 Assert(info->is_rowtype == 1);
1869 /* Build tuple */
1870 values = palloc(sizeof(Datum) * desc->natts);
1871 nulls = palloc(sizeof(bool) * desc->natts);
1872 for (i = 0; i < desc->natts; ++i)
1874 PyObject *volatile value,
1875 *volatile so;
1877 value = so = NULL;
1878 PG_TRY();
1880 value = PySequence_GetItem(sequence, i);
1881 Assert(value);
1882 if (value == Py_None)
1884 values[i] = (Datum) NULL;
1885 nulls[i] = true;
1887 else if (value)
1889 char *valuestr;
1891 so = PyObject_Str(value);
1892 if (so == NULL)
1893 PLy_elog(ERROR, "cannot convert sequence type");
1894 valuestr = PyString_AsString(so);
1895 values[i] = InputFunctionCall(&info->out.r.atts[i].typfunc
1896 ,valuestr
1897 ,info->out.r.atts[i].typioparam
1898 ,-1);
1899 Py_DECREF(so);
1900 so = NULL;
1901 nulls[i] = false;
1904 Py_XDECREF(value);
1905 value = NULL;
1907 PG_CATCH();
1909 Py_XDECREF(so);
1910 Py_XDECREF(value);
1911 PG_RE_THROW();
1913 PG_END_TRY();
1916 tuple = heap_form_tuple(desc, values, nulls);
1917 ReleaseTupleDesc(desc);
1918 pfree(values);
1919 pfree(nulls);
1921 return tuple;
1925 static HeapTuple
1926 PLyObject_ToTuple(PLyTypeInfo * info, PyObject * object)
1928 TupleDesc desc;
1929 HeapTuple tuple;
1930 Datum *values;
1931 bool *nulls;
1932 volatile int i;
1934 desc = lookup_rowtype_tupdesc(info->out.d.typoid, -1);
1935 if (info->is_rowtype == 2)
1936 PLy_output_tuple_funcs(info, desc);
1937 Assert(info->is_rowtype == 1);
1939 /* Build tuple */
1940 values = palloc(sizeof(Datum) * desc->natts);
1941 nulls = palloc(sizeof(bool) * desc->natts);
1942 for (i = 0; i < desc->natts; ++i)
1944 char *key;
1945 PyObject *volatile value,
1946 *volatile so;
1948 key = NameStr(desc->attrs[i]->attname);
1949 value = so = NULL;
1950 PG_TRY();
1952 value = PyObject_GetAttrString(object, key);
1953 if (value == Py_None)
1955 values[i] = (Datum) NULL;
1956 nulls[i] = true;
1958 else if (value)
1960 char *valuestr;
1962 so = PyObject_Str(value);
1963 if (so == NULL)
1964 PLy_elog(ERROR, "cannot convert object type");
1965 valuestr = PyString_AsString(so);
1966 values[i] = InputFunctionCall(&info->out.r.atts[i].typfunc
1967 ,valuestr
1968 ,info->out.r.atts[i].typioparam
1969 ,-1);
1970 Py_DECREF(so);
1971 so = NULL;
1972 nulls[i] = false;
1974 else
1975 ereport(ERROR,
1976 (errcode(ERRCODE_UNDEFINED_COLUMN),
1977 errmsg("no attribute named \"%s\"", key),
1978 errhint("to return null in specific column, "
1979 "let returned object to have attribute named "
1980 "after column with value None")));
1982 Py_XDECREF(value);
1983 value = NULL;
1985 PG_CATCH();
1987 Py_XDECREF(so);
1988 Py_XDECREF(value);
1989 PG_RE_THROW();
1991 PG_END_TRY();
1994 tuple = heap_form_tuple(desc, values, nulls);
1995 ReleaseTupleDesc(desc);
1996 pfree(values);
1997 pfree(nulls);
1999 return tuple;
2003 /* initialization, some python variables function declared here */
2005 /* interface to postgresql elog */
2006 static PyObject *PLy_debug(PyObject *, PyObject *);
2007 static PyObject *PLy_log(PyObject *, PyObject *);
2008 static PyObject *PLy_info(PyObject *, PyObject *);
2009 static PyObject *PLy_notice(PyObject *, PyObject *);
2010 static PyObject *PLy_warning(PyObject *, PyObject *);
2011 static PyObject *PLy_error(PyObject *, PyObject *);
2012 static PyObject *PLy_fatal(PyObject *, PyObject *);
2014 /* PLyPlanObject, PLyResultObject and SPI interface */
2015 #define is_PLyPlanObject(x) ((x)->ob_type == &PLy_PlanType)
2016 static PyObject *PLy_plan_new(void);
2017 static void PLy_plan_dealloc(PyObject *);
2018 static PyObject *PLy_plan_getattr(PyObject *, char *);
2019 static PyObject *PLy_plan_status(PyObject *, PyObject *);
2021 static PyObject *PLy_result_new(void);
2022 static void PLy_result_dealloc(PyObject *);
2023 static PyObject *PLy_result_getattr(PyObject *, char *);
2024 static PyObject *PLy_result_nrows(PyObject *, PyObject *);
2025 static PyObject *PLy_result_status(PyObject *, PyObject *);
2026 static Py_ssize_t PLy_result_length(PyObject *);
2027 static PyObject *PLy_result_item(PyObject *, Py_ssize_t);
2028 static PyObject *PLy_result_slice(PyObject *, Py_ssize_t, Py_ssize_t);
2029 static int PLy_result_ass_item(PyObject *, Py_ssize_t, PyObject *);
2030 static int PLy_result_ass_slice(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
2033 static PyObject *PLy_spi_prepare(PyObject *, PyObject *);
2034 static PyObject *PLy_spi_execute(PyObject *, PyObject *);
2035 static PyObject *PLy_spi_execute_query(char *query, long limit);
2036 static PyObject *PLy_spi_execute_plan(PyObject *, PyObject *, long);
2037 static PyObject *PLy_spi_execute_fetch_result(SPITupleTable *, int, int);
2040 static PyTypeObject PLy_PlanType = {
2041 PyObject_HEAD_INIT(NULL)
2042 0, /* ob_size */
2043 "PLyPlan", /* tp_name */
2044 sizeof(PLyPlanObject), /* tp_size */
2045 0, /* tp_itemsize */
2048 * methods
2050 PLy_plan_dealloc, /* tp_dealloc */
2051 0, /* tp_print */
2052 PLy_plan_getattr, /* tp_getattr */
2053 0, /* tp_setattr */
2054 0, /* tp_compare */
2055 0, /* tp_repr */
2056 0, /* tp_as_number */
2057 0, /* tp_as_sequence */
2058 0, /* tp_as_mapping */
2059 0, /* tp_hash */
2060 0, /* tp_call */
2061 0, /* tp_str */
2062 0, /* tp_getattro */
2063 0, /* tp_setattro */
2064 0, /* tp_as_buffer */
2065 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2066 PLy_plan_doc, /* tp_doc */
2069 static PyMethodDef PLy_plan_methods[] = {
2070 {"status", PLy_plan_status, METH_VARARGS, NULL},
2071 {NULL, NULL, 0, NULL}
2074 static PySequenceMethods PLy_result_as_sequence = {
2075 PLy_result_length, /* sq_length */
2076 NULL, /* sq_concat */
2077 NULL, /* sq_repeat */
2078 PLy_result_item, /* sq_item */
2079 PLy_result_slice, /* sq_slice */
2080 PLy_result_ass_item, /* sq_ass_item */
2081 PLy_result_ass_slice, /* sq_ass_slice */
2084 static PyTypeObject PLy_ResultType = {
2085 PyObject_HEAD_INIT(NULL)
2086 0, /* ob_size */
2087 "PLyResult", /* tp_name */
2088 sizeof(PLyResultObject), /* tp_size */
2089 0, /* tp_itemsize */
2092 * methods
2094 PLy_result_dealloc, /* tp_dealloc */
2095 0, /* tp_print */
2096 PLy_result_getattr, /* tp_getattr */
2097 0, /* tp_setattr */
2098 0, /* tp_compare */
2099 0, /* tp_repr */
2100 0, /* tp_as_number */
2101 &PLy_result_as_sequence, /* tp_as_sequence */
2102 0, /* tp_as_mapping */
2103 0, /* tp_hash */
2104 0, /* tp_call */
2105 0, /* tp_str */
2106 0, /* tp_getattro */
2107 0, /* tp_setattro */
2108 0, /* tp_as_buffer */
2109 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2110 PLy_result_doc, /* tp_doc */
2113 static PyMethodDef PLy_result_methods[] = {
2114 {"nrows", PLy_result_nrows, METH_VARARGS, NULL},
2115 {"status", PLy_result_status, METH_VARARGS, NULL},
2116 {NULL, NULL, 0, NULL}
2119 static PyMethodDef PLy_methods[] = {
2121 * logging methods
2123 {"debug", PLy_debug, METH_VARARGS, NULL},
2124 {"log", PLy_log, METH_VARARGS, NULL},
2125 {"info", PLy_info, METH_VARARGS, NULL},
2126 {"notice", PLy_notice, METH_VARARGS, NULL},
2127 {"warning", PLy_warning, METH_VARARGS, NULL},
2128 {"error", PLy_error, METH_VARARGS, NULL},
2129 {"fatal", PLy_fatal, METH_VARARGS, NULL},
2132 * create a stored plan
2134 {"prepare", PLy_spi_prepare, METH_VARARGS, NULL},
2137 * execute a plan or query
2139 {"execute", PLy_spi_execute, METH_VARARGS, NULL},
2141 {NULL, NULL, 0, NULL}
2145 /* plan object methods */
2146 static PyObject *
2147 PLy_plan_new(void)
2149 PLyPlanObject *ob;
2151 if ((ob = PyObject_NEW(PLyPlanObject, &PLy_PlanType)) == NULL)
2152 return NULL;
2154 ob->plan = NULL;
2155 ob->nargs = 0;
2156 ob->types = NULL;
2157 ob->args = NULL;
2159 return (PyObject *) ob;
2163 static void
2164 PLy_plan_dealloc(PyObject * arg)
2166 PLyPlanObject *ob = (PLyPlanObject *) arg;
2168 if (ob->plan)
2169 SPI_freeplan(ob->plan);
2170 if (ob->types)
2171 PLy_free(ob->types);
2172 if (ob->args)
2174 int i;
2176 for (i = 0; i < ob->nargs; i++)
2177 PLy_typeinfo_dealloc(&ob->args[i]);
2178 PLy_free(ob->args);
2181 arg->ob_type->tp_free(arg);
2185 static PyObject *
2186 PLy_plan_getattr(PyObject * self, char *name)
2188 return Py_FindMethod(PLy_plan_methods, self, name);
2191 static PyObject *
2192 PLy_plan_status(PyObject * self, PyObject * args)
2194 if (PyArg_ParseTuple(args, ""))
2196 Py_INCREF(Py_True);
2197 return Py_True;
2198 /* return PyInt_FromLong(self->status); */
2200 PyErr_SetString(PLy_exc_error, "plan.status() takes no arguments");
2201 return NULL;
2206 /* result object methods */
2208 static PyObject *
2209 PLy_result_new(void)
2211 PLyResultObject *ob;
2213 if ((ob = PyObject_NEW(PLyResultObject, &PLy_ResultType)) == NULL)
2214 return NULL;
2216 /* ob->tuples = NULL; */
2218 Py_INCREF(Py_None);
2219 ob->status = Py_None;
2220 ob->nrows = PyInt_FromLong(-1);
2221 ob->rows = PyList_New(0);
2223 return (PyObject *) ob;
2226 static void
2227 PLy_result_dealloc(PyObject * arg)
2229 PLyResultObject *ob = (PLyResultObject *) arg;
2231 Py_XDECREF(ob->nrows);
2232 Py_XDECREF(ob->rows);
2233 Py_XDECREF(ob->status);
2235 arg->ob_type->tp_free(arg);
2238 static PyObject *
2239 PLy_result_getattr(PyObject * self, char *name)
2241 return Py_FindMethod(PLy_result_methods, self, name);
2244 static PyObject *
2245 PLy_result_nrows(PyObject * self, PyObject * args)
2247 PLyResultObject *ob = (PLyResultObject *) self;
2249 Py_INCREF(ob->nrows);
2250 return ob->nrows;
2253 static PyObject *
2254 PLy_result_status(PyObject * self, PyObject * args)
2256 PLyResultObject *ob = (PLyResultObject *) self;
2258 Py_INCREF(ob->status);
2259 return ob->status;
2262 static Py_ssize_t
2263 PLy_result_length(PyObject * arg)
2265 PLyResultObject *ob = (PLyResultObject *) arg;
2267 return PyList_Size(ob->rows);
2270 static PyObject *
2271 PLy_result_item(PyObject * arg, Py_ssize_t idx)
2273 PyObject *rv;
2274 PLyResultObject *ob = (PLyResultObject *) arg;
2276 rv = PyList_GetItem(ob->rows, idx);
2277 if (rv != NULL)
2278 Py_INCREF(rv);
2279 return rv;
2282 static int
2283 PLy_result_ass_item(PyObject * arg, Py_ssize_t idx, PyObject * item)
2285 int rv;
2286 PLyResultObject *ob = (PLyResultObject *) arg;
2288 Py_INCREF(item);
2289 rv = PyList_SetItem(ob->rows, idx, item);
2290 return rv;
2293 static PyObject *
2294 PLy_result_slice(PyObject * arg, Py_ssize_t lidx, Py_ssize_t hidx)
2296 PyObject *rv;
2297 PLyResultObject *ob = (PLyResultObject *) arg;
2299 rv = PyList_GetSlice(ob->rows, lidx, hidx);
2300 if (rv == NULL)
2301 return NULL;
2302 Py_INCREF(rv);
2303 return rv;
2306 static int
2307 PLy_result_ass_slice(PyObject * arg, Py_ssize_t lidx, Py_ssize_t hidx, PyObject * slice)
2309 int rv;
2310 PLyResultObject *ob = (PLyResultObject *) arg;
2312 rv = PyList_SetSlice(ob->rows, lidx, hidx, slice);
2313 return rv;
2316 /* SPI interface */
2317 static PyObject *
2318 PLy_spi_prepare(PyObject * self, PyObject * args)
2320 PLyPlanObject *plan;
2321 PyObject *list = NULL;
2322 PyObject *volatile optr = NULL;
2323 char *query;
2324 void *tmpplan;
2325 MemoryContext oldcontext;
2327 /* Can't execute more if we have an unhandled error */
2328 if (PLy_error_in_progress)
2330 PyErr_SetString(PLy_exc_error, "Transaction aborted.");
2331 return NULL;
2334 if (!PyArg_ParseTuple(args, "s|O", &query, &list))
2336 PyErr_SetString(PLy_exc_spi_error,
2337 "Invalid arguments for plpy.prepare()");
2338 return NULL;
2341 if (list && (!PySequence_Check(list)))
2343 PyErr_SetString(PLy_exc_spi_error,
2344 "Second argument in plpy.prepare() must be a sequence");
2345 return NULL;
2348 if ((plan = (PLyPlanObject *) PLy_plan_new()) == NULL)
2349 return NULL;
2351 oldcontext = CurrentMemoryContext;
2352 PG_TRY();
2354 if (list != NULL)
2356 int nargs,
2359 nargs = PySequence_Length(list);
2360 if (nargs > 0)
2362 plan->nargs = nargs;
2363 plan->types = PLy_malloc(sizeof(Oid) * nargs);
2364 plan->values = PLy_malloc(sizeof(Datum) * nargs);
2365 plan->args = PLy_malloc(sizeof(PLyTypeInfo) * nargs);
2368 * the other loop might throw an exception, if PLyTypeInfo
2369 * member isn't properly initialized the Py_DECREF(plan) will
2370 * go boom
2372 for (i = 0; i < nargs; i++)
2374 PLy_typeinfo_init(&plan->args[i]);
2375 plan->values[i] = PointerGetDatum(NULL);
2378 for (i = 0; i < nargs; i++)
2380 char *sptr;
2381 HeapTuple typeTup;
2382 Oid typeId;
2383 int32 typmod;
2384 Form_pg_type typeStruct;
2386 optr = PySequence_GetItem(list, i);
2387 if (!PyString_Check(optr))
2388 elog(ERROR, "Type names must be strings.");
2389 sptr = PyString_AsString(optr);
2391 /********************************************************
2392 * Resolve argument type names and then look them up by
2393 * oid in the system cache, and remember the required
2394 *information for input conversion.
2395 ********************************************************/
2397 parseTypeString(sptr, &typeId, &typmod);
2399 typeTup = SearchSysCache(TYPEOID,
2400 ObjectIdGetDatum(typeId),
2401 0, 0, 0);
2402 if (!HeapTupleIsValid(typeTup))
2403 elog(ERROR, "cache lookup failed for type %u", typeId);
2405 Py_DECREF(optr);
2406 optr = NULL; /* this is important */
2408 plan->types[i] = typeId;
2409 typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
2410 if (typeStruct->typtype != TYPTYPE_COMPOSITE)
2411 PLy_output_datum_func(&plan->args[i], typeTup);
2412 else
2413 elog(ERROR, "tuples not handled in plpy.prepare, yet.");
2414 ReleaseSysCache(typeTup);
2419 plan->plan = SPI_prepare(query, plan->nargs, plan->types);
2420 if (plan->plan == NULL)
2421 elog(ERROR, "SPI_prepare failed: %s",
2422 SPI_result_code_string(SPI_result));
2424 /* transfer plan from procCxt to topCxt */
2425 tmpplan = plan->plan;
2426 plan->plan = SPI_saveplan(tmpplan);
2427 SPI_freeplan(tmpplan);
2428 if (plan->plan == NULL)
2429 elog(ERROR, "SPI_saveplan failed: %s",
2430 SPI_result_code_string(SPI_result));
2432 PG_CATCH();
2434 MemoryContextSwitchTo(oldcontext);
2435 PLy_error_in_progress = CopyErrorData();
2436 FlushErrorState();
2437 Py_DECREF(plan);
2438 Py_XDECREF(optr);
2439 if (!PyErr_Occurred())
2440 PyErr_SetString(PLy_exc_spi_error,
2441 "Unknown error in PLy_spi_prepare");
2442 /* XXX this oughta be replaced with errcontext mechanism */
2443 PLy_elog(WARNING, "in function %s:",
2444 PLy_procedure_name(PLy_curr_procedure));
2445 return NULL;
2447 PG_END_TRY();
2449 return (PyObject *) plan;
2452 /* execute(query="select * from foo", limit=5)
2453 * execute(plan=plan, values=(foo, bar), limit=5)
2455 static PyObject *
2456 PLy_spi_execute(PyObject * self, PyObject * args)
2458 char *query;
2459 PyObject *plan;
2460 PyObject *list = NULL;
2461 long limit = 0;
2463 /* Can't execute more if we have an unhandled error */
2464 if (PLy_error_in_progress)
2466 PyErr_SetString(PLy_exc_error, "Transaction aborted.");
2467 return NULL;
2470 if (PyArg_ParseTuple(args, "s|l", &query, &limit))
2471 return PLy_spi_execute_query(query, limit);
2473 PyErr_Clear();
2475 if (PyArg_ParseTuple(args, "O|Ol", &plan, &list, &limit) &&
2476 is_PLyPlanObject(plan))
2477 return PLy_spi_execute_plan(plan, list, limit);
2479 PyErr_SetString(PLy_exc_error, "Expected a query or plan.");
2480 return NULL;
2483 static PyObject *
2484 PLy_spi_execute_plan(PyObject * ob, PyObject * list, long limit)
2486 volatile int nargs;
2487 int i,
2489 PLyPlanObject *plan;
2490 MemoryContext oldcontext;
2492 if (list != NULL)
2494 if (!PySequence_Check(list) || PyString_Check(list))
2496 char *msg = "plpy.execute() takes a sequence as its second argument";
2498 PyErr_SetString(PLy_exc_spi_error, msg);
2499 return NULL;
2501 nargs = PySequence_Length(list);
2503 else
2504 nargs = 0;
2506 plan = (PLyPlanObject *) ob;
2508 if (nargs != plan->nargs)
2510 char *sv;
2511 PyObject *so = PyObject_Str(list);
2513 if (!so)
2514 PLy_elog(ERROR, "function \"%s\" could not execute plan",
2515 PLy_procedure_name(PLy_curr_procedure));
2516 sv = PyString_AsString(so);
2517 PLy_exception_set(PLy_exc_spi_error,
2518 "Expected sequence of %d arguments, got %d. %s",
2519 plan->nargs, nargs, sv);
2520 Py_DECREF(so);
2522 return NULL;
2525 oldcontext = CurrentMemoryContext;
2526 PG_TRY();
2528 char *nulls = palloc(nargs * sizeof(char));
2529 volatile int j;
2531 for (j = 0; j < nargs; j++)
2533 PyObject *elem,
2534 *so;
2536 elem = PySequence_GetItem(list, j);
2537 if (elem != Py_None)
2539 so = PyObject_Str(elem);
2540 if (!so)
2541 PLy_elog(ERROR, "function \"%s\" could not execute plan",
2542 PLy_procedure_name(PLy_curr_procedure));
2543 Py_DECREF(elem);
2545 PG_TRY();
2547 char *sv = PyString_AsString(so);
2549 plan->values[j] =
2550 InputFunctionCall(&(plan->args[j].out.d.typfunc),
2552 plan->args[j].out.d.typioparam,
2553 -1);
2555 PG_CATCH();
2557 Py_DECREF(so);
2558 PG_RE_THROW();
2560 PG_END_TRY();
2562 Py_DECREF(so);
2563 nulls[j] = ' ';
2565 else
2567 Py_DECREF(elem);
2568 plan->values[j] =
2569 InputFunctionCall(&(plan->args[j].out.d.typfunc),
2570 NULL,
2571 plan->args[j].out.d.typioparam,
2572 -1);
2573 nulls[j] = 'n';
2577 rv = SPI_execute_plan(plan->plan, plan->values, nulls,
2578 PLy_curr_procedure->fn_readonly, limit);
2580 pfree(nulls);
2582 PG_CATCH();
2584 int k;
2586 MemoryContextSwitchTo(oldcontext);
2587 PLy_error_in_progress = CopyErrorData();
2588 FlushErrorState();
2591 * cleanup plan->values array
2593 for (k = 0; k < nargs; k++)
2595 if (!plan->args[k].out.d.typbyval &&
2596 (plan->values[k] != PointerGetDatum(NULL)))
2598 pfree(DatumGetPointer(plan->values[k]));
2599 plan->values[k] = PointerGetDatum(NULL);
2603 if (!PyErr_Occurred())
2604 PyErr_SetString(PLy_exc_error,
2605 "Unknown error in PLy_spi_execute_plan");
2606 /* XXX this oughta be replaced with errcontext mechanism */
2607 PLy_elog(WARNING, "in function %s:",
2608 PLy_procedure_name(PLy_curr_procedure));
2609 return NULL;
2611 PG_END_TRY();
2613 for (i = 0; i < nargs; i++)
2615 if (!plan->args[i].out.d.typbyval &&
2616 (plan->values[i] != PointerGetDatum(NULL)))
2618 pfree(DatumGetPointer(plan->values[i]));
2619 plan->values[i] = PointerGetDatum(NULL);
2623 if (rv < 0)
2625 PLy_exception_set(PLy_exc_spi_error,
2626 "SPI_execute_plan failed: %s",
2627 SPI_result_code_string(rv));
2628 return NULL;
2631 return PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
2634 static PyObject *
2635 PLy_spi_execute_query(char *query, long limit)
2637 int rv;
2638 MemoryContext oldcontext;
2640 oldcontext = CurrentMemoryContext;
2641 PG_TRY();
2643 rv = SPI_execute(query, PLy_curr_procedure->fn_readonly, limit);
2645 PG_CATCH();
2647 MemoryContextSwitchTo(oldcontext);
2648 PLy_error_in_progress = CopyErrorData();
2649 FlushErrorState();
2650 if (!PyErr_Occurred())
2651 PyErr_SetString(PLy_exc_spi_error,
2652 "Unknown error in PLy_spi_execute_query");
2653 /* XXX this oughta be replaced with errcontext mechanism */
2654 PLy_elog(WARNING, "in function %s:",
2655 PLy_procedure_name(PLy_curr_procedure));
2656 return NULL;
2658 PG_END_TRY();
2660 if (rv < 0)
2662 PLy_exception_set(PLy_exc_spi_error,
2663 "SPI_execute failed: %s",
2664 SPI_result_code_string(rv));
2665 return NULL;
2668 return PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
2671 static PyObject *
2672 PLy_spi_execute_fetch_result(SPITupleTable *tuptable, int rows, int status)
2674 PLyResultObject *result;
2675 MemoryContext oldcontext;
2677 result = (PLyResultObject *) PLy_result_new();
2678 Py_DECREF(result->status);
2679 result->status = PyInt_FromLong(status);
2681 if (status > 0 && tuptable == NULL)
2683 Py_DECREF(result->nrows);
2684 result->nrows = PyInt_FromLong(rows);
2686 else if (status > 0 && tuptable != NULL)
2688 PLyTypeInfo args;
2689 int i;
2691 Py_DECREF(result->nrows);
2692 result->nrows = PyInt_FromLong(rows);
2693 PLy_typeinfo_init(&args);
2695 oldcontext = CurrentMemoryContext;
2696 PG_TRY();
2698 if (rows)
2700 Py_DECREF(result->rows);
2701 result->rows = PyList_New(rows);
2703 PLy_input_tuple_funcs(&args, tuptable->tupdesc);
2704 for (i = 0; i < rows; i++)
2706 PyObject *row = PLyDict_FromTuple(&args, tuptable->vals[i],
2707 tuptable->tupdesc);
2709 PyList_SetItem(result->rows, i, row);
2711 PLy_typeinfo_dealloc(&args);
2713 SPI_freetuptable(tuptable);
2716 PG_CATCH();
2718 MemoryContextSwitchTo(oldcontext);
2719 PLy_error_in_progress = CopyErrorData();
2720 FlushErrorState();
2721 if (!PyErr_Occurred())
2722 PyErr_SetString(PLy_exc_error,
2723 "Unknown error in PLy_spi_execute_fetch_result");
2724 Py_DECREF(result);
2725 PLy_typeinfo_dealloc(&args);
2726 return NULL;
2728 PG_END_TRY();
2731 return (PyObject *) result;
2736 * language handler and interpreter initialization
2740 * _PG_init() - library load-time initialization
2742 * DO NOT make this static nor change its name!
2744 void
2745 _PG_init(void)
2747 /* Be sure we do initialization only once (should be redundant now) */
2748 static bool inited = false;
2750 if (inited)
2751 return;
2753 set_text_domain(TEXTDOMAIN);
2755 Py_Initialize();
2756 PLy_init_interp();
2757 PLy_init_plpy();
2758 if (PyErr_Occurred())
2759 PLy_elog(FATAL, "untrapped error in initialization");
2760 PLy_procedure_cache = PyDict_New();
2761 if (PLy_procedure_cache == NULL)
2762 PLy_elog(ERROR, "could not create procedure cache");
2764 inited = true;
2767 static void
2768 PLy_init_interp(void)
2770 PyObject *mainmod;
2772 mainmod = PyImport_AddModule("__main__");
2773 if (mainmod == NULL || PyErr_Occurred())
2774 PLy_elog(ERROR, "could not import \"__main__\" module.");
2775 Py_INCREF(mainmod);
2776 PLy_interp_globals = PyModule_GetDict(mainmod);
2777 PLy_interp_safe_globals = PyDict_New();
2778 PyDict_SetItemString(PLy_interp_globals, "GD", PLy_interp_safe_globals);
2779 Py_DECREF(mainmod);
2780 if (PLy_interp_globals == NULL || PyErr_Occurred())
2781 PLy_elog(ERROR, "could not initialize globals");
2784 static void
2785 PLy_init_plpy(void)
2787 PyObject *main_mod,
2788 *main_dict,
2789 *plpy_mod;
2790 PyObject *plpy,
2791 *plpy_dict;
2794 * initialize plpy module
2796 if (PyType_Ready(&PLy_PlanType) < 0)
2797 elog(ERROR, "could not init PLy_PlanType");
2798 if (PyType_Ready(&PLy_ResultType) < 0)
2799 elog(ERROR, "could not init PLy_ResultType");
2801 plpy = Py_InitModule("plpy", PLy_methods);
2802 plpy_dict = PyModule_GetDict(plpy);
2804 /* PyDict_SetItemString(plpy, "PlanType", (PyObject *) &PLy_PlanType); */
2806 PLy_exc_error = PyErr_NewException("plpy.Error", NULL, NULL);
2807 PLy_exc_fatal = PyErr_NewException("plpy.Fatal", NULL, NULL);
2808 PLy_exc_spi_error = PyErr_NewException("plpy.SPIError", NULL, NULL);
2809 PyDict_SetItemString(plpy_dict, "Error", PLy_exc_error);
2810 PyDict_SetItemString(plpy_dict, "Fatal", PLy_exc_fatal);
2811 PyDict_SetItemString(plpy_dict, "SPIError", PLy_exc_spi_error);
2814 * initialize main module, and add plpy
2816 main_mod = PyImport_AddModule("__main__");
2817 main_dict = PyModule_GetDict(main_mod);
2818 plpy_mod = PyImport_AddModule("plpy");
2819 PyDict_SetItemString(main_dict, "plpy", plpy_mod);
2820 if (PyErr_Occurred())
2821 elog(ERROR, "could not init plpy");
2824 /* the python interface to the elog function
2825 * don't confuse these with PLy_elog
2827 static PyObject *PLy_output(volatile int, PyObject *, PyObject *);
2829 static PyObject *
2830 PLy_debug(PyObject * self, PyObject * args)
2832 return PLy_output(DEBUG2, self, args);
2835 static PyObject *
2836 PLy_log(PyObject * self, PyObject * args)
2838 return PLy_output(LOG, self, args);
2841 static PyObject *
2842 PLy_info(PyObject * self, PyObject * args)
2844 return PLy_output(INFO, self, args);
2847 static PyObject *
2848 PLy_notice(PyObject * self, PyObject * args)
2850 return PLy_output(NOTICE, self, args);
2853 static PyObject *
2854 PLy_warning(PyObject * self, PyObject * args)
2856 return PLy_output(WARNING, self, args);
2859 static PyObject *
2860 PLy_error(PyObject * self, PyObject * args)
2862 return PLy_output(ERROR, self, args);
2865 static PyObject *
2866 PLy_fatal(PyObject * self, PyObject * args)
2868 return PLy_output(FATAL, self, args);
2872 static PyObject *
2873 PLy_output(volatile int level, PyObject * self, PyObject * args)
2875 PyObject *so;
2876 char *volatile sv;
2877 MemoryContext oldcontext;
2879 so = PyObject_Str(args);
2880 if (so == NULL || ((sv = PyString_AsString(so)) == NULL))
2882 level = ERROR;
2883 sv = "could not parse error message in `plpy.elog'";
2886 oldcontext = CurrentMemoryContext;
2887 PG_TRY();
2889 elog(level, "%s", sv);
2891 PG_CATCH();
2893 MemoryContextSwitchTo(oldcontext);
2894 PLy_error_in_progress = CopyErrorData();
2895 FlushErrorState();
2896 Py_XDECREF(so);
2899 * returning NULL here causes the python interpreter to bail. when
2900 * control passes back to PLy_procedure_call, we check for PG
2901 * exceptions and re-throw the error.
2903 PyErr_SetString(PLy_exc_error, sv);
2904 return NULL;
2906 PG_END_TRY();
2908 Py_XDECREF(so);
2911 * return a legal object so the interpreter will continue on its merry way
2913 Py_INCREF(Py_None);
2914 return Py_None;
2919 * Get the name of the last procedure called by the backend (the
2920 * innermost, if a plpython procedure call calls the backend and the
2921 * backend calls another plpython procedure).
2923 * NB: this returns the SQL name, not the internal Python procedure name
2925 static char *
2926 PLy_procedure_name(PLyProcedure * proc)
2928 if (proc == NULL)
2929 return "<unknown procedure>";
2930 return proc->proname;
2933 /* output a python traceback/exception via the postgresql elog
2934 * function. not pretty.
2936 static void
2937 PLy_exception_set(PyObject * exc, const char *fmt,...)
2939 char buf[1024];
2940 va_list ap;
2942 va_start(ap, fmt);
2943 vsnprintf(buf, sizeof(buf), fmt, ap);
2944 va_end(ap);
2946 PyErr_SetString(exc, buf);
2949 /* Emit a PG error or notice, together with any available info about the
2950 * current Python error. This should be used to propagate Python errors
2951 * into PG.
2953 static void
2954 PLy_elog(int elevel, const char *fmt,...)
2956 char *xmsg;
2957 int xlevel;
2958 StringInfoData emsg;
2960 xmsg = PLy_traceback(&xlevel);
2962 initStringInfo(&emsg);
2963 for (;;)
2965 va_list ap;
2966 bool success;
2968 va_start(ap, fmt);
2969 success = appendStringInfoVA(&emsg, fmt, ap);
2970 va_end(ap);
2971 if (success)
2972 break;
2973 enlargeStringInfo(&emsg, emsg.maxlen);
2976 PG_TRY();
2978 ereport(elevel,
2979 (errmsg("plpython: %s", emsg.data),
2980 (xmsg) ? errdetail("%s", xmsg) : 0));
2982 PG_CATCH();
2984 pfree(emsg.data);
2985 if (xmsg)
2986 pfree(xmsg);
2987 PG_RE_THROW();
2989 PG_END_TRY();
2991 pfree(emsg.data);
2992 if (xmsg)
2993 pfree(xmsg);
2996 static char *
2997 PLy_traceback(int *xlevel)
2999 PyObject *e,
3001 *tb;
3002 PyObject *eob,
3003 *vob = NULL;
3004 char *vstr,
3005 *estr;
3006 StringInfoData xstr;
3009 * get the current exception
3011 PyErr_Fetch(&e, &v, &tb);
3014 * oops, no exception, return
3016 if (e == NULL)
3018 *xlevel = WARNING;
3019 return NULL;
3022 PyErr_NormalizeException(&e, &v, &tb);
3023 Py_XDECREF(tb);
3025 eob = PyObject_Str(e);
3026 if (v && ((vob = PyObject_Str(v)) != NULL))
3027 vstr = PyString_AsString(vob);
3028 else
3029 vstr = "Unknown";
3032 * I'm not sure what to do if eob is NULL here -- we can't call PLy_elog
3033 * because that function calls us, so we could end up with infinite
3034 * recursion. I'm not even sure if eob could be NULL here -- would an
3035 * Assert() be more appropriate?
3037 estr = eob ? PyString_AsString(eob) : "Unknown Exception";
3038 initStringInfo(&xstr);
3039 appendStringInfo(&xstr, "%s: %s", estr, vstr);
3041 Py_DECREF(eob);
3042 Py_XDECREF(vob);
3043 Py_XDECREF(v);
3046 * intuit an appropriate error level based on the exception type
3048 if (PLy_exc_error && PyErr_GivenExceptionMatches(e, PLy_exc_error))
3049 *xlevel = ERROR;
3050 else if (PLy_exc_fatal && PyErr_GivenExceptionMatches(e, PLy_exc_fatal))
3051 *xlevel = FATAL;
3052 else
3053 *xlevel = ERROR;
3055 Py_DECREF(e);
3056 return xstr.data;
3059 /* python module code */
3061 /* some dumb utility functions */
3062 static void *
3063 PLy_malloc(size_t bytes)
3065 void *ptr = malloc(bytes);
3067 if (ptr == NULL)
3068 ereport(FATAL,
3069 (errcode(ERRCODE_OUT_OF_MEMORY),
3070 errmsg("out of memory")));
3071 return ptr;
3074 static void *
3075 PLy_malloc0(size_t bytes)
3077 void *ptr = PLy_malloc(bytes);
3079 MemSet(ptr, 0, bytes);
3080 return ptr;
3083 static char *
3084 PLy_strdup(const char *str)
3086 char *result;
3087 size_t len;
3089 len = strlen(str) + 1;
3090 result = PLy_malloc(len);
3091 memcpy(result, str, len);
3093 return result;
3096 /* define this away */
3097 static void
3098 PLy_free(void *ptr)
3100 free(ptr);