1 /**********************************************************************
2 * plpython.c - python as a procedural language for PostgreSQL
6 *********************************************************************
9 #if defined(_MSC_VER) && defined(_DEBUG)
10 /* Python uses #pragma to bring in a non-default libpython on VC++ if
11 * _DEBUG is defined */
13 /* Also hide away errcode, since we load Python.h before postgres.h */
14 #define errcode __msvc_errcode
18 #elif defined (_MSC_VER)
19 #define errcode __msvc_errcode
27 * Py_ssize_t compat for Python <= 2.4
29 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
30 typedef int Py_ssize_t
;
32 #define PY_SSIZE_T_MAX INT_MAX
33 #define PY_SSIZE_T_MIN INT_MIN
37 * PyBool_FromLong is supported from 2.3.
39 #if PY_VERSION_HEX < 0x02030000
40 #define PyBool_FromLong(x) PyInt_FromLong(x)
50 /* postgreSQL stuff */
51 #include "catalog/pg_proc.h"
52 #include "catalog/pg_type.h"
53 #include "commands/trigger.h"
54 #include "executor/spi.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 */
69 #define TEXTDOMAIN "plpython"
76 /* convert Postgresql Datum or tuple into a PyObject.
77 * input to Python. Tuples are converted to dictionary
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 */
92 typedef struct PLyTupleToOb
98 typedef union PLyTypeInput
104 /* convert PyObject to a Postgresql Datum or tuple.
107 typedef struct PLyObToDatum
109 FmgrInfo typfunc
; /* The type's input function */
110 Oid typoid
; /* The OID of the type */
115 typedef struct PLyObToTuple
121 typedef union PLyTypeOutput
127 /* all we need to move Postgresql data to Python objects,
130 typedef struct PLyTypeInfo
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
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
;
151 PLyTypeInfo result
; /* also used to store info for trigger tuple
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
];
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
167 typedef struct PLyPlanObject
170 void *plan
; /* return of an SPI_saveplan */
177 typedef struct PLyResultObject
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_* */
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
);
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
204 static void PLy_init_interp(void);
205 static void PLy_init_plpy(void);
207 /* call PyErr_SetString with a vprint interface */
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
*,
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
,
240 static PLyProcedure
*PLy_procedure_create(HeapTuple procTup
, Oid tgreloid
,
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
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.
321 perm_fmgr_info(Oid functionId
, FmgrInfo
*finfo
)
323 fmgr_info_cxt(functionId
, finfo
, TopMemoryContext
);
327 plpython_call_handler(PG_FUNCTION_ARGS
)
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
;
340 if (CALLED_AS_TRIGGER(fcinfo
))
342 TriggerData
*tdata
= (TriggerData
*) fcinfo
->context
;
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
);
353 proc
= PLy_procedure_get(fcinfo
, InvalidOid
);
354 PLy_curr_procedure
= proc
;
355 retval
= PLy_function_handler(fcinfo
, proc
);
360 PLy_curr_procedure
= save_curr_proc
;
363 /* note: Py_DECREF needs braces around it, as of 2003/08 */
371 PLy_curr_procedure
= save_curr_proc
;
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.
389 PLy_trigger_handler(FunctionCallInfo fcinfo
, PLyProcedure
* proc
)
392 PyObject
*volatile plargs
= NULL
;
393 PyObject
*volatile plrv
= NULL
;
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
416 if (!PyString_Check(plrv
))
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)
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
);
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
442 (errcode(ERRCODE_DATA_EXCEPTION
),
443 errmsg("unexpected return value from trigger procedure"),
444 errdetail("Expected None, \"OK\", \"SKIP\", or \"MODIFY\".")));
464 PLy_modify_tuple(PLyProcedure
* proc
, PyObject
* pltd
, TriggerData
*tdata
,
467 PyObject
*volatile plntup
;
468 PyObject
*volatile plkeys
;
469 PyObject
*volatile platt
;
470 PyObject
*volatile plval
;
471 PyObject
*volatile plstr
;
477 int *volatile modattrs
;
478 Datum
*volatile modvalues
;
479 char *volatile modnulls
;
482 plntup
= plkeys
= platt
= plval
= plstr
= NULL
;
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");
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
++)
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
));
517 plval
= PyDict_GetItem(plntup
, platt
);
519 elog(FATAL
, "python interpreter is probably corrupted");
525 if (tupdesc
->attrs
[atti
]->attisdropped
)
527 modvalues
[i
] = (Datum
) 0;
530 else if (plval
!= Py_None
)
532 plstr
= PyObject_Str(plval
);
534 PLy_elog(ERROR
, "function \"%s\" could not modify tuple",
536 src
= PyString_AsString(plstr
);
539 InputFunctionCall(&proc
->result
.out
.r
.atts
[atti
].typfunc
,
541 proc
->result
.out
.r
.atts
[atti
].typioparam
,
542 tupdesc
->attrs
[atti
]->atttypmod
);
551 InputFunctionCall(&proc
->result
.out
.r
.atts
[atti
].typfunc
,
553 proc
->result
.out
.r
.atts
[atti
].typioparam
,
554 tupdesc
->attrs
[atti
]->atttypmod
);
562 rtup
= SPI_modifytuple(tdata
->tg_relation
, otup
, natts
,
563 modattrs
, modvalues
, modnulls
);
565 elog(ERROR
, "SPI_modifytuple failed -- error %d", SPI_result
);
596 PLy_trigger_build_args(FunctionCallInfo fcinfo
, PLyProcedure
* proc
, HeapTuple
*rv
)
598 TriggerData
*tdata
= (TriggerData
*) fcinfo
->context
;
609 PyObject
*volatile pltdata
= NULL
;
614 pltdata
= PyDict_New();
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
);
622 stroid
= DatumGetCString(DirectFunctionCall1(oidout
,
623 ObjectIdGetDatum(tdata
->tg_relation
->rd_id
)));
624 pltrelid
= PyString_FromString(stroid
);
625 PyDict_SetItemString(pltdata
, "relid", pltrelid
);
629 stroid
= SPI_getrelname(tdata
->tg_relation
);
630 plttablename
= PyString_FromString(stroid
);
631 PyDict_SetItemString(pltdata
, "table_name", plttablename
);
632 Py_DECREF(plttablename
);
635 stroid
= SPI_getnspname(tdata
->tg_relation
);
636 plttableschema
= PyString_FromString(stroid
);
637 PyDict_SetItemString(pltdata
, "table_schema", plttableschema
);
638 Py_DECREF(plttableschema
);
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");
648 elog(ERROR
, "unrecognized WHEN tg_event: %u", tdata
->tg_event
);
649 pltwhen
= NULL
; /* keep compiler quiet */
651 PyDict_SetItemString(pltdata
, "when", pltwhen
);
654 if (TRIGGER_FIRED_FOR_ROW(tdata
->tg_event
))
656 pltlevel
= PyString_FromString("ROW");
657 PyDict_SetItemString(pltdata
, "level", 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
);
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
);
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
);
690 pytold
= PLyDict_FromTuple(&(proc
->result
), tdata
->tg_trigtuple
,
691 tdata
->tg_relation
->rd_att
);
692 PyDict_SetItemString(pltdata
, "old", pytold
);
694 *rv
= tdata
->tg_newtuple
;
698 elog(ERROR
, "unrecognized OP tg_event: %u", tdata
->tg_event
);
699 pltevent
= NULL
; /* keep compiler quiet */
702 PyDict_SetItemString(pltdata
, "event", pltevent
);
705 else if (TRIGGER_FIRED_FOR_STATEMENT(tdata
->tg_event
))
707 pltlevel
= PyString_FromString("STATEMENT");
708 PyDict_SetItemString(pltdata
, "level", pltlevel
);
711 PyDict_SetItemString(pltdata
, "old", Py_None
);
712 PyDict_SetItemString(pltdata
, "new", Py_None
);
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");
725 elog(ERROR
, "unrecognized OP tg_event: %u", tdata
->tg_event
);
726 pltevent
= NULL
; /* keep compiler quiet */
729 PyDict_SetItemString(pltdata
, "event", pltevent
);
733 elog(ERROR
, "unrecognized LEVEL tg_event: %u", tdata
->tg_event
);
735 if (tdata
->tg_trigger
->tgnargs
)
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
);
759 PyDict_SetItemString(pltdata
, "args", pltargs
);
774 /* function handler and friends */
776 PLy_function_handler(FunctionCallInfo fcinfo
, PLyProcedure
* proc
)
779 PyObject
*volatile plargs
= NULL
;
780 PyObject
*volatile plrv
= NULL
;
781 PyObject
*volatile plrv_so
= NULL
;
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
);
794 * SETOF function parameters will be deleted when last row is
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
808 if (SPI_finish() != SPI_OK_FINISH
)
809 elog(ERROR
, "SPI_finish failed");
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)
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
);
833 if (proc
->setof
== NULL
)
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
);
843 rsi
->isDone
= ExprMultipleResult
;
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
);
860 PLy_function_delete_args(proc
);
864 (errcode(ERRCODE_DATA_EXCEPTION
),
865 errmsg("error fetching next item from iterator")));
867 fcinfo
->isnull
= true;
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
)
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;
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
,
895 proc
->result
.out
.d
.typioparam
,
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
);
912 /* returned as smth, must provide method __getattr__(name) */
913 tuple
= PLyObject_ToTuple(&proc
->result
, plrv
);
917 fcinfo
->isnull
= false;
918 rv
= HeapTupleGetDatum(tuple
);
922 fcinfo
->isnull
= true;
928 fcinfo
->isnull
= false;
929 plrv_so
= PyObject_Str(plrv
);
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
,
935 proc
->result
.out
.d
.typioparam
,
957 PLy_procedure_call(PLyProcedure
* proc
, char *kargs
, PyObject
* vargs
)
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
;
977 if (rv
== NULL
|| PyErr_Occurred())
980 PLy_elog(ERROR
, "function \"%s\" failed", proc
->proname
);
987 PLy_function_build_args(FunctionCallInfo fcinfo
, PLyProcedure
* proc
)
989 PyObject
*volatile arg
= NULL
;
990 PyObject
*volatile args
= NULL
;
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
])
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
);
1024 arg
= PLyDict_FromTuple(&(proc
->args
[i
]), &tmptup
, tupdesc
);
1025 ReleaseTupleDesc(tupdesc
);
1030 if (fcinfo
->argnull
[i
])
1036 ct
= OutputFunctionCall(&(proc
->args
[i
].in
.d
.typfunc
),
1038 arg
= (proc
->args
[i
].in
.d
.func
) (ct
);
1049 if (PyList_SetItem(args
, i
, arg
) == -1 ||
1051 PyDict_SetItemString(proc
->globals
, proc
->argnames
[i
], arg
) == -1))
1052 PLy_elog(ERROR
, "problem setting up arguments for \"%s\"", proc
->proname
);
1070 PLy_function_delete_args(PLyProcedure
* proc
)
1074 if (!proc
->argnames
)
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
1091 static PLyProcedure
*
1092 PLy_procedure_get(FunctionCallInfo fcinfo
, Oid tgreloid
)
1098 PLyProcedure
*proc
= NULL
;
1101 fn_oid
= fcinfo
->flinfo
->fn_oid
;
1102 procTup
= SearchSysCache(PROCOID
,
1103 ObjectIdGetDatum(fn_oid
),
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
);
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
))
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
);
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
;
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
),
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 */
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
]);
1197 proc
->code
= proc
->statics
= NULL
;
1198 proc
->globals
= proc
->me
= NULL
;
1199 proc
->is_setof
= procStruct
->proretset
;
1201 proc
->argnames
= NULL
;
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
),
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
)
1228 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
1229 errmsg("trigger functions can only be called as triggers")));
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;
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
1259 if (procStruct
->pronargs
)
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 */
1273 proc
->nargs
= total
;
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
)
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
;
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
]),
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 */
1311 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
1312 errmsg("plpython functions cannot take type %s",
1313 format_type_be(types
[i
]))));
1315 case TYPTYPE_COMPOSITE
:
1316 /* we'll set IO funcs at first call */
1317 proc
->args
[pos
].is_rowtype
= 2;
1320 PLy_input_datum_func(&(proc
->args
[pos
]),
1326 /* get argument name */
1327 proc
->argnames
[pos
] = names
? PLy_strdup(names
[i
]) : NULL
;
1329 ReleaseSysCache(argTypeTup
);
1336 * get the text of the function.
1338 prosrcdatum
= SysCacheGetAttr(PROCOID
, procTup
,
1339 Anum_pg_proc_prosrc
, &isnull
);
1341 elog(ERROR
, "null prosrc");
1342 procSource
= TextDatumGetCString(prosrcdatum
);
1344 PLy_procedure_compile(proc
, procSource
);
1348 proc
->me
= PyCObject_FromVoidPtr(proc
, NULL
);
1349 PyDict_SetItemString(PLy_procedure_cache
, key
, proc
->me
);
1353 PLy_procedure_delete(proc
);
1365 PLy_procedure_compile(PLyProcedure
* proc
, const char *src
)
1367 PyObject
*crv
= NULL
;
1370 proc
->globals
= PyDict_Copy(PLy_interp_globals
);
1373 * SD is private preserved data between calls. GD is global data shared by
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
);
1386 if (crv
!= NULL
&& (!PyErr_Occurred()))
1389 char call
[NAMEDATALEN
+ 256];
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()))
1406 PLy_elog(ERROR
, "could not compile function \"%s\"", proc
->proname
);
1410 PLy_procedure_munge_source(const char *name
, const char *src
)
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
);
1432 if (*sp
== '\r' && *(sp
+ 1) == '\n')
1435 if (*sp
== '\n' || *sp
== '\r')
1448 if (mp
> (mrc
+ mlen
))
1449 elog(FATAL
, "buffer overrun in PLy_munge_source");
1455 PLy_procedure_delete(PLyProcedure
* proc
)
1459 Py_XDECREF(proc
->code
);
1460 Py_XDECREF(proc
->statics
);
1461 Py_XDECREF(proc
->globals
);
1462 Py_XDECREF(proc
->me
);
1464 PLy_free(proc
->proname
);
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
]);
1480 PLy_free(proc
->argnames
);
1483 /* conversion functions. remember output from python is
1484 * input to postgresql, and vis versa.
1487 PLy_input_tuple_funcs(PLyTypeInfo
* arg
, TupleDesc desc
)
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
)
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
++)
1507 if (desc
->attrs
[i
]->attisdropped
)
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
),
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
,
1524 ReleaseSysCache(typeTup
);
1529 PLy_output_tuple_funcs(PLyTypeInfo
* arg
, TupleDesc desc
)
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
++)
1549 if (desc
->attrs
[i
]->attisdropped
)
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
),
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
);
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
);
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
;
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
);
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 */
1612 arg
->func
= PLyBool_FromString
;
1617 arg
->func
= PLyFloat_FromString
;
1621 arg
->func
= PLyInt_FromString
;
1624 arg
->func
= PLyLong_FromString
;
1627 arg
->func
= PLyString_FromString
;
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
;
1642 PLy_typeinfo_dealloc(PLyTypeInfo
* arg
)
1644 if (arg
->is_rowtype
== 1)
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' */
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
1664 return PyBool_FromLong(1);
1665 return PyBool_FromLong(0);
1669 PLyFloat_FromString(const char *src
)
1675 v
= strtod(src
, &eptr
);
1676 if (*eptr
!= '\0' || errno
)
1678 return PyFloat_FromDouble(v
);
1682 PLyInt_FromString(const char *src
)
1688 v
= strtol(src
, &eptr
, 0);
1689 if (*eptr
!= '\0' || errno
)
1691 return PyInt_FromLong(v
);
1695 PLyLong_FromString(const char *src
)
1697 return PyLong_FromString((char *) src
, NULL
, 0);
1701 PLyString_FromString(const char *src
)
1703 return PyString_FromString(src
);
1707 PLyDict_FromTuple(PLyTypeInfo
* info
, HeapTuple tuple
, TupleDesc desc
)
1709 PyObject
*volatile dict
;
1712 if (info
->is_rowtype
!= 1)
1713 elog(ERROR
, "PLyTypeInfo structure describes a datum");
1715 dict
= PyDict_New();
1717 PLy_elog(ERROR
, "could not create tuple dictionary");
1721 for (i
= 0; i
< info
->in
.r
.natts
; i
++)
1729 if (desc
->attrs
[i
]->attisdropped
)
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
);
1739 vsrc
= OutputFunctionCall(&info
->in
.r
.atts
[i
].typfunc
,
1743 * no exceptions allowed
1745 value
= info
->in
.r
.atts
[i
].func(vsrc
);
1747 PyDict_SetItemString(dict
, key
, value
);
1764 PLyMapping_ToTuple(PLyTypeInfo
* info
, PyObject
* mapping
)
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);
1780 values
= palloc(sizeof(Datum
) * desc
->natts
);
1781 nulls
= palloc(sizeof(bool) * desc
->natts
);
1782 for (i
= 0; i
< desc
->natts
; ++i
)
1785 PyObject
*volatile value
,
1788 key
= NameStr(desc
->attrs
[i
]->attname
);
1792 value
= PyMapping_GetItemString(mapping
, key
);
1793 if (value
== Py_None
)
1795 values
[i
] = (Datum
) NULL
;
1802 so
= PyObject_Str(value
);
1804 PLy_elog(ERROR
, "cannot convert mapping type");
1805 valuestr
= PyString_AsString(so
);
1807 values
[i
] = InputFunctionCall(&info
->out
.r
.atts
[i
].typfunc
1809 ,info
->out
.r
.atts
[i
].typioparam
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")));
1834 tuple
= heap_form_tuple(desc
, values
, nulls
);
1835 ReleaseTupleDesc(desc
);
1844 PLySequence_ToTuple(PLyTypeInfo
* info
, PyObject
* sequence
)
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
)
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);
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
,
1880 value
= PySequence_GetItem(sequence
, i
);
1882 if (value
== Py_None
)
1884 values
[i
] = (Datum
) NULL
;
1891 so
= PyObject_Str(value
);
1893 PLy_elog(ERROR
, "cannot convert sequence type");
1894 valuestr
= PyString_AsString(so
);
1895 values
[i
] = InputFunctionCall(&info
->out
.r
.atts
[i
].typfunc
1897 ,info
->out
.r
.atts
[i
].typioparam
1916 tuple
= heap_form_tuple(desc
, values
, nulls
);
1917 ReleaseTupleDesc(desc
);
1926 PLyObject_ToTuple(PLyTypeInfo
* info
, PyObject
* object
)
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);
1940 values
= palloc(sizeof(Datum
) * desc
->natts
);
1941 nulls
= palloc(sizeof(bool) * desc
->natts
);
1942 for (i
= 0; i
< desc
->natts
; ++i
)
1945 PyObject
*volatile value
,
1948 key
= NameStr(desc
->attrs
[i
]->attname
);
1952 value
= PyObject_GetAttrString(object
, key
);
1953 if (value
== Py_None
)
1955 values
[i
] = (Datum
) NULL
;
1962 so
= PyObject_Str(value
);
1964 PLy_elog(ERROR
, "cannot convert object type");
1965 valuestr
= PyString_AsString(so
);
1966 values
[i
] = InputFunctionCall(&info
->out
.r
.atts
[i
].typfunc
1968 ,info
->out
.r
.atts
[i
].typioparam
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")));
1994 tuple
= heap_form_tuple(desc
, values
, nulls
);
1995 ReleaseTupleDesc(desc
);
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
)
2043 "PLyPlan", /* tp_name */
2044 sizeof(PLyPlanObject
), /* tp_size */
2045 0, /* tp_itemsize */
2050 PLy_plan_dealloc
, /* tp_dealloc */
2052 PLy_plan_getattr
, /* tp_getattr */
2056 0, /* tp_as_number */
2057 0, /* tp_as_sequence */
2058 0, /* tp_as_mapping */
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
)
2087 "PLyResult", /* tp_name */
2088 sizeof(PLyResultObject
), /* tp_size */
2089 0, /* tp_itemsize */
2094 PLy_result_dealloc
, /* tp_dealloc */
2096 PLy_result_getattr
, /* tp_getattr */
2100 0, /* tp_as_number */
2101 &PLy_result_as_sequence
, /* tp_as_sequence */
2102 0, /* tp_as_mapping */
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
[] = {
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 */
2151 if ((ob
= PyObject_NEW(PLyPlanObject
, &PLy_PlanType
)) == NULL
)
2159 return (PyObject
*) ob
;
2164 PLy_plan_dealloc(PyObject
* arg
)
2166 PLyPlanObject
*ob
= (PLyPlanObject
*) arg
;
2169 SPI_freeplan(ob
->plan
);
2171 PLy_free(ob
->types
);
2176 for (i
= 0; i
< ob
->nargs
; i
++)
2177 PLy_typeinfo_dealloc(&ob
->args
[i
]);
2181 arg
->ob_type
->tp_free(arg
);
2186 PLy_plan_getattr(PyObject
* self
, char *name
)
2188 return Py_FindMethod(PLy_plan_methods
, self
, name
);
2192 PLy_plan_status(PyObject
* self
, PyObject
* args
)
2194 if (PyArg_ParseTuple(args
, ""))
2198 /* return PyInt_FromLong(self->status); */
2200 PyErr_SetString(PLy_exc_error
, "plan.status() takes no arguments");
2206 /* result object methods */
2209 PLy_result_new(void)
2211 PLyResultObject
*ob
;
2213 if ((ob
= PyObject_NEW(PLyResultObject
, &PLy_ResultType
)) == NULL
)
2216 /* ob->tuples = NULL; */
2219 ob
->status
= Py_None
;
2220 ob
->nrows
= PyInt_FromLong(-1);
2221 ob
->rows
= PyList_New(0);
2223 return (PyObject
*) ob
;
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
);
2239 PLy_result_getattr(PyObject
* self
, char *name
)
2241 return Py_FindMethod(PLy_result_methods
, self
, name
);
2245 PLy_result_nrows(PyObject
* self
, PyObject
* args
)
2247 PLyResultObject
*ob
= (PLyResultObject
*) self
;
2249 Py_INCREF(ob
->nrows
);
2254 PLy_result_status(PyObject
* self
, PyObject
* args
)
2256 PLyResultObject
*ob
= (PLyResultObject
*) self
;
2258 Py_INCREF(ob
->status
);
2263 PLy_result_length(PyObject
* arg
)
2265 PLyResultObject
*ob
= (PLyResultObject
*) arg
;
2267 return PyList_Size(ob
->rows
);
2271 PLy_result_item(PyObject
* arg
, Py_ssize_t idx
)
2274 PLyResultObject
*ob
= (PLyResultObject
*) arg
;
2276 rv
= PyList_GetItem(ob
->rows
, idx
);
2283 PLy_result_ass_item(PyObject
* arg
, Py_ssize_t idx
, PyObject
* item
)
2286 PLyResultObject
*ob
= (PLyResultObject
*) arg
;
2289 rv
= PyList_SetItem(ob
->rows
, idx
, item
);
2294 PLy_result_slice(PyObject
* arg
, Py_ssize_t lidx
, Py_ssize_t hidx
)
2297 PLyResultObject
*ob
= (PLyResultObject
*) arg
;
2299 rv
= PyList_GetSlice(ob
->rows
, lidx
, hidx
);
2307 PLy_result_ass_slice(PyObject
* arg
, Py_ssize_t lidx
, Py_ssize_t hidx
, PyObject
* slice
)
2310 PLyResultObject
*ob
= (PLyResultObject
*) arg
;
2312 rv
= PyList_SetSlice(ob
->rows
, lidx
, hidx
, slice
);
2318 PLy_spi_prepare(PyObject
* self
, PyObject
* args
)
2320 PLyPlanObject
*plan
;
2321 PyObject
*list
= NULL
;
2322 PyObject
*volatile optr
= NULL
;
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.");
2334 if (!PyArg_ParseTuple(args
, "s|O", &query
, &list
))
2336 PyErr_SetString(PLy_exc_spi_error
,
2337 "Invalid arguments for plpy.prepare()");
2341 if (list
&& (!PySequence_Check(list
)))
2343 PyErr_SetString(PLy_exc_spi_error
,
2344 "Second argument in plpy.prepare() must be a sequence");
2348 if ((plan
= (PLyPlanObject
*) PLy_plan_new()) == NULL
)
2351 oldcontext
= CurrentMemoryContext
;
2359 nargs
= PySequence_Length(list
);
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
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
++)
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
),
2402 if (!HeapTupleIsValid(typeTup
))
2403 elog(ERROR
, "cache lookup failed for type %u", typeId
);
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
);
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
));
2434 MemoryContextSwitchTo(oldcontext
);
2435 PLy_error_in_progress
= CopyErrorData();
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
));
2449 return (PyObject
*) plan
;
2452 /* execute(query="select * from foo", limit=5)
2453 * execute(plan=plan, values=(foo, bar), limit=5)
2456 PLy_spi_execute(PyObject
* self
, PyObject
* args
)
2460 PyObject
*list
= NULL
;
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.");
2470 if (PyArg_ParseTuple(args
, "s|l", &query
, &limit
))
2471 return PLy_spi_execute_query(query
, limit
);
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.");
2484 PLy_spi_execute_plan(PyObject
* ob
, PyObject
* list
, long limit
)
2489 PLyPlanObject
*plan
;
2490 MemoryContext oldcontext
;
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
);
2501 nargs
= PySequence_Length(list
);
2506 plan
= (PLyPlanObject
*) ob
;
2508 if (nargs
!= plan
->nargs
)
2511 PyObject
*so
= PyObject_Str(list
);
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
);
2525 oldcontext
= CurrentMemoryContext
;
2528 char *nulls
= palloc(nargs
* sizeof(char));
2531 for (j
= 0; j
< nargs
; j
++)
2536 elem
= PySequence_GetItem(list
, j
);
2537 if (elem
!= Py_None
)
2539 so
= PyObject_Str(elem
);
2541 PLy_elog(ERROR
, "function \"%s\" could not execute plan",
2542 PLy_procedure_name(PLy_curr_procedure
));
2547 char *sv
= PyString_AsString(so
);
2550 InputFunctionCall(&(plan
->args
[j
].out
.d
.typfunc
),
2552 plan
->args
[j
].out
.d
.typioparam
,
2569 InputFunctionCall(&(plan
->args
[j
].out
.d
.typfunc
),
2571 plan
->args
[j
].out
.d
.typioparam
,
2577 rv
= SPI_execute_plan(plan
->plan
, plan
->values
, nulls
,
2578 PLy_curr_procedure
->fn_readonly
, limit
);
2586 MemoryContextSwitchTo(oldcontext
);
2587 PLy_error_in_progress
= CopyErrorData();
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
));
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
);
2625 PLy_exception_set(PLy_exc_spi_error
,
2626 "SPI_execute_plan failed: %s",
2627 SPI_result_code_string(rv
));
2631 return PLy_spi_execute_fetch_result(SPI_tuptable
, SPI_processed
, rv
);
2635 PLy_spi_execute_query(char *query
, long limit
)
2638 MemoryContext oldcontext
;
2640 oldcontext
= CurrentMemoryContext
;
2643 rv
= SPI_execute(query
, PLy_curr_procedure
->fn_readonly
, limit
);
2647 MemoryContextSwitchTo(oldcontext
);
2648 PLy_error_in_progress
= CopyErrorData();
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
));
2662 PLy_exception_set(PLy_exc_spi_error
,
2663 "SPI_execute failed: %s",
2664 SPI_result_code_string(rv
));
2668 return PLy_spi_execute_fetch_result(SPI_tuptable
, SPI_processed
, rv
);
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
)
2691 Py_DECREF(result
->nrows
);
2692 result
->nrows
= PyInt_FromLong(rows
);
2693 PLy_typeinfo_init(&args
);
2695 oldcontext
= CurrentMemoryContext
;
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
],
2709 PyList_SetItem(result
->rows
, i
, row
);
2711 PLy_typeinfo_dealloc(&args
);
2713 SPI_freetuptable(tuptable
);
2718 MemoryContextSwitchTo(oldcontext
);
2719 PLy_error_in_progress
= CopyErrorData();
2721 if (!PyErr_Occurred())
2722 PyErr_SetString(PLy_exc_error
,
2723 "Unknown error in PLy_spi_execute_fetch_result");
2725 PLy_typeinfo_dealloc(&args
);
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!
2747 /* Be sure we do initialization only once (should be redundant now) */
2748 static bool inited
= false;
2753 set_text_domain(TEXTDOMAIN
);
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");
2768 PLy_init_interp(void)
2772 mainmod
= PyImport_AddModule("__main__");
2773 if (mainmod
== NULL
|| PyErr_Occurred())
2774 PLy_elog(ERROR
, "could not import \"__main__\" module.");
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
);
2780 if (PLy_interp_globals
== NULL
|| PyErr_Occurred())
2781 PLy_elog(ERROR
, "could not initialize globals");
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
*);
2830 PLy_debug(PyObject
* self
, PyObject
* args
)
2832 return PLy_output(DEBUG2
, self
, args
);
2836 PLy_log(PyObject
* self
, PyObject
* args
)
2838 return PLy_output(LOG
, self
, args
);
2842 PLy_info(PyObject
* self
, PyObject
* args
)
2844 return PLy_output(INFO
, self
, args
);
2848 PLy_notice(PyObject
* self
, PyObject
* args
)
2850 return PLy_output(NOTICE
, self
, args
);
2854 PLy_warning(PyObject
* self
, PyObject
* args
)
2856 return PLy_output(WARNING
, self
, args
);
2860 PLy_error(PyObject
* self
, PyObject
* args
)
2862 return PLy_output(ERROR
, self
, args
);
2866 PLy_fatal(PyObject
* self
, PyObject
* args
)
2868 return PLy_output(FATAL
, self
, args
);
2873 PLy_output(volatile int level
, PyObject
* self
, PyObject
* args
)
2877 MemoryContext oldcontext
;
2879 so
= PyObject_Str(args
);
2880 if (so
== NULL
|| ((sv
= PyString_AsString(so
)) == NULL
))
2883 sv
= "could not parse error message in `plpy.elog'";
2886 oldcontext
= CurrentMemoryContext
;
2889 elog(level
, "%s", sv
);
2893 MemoryContextSwitchTo(oldcontext
);
2894 PLy_error_in_progress
= CopyErrorData();
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
);
2911 * return a legal object so the interpreter will continue on its merry way
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
2926 PLy_procedure_name(PLyProcedure
* proc
)
2929 return "<unknown procedure>";
2930 return proc
->proname
;
2933 /* output a python traceback/exception via the postgresql elog
2934 * function. not pretty.
2937 PLy_exception_set(PyObject
* exc
, const char *fmt
,...)
2943 vsnprintf(buf
, sizeof(buf
), fmt
, 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
2954 PLy_elog(int elevel
, const char *fmt
,...)
2958 StringInfoData emsg
;
2960 xmsg
= PLy_traceback(&xlevel
);
2962 initStringInfo(&emsg
);
2969 success
= appendStringInfoVA(&emsg
, fmt
, ap
);
2973 enlargeStringInfo(&emsg
, emsg
.maxlen
);
2979 (errmsg("plpython: %s", emsg
.data
),
2980 (xmsg
) ? errdetail("%s", xmsg
) : 0));
2997 PLy_traceback(int *xlevel
)
3006 StringInfoData xstr
;
3009 * get the current exception
3011 PyErr_Fetch(&e
, &v
, &tb
);
3014 * oops, no exception, return
3022 PyErr_NormalizeException(&e
, &v
, &tb
);
3025 eob
= PyObject_Str(e
);
3026 if (v
&& ((vob
= PyObject_Str(v
)) != NULL
))
3027 vstr
= PyString_AsString(vob
);
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
);
3046 * intuit an appropriate error level based on the exception type
3048 if (PLy_exc_error
&& PyErr_GivenExceptionMatches(e
, PLy_exc_error
))
3050 else if (PLy_exc_fatal
&& PyErr_GivenExceptionMatches(e
, PLy_exc_fatal
))
3059 /* python module code */
3061 /* some dumb utility functions */
3063 PLy_malloc(size_t bytes
)
3065 void *ptr
= malloc(bytes
);
3069 (errcode(ERRCODE_OUT_OF_MEMORY
),
3070 errmsg("out of memory")));
3075 PLy_malloc0(size_t bytes
)
3077 void *ptr
= PLy_malloc(bytes
);
3079 MemSet(ptr
, 0, bytes
);
3084 PLy_strdup(const char *str
)
3089 len
= strlen(str
) + 1;
3090 result
= PLy_malloc(len
);
3091 memcpy(result
, str
, len
);
3096 /* define this away */