Fix xslt_process() to ensure that it inserts a NULL terminator after the
[PostgreSQL.git] / src / pl / plpython / plpython.c
blob50b48ac5ae33e57edf293a549a806d63f964e154
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 PG_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 and translation support */
208 static void
209 PLy_exception_set(PyObject *, const char *,...)
210 __attribute__((format(printf, 2, 3)));
212 /* same, with pluralized message */
213 static void
214 PLy_exception_set_plural(PyObject *, const char *, const char *,
215 unsigned long n,...)
216 __attribute__((format(printf, 2, 5)))
217 __attribute__((format(printf, 3, 5)));
219 /* Get the innermost python procedure called from the backend */
220 static char *PLy_procedure_name(PLyProcedure *);
222 /* some utility functions */
223 static void
224 PLy_elog(int, const char *,...)
225 __attribute__((format(printf, 2, 3)));
226 static char *PLy_traceback(int *);
228 static void *PLy_malloc(size_t);
229 static void *PLy_malloc0(size_t);
230 static char *PLy_strdup(const char *);
231 static void PLy_free(void *);
233 /* sub handlers for functions and triggers */
234 static Datum PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure *);
235 static HeapTuple PLy_trigger_handler(FunctionCallInfo fcinfo, PLyProcedure *);
237 static PyObject *PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *);
238 static void PLy_function_delete_args(PLyProcedure *);
239 static PyObject *PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *,
240 HeapTuple *);
241 static HeapTuple PLy_modify_tuple(PLyProcedure *, PyObject *,
242 TriggerData *, HeapTuple);
244 static PyObject *PLy_procedure_call(PLyProcedure *, char *, PyObject *);
246 static PLyProcedure *PLy_procedure_get(FunctionCallInfo fcinfo,
247 Oid tgreloid);
249 static PLyProcedure *PLy_procedure_create(HeapTuple procTup, Oid tgreloid,
250 char *key);
252 static void PLy_procedure_compile(PLyProcedure *, const char *);
253 static char *PLy_procedure_munge_source(const char *, const char *);
254 static void PLy_procedure_delete(PLyProcedure *);
256 static void PLy_typeinfo_init(PLyTypeInfo *);
257 static void PLy_typeinfo_dealloc(PLyTypeInfo *);
258 static void PLy_output_datum_func(PLyTypeInfo *, HeapTuple);
259 static void PLy_output_datum_func2(PLyObToDatum *, HeapTuple);
260 static void PLy_input_datum_func(PLyTypeInfo *, Oid, HeapTuple);
261 static void PLy_input_datum_func2(PLyDatumToOb *, Oid, HeapTuple);
262 static void PLy_output_tuple_funcs(PLyTypeInfo *, TupleDesc);
263 static void PLy_input_tuple_funcs(PLyTypeInfo *, TupleDesc);
265 /* conversion functions */
266 static PyObject *PLyDict_FromTuple(PLyTypeInfo *, HeapTuple, TupleDesc);
267 static PyObject *PLyBool_FromString(const char *);
268 static PyObject *PLyFloat_FromString(const char *);
269 static PyObject *PLyInt_FromString(const char *);
270 static PyObject *PLyLong_FromString(const char *);
271 static PyObject *PLyString_FromString(const char *);
273 static HeapTuple PLyMapping_ToTuple(PLyTypeInfo *, PyObject *);
274 static HeapTuple PLySequence_ToTuple(PLyTypeInfo *, PyObject *);
275 static HeapTuple PLyObject_ToTuple(PLyTypeInfo *, PyObject *);
278 * Currently active plpython function
280 static PLyProcedure *PLy_curr_procedure = NULL;
283 * When a callback from Python into PG incurs an error, we temporarily store
284 * the error information here, and return NULL to the Python interpreter.
285 * Any further callback attempts immediately fail, and when the Python
286 * interpreter returns to the calling function, we re-throw the error (even if
287 * Python thinks it trapped the error and doesn't return NULL). Eventually
288 * this ought to be improved to let Python code really truly trap the error,
289 * but that's more of a change from the pre-8.0 semantics than I have time for
290 * now --- it will only be possible if the callback query is executed inside a
291 * subtransaction.
293 static ErrorData *PLy_error_in_progress = NULL;
295 static PyObject *PLy_interp_globals = NULL;
296 static PyObject *PLy_interp_safe_globals = NULL;
297 static PyObject *PLy_procedure_cache = NULL;
299 /* Python exceptions */
300 static PyObject *PLy_exc_error = NULL;
301 static PyObject *PLy_exc_fatal = NULL;
302 static PyObject *PLy_exc_spi_error = NULL;
304 /* some globals for the python module */
305 static char PLy_plan_doc[] = {
306 "Store a PostgreSQL plan"
309 static char PLy_result_doc[] = {
310 "Results of a PostgreSQL query"
315 * the function definitions
319 * This routine is a crock, and so is everyplace that calls it. The problem
320 * is that the cached form of plpython functions/queries is allocated permanently
321 * (mostly via malloc()) and never released until backend exit. Subsidiary
322 * data structures such as fmgr info records therefore must live forever
323 * as well. A better implementation would store all this stuff in a per-
324 * function memory context that could be reclaimed at need. In the meantime,
325 * fmgr_info_cxt must be called specifying TopMemoryContext so that whatever
326 * it might allocate, and whatever the eventual function might allocate using
327 * fn_mcxt, will live forever too.
329 static void
330 perm_fmgr_info(Oid functionId, FmgrInfo *finfo)
332 fmgr_info_cxt(functionId, finfo, TopMemoryContext);
335 Datum
336 plpython_call_handler(PG_FUNCTION_ARGS)
338 Datum retval;
339 PLyProcedure *save_curr_proc;
340 PLyProcedure *volatile proc = NULL;
342 if (SPI_connect() != SPI_OK_CONNECT)
343 elog(ERROR, "SPI_connect failed");
345 save_curr_proc = PLy_curr_procedure;
347 PG_TRY();
349 if (CALLED_AS_TRIGGER(fcinfo))
351 TriggerData *tdata = (TriggerData *) fcinfo->context;
352 HeapTuple trv;
354 proc = PLy_procedure_get(fcinfo,
355 RelationGetRelid(tdata->tg_relation));
356 PLy_curr_procedure = proc;
357 trv = PLy_trigger_handler(fcinfo, proc);
358 retval = PointerGetDatum(trv);
360 else
362 proc = PLy_procedure_get(fcinfo, InvalidOid);
363 PLy_curr_procedure = proc;
364 retval = PLy_function_handler(fcinfo, proc);
367 PG_CATCH();
369 PLy_curr_procedure = save_curr_proc;
370 if (proc)
372 /* note: Py_DECREF needs braces around it, as of 2003/08 */
373 Py_DECREF(proc->me);
375 PyErr_Clear();
376 PG_RE_THROW();
378 PG_END_TRY();
380 PLy_curr_procedure = save_curr_proc;
382 Py_DECREF(proc->me);
384 return retval;
387 /* trigger and function sub handlers
389 * the python function is expected to return Py_None if the tuple is
390 * acceptable and unmodified. Otherwise it should return a PyString
391 * object who's value is SKIP, or MODIFY. SKIP means don't perform
392 * this action. MODIFY means the tuple has been modified, so update
393 * tuple and perform action. SKIP and MODIFY assume the trigger fires
394 * BEFORE the event and is ROW level. postgres expects the function
395 * to take no arguments and return an argument of type trigger.
397 static HeapTuple
398 PLy_trigger_handler(FunctionCallInfo fcinfo, PLyProcedure *proc)
400 HeapTuple rv = NULL;
401 PyObject *volatile plargs = NULL;
402 PyObject *volatile plrv = NULL;
404 PG_TRY();
406 plargs = PLy_trigger_build_args(fcinfo, proc, &rv);
407 plrv = PLy_procedure_call(proc, "TD", plargs);
409 Assert(plrv != NULL);
410 Assert(!PLy_error_in_progress);
413 * Disconnect from SPI manager
415 if (SPI_finish() != SPI_OK_FINISH)
416 elog(ERROR, "SPI_finish failed");
419 * return of None means we're happy with the tuple
421 if (plrv != Py_None)
423 char *srv;
425 if (!PyString_Check(plrv))
426 ereport(ERROR,
427 (errcode(ERRCODE_DATA_EXCEPTION),
428 errmsg("unexpected return value from trigger procedure"),
429 errdetail("Expected None or a string.")));
431 srv = PyString_AsString(plrv);
432 if (pg_strcasecmp(srv, "SKIP") == 0)
433 rv = NULL;
434 else if (pg_strcasecmp(srv, "MODIFY") == 0)
436 TriggerData *tdata = (TriggerData *) fcinfo->context;
438 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event) ||
439 TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
440 rv = PLy_modify_tuple(proc, plargs, tdata, rv);
441 else
442 ereport(WARNING,
443 (errmsg("PL/Python trigger function returned \"MODIFY\" in a DELETE trigger -- ignored")));
445 else if (pg_strcasecmp(srv, "OK") != 0)
448 * accept "OK" as an alternative to None; otherwise, raise an
449 * error
451 ereport(ERROR,
452 (errcode(ERRCODE_DATA_EXCEPTION),
453 errmsg("unexpected return value from trigger procedure"),
454 errdetail("Expected None, \"OK\", \"SKIP\", or \"MODIFY\".")));
458 PG_CATCH();
460 Py_XDECREF(plargs);
461 Py_XDECREF(plrv);
463 PG_RE_THROW();
465 PG_END_TRY();
467 Py_DECREF(plargs);
468 Py_DECREF(plrv);
470 return rv;
473 static HeapTuple
474 PLy_modify_tuple(PLyProcedure *proc, PyObject *pltd, TriggerData *tdata,
475 HeapTuple otup)
477 PyObject *volatile plntup;
478 PyObject *volatile plkeys;
479 PyObject *volatile platt;
480 PyObject *volatile plval;
481 PyObject *volatile plstr;
482 HeapTuple rtup;
483 int natts,
485 attn,
486 atti;
487 int *volatile modattrs;
488 Datum *volatile modvalues;
489 char *volatile modnulls;
490 TupleDesc tupdesc;
492 plntup = plkeys = platt = plval = plstr = NULL;
493 modattrs = NULL;
494 modvalues = NULL;
495 modnulls = NULL;
497 PG_TRY();
499 if ((plntup = PyDict_GetItemString(pltd, "new")) == NULL)
500 ereport(ERROR,
501 (errmsg("TD[\"new\"] deleted, cannot modify row")));
502 if (!PyDict_Check(plntup))
503 ereport(ERROR,
504 (errmsg("TD[\"new\"] is not a dictionary")));
505 Py_INCREF(plntup);
507 plkeys = PyDict_Keys(plntup);
508 natts = PyList_Size(plkeys);
510 modattrs = (int *) palloc(natts * sizeof(int));
511 modvalues = (Datum *) palloc(natts * sizeof(Datum));
512 modnulls = (char *) palloc(natts * sizeof(char));
514 tupdesc = tdata->tg_relation->rd_att;
516 for (i = 0; i < natts; i++)
518 char *src;
520 platt = PyList_GetItem(plkeys, i);
521 if (!PyString_Check(platt))
522 ereport(ERROR,
523 (errmsg("name of TD[\"new\"] attribute at ordinal position %d is not a string", i)));
524 attn = SPI_fnumber(tupdesc, PyString_AsString(platt));
525 if (attn == SPI_ERROR_NOATTRIBUTE)
526 ereport(ERROR,
527 (errmsg("key \"%s\" found in TD[\"new\"] does not exist as a column in the triggering row",
528 PyString_AsString(platt))));
529 atti = attn - 1;
531 plval = PyDict_GetItem(plntup, platt);
532 if (plval == NULL)
533 elog(FATAL, "Python interpreter is probably corrupted");
535 Py_INCREF(plval);
537 modattrs[i] = attn;
539 if (tupdesc->attrs[atti]->attisdropped)
541 modvalues[i] = (Datum) 0;
542 modnulls[i] = 'n';
544 else if (plval != Py_None)
546 plstr = PyObject_Str(plval);
547 if (!plstr)
548 PLy_elog(ERROR, "could not compute string representation of Python object in PL/Python function \"%s\" while modifying trigger row",
549 proc->proname);
550 src = PyString_AsString(plstr);
552 modvalues[i] =
553 InputFunctionCall(&proc->result.out.r.atts[atti].typfunc,
554 src,
555 proc->result.out.r.atts[atti].typioparam,
556 tupdesc->attrs[atti]->atttypmod);
557 modnulls[i] = ' ';
559 Py_DECREF(plstr);
560 plstr = NULL;
562 else
564 modvalues[i] =
565 InputFunctionCall(&proc->result.out.r.atts[atti].typfunc,
566 NULL,
567 proc->result.out.r.atts[atti].typioparam,
568 tupdesc->attrs[atti]->atttypmod);
569 modnulls[i] = 'n';
572 Py_DECREF(plval);
573 plval = NULL;
576 rtup = SPI_modifytuple(tdata->tg_relation, otup, natts,
577 modattrs, modvalues, modnulls);
578 if (rtup == NULL)
579 elog(ERROR, "SPI_modifytuple failed: error %d", SPI_result);
581 PG_CATCH();
583 Py_XDECREF(plntup);
584 Py_XDECREF(plkeys);
585 Py_XDECREF(plval);
586 Py_XDECREF(plstr);
588 if (modnulls)
589 pfree(modnulls);
590 if (modvalues)
591 pfree(modvalues);
592 if (modattrs)
593 pfree(modattrs);
595 PG_RE_THROW();
597 PG_END_TRY();
599 Py_DECREF(plntup);
600 Py_DECREF(plkeys);
602 pfree(modattrs);
603 pfree(modvalues);
604 pfree(modnulls);
606 return rtup;
609 static PyObject *
610 PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *rv)
612 TriggerData *tdata = (TriggerData *) fcinfo->context;
613 PyObject *pltname,
614 *pltevent,
615 *pltwhen,
616 *pltlevel,
617 *pltrelid,
618 *plttablename,
619 *plttableschema;
620 PyObject *pltargs,
621 *pytnew,
622 *pytold;
623 PyObject *volatile pltdata = NULL;
624 char *stroid;
626 PG_TRY();
628 pltdata = PyDict_New();
629 if (!pltdata)
630 PLy_elog(ERROR, "could not create new dictionary while building trigger arguments");
632 pltname = PyString_FromString(tdata->tg_trigger->tgname);
633 PyDict_SetItemString(pltdata, "name", pltname);
634 Py_DECREF(pltname);
636 stroid = DatumGetCString(DirectFunctionCall1(oidout,
637 ObjectIdGetDatum(tdata->tg_relation->rd_id)));
638 pltrelid = PyString_FromString(stroid);
639 PyDict_SetItemString(pltdata, "relid", pltrelid);
640 Py_DECREF(pltrelid);
641 pfree(stroid);
643 stroid = SPI_getrelname(tdata->tg_relation);
644 plttablename = PyString_FromString(stroid);
645 PyDict_SetItemString(pltdata, "table_name", plttablename);
646 Py_DECREF(plttablename);
647 pfree(stroid);
649 stroid = SPI_getnspname(tdata->tg_relation);
650 plttableschema = PyString_FromString(stroid);
651 PyDict_SetItemString(pltdata, "table_schema", plttableschema);
652 Py_DECREF(plttableschema);
653 pfree(stroid);
656 if (TRIGGER_FIRED_BEFORE(tdata->tg_event))
657 pltwhen = PyString_FromString("BEFORE");
658 else if (TRIGGER_FIRED_AFTER(tdata->tg_event))
659 pltwhen = PyString_FromString("AFTER");
660 else
662 elog(ERROR, "unrecognized WHEN tg_event: %u", tdata->tg_event);
663 pltwhen = NULL; /* keep compiler quiet */
665 PyDict_SetItemString(pltdata, "when", pltwhen);
666 Py_DECREF(pltwhen);
668 if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
670 pltlevel = PyString_FromString("ROW");
671 PyDict_SetItemString(pltdata, "level", pltlevel);
672 Py_DECREF(pltlevel);
674 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
676 pltevent = PyString_FromString("INSERT");
678 PyDict_SetItemString(pltdata, "old", Py_None);
679 pytnew = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
680 tdata->tg_relation->rd_att);
681 PyDict_SetItemString(pltdata, "new", pytnew);
682 Py_DECREF(pytnew);
683 *rv = tdata->tg_trigtuple;
685 else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
687 pltevent = PyString_FromString("DELETE");
689 PyDict_SetItemString(pltdata, "new", Py_None);
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_trigtuple;
696 else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
698 pltevent = PyString_FromString("UPDATE");
700 pytnew = PLyDict_FromTuple(&(proc->result), tdata->tg_newtuple,
701 tdata->tg_relation->rd_att);
702 PyDict_SetItemString(pltdata, "new", pytnew);
703 Py_DECREF(pytnew);
704 pytold = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
705 tdata->tg_relation->rd_att);
706 PyDict_SetItemString(pltdata, "old", pytold);
707 Py_DECREF(pytold);
708 *rv = tdata->tg_newtuple;
710 else
712 elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
713 pltevent = NULL; /* keep compiler quiet */
716 PyDict_SetItemString(pltdata, "event", pltevent);
717 Py_DECREF(pltevent);
719 else if (TRIGGER_FIRED_FOR_STATEMENT(tdata->tg_event))
721 pltlevel = PyString_FromString("STATEMENT");
722 PyDict_SetItemString(pltdata, "level", pltlevel);
723 Py_DECREF(pltlevel);
725 PyDict_SetItemString(pltdata, "old", Py_None);
726 PyDict_SetItemString(pltdata, "new", Py_None);
727 *rv = NULL;
729 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
730 pltevent = PyString_FromString("INSERT");
731 else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
732 pltevent = PyString_FromString("DELETE");
733 else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
734 pltevent = PyString_FromString("UPDATE");
735 else if (TRIGGER_FIRED_BY_TRUNCATE(tdata->tg_event))
736 pltevent = PyString_FromString("TRUNCATE");
737 else
739 elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
740 pltevent = NULL; /* keep compiler quiet */
743 PyDict_SetItemString(pltdata, "event", pltevent);
744 Py_DECREF(pltevent);
746 else
747 elog(ERROR, "unrecognized LEVEL tg_event: %u", tdata->tg_event);
749 if (tdata->tg_trigger->tgnargs)
752 * all strings...
754 int i;
755 PyObject *pltarg;
757 pltargs = PyList_New(tdata->tg_trigger->tgnargs);
758 for (i = 0; i < tdata->tg_trigger->tgnargs; i++)
760 pltarg = PyString_FromString(tdata->tg_trigger->tgargs[i]);
763 * stolen, don't Py_DECREF
765 PyList_SetItem(pltargs, i, pltarg);
768 else
770 Py_INCREF(Py_None);
771 pltargs = Py_None;
773 PyDict_SetItemString(pltdata, "args", pltargs);
774 Py_DECREF(pltargs);
776 PG_CATCH();
778 Py_XDECREF(pltdata);
779 PG_RE_THROW();
781 PG_END_TRY();
783 return pltdata;
788 /* function handler and friends */
789 static Datum
790 PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure *proc)
792 Datum rv;
793 PyObject *volatile plargs = NULL;
794 PyObject *volatile plrv = NULL;
795 PyObject *volatile plrv_so = NULL;
796 char *plrv_sc;
798 PG_TRY();
800 if (!proc->is_setof || proc->setof == NULL)
802 /* Simple type returning function or first time for SETOF function */
803 plargs = PLy_function_build_args(fcinfo, proc);
804 plrv = PLy_procedure_call(proc, "args", plargs);
805 if (!proc->is_setof)
808 * SETOF function parameters will be deleted when last row is
809 * returned
811 PLy_function_delete_args(proc);
812 Assert(plrv != NULL);
813 Assert(!PLy_error_in_progress);
817 * Disconnect from SPI manager and then create the return values datum
818 * (if the input function does a palloc for it this must not be
819 * allocated in the SPI memory context because SPI_finish would free
820 * it).
822 if (SPI_finish() != SPI_OK_FINISH)
823 elog(ERROR, "SPI_finish failed");
825 if (proc->is_setof)
827 bool has_error = false;
828 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
830 if (proc->setof == NULL)
832 /* first time -- do checks and setup */
833 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
834 (rsi->allowedModes & SFRM_ValuePerCall) == 0)
836 ereport(ERROR,
837 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
838 errmsg("unsupported set function return mode"),
839 errdetail("PL/Python set-returning functions only support returning only value per call.")));
841 rsi->returnMode = SFRM_ValuePerCall;
843 /* Make iterator out of returned object */
844 proc->setof = PyObject_GetIter(plrv);
845 Py_DECREF(plrv);
846 plrv = NULL;
848 if (proc->setof == NULL)
849 ereport(ERROR,
850 (errcode(ERRCODE_DATATYPE_MISMATCH),
851 errmsg("returned object cannot be iterated"),
852 errdetail("PL/Python set-returning functions must return an iterable object.")));
855 /* Fetch next from iterator */
856 plrv = PyIter_Next(proc->setof);
857 if (plrv)
858 rsi->isDone = ExprMultipleResult;
859 else
861 rsi->isDone = ExprEndResult;
862 has_error = PyErr_Occurred() != NULL;
865 if (rsi->isDone == ExprEndResult)
867 /* Iterator is exhausted or error happened */
868 Py_DECREF(proc->setof);
869 proc->setof = NULL;
871 Py_XDECREF(plargs);
872 Py_XDECREF(plrv);
873 Py_XDECREF(plrv_so);
875 PLy_function_delete_args(proc);
877 if (has_error)
878 ereport(ERROR,
879 (errcode(ERRCODE_DATA_EXCEPTION),
880 errmsg("error fetching next item from iterator")));
882 fcinfo->isnull = true;
883 return (Datum) NULL;
888 * If the function is declared to return void, the Python return value
889 * must be None. For void-returning functions, we also treat a None
890 * return value as a special "void datum" rather than NULL (as is the
891 * case for non-void-returning functions).
893 if (proc->result.out.d.typoid == VOIDOID)
895 if (plrv != Py_None)
896 ereport(ERROR,
897 (errcode(ERRCODE_DATATYPE_MISMATCH),
898 errmsg("PL/Python function with return type \"void\" did not return None")));
900 fcinfo->isnull = false;
901 rv = (Datum) 0;
903 else if (plrv == Py_None)
905 fcinfo->isnull = true;
906 if (proc->result.is_rowtype < 1)
907 rv = InputFunctionCall(&proc->result.out.d.typfunc,
908 NULL,
909 proc->result.out.d.typioparam,
910 -1);
911 else
912 /* Tuple as None */
913 rv = (Datum) NULL;
915 else if (proc->result.is_rowtype >= 1)
917 HeapTuple tuple = NULL;
919 if (PySequence_Check(plrv))
920 /* composite type as sequence (tuple, list etc) */
921 tuple = PLySequence_ToTuple(&proc->result, plrv);
922 else if (PyMapping_Check(plrv))
923 /* composite type as mapping (currently only dict) */
924 tuple = PLyMapping_ToTuple(&proc->result, plrv);
925 else
926 /* returned as smth, must provide method __getattr__(name) */
927 tuple = PLyObject_ToTuple(&proc->result, plrv);
929 if (tuple != NULL)
931 fcinfo->isnull = false;
932 rv = HeapTupleGetDatum(tuple);
934 else
936 fcinfo->isnull = true;
937 rv = (Datum) NULL;
940 else
942 fcinfo->isnull = false;
943 plrv_so = PyObject_Str(plrv);
944 if (!plrv_so)
945 PLy_elog(ERROR, "could not create string representation of Python object in PL/Python function \"%s\" while creating return value", proc->proname);
946 plrv_sc = PyString_AsString(plrv_so);
947 rv = InputFunctionCall(&proc->result.out.d.typfunc,
948 plrv_sc,
949 proc->result.out.d.typioparam,
950 -1);
953 PG_CATCH();
955 Py_XDECREF(plargs);
956 Py_XDECREF(plrv);
957 Py_XDECREF(plrv_so);
959 PG_RE_THROW();
961 PG_END_TRY();
963 Py_XDECREF(plargs);
964 Py_DECREF(plrv);
965 Py_XDECREF(plrv_so);
967 return rv;
970 static PyObject *
971 PLy_procedure_call(PLyProcedure *proc, char *kargs, PyObject *vargs)
973 PyObject *rv;
975 PyDict_SetItemString(proc->globals, kargs, vargs);
976 rv = PyEval_EvalCode((PyCodeObject *) proc->code,
977 proc->globals, proc->globals);
980 * If there was an error in a PG callback, propagate that no matter what
981 * Python claims about its success.
983 if (PLy_error_in_progress)
985 ErrorData *edata = PLy_error_in_progress;
987 PLy_error_in_progress = NULL;
988 ReThrowError(edata);
991 if (rv == NULL || PyErr_Occurred())
993 Py_XDECREF(rv);
994 PLy_elog(ERROR, "PL/Python function \"%s\" failed", proc->proname);
997 return rv;
1000 static PyObject *
1001 PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc)
1003 PyObject *volatile arg = NULL;
1004 PyObject *volatile args = NULL;
1005 int i;
1007 PG_TRY();
1009 args = PyList_New(proc->nargs);
1010 for (i = 0; i < proc->nargs; i++)
1012 if (proc->args[i].is_rowtype > 0)
1014 if (fcinfo->argnull[i])
1015 arg = NULL;
1016 else
1018 HeapTupleHeader td;
1019 Oid tupType;
1020 int32 tupTypmod;
1021 TupleDesc tupdesc;
1022 HeapTupleData tmptup;
1024 td = DatumGetHeapTupleHeader(fcinfo->arg[i]);
1025 /* Extract rowtype info and find a tupdesc */
1026 tupType = HeapTupleHeaderGetTypeId(td);
1027 tupTypmod = HeapTupleHeaderGetTypMod(td);
1028 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
1030 /* Set up I/O funcs if not done yet */
1031 if (proc->args[i].is_rowtype != 1)
1032 PLy_input_tuple_funcs(&(proc->args[i]), tupdesc);
1034 /* Build a temporary HeapTuple control structure */
1035 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
1036 tmptup.t_data = td;
1038 arg = PLyDict_FromTuple(&(proc->args[i]), &tmptup, tupdesc);
1039 ReleaseTupleDesc(tupdesc);
1042 else
1044 if (fcinfo->argnull[i])
1045 arg = NULL;
1046 else
1048 char *ct;
1050 ct = OutputFunctionCall(&(proc->args[i].in.d.typfunc),
1051 fcinfo->arg[i]);
1052 arg = (proc->args[i].in.d.func) (ct);
1053 pfree(ct);
1057 if (arg == NULL)
1059 Py_INCREF(Py_None);
1060 arg = Py_None;
1063 if (PyList_SetItem(args, i, arg) == -1)
1064 PLy_elog(ERROR, "PyList_SetItem() failed for PL/Python function \"%s\" while setting up arguments", proc->proname);
1066 if (proc->argnames && proc->argnames[i] &&
1067 PyDict_SetItemString(proc->globals, proc->argnames[i], arg) == -1)
1068 PLy_elog(ERROR, "PyDict_SetItemString() failed for PL/Python function \"%s\" while setting up arguments", proc->proname);
1069 arg = NULL;
1072 PG_CATCH();
1074 Py_XDECREF(arg);
1075 Py_XDECREF(args);
1077 PG_RE_THROW();
1079 PG_END_TRY();
1081 return args;
1085 static void
1086 PLy_function_delete_args(PLyProcedure *proc)
1088 int i;
1090 if (!proc->argnames)
1091 return;
1093 for (i = 0; i < proc->nargs; i++)
1094 if (proc->argnames[i])
1095 PyDict_DelItemString(proc->globals, proc->argnames[i]);
1100 * PLyProcedure functions
1103 /* PLy_procedure_get: returns a cached PLyProcedure, or creates, stores and
1104 * returns a new PLyProcedure. fcinfo is the call info, tgreloid is the
1105 * relation OID when calling a trigger, or InvalidOid (zero) for ordinary
1106 * function calls.
1108 static PLyProcedure *
1109 PLy_procedure_get(FunctionCallInfo fcinfo, Oid tgreloid)
1111 Oid fn_oid;
1112 HeapTuple procTup;
1113 char key[128];
1114 PyObject *plproc;
1115 PLyProcedure *proc = NULL;
1116 int rv;
1118 fn_oid = fcinfo->flinfo->fn_oid;
1119 procTup = SearchSysCache(PROCOID,
1120 ObjectIdGetDatum(fn_oid),
1121 0, 0, 0);
1122 if (!HeapTupleIsValid(procTup))
1123 elog(ERROR, "cache lookup failed for function %u", fn_oid);
1125 rv = snprintf(key, sizeof(key), "%u_%u", fn_oid, tgreloid);
1126 if (rv >= sizeof(key) || rv < 0)
1127 elog(ERROR, "key too long");
1129 plproc = PyDict_GetItemString(PLy_procedure_cache, key);
1131 if (plproc != NULL)
1133 Py_INCREF(plproc);
1134 if (!PyCObject_Check(plproc))
1135 elog(FATAL, "expected a PyCObject, didn't get one");
1137 proc = PyCObject_AsVoidPtr(plproc);
1138 if (proc->me != plproc)
1139 elog(FATAL, "proc->me != plproc");
1140 /* did we find an up-to-date cache entry? */
1141 if (proc->fn_xmin != HeapTupleHeaderGetXmin(procTup->t_data) ||
1142 !ItemPointerEquals(&proc->fn_tid, &procTup->t_self))
1144 Py_DECREF(plproc);
1145 proc = NULL;
1149 if (proc == NULL)
1150 proc = PLy_procedure_create(procTup, tgreloid, key);
1152 if (OidIsValid(tgreloid))
1155 * Input/output conversion for trigger tuples. Use the result
1156 * TypeInfo variable to store the tuple conversion info. We do this
1157 * over again on each call to cover the possibility that the
1158 * relation's tupdesc changed since the trigger was last called.
1159 * PLy_input_tuple_funcs and PLy_output_tuple_funcs are responsible
1160 * for not doing repetitive work.
1162 TriggerData *tdata = (TriggerData *) fcinfo->context;
1164 Assert(CALLED_AS_TRIGGER(fcinfo));
1165 PLy_input_tuple_funcs(&(proc->result), tdata->tg_relation->rd_att);
1166 PLy_output_tuple_funcs(&(proc->result), tdata->tg_relation->rd_att);
1169 ReleaseSysCache(procTup);
1171 return proc;
1174 static PLyProcedure *
1175 PLy_procedure_create(HeapTuple procTup, Oid tgreloid, char *key)
1177 char procName[NAMEDATALEN + 256];
1178 Form_pg_proc procStruct;
1179 PLyProcedure *volatile proc;
1180 char *volatile procSource = NULL;
1181 Datum prosrcdatum;
1182 bool isnull;
1183 int i,
1186 procStruct = (Form_pg_proc) GETSTRUCT(procTup);
1188 if (OidIsValid(tgreloid))
1189 rv = snprintf(procName, sizeof(procName),
1190 "__plpython_procedure_%s_%u_trigger_%u",
1191 NameStr(procStruct->proname),
1192 HeapTupleGetOid(procTup),
1193 tgreloid);
1194 else
1195 rv = snprintf(procName, sizeof(procName),
1196 "__plpython_procedure_%s_%u",
1197 NameStr(procStruct->proname),
1198 HeapTupleGetOid(procTup));
1199 if (rv >= sizeof(procName) || rv < 0)
1200 elog(ERROR, "procedure name would overrun buffer");
1202 proc = PLy_malloc(sizeof(PLyProcedure));
1203 proc->proname = PLy_strdup(NameStr(procStruct->proname));
1204 proc->pyname = PLy_strdup(procName);
1205 proc->fn_xmin = HeapTupleHeaderGetXmin(procTup->t_data);
1206 proc->fn_tid = procTup->t_self;
1207 /* Remember if function is STABLE/IMMUTABLE */
1208 proc->fn_readonly =
1209 (procStruct->provolatile != PROVOLATILE_VOLATILE);
1210 PLy_typeinfo_init(&proc->result);
1211 for (i = 0; i < FUNC_MAX_ARGS; i++)
1212 PLy_typeinfo_init(&proc->args[i]);
1213 proc->nargs = 0;
1214 proc->code = proc->statics = NULL;
1215 proc->globals = proc->me = NULL;
1216 proc->is_setof = procStruct->proretset;
1217 proc->setof = NULL;
1218 proc->argnames = NULL;
1220 PG_TRY();
1223 * get information required for output conversion of the return value,
1224 * but only if this isn't a trigger.
1226 if (!OidIsValid(tgreloid))
1228 HeapTuple rvTypeTup;
1229 Form_pg_type rvTypeStruct;
1231 rvTypeTup = SearchSysCache(TYPEOID,
1232 ObjectIdGetDatum(procStruct->prorettype),
1233 0, 0, 0);
1234 if (!HeapTupleIsValid(rvTypeTup))
1235 elog(ERROR, "cache lookup failed for type %u",
1236 procStruct->prorettype);
1237 rvTypeStruct = (Form_pg_type) GETSTRUCT(rvTypeTup);
1239 /* Disallow pseudotype result, except for void */
1240 if (rvTypeStruct->typtype == TYPTYPE_PSEUDO &&
1241 procStruct->prorettype != VOIDOID)
1243 if (procStruct->prorettype == TRIGGEROID)
1244 ereport(ERROR,
1245 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1246 errmsg("trigger functions can only be called as triggers")));
1247 else
1248 ereport(ERROR,
1249 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1250 errmsg("PL/Python functions cannot return type %s",
1251 format_type_be(procStruct->prorettype))));
1254 if (rvTypeStruct->typtype == TYPTYPE_COMPOSITE)
1257 * Tuple: set up later, during first call to
1258 * PLy_function_handler
1260 proc->result.out.d.typoid = procStruct->prorettype;
1261 proc->result.is_rowtype = 2;
1263 else
1264 PLy_output_datum_func(&proc->result, rvTypeTup);
1266 ReleaseSysCache(rvTypeTup);
1270 * Now get information required for input conversion of the
1271 * procedure's arguments. Note that we ignore output arguments here
1272 * --- since we don't support returning record, and that was already
1273 * checked above, there's no need to worry about multiple output
1274 * arguments.
1276 if (procStruct->pronargs)
1278 Oid *types;
1279 char **names,
1280 *modes;
1281 int i,
1282 pos,
1283 total;
1285 /* extract argument type info from the pg_proc tuple */
1286 total = get_func_arg_info(procTup, &types, &names, &modes);
1288 /* count number of in+inout args into proc->nargs */
1289 if (modes == NULL)
1290 proc->nargs = total;
1291 else
1293 /* proc->nargs was initialized to 0 above */
1294 for (i = 0; i < total; i++)
1296 if (modes[i] != PROARGMODE_OUT &&
1297 modes[i] != PROARGMODE_TABLE)
1298 (proc->nargs)++;
1302 proc->argnames = (char **) PLy_malloc0(sizeof(char *) * proc->nargs);
1303 for (i = pos = 0; i < total; i++)
1305 HeapTuple argTypeTup;
1306 Form_pg_type argTypeStruct;
1308 if (modes &&
1309 (modes[i] == PROARGMODE_OUT ||
1310 modes[i] == PROARGMODE_TABLE))
1311 continue; /* skip OUT arguments */
1313 Assert(types[i] == procStruct->proargtypes.values[pos]);
1315 argTypeTup = SearchSysCache(TYPEOID,
1316 ObjectIdGetDatum(types[i]),
1317 0, 0, 0);
1318 if (!HeapTupleIsValid(argTypeTup))
1319 elog(ERROR, "cache lookup failed for type %u", types[i]);
1320 argTypeStruct = (Form_pg_type) GETSTRUCT(argTypeTup);
1322 /* check argument type is OK, set up I/O function info */
1323 switch (argTypeStruct->typtype)
1325 case TYPTYPE_PSEUDO:
1326 /* Disallow pseudotype argument */
1327 ereport(ERROR,
1328 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1329 errmsg("PL/Python functions cannot accept type %s",
1330 format_type_be(types[i]))));
1331 break;
1332 case TYPTYPE_COMPOSITE:
1333 /* we'll set IO funcs at first call */
1334 proc->args[pos].is_rowtype = 2;
1335 break;
1336 default:
1337 PLy_input_datum_func(&(proc->args[pos]),
1338 types[i],
1339 argTypeTup);
1340 break;
1343 /* get argument name */
1344 proc->argnames[pos] = names ? PLy_strdup(names[i]) : NULL;
1346 ReleaseSysCache(argTypeTup);
1348 pos++;
1353 * get the text of the function.
1355 prosrcdatum = SysCacheGetAttr(PROCOID, procTup,
1356 Anum_pg_proc_prosrc, &isnull);
1357 if (isnull)
1358 elog(ERROR, "null prosrc");
1359 procSource = TextDatumGetCString(prosrcdatum);
1361 PLy_procedure_compile(proc, procSource);
1363 pfree(procSource);
1365 proc->me = PyCObject_FromVoidPtr(proc, NULL);
1366 PyDict_SetItemString(PLy_procedure_cache, key, proc->me);
1368 PG_CATCH();
1370 PLy_procedure_delete(proc);
1371 if (procSource)
1372 pfree(procSource);
1374 PG_RE_THROW();
1376 PG_END_TRY();
1378 return proc;
1381 static void
1382 PLy_procedure_compile(PLyProcedure *proc, const char *src)
1384 PyObject *crv = NULL;
1385 char *msrc;
1387 proc->globals = PyDict_Copy(PLy_interp_globals);
1390 * SD is private preserved data between calls. GD is global data shared by
1391 * all functions
1393 proc->statics = PyDict_New();
1394 PyDict_SetItemString(proc->globals, "SD", proc->statics);
1397 * insert the function code into the interpreter
1399 msrc = PLy_procedure_munge_source(proc->pyname, src);
1400 crv = PyRun_String(msrc, Py_file_input, proc->globals, NULL);
1401 free(msrc);
1403 if (crv != NULL && (!PyErr_Occurred()))
1405 int clen;
1406 char call[NAMEDATALEN + 256];
1408 Py_DECREF(crv);
1411 * compile a call to the function
1413 clen = snprintf(call, sizeof(call), "%s()", proc->pyname);
1414 if (clen < 0 || clen >= sizeof(call))
1415 elog(ERROR, "string would overflow buffer");
1416 proc->code = Py_CompileString(call, "<string>", Py_eval_input);
1417 if (proc->code != NULL && (!PyErr_Occurred()))
1418 return;
1420 else
1421 Py_XDECREF(crv);
1423 PLy_elog(ERROR, "could not compile PL/Python function \"%s\"", proc->proname);
1426 static char *
1427 PLy_procedure_munge_source(const char *name, const char *src)
1429 char *mrc,
1430 *mp;
1431 const char *sp;
1432 size_t mlen,
1433 plen;
1436 * room for function source and the def statement
1438 mlen = (strlen(src) * 2) + strlen(name) + 16;
1440 mrc = PLy_malloc(mlen);
1441 plen = snprintf(mrc, mlen, "def %s():\n\t", name);
1442 Assert(plen >= 0 && plen < mlen);
1444 sp = src;
1445 mp = mrc + plen;
1447 while (*sp != '\0')
1449 if (*sp == '\r' && *(sp + 1) == '\n')
1450 sp++;
1452 if (*sp == '\n' || *sp == '\r')
1454 *mp++ = '\n';
1455 *mp++ = '\t';
1456 sp++;
1458 else
1459 *mp++ = *sp++;
1461 *mp++ = '\n';
1462 *mp++ = '\n';
1463 *mp = '\0';
1465 if (mp > (mrc + mlen))
1466 elog(FATAL, "buffer overrun in PLy_munge_source");
1468 return mrc;
1471 static void
1472 PLy_procedure_delete(PLyProcedure *proc)
1474 int i;
1476 Py_XDECREF(proc->code);
1477 Py_XDECREF(proc->statics);
1478 Py_XDECREF(proc->globals);
1479 Py_XDECREF(proc->me);
1480 if (proc->proname)
1481 PLy_free(proc->proname);
1482 if (proc->pyname)
1483 PLy_free(proc->pyname);
1484 for (i = 0; i < proc->nargs; i++)
1486 if (proc->args[i].is_rowtype == 1)
1488 if (proc->args[i].in.r.atts)
1489 PLy_free(proc->args[i].in.r.atts);
1490 if (proc->args[i].out.r.atts)
1491 PLy_free(proc->args[i].out.r.atts);
1493 if (proc->argnames && proc->argnames[i])
1494 PLy_free(proc->argnames[i]);
1496 if (proc->argnames)
1497 PLy_free(proc->argnames);
1501 * Conversion functions. Remember output from Python is input to
1502 * PostgreSQL, and vice versa.
1504 static void
1505 PLy_input_tuple_funcs(PLyTypeInfo *arg, TupleDesc desc)
1507 int i;
1509 if (arg->is_rowtype == 0)
1510 elog(ERROR, "PLyTypeInfo struct is initialized for a Datum");
1511 arg->is_rowtype = 1;
1513 if (arg->in.r.natts != desc->natts)
1515 if (arg->in.r.atts)
1516 PLy_free(arg->in.r.atts);
1517 arg->in.r.natts = desc->natts;
1518 arg->in.r.atts = PLy_malloc0(desc->natts * sizeof(PLyDatumToOb));
1521 for (i = 0; i < desc->natts; i++)
1523 HeapTuple typeTup;
1525 if (desc->attrs[i]->attisdropped)
1526 continue;
1528 if (arg->in.r.atts[i].typoid == desc->attrs[i]->atttypid)
1529 continue; /* already set up this entry */
1531 typeTup = SearchSysCache(TYPEOID,
1532 ObjectIdGetDatum(desc->attrs[i]->atttypid),
1533 0, 0, 0);
1534 if (!HeapTupleIsValid(typeTup))
1535 elog(ERROR, "cache lookup failed for type %u",
1536 desc->attrs[i]->atttypid);
1538 PLy_input_datum_func2(&(arg->in.r.atts[i]),
1539 desc->attrs[i]->atttypid,
1540 typeTup);
1542 ReleaseSysCache(typeTup);
1546 static void
1547 PLy_output_tuple_funcs(PLyTypeInfo *arg, TupleDesc desc)
1549 int i;
1551 if (arg->is_rowtype == 0)
1552 elog(ERROR, "PLyTypeInfo struct is initialized for a Datum");
1553 arg->is_rowtype = 1;
1555 if (arg->out.r.natts != desc->natts)
1557 if (arg->out.r.atts)
1558 PLy_free(arg->out.r.atts);
1559 arg->out.r.natts = desc->natts;
1560 arg->out.r.atts = PLy_malloc0(desc->natts * sizeof(PLyDatumToOb));
1563 for (i = 0; i < desc->natts; i++)
1565 HeapTuple typeTup;
1567 if (desc->attrs[i]->attisdropped)
1568 continue;
1570 if (arg->out.r.atts[i].typoid == desc->attrs[i]->atttypid)
1571 continue; /* already set up this entry */
1573 typeTup = SearchSysCache(TYPEOID,
1574 ObjectIdGetDatum(desc->attrs[i]->atttypid),
1575 0, 0, 0);
1576 if (!HeapTupleIsValid(typeTup))
1577 elog(ERROR, "cache lookup failed for type %u",
1578 desc->attrs[i]->atttypid);
1580 PLy_output_datum_func2(&(arg->out.r.atts[i]), typeTup);
1582 ReleaseSysCache(typeTup);
1586 static void
1587 PLy_output_datum_func(PLyTypeInfo *arg, HeapTuple typeTup)
1589 if (arg->is_rowtype > 0)
1590 elog(ERROR, "PLyTypeInfo struct is initialized for a Tuple");
1591 arg->is_rowtype = 0;
1592 PLy_output_datum_func2(&(arg->out.d), typeTup);
1595 static void
1596 PLy_output_datum_func2(PLyObToDatum *arg, HeapTuple typeTup)
1598 Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1600 perm_fmgr_info(typeStruct->typinput, &arg->typfunc);
1601 arg->typoid = HeapTupleGetOid(typeTup);
1602 arg->typioparam = getTypeIOParam(typeTup);
1603 arg->typbyval = typeStruct->typbyval;
1606 static void
1607 PLy_input_datum_func(PLyTypeInfo *arg, Oid typeOid, HeapTuple typeTup)
1609 if (arg->is_rowtype > 0)
1610 elog(ERROR, "PLyTypeInfo struct is initialized for Tuple");
1611 arg->is_rowtype = 0;
1612 PLy_input_datum_func2(&(arg->in.d), typeOid, typeTup);
1615 static void
1616 PLy_input_datum_func2(PLyDatumToOb *arg, Oid typeOid, HeapTuple typeTup)
1618 Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1620 /* Get the type's conversion information */
1621 perm_fmgr_info(typeStruct->typoutput, &arg->typfunc);
1622 arg->typoid = HeapTupleGetOid(typeTup);
1623 arg->typioparam = getTypeIOParam(typeTup);
1624 arg->typbyval = typeStruct->typbyval;
1626 /* Determine which kind of Python object we will convert to */
1627 switch (typeOid)
1629 case BOOLOID:
1630 arg->func = PLyBool_FromString;
1631 break;
1632 case FLOAT4OID:
1633 case FLOAT8OID:
1634 case NUMERICOID:
1635 arg->func = PLyFloat_FromString;
1636 break;
1637 case INT2OID:
1638 case INT4OID:
1639 arg->func = PLyInt_FromString;
1640 break;
1641 case INT8OID:
1642 arg->func = PLyLong_FromString;
1643 break;
1644 default:
1645 arg->func = PLyString_FromString;
1646 break;
1650 static void
1651 PLy_typeinfo_init(PLyTypeInfo *arg)
1653 arg->is_rowtype = -1;
1654 arg->in.r.natts = arg->out.r.natts = 0;
1655 arg->in.r.atts = NULL;
1656 arg->out.r.atts = NULL;
1659 static void
1660 PLy_typeinfo_dealloc(PLyTypeInfo *arg)
1662 if (arg->is_rowtype == 1)
1664 if (arg->in.r.atts)
1665 PLy_free(arg->in.r.atts);
1666 if (arg->out.r.atts)
1667 PLy_free(arg->out.r.atts);
1671 /* assumes that a bool is always returned as a 't' or 'f' */
1672 static PyObject *
1673 PLyBool_FromString(const char *src)
1676 * We would like to use Py_RETURN_TRUE and Py_RETURN_FALSE here for
1677 * generating SQL from trigger functions, but those are only supported in
1678 * Python >= 2.3, and we support older versions.
1679 * http://docs.python.org/api/boolObjects.html
1681 if (src[0] == 't')
1682 return PyBool_FromLong(1);
1683 return PyBool_FromLong(0);
1686 static PyObject *
1687 PLyFloat_FromString(const char *src)
1689 double v;
1690 char *eptr;
1692 errno = 0;
1693 v = strtod(src, &eptr);
1694 if (*eptr != '\0' || errno)
1695 return NULL;
1696 return PyFloat_FromDouble(v);
1699 static PyObject *
1700 PLyInt_FromString(const char *src)
1702 long v;
1703 char *eptr;
1705 errno = 0;
1706 v = strtol(src, &eptr, 0);
1707 if (*eptr != '\0' || errno)
1708 return NULL;
1709 return PyInt_FromLong(v);
1712 static PyObject *
1713 PLyLong_FromString(const char *src)
1715 return PyLong_FromString((char *) src, NULL, 0);
1718 static PyObject *
1719 PLyString_FromString(const char *src)
1721 return PyString_FromString(src);
1724 static PyObject *
1725 PLyDict_FromTuple(PLyTypeInfo *info, HeapTuple tuple, TupleDesc desc)
1727 PyObject *volatile dict;
1728 int i;
1730 if (info->is_rowtype != 1)
1731 elog(ERROR, "PLyTypeInfo structure describes a datum");
1733 dict = PyDict_New();
1734 if (dict == NULL)
1735 PLy_elog(ERROR, "could not create new dictionary");
1737 PG_TRY();
1739 for (i = 0; i < info->in.r.natts; i++)
1741 char *key,
1742 *vsrc;
1743 Datum vattr;
1744 bool is_null;
1745 PyObject *value;
1747 if (desc->attrs[i]->attisdropped)
1748 continue;
1750 key = NameStr(desc->attrs[i]->attname);
1751 vattr = heap_getattr(tuple, (i + 1), desc, &is_null);
1753 if (is_null || info->in.r.atts[i].func == NULL)
1754 PyDict_SetItemString(dict, key, Py_None);
1755 else
1757 vsrc = OutputFunctionCall(&info->in.r.atts[i].typfunc,
1758 vattr);
1761 * no exceptions allowed
1763 value = info->in.r.atts[i].func(vsrc);
1764 pfree(vsrc);
1765 PyDict_SetItemString(dict, key, value);
1766 Py_DECREF(value);
1770 PG_CATCH();
1772 Py_DECREF(dict);
1773 PG_RE_THROW();
1775 PG_END_TRY();
1777 return dict;
1781 static HeapTuple
1782 PLyMapping_ToTuple(PLyTypeInfo *info, PyObject *mapping)
1784 TupleDesc desc;
1785 HeapTuple tuple;
1786 Datum *values;
1787 bool *nulls;
1788 volatile int i;
1790 Assert(PyMapping_Check(mapping));
1792 desc = lookup_rowtype_tupdesc(info->out.d.typoid, -1);
1793 if (info->is_rowtype == 2)
1794 PLy_output_tuple_funcs(info, desc);
1795 Assert(info->is_rowtype == 1);
1797 /* Build tuple */
1798 values = palloc(sizeof(Datum) * desc->natts);
1799 nulls = palloc(sizeof(bool) * desc->natts);
1800 for (i = 0; i < desc->natts; ++i)
1802 char *key;
1803 PyObject *volatile value,
1804 *volatile so;
1806 key = NameStr(desc->attrs[i]->attname);
1807 value = so = NULL;
1808 PG_TRY();
1810 value = PyMapping_GetItemString(mapping, key);
1811 if (value == Py_None)
1813 values[i] = (Datum) NULL;
1814 nulls[i] = true;
1816 else if (value)
1818 char *valuestr;
1820 so = PyObject_Str(value);
1821 if (so == NULL)
1822 PLy_elog(ERROR, "could not compute string representation of Python object");
1823 valuestr = PyString_AsString(so);
1825 values[i] = InputFunctionCall(&info->out.r.atts[i].typfunc
1826 ,valuestr
1827 ,info->out.r.atts[i].typioparam
1828 ,-1);
1829 Py_DECREF(so);
1830 so = NULL;
1831 nulls[i] = false;
1833 else
1834 ereport(ERROR,
1835 (errcode(ERRCODE_UNDEFINED_COLUMN),
1836 errmsg("key \"%s\" not found in mapping", key),
1837 errhint("To return null in a column, "
1838 "add the value None to the mapping with the key named after the column.")));
1840 Py_XDECREF(value);
1841 value = NULL;
1843 PG_CATCH();
1845 Py_XDECREF(so);
1846 Py_XDECREF(value);
1847 PG_RE_THROW();
1849 PG_END_TRY();
1852 tuple = heap_form_tuple(desc, values, nulls);
1853 ReleaseTupleDesc(desc);
1854 pfree(values);
1855 pfree(nulls);
1857 return tuple;
1861 static HeapTuple
1862 PLySequence_ToTuple(PLyTypeInfo *info, PyObject *sequence)
1864 TupleDesc desc;
1865 HeapTuple tuple;
1866 Datum *values;
1867 bool *nulls;
1868 volatile int i;
1870 Assert(PySequence_Check(sequence));
1873 * Check that sequence length is exactly same as PG tuple's. We actually
1874 * can ignore exceeding items or assume missing ones as null but to avoid
1875 * plpython developer's errors we are strict here
1877 desc = lookup_rowtype_tupdesc(info->out.d.typoid, -1);
1878 if (PySequence_Length(sequence) != desc->natts)
1879 ereport(ERROR,
1880 (errcode(ERRCODE_DATATYPE_MISMATCH),
1881 errmsg("length of returned sequence did not match number of columns in row")));
1883 if (info->is_rowtype == 2)
1884 PLy_output_tuple_funcs(info, desc);
1885 Assert(info->is_rowtype == 1);
1887 /* Build tuple */
1888 values = palloc(sizeof(Datum) * desc->natts);
1889 nulls = palloc(sizeof(bool) * desc->natts);
1890 for (i = 0; i < desc->natts; ++i)
1892 PyObject *volatile value,
1893 *volatile so;
1895 value = so = NULL;
1896 PG_TRY();
1898 value = PySequence_GetItem(sequence, i);
1899 Assert(value);
1900 if (value == Py_None)
1902 values[i] = (Datum) NULL;
1903 nulls[i] = true;
1905 else if (value)
1907 char *valuestr;
1909 so = PyObject_Str(value);
1910 if (so == NULL)
1911 PLy_elog(ERROR, "could not compute string representation of Python object");
1912 valuestr = PyString_AsString(so);
1913 values[i] = InputFunctionCall(&info->out.r.atts[i].typfunc
1914 ,valuestr
1915 ,info->out.r.atts[i].typioparam
1916 ,-1);
1917 Py_DECREF(so);
1918 so = NULL;
1919 nulls[i] = false;
1922 Py_XDECREF(value);
1923 value = NULL;
1925 PG_CATCH();
1927 Py_XDECREF(so);
1928 Py_XDECREF(value);
1929 PG_RE_THROW();
1931 PG_END_TRY();
1934 tuple = heap_form_tuple(desc, values, nulls);
1935 ReleaseTupleDesc(desc);
1936 pfree(values);
1937 pfree(nulls);
1939 return tuple;
1943 static HeapTuple
1944 PLyObject_ToTuple(PLyTypeInfo *info, PyObject *object)
1946 TupleDesc desc;
1947 HeapTuple tuple;
1948 Datum *values;
1949 bool *nulls;
1950 volatile int i;
1952 desc = lookup_rowtype_tupdesc(info->out.d.typoid, -1);
1953 if (info->is_rowtype == 2)
1954 PLy_output_tuple_funcs(info, desc);
1955 Assert(info->is_rowtype == 1);
1957 /* Build tuple */
1958 values = palloc(sizeof(Datum) * desc->natts);
1959 nulls = palloc(sizeof(bool) * desc->natts);
1960 for (i = 0; i < desc->natts; ++i)
1962 char *key;
1963 PyObject *volatile value,
1964 *volatile so;
1966 key = NameStr(desc->attrs[i]->attname);
1967 value = so = NULL;
1968 PG_TRY();
1970 value = PyObject_GetAttrString(object, key);
1971 if (value == Py_None)
1973 values[i] = (Datum) NULL;
1974 nulls[i] = true;
1976 else if (value)
1978 char *valuestr;
1980 so = PyObject_Str(value);
1981 if (so == NULL)
1982 PLy_elog(ERROR, "could not compute string representation of Python object");
1983 valuestr = PyString_AsString(so);
1984 values[i] = InputFunctionCall(&info->out.r.atts[i].typfunc
1985 ,valuestr
1986 ,info->out.r.atts[i].typioparam
1987 ,-1);
1988 Py_DECREF(so);
1989 so = NULL;
1990 nulls[i] = false;
1992 else
1993 ereport(ERROR,
1994 (errcode(ERRCODE_UNDEFINED_COLUMN),
1995 errmsg("attribute \"%s\" does not exist in Python object", key),
1996 errhint("To return null in a column, "
1997 "let the returned object have an attribute named "
1998 "after column with value None.")));
2000 Py_XDECREF(value);
2001 value = NULL;
2003 PG_CATCH();
2005 Py_XDECREF(so);
2006 Py_XDECREF(value);
2007 PG_RE_THROW();
2009 PG_END_TRY();
2012 tuple = heap_form_tuple(desc, values, nulls);
2013 ReleaseTupleDesc(desc);
2014 pfree(values);
2015 pfree(nulls);
2017 return tuple;
2021 /* initialization, some python variables function declared here */
2023 /* interface to postgresql elog */
2024 static PyObject *PLy_debug(PyObject *, PyObject *);
2025 static PyObject *PLy_log(PyObject *, PyObject *);
2026 static PyObject *PLy_info(PyObject *, PyObject *);
2027 static PyObject *PLy_notice(PyObject *, PyObject *);
2028 static PyObject *PLy_warning(PyObject *, PyObject *);
2029 static PyObject *PLy_error(PyObject *, PyObject *);
2030 static PyObject *PLy_fatal(PyObject *, PyObject *);
2032 /* PLyPlanObject, PLyResultObject and SPI interface */
2033 #define is_PLyPlanObject(x) ((x)->ob_type == &PLy_PlanType)
2034 static PyObject *PLy_plan_new(void);
2035 static void PLy_plan_dealloc(PyObject *);
2036 static PyObject *PLy_plan_getattr(PyObject *, char *);
2037 static PyObject *PLy_plan_status(PyObject *, PyObject *);
2039 static PyObject *PLy_result_new(void);
2040 static void PLy_result_dealloc(PyObject *);
2041 static PyObject *PLy_result_getattr(PyObject *, char *);
2042 static PyObject *PLy_result_nrows(PyObject *, PyObject *);
2043 static PyObject *PLy_result_status(PyObject *, PyObject *);
2044 static Py_ssize_t PLy_result_length(PyObject *);
2045 static PyObject *PLy_result_item(PyObject *, Py_ssize_t);
2046 static PyObject *PLy_result_slice(PyObject *, Py_ssize_t, Py_ssize_t);
2047 static int PLy_result_ass_item(PyObject *, Py_ssize_t, PyObject *);
2048 static int PLy_result_ass_slice(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
2051 static PyObject *PLy_spi_prepare(PyObject *, PyObject *);
2052 static PyObject *PLy_spi_execute(PyObject *, PyObject *);
2053 static PyObject *PLy_spi_execute_query(char *query, long limit);
2054 static PyObject *PLy_spi_execute_plan(PyObject *, PyObject *, long);
2055 static PyObject *PLy_spi_execute_fetch_result(SPITupleTable *, int, int);
2058 static PyTypeObject PLy_PlanType = {
2059 PyObject_HEAD_INIT(NULL)
2060 0, /* ob_size */
2061 "PLyPlan", /* tp_name */
2062 sizeof(PLyPlanObject), /* tp_size */
2063 0, /* tp_itemsize */
2066 * methods
2068 PLy_plan_dealloc, /* tp_dealloc */
2069 0, /* tp_print */
2070 PLy_plan_getattr, /* tp_getattr */
2071 0, /* tp_setattr */
2072 0, /* tp_compare */
2073 0, /* tp_repr */
2074 0, /* tp_as_number */
2075 0, /* tp_as_sequence */
2076 0, /* tp_as_mapping */
2077 0, /* tp_hash */
2078 0, /* tp_call */
2079 0, /* tp_str */
2080 0, /* tp_getattro */
2081 0, /* tp_setattro */
2082 0, /* tp_as_buffer */
2083 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2084 PLy_plan_doc, /* tp_doc */
2087 static PyMethodDef PLy_plan_methods[] = {
2088 {"status", PLy_plan_status, METH_VARARGS, NULL},
2089 {NULL, NULL, 0, NULL}
2092 static PySequenceMethods PLy_result_as_sequence = {
2093 PLy_result_length, /* sq_length */
2094 NULL, /* sq_concat */
2095 NULL, /* sq_repeat */
2096 PLy_result_item, /* sq_item */
2097 PLy_result_slice, /* sq_slice */
2098 PLy_result_ass_item, /* sq_ass_item */
2099 PLy_result_ass_slice, /* sq_ass_slice */
2102 static PyTypeObject PLy_ResultType = {
2103 PyObject_HEAD_INIT(NULL)
2104 0, /* ob_size */
2105 "PLyResult", /* tp_name */
2106 sizeof(PLyResultObject), /* tp_size */
2107 0, /* tp_itemsize */
2110 * methods
2112 PLy_result_dealloc, /* tp_dealloc */
2113 0, /* tp_print */
2114 PLy_result_getattr, /* tp_getattr */
2115 0, /* tp_setattr */
2116 0, /* tp_compare */
2117 0, /* tp_repr */
2118 0, /* tp_as_number */
2119 &PLy_result_as_sequence, /* tp_as_sequence */
2120 0, /* tp_as_mapping */
2121 0, /* tp_hash */
2122 0, /* tp_call */
2123 0, /* tp_str */
2124 0, /* tp_getattro */
2125 0, /* tp_setattro */
2126 0, /* tp_as_buffer */
2127 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2128 PLy_result_doc, /* tp_doc */
2131 static PyMethodDef PLy_result_methods[] = {
2132 {"nrows", PLy_result_nrows, METH_VARARGS, NULL},
2133 {"status", PLy_result_status, METH_VARARGS, NULL},
2134 {NULL, NULL, 0, NULL}
2137 static PyMethodDef PLy_methods[] = {
2139 * logging methods
2141 {"debug", PLy_debug, METH_VARARGS, NULL},
2142 {"log", PLy_log, METH_VARARGS, NULL},
2143 {"info", PLy_info, METH_VARARGS, NULL},
2144 {"notice", PLy_notice, METH_VARARGS, NULL},
2145 {"warning", PLy_warning, METH_VARARGS, NULL},
2146 {"error", PLy_error, METH_VARARGS, NULL},
2147 {"fatal", PLy_fatal, METH_VARARGS, NULL},
2150 * create a stored plan
2152 {"prepare", PLy_spi_prepare, METH_VARARGS, NULL},
2155 * execute a plan or query
2157 {"execute", PLy_spi_execute, METH_VARARGS, NULL},
2159 {NULL, NULL, 0, NULL}
2163 /* plan object methods */
2164 static PyObject *
2165 PLy_plan_new(void)
2167 PLyPlanObject *ob;
2169 if ((ob = PyObject_NEW(PLyPlanObject, &PLy_PlanType)) == NULL)
2170 return NULL;
2172 ob->plan = NULL;
2173 ob->nargs = 0;
2174 ob->types = NULL;
2175 ob->args = NULL;
2177 return (PyObject *) ob;
2181 static void
2182 PLy_plan_dealloc(PyObject *arg)
2184 PLyPlanObject *ob = (PLyPlanObject *) arg;
2186 if (ob->plan)
2187 SPI_freeplan(ob->plan);
2188 if (ob->types)
2189 PLy_free(ob->types);
2190 if (ob->args)
2192 int i;
2194 for (i = 0; i < ob->nargs; i++)
2195 PLy_typeinfo_dealloc(&ob->args[i]);
2196 PLy_free(ob->args);
2199 arg->ob_type->tp_free(arg);
2203 static PyObject *
2204 PLy_plan_getattr(PyObject *self, char *name)
2206 return Py_FindMethod(PLy_plan_methods, self, name);
2209 static PyObject *
2210 PLy_plan_status(PyObject *self, PyObject *args)
2212 if (PyArg_ParseTuple(args, ""))
2214 Py_INCREF(Py_True);
2215 return Py_True;
2216 /* return PyInt_FromLong(self->status); */
2218 PLy_exception_set(PLy_exc_error, "plan.status takes no arguments");
2219 return NULL;
2224 /* result object methods */
2226 static PyObject *
2227 PLy_result_new(void)
2229 PLyResultObject *ob;
2231 if ((ob = PyObject_NEW(PLyResultObject, &PLy_ResultType)) == NULL)
2232 return NULL;
2234 /* ob->tuples = NULL; */
2236 Py_INCREF(Py_None);
2237 ob->status = Py_None;
2238 ob->nrows = PyInt_FromLong(-1);
2239 ob->rows = PyList_New(0);
2241 return (PyObject *) ob;
2244 static void
2245 PLy_result_dealloc(PyObject *arg)
2247 PLyResultObject *ob = (PLyResultObject *) arg;
2249 Py_XDECREF(ob->nrows);
2250 Py_XDECREF(ob->rows);
2251 Py_XDECREF(ob->status);
2253 arg->ob_type->tp_free(arg);
2256 static PyObject *
2257 PLy_result_getattr(PyObject *self, char *name)
2259 return Py_FindMethod(PLy_result_methods, self, name);
2262 static PyObject *
2263 PLy_result_nrows(PyObject *self, PyObject *args)
2265 PLyResultObject *ob = (PLyResultObject *) self;
2267 Py_INCREF(ob->nrows);
2268 return ob->nrows;
2271 static PyObject *
2272 PLy_result_status(PyObject *self, PyObject *args)
2274 PLyResultObject *ob = (PLyResultObject *) self;
2276 Py_INCREF(ob->status);
2277 return ob->status;
2280 static Py_ssize_t
2281 PLy_result_length(PyObject *arg)
2283 PLyResultObject *ob = (PLyResultObject *) arg;
2285 return PyList_Size(ob->rows);
2288 static PyObject *
2289 PLy_result_item(PyObject *arg, Py_ssize_t idx)
2291 PyObject *rv;
2292 PLyResultObject *ob = (PLyResultObject *) arg;
2294 rv = PyList_GetItem(ob->rows, idx);
2295 if (rv != NULL)
2296 Py_INCREF(rv);
2297 return rv;
2300 static int
2301 PLy_result_ass_item(PyObject *arg, Py_ssize_t idx, PyObject *item)
2303 int rv;
2304 PLyResultObject *ob = (PLyResultObject *) arg;
2306 Py_INCREF(item);
2307 rv = PyList_SetItem(ob->rows, idx, item);
2308 return rv;
2311 static PyObject *
2312 PLy_result_slice(PyObject *arg, Py_ssize_t lidx, Py_ssize_t hidx)
2314 PyObject *rv;
2315 PLyResultObject *ob = (PLyResultObject *) arg;
2317 rv = PyList_GetSlice(ob->rows, lidx, hidx);
2318 if (rv == NULL)
2319 return NULL;
2320 Py_INCREF(rv);
2321 return rv;
2324 static int
2325 PLy_result_ass_slice(PyObject *arg, Py_ssize_t lidx, Py_ssize_t hidx, PyObject *slice)
2327 int rv;
2328 PLyResultObject *ob = (PLyResultObject *) arg;
2330 rv = PyList_SetSlice(ob->rows, lidx, hidx, slice);
2331 return rv;
2334 /* SPI interface */
2335 static PyObject *
2336 PLy_spi_prepare(PyObject *self, PyObject *args)
2338 PLyPlanObject *plan;
2339 PyObject *list = NULL;
2340 PyObject *volatile optr = NULL;
2341 char *query;
2342 void *tmpplan;
2343 MemoryContext oldcontext;
2345 /* Can't execute more if we have an unhandled error */
2346 if (PLy_error_in_progress)
2348 PLy_exception_set(PLy_exc_error, "transaction aborted");
2349 return NULL;
2352 if (!PyArg_ParseTuple(args, "s|O", &query, &list))
2354 PLy_exception_set(PLy_exc_spi_error,
2355 "invalid arguments for plpy.prepare");
2356 return NULL;
2359 if (list && (!PySequence_Check(list)))
2361 PLy_exception_set(PLy_exc_spi_error,
2362 "second argument of plpy.prepare must be a sequence");
2363 return NULL;
2366 if ((plan = (PLyPlanObject *) PLy_plan_new()) == NULL)
2367 return NULL;
2369 oldcontext = CurrentMemoryContext;
2370 PG_TRY();
2372 if (list != NULL)
2374 int nargs,
2377 nargs = PySequence_Length(list);
2378 if (nargs > 0)
2380 plan->nargs = nargs;
2381 plan->types = PLy_malloc(sizeof(Oid) * nargs);
2382 plan->values = PLy_malloc(sizeof(Datum) * nargs);
2383 plan->args = PLy_malloc(sizeof(PLyTypeInfo) * nargs);
2386 * the other loop might throw an exception, if PLyTypeInfo
2387 * member isn't properly initialized the Py_DECREF(plan) will
2388 * go boom
2390 for (i = 0; i < nargs; i++)
2392 PLy_typeinfo_init(&plan->args[i]);
2393 plan->values[i] = PointerGetDatum(NULL);
2396 for (i = 0; i < nargs; i++)
2398 char *sptr;
2399 HeapTuple typeTup;
2400 Oid typeId;
2401 int32 typmod;
2402 Form_pg_type typeStruct;
2404 optr = PySequence_GetItem(list, i);
2405 if (!PyString_Check(optr))
2406 ereport(ERROR,
2407 (errmsg("plpy.prepare: type name at ordinal position %d is not a string", i)));
2408 sptr = PyString_AsString(optr);
2410 /********************************************************
2411 * Resolve argument type names and then look them up by
2412 * oid in the system cache, and remember the required
2413 *information for input conversion.
2414 ********************************************************/
2416 parseTypeString(sptr, &typeId, &typmod);
2418 typeTup = SearchSysCache(TYPEOID,
2419 ObjectIdGetDatum(typeId),
2420 0, 0, 0);
2421 if (!HeapTupleIsValid(typeTup))
2422 elog(ERROR, "cache lookup failed for type %u", typeId);
2424 Py_DECREF(optr);
2425 optr = NULL; /* this is important */
2427 plan->types[i] = typeId;
2428 typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
2429 if (typeStruct->typtype != TYPTYPE_COMPOSITE)
2430 PLy_output_datum_func(&plan->args[i], typeTup);
2431 else
2432 ereport(ERROR,
2433 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2434 errmsg("plpy.prepare does not support composite types")));
2435 ReleaseSysCache(typeTup);
2440 plan->plan = SPI_prepare(query, plan->nargs, plan->types);
2441 if (plan->plan == NULL)
2442 elog(ERROR, "SPI_prepare failed: %s",
2443 SPI_result_code_string(SPI_result));
2445 /* transfer plan from procCxt to topCxt */
2446 tmpplan = plan->plan;
2447 plan->plan = SPI_saveplan(tmpplan);
2448 SPI_freeplan(tmpplan);
2449 if (plan->plan == NULL)
2450 elog(ERROR, "SPI_saveplan failed: %s",
2451 SPI_result_code_string(SPI_result));
2453 PG_CATCH();
2455 MemoryContextSwitchTo(oldcontext);
2456 PLy_error_in_progress = CopyErrorData();
2457 FlushErrorState();
2458 Py_DECREF(plan);
2459 Py_XDECREF(optr);
2460 if (!PyErr_Occurred())
2461 PLy_exception_set(PLy_exc_spi_error,
2462 "unrecognized error in PLy_spi_prepare");
2463 /* XXX this oughta be replaced with errcontext mechanism */
2464 PLy_elog(WARNING, "in PL/Python function \"%s\"",
2465 PLy_procedure_name(PLy_curr_procedure));
2466 return NULL;
2468 PG_END_TRY();
2470 return (PyObject *) plan;
2473 /* execute(query="select * from foo", limit=5)
2474 * execute(plan=plan, values=(foo, bar), limit=5)
2476 static PyObject *
2477 PLy_spi_execute(PyObject *self, PyObject *args)
2479 char *query;
2480 PyObject *plan;
2481 PyObject *list = NULL;
2482 long limit = 0;
2484 /* Can't execute more if we have an unhandled error */
2485 if (PLy_error_in_progress)
2487 PLy_exception_set(PLy_exc_error, "transaction aborted");
2488 return NULL;
2491 if (PyArg_ParseTuple(args, "s|l", &query, &limit))
2492 return PLy_spi_execute_query(query, limit);
2494 PyErr_Clear();
2496 if (PyArg_ParseTuple(args, "O|Ol", &plan, &list, &limit) &&
2497 is_PLyPlanObject(plan))
2498 return PLy_spi_execute_plan(plan, list, limit);
2500 PLy_exception_set(PLy_exc_error, "plpy.execute expected a query or a plan");
2501 return NULL;
2504 static PyObject *
2505 PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit)
2507 volatile int nargs;
2508 int i,
2510 PLyPlanObject *plan;
2511 MemoryContext oldcontext;
2513 if (list != NULL)
2515 if (!PySequence_Check(list) || PyString_Check(list))
2517 PLy_exception_set(PLy_exc_spi_error, "plpy.execute takes a sequence as its second argument");
2518 return NULL;
2520 nargs = PySequence_Length(list);
2522 else
2523 nargs = 0;
2525 plan = (PLyPlanObject *) ob;
2527 if (nargs != plan->nargs)
2529 char *sv;
2530 PyObject *so = PyObject_Str(list);
2532 if (!so)
2533 PLy_elog(ERROR, "PL/Python function \"%s\" could not execute plan",
2534 PLy_procedure_name(PLy_curr_procedure));
2535 sv = PyString_AsString(so);
2536 PLy_exception_set_plural(PLy_exc_spi_error,
2537 "Expected sequence of %d argument, got %d: %s",
2538 "Expected sequence of %d arguments, got %d: %s",
2539 plan->nargs,
2540 plan->nargs, nargs, sv);
2541 Py_DECREF(so);
2543 return NULL;
2546 oldcontext = CurrentMemoryContext;
2547 PG_TRY();
2549 char *nulls = palloc(nargs * sizeof(char));
2550 volatile int j;
2552 for (j = 0; j < nargs; j++)
2554 PyObject *elem,
2555 *so;
2557 elem = PySequence_GetItem(list, j);
2558 if (elem != Py_None)
2560 so = PyObject_Str(elem);
2561 if (!so)
2562 PLy_elog(ERROR, "PL/Python function \"%s\" could not execute plan",
2563 PLy_procedure_name(PLy_curr_procedure));
2564 Py_DECREF(elem);
2566 PG_TRY();
2568 char *sv = PyString_AsString(so);
2570 plan->values[j] =
2571 InputFunctionCall(&(plan->args[j].out.d.typfunc),
2573 plan->args[j].out.d.typioparam,
2574 -1);
2576 PG_CATCH();
2578 Py_DECREF(so);
2579 PG_RE_THROW();
2581 PG_END_TRY();
2583 Py_DECREF(so);
2584 nulls[j] = ' ';
2586 else
2588 Py_DECREF(elem);
2589 plan->values[j] =
2590 InputFunctionCall(&(plan->args[j].out.d.typfunc),
2591 NULL,
2592 plan->args[j].out.d.typioparam,
2593 -1);
2594 nulls[j] = 'n';
2598 rv = SPI_execute_plan(plan->plan, plan->values, nulls,
2599 PLy_curr_procedure->fn_readonly, limit);
2601 pfree(nulls);
2603 PG_CATCH();
2605 int k;
2607 MemoryContextSwitchTo(oldcontext);
2608 PLy_error_in_progress = CopyErrorData();
2609 FlushErrorState();
2612 * cleanup plan->values array
2614 for (k = 0; k < nargs; k++)
2616 if (!plan->args[k].out.d.typbyval &&
2617 (plan->values[k] != PointerGetDatum(NULL)))
2619 pfree(DatumGetPointer(plan->values[k]));
2620 plan->values[k] = PointerGetDatum(NULL);
2624 if (!PyErr_Occurred())
2625 PLy_exception_set(PLy_exc_error,
2626 "unrecognized error in PLy_spi_execute_plan");
2627 /* XXX this oughta be replaced with errcontext mechanism */
2628 PLy_elog(WARNING, "in PL/Python function \"%s\"",
2629 PLy_procedure_name(PLy_curr_procedure));
2630 return NULL;
2632 PG_END_TRY();
2634 for (i = 0; i < nargs; i++)
2636 if (!plan->args[i].out.d.typbyval &&
2637 (plan->values[i] != PointerGetDatum(NULL)))
2639 pfree(DatumGetPointer(plan->values[i]));
2640 plan->values[i] = PointerGetDatum(NULL);
2644 if (rv < 0)
2646 PLy_exception_set(PLy_exc_spi_error,
2647 "SPI_execute_plan failed: %s",
2648 SPI_result_code_string(rv));
2649 return NULL;
2652 return PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
2655 static PyObject *
2656 PLy_spi_execute_query(char *query, long limit)
2658 int rv;
2659 MemoryContext oldcontext;
2661 oldcontext = CurrentMemoryContext;
2662 PG_TRY();
2664 rv = SPI_execute(query, PLy_curr_procedure->fn_readonly, limit);
2666 PG_CATCH();
2668 MemoryContextSwitchTo(oldcontext);
2669 PLy_error_in_progress = CopyErrorData();
2670 FlushErrorState();
2671 if (!PyErr_Occurred())
2672 PLy_exception_set(PLy_exc_spi_error,
2673 "unrecognized error in PLy_spi_execute_query");
2674 /* XXX this oughta be replaced with errcontext mechanism */
2675 PLy_elog(WARNING, "in PL/Python function \"%s\"",
2676 PLy_procedure_name(PLy_curr_procedure));
2677 return NULL;
2679 PG_END_TRY();
2681 if (rv < 0)
2683 PLy_exception_set(PLy_exc_spi_error,
2684 "SPI_execute failed: %s",
2685 SPI_result_code_string(rv));
2686 return NULL;
2689 return PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
2692 static PyObject *
2693 PLy_spi_execute_fetch_result(SPITupleTable *tuptable, int rows, int status)
2695 PLyResultObject *result;
2696 MemoryContext oldcontext;
2698 result = (PLyResultObject *) PLy_result_new();
2699 Py_DECREF(result->status);
2700 result->status = PyInt_FromLong(status);
2702 if (status > 0 && tuptable == NULL)
2704 Py_DECREF(result->nrows);
2705 result->nrows = PyInt_FromLong(rows);
2707 else if (status > 0 && tuptable != NULL)
2709 PLyTypeInfo args;
2710 int i;
2712 Py_DECREF(result->nrows);
2713 result->nrows = PyInt_FromLong(rows);
2714 PLy_typeinfo_init(&args);
2716 oldcontext = CurrentMemoryContext;
2717 PG_TRY();
2719 if (rows)
2721 Py_DECREF(result->rows);
2722 result->rows = PyList_New(rows);
2724 PLy_input_tuple_funcs(&args, tuptable->tupdesc);
2725 for (i = 0; i < rows; i++)
2727 PyObject *row = PLyDict_FromTuple(&args, tuptable->vals[i],
2728 tuptable->tupdesc);
2730 PyList_SetItem(result->rows, i, row);
2732 PLy_typeinfo_dealloc(&args);
2734 SPI_freetuptable(tuptable);
2737 PG_CATCH();
2739 MemoryContextSwitchTo(oldcontext);
2740 PLy_error_in_progress = CopyErrorData();
2741 FlushErrorState();
2742 if (!PyErr_Occurred())
2743 PLy_exception_set(PLy_exc_error,
2744 "unrecognized error in PLy_spi_execute_fetch_result");
2745 Py_DECREF(result);
2746 PLy_typeinfo_dealloc(&args);
2747 return NULL;
2749 PG_END_TRY();
2752 return (PyObject *) result;
2757 * language handler and interpreter initialization
2761 * _PG_init() - library load-time initialization
2763 * DO NOT make this static nor change its name!
2765 void
2766 _PG_init(void)
2768 /* Be sure we do initialization only once (should be redundant now) */
2769 static bool inited = false;
2771 if (inited)
2772 return;
2774 pg_bindtextdomain(TEXTDOMAIN);
2776 Py_Initialize();
2777 PLy_init_interp();
2778 PLy_init_plpy();
2779 if (PyErr_Occurred())
2780 PLy_elog(FATAL, "untrapped error in initialization");
2781 PLy_procedure_cache = PyDict_New();
2782 if (PLy_procedure_cache == NULL)
2783 PLy_elog(ERROR, "could not create procedure cache");
2785 inited = true;
2788 static void
2789 PLy_init_interp(void)
2791 PyObject *mainmod;
2793 mainmod = PyImport_AddModule("__main__");
2794 if (mainmod == NULL || PyErr_Occurred())
2795 PLy_elog(ERROR, "could not import \"__main__\" module");
2796 Py_INCREF(mainmod);
2797 PLy_interp_globals = PyModule_GetDict(mainmod);
2798 PLy_interp_safe_globals = PyDict_New();
2799 PyDict_SetItemString(PLy_interp_globals, "GD", PLy_interp_safe_globals);
2800 Py_DECREF(mainmod);
2801 if (PLy_interp_globals == NULL || PyErr_Occurred())
2802 PLy_elog(ERROR, "could not initialize globals");
2805 static void
2806 PLy_init_plpy(void)
2808 PyObject *main_mod,
2809 *main_dict,
2810 *plpy_mod;
2811 PyObject *plpy,
2812 *plpy_dict;
2815 * initialize plpy module
2817 if (PyType_Ready(&PLy_PlanType) < 0)
2818 elog(ERROR, "could not initialize PLy_PlanType");
2819 if (PyType_Ready(&PLy_ResultType) < 0)
2820 elog(ERROR, "could not initialize PLy_ResultType");
2822 plpy = Py_InitModule("plpy", PLy_methods);
2823 plpy_dict = PyModule_GetDict(plpy);
2825 /* PyDict_SetItemString(plpy, "PlanType", (PyObject *) &PLy_PlanType); */
2827 PLy_exc_error = PyErr_NewException("plpy.Error", NULL, NULL);
2828 PLy_exc_fatal = PyErr_NewException("plpy.Fatal", NULL, NULL);
2829 PLy_exc_spi_error = PyErr_NewException("plpy.SPIError", NULL, NULL);
2830 PyDict_SetItemString(plpy_dict, "Error", PLy_exc_error);
2831 PyDict_SetItemString(plpy_dict, "Fatal", PLy_exc_fatal);
2832 PyDict_SetItemString(plpy_dict, "SPIError", PLy_exc_spi_error);
2835 * initialize main module, and add plpy
2837 main_mod = PyImport_AddModule("__main__");
2838 main_dict = PyModule_GetDict(main_mod);
2839 plpy_mod = PyImport_AddModule("plpy");
2840 PyDict_SetItemString(main_dict, "plpy", plpy_mod);
2841 if (PyErr_Occurred())
2842 elog(ERROR, "could not initialize plpy");
2845 /* the python interface to the elog function
2846 * don't confuse these with PLy_elog
2848 static PyObject *PLy_output(volatile int, PyObject *, PyObject *);
2850 static PyObject *
2851 PLy_debug(PyObject *self, PyObject *args)
2853 return PLy_output(DEBUG2, self, args);
2856 static PyObject *
2857 PLy_log(PyObject *self, PyObject *args)
2859 return PLy_output(LOG, self, args);
2862 static PyObject *
2863 PLy_info(PyObject *self, PyObject *args)
2865 return PLy_output(INFO, self, args);
2868 static PyObject *
2869 PLy_notice(PyObject *self, PyObject *args)
2871 return PLy_output(NOTICE, self, args);
2874 static PyObject *
2875 PLy_warning(PyObject *self, PyObject *args)
2877 return PLy_output(WARNING, self, args);
2880 static PyObject *
2881 PLy_error(PyObject *self, PyObject *args)
2883 return PLy_output(ERROR, self, args);
2886 static PyObject *
2887 PLy_fatal(PyObject *self, PyObject *args)
2889 return PLy_output(FATAL, self, args);
2893 static PyObject *
2894 PLy_output(volatile int level, PyObject *self, PyObject *args)
2896 PyObject *so;
2897 char *volatile sv;
2898 MemoryContext oldcontext;
2900 so = PyObject_Str(args);
2901 if (so == NULL || ((sv = PyString_AsString(so)) == NULL))
2903 level = ERROR;
2904 sv = dgettext(TEXTDOMAIN, "could not parse error message in plpy.elog");
2907 oldcontext = CurrentMemoryContext;
2908 PG_TRY();
2910 elog(level, "%s", sv);
2912 PG_CATCH();
2914 MemoryContextSwitchTo(oldcontext);
2915 PLy_error_in_progress = CopyErrorData();
2916 FlushErrorState();
2917 Py_XDECREF(so);
2920 * returning NULL here causes the python interpreter to bail. when
2921 * control passes back to PLy_procedure_call, we check for PG
2922 * exceptions and re-throw the error.
2924 PyErr_SetString(PLy_exc_error, sv);
2925 return NULL;
2927 PG_END_TRY();
2929 Py_XDECREF(so);
2932 * return a legal object so the interpreter will continue on its merry way
2934 Py_INCREF(Py_None);
2935 return Py_None;
2940 * Get the name of the last procedure called by the backend (the
2941 * innermost, if a plpython procedure call calls the backend and the
2942 * backend calls another plpython procedure).
2944 * NB: this returns the SQL name, not the internal Python procedure name
2946 static char *
2947 PLy_procedure_name(PLyProcedure *proc)
2949 if (proc == NULL)
2950 return "<unknown procedure>";
2951 return proc->proname;
2955 * Call PyErr_SetString with a vprint interface and translation support
2957 static void
2958 PLy_exception_set(PyObject *exc, const char *fmt,...)
2960 char buf[1024];
2961 va_list ap;
2963 va_start(ap, fmt);
2964 vsnprintf(buf, sizeof(buf), dgettext(TEXTDOMAIN, fmt), ap);
2965 va_end(ap);
2967 PyErr_SetString(exc, buf);
2971 * The same, pluralized.
2973 static void
2974 PLy_exception_set_plural(PyObject *exc,
2975 const char *fmt_singular, const char *fmt_plural,
2976 unsigned long n,...)
2978 char buf[1024];
2979 va_list ap;
2981 va_start(ap, n);
2982 vsnprintf(buf, sizeof(buf),
2983 dngettext(TEXTDOMAIN, fmt_singular, fmt_plural, n),
2984 ap);
2985 va_end(ap);
2987 PyErr_SetString(exc, buf);
2990 /* Emit a PG error or notice, together with any available info about the
2991 * current Python error. This should be used to propagate Python errors
2992 * into PG.
2994 static void
2995 PLy_elog(int elevel, const char *fmt,...)
2997 char *xmsg;
2998 int xlevel;
2999 StringInfoData emsg;
3001 xmsg = PLy_traceback(&xlevel);
3003 initStringInfo(&emsg);
3004 for (;;)
3006 va_list ap;
3007 bool success;
3009 va_start(ap, fmt);
3010 success = appendStringInfoVA(&emsg, dgettext(TEXTDOMAIN, fmt), ap);
3011 va_end(ap);
3012 if (success)
3013 break;
3014 enlargeStringInfo(&emsg, emsg.maxlen);
3017 PG_TRY();
3019 ereport(elevel,
3020 (errmsg("PL/Python: %s", emsg.data),
3021 (xmsg) ? errdetail("%s", xmsg) : 0));
3023 PG_CATCH();
3025 pfree(emsg.data);
3026 if (xmsg)
3027 pfree(xmsg);
3028 PG_RE_THROW();
3030 PG_END_TRY();
3032 pfree(emsg.data);
3033 if (xmsg)
3034 pfree(xmsg);
3037 static char *
3038 PLy_traceback(int *xlevel)
3040 PyObject *e,
3042 *tb;
3043 PyObject *eob,
3044 *vob = NULL;
3045 char *vstr,
3046 *estr;
3047 StringInfoData xstr;
3050 * get the current exception
3052 PyErr_Fetch(&e, &v, &tb);
3055 * oops, no exception, return
3057 if (e == NULL)
3059 *xlevel = WARNING;
3060 return NULL;
3063 PyErr_NormalizeException(&e, &v, &tb);
3064 Py_XDECREF(tb);
3066 eob = PyObject_Str(e);
3067 if (v && ((vob = PyObject_Str(v)) != NULL))
3068 vstr = PyString_AsString(vob);
3069 else
3070 vstr = "unknown";
3073 * I'm not sure what to do if eob is NULL here -- we can't call PLy_elog
3074 * because that function calls us, so we could end up with infinite
3075 * recursion. I'm not even sure if eob could be NULL here -- would an
3076 * Assert() be more appropriate?
3078 estr = eob ? PyString_AsString(eob) : "unrecognized exception";
3079 initStringInfo(&xstr);
3080 appendStringInfo(&xstr, "%s: %s", estr, vstr);
3082 Py_DECREF(eob);
3083 Py_XDECREF(vob);
3084 Py_XDECREF(v);
3087 * intuit an appropriate error level based on the exception type
3089 if (PLy_exc_error && PyErr_GivenExceptionMatches(e, PLy_exc_error))
3090 *xlevel = ERROR;
3091 else if (PLy_exc_fatal && PyErr_GivenExceptionMatches(e, PLy_exc_fatal))
3092 *xlevel = FATAL;
3093 else
3094 *xlevel = ERROR;
3096 Py_DECREF(e);
3097 return xstr.data;
3100 /* python module code */
3102 /* some dumb utility functions */
3103 static void *
3104 PLy_malloc(size_t bytes)
3106 void *ptr = malloc(bytes);
3108 if (ptr == NULL)
3109 ereport(FATAL,
3110 (errcode(ERRCODE_OUT_OF_MEMORY),
3111 errmsg("out of memory")));
3112 return ptr;
3115 static void *
3116 PLy_malloc0(size_t bytes)
3118 void *ptr = PLy_malloc(bytes);
3120 MemSet(ptr, 0, bytes);
3121 return ptr;
3124 static char *
3125 PLy_strdup(const char *str)
3127 char *result;
3128 size_t len;
3130 len = strlen(str) + 1;
3131 result = PLy_malloc(len);
3132 memcpy(result, str, len);
3134 return result;
3137 /* define this away */
3138 static void
3139 PLy_free(void *ptr)
3141 free(ptr);