]> granicus.if.org Git - postgresql/blob - src/pl/plpython/plpython.c
Defend against possible crash if a plpython function does not specify names
[postgresql] / src / pl / plpython / plpython.c
1 /**********************************************************************
2  * plpython.c - python as a procedural language for PostgreSQL
3  *
4  *      $PostgreSQL: pgsql/src/pl/plpython/plpython.c,v 1.120 2009/04/03 16:59:42 tgl Exp $
5  *
6  *********************************************************************
7  */
8
9 #if defined(_MSC_VER) && defined(_DEBUG)
10 /* Python uses #pragma to bring in a non-default libpython on VC++ if
11  * _DEBUG is defined */
12 #undef _DEBUG
13 /* Also hide away errcode, since we load Python.h before postgres.h */
14 #define errcode __msvc_errcode
15 #include <Python.h>
16 #undef errcode
17 #define _DEBUG
18 #elif defined (_MSC_VER)
19 #define errcode __msvc_errcode
20 #include <Python.h>
21 #undef errcode
22 #else
23 #include <Python.h>
24 #endif
25
26 /*
27  * Py_ssize_t compat for Python <= 2.4
28  */
29 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
30 typedef int Py_ssize_t;
31
32 #define PY_SSIZE_T_MAX INT_MAX
33 #define PY_SSIZE_T_MIN INT_MIN
34 #endif
35
36 /*
37  * PyBool_FromLong is supported from 2.3.
38  */
39 #if PY_VERSION_HEX < 0x02030000
40 #define PyBool_FromLong(x) PyInt_FromLong(x)
41 #endif
42
43
44 #include "postgres.h"
45
46 /* system stuff */
47 #include <unistd.h>
48 #include <fcntl.h>
49
50 /* postgreSQL stuff */
51 #include "catalog/pg_proc.h"
52 #include "catalog/pg_type.h"
53 #include "commands/trigger.h"
54 #include "executor/spi.h"
55 #include "funcapi.h"
56 #include "fmgr.h"
57 #include "miscadmin.h"
58 #include "nodes/makefuncs.h"
59 #include "parser/parse_type.h"
60 #include "tcop/tcopprot.h"
61 #include "utils/builtins.h"
62 #include "utils/lsyscache.h"
63 #include "utils/memutils.h"
64 #include "utils/syscache.h"
65 #include "utils/typcache.h"
66
67 /* define our text domain for translations */
68 #undef TEXTDOMAIN
69 #define TEXTDOMAIN PG_TEXTDOMAIN("plpython")
70
71 #include <compile.h>
72 #include <eval.h>
73
74 PG_MODULE_MAGIC;
75
76 /* convert Postgresql Datum or tuple into a PyObject.
77  * input to Python.  Tuples are converted to dictionary
78  * objects.
79  */
80
81 typedef PyObject *(*PLyDatumToObFunc) (const char *);
82
83 typedef struct PLyDatumToOb
84 {
85         PLyDatumToObFunc func;
86         FmgrInfo        typfunc;                /* The type's output function */
87         Oid                     typoid;                 /* The OID of the type */
88         Oid                     typioparam;
89         bool            typbyval;
90 }       PLyDatumToOb;
91
92 typedef struct PLyTupleToOb
93 {
94         PLyDatumToOb *atts;
95         int                     natts;
96 }       PLyTupleToOb;
97
98 typedef union PLyTypeInput
99 {
100         PLyDatumToOb d;
101         PLyTupleToOb r;
102 }       PLyTypeInput;
103
104 /* convert PyObject to a Postgresql Datum or tuple.
105  * output from Python
106  */
107 typedef struct PLyObToDatum
108 {
109         FmgrInfo        typfunc;                /* The type's input function */
110         Oid                     typoid;                 /* The OID of the type */
111         Oid                     typioparam;
112         bool            typbyval;
113 }       PLyObToDatum;
114
115 typedef struct PLyObToTuple
116 {
117         PLyObToDatum *atts;
118         int                     natts;
119 }       PLyObToTuple;
120
121 typedef union PLyTypeOutput
122 {
123         PLyObToDatum d;
124         PLyObToTuple r;
125 }       PLyTypeOutput;
126
127 /* all we need to move Postgresql data to Python objects,
128  * and vis versa
129  */
130 typedef struct PLyTypeInfo
131 {
132         PLyTypeInput in;
133         PLyTypeOutput out;
134         int                     is_rowtype;
135
136         /*
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
139          */
140 }       PLyTypeInfo;
141
142
143 /* cached procedure data */
144 typedef struct PLyProcedure
145 {
146         char       *proname;            /* SQL name of procedure */
147         char       *pyname;                     /* Python name of procedure */
148         TransactionId fn_xmin;
149         ItemPointerData fn_tid;
150         bool            fn_readonly;
151         PLyTypeInfo result;                     /* also used to store info for trigger tuple
152                                                                  * type */
153         bool            is_setof;               /* true, if procedure returns result set */
154         PyObject   *setof;                      /* contents of result set. */
155         char      **argnames;           /* Argument names */
156         PLyTypeInfo args[FUNC_MAX_ARGS];
157         int                     nargs;
158         PyObject   *code;                       /* compiled procedure code */
159         PyObject   *statics;            /* data saved across calls, local scope */
160         PyObject   *globals;            /* data saved across calls, global scope */
161         PyObject   *me;                         /* PyCObject containing pointer to this
162                                                                  * PLyProcedure */
163 }       PLyProcedure;
164
165
166 /* Python objects */
167 typedef struct PLyPlanObject
168 {
169         PyObject_HEAD
170         void       *plan;                       /* return of an SPI_saveplan */
171         int                     nargs;
172         Oid                *types;
173         Datum      *values;
174         PLyTypeInfo *args;
175 }       PLyPlanObject;
176
177 typedef struct PLyResultObject
178 {
179         PyObject_HEAD
180         /* HeapTuple *tuples; */
181         PyObject * nrows;                       /* number of rows returned by query */
182         PyObject   *rows;                       /* data rows, or None if no data returned */
183         PyObject   *status;                     /* query status, SPI_OK_*, or SPI_ERR_* */
184 }       PLyResultObject;
185
186
187 /* function declarations */
188
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.
192  */
193 Datum           plpython_call_handler(PG_FUNCTION_ARGS);
194 void            _PG_init(void);
195
196 PG_FUNCTION_INFO_V1(plpython_call_handler);
197
198 /* most of the remaining of the declarations, all static */
199
200 /* these should only be called once at the first call
201  * of plpython_call_handler.  initialize the python interpreter
202  * and global data.
203  */
204 static void PLy_init_interp(void);
205 static void PLy_init_plpy(void);
206
207 /* call PyErr_SetString with a vprint interface and translation support */
208 static void
209 PLy_exception_set(PyObject *, const char *,...)
210 __attribute__((format(printf, 2, 3)));
211
212 /* Get the innermost python procedure called from the backend */
213 static char *PLy_procedure_name(PLyProcedure *);
214
215 /* some utility functions */
216 static void PLy_elog(int, const char *,...)
217 __attribute__((format(printf, 2, 3)));
218 static char *PLy_traceback(int *);
219
220 static void *PLy_malloc(size_t);
221 static void *PLy_malloc0(size_t);
222 static char *PLy_strdup(const char *);
223 static void PLy_free(void *);
224
225 /* sub handlers for functions and triggers */
226 static Datum PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure *);
227 static HeapTuple PLy_trigger_handler(FunctionCallInfo fcinfo, PLyProcedure *);
228
229 static PyObject *PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *);
230 static void PLy_function_delete_args(PLyProcedure *);
231 static PyObject *PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *,
232                                            HeapTuple *);
233 static HeapTuple PLy_modify_tuple(PLyProcedure *, PyObject *,
234                                  TriggerData *, HeapTuple);
235
236 static PyObject *PLy_procedure_call(PLyProcedure *, char *, PyObject *);
237
238 static PLyProcedure *PLy_procedure_get(FunctionCallInfo fcinfo,
239                                   Oid tgreloid);
240
241 static PLyProcedure *PLy_procedure_create(HeapTuple procTup, Oid tgreloid,
242                                                                                   char *key);
243
244 static void PLy_procedure_compile(PLyProcedure *, const char *);
245 static char *PLy_procedure_munge_source(const char *, const char *);
246 static void PLy_procedure_delete(PLyProcedure *);
247
248 static void PLy_typeinfo_init(PLyTypeInfo *);
249 static void PLy_typeinfo_dealloc(PLyTypeInfo *);
250 static void PLy_output_datum_func(PLyTypeInfo *, HeapTuple);
251 static void PLy_output_datum_func2(PLyObToDatum *, HeapTuple);
252 static void PLy_input_datum_func(PLyTypeInfo *, Oid, HeapTuple);
253 static void PLy_input_datum_func2(PLyDatumToOb *, Oid, HeapTuple);
254 static void PLy_output_tuple_funcs(PLyTypeInfo *, TupleDesc);
255 static void PLy_input_tuple_funcs(PLyTypeInfo *, TupleDesc);
256
257 /* conversion functions */
258 static PyObject *PLyDict_FromTuple(PLyTypeInfo *, HeapTuple, TupleDesc);
259 static PyObject *PLyBool_FromString(const char *);
260 static PyObject *PLyFloat_FromString(const char *);
261 static PyObject *PLyInt_FromString(const char *);
262 static PyObject *PLyLong_FromString(const char *);
263 static PyObject *PLyString_FromString(const char *);
264
265 static HeapTuple PLyMapping_ToTuple(PLyTypeInfo *, PyObject *);
266 static HeapTuple PLySequence_ToTuple(PLyTypeInfo *, PyObject *);
267 static HeapTuple PLyObject_ToTuple(PLyTypeInfo *, PyObject *);
268
269 /*
270  * Currently active plpython function
271  */
272 static PLyProcedure *PLy_curr_procedure = NULL;
273
274 /*
275  * When a callback from Python into PG incurs an error, we temporarily store
276  * the error information here, and return NULL to the Python interpreter.
277  * Any further callback attempts immediately fail, and when the Python
278  * interpreter returns to the calling function, we re-throw the error (even if
279  * Python thinks it trapped the error and doesn't return NULL).  Eventually
280  * this ought to be improved to let Python code really truly trap the error,
281  * but that's more of a change from the pre-8.0 semantics than I have time for
282  * now --- it will only be possible if the callback query is executed inside a
283  * subtransaction.
284  */
285 static ErrorData *PLy_error_in_progress = NULL;
286
287 static PyObject *PLy_interp_globals = NULL;
288 static PyObject *PLy_interp_safe_globals = NULL;
289 static PyObject *PLy_procedure_cache = NULL;
290
291 /* Python exceptions */
292 static PyObject *PLy_exc_error = NULL;
293 static PyObject *PLy_exc_fatal = NULL;
294 static PyObject *PLy_exc_spi_error = NULL;
295
296 /* some globals for the python module */
297 static char PLy_plan_doc[] = {
298         "Store a PostgreSQL plan"
299 };
300
301 static char PLy_result_doc[] = {
302         "Results of a PostgreSQL query"
303 };
304
305
306 /*
307  * the function definitions
308  */
309
310 /*
311  * This routine is a crock, and so is everyplace that calls it.  The problem
312  * is that the cached form of plpython functions/queries is allocated permanently
313  * (mostly via malloc()) and never released until backend exit.  Subsidiary
314  * data structures such as fmgr info records therefore must live forever
315  * as well.  A better implementation would store all this stuff in a per-
316  * function memory context that could be reclaimed at need.  In the meantime,
317  * fmgr_info_cxt must be called specifying TopMemoryContext so that whatever
318  * it might allocate, and whatever the eventual function might allocate using
319  * fn_mcxt, will live forever too.
320  */
321 static void
322 perm_fmgr_info(Oid functionId, FmgrInfo *finfo)
323 {
324         fmgr_info_cxt(functionId, finfo, TopMemoryContext);
325 }
326
327 Datum
328 plpython_call_handler(PG_FUNCTION_ARGS)
329 {
330         Datum           retval;
331         PLyProcedure *save_curr_proc;
332         PLyProcedure *volatile proc = NULL;
333
334         if (SPI_connect() != SPI_OK_CONNECT)
335                 elog(ERROR, "SPI_connect failed");
336
337         save_curr_proc = PLy_curr_procedure;
338
339         PG_TRY();
340         {
341                 if (CALLED_AS_TRIGGER(fcinfo))
342                 {
343                         TriggerData *tdata = (TriggerData *) fcinfo->context;
344                         HeapTuple       trv;
345
346                         proc = PLy_procedure_get(fcinfo,
347                                                                          RelationGetRelid(tdata->tg_relation));
348                         PLy_curr_procedure = proc;
349                         trv = PLy_trigger_handler(fcinfo, proc);
350                         retval = PointerGetDatum(trv);
351                 }
352                 else
353                 {
354                         proc = PLy_procedure_get(fcinfo, InvalidOid);
355                         PLy_curr_procedure = proc;
356                         retval = PLy_function_handler(fcinfo, proc);
357                 }
358         }
359         PG_CATCH();
360         {
361                 PLy_curr_procedure = save_curr_proc;
362                 if (proc)
363                 {
364                         /* note: Py_DECREF needs braces around it, as of 2003/08 */
365                         Py_DECREF(proc->me);
366                 }
367                 PyErr_Clear();
368                 PG_RE_THROW();
369         }
370         PG_END_TRY();
371
372         PLy_curr_procedure = save_curr_proc;
373
374         Py_DECREF(proc->me);
375
376         return retval;
377 }
378
379 /* trigger and function sub handlers
380  *
381  * the python function is expected to return Py_None if the tuple is
382  * acceptable and unmodified.  Otherwise it should return a PyString
383  * object who's value is SKIP, or MODIFY.  SKIP means don't perform
384  * this action.  MODIFY means the tuple has been modified, so update
385  * tuple and perform action.  SKIP and MODIFY assume the trigger fires
386  * BEFORE the event and is ROW level.  postgres expects the function
387  * to take no arguments and return an argument of type trigger.
388  */
389 static HeapTuple
390 PLy_trigger_handler(FunctionCallInfo fcinfo, PLyProcedure * proc)
391 {
392         HeapTuple       rv = NULL;
393         PyObject   *volatile plargs = NULL;
394         PyObject   *volatile plrv = NULL;
395
396         PG_TRY();
397         {
398                 plargs = PLy_trigger_build_args(fcinfo, proc, &rv);
399                 plrv = PLy_procedure_call(proc, "TD", plargs);
400
401                 Assert(plrv != NULL);
402                 Assert(!PLy_error_in_progress);
403
404                 /*
405                  * Disconnect from SPI manager
406                  */
407                 if (SPI_finish() != SPI_OK_FINISH)
408                         elog(ERROR, "SPI_finish failed");
409
410                 /*
411                  * return of None means we're happy with the tuple
412                  */
413                 if (plrv != Py_None)
414                 {
415                         char       *srv;
416
417                         if (!PyString_Check(plrv))
418                                 ereport(ERROR,
419                                                 (errcode(ERRCODE_DATA_EXCEPTION),
420                                         errmsg("unexpected return value from trigger procedure"),
421                                                  errdetail("Expected None or a string.")));
422
423                         srv = PyString_AsString(plrv);
424                         if (pg_strcasecmp(srv, "SKIP") == 0)
425                                 rv = NULL;
426                         else if (pg_strcasecmp(srv, "MODIFY") == 0)
427                         {
428                                 TriggerData *tdata = (TriggerData *) fcinfo->context;
429
430                                 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event) ||
431                                         TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
432                                         rv = PLy_modify_tuple(proc, plargs, tdata, rv);
433                                 else
434                                         ereport(WARNING,
435                                                         (errmsg("PL/Python trigger function returned \"MODIFY\" in a DELETE trigger -- ignored")));
436                         }
437                         else if (pg_strcasecmp(srv, "OK") != 0)
438                         {
439                                 /*
440                                  * accept "OK" as an alternative to None; otherwise, raise an
441                                  * error
442                                  */
443                                 ereport(ERROR,
444                                                 (errcode(ERRCODE_DATA_EXCEPTION),
445                                         errmsg("unexpected return value from trigger procedure"),
446                                                  errdetail("Expected None, \"OK\", \"SKIP\", or \"MODIFY\".")));
447                         }
448                 }
449         }
450         PG_CATCH();
451         {
452                 Py_XDECREF(plargs);
453                 Py_XDECREF(plrv);
454
455                 PG_RE_THROW();
456         }
457         PG_END_TRY();
458
459         Py_DECREF(plargs);
460         Py_DECREF(plrv);
461
462         return rv;
463 }
464
465 static HeapTuple
466 PLy_modify_tuple(PLyProcedure * proc, PyObject * pltd, TriggerData *tdata,
467                                  HeapTuple otup)
468 {
469         PyObject   *volatile plntup;
470         PyObject   *volatile plkeys;
471         PyObject   *volatile platt;
472         PyObject   *volatile plval;
473         PyObject   *volatile plstr;
474         HeapTuple       rtup;
475         int                     natts,
476                                 i,
477                                 attn,
478                                 atti;
479         int                *volatile modattrs;
480         Datum      *volatile modvalues;
481         char       *volatile modnulls;
482         TupleDesc       tupdesc;
483
484         plntup = plkeys = platt = plval = plstr = NULL;
485         modattrs = NULL;
486         modvalues = NULL;
487         modnulls = NULL;
488
489         PG_TRY();
490         {
491                 if ((plntup = PyDict_GetItemString(pltd, "new")) == NULL)
492                         ereport(ERROR, 
493                                         (errmsg("TD[\"new\"] deleted, cannot modify row")));
494                 if (!PyDict_Check(plntup))
495                         ereport(ERROR,
496                                         (errmsg("TD[\"new\"] is not a dictionary")));
497                 Py_INCREF(plntup);
498
499                 plkeys = PyDict_Keys(plntup);
500                 natts = PyList_Size(plkeys);
501
502                 modattrs = (int *) palloc(natts * sizeof(int));
503                 modvalues = (Datum *) palloc(natts * sizeof(Datum));
504                 modnulls = (char *) palloc(natts * sizeof(char));
505
506                 tupdesc = tdata->tg_relation->rd_att;
507
508                 for (i = 0; i < natts; i++)
509                 {
510                         char       *src;
511
512                         platt = PyList_GetItem(plkeys, i);
513                         if (!PyString_Check(platt))
514                                 ereport(ERROR,
515                                                 (errmsg("name of TD[\"new\"] attribute at ordinal position %d is not a string", i)));
516                         attn = SPI_fnumber(tupdesc, PyString_AsString(platt));
517                         if (attn == SPI_ERROR_NOATTRIBUTE)
518                                 ereport(ERROR,
519                                                 (errmsg("key \"%s\" found in TD[\"new\"] does not exist as a column in the triggering row",
520                                                                 PyString_AsString(platt))));
521                         atti = attn - 1;
522
523                         plval = PyDict_GetItem(plntup, platt);
524                         if (plval == NULL)
525                                 elog(FATAL, "Python interpreter is probably corrupted");
526
527                         Py_INCREF(plval);
528
529                         modattrs[i] = attn;
530
531                         if (tupdesc->attrs[atti]->attisdropped)
532                         {
533                                 modvalues[i] = (Datum) 0;
534                                 modnulls[i] = 'n';
535                         }
536                         else if (plval != Py_None)
537                         {
538                                 plstr = PyObject_Str(plval);
539                                 if (!plstr)
540                                         PLy_elog(ERROR, "could not compute string representation of Python object in PL/Python function \"%s\" while modifying trigger row",
541                                                          proc->proname);
542                                 src = PyString_AsString(plstr);
543
544                                 modvalues[i] =
545                                         InputFunctionCall(&proc->result.out.r.atts[atti].typfunc,
546                                                                           src,
547                                                                         proc->result.out.r.atts[atti].typioparam,
548                                                                           tupdesc->attrs[atti]->atttypmod);
549                                 modnulls[i] = ' ';
550
551                                 Py_DECREF(plstr);
552                                 plstr = NULL;
553                         }
554                         else
555                         {
556                                 modvalues[i] =
557                                         InputFunctionCall(&proc->result.out.r.atts[atti].typfunc,
558                                                                           NULL,
559                                                                         proc->result.out.r.atts[atti].typioparam,
560                                                                           tupdesc->attrs[atti]->atttypmod);
561                                 modnulls[i] = 'n';
562                         }
563
564                         Py_DECREF(plval);
565                         plval = NULL;
566                 }
567
568                 rtup = SPI_modifytuple(tdata->tg_relation, otup, natts,
569                                                            modattrs, modvalues, modnulls);
570                 if (rtup == NULL)
571                         elog(ERROR, "SPI_modifytuple failed: error %d", SPI_result);
572         }
573         PG_CATCH();
574         {
575                 Py_XDECREF(plntup);
576                 Py_XDECREF(plkeys);
577                 Py_XDECREF(plval);
578                 Py_XDECREF(plstr);
579
580                 if (modnulls)
581                         pfree(modnulls);
582                 if (modvalues)
583                         pfree(modvalues);
584                 if (modattrs)
585                         pfree(modattrs);
586
587                 PG_RE_THROW();
588         }
589         PG_END_TRY();
590
591         Py_DECREF(plntup);
592         Py_DECREF(plkeys);
593
594         pfree(modattrs);
595         pfree(modvalues);
596         pfree(modnulls);
597
598         return rtup;
599 }
600
601 static PyObject *
602 PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure * proc, HeapTuple *rv)
603 {
604         TriggerData *tdata = (TriggerData *) fcinfo->context;
605         PyObject   *pltname,
606                            *pltevent,
607                            *pltwhen,
608                            *pltlevel,
609                            *pltrelid,
610                            *plttablename,
611                            *plttableschema;
612         PyObject   *pltargs,
613                            *pytnew,
614                            *pytold;
615         PyObject   *volatile pltdata = NULL;
616         char       *stroid;
617
618         PG_TRY();
619         {
620                 pltdata = PyDict_New();
621                 if (!pltdata)
622                         PLy_elog(ERROR, "could not create new dictionary while building trigger arguments");
623
624                 pltname = PyString_FromString(tdata->tg_trigger->tgname);
625                 PyDict_SetItemString(pltdata, "name", pltname);
626                 Py_DECREF(pltname);
627
628                 stroid = DatumGetCString(DirectFunctionCall1(oidout,
629                                                            ObjectIdGetDatum(tdata->tg_relation->rd_id)));
630                 pltrelid = PyString_FromString(stroid);
631                 PyDict_SetItemString(pltdata, "relid", pltrelid);
632                 Py_DECREF(pltrelid);
633                 pfree(stroid);
634
635                 stroid = SPI_getrelname(tdata->tg_relation);
636                 plttablename = PyString_FromString(stroid);
637                 PyDict_SetItemString(pltdata, "table_name", plttablename);
638                 Py_DECREF(plttablename);
639                 pfree(stroid);
640
641                 stroid = SPI_getnspname(tdata->tg_relation);
642                 plttableschema = PyString_FromString(stroid);
643                 PyDict_SetItemString(pltdata, "table_schema", plttableschema);
644                 Py_DECREF(plttableschema);
645                 pfree(stroid);
646
647
648                 if (TRIGGER_FIRED_BEFORE(tdata->tg_event))
649                         pltwhen = PyString_FromString("BEFORE");
650                 else if (TRIGGER_FIRED_AFTER(tdata->tg_event))
651                         pltwhen = PyString_FromString("AFTER");
652                 else
653                 {
654                         elog(ERROR, "unrecognized WHEN tg_event: %u", tdata->tg_event);
655                         pltwhen = NULL;         /* keep compiler quiet */
656                 }
657                 PyDict_SetItemString(pltdata, "when", pltwhen);
658                 Py_DECREF(pltwhen);
659
660                 if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
661                 {
662                         pltlevel = PyString_FromString("ROW");
663                         PyDict_SetItemString(pltdata, "level", pltlevel);
664                         Py_DECREF(pltlevel);
665
666                         if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
667                         {
668                                 pltevent = PyString_FromString("INSERT");
669
670                                 PyDict_SetItemString(pltdata, "old", Py_None);
671                                 pytnew = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
672                                                                                    tdata->tg_relation->rd_att);
673                                 PyDict_SetItemString(pltdata, "new", pytnew);
674                                 Py_DECREF(pytnew);
675                                 *rv = tdata->tg_trigtuple;
676                         }
677                         else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
678                         {
679                                 pltevent = PyString_FromString("DELETE");
680
681                                 PyDict_SetItemString(pltdata, "new", Py_None);
682                                 pytold = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
683                                                                                    tdata->tg_relation->rd_att);
684                                 PyDict_SetItemString(pltdata, "old", pytold);
685                                 Py_DECREF(pytold);
686                                 *rv = tdata->tg_trigtuple;
687                         }
688                         else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
689                         {
690                                 pltevent = PyString_FromString("UPDATE");
691
692                                 pytnew = PLyDict_FromTuple(&(proc->result), tdata->tg_newtuple,
693                                                                                    tdata->tg_relation->rd_att);
694                                 PyDict_SetItemString(pltdata, "new", pytnew);
695                                 Py_DECREF(pytnew);
696                                 pytold = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
697                                                                                    tdata->tg_relation->rd_att);
698                                 PyDict_SetItemString(pltdata, "old", pytold);
699                                 Py_DECREF(pytold);
700                                 *rv = tdata->tg_newtuple;
701                         }
702                         else
703                         {
704                                 elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
705                                 pltevent = NULL;        /* keep compiler quiet */
706                         }
707
708                         PyDict_SetItemString(pltdata, "event", pltevent);
709                         Py_DECREF(pltevent);
710                 }
711                 else if (TRIGGER_FIRED_FOR_STATEMENT(tdata->tg_event))
712                 {
713                         pltlevel = PyString_FromString("STATEMENT");
714                         PyDict_SetItemString(pltdata, "level", pltlevel);
715                         Py_DECREF(pltlevel);
716
717                         PyDict_SetItemString(pltdata, "old", Py_None);
718                         PyDict_SetItemString(pltdata, "new", Py_None);
719                         *rv = NULL;
720
721                         if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
722                                 pltevent = PyString_FromString("INSERT");
723                         else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
724                                 pltevent = PyString_FromString("DELETE");
725                         else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
726                                 pltevent = PyString_FromString("UPDATE");
727                         else if (TRIGGER_FIRED_BY_TRUNCATE(tdata->tg_event))
728                                 pltevent = PyString_FromString("TRUNCATE");
729                         else
730                         {
731                                 elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
732                                 pltevent = NULL;        /* keep compiler quiet */
733                         }
734
735                         PyDict_SetItemString(pltdata, "event", pltevent);
736                         Py_DECREF(pltevent);
737                 }
738                 else
739                         elog(ERROR, "unrecognized LEVEL tg_event: %u", tdata->tg_event);
740
741                 if (tdata->tg_trigger->tgnargs)
742                 {
743                         /*
744                          * all strings...
745                          */
746                         int                     i;
747                         PyObject   *pltarg;
748
749                         pltargs = PyList_New(tdata->tg_trigger->tgnargs);
750                         for (i = 0; i < tdata->tg_trigger->tgnargs; i++)
751                         {
752                                 pltarg = PyString_FromString(tdata->tg_trigger->tgargs[i]);
753
754                                 /*
755                                  * stolen, don't Py_DECREF
756                                  */
757                                 PyList_SetItem(pltargs, i, pltarg);
758                         }
759                 }
760                 else
761                 {
762                         Py_INCREF(Py_None);
763                         pltargs = Py_None;
764                 }
765                 PyDict_SetItemString(pltdata, "args", pltargs);
766                 Py_DECREF(pltargs);
767         }
768         PG_CATCH();
769         {
770                 Py_XDECREF(pltdata);
771                 PG_RE_THROW();
772         }
773         PG_END_TRY();
774
775         return pltdata;
776 }
777
778
779
780 /* function handler and friends */
781 static Datum
782 PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure * proc)
783 {
784         Datum           rv;
785         PyObject   *volatile plargs = NULL;
786         PyObject   *volatile plrv = NULL;
787         PyObject   *volatile plrv_so = NULL;
788         char       *plrv_sc;
789
790         PG_TRY();
791         {
792                 if (!proc->is_setof || proc->setof == NULL)
793                 {
794                         /* Simple type returning function or first time for SETOF function */
795                         plargs = PLy_function_build_args(fcinfo, proc);
796                         plrv = PLy_procedure_call(proc, "args", plargs);
797                         if (!proc->is_setof)
798
799                                 /*
800                                  * SETOF function parameters will be deleted when last row is
801                                  * returned
802                                  */
803                                 PLy_function_delete_args(proc);
804                         Assert(plrv != NULL);
805                         Assert(!PLy_error_in_progress);
806                 }
807
808                 /*
809                  * Disconnect from SPI manager and then create the return values datum
810                  * (if the input function does a palloc for it this must not be
811                  * allocated in the SPI memory context because SPI_finish would free
812                  * it).
813                  */
814                 if (SPI_finish() != SPI_OK_FINISH)
815                         elog(ERROR, "SPI_finish failed");
816
817                 if (proc->is_setof)
818                 {
819                         bool            has_error = false;
820                         ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
821
822                         if (proc->setof == NULL)
823                         {
824                                 /* first time -- do checks and setup */
825                                 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
826                                         (rsi->allowedModes & SFRM_ValuePerCall) == 0)
827                                 {
828                                         ereport(ERROR,
829                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
830                                                          errmsg("unsupported set function return mode"),
831                                                          errdetail("PL/Python set-returning functions only support returning only value per call.")));
832                                 }
833                                 rsi->returnMode = SFRM_ValuePerCall;
834
835                                 /* Make iterator out of returned object */
836                                 proc->setof = PyObject_GetIter(plrv);
837                                 Py_DECREF(plrv);
838                                 plrv = NULL;
839
840                                 if (proc->setof == NULL)
841                                         ereport(ERROR,
842                                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
843                                                          errmsg("returned object cannot be iterated"),
844                                         errdetail("PL/Python set-returning functions must return an iterable object.")));
845                         }
846
847                         /* Fetch next from iterator */
848                         plrv = PyIter_Next(proc->setof);
849                         if (plrv)
850                                 rsi->isDone = ExprMultipleResult;
851                         else
852                         {
853                                 rsi->isDone = ExprEndResult;
854                                 has_error = PyErr_Occurred() != NULL;
855                         }
856
857                         if (rsi->isDone == ExprEndResult)
858                         {
859                                 /* Iterator is exhausted or error happened */
860                                 Py_DECREF(proc->setof);
861                                 proc->setof = NULL;
862
863                                 Py_XDECREF(plargs);
864                                 Py_XDECREF(plrv);
865                                 Py_XDECREF(plrv_so);
866
867                                 PLy_function_delete_args(proc);
868
869                                 if (has_error)
870                                         ereport(ERROR,
871                                                         (errcode(ERRCODE_DATA_EXCEPTION),
872                                                   errmsg("error fetching next item from iterator")));
873
874                                 fcinfo->isnull = true;
875                                 return (Datum) NULL;
876                         }
877                 }
878
879                 /*
880                  * If the function is declared to return void, the Python return value
881                  * must be None. For void-returning functions, we also treat a None
882                  * return value as a special "void datum" rather than NULL (as is the
883                  * case for non-void-returning functions).
884                  */
885                 if (proc->result.out.d.typoid == VOIDOID)
886                 {
887                         if (plrv != Py_None)
888                                 ereport(ERROR,
889                                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
890                                            errmsg("PL/Python function with return type \"void\" did not return None")));
891
892                         fcinfo->isnull = false;
893                         rv = (Datum) 0;
894                 }
895                 else if (plrv == Py_None)
896                 {
897                         fcinfo->isnull = true;
898                         if (proc->result.is_rowtype < 1)
899                                 rv = InputFunctionCall(&proc->result.out.d.typfunc,
900                                                                            NULL,
901                                                                            proc->result.out.d.typioparam,
902                                                                            -1);
903                         else
904                                 /* Tuple as None */
905                                 rv = (Datum) NULL;
906                 }
907                 else if (proc->result.is_rowtype >= 1)
908                 {
909                         HeapTuple       tuple = NULL;
910
911                         if (PySequence_Check(plrv))
912                                 /* composite type as sequence (tuple, list etc) */
913                                 tuple = PLySequence_ToTuple(&proc->result, plrv);
914                         else if (PyMapping_Check(plrv))
915                                 /* composite type as mapping (currently only dict) */
916                                 tuple = PLyMapping_ToTuple(&proc->result, plrv);
917                         else
918                                 /* returned as smth, must provide method __getattr__(name) */
919                                 tuple = PLyObject_ToTuple(&proc->result, plrv);
920
921                         if (tuple != NULL)
922                         {
923                                 fcinfo->isnull = false;
924                                 rv = HeapTupleGetDatum(tuple);
925                         }
926                         else
927                         {
928                                 fcinfo->isnull = true;
929                                 rv = (Datum) NULL;
930                         }
931                 }
932                 else
933                 {
934                         fcinfo->isnull = false;
935                         plrv_so = PyObject_Str(plrv);
936                         if (!plrv_so)
937                                 PLy_elog(ERROR, "could not create string representation of Python object in PL/Python function \"%s\" while creating return value", proc->proname);
938                         plrv_sc = PyString_AsString(plrv_so);
939                         rv = InputFunctionCall(&proc->result.out.d.typfunc,
940                                                                    plrv_sc,
941                                                                    proc->result.out.d.typioparam,
942                                                                    -1);
943                 }
944         }
945         PG_CATCH();
946         {
947                 Py_XDECREF(plargs);
948                 Py_XDECREF(plrv);
949                 Py_XDECREF(plrv_so);
950
951                 PG_RE_THROW();
952         }
953         PG_END_TRY();
954
955         Py_XDECREF(plargs);
956         Py_DECREF(plrv);
957         Py_XDECREF(plrv_so);
958
959         return rv;
960 }
961
962 static PyObject *
963 PLy_procedure_call(PLyProcedure * proc, char *kargs, PyObject * vargs)
964 {
965         PyObject   *rv;
966
967         PyDict_SetItemString(proc->globals, kargs, vargs);
968         rv = PyEval_EvalCode((PyCodeObject *) proc->code,
969                                                  proc->globals, proc->globals);
970
971         /*
972          * If there was an error in a PG callback, propagate that no matter what
973          * Python claims about its success.
974          */
975         if (PLy_error_in_progress)
976         {
977                 ErrorData  *edata = PLy_error_in_progress;
978
979                 PLy_error_in_progress = NULL;
980                 ReThrowError(edata);
981         }
982
983         if (rv == NULL || PyErr_Occurred())
984         {
985                 Py_XDECREF(rv);
986                 PLy_elog(ERROR, "PL/Python function \"%s\" failed", proc->proname);
987         }
988
989         return rv;
990 }
991
992 static PyObject *
993 PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure * proc)
994 {
995         PyObject   *volatile arg = NULL;
996         PyObject   *volatile args = NULL;
997         int                     i;
998
999         PG_TRY();
1000         {
1001                 args = PyList_New(proc->nargs);
1002                 for (i = 0; i < proc->nargs; i++)
1003                 {
1004                         if (proc->args[i].is_rowtype > 0)
1005                         {
1006                                 if (fcinfo->argnull[i])
1007                                         arg = NULL;
1008                                 else
1009                                 {
1010                                         HeapTupleHeader td;
1011                                         Oid                     tupType;
1012                                         int32           tupTypmod;
1013                                         TupleDesc       tupdesc;
1014                                         HeapTupleData tmptup;
1015
1016                                         td = DatumGetHeapTupleHeader(fcinfo->arg[i]);
1017                                         /* Extract rowtype info and find a tupdesc */
1018                                         tupType = HeapTupleHeaderGetTypeId(td);
1019                                         tupTypmod = HeapTupleHeaderGetTypMod(td);
1020                                         tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
1021
1022                                         /* Set up I/O funcs if not done yet */
1023                                         if (proc->args[i].is_rowtype != 1)
1024                                                 PLy_input_tuple_funcs(&(proc->args[i]), tupdesc);
1025
1026                                         /* Build a temporary HeapTuple control structure */
1027                                         tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
1028                                         tmptup.t_data = td;
1029
1030                                         arg = PLyDict_FromTuple(&(proc->args[i]), &tmptup, tupdesc);
1031                                         ReleaseTupleDesc(tupdesc);
1032                                 }
1033                         }
1034                         else
1035                         {
1036                                 if (fcinfo->argnull[i])
1037                                         arg = NULL;
1038                                 else
1039                                 {
1040                                         char       *ct;
1041
1042                                         ct = OutputFunctionCall(&(proc->args[i].in.d.typfunc),
1043                                                                                         fcinfo->arg[i]);
1044                                         arg = (proc->args[i].in.d.func) (ct);
1045                                         pfree(ct);
1046                                 }
1047                         }
1048
1049                         if (arg == NULL)
1050                         {
1051                                 Py_INCREF(Py_None);
1052                                 arg = Py_None;
1053                         }
1054
1055                         if (PyList_SetItem(args, i, arg) == -1)
1056                                 PLy_elog(ERROR, "PyList_SetItem() failed for PL/Python function \"%s\" while setting up arguments", proc->proname);
1057
1058                         if (proc->argnames && proc->argnames[i] &&
1059                                 PyDict_SetItemString(proc->globals, proc->argnames[i], arg) == -1)
1060                                 PLy_elog(ERROR, "PyDict_SetItemString() failed for PL/Python function \"%s\" while setting up arguments", proc->proname);
1061                         arg = NULL;
1062                 }
1063         }
1064         PG_CATCH();
1065         {
1066                 Py_XDECREF(arg);
1067                 Py_XDECREF(args);
1068
1069                 PG_RE_THROW();
1070         }
1071         PG_END_TRY();
1072
1073         return args;
1074 }
1075
1076
1077 static void
1078 PLy_function_delete_args(PLyProcedure * proc)
1079 {
1080         int                     i;
1081
1082         if (!proc->argnames)
1083                 return;
1084
1085         for (i = 0; i < proc->nargs; i++)
1086                 if (proc->argnames[i])
1087                         PyDict_DelItemString(proc->globals, proc->argnames[i]);
1088 }
1089
1090
1091 /*
1092  * PLyProcedure functions
1093  */
1094
1095 /* PLy_procedure_get: returns a cached PLyProcedure, or creates, stores and
1096  * returns a new PLyProcedure.  fcinfo is the call info, tgreloid is the
1097  * relation OID when calling a trigger, or InvalidOid (zero) for ordinary
1098  * function calls.
1099  */
1100 static PLyProcedure *
1101 PLy_procedure_get(FunctionCallInfo fcinfo, Oid tgreloid)
1102 {
1103         Oid                     fn_oid;
1104         HeapTuple       procTup;
1105         char            key[128];
1106         PyObject   *plproc;
1107         PLyProcedure *proc = NULL;
1108         int                     rv;
1109
1110         fn_oid = fcinfo->flinfo->fn_oid;
1111         procTup = SearchSysCache(PROCOID,
1112                                                          ObjectIdGetDatum(fn_oid),
1113                                                          0, 0, 0);
1114         if (!HeapTupleIsValid(procTup))
1115                 elog(ERROR, "cache lookup failed for function %u", fn_oid);
1116
1117         rv = snprintf(key, sizeof(key), "%u_%u", fn_oid, tgreloid);
1118         if (rv >= sizeof(key) || rv < 0)
1119                 elog(ERROR, "key too long");
1120
1121         plproc = PyDict_GetItemString(PLy_procedure_cache, key);
1122
1123         if (plproc != NULL)
1124         {
1125                 Py_INCREF(plproc);
1126                 if (!PyCObject_Check(plproc))
1127                         elog(FATAL, "expected a PyCObject, didn't get one");
1128
1129                 proc = PyCObject_AsVoidPtr(plproc);
1130                 if (proc->me != plproc)
1131                         elog(FATAL, "proc->me != plproc");
1132                 /* did we find an up-to-date cache entry? */
1133                 if (proc->fn_xmin != HeapTupleHeaderGetXmin(procTup->t_data) ||
1134                         !ItemPointerEquals(&proc->fn_tid, &procTup->t_self))
1135                 {
1136                         Py_DECREF(plproc);
1137                         proc = NULL;
1138                 }
1139         }
1140
1141         if (proc == NULL)
1142                 proc = PLy_procedure_create(procTup, tgreloid, key);
1143
1144         if (OidIsValid(tgreloid))
1145         {
1146                 /*
1147                  * Input/output conversion for trigger tuples.  Use the result
1148                  * TypeInfo variable to store the tuple conversion info.  We
1149                  * do this over again on each call to cover the possibility that
1150                  * the relation's tupdesc changed since the trigger was last called.
1151                  * PLy_input_tuple_funcs and PLy_output_tuple_funcs are responsible
1152                  * for not doing repetitive work.
1153                  */
1154                 TriggerData *tdata = (TriggerData *) fcinfo->context;
1155
1156                 Assert(CALLED_AS_TRIGGER(fcinfo));
1157                 PLy_input_tuple_funcs(&(proc->result), tdata->tg_relation->rd_att);
1158                 PLy_output_tuple_funcs(&(proc->result), tdata->tg_relation->rd_att);
1159         }
1160
1161         ReleaseSysCache(procTup);
1162
1163         return proc;
1164 }
1165
1166 static PLyProcedure *
1167 PLy_procedure_create(HeapTuple procTup, Oid tgreloid, char *key)
1168 {
1169         char            procName[NAMEDATALEN + 256];
1170         Form_pg_proc procStruct;
1171         PLyProcedure *volatile proc;
1172         char       *volatile procSource = NULL;
1173         Datum           prosrcdatum;
1174         bool            isnull;
1175         int                     i,
1176                                 rv;
1177
1178         procStruct = (Form_pg_proc) GETSTRUCT(procTup);
1179
1180         if (OidIsValid(tgreloid))
1181                 rv = snprintf(procName, sizeof(procName),
1182                                           "__plpython_procedure_%s_%u_trigger_%u",
1183                                           NameStr(procStruct->proname),
1184                                           HeapTupleGetOid(procTup),
1185                                           tgreloid);
1186         else
1187                 rv = snprintf(procName, sizeof(procName),
1188                                           "__plpython_procedure_%s_%u",
1189                                           NameStr(procStruct->proname),
1190                                           HeapTupleGetOid(procTup));
1191         if (rv >= sizeof(procName) || rv < 0)
1192                 elog(ERROR, "procedure name would overrun buffer");
1193
1194         proc = PLy_malloc(sizeof(PLyProcedure));
1195         proc->proname = PLy_strdup(NameStr(procStruct->proname));
1196         proc->pyname = PLy_strdup(procName);
1197         proc->fn_xmin = HeapTupleHeaderGetXmin(procTup->t_data);
1198         proc->fn_tid = procTup->t_self;
1199         /* Remember if function is STABLE/IMMUTABLE */
1200         proc->fn_readonly =
1201                 (procStruct->provolatile != PROVOLATILE_VOLATILE);
1202         PLy_typeinfo_init(&proc->result);
1203         for (i = 0; i < FUNC_MAX_ARGS; i++)
1204                 PLy_typeinfo_init(&proc->args[i]);
1205         proc->nargs = 0;
1206         proc->code = proc->statics = NULL;
1207         proc->globals = proc->me = NULL;
1208         proc->is_setof = procStruct->proretset;
1209         proc->setof = NULL;
1210         proc->argnames = NULL;
1211
1212         PG_TRY();
1213         {
1214                 /*
1215                  * get information required for output conversion of the return value,
1216                  * but only if this isn't a trigger.
1217                  */
1218                 if (!OidIsValid(tgreloid))
1219                 {
1220                         HeapTuple       rvTypeTup;
1221                         Form_pg_type rvTypeStruct;
1222
1223                         rvTypeTup = SearchSysCache(TYPEOID,
1224                                                                         ObjectIdGetDatum(procStruct->prorettype),
1225                                                                            0, 0, 0);
1226                         if (!HeapTupleIsValid(rvTypeTup))
1227                                 elog(ERROR, "cache lookup failed for type %u",
1228                                          procStruct->prorettype);
1229                         rvTypeStruct = (Form_pg_type) GETSTRUCT(rvTypeTup);
1230
1231                         /* Disallow pseudotype result, except for void */
1232                         if (rvTypeStruct->typtype == TYPTYPE_PSEUDO &&
1233                                 procStruct->prorettype != VOIDOID)
1234                         {
1235                                 if (procStruct->prorettype == TRIGGEROID)
1236                                         ereport(ERROR,
1237                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1238                                                          errmsg("trigger functions can only be called as triggers")));
1239                                 else
1240                                         ereport(ERROR,
1241                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1242                                                    errmsg("PL/Python functions cannot return type %s",
1243                                                                   format_type_be(procStruct->prorettype))));
1244                         }
1245
1246                         if (rvTypeStruct->typtype == TYPTYPE_COMPOSITE)
1247                         {
1248                                 /*
1249                                  * Tuple: set up later, during first call to
1250                                  * PLy_function_handler
1251                                  */
1252                                 proc->result.out.d.typoid = procStruct->prorettype;
1253                                 proc->result.is_rowtype = 2;
1254                         }
1255                         else
1256                                 PLy_output_datum_func(&proc->result, rvTypeTup);
1257
1258                         ReleaseSysCache(rvTypeTup);
1259                 }
1260
1261                 /*
1262                  * Now get information required for input conversion of the
1263                  * procedure's arguments.  Note that we ignore output arguments
1264                  * here --- since we don't support returning record, and that was
1265                  * already checked above, there's no need to worry about multiple
1266                  * output arguments.
1267                  */
1268                 if (procStruct->pronargs)
1269                 {
1270                         Oid             *types;
1271                         char   **names,
1272                                         *modes;
1273                         int              i,
1274                                          pos,
1275                                          total;
1276
1277                         /* extract argument type info from the pg_proc tuple */
1278                         total = get_func_arg_info(procTup, &types, &names, &modes);
1279
1280                         /* count number of in+inout args into proc->nargs */
1281                         if (modes == NULL)
1282                                 proc->nargs = total;
1283                         else
1284                         {
1285                                 /* proc->nargs was initialized to 0 above */
1286                                 for (i = 0; i < total; i++)
1287                                 {
1288                                         if (modes[i] != PROARGMODE_OUT &&
1289                                                 modes[i] != PROARGMODE_TABLE)
1290                                                 (proc->nargs)++;
1291                                 }
1292                         }
1293
1294                         proc->argnames = (char **) PLy_malloc0(sizeof(char *) * proc->nargs);
1295                         for (i = pos = 0; i < total; i++)
1296                         {
1297                                 HeapTuple       argTypeTup;
1298                                 Form_pg_type argTypeStruct;
1299
1300                                 if (modes &&
1301                                         (modes[i] == PROARGMODE_OUT ||
1302                                          modes[i] == PROARGMODE_TABLE))
1303                                         continue;       /* skip OUT arguments */
1304
1305                                 Assert(types[i] == procStruct->proargtypes.values[pos]);
1306
1307                                 argTypeTup = SearchSysCache(TYPEOID,
1308                                                                                         ObjectIdGetDatum(types[i]),
1309                                                                                         0, 0, 0);
1310                                 if (!HeapTupleIsValid(argTypeTup))
1311                                         elog(ERROR, "cache lookup failed for type %u", types[i]);
1312                                 argTypeStruct = (Form_pg_type) GETSTRUCT(argTypeTup);
1313
1314                                 /* check argument type is OK, set up I/O function info */
1315                                 switch (argTypeStruct->typtype)
1316                                 {
1317                                         case TYPTYPE_PSEUDO:
1318                                                 /* Disallow pseudotype argument */
1319                                                 ereport(ERROR,
1320                                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1321                                                                  errmsg("PL/Python functions cannot accept type %s",
1322                                                                  format_type_be(types[i]))));
1323                                                 break;
1324                                         case TYPTYPE_COMPOSITE:
1325                                                 /* we'll set IO funcs at first call */
1326                                                 proc->args[pos].is_rowtype = 2;
1327                                                 break;
1328                                         default:
1329                                                 PLy_input_datum_func(&(proc->args[pos]),
1330                                                                                          types[i],
1331                                                                                          argTypeTup);
1332                                                 break;
1333                                 }
1334
1335                                 /* get argument name */
1336                                 proc->argnames[pos] = names ? PLy_strdup(names[i]) : NULL;
1337
1338                                 ReleaseSysCache(argTypeTup);
1339
1340                                 pos++;
1341                         }
1342                 }
1343
1344                 /*
1345                  * get the text of the function.
1346                  */
1347                 prosrcdatum = SysCacheGetAttr(PROCOID, procTup,
1348                                                                           Anum_pg_proc_prosrc, &isnull);
1349                 if (isnull)
1350                         elog(ERROR, "null prosrc");
1351                 procSource = TextDatumGetCString(prosrcdatum);
1352
1353                 PLy_procedure_compile(proc, procSource);
1354
1355                 pfree(procSource);
1356
1357                 proc->me = PyCObject_FromVoidPtr(proc, NULL);
1358                 PyDict_SetItemString(PLy_procedure_cache, key, proc->me);
1359         }
1360         PG_CATCH();
1361         {
1362                 PLy_procedure_delete(proc);
1363                 if (procSource)
1364                         pfree(procSource);
1365
1366                 PG_RE_THROW();
1367         }
1368         PG_END_TRY();
1369
1370         return proc;
1371 }
1372
1373 static void
1374 PLy_procedure_compile(PLyProcedure * proc, const char *src)
1375 {
1376         PyObject   *crv = NULL;
1377         char       *msrc;
1378
1379         proc->globals = PyDict_Copy(PLy_interp_globals);
1380
1381         /*
1382          * SD is private preserved data between calls. GD is global data shared by
1383          * all functions
1384          */
1385         proc->statics = PyDict_New();
1386         PyDict_SetItemString(proc->globals, "SD", proc->statics);
1387
1388         /*
1389          * insert the function code into the interpreter
1390          */
1391         msrc = PLy_procedure_munge_source(proc->pyname, src);
1392         crv = PyRun_String(msrc, Py_file_input, proc->globals, NULL);
1393         free(msrc);
1394
1395         if (crv != NULL && (!PyErr_Occurred()))
1396         {
1397                 int                     clen;
1398                 char            call[NAMEDATALEN + 256];
1399
1400                 Py_DECREF(crv);
1401
1402                 /*
1403                  * compile a call to the function
1404                  */
1405                 clen = snprintf(call, sizeof(call), "%s()", proc->pyname);
1406                 if (clen < 0 || clen >= sizeof(call))
1407                         elog(ERROR, "string would overflow buffer");
1408                 proc->code = Py_CompileString(call, "<string>", Py_eval_input);
1409                 if (proc->code != NULL && (!PyErr_Occurred()))
1410                         return;
1411         }
1412         else
1413                 Py_XDECREF(crv);
1414
1415         PLy_elog(ERROR, "could not compile PL/Python function \"%s\"", proc->proname);
1416 }
1417
1418 static char *
1419 PLy_procedure_munge_source(const char *name, const char *src)
1420 {
1421         char       *mrc,
1422                            *mp;
1423         const char *sp;
1424         size_t          mlen,
1425                                 plen;
1426
1427         /*
1428          * room for function source and the def statement
1429          */
1430         mlen = (strlen(src) * 2) + strlen(name) + 16;
1431
1432         mrc = PLy_malloc(mlen);
1433         plen = snprintf(mrc, mlen, "def %s():\n\t", name);
1434         Assert(plen >= 0 && plen < mlen);
1435
1436         sp = src;
1437         mp = mrc + plen;
1438
1439         while (*sp != '\0')
1440         {
1441                 if (*sp == '\r' && *(sp + 1) == '\n')
1442                         sp++;
1443
1444                 if (*sp == '\n' || *sp == '\r')
1445                 {
1446                         *mp++ = '\n';
1447                         *mp++ = '\t';
1448                         sp++;
1449                 }
1450                 else
1451                         *mp++ = *sp++;
1452         }
1453         *mp++ = '\n';
1454         *mp++ = '\n';
1455         *mp = '\0';
1456
1457         if (mp > (mrc + mlen))
1458                 elog(FATAL, "buffer overrun in PLy_munge_source");
1459
1460         return mrc;
1461 }
1462
1463 static void
1464 PLy_procedure_delete(PLyProcedure * proc)
1465 {
1466         int                     i;
1467
1468         Py_XDECREF(proc->code);
1469         Py_XDECREF(proc->statics);
1470         Py_XDECREF(proc->globals);
1471         Py_XDECREF(proc->me);
1472         if (proc->proname)
1473                 PLy_free(proc->proname);
1474         if (proc->pyname)
1475                 PLy_free(proc->pyname);
1476         for (i = 0; i < proc->nargs; i++)
1477         {
1478                 if (proc->args[i].is_rowtype == 1)
1479                 {
1480                         if (proc->args[i].in.r.atts)
1481                                 PLy_free(proc->args[i].in.r.atts);
1482                         if (proc->args[i].out.r.atts)
1483                                 PLy_free(proc->args[i].out.r.atts);
1484                 }
1485                 if (proc->argnames && proc->argnames[i])
1486                         PLy_free(proc->argnames[i]);
1487         }
1488         if (proc->argnames)
1489                 PLy_free(proc->argnames);
1490 }
1491
1492 /*
1493  * Conversion functions.  Remember output from Python is input to
1494  * PostgreSQL, and vice versa.
1495  */
1496 static void
1497 PLy_input_tuple_funcs(PLyTypeInfo * arg, TupleDesc desc)
1498 {
1499         int                     i;
1500
1501         if (arg->is_rowtype == 0)
1502                 elog(ERROR, "PLyTypeInfo struct is initialized for a Datum");
1503         arg->is_rowtype = 1;
1504
1505         if (arg->in.r.natts != desc->natts)
1506         {
1507                 if (arg->in.r.atts)
1508                         PLy_free(arg->in.r.atts);
1509                 arg->in.r.natts = desc->natts;
1510                 arg->in.r.atts = PLy_malloc0(desc->natts * sizeof(PLyDatumToOb));
1511         }
1512
1513         for (i = 0; i < desc->natts; i++)
1514         {
1515                 HeapTuple       typeTup;
1516
1517                 if (desc->attrs[i]->attisdropped)
1518                         continue;
1519
1520                 if (arg->in.r.atts[i].typoid == desc->attrs[i]->atttypid)
1521                         continue;                       /* already set up this entry */
1522
1523                 typeTup = SearchSysCache(TYPEOID,
1524                                                                  ObjectIdGetDatum(desc->attrs[i]->atttypid),
1525                                                                  0, 0, 0);
1526                 if (!HeapTupleIsValid(typeTup))
1527                         elog(ERROR, "cache lookup failed for type %u",
1528                                  desc->attrs[i]->atttypid);
1529
1530                 PLy_input_datum_func2(&(arg->in.r.atts[i]),
1531                                                           desc->attrs[i]->atttypid,
1532                                                           typeTup);
1533
1534                 ReleaseSysCache(typeTup);
1535         }
1536 }
1537
1538 static void
1539 PLy_output_tuple_funcs(PLyTypeInfo * arg, TupleDesc desc)
1540 {
1541         int                     i;
1542
1543         if (arg->is_rowtype == 0)
1544                 elog(ERROR, "PLyTypeInfo struct is initialized for a Datum");
1545         arg->is_rowtype = 1;
1546
1547         if (arg->out.r.natts != desc->natts)
1548         {
1549                 if (arg->out.r.atts)
1550                         PLy_free(arg->out.r.atts);
1551                 arg->out.r.natts = desc->natts;
1552                 arg->out.r.atts = PLy_malloc0(desc->natts * sizeof(PLyDatumToOb));
1553         }
1554
1555         for (i = 0; i < desc->natts; i++)
1556         {
1557                 HeapTuple       typeTup;
1558
1559                 if (desc->attrs[i]->attisdropped)
1560                         continue;
1561
1562                 if (arg->out.r.atts[i].typoid == desc->attrs[i]->atttypid)
1563                         continue;                       /* already set up this entry */
1564
1565                 typeTup = SearchSysCache(TYPEOID,
1566                                                                  ObjectIdGetDatum(desc->attrs[i]->atttypid),
1567                                                                  0, 0, 0);
1568                 if (!HeapTupleIsValid(typeTup))
1569                         elog(ERROR, "cache lookup failed for type %u",
1570                                  desc->attrs[i]->atttypid);
1571
1572                 PLy_output_datum_func2(&(arg->out.r.atts[i]), typeTup);
1573
1574                 ReleaseSysCache(typeTup);
1575         }
1576 }
1577
1578 static void
1579 PLy_output_datum_func(PLyTypeInfo * arg, HeapTuple typeTup)
1580 {
1581         if (arg->is_rowtype > 0)
1582                 elog(ERROR, "PLyTypeInfo struct is initialized for a Tuple");
1583         arg->is_rowtype = 0;
1584         PLy_output_datum_func2(&(arg->out.d), typeTup);
1585 }
1586
1587 static void
1588 PLy_output_datum_func2(PLyObToDatum * arg, HeapTuple typeTup)
1589 {
1590         Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1591
1592         perm_fmgr_info(typeStruct->typinput, &arg->typfunc);
1593         arg->typoid = HeapTupleGetOid(typeTup);
1594         arg->typioparam = getTypeIOParam(typeTup);
1595         arg->typbyval = typeStruct->typbyval;
1596 }
1597
1598 static void
1599 PLy_input_datum_func(PLyTypeInfo * arg, Oid typeOid, HeapTuple typeTup)
1600 {
1601         if (arg->is_rowtype > 0)
1602                 elog(ERROR, "PLyTypeInfo struct is initialized for Tuple");
1603         arg->is_rowtype = 0;
1604         PLy_input_datum_func2(&(arg->in.d), typeOid, typeTup);
1605 }
1606
1607 static void
1608 PLy_input_datum_func2(PLyDatumToOb * arg, Oid typeOid, HeapTuple typeTup)
1609 {
1610         Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1611
1612         /* Get the type's conversion information */
1613         perm_fmgr_info(typeStruct->typoutput, &arg->typfunc);
1614         arg->typoid = HeapTupleGetOid(typeTup);
1615         arg->typioparam = getTypeIOParam(typeTup);
1616         arg->typbyval = typeStruct->typbyval;
1617
1618         /* Determine which kind of Python object we will convert to */
1619         switch (typeOid)
1620         {
1621                 case BOOLOID:
1622                         arg->func = PLyBool_FromString;
1623                         break;
1624                 case FLOAT4OID:
1625                 case FLOAT8OID:
1626                 case NUMERICOID:
1627                         arg->func = PLyFloat_FromString;
1628                         break;
1629                 case INT2OID:
1630                 case INT4OID:
1631                         arg->func = PLyInt_FromString;
1632                         break;
1633                 case INT8OID:
1634                         arg->func = PLyLong_FromString;
1635                         break;
1636                 default:
1637                         arg->func = PLyString_FromString;
1638                         break;
1639         }
1640 }
1641
1642 static void
1643 PLy_typeinfo_init(PLyTypeInfo * arg)
1644 {
1645         arg->is_rowtype = -1;
1646         arg->in.r.natts = arg->out.r.natts = 0;
1647         arg->in.r.atts = NULL;
1648         arg->out.r.atts = NULL;
1649 }
1650
1651 static void
1652 PLy_typeinfo_dealloc(PLyTypeInfo * arg)
1653 {
1654         if (arg->is_rowtype == 1)
1655         {
1656                 if (arg->in.r.atts)
1657                         PLy_free(arg->in.r.atts);
1658                 if (arg->out.r.atts)
1659                         PLy_free(arg->out.r.atts);
1660         }
1661 }
1662
1663 /* assumes that a bool is always returned as a 't' or 'f' */
1664 static PyObject *
1665 PLyBool_FromString(const char *src)
1666 {
1667         /*
1668          * We would like to use Py_RETURN_TRUE and Py_RETURN_FALSE here for
1669          * generating SQL from trigger functions, but those are only supported in
1670          * Python >= 2.3, and we support older versions.
1671          * http://docs.python.org/api/boolObjects.html
1672          */
1673         if (src[0] == 't')
1674                 return PyBool_FromLong(1);
1675         return PyBool_FromLong(0);
1676 }
1677
1678 static PyObject *
1679 PLyFloat_FromString(const char *src)
1680 {
1681         double          v;
1682         char       *eptr;
1683
1684         errno = 0;
1685         v = strtod(src, &eptr);
1686         if (*eptr != '\0' || errno)
1687                 return NULL;
1688         return PyFloat_FromDouble(v);
1689 }
1690
1691 static PyObject *
1692 PLyInt_FromString(const char *src)
1693 {
1694         long            v;
1695         char       *eptr;
1696
1697         errno = 0;
1698         v = strtol(src, &eptr, 0);
1699         if (*eptr != '\0' || errno)
1700                 return NULL;
1701         return PyInt_FromLong(v);
1702 }
1703
1704 static PyObject *
1705 PLyLong_FromString(const char *src)
1706 {
1707         return PyLong_FromString((char *) src, NULL, 0);
1708 }
1709
1710 static PyObject *
1711 PLyString_FromString(const char *src)
1712 {
1713         return PyString_FromString(src);
1714 }
1715
1716 static PyObject *
1717 PLyDict_FromTuple(PLyTypeInfo * info, HeapTuple tuple, TupleDesc desc)
1718 {
1719         PyObject   *volatile dict;
1720         int                     i;
1721
1722         if (info->is_rowtype != 1)
1723                 elog(ERROR, "PLyTypeInfo structure describes a datum");
1724
1725         dict = PyDict_New();
1726         if (dict == NULL)
1727                 PLy_elog(ERROR, "could not create new dictionary");
1728
1729         PG_TRY();
1730         {
1731                 for (i = 0; i < info->in.r.natts; i++)
1732                 {
1733                         char       *key,
1734                                            *vsrc;
1735                         Datum           vattr;
1736                         bool            is_null;
1737                         PyObject   *value;
1738
1739                         if (desc->attrs[i]->attisdropped)
1740                                 continue;
1741
1742                         key = NameStr(desc->attrs[i]->attname);
1743                         vattr = heap_getattr(tuple, (i + 1), desc, &is_null);
1744
1745                         if (is_null || info->in.r.atts[i].func == NULL)
1746                                 PyDict_SetItemString(dict, key, Py_None);
1747                         else
1748                         {
1749                                 vsrc = OutputFunctionCall(&info->in.r.atts[i].typfunc,
1750                                                                                   vattr);
1751
1752                                 /*
1753                                  * no exceptions allowed
1754                                  */
1755                                 value = info->in.r.atts[i].func(vsrc);
1756                                 pfree(vsrc);
1757                                 PyDict_SetItemString(dict, key, value);
1758                                 Py_DECREF(value);
1759                         }
1760                 }
1761         }
1762         PG_CATCH();
1763         {
1764                 Py_DECREF(dict);
1765                 PG_RE_THROW();
1766         }
1767         PG_END_TRY();
1768
1769         return dict;
1770 }
1771
1772
1773 static HeapTuple
1774 PLyMapping_ToTuple(PLyTypeInfo * info, PyObject * mapping)
1775 {
1776         TupleDesc       desc;
1777         HeapTuple       tuple;
1778         Datum      *values;
1779         bool       *nulls;
1780         volatile int i;
1781
1782         Assert(PyMapping_Check(mapping));
1783
1784         desc = lookup_rowtype_tupdesc(info->out.d.typoid, -1);
1785         if (info->is_rowtype == 2)
1786                 PLy_output_tuple_funcs(info, desc);
1787         Assert(info->is_rowtype == 1);
1788
1789         /* Build tuple */
1790         values = palloc(sizeof(Datum) * desc->natts);
1791         nulls = palloc(sizeof(bool) * desc->natts);
1792         for (i = 0; i < desc->natts; ++i)
1793         {
1794                 char       *key;
1795                 PyObject   *volatile value,
1796                                    *volatile so;
1797
1798                 key = NameStr(desc->attrs[i]->attname);
1799                 value = so = NULL;
1800                 PG_TRY();
1801                 {
1802                         value = PyMapping_GetItemString(mapping, key);
1803                         if (value == Py_None)
1804                         {
1805                                 values[i] = (Datum) NULL;
1806                                 nulls[i] = true;
1807                         }
1808                         else if (value)
1809                         {
1810                                 char       *valuestr;
1811
1812                                 so = PyObject_Str(value);
1813                                 if (so == NULL)
1814                                         PLy_elog(ERROR, "could not compute string representation of Python object");
1815                                 valuestr = PyString_AsString(so);
1816
1817                                 values[i] = InputFunctionCall(&info->out.r.atts[i].typfunc
1818                                                                                           ,valuestr
1819                                                                                           ,info->out.r.atts[i].typioparam
1820                                                                                           ,-1);
1821                                 Py_DECREF(so);
1822                                 so = NULL;
1823                                 nulls[i] = false;
1824                         }
1825                         else
1826                                 ereport(ERROR,
1827                                                 (errcode(ERRCODE_UNDEFINED_COLUMN),
1828                                                  errmsg("key \"%s\" not found in mapping", key),
1829                                                  errhint("To return null in a column, "
1830                                           "add the value None to the mapping with the key named after the column.")));
1831
1832                         Py_XDECREF(value);
1833                         value = NULL;
1834                 }
1835                 PG_CATCH();
1836                 {
1837                         Py_XDECREF(so);
1838                         Py_XDECREF(value);
1839                         PG_RE_THROW();
1840                 }
1841                 PG_END_TRY();
1842         }
1843
1844         tuple = heap_form_tuple(desc, values, nulls);
1845         ReleaseTupleDesc(desc);
1846         pfree(values);
1847         pfree(nulls);
1848
1849         return tuple;
1850 }
1851
1852
1853 static HeapTuple
1854 PLySequence_ToTuple(PLyTypeInfo * info, PyObject * sequence)
1855 {
1856         TupleDesc       desc;
1857         HeapTuple       tuple;
1858         Datum      *values;
1859         bool       *nulls;
1860         volatile int i;
1861
1862         Assert(PySequence_Check(sequence));
1863
1864         /*
1865          * Check that sequence length is exactly same as PG tuple's. We actually
1866          * can ignore exceeding items or assume missing ones as null but to avoid
1867          * plpython developer's errors we are strict here
1868          */
1869         desc = lookup_rowtype_tupdesc(info->out.d.typoid, -1);
1870         if (PySequence_Length(sequence) != desc->natts)
1871                 ereport(ERROR,
1872                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
1873                 errmsg("length of returned sequence did not match number of columns in row")));
1874
1875         if (info->is_rowtype == 2)
1876                 PLy_output_tuple_funcs(info, desc);
1877         Assert(info->is_rowtype == 1);
1878
1879         /* Build tuple */
1880         values = palloc(sizeof(Datum) * desc->natts);
1881         nulls = palloc(sizeof(bool) * desc->natts);
1882         for (i = 0; i < desc->natts; ++i)
1883         {
1884                 PyObject   *volatile value,
1885                                    *volatile so;
1886
1887                 value = so = NULL;
1888                 PG_TRY();
1889                 {
1890                         value = PySequence_GetItem(sequence, i);
1891                         Assert(value);
1892                         if (value == Py_None)
1893                         {
1894                                 values[i] = (Datum) NULL;
1895                                 nulls[i] = true;
1896                         }
1897                         else if (value)
1898                         {
1899                                 char       *valuestr;
1900
1901                                 so = PyObject_Str(value);
1902                                 if (so == NULL)
1903                                         PLy_elog(ERROR, "could not compute string representation of Python object");
1904                                 valuestr = PyString_AsString(so);
1905                                 values[i] = InputFunctionCall(&info->out.r.atts[i].typfunc
1906                                                                                           ,valuestr
1907                                                                                           ,info->out.r.atts[i].typioparam
1908                                                                                           ,-1);
1909                                 Py_DECREF(so);
1910                                 so = NULL;
1911                                 nulls[i] = false;
1912                         }
1913
1914                         Py_XDECREF(value);
1915                         value = NULL;
1916                 }
1917                 PG_CATCH();
1918                 {
1919                         Py_XDECREF(so);
1920                         Py_XDECREF(value);
1921                         PG_RE_THROW();
1922                 }
1923                 PG_END_TRY();
1924         }
1925
1926         tuple = heap_form_tuple(desc, values, nulls);
1927         ReleaseTupleDesc(desc);
1928         pfree(values);
1929         pfree(nulls);
1930
1931         return tuple;
1932 }
1933
1934
1935 static HeapTuple
1936 PLyObject_ToTuple(PLyTypeInfo * info, PyObject * object)
1937 {
1938         TupleDesc       desc;
1939         HeapTuple       tuple;
1940         Datum      *values;
1941         bool       *nulls;
1942         volatile int i;
1943
1944         desc = lookup_rowtype_tupdesc(info->out.d.typoid, -1);
1945         if (info->is_rowtype == 2)
1946                 PLy_output_tuple_funcs(info, desc);
1947         Assert(info->is_rowtype == 1);
1948
1949         /* Build tuple */
1950         values = palloc(sizeof(Datum) * desc->natts);
1951         nulls = palloc(sizeof(bool) * desc->natts);
1952         for (i = 0; i < desc->natts; ++i)
1953         {
1954                 char       *key;
1955                 PyObject   *volatile value,
1956                                    *volatile so;
1957
1958                 key = NameStr(desc->attrs[i]->attname);
1959                 value = so = NULL;
1960                 PG_TRY();
1961                 {
1962                         value = PyObject_GetAttrString(object, key);
1963                         if (value == Py_None)
1964                         {
1965                                 values[i] = (Datum) NULL;
1966                                 nulls[i] = true;
1967                         }
1968                         else if (value)
1969                         {
1970                                 char       *valuestr;
1971
1972                                 so = PyObject_Str(value);
1973                                 if (so == NULL)
1974                                         PLy_elog(ERROR, "could not compute string representation of Python object");
1975                                 valuestr = PyString_AsString(so);
1976                                 values[i] = InputFunctionCall(&info->out.r.atts[i].typfunc
1977                                                                                           ,valuestr
1978                                                                                           ,info->out.r.atts[i].typioparam
1979                                                                                           ,-1);
1980                                 Py_DECREF(so);
1981                                 so = NULL;
1982                                 nulls[i] = false;
1983                         }
1984                         else
1985                                 ereport(ERROR,
1986                                                 (errcode(ERRCODE_UNDEFINED_COLUMN),
1987                                                  errmsg("attribute \"%s\" does not exist in Python object", key),
1988                                                  errhint("To return null in a column, "
1989                                                                  "let the returned object have an attribute named "
1990                                                                  "after column with value None.")));
1991
1992                         Py_XDECREF(value);
1993                         value = NULL;
1994                 }
1995                 PG_CATCH();
1996                 {
1997                         Py_XDECREF(so);
1998                         Py_XDECREF(value);
1999                         PG_RE_THROW();
2000                 }
2001                 PG_END_TRY();
2002         }
2003
2004         tuple = heap_form_tuple(desc, values, nulls);
2005         ReleaseTupleDesc(desc);
2006         pfree(values);
2007         pfree(nulls);
2008
2009         return tuple;
2010 }
2011
2012
2013 /* initialization, some python variables function declared here */
2014
2015 /* interface to postgresql elog */
2016 static PyObject *PLy_debug(PyObject *, PyObject *);
2017 static PyObject *PLy_log(PyObject *, PyObject *);
2018 static PyObject *PLy_info(PyObject *, PyObject *);
2019 static PyObject *PLy_notice(PyObject *, PyObject *);
2020 static PyObject *PLy_warning(PyObject *, PyObject *);
2021 static PyObject *PLy_error(PyObject *, PyObject *);
2022 static PyObject *PLy_fatal(PyObject *, PyObject *);
2023
2024 /* PLyPlanObject, PLyResultObject and SPI interface */
2025 #define is_PLyPlanObject(x) ((x)->ob_type == &PLy_PlanType)
2026 static PyObject *PLy_plan_new(void);
2027 static void PLy_plan_dealloc(PyObject *);
2028 static PyObject *PLy_plan_getattr(PyObject *, char *);
2029 static PyObject *PLy_plan_status(PyObject *, PyObject *);
2030
2031 static PyObject *PLy_result_new(void);
2032 static void PLy_result_dealloc(PyObject *);
2033 static PyObject *PLy_result_getattr(PyObject *, char *);
2034 static PyObject *PLy_result_nrows(PyObject *, PyObject *);
2035 static PyObject *PLy_result_status(PyObject *, PyObject *);
2036 static Py_ssize_t PLy_result_length(PyObject *);
2037 static PyObject *PLy_result_item(PyObject *, Py_ssize_t);
2038 static PyObject *PLy_result_slice(PyObject *, Py_ssize_t, Py_ssize_t);
2039 static int      PLy_result_ass_item(PyObject *, Py_ssize_t, PyObject *);
2040 static int      PLy_result_ass_slice(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
2041
2042
2043 static PyObject *PLy_spi_prepare(PyObject *, PyObject *);
2044 static PyObject *PLy_spi_execute(PyObject *, PyObject *);
2045 static PyObject *PLy_spi_execute_query(char *query, long limit);
2046 static PyObject *PLy_spi_execute_plan(PyObject *, PyObject *, long);
2047 static PyObject *PLy_spi_execute_fetch_result(SPITupleTable *, int, int);
2048
2049
2050 static PyTypeObject PLy_PlanType = {
2051         PyObject_HEAD_INIT(NULL)
2052         0,                                                      /* ob_size */
2053         "PLyPlan",                                      /* tp_name */
2054         sizeof(PLyPlanObject),          /* tp_size */
2055         0,                                                      /* tp_itemsize */
2056
2057         /*
2058          * methods
2059          */
2060         PLy_plan_dealloc,                       /* tp_dealloc */
2061         0,                                                      /* tp_print */
2062         PLy_plan_getattr,                       /* tp_getattr */
2063         0,                                                      /* tp_setattr */
2064         0,                                                      /* tp_compare */
2065         0,                                                      /* tp_repr */
2066         0,                                                      /* tp_as_number */
2067         0,                                                      /* tp_as_sequence */
2068         0,                                                      /* tp_as_mapping */
2069         0,                                                      /* tp_hash */
2070         0,                                                      /* tp_call */
2071         0,                                                      /* tp_str */
2072         0,                                                      /* tp_getattro */
2073         0,                                                      /* tp_setattro */
2074         0,                                                      /* tp_as_buffer */
2075         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,       /* tp_flags */
2076         PLy_plan_doc,                           /* tp_doc */
2077 };
2078
2079 static PyMethodDef PLy_plan_methods[] = {
2080         {"status", PLy_plan_status, METH_VARARGS, NULL},
2081         {NULL, NULL, 0, NULL}
2082 };
2083
2084 static PySequenceMethods PLy_result_as_sequence = {
2085         PLy_result_length,                      /* sq_length */
2086         NULL,                                           /* sq_concat */
2087         NULL,                                           /* sq_repeat */
2088         PLy_result_item,                        /* sq_item */
2089         PLy_result_slice,                       /* sq_slice */
2090         PLy_result_ass_item,            /* sq_ass_item */
2091         PLy_result_ass_slice,           /* sq_ass_slice */
2092 };
2093
2094 static PyTypeObject PLy_ResultType = {
2095         PyObject_HEAD_INIT(NULL)
2096         0,                                                      /* ob_size */
2097         "PLyResult",                            /* tp_name */
2098         sizeof(PLyResultObject),        /* tp_size */
2099         0,                                                      /* tp_itemsize */
2100
2101         /*
2102          * methods
2103          */
2104         PLy_result_dealloc,                     /* tp_dealloc */
2105         0,                                                      /* tp_print */
2106         PLy_result_getattr,                     /* tp_getattr */
2107         0,                                                      /* tp_setattr */
2108         0,                                                      /* tp_compare */
2109         0,                                                      /* tp_repr */
2110         0,                                                      /* tp_as_number */
2111         &PLy_result_as_sequence,        /* tp_as_sequence */
2112         0,                                                      /* tp_as_mapping */
2113         0,                                                      /* tp_hash */
2114         0,                                                      /* tp_call */
2115         0,                                                      /* tp_str */
2116         0,                                                      /* tp_getattro */
2117         0,                                                      /* tp_setattro */
2118         0,                                                      /* tp_as_buffer */
2119         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,       /* tp_flags */
2120         PLy_result_doc,                         /* tp_doc */
2121 };
2122
2123 static PyMethodDef PLy_result_methods[] = {
2124         {"nrows", PLy_result_nrows, METH_VARARGS, NULL},
2125         {"status", PLy_result_status, METH_VARARGS, NULL},
2126         {NULL, NULL, 0, NULL}
2127 };
2128
2129 static PyMethodDef PLy_methods[] = {
2130         /*
2131          * logging methods
2132          */
2133         {"debug", PLy_debug, METH_VARARGS, NULL},
2134         {"log", PLy_log, METH_VARARGS, NULL},
2135         {"info", PLy_info, METH_VARARGS, NULL},
2136         {"notice", PLy_notice, METH_VARARGS, NULL},
2137         {"warning", PLy_warning, METH_VARARGS, NULL},
2138         {"error", PLy_error, METH_VARARGS, NULL},
2139         {"fatal", PLy_fatal, METH_VARARGS, NULL},
2140
2141         /*
2142          * create a stored plan
2143          */
2144         {"prepare", PLy_spi_prepare, METH_VARARGS, NULL},
2145
2146         /*
2147          * execute a plan or query
2148          */
2149         {"execute", PLy_spi_execute, METH_VARARGS, NULL},
2150
2151         {NULL, NULL, 0, NULL}
2152 };
2153
2154
2155 /* plan object methods */
2156 static PyObject *
2157 PLy_plan_new(void)
2158 {
2159         PLyPlanObject *ob;
2160
2161         if ((ob = PyObject_NEW(PLyPlanObject, &PLy_PlanType)) == NULL)
2162                 return NULL;
2163
2164         ob->plan = NULL;
2165         ob->nargs = 0;
2166         ob->types = NULL;
2167         ob->args = NULL;
2168
2169         return (PyObject *) ob;
2170 }
2171
2172
2173 static void
2174 PLy_plan_dealloc(PyObject * arg)
2175 {
2176         PLyPlanObject *ob = (PLyPlanObject *) arg;
2177
2178         if (ob->plan)
2179                 SPI_freeplan(ob->plan);
2180         if (ob->types)
2181                 PLy_free(ob->types);
2182         if (ob->args)
2183         {
2184                 int                     i;
2185
2186                 for (i = 0; i < ob->nargs; i++)
2187                         PLy_typeinfo_dealloc(&ob->args[i]);
2188                 PLy_free(ob->args);
2189         }
2190
2191         arg->ob_type->tp_free(arg);
2192 }
2193
2194
2195 static PyObject *
2196 PLy_plan_getattr(PyObject * self, char *name)
2197 {
2198         return Py_FindMethod(PLy_plan_methods, self, name);
2199 }
2200
2201 static PyObject *
2202 PLy_plan_status(PyObject * self, PyObject * args)
2203 {
2204         if (PyArg_ParseTuple(args, ""))
2205         {
2206                 Py_INCREF(Py_True);
2207                 return Py_True;
2208                 /* return PyInt_FromLong(self->status); */
2209         }
2210         PLy_exception_set(PLy_exc_error, "plan.status takes no arguments");
2211         return NULL;
2212 }
2213
2214
2215
2216 /* result object methods */
2217
2218 static PyObject *
2219 PLy_result_new(void)
2220 {
2221         PLyResultObject *ob;
2222
2223         if ((ob = PyObject_NEW(PLyResultObject, &PLy_ResultType)) == NULL)
2224                 return NULL;
2225
2226         /* ob->tuples = NULL; */
2227
2228         Py_INCREF(Py_None);
2229         ob->status = Py_None;
2230         ob->nrows = PyInt_FromLong(-1);
2231         ob->rows = PyList_New(0);
2232
2233         return (PyObject *) ob;
2234 }
2235
2236 static void
2237 PLy_result_dealloc(PyObject * arg)
2238 {
2239         PLyResultObject *ob = (PLyResultObject *) arg;
2240
2241         Py_XDECREF(ob->nrows);
2242         Py_XDECREF(ob->rows);
2243         Py_XDECREF(ob->status);
2244
2245         arg->ob_type->tp_free(arg);
2246 }
2247
2248 static PyObject *
2249 PLy_result_getattr(PyObject * self, char *name)
2250 {
2251         return Py_FindMethod(PLy_result_methods, self, name);
2252 }
2253
2254 static PyObject *
2255 PLy_result_nrows(PyObject * self, PyObject * args)
2256 {
2257         PLyResultObject *ob = (PLyResultObject *) self;
2258
2259         Py_INCREF(ob->nrows);
2260         return ob->nrows;
2261 }
2262
2263 static PyObject *
2264 PLy_result_status(PyObject * self, PyObject * args)
2265 {
2266         PLyResultObject *ob = (PLyResultObject *) self;
2267
2268         Py_INCREF(ob->status);
2269         return ob->status;
2270 }
2271
2272 static Py_ssize_t
2273 PLy_result_length(PyObject * arg)
2274 {
2275         PLyResultObject *ob = (PLyResultObject *) arg;
2276
2277         return PyList_Size(ob->rows);
2278 }
2279
2280 static PyObject *
2281 PLy_result_item(PyObject * arg, Py_ssize_t idx)
2282 {
2283         PyObject   *rv;
2284         PLyResultObject *ob = (PLyResultObject *) arg;
2285
2286         rv = PyList_GetItem(ob->rows, idx);
2287         if (rv != NULL)
2288                 Py_INCREF(rv);
2289         return rv;
2290 }
2291
2292 static int
2293 PLy_result_ass_item(PyObject * arg, Py_ssize_t idx, PyObject * item)
2294 {
2295         int                     rv;
2296         PLyResultObject *ob = (PLyResultObject *) arg;
2297
2298         Py_INCREF(item);
2299         rv = PyList_SetItem(ob->rows, idx, item);
2300         return rv;
2301 }
2302
2303 static PyObject *
2304 PLy_result_slice(PyObject * arg, Py_ssize_t lidx, Py_ssize_t hidx)
2305 {
2306         PyObject   *rv;
2307         PLyResultObject *ob = (PLyResultObject *) arg;
2308
2309         rv = PyList_GetSlice(ob->rows, lidx, hidx);
2310         if (rv == NULL)
2311                 return NULL;
2312         Py_INCREF(rv);
2313         return rv;
2314 }
2315
2316 static int
2317 PLy_result_ass_slice(PyObject * arg, Py_ssize_t lidx, Py_ssize_t hidx, PyObject * slice)
2318 {
2319         int                     rv;
2320         PLyResultObject *ob = (PLyResultObject *) arg;
2321
2322         rv = PyList_SetSlice(ob->rows, lidx, hidx, slice);
2323         return rv;
2324 }
2325
2326 /* SPI interface */
2327 static PyObject *
2328 PLy_spi_prepare(PyObject * self, PyObject * args)
2329 {
2330         PLyPlanObject *plan;
2331         PyObject   *list = NULL;
2332         PyObject   *volatile optr = NULL;
2333         char       *query;
2334         void       *tmpplan;
2335         MemoryContext oldcontext;
2336
2337         /* Can't execute more if we have an unhandled error */
2338         if (PLy_error_in_progress)
2339         {
2340                 PLy_exception_set(PLy_exc_error, "transaction aborted");
2341                 return NULL;
2342         }
2343
2344         if (!PyArg_ParseTuple(args, "s|O", &query, &list))
2345         {
2346                 PLy_exception_set(PLy_exc_spi_error,
2347                                                   "invalid arguments for plpy.prepare");
2348                 return NULL;
2349         }
2350
2351         if (list && (!PySequence_Check(list)))
2352         {
2353                 PLy_exception_set(PLy_exc_spi_error,
2354                                                   "second argument of plpy.prepare must be a sequence");
2355                 return NULL;
2356         }
2357
2358         if ((plan = (PLyPlanObject *) PLy_plan_new()) == NULL)
2359                 return NULL;
2360
2361         oldcontext = CurrentMemoryContext;
2362         PG_TRY();
2363         {
2364                 if (list != NULL)
2365                 {
2366                         int                     nargs,
2367                                                 i;
2368
2369                         nargs = PySequence_Length(list);
2370                         if (nargs > 0)
2371                         {
2372                                 plan->nargs = nargs;
2373                                 plan->types = PLy_malloc(sizeof(Oid) * nargs);
2374                                 plan->values = PLy_malloc(sizeof(Datum) * nargs);
2375                                 plan->args = PLy_malloc(sizeof(PLyTypeInfo) * nargs);
2376
2377                                 /*
2378                                  * the other loop might throw an exception, if PLyTypeInfo
2379                                  * member isn't properly initialized the Py_DECREF(plan) will
2380                                  * go boom
2381                                  */
2382                                 for (i = 0; i < nargs; i++)
2383                                 {
2384                                         PLy_typeinfo_init(&plan->args[i]);
2385                                         plan->values[i] = PointerGetDatum(NULL);
2386                                 }
2387
2388                                 for (i = 0; i < nargs; i++)
2389                                 {
2390                                         char       *sptr;
2391                                         HeapTuple       typeTup;
2392                                         Oid                     typeId;
2393                                         int32           typmod;
2394                                         Form_pg_type typeStruct;
2395
2396                                         optr = PySequence_GetItem(list, i);
2397                                         if (!PyString_Check(optr))
2398                                                 ereport(ERROR,
2399                                                                 (errmsg("plpy.prepare: type name at ordinal position %d is not a string", i)));
2400                                         sptr = PyString_AsString(optr);
2401
2402                                         /********************************************************
2403                                          * Resolve argument type names and then look them up by
2404                                          * oid in the system cache, and remember the required
2405                                          *information for input conversion.
2406                                          ********************************************************/
2407
2408                                         parseTypeString(sptr, &typeId, &typmod);
2409
2410                                         typeTup = SearchSysCache(TYPEOID,
2411                                                                                          ObjectIdGetDatum(typeId),
2412                                                                                          0, 0, 0);
2413                                         if (!HeapTupleIsValid(typeTup))
2414                                                 elog(ERROR, "cache lookup failed for type %u", typeId);
2415
2416                                         Py_DECREF(optr);
2417                                         optr = NULL;    /* this is important */
2418
2419                                         plan->types[i] = typeId;
2420                                         typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
2421                                         if (typeStruct->typtype != TYPTYPE_COMPOSITE)
2422                                                 PLy_output_datum_func(&plan->args[i], typeTup);
2423                                         else
2424                                                 ereport(ERROR,
2425                                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2426                                                                  errmsg("plpy.prepare does not support composite types")));
2427                                         ReleaseSysCache(typeTup);
2428                                 }
2429                         }
2430                 }
2431
2432                 plan->plan = SPI_prepare(query, plan->nargs, plan->types);
2433                 if (plan->plan == NULL)
2434                         elog(ERROR, "SPI_prepare failed: %s",
2435                                  SPI_result_code_string(SPI_result));
2436
2437                 /* transfer plan from procCxt to topCxt */
2438                 tmpplan = plan->plan;
2439                 plan->plan = SPI_saveplan(tmpplan);
2440                 SPI_freeplan(tmpplan);
2441                 if (plan->plan == NULL)
2442                         elog(ERROR, "SPI_saveplan failed: %s",
2443                                  SPI_result_code_string(SPI_result));
2444         }
2445         PG_CATCH();
2446         {
2447                 MemoryContextSwitchTo(oldcontext);
2448                 PLy_error_in_progress = CopyErrorData();
2449                 FlushErrorState();
2450                 Py_DECREF(plan);
2451                 Py_XDECREF(optr);
2452                 if (!PyErr_Occurred())
2453                         PLy_exception_set(PLy_exc_spi_error,
2454                                                           "unrecognized error in PLy_spi_prepare");
2455                 /* XXX this oughta be replaced with errcontext mechanism */
2456                 PLy_elog(WARNING, "in PL/Python function \"%s\"",
2457                                  PLy_procedure_name(PLy_curr_procedure));
2458                 return NULL;
2459         }
2460         PG_END_TRY();
2461
2462         return (PyObject *) plan;
2463 }
2464
2465 /* execute(query="select * from foo", limit=5)
2466  * execute(plan=plan, values=(foo, bar), limit=5)
2467  */
2468 static PyObject *
2469 PLy_spi_execute(PyObject * self, PyObject * args)
2470 {
2471         char       *query;
2472         PyObject   *plan;
2473         PyObject   *list = NULL;
2474         long            limit = 0;
2475
2476         /* Can't execute more if we have an unhandled error */
2477         if (PLy_error_in_progress)
2478         {
2479                 PLy_exception_set(PLy_exc_error, "transaction aborted");
2480                 return NULL;
2481         }
2482
2483         if (PyArg_ParseTuple(args, "s|l", &query, &limit))
2484                 return PLy_spi_execute_query(query, limit);
2485
2486         PyErr_Clear();
2487
2488         if (PyArg_ParseTuple(args, "O|Ol", &plan, &list, &limit) &&
2489                 is_PLyPlanObject(plan))
2490                 return PLy_spi_execute_plan(plan, list, limit);
2491
2492         PLy_exception_set(PLy_exc_error, "plpy.execute expected a query or a plan");
2493         return NULL;
2494 }
2495
2496 static PyObject *
2497 PLy_spi_execute_plan(PyObject * ob, PyObject * list, long limit)
2498 {
2499         volatile int nargs;
2500         int                     i,
2501                                 rv;
2502         PLyPlanObject *plan;
2503         MemoryContext oldcontext;
2504
2505         if (list != NULL)
2506         {
2507                 if (!PySequence_Check(list) || PyString_Check(list))
2508                 {
2509                         PLy_exception_set(PLy_exc_spi_error, "plpy.execute takes a sequence as its second argument");
2510                         return NULL;
2511                 }
2512                 nargs = PySequence_Length(list);
2513         }
2514         else
2515                 nargs = 0;
2516
2517         plan = (PLyPlanObject *) ob;
2518
2519         if (nargs != plan->nargs)
2520         {
2521                 char       *sv;
2522                 PyObject   *so = PyObject_Str(list);
2523
2524                 if (!so)
2525                         PLy_elog(ERROR, "PL/Python function \"%s\" could not execute plan",
2526                                          PLy_procedure_name(PLy_curr_procedure));
2527                 sv = PyString_AsString(so);
2528                 PLy_exception_set(PLy_exc_spi_error,
2529                                                   dngettext(TEXTDOMAIN, "Expected sequence of %d argument, got %d: %s", "Expected sequence of %d arguments, got %d: %s", plan->nargs),
2530                                                   plan->nargs, nargs, sv);
2531                 Py_DECREF(so);
2532
2533                 return NULL;
2534         }
2535
2536         oldcontext = CurrentMemoryContext;
2537         PG_TRY();
2538         {
2539                 char       *nulls = palloc(nargs * sizeof(char));
2540                 volatile int j;
2541
2542                 for (j = 0; j < nargs; j++)
2543                 {
2544                         PyObject   *elem,
2545                                            *so;
2546
2547                         elem = PySequence_GetItem(list, j);
2548                         if (elem != Py_None)
2549                         {
2550                                 so = PyObject_Str(elem);
2551                                 if (!so)
2552                                         PLy_elog(ERROR, "PL/Python function \"%s\" could not execute plan",
2553                                                          PLy_procedure_name(PLy_curr_procedure));
2554                                 Py_DECREF(elem);
2555
2556                                 PG_TRY();
2557                                 {
2558                                         char       *sv = PyString_AsString(so);
2559
2560                                         plan->values[j] =
2561                                                 InputFunctionCall(&(plan->args[j].out.d.typfunc),
2562                                                                                   sv,
2563                                                                                   plan->args[j].out.d.typioparam,
2564                                                                                   -1);
2565                                 }
2566                                 PG_CATCH();
2567                                 {
2568                                         Py_DECREF(so);
2569                                         PG_RE_THROW();
2570                                 }
2571                                 PG_END_TRY();
2572
2573                                 Py_DECREF(so);
2574                                 nulls[j] = ' ';
2575                         }
2576                         else
2577                         {
2578                                 Py_DECREF(elem);
2579                                 plan->values[j] =
2580                                         InputFunctionCall(&(plan->args[j].out.d.typfunc),
2581                                                                           NULL,
2582                                                                           plan->args[j].out.d.typioparam,
2583                                                                           -1);
2584                                 nulls[j] = 'n';
2585                         }
2586                 }
2587
2588                 rv = SPI_execute_plan(plan->plan, plan->values, nulls,
2589                                                           PLy_curr_procedure->fn_readonly, limit);
2590
2591                 pfree(nulls);
2592         }
2593         PG_CATCH();
2594         {
2595                 int                     k;
2596
2597                 MemoryContextSwitchTo(oldcontext);
2598                 PLy_error_in_progress = CopyErrorData();
2599                 FlushErrorState();
2600
2601                 /*
2602                  * cleanup plan->values array
2603                  */
2604                 for (k = 0; k < nargs; k++)
2605                 {
2606                         if (!plan->args[k].out.d.typbyval &&
2607                                 (plan->values[k] != PointerGetDatum(NULL)))
2608                         {
2609                                 pfree(DatumGetPointer(plan->values[k]));
2610                                 plan->values[k] = PointerGetDatum(NULL);
2611                         }
2612                 }
2613
2614                 if (!PyErr_Occurred())
2615                         PLy_exception_set(PLy_exc_error,
2616                                                           "unrecognized error in PLy_spi_execute_plan");
2617                 /* XXX this oughta be replaced with errcontext mechanism */
2618                 PLy_elog(WARNING, "in PL/Python function \"%s\"",
2619                                  PLy_procedure_name(PLy_curr_procedure));
2620                 return NULL;
2621         }
2622         PG_END_TRY();
2623
2624         for (i = 0; i < nargs; i++)
2625         {
2626                 if (!plan->args[i].out.d.typbyval &&
2627                         (plan->values[i] != PointerGetDatum(NULL)))
2628                 {
2629                         pfree(DatumGetPointer(plan->values[i]));
2630                         plan->values[i] = PointerGetDatum(NULL);
2631                 }
2632         }
2633
2634         if (rv < 0)
2635         {
2636                 PLy_exception_set(PLy_exc_spi_error,
2637                                                   "SPI_execute_plan failed: %s",
2638                                                   SPI_result_code_string(rv));
2639                 return NULL;
2640         }
2641
2642         return PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
2643 }
2644
2645 static PyObject *
2646 PLy_spi_execute_query(char *query, long limit)
2647 {
2648         int                     rv;
2649         MemoryContext oldcontext;
2650
2651         oldcontext = CurrentMemoryContext;
2652         PG_TRY();
2653         {
2654                 rv = SPI_execute(query, PLy_curr_procedure->fn_readonly, limit);
2655         }
2656         PG_CATCH();
2657         {
2658                 MemoryContextSwitchTo(oldcontext);
2659                 PLy_error_in_progress = CopyErrorData();
2660                 FlushErrorState();
2661                 if (!PyErr_Occurred())
2662                         PLy_exception_set(PLy_exc_spi_error,
2663                                                           "unrecognized error in PLy_spi_execute_query");
2664                 /* XXX this oughta be replaced with errcontext mechanism */
2665                 PLy_elog(WARNING, "in PL/Python function \"%s\"",
2666                                  PLy_procedure_name(PLy_curr_procedure));
2667                 return NULL;
2668         }
2669         PG_END_TRY();
2670
2671         if (rv < 0)
2672         {
2673                 PLy_exception_set(PLy_exc_spi_error,
2674                                                   "SPI_execute failed: %s",
2675                                                   SPI_result_code_string(rv));
2676                 return NULL;
2677         }
2678
2679         return PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
2680 }
2681
2682 static PyObject *
2683 PLy_spi_execute_fetch_result(SPITupleTable *tuptable, int rows, int status)
2684 {
2685         PLyResultObject *result;
2686         MemoryContext oldcontext;
2687
2688         result = (PLyResultObject *) PLy_result_new();
2689         Py_DECREF(result->status);
2690         result->status = PyInt_FromLong(status);
2691
2692         if (status > 0 && tuptable == NULL)
2693         {
2694                 Py_DECREF(result->nrows);
2695                 result->nrows = PyInt_FromLong(rows);
2696         }
2697         else if (status > 0 && tuptable != NULL)
2698         {
2699                 PLyTypeInfo args;
2700                 int                     i;
2701
2702                 Py_DECREF(result->nrows);
2703                 result->nrows = PyInt_FromLong(rows);
2704                 PLy_typeinfo_init(&args);
2705
2706                 oldcontext = CurrentMemoryContext;
2707                 PG_TRY();
2708                 {
2709                         if (rows)
2710                         {
2711                                 Py_DECREF(result->rows);
2712                                 result->rows = PyList_New(rows);
2713
2714                                 PLy_input_tuple_funcs(&args, tuptable->tupdesc);
2715                                 for (i = 0; i < rows; i++)
2716                                 {
2717                                         PyObject   *row = PLyDict_FromTuple(&args, tuptable->vals[i],
2718                                                                                                                 tuptable->tupdesc);
2719
2720                                         PyList_SetItem(result->rows, i, row);
2721                                 }
2722                                 PLy_typeinfo_dealloc(&args);
2723
2724                                 SPI_freetuptable(tuptable);
2725                         }
2726                 }
2727                 PG_CATCH();
2728                 {
2729                         MemoryContextSwitchTo(oldcontext);
2730                         PLy_error_in_progress = CopyErrorData();
2731                         FlushErrorState();
2732                         if (!PyErr_Occurred())
2733                                 PLy_exception_set(PLy_exc_error,
2734                                                                   "unrecognized error in PLy_spi_execute_fetch_result");
2735                         Py_DECREF(result);
2736                         PLy_typeinfo_dealloc(&args);
2737                         return NULL;
2738                 }
2739                 PG_END_TRY();
2740         }
2741
2742         return (PyObject *) result;
2743 }
2744
2745
2746 /*
2747  * language handler and interpreter initialization
2748  */
2749
2750 /*
2751  * _PG_init()                   - library load-time initialization
2752  *
2753  * DO NOT make this static nor change its name!
2754  */
2755 void
2756 _PG_init(void)
2757 {
2758         /* Be sure we do initialization only once (should be redundant now) */
2759         static bool inited = false;
2760
2761         if (inited)
2762                 return;
2763
2764         pg_bindtextdomain(TEXTDOMAIN);
2765
2766         Py_Initialize();
2767         PLy_init_interp();
2768         PLy_init_plpy();
2769         if (PyErr_Occurred())
2770                 PLy_elog(FATAL, "untrapped error in initialization");
2771         PLy_procedure_cache = PyDict_New();
2772         if (PLy_procedure_cache == NULL)
2773                 PLy_elog(ERROR, "could not create procedure cache");
2774
2775         inited = true;
2776 }
2777
2778 static void
2779 PLy_init_interp(void)
2780 {
2781         PyObject   *mainmod;
2782
2783         mainmod = PyImport_AddModule("__main__");
2784         if (mainmod == NULL || PyErr_Occurred())
2785                 PLy_elog(ERROR, "could not import \"__main__\" module");
2786         Py_INCREF(mainmod);
2787         PLy_interp_globals = PyModule_GetDict(mainmod);
2788         PLy_interp_safe_globals = PyDict_New();
2789         PyDict_SetItemString(PLy_interp_globals, "GD", PLy_interp_safe_globals);
2790         Py_DECREF(mainmod);
2791         if (PLy_interp_globals == NULL || PyErr_Occurred())
2792                 PLy_elog(ERROR, "could not initialize globals");
2793 }
2794
2795 static void
2796 PLy_init_plpy(void)
2797 {
2798         PyObject   *main_mod,
2799                            *main_dict,
2800                            *plpy_mod;
2801         PyObject   *plpy,
2802                            *plpy_dict;
2803
2804         /*
2805          * initialize plpy module
2806          */
2807         if (PyType_Ready(&PLy_PlanType) < 0)
2808                 elog(ERROR, "could not initialize PLy_PlanType");
2809         if (PyType_Ready(&PLy_ResultType) < 0)
2810                 elog(ERROR, "could not initialize PLy_ResultType");
2811
2812         plpy = Py_InitModule("plpy", PLy_methods);
2813         plpy_dict = PyModule_GetDict(plpy);
2814
2815         /* PyDict_SetItemString(plpy, "PlanType", (PyObject *) &PLy_PlanType); */
2816
2817         PLy_exc_error = PyErr_NewException("plpy.Error", NULL, NULL);
2818         PLy_exc_fatal = PyErr_NewException("plpy.Fatal", NULL, NULL);
2819         PLy_exc_spi_error = PyErr_NewException("plpy.SPIError", NULL, NULL);
2820         PyDict_SetItemString(plpy_dict, "Error", PLy_exc_error);
2821         PyDict_SetItemString(plpy_dict, "Fatal", PLy_exc_fatal);
2822         PyDict_SetItemString(plpy_dict, "SPIError", PLy_exc_spi_error);
2823
2824         /*
2825          * initialize main module, and add plpy
2826          */
2827         main_mod = PyImport_AddModule("__main__");
2828         main_dict = PyModule_GetDict(main_mod);
2829         plpy_mod = PyImport_AddModule("plpy");
2830         PyDict_SetItemString(main_dict, "plpy", plpy_mod);
2831         if (PyErr_Occurred())
2832                 elog(ERROR, "could not initialize plpy");
2833 }
2834
2835 /* the python interface to the elog function
2836  * don't confuse these with PLy_elog
2837  */
2838 static PyObject *PLy_output(volatile int, PyObject *, PyObject *);
2839
2840 static PyObject *
2841 PLy_debug(PyObject * self, PyObject * args)
2842 {
2843         return PLy_output(DEBUG2, self, args);
2844 }
2845
2846 static PyObject *
2847 PLy_log(PyObject * self, PyObject * args)
2848 {
2849         return PLy_output(LOG, self, args);
2850 }
2851
2852 static PyObject *
2853 PLy_info(PyObject * self, PyObject * args)
2854 {
2855         return PLy_output(INFO, self, args);
2856 }
2857
2858 static PyObject *
2859 PLy_notice(PyObject * self, PyObject * args)
2860 {
2861         return PLy_output(NOTICE, self, args);
2862 }
2863
2864 static PyObject *
2865 PLy_warning(PyObject * self, PyObject * args)
2866 {
2867         return PLy_output(WARNING, self, args);
2868 }
2869
2870 static PyObject *
2871 PLy_error(PyObject * self, PyObject * args)
2872 {
2873         return PLy_output(ERROR, self, args);
2874 }
2875
2876 static PyObject *
2877 PLy_fatal(PyObject * self, PyObject * args)
2878 {
2879         return PLy_output(FATAL, self, args);
2880 }
2881
2882
2883 static PyObject *
2884 PLy_output(volatile int level, PyObject * self, PyObject * args)
2885 {
2886         PyObject   *so;
2887         char       *volatile sv;
2888         MemoryContext oldcontext;
2889
2890         so = PyObject_Str(args);
2891         if (so == NULL || ((sv = PyString_AsString(so)) == NULL))
2892         {
2893                 level = ERROR;
2894                 sv = dgettext(TEXTDOMAIN, "could not parse error message in plpy.elog");
2895         }
2896
2897         oldcontext = CurrentMemoryContext;
2898         PG_TRY();
2899         {
2900                 elog(level, "%s", sv);
2901         }
2902         PG_CATCH();
2903         {
2904                 MemoryContextSwitchTo(oldcontext);
2905                 PLy_error_in_progress = CopyErrorData();
2906                 FlushErrorState();
2907                 Py_XDECREF(so);
2908
2909                 /*
2910                  * returning NULL here causes the python interpreter to bail. when
2911                  * control passes back to PLy_procedure_call, we check for PG
2912                  * exceptions and re-throw the error.
2913                  */
2914                 PyErr_SetString(PLy_exc_error, sv);
2915                 return NULL;
2916         }
2917         PG_END_TRY();
2918
2919         Py_XDECREF(so);
2920
2921         /*
2922          * return a legal object so the interpreter will continue on its merry way
2923          */
2924         Py_INCREF(Py_None);
2925         return Py_None;
2926 }
2927
2928
2929 /*
2930  * Get the name of the last procedure called by the backend (the
2931  * innermost, if a plpython procedure call calls the backend and the
2932  * backend calls another plpython procedure).
2933  *
2934  * NB: this returns the SQL name, not the internal Python procedure name
2935  */
2936 static char *
2937 PLy_procedure_name(PLyProcedure * proc)
2938 {
2939         if (proc == NULL)
2940                 return "<unknown procedure>";
2941         return proc->proname;
2942 }
2943
2944 /* output a python traceback/exception via the postgresql elog
2945  * function.  not pretty.
2946  */
2947 static void
2948 PLy_exception_set(PyObject * exc, const char *fmt,...)
2949 {
2950         char            buf[1024];
2951         va_list         ap;
2952
2953         va_start(ap, fmt);
2954         vsnprintf(buf, sizeof(buf), dgettext(TEXTDOMAIN, fmt), ap);
2955         va_end(ap);
2956
2957         PyErr_SetString(exc, buf);
2958 }
2959
2960 /* Emit a PG error or notice, together with any available info about the
2961  * current Python error.  This should be used to propagate Python errors
2962  * into PG.
2963  */
2964 static void
2965 PLy_elog(int elevel, const char *fmt,...)
2966 {
2967         char       *xmsg;
2968         int                     xlevel;
2969         StringInfoData emsg;
2970
2971         xmsg = PLy_traceback(&xlevel);
2972
2973         initStringInfo(&emsg);
2974         for (;;)
2975         {
2976                 va_list         ap;
2977                 bool            success;
2978
2979                 va_start(ap, fmt);
2980                 success = appendStringInfoVA(&emsg, dgettext(TEXTDOMAIN, fmt), ap);
2981                 va_end(ap);
2982                 if (success)
2983                         break;
2984                 enlargeStringInfo(&emsg, emsg.maxlen);
2985         }
2986
2987         PG_TRY();
2988         {
2989                 ereport(elevel,
2990                                 (errmsg("PL/Python: %s", emsg.data),
2991                                  (xmsg) ? errdetail("%s", xmsg) : 0));
2992         }
2993         PG_CATCH();
2994         {
2995                 pfree(emsg.data);
2996                 if (xmsg)
2997                         pfree(xmsg);
2998                 PG_RE_THROW();
2999         }
3000         PG_END_TRY();
3001
3002         pfree(emsg.data);
3003         if (xmsg)
3004                 pfree(xmsg);
3005 }
3006
3007 static char *
3008 PLy_traceback(int *xlevel)
3009 {
3010         PyObject   *e,
3011                            *v,
3012                            *tb;
3013         PyObject   *eob,
3014                            *vob = NULL;
3015         char       *vstr,
3016                            *estr;
3017         StringInfoData xstr;
3018
3019         /*
3020          * get the current exception
3021          */
3022         PyErr_Fetch(&e, &v, &tb);
3023
3024         /*
3025          * oops, no exception, return
3026          */
3027         if (e == NULL)
3028         {
3029                 *xlevel = WARNING;
3030                 return NULL;
3031         }
3032
3033         PyErr_NormalizeException(&e, &v, &tb);
3034         Py_XDECREF(tb);
3035
3036         eob = PyObject_Str(e);
3037         if (v && ((vob = PyObject_Str(v)) != NULL))
3038                 vstr = PyString_AsString(vob);
3039         else
3040                 vstr = "unknown";
3041
3042         /*
3043          * I'm not sure what to do if eob is NULL here -- we can't call PLy_elog
3044          * because that function calls us, so we could end up with infinite
3045          * recursion.  I'm not even sure if eob could be NULL here -- would an
3046          * Assert() be more appropriate?
3047          */
3048         estr = eob ? PyString_AsString(eob) : "unrecognized exception";
3049         initStringInfo(&xstr);
3050         appendStringInfo(&xstr, "%s: %s", estr, vstr);
3051
3052         Py_DECREF(eob);
3053         Py_XDECREF(vob);
3054         Py_XDECREF(v);
3055
3056         /*
3057          * intuit an appropriate error level based on the exception type
3058          */
3059         if (PLy_exc_error && PyErr_GivenExceptionMatches(e, PLy_exc_error))
3060                 *xlevel = ERROR;
3061         else if (PLy_exc_fatal && PyErr_GivenExceptionMatches(e, PLy_exc_fatal))
3062                 *xlevel = FATAL;
3063         else
3064                 *xlevel = ERROR;
3065
3066         Py_DECREF(e);
3067         return xstr.data;
3068 }
3069
3070 /* python module code */
3071
3072 /* some dumb utility functions */
3073 static void *
3074 PLy_malloc(size_t bytes)
3075 {
3076         void       *ptr = malloc(bytes);
3077
3078         if (ptr == NULL)
3079                 ereport(FATAL,
3080                                 (errcode(ERRCODE_OUT_OF_MEMORY),
3081                                  errmsg("out of memory")));
3082         return ptr;
3083 }
3084
3085 static void *
3086 PLy_malloc0(size_t bytes)
3087 {
3088         void       *ptr = PLy_malloc(bytes);
3089
3090         MemSet(ptr, 0, bytes);
3091         return ptr;
3092 }
3093
3094 static char *
3095 PLy_strdup(const char *str)
3096 {
3097         char       *result;
3098         size_t          len;
3099
3100         len = strlen(str) + 1;
3101         result = PLy_malloc(len);
3102         memcpy(result, str, len);
3103
3104         return result;
3105 }
3106
3107 /* define this away */
3108 static void
3109 PLy_free(void *ptr)
3110 {
3111         free(ptr);
3112 }