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 PG_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 and translation support */
209 PLy_exception_set(PyObject
*, const char *,...)
210 __attribute__((format(printf
, 2, 3)));
212 /* same, with pluralized message */
214 PLy_exception_set_plural(PyObject
*, const char *, const char *,
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 */
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
*,
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
,
249 static PLyProcedure
*PLy_procedure_create(HeapTuple procTup
, Oid tgreloid
,
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
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.
330 perm_fmgr_info(Oid functionId
, FmgrInfo
*finfo
)
332 fmgr_info_cxt(functionId
, finfo
, TopMemoryContext
);
336 plpython_call_handler(PG_FUNCTION_ARGS
)
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
;
349 if (CALLED_AS_TRIGGER(fcinfo
))
351 TriggerData
*tdata
= (TriggerData
*) fcinfo
->context
;
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
);
362 proc
= PLy_procedure_get(fcinfo
, InvalidOid
);
363 PLy_curr_procedure
= proc
;
364 retval
= PLy_function_handler(fcinfo
, proc
);
369 PLy_curr_procedure
= save_curr_proc
;
372 /* note: Py_DECREF needs braces around it, as of 2003/08 */
380 PLy_curr_procedure
= save_curr_proc
;
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.
398 PLy_trigger_handler(FunctionCallInfo fcinfo
, PLyProcedure
*proc
)
401 PyObject
*volatile plargs
= NULL
;
402 PyObject
*volatile plrv
= NULL
;
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
425 if (!PyString_Check(plrv
))
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)
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
);
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
452 (errcode(ERRCODE_DATA_EXCEPTION
),
453 errmsg("unexpected return value from trigger procedure"),
454 errdetail("Expected None, \"OK\", \"SKIP\", or \"MODIFY\".")));
474 PLy_modify_tuple(PLyProcedure
*proc
, PyObject
*pltd
, TriggerData
*tdata
,
477 PyObject
*volatile plntup
;
478 PyObject
*volatile plkeys
;
479 PyObject
*volatile platt
;
480 PyObject
*volatile plval
;
481 PyObject
*volatile plstr
;
487 int *volatile modattrs
;
488 Datum
*volatile modvalues
;
489 char *volatile modnulls
;
492 plntup
= plkeys
= platt
= plval
= plstr
= NULL
;
499 if ((plntup
= PyDict_GetItemString(pltd
, "new")) == NULL
)
501 (errmsg("TD[\"new\"] deleted, cannot modify row")));
502 if (!PyDict_Check(plntup
))
504 (errmsg("TD[\"new\"] is not a dictionary")));
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
++)
520 platt
= PyList_GetItem(plkeys
, i
);
521 if (!PyString_Check(platt
))
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
)
527 (errmsg("key \"%s\" found in TD[\"new\"] does not exist as a column in the triggering row",
528 PyString_AsString(platt
))));
531 plval
= PyDict_GetItem(plntup
, platt
);
533 elog(FATAL
, "Python interpreter is probably corrupted");
539 if (tupdesc
->attrs
[atti
]->attisdropped
)
541 modvalues
[i
] = (Datum
) 0;
544 else if (plval
!= Py_None
)
546 plstr
= PyObject_Str(plval
);
548 PLy_elog(ERROR
, "could not compute string representation of Python object in PL/Python function \"%s\" while modifying trigger row",
550 src
= PyString_AsString(plstr
);
553 InputFunctionCall(&proc
->result
.out
.r
.atts
[atti
].typfunc
,
555 proc
->result
.out
.r
.atts
[atti
].typioparam
,
556 tupdesc
->attrs
[atti
]->atttypmod
);
565 InputFunctionCall(&proc
->result
.out
.r
.atts
[atti
].typfunc
,
567 proc
->result
.out
.r
.atts
[atti
].typioparam
,
568 tupdesc
->attrs
[atti
]->atttypmod
);
576 rtup
= SPI_modifytuple(tdata
->tg_relation
, otup
, natts
,
577 modattrs
, modvalues
, modnulls
);
579 elog(ERROR
, "SPI_modifytuple failed: error %d", SPI_result
);
610 PLy_trigger_build_args(FunctionCallInfo fcinfo
, PLyProcedure
*proc
, HeapTuple
*rv
)
612 TriggerData
*tdata
= (TriggerData
*) fcinfo
->context
;
623 PyObject
*volatile pltdata
= NULL
;
628 pltdata
= PyDict_New();
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
);
636 stroid
= DatumGetCString(DirectFunctionCall1(oidout
,
637 ObjectIdGetDatum(tdata
->tg_relation
->rd_id
)));
638 pltrelid
= PyString_FromString(stroid
);
639 PyDict_SetItemString(pltdata
, "relid", pltrelid
);
643 stroid
= SPI_getrelname(tdata
->tg_relation
);
644 plttablename
= PyString_FromString(stroid
);
645 PyDict_SetItemString(pltdata
, "table_name", plttablename
);
646 Py_DECREF(plttablename
);
649 stroid
= SPI_getnspname(tdata
->tg_relation
);
650 plttableschema
= PyString_FromString(stroid
);
651 PyDict_SetItemString(pltdata
, "table_schema", plttableschema
);
652 Py_DECREF(plttableschema
);
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");
662 elog(ERROR
, "unrecognized WHEN tg_event: %u", tdata
->tg_event
);
663 pltwhen
= NULL
; /* keep compiler quiet */
665 PyDict_SetItemString(pltdata
, "when", pltwhen
);
668 if (TRIGGER_FIRED_FOR_ROW(tdata
->tg_event
))
670 pltlevel
= PyString_FromString("ROW");
671 PyDict_SetItemString(pltdata
, "level", 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
);
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
);
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
);
704 pytold
= PLyDict_FromTuple(&(proc
->result
), tdata
->tg_trigtuple
,
705 tdata
->tg_relation
->rd_att
);
706 PyDict_SetItemString(pltdata
, "old", pytold
);
708 *rv
= tdata
->tg_newtuple
;
712 elog(ERROR
, "unrecognized OP tg_event: %u", tdata
->tg_event
);
713 pltevent
= NULL
; /* keep compiler quiet */
716 PyDict_SetItemString(pltdata
, "event", pltevent
);
719 else if (TRIGGER_FIRED_FOR_STATEMENT(tdata
->tg_event
))
721 pltlevel
= PyString_FromString("STATEMENT");
722 PyDict_SetItemString(pltdata
, "level", pltlevel
);
725 PyDict_SetItemString(pltdata
, "old", Py_None
);
726 PyDict_SetItemString(pltdata
, "new", Py_None
);
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");
739 elog(ERROR
, "unrecognized OP tg_event: %u", tdata
->tg_event
);
740 pltevent
= NULL
; /* keep compiler quiet */
743 PyDict_SetItemString(pltdata
, "event", pltevent
);
747 elog(ERROR
, "unrecognized LEVEL tg_event: %u", tdata
->tg_event
);
749 if (tdata
->tg_trigger
->tgnargs
)
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
);
773 PyDict_SetItemString(pltdata
, "args", pltargs
);
788 /* function handler and friends */
790 PLy_function_handler(FunctionCallInfo fcinfo
, PLyProcedure
*proc
)
793 PyObject
*volatile plargs
= NULL
;
794 PyObject
*volatile plrv
= NULL
;
795 PyObject
*volatile plrv_so
= NULL
;
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
);
808 * SETOF function parameters will be deleted when last row is
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
822 if (SPI_finish() != SPI_OK_FINISH
)
823 elog(ERROR
, "SPI_finish failed");
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)
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
);
848 if (proc
->setof
== NULL
)
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
);
858 rsi
->isDone
= ExprMultipleResult
;
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
);
875 PLy_function_delete_args(proc
);
879 (errcode(ERRCODE_DATA_EXCEPTION
),
880 errmsg("error fetching next item from iterator")));
882 fcinfo
->isnull
= true;
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
)
897 (errcode(ERRCODE_DATATYPE_MISMATCH
),
898 errmsg("PL/Python function with return type \"void\" did not return None")));
900 fcinfo
->isnull
= false;
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
,
909 proc
->result
.out
.d
.typioparam
,
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
);
926 /* returned as smth, must provide method __getattr__(name) */
927 tuple
= PLyObject_ToTuple(&proc
->result
, plrv
);
931 fcinfo
->isnull
= false;
932 rv
= HeapTupleGetDatum(tuple
);
936 fcinfo
->isnull
= true;
942 fcinfo
->isnull
= false;
943 plrv_so
= PyObject_Str(plrv
);
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
,
949 proc
->result
.out
.d
.typioparam
,
971 PLy_procedure_call(PLyProcedure
*proc
, char *kargs
, PyObject
*vargs
)
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
;
991 if (rv
== NULL
|| PyErr_Occurred())
994 PLy_elog(ERROR
, "PL/Python function \"%s\" failed", proc
->proname
);
1001 PLy_function_build_args(FunctionCallInfo fcinfo
, PLyProcedure
*proc
)
1003 PyObject
*volatile arg
= NULL
;
1004 PyObject
*volatile args
= NULL
;
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
])
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
);
1038 arg
= PLyDict_FromTuple(&(proc
->args
[i
]), &tmptup
, tupdesc
);
1039 ReleaseTupleDesc(tupdesc
);
1044 if (fcinfo
->argnull
[i
])
1050 ct
= OutputFunctionCall(&(proc
->args
[i
].in
.d
.typfunc
),
1052 arg
= (proc
->args
[i
].in
.d
.func
) (ct
);
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
);
1086 PLy_function_delete_args(PLyProcedure
*proc
)
1090 if (!proc
->argnames
)
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
1108 static PLyProcedure
*
1109 PLy_procedure_get(FunctionCallInfo fcinfo
, Oid tgreloid
)
1115 PLyProcedure
*proc
= NULL
;
1118 fn_oid
= fcinfo
->flinfo
->fn_oid
;
1119 procTup
= SearchSysCache(PROCOID
,
1120 ObjectIdGetDatum(fn_oid
),
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
);
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
))
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
);
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
;
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
),
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 */
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
]);
1214 proc
->code
= proc
->statics
= NULL
;
1215 proc
->globals
= proc
->me
= NULL
;
1216 proc
->is_setof
= procStruct
->proretset
;
1218 proc
->argnames
= NULL
;
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
),
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
)
1245 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
1246 errmsg("trigger functions can only be called as triggers")));
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;
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
1276 if (procStruct
->pronargs
)
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 */
1290 proc
->nargs
= total
;
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
)
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
;
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
]),
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 */
1328 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
1329 errmsg("PL/Python functions cannot accept type %s",
1330 format_type_be(types
[i
]))));
1332 case TYPTYPE_COMPOSITE
:
1333 /* we'll set IO funcs at first call */
1334 proc
->args
[pos
].is_rowtype
= 2;
1337 PLy_input_datum_func(&(proc
->args
[pos
]),
1343 /* get argument name */
1344 proc
->argnames
[pos
] = names
? PLy_strdup(names
[i
]) : NULL
;
1346 ReleaseSysCache(argTypeTup
);
1353 * get the text of the function.
1355 prosrcdatum
= SysCacheGetAttr(PROCOID
, procTup
,
1356 Anum_pg_proc_prosrc
, &isnull
);
1358 elog(ERROR
, "null prosrc");
1359 procSource
= TextDatumGetCString(prosrcdatum
);
1361 PLy_procedure_compile(proc
, procSource
);
1365 proc
->me
= PyCObject_FromVoidPtr(proc
, NULL
);
1366 PyDict_SetItemString(PLy_procedure_cache
, key
, proc
->me
);
1370 PLy_procedure_delete(proc
);
1382 PLy_procedure_compile(PLyProcedure
*proc
, const char *src
)
1384 PyObject
*crv
= NULL
;
1387 proc
->globals
= PyDict_Copy(PLy_interp_globals
);
1390 * SD is private preserved data between calls. GD is global data shared by
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
);
1403 if (crv
!= NULL
&& (!PyErr_Occurred()))
1406 char call
[NAMEDATALEN
+ 256];
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()))
1423 PLy_elog(ERROR
, "could not compile PL/Python function \"%s\"", proc
->proname
);
1427 PLy_procedure_munge_source(const char *name
, const char *src
)
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
);
1449 if (*sp
== '\r' && *(sp
+ 1) == '\n')
1452 if (*sp
== '\n' || *sp
== '\r')
1465 if (mp
> (mrc
+ mlen
))
1466 elog(FATAL
, "buffer overrun in PLy_munge_source");
1472 PLy_procedure_delete(PLyProcedure
*proc
)
1476 Py_XDECREF(proc
->code
);
1477 Py_XDECREF(proc
->statics
);
1478 Py_XDECREF(proc
->globals
);
1479 Py_XDECREF(proc
->me
);
1481 PLy_free(proc
->proname
);
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
]);
1497 PLy_free(proc
->argnames
);
1501 * Conversion functions. Remember output from Python is input to
1502 * PostgreSQL, and vice versa.
1505 PLy_input_tuple_funcs(PLyTypeInfo
*arg
, TupleDesc desc
)
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
)
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
++)
1525 if (desc
->attrs
[i
]->attisdropped
)
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
),
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
,
1542 ReleaseSysCache(typeTup
);
1547 PLy_output_tuple_funcs(PLyTypeInfo
*arg
, TupleDesc desc
)
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
++)
1567 if (desc
->attrs
[i
]->attisdropped
)
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
),
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
);
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
);
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
;
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
);
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 */
1630 arg
->func
= PLyBool_FromString
;
1635 arg
->func
= PLyFloat_FromString
;
1639 arg
->func
= PLyInt_FromString
;
1642 arg
->func
= PLyLong_FromString
;
1645 arg
->func
= PLyString_FromString
;
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
;
1660 PLy_typeinfo_dealloc(PLyTypeInfo
*arg
)
1662 if (arg
->is_rowtype
== 1)
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' */
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
1682 return PyBool_FromLong(1);
1683 return PyBool_FromLong(0);
1687 PLyFloat_FromString(const char *src
)
1693 v
= strtod(src
, &eptr
);
1694 if (*eptr
!= '\0' || errno
)
1696 return PyFloat_FromDouble(v
);
1700 PLyInt_FromString(const char *src
)
1706 v
= strtol(src
, &eptr
, 0);
1707 if (*eptr
!= '\0' || errno
)
1709 return PyInt_FromLong(v
);
1713 PLyLong_FromString(const char *src
)
1715 return PyLong_FromString((char *) src
, NULL
, 0);
1719 PLyString_FromString(const char *src
)
1721 return PyString_FromString(src
);
1725 PLyDict_FromTuple(PLyTypeInfo
*info
, HeapTuple tuple
, TupleDesc desc
)
1727 PyObject
*volatile dict
;
1730 if (info
->is_rowtype
!= 1)
1731 elog(ERROR
, "PLyTypeInfo structure describes a datum");
1733 dict
= PyDict_New();
1735 PLy_elog(ERROR
, "could not create new dictionary");
1739 for (i
= 0; i
< info
->in
.r
.natts
; i
++)
1747 if (desc
->attrs
[i
]->attisdropped
)
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
);
1757 vsrc
= OutputFunctionCall(&info
->in
.r
.atts
[i
].typfunc
,
1761 * no exceptions allowed
1763 value
= info
->in
.r
.atts
[i
].func(vsrc
);
1765 PyDict_SetItemString(dict
, key
, value
);
1782 PLyMapping_ToTuple(PLyTypeInfo
*info
, PyObject
*mapping
)
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);
1798 values
= palloc(sizeof(Datum
) * desc
->natts
);
1799 nulls
= palloc(sizeof(bool) * desc
->natts
);
1800 for (i
= 0; i
< desc
->natts
; ++i
)
1803 PyObject
*volatile value
,
1806 key
= NameStr(desc
->attrs
[i
]->attname
);
1810 value
= PyMapping_GetItemString(mapping
, key
);
1811 if (value
== Py_None
)
1813 values
[i
] = (Datum
) NULL
;
1820 so
= PyObject_Str(value
);
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
1827 ,info
->out
.r
.atts
[i
].typioparam
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.")));
1852 tuple
= heap_form_tuple(desc
, values
, nulls
);
1853 ReleaseTupleDesc(desc
);
1862 PLySequence_ToTuple(PLyTypeInfo
*info
, PyObject
*sequence
)
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
)
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);
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
,
1898 value
= PySequence_GetItem(sequence
, i
);
1900 if (value
== Py_None
)
1902 values
[i
] = (Datum
) NULL
;
1909 so
= PyObject_Str(value
);
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
1915 ,info
->out
.r
.atts
[i
].typioparam
1934 tuple
= heap_form_tuple(desc
, values
, nulls
);
1935 ReleaseTupleDesc(desc
);
1944 PLyObject_ToTuple(PLyTypeInfo
*info
, PyObject
*object
)
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);
1958 values
= palloc(sizeof(Datum
) * desc
->natts
);
1959 nulls
= palloc(sizeof(bool) * desc
->natts
);
1960 for (i
= 0; i
< desc
->natts
; ++i
)
1963 PyObject
*volatile value
,
1966 key
= NameStr(desc
->attrs
[i
]->attname
);
1970 value
= PyObject_GetAttrString(object
, key
);
1971 if (value
== Py_None
)
1973 values
[i
] = (Datum
) NULL
;
1980 so
= PyObject_Str(value
);
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
1986 ,info
->out
.r
.atts
[i
].typioparam
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.")));
2012 tuple
= heap_form_tuple(desc
, values
, nulls
);
2013 ReleaseTupleDesc(desc
);
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
)
2061 "PLyPlan", /* tp_name */
2062 sizeof(PLyPlanObject
), /* tp_size */
2063 0, /* tp_itemsize */
2068 PLy_plan_dealloc
, /* tp_dealloc */
2070 PLy_plan_getattr
, /* tp_getattr */
2074 0, /* tp_as_number */
2075 0, /* tp_as_sequence */
2076 0, /* tp_as_mapping */
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
)
2105 "PLyResult", /* tp_name */
2106 sizeof(PLyResultObject
), /* tp_size */
2107 0, /* tp_itemsize */
2112 PLy_result_dealloc
, /* tp_dealloc */
2114 PLy_result_getattr
, /* tp_getattr */
2118 0, /* tp_as_number */
2119 &PLy_result_as_sequence
, /* tp_as_sequence */
2120 0, /* tp_as_mapping */
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
[] = {
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 */
2169 if ((ob
= PyObject_NEW(PLyPlanObject
, &PLy_PlanType
)) == NULL
)
2177 return (PyObject
*) ob
;
2182 PLy_plan_dealloc(PyObject
*arg
)
2184 PLyPlanObject
*ob
= (PLyPlanObject
*) arg
;
2187 SPI_freeplan(ob
->plan
);
2189 PLy_free(ob
->types
);
2194 for (i
= 0; i
< ob
->nargs
; i
++)
2195 PLy_typeinfo_dealloc(&ob
->args
[i
]);
2199 arg
->ob_type
->tp_free(arg
);
2204 PLy_plan_getattr(PyObject
*self
, char *name
)
2206 return Py_FindMethod(PLy_plan_methods
, self
, name
);
2210 PLy_plan_status(PyObject
*self
, PyObject
*args
)
2212 if (PyArg_ParseTuple(args
, ""))
2216 /* return PyInt_FromLong(self->status); */
2218 PLy_exception_set(PLy_exc_error
, "plan.status takes no arguments");
2224 /* result object methods */
2227 PLy_result_new(void)
2229 PLyResultObject
*ob
;
2231 if ((ob
= PyObject_NEW(PLyResultObject
, &PLy_ResultType
)) == NULL
)
2234 /* ob->tuples = NULL; */
2237 ob
->status
= Py_None
;
2238 ob
->nrows
= PyInt_FromLong(-1);
2239 ob
->rows
= PyList_New(0);
2241 return (PyObject
*) ob
;
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
);
2257 PLy_result_getattr(PyObject
*self
, char *name
)
2259 return Py_FindMethod(PLy_result_methods
, self
, name
);
2263 PLy_result_nrows(PyObject
*self
, PyObject
*args
)
2265 PLyResultObject
*ob
= (PLyResultObject
*) self
;
2267 Py_INCREF(ob
->nrows
);
2272 PLy_result_status(PyObject
*self
, PyObject
*args
)
2274 PLyResultObject
*ob
= (PLyResultObject
*) self
;
2276 Py_INCREF(ob
->status
);
2281 PLy_result_length(PyObject
*arg
)
2283 PLyResultObject
*ob
= (PLyResultObject
*) arg
;
2285 return PyList_Size(ob
->rows
);
2289 PLy_result_item(PyObject
*arg
, Py_ssize_t idx
)
2292 PLyResultObject
*ob
= (PLyResultObject
*) arg
;
2294 rv
= PyList_GetItem(ob
->rows
, idx
);
2301 PLy_result_ass_item(PyObject
*arg
, Py_ssize_t idx
, PyObject
*item
)
2304 PLyResultObject
*ob
= (PLyResultObject
*) arg
;
2307 rv
= PyList_SetItem(ob
->rows
, idx
, item
);
2312 PLy_result_slice(PyObject
*arg
, Py_ssize_t lidx
, Py_ssize_t hidx
)
2315 PLyResultObject
*ob
= (PLyResultObject
*) arg
;
2317 rv
= PyList_GetSlice(ob
->rows
, lidx
, hidx
);
2325 PLy_result_ass_slice(PyObject
*arg
, Py_ssize_t lidx
, Py_ssize_t hidx
, PyObject
*slice
)
2328 PLyResultObject
*ob
= (PLyResultObject
*) arg
;
2330 rv
= PyList_SetSlice(ob
->rows
, lidx
, hidx
, slice
);
2336 PLy_spi_prepare(PyObject
*self
, PyObject
*args
)
2338 PLyPlanObject
*plan
;
2339 PyObject
*list
= NULL
;
2340 PyObject
*volatile optr
= NULL
;
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");
2352 if (!PyArg_ParseTuple(args
, "s|O", &query
, &list
))
2354 PLy_exception_set(PLy_exc_spi_error
,
2355 "invalid arguments for plpy.prepare");
2359 if (list
&& (!PySequence_Check(list
)))
2361 PLy_exception_set(PLy_exc_spi_error
,
2362 "second argument of plpy.prepare must be a sequence");
2366 if ((plan
= (PLyPlanObject
*) PLy_plan_new()) == NULL
)
2369 oldcontext
= CurrentMemoryContext
;
2377 nargs
= PySequence_Length(list
);
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
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
++)
2402 Form_pg_type typeStruct
;
2404 optr
= PySequence_GetItem(list
, i
);
2405 if (!PyString_Check(optr
))
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
),
2421 if (!HeapTupleIsValid(typeTup
))
2422 elog(ERROR
, "cache lookup failed for type %u", typeId
);
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
);
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
));
2455 MemoryContextSwitchTo(oldcontext
);
2456 PLy_error_in_progress
= CopyErrorData();
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
));
2470 return (PyObject
*) plan
;
2473 /* execute(query="select * from foo", limit=5)
2474 * execute(plan=plan, values=(foo, bar), limit=5)
2477 PLy_spi_execute(PyObject
*self
, PyObject
*args
)
2481 PyObject
*list
= NULL
;
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");
2491 if (PyArg_ParseTuple(args
, "s|l", &query
, &limit
))
2492 return PLy_spi_execute_query(query
, limit
);
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");
2505 PLy_spi_execute_plan(PyObject
*ob
, PyObject
*list
, long limit
)
2510 PLyPlanObject
*plan
;
2511 MemoryContext oldcontext
;
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");
2520 nargs
= PySequence_Length(list
);
2525 plan
= (PLyPlanObject
*) ob
;
2527 if (nargs
!= plan
->nargs
)
2530 PyObject
*so
= PyObject_Str(list
);
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",
2540 plan
->nargs
, nargs
, sv
);
2546 oldcontext
= CurrentMemoryContext
;
2549 char *nulls
= palloc(nargs
* sizeof(char));
2552 for (j
= 0; j
< nargs
; j
++)
2557 elem
= PySequence_GetItem(list
, j
);
2558 if (elem
!= Py_None
)
2560 so
= PyObject_Str(elem
);
2562 PLy_elog(ERROR
, "PL/Python function \"%s\" could not execute plan",
2563 PLy_procedure_name(PLy_curr_procedure
));
2568 char *sv
= PyString_AsString(so
);
2571 InputFunctionCall(&(plan
->args
[j
].out
.d
.typfunc
),
2573 plan
->args
[j
].out
.d
.typioparam
,
2590 InputFunctionCall(&(plan
->args
[j
].out
.d
.typfunc
),
2592 plan
->args
[j
].out
.d
.typioparam
,
2598 rv
= SPI_execute_plan(plan
->plan
, plan
->values
, nulls
,
2599 PLy_curr_procedure
->fn_readonly
, limit
);
2607 MemoryContextSwitchTo(oldcontext
);
2608 PLy_error_in_progress
= CopyErrorData();
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
));
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
);
2646 PLy_exception_set(PLy_exc_spi_error
,
2647 "SPI_execute_plan failed: %s",
2648 SPI_result_code_string(rv
));
2652 return PLy_spi_execute_fetch_result(SPI_tuptable
, SPI_processed
, rv
);
2656 PLy_spi_execute_query(char *query
, long limit
)
2659 MemoryContext oldcontext
;
2661 oldcontext
= CurrentMemoryContext
;
2664 rv
= SPI_execute(query
, PLy_curr_procedure
->fn_readonly
, limit
);
2668 MemoryContextSwitchTo(oldcontext
);
2669 PLy_error_in_progress
= CopyErrorData();
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
));
2683 PLy_exception_set(PLy_exc_spi_error
,
2684 "SPI_execute failed: %s",
2685 SPI_result_code_string(rv
));
2689 return PLy_spi_execute_fetch_result(SPI_tuptable
, SPI_processed
, rv
);
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
)
2712 Py_DECREF(result
->nrows
);
2713 result
->nrows
= PyInt_FromLong(rows
);
2714 PLy_typeinfo_init(&args
);
2716 oldcontext
= CurrentMemoryContext
;
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
],
2730 PyList_SetItem(result
->rows
, i
, row
);
2732 PLy_typeinfo_dealloc(&args
);
2734 SPI_freetuptable(tuptable
);
2739 MemoryContextSwitchTo(oldcontext
);
2740 PLy_error_in_progress
= CopyErrorData();
2742 if (!PyErr_Occurred())
2743 PLy_exception_set(PLy_exc_error
,
2744 "unrecognized error in PLy_spi_execute_fetch_result");
2746 PLy_typeinfo_dealloc(&args
);
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!
2768 /* Be sure we do initialization only once (should be redundant now) */
2769 static bool inited
= false;
2774 pg_bindtextdomain(TEXTDOMAIN
);
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");
2789 PLy_init_interp(void)
2793 mainmod
= PyImport_AddModule("__main__");
2794 if (mainmod
== NULL
|| PyErr_Occurred())
2795 PLy_elog(ERROR
, "could not import \"__main__\" module");
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
);
2801 if (PLy_interp_globals
== NULL
|| PyErr_Occurred())
2802 PLy_elog(ERROR
, "could not initialize globals");
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
*);
2851 PLy_debug(PyObject
*self
, PyObject
*args
)
2853 return PLy_output(DEBUG2
, self
, args
);
2857 PLy_log(PyObject
*self
, PyObject
*args
)
2859 return PLy_output(LOG
, self
, args
);
2863 PLy_info(PyObject
*self
, PyObject
*args
)
2865 return PLy_output(INFO
, self
, args
);
2869 PLy_notice(PyObject
*self
, PyObject
*args
)
2871 return PLy_output(NOTICE
, self
, args
);
2875 PLy_warning(PyObject
*self
, PyObject
*args
)
2877 return PLy_output(WARNING
, self
, args
);
2881 PLy_error(PyObject
*self
, PyObject
*args
)
2883 return PLy_output(ERROR
, self
, args
);
2887 PLy_fatal(PyObject
*self
, PyObject
*args
)
2889 return PLy_output(FATAL
, self
, args
);
2894 PLy_output(volatile int level
, PyObject
*self
, PyObject
*args
)
2898 MemoryContext oldcontext
;
2900 so
= PyObject_Str(args
);
2901 if (so
== NULL
|| ((sv
= PyString_AsString(so
)) == NULL
))
2904 sv
= dgettext(TEXTDOMAIN
, "could not parse error message in plpy.elog");
2907 oldcontext
= CurrentMemoryContext
;
2910 elog(level
, "%s", sv
);
2914 MemoryContextSwitchTo(oldcontext
);
2915 PLy_error_in_progress
= CopyErrorData();
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
);
2932 * return a legal object so the interpreter will continue on its merry way
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
2947 PLy_procedure_name(PLyProcedure
*proc
)
2950 return "<unknown procedure>";
2951 return proc
->proname
;
2955 * Call PyErr_SetString with a vprint interface and translation support
2958 PLy_exception_set(PyObject
*exc
, const char *fmt
,...)
2964 vsnprintf(buf
, sizeof(buf
), dgettext(TEXTDOMAIN
, fmt
), ap
);
2967 PyErr_SetString(exc
, buf
);
2971 * The same, pluralized.
2974 PLy_exception_set_plural(PyObject
*exc
,
2975 const char *fmt_singular
, const char *fmt_plural
,
2976 unsigned long n
,...)
2982 vsnprintf(buf
, sizeof(buf
),
2983 dngettext(TEXTDOMAIN
, fmt_singular
, fmt_plural
, n
),
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
2995 PLy_elog(int elevel
, const char *fmt
,...)
2999 StringInfoData emsg
;
3001 xmsg
= PLy_traceback(&xlevel
);
3003 initStringInfo(&emsg
);
3010 success
= appendStringInfoVA(&emsg
, dgettext(TEXTDOMAIN
, fmt
), ap
);
3014 enlargeStringInfo(&emsg
, emsg
.maxlen
);
3020 (errmsg("PL/Python: %s", emsg
.data
),
3021 (xmsg
) ? errdetail("%s", xmsg
) : 0));
3038 PLy_traceback(int *xlevel
)
3047 StringInfoData xstr
;
3050 * get the current exception
3052 PyErr_Fetch(&e
, &v
, &tb
);
3055 * oops, no exception, return
3063 PyErr_NormalizeException(&e
, &v
, &tb
);
3066 eob
= PyObject_Str(e
);
3067 if (v
&& ((vob
= PyObject_Str(v
)) != NULL
))
3068 vstr
= PyString_AsString(vob
);
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
);
3087 * intuit an appropriate error level based on the exception type
3089 if (PLy_exc_error
&& PyErr_GivenExceptionMatches(e
, PLy_exc_error
))
3091 else if (PLy_exc_fatal
&& PyErr_GivenExceptionMatches(e
, PLy_exc_fatal
))
3100 /* python module code */
3102 /* some dumb utility functions */
3104 PLy_malloc(size_t bytes
)
3106 void *ptr
= malloc(bytes
);
3110 (errcode(ERRCODE_OUT_OF_MEMORY
),
3111 errmsg("out of memory")));
3116 PLy_malloc0(size_t bytes
)
3118 void *ptr
= PLy_malloc(bytes
);
3120 MemSet(ptr
, 0, bytes
);
3125 PLy_strdup(const char *str
)
3130 len
= strlen(str
) + 1;
3131 result
= PLy_malloc(len
);
3132 memcpy(result
, str
, len
);
3137 /* define this away */