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