]> granicus.if.org Git - postgresql/blob - src/pl/plpython/plpython.c
Move include for Python.h above postgres.h to eliminate compiler warning.
[postgresql] / src / pl / plpython / plpython.c
1 /**********************************************************************
2  * plpython.c - python as a procedural language for PostgreSQL
3  *
4  * This software is copyright by Andrew Bosma
5  * but is really shameless cribbed from pltcl.c by Jan Weick, and
6  * plperl.c by Mark Hollomon.
7  *
8  * The author hereby grants permission to use, copy, modify,
9  * distribute, and license this software and its documentation for any
10  * purpose, provided that existing copyright notices are retained in
11  * all copies and that this notice is included verbatim in any
12  * distributions. No written agreement, license, or royalty fee is
13  * required for any of the authorized uses.  Modifications to this
14  * software may be copyrighted by their author and need not follow the
15  * licensing terms described here, provided that the new terms are
16  * clearly indicated on the first page of each file where they apply.
17  *
18  * IN NO EVENT SHALL THE AUTHOR OR DISTRIBUTORS BE LIABLE TO ANY PARTY
19  * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
20  * ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
21  * DERIVATIVES THEREOF, EVEN IF THE AUTHOR HAVE BEEN ADVISED OF THE
22  * POSSIBILITY OF SUCH DAMAGE.
23  *
24  * THE AUTHOR AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
25  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
27  * NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
28  * AND THE AUTHOR AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
29  * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
30  *
31  * IDENTIFICATION
32  *      $PostgreSQL: pgsql/src/pl/plpython/plpython.c,v 1.53 2004/08/05 03:10:29 joe Exp $
33  *
34  *********************************************************************
35  */
36
37 #include <Python.h>
38 #include "postgres.h"
39
40 /* system stuff */
41 #include <unistd.h>
42 #include <fcntl.h>
43
44 /* postgreSQL stuff */
45 #include "access/heapam.h"
46 #include "catalog/pg_proc.h"
47 #include "catalog/pg_type.h"
48 #include "commands/trigger.h"
49 #include "executor/spi.h"
50 #include "fmgr.h"
51 #include "nodes/makefuncs.h"
52 #include "parser/parse_type.h"
53 #include "tcop/tcopprot.h"
54 #include "utils/lsyscache.h"
55 #include "utils/syscache.h"
56 #include "utils/typcache.h"
57
58 #include <compile.h>
59 #include <eval.h>
60
61 /* convert Postgresql Datum or tuple into a PyObject.
62  * input to Python.  Tuples are converted to dictionary
63  * objects.
64  */
65
66 typedef PyObject *(*PLyDatumToObFunc) (const char *);
67
68 typedef struct PLyDatumToOb
69 {
70         PLyDatumToObFunc func;
71         FmgrInfo        typfunc;
72         Oid                     typioparam;
73         bool            typbyval;
74 }       PLyDatumToOb;
75
76 typedef struct PLyTupleToOb
77 {
78         PLyDatumToOb *atts;
79         int                     natts;
80 }       PLyTupleToOb;
81
82 typedef union PLyTypeInput
83 {
84         PLyDatumToOb d;
85         PLyTupleToOb r;
86 }       PLyTypeInput;
87
88 /* convert PyObject to a Postgresql Datum or tuple.
89  * output from Python
90  */
91 typedef struct PLyObToDatum
92 {
93         FmgrInfo        typfunc;
94         Oid                     typioparam;
95         bool            typbyval;
96 }       PLyObToDatum;
97
98 typedef struct PLyObToTuple
99 {
100         PLyObToDatum *atts;
101         int                     natts;
102 }       PLyObToTuple;
103
104 typedef union PLyTypeOutput
105 {
106         PLyObToDatum d;
107         PLyObToTuple r;
108 }       PLyTypeOutput;
109
110 /* all we need to move Postgresql data to Python objects,
111  * and vis versa
112  */
113 typedef struct PLyTypeInfo
114 {
115         PLyTypeInput in;
116         PLyTypeOutput out;
117         int                     is_rowtype;
118         /*
119          * is_rowtype can be:
120          *              -1      not known yet (initial state)
121          *               0      scalar datatype
122          *               1      rowtype
123          *               2      rowtype, but I/O functions not set up yet
124          */
125 }       PLyTypeInfo;
126
127
128 /* cached procedure data
129  */
130 typedef struct PLyProcedure
131 {
132         char       *proname;            /* SQL name of procedure */
133         char       *pyname;                     /* Python name of procedure */
134         TransactionId fn_xmin;
135         CommandId       fn_cmin;
136         PLyTypeInfo result;                     /* also used to store info for trigger
137                                                                  * tuple type */
138         PLyTypeInfo args[FUNC_MAX_ARGS];
139         int                     nargs;
140         PyObject   *code;                       /* compiled procedure code */
141         PyObject   *statics;            /* data saved across calls, local scope */
142         PyObject   *globals;            /* data saved across calls, global score */
143         PyObject   *me;                         /* PyCObject containing pointer to this
144                                                                  * PLyProcedure */
145 }       PLyProcedure;
146
147
148 /* Python objects.
149  */
150 typedef struct PLyPlanObject
151 {
152         PyObject_HEAD
153         void       *plan;                       /* return of an SPI_saveplan */
154         int                     nargs;
155         Oid                *types;
156         Datum      *values;
157         PLyTypeInfo *args;
158 }       PLyPlanObject;
159
160 typedef struct PLyResultObject
161 {
162         PyObject_HEAD
163         /* HeapTuple *tuples; */
164         PyObject   *nrows;                      /* number of rows returned by query */
165         PyObject   *rows;                       /* data rows, or None if no data returned */
166         PyObject   *status;                     /* query status, SPI_OK_*, or SPI_ERR_* */
167 }       PLyResultObject;
168
169
170 /* function declarations
171  */
172
173 /* Two exported functions: first is the magic telling Postgresql
174  * what function call interface it implements. Second allows
175  * preinitialization of the interpreter during postmaster startup.
176  */
177 Datum           plpython_call_handler(PG_FUNCTION_ARGS);
178 void            plpython_init(void);
179
180 PG_FUNCTION_INFO_V1(plpython_call_handler);
181
182 /* most of the remaining of the declarations, all static
183  */
184
185 /* these should only be called once at the first call
186  * of plpython_call_handler.  initialize the python interpreter
187  * and global data.
188  */
189 static void PLy_init_all(void);
190 static void PLy_init_interp(void);
191 static void PLy_init_plpy(void);
192
193 /* call PyErr_SetString with a vprint interface
194  */
195 static void
196 PLy_exception_set(PyObject *, const char *,...)
197 __attribute__((format(printf, 2, 3)));
198
199 /* Get the innermost python procedure called from the backend.
200  */
201 static char *PLy_procedure_name(PLyProcedure *);
202
203 /* some utility functions
204  */
205 static void PLy_elog(int, const char *,...);
206 static char *PLy_traceback(int *);
207 static char *PLy_vprintf(const char *fmt, va_list ap);
208 static char *PLy_printf(const char *fmt,...);
209
210 static void *PLy_malloc(size_t);
211 static void *PLy_realloc(void *, size_t);
212 static void PLy_free(void *);
213
214 /* sub handlers for functions and triggers
215  */
216 static Datum PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure *);
217 static HeapTuple PLy_trigger_handler(FunctionCallInfo fcinfo, PLyProcedure *);
218
219 static PyObject *PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *);
220 static PyObject *PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *,
221                                            HeapTuple *);
222 static HeapTuple PLy_modify_tuple(PLyProcedure *, PyObject *,
223                                  TriggerData *, HeapTuple);
224
225 static PyObject *PLy_procedure_call(PLyProcedure *, char *, PyObject *);
226
227 static PLyProcedure *PLy_procedure_get(FunctionCallInfo fcinfo,
228                                                                            Oid tgreloid);
229
230 static PLyProcedure *PLy_procedure_create(FunctionCallInfo fcinfo,
231                                          Oid tgreloid,
232                                          HeapTuple procTup, char *key);
233
234 static void PLy_procedure_compile(PLyProcedure *, const char *);
235 static char *PLy_procedure_munge_source(const char *, const char *);
236 static void PLy_procedure_delete(PLyProcedure *);
237
238 static void PLy_typeinfo_init(PLyTypeInfo *);
239 static void PLy_typeinfo_dealloc(PLyTypeInfo *);
240 static void PLy_output_datum_func(PLyTypeInfo *, HeapTuple);
241 static void PLy_output_datum_func2(PLyObToDatum *, HeapTuple);
242 static void PLy_input_datum_func(PLyTypeInfo *, Oid, HeapTuple);
243 static void PLy_input_datum_func2(PLyDatumToOb *, Oid, HeapTuple);
244 static void PLy_output_tuple_funcs(PLyTypeInfo *, TupleDesc);
245 static void PLy_input_tuple_funcs(PLyTypeInfo *, TupleDesc);
246
247 /* conversion functions
248  */
249 static PyObject *PLyDict_FromTuple(PLyTypeInfo *, HeapTuple, TupleDesc);
250 static PyObject *PLyBool_FromString(const char *);
251 static PyObject *PLyFloat_FromString(const char *);
252 static PyObject *PLyInt_FromString(const char *);
253 static PyObject *PLyLong_FromString(const char *);
254 static PyObject *PLyString_FromString(const char *);
255
256
257 /* global data
258  */
259 static int      PLy_first_call = 1;
260
261 /*
262  * Last function called by postgres backend
263  *
264  * XXX replace this with errcontext mechanism
265  */
266 static PLyProcedure *PLy_last_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  */
287 static PyObject *PLy_exc_error = NULL;
288 static PyObject *PLy_exc_fatal = NULL;
289 static PyObject *PLy_exc_spi_error = NULL;
290
291 /* some globals for the python module
292  */
293 static char PLy_plan_doc[] = {
294         "Store a PostgreSQL plan"
295 };
296
297 static char PLy_result_doc[] = {
298         "Results of a PostgreSQL query"
299 };
300
301
302 /*
303  * the function definitions
304  */
305
306 /*
307  * This routine is a crock, and so is everyplace that calls it.  The problem
308  * is that the cached form of plpython functions/queries is allocated permanently
309  * (mostly via malloc()) and never released until backend exit.  Subsidiary
310  * data structures such as fmgr info records therefore must live forever
311  * as well.  A better implementation would store all this stuff in a per-
312  * function memory context that could be reclaimed at need.  In the meantime,
313  * fmgr_info_cxt must be called specifying TopMemoryContext so that whatever
314  * it might allocate, and whatever the eventual function might allocate using
315  * fn_mcxt, will live forever too.
316  */
317 static void
318 perm_fmgr_info(Oid functionId, FmgrInfo *finfo)
319 {
320         fmgr_info_cxt(functionId, finfo, TopMemoryContext);
321 }
322
323 Datum
324 plpython_call_handler(PG_FUNCTION_ARGS)
325 {
326         Datum           retval;
327         PLyProcedure *volatile proc = NULL;
328
329         PLy_init_all();
330
331         if (SPI_connect() != SPI_OK_CONNECT)
332                 elog(ERROR, "could not connect to SPI manager");
333
334         PG_TRY();
335         {
336                 if (CALLED_AS_TRIGGER(fcinfo))
337                 {
338                         TriggerData *tdata = (TriggerData *) fcinfo->context;
339                         HeapTuple       trv;
340
341                         proc = PLy_procedure_get(fcinfo,
342                                                                          RelationGetRelid(tdata->tg_relation));
343                         trv = PLy_trigger_handler(fcinfo, proc);
344                         retval = PointerGetDatum(trv);
345                 }
346                 else
347                 {
348                         proc = PLy_procedure_get(fcinfo, InvalidOid);
349                         retval = PLy_function_handler(fcinfo, proc);
350                 }
351         }
352         PG_CATCH();
353         {
354                 if (proc)
355                 {
356                         /* note: Py_DECREF needs braces around it, as of 2003/08 */
357                         Py_DECREF(proc->me);
358                 }
359                 PyErr_Clear();
360                 PG_RE_THROW();
361         }
362         PG_END_TRY();
363
364         Py_DECREF(proc->me);
365
366         return retval;
367 }
368
369 /* trigger and function sub handlers
370  *
371  * the python function is expected to return Py_None if the tuple is
372  * acceptable and unmodified.  Otherwise it should return a PyString
373  * object who's value is SKIP, or MODIFY.  SKIP means don't perform
374  * this action.  MODIFY means the tuple has been modified, so update
375  * tuple and perform action.  SKIP and MODIFY assume the trigger fires
376  * BEFORE the event and is ROW level.  postgres expects the function
377  * to take no arguments and return an argument of type trigger.
378  */
379 static HeapTuple
380 PLy_trigger_handler(FunctionCallInfo fcinfo, PLyProcedure * proc)
381 {
382         HeapTuple       rv = NULL;
383         PyObject   *volatile plargs = NULL;
384         PyObject   *volatile plrv = NULL;
385
386         PG_TRY();
387         {
388         plargs = PLy_trigger_build_args(fcinfo, proc, &rv);
389         plrv = PLy_procedure_call(proc, "TD", plargs);
390
391         Assert(plrv != NULL);
392         Assert(!PLy_error_in_progress);
393
394         /*
395          * Disconnect from SPI manager
396          */
397         if (SPI_finish() != SPI_OK_FINISH)
398                 elog(ERROR, "SPI_finish failed");
399
400         /*
401          * return of None means we're happy with the tuple
402          */
403         if (plrv != Py_None)
404         {
405                 char       *srv;
406
407                 if (!PyString_Check(plrv))
408                         elog(ERROR, "expected trigger to return None or a String");
409
410                 srv = PyString_AsString(plrv);
411                 if (pg_strcasecmp(srv, "SKIP") == 0)
412                         rv = NULL;
413                 else if (pg_strcasecmp(srv, "MODIFY") == 0)
414                 {
415                         TriggerData *tdata = (TriggerData *) fcinfo->context;
416
417                         if ((TRIGGER_FIRED_BY_INSERT(tdata->tg_event)) ||
418                                 (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event)))
419                                 rv = PLy_modify_tuple(proc, plargs, tdata, rv);
420                         else
421                                 elog(WARNING, "ignoring modified tuple in DELETE trigger");
422                 }
423                 else if (pg_strcasecmp(srv, "OK") != 0)
424                 {
425                         /*
426                          * hmmm, perhaps they only read the pltcl page, not a
427                          * surprising thing since i've written no documentation, so
428                          * accept a belated OK
429                          */
430                         elog(ERROR, "expected return to be \"SKIP\" or \"MODIFY\"");
431                 }
432         }
433         }
434         PG_CATCH();
435         {
436                 Py_XDECREF(plargs);
437                 Py_XDECREF(plrv);
438
439                 PG_RE_THROW();
440         }
441         PG_END_TRY();
442
443         Py_DECREF(plargs);
444         Py_DECREF(plrv);
445
446         return rv;
447 }
448
449 static HeapTuple
450 PLy_modify_tuple(PLyProcedure * proc, PyObject * pltd, TriggerData *tdata,
451                                  HeapTuple otup)
452 {
453         PyObject   *volatile plntup;
454         PyObject   *volatile plkeys;
455         PyObject   *volatile platt;
456         PyObject   *volatile plval;
457         PyObject   *volatile plstr;
458         HeapTuple       rtup;
459         int                     natts,
460                                 i,
461                                 attn,
462                                 atti;
463         int                *volatile modattrs;
464         Datum      *volatile modvalues;
465         char       *volatile modnulls;
466         TupleDesc       tupdesc;
467
468         plntup = plkeys = platt = plval = plstr = NULL;
469         modattrs = NULL;
470         modvalues = NULL;
471         modnulls = NULL;
472
473         PG_TRY();
474         {
475         if ((plntup = PyDict_GetItemString(pltd, "new")) == NULL)
476                 elog(ERROR, "TD[\"new\"] deleted, unable to modify tuple");
477         if (!PyDict_Check(plntup))
478                 elog(ERROR, "TD[\"new\"] is not a dictionary object");
479         Py_INCREF(plntup);
480
481         plkeys = PyDict_Keys(plntup);
482         natts = PyList_Size(plkeys);
483
484         modattrs = (int *) palloc(natts * sizeof(int));
485         modvalues = (Datum *) palloc(natts * sizeof(Datum));
486         modnulls = (char *) palloc(natts * sizeof(char));
487
488         tupdesc = tdata->tg_relation->rd_att;
489
490         for (i = 0; i < natts; i++)
491         {
492                 char       *src;
493
494                 platt = PyList_GetItem(plkeys, i);
495                 if (!PyString_Check(platt))
496                         elog(ERROR, "attribute name is not a string");
497                 attn = SPI_fnumber(tupdesc, PyString_AsString(platt));
498                 if (attn == SPI_ERROR_NOATTRIBUTE)
499                         elog(ERROR, "invalid attribute \"%s\" in tuple",
500                                  PyString_AsString(platt));
501                 atti = attn - 1;
502
503                 plval = PyDict_GetItem(plntup, platt);
504                 if (plval == NULL)
505                         elog(FATAL, "python interpreter is probably corrupted");
506
507                 Py_INCREF(plval);
508
509                 modattrs[i] = attn;
510
511                 if (plval != Py_None && !tupdesc->attrs[atti]->attisdropped)
512                 {
513                         plstr = PyObject_Str(plval);
514                         src = PyString_AsString(plstr);
515
516                         modvalues[i] = FunctionCall3(&proc->result.out.r.atts[atti].typfunc,
517                                                                                  CStringGetDatum(src),
518                                  ObjectIdGetDatum(proc->result.out.r.atts[atti].typioparam),
519                                                  Int32GetDatum(tupdesc->attrs[atti]->atttypmod));
520                         modnulls[i] = ' ';
521
522                         Py_DECREF(plstr);
523                         plstr = NULL;
524                 }
525                 else
526                 {
527                         modvalues[i] = (Datum) 0;
528                         modnulls[i] = 'n';
529                 }
530
531                 Py_DECREF(plval);
532                 plval = NULL;
533         }
534
535         rtup = SPI_modifytuple(tdata->tg_relation, otup, natts,
536                                                    modattrs, modvalues, modnulls);
537         if (rtup == NULL)
538                 elog(ERROR, "SPI_modifytuple failed -- error %d", SPI_result);
539         }
540         PG_CATCH();
541         {
542                 Py_XDECREF(plntup);
543                 Py_XDECREF(plkeys);
544                 Py_XDECREF(platt);
545                 Py_XDECREF(plval);
546                 Py_XDECREF(plstr);
547
548                 if (modnulls)
549                         pfree(modnulls);
550                 if (modvalues)
551                         pfree(modvalues);
552                 if (modattrs)
553                         pfree(modattrs);
554
555                 PG_RE_THROW();
556         }
557         PG_END_TRY();
558
559         Py_DECREF(plntup);
560         Py_DECREF(plkeys);
561
562         pfree(modattrs);
563         pfree(modvalues);
564         pfree(modnulls);
565
566         return rtup;
567 }
568
569 static PyObject *
570 PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure * proc, HeapTuple *rv)
571 {
572         TriggerData *tdata = (TriggerData *) fcinfo->context;
573         PyObject   *pltname,
574                            *pltevent,
575                            *pltwhen,
576                            *pltlevel,
577                            *pltrelid;
578         PyObject   *pltargs,
579                            *pytnew,
580                            *pytold;
581         PyObject   *volatile pltdata = NULL;
582         char       *stroid;
583
584         PG_TRY();
585         {
586         pltdata = PyDict_New();
587         if (!pltdata)
588                 PLy_elog(ERROR, "could not build arguments for trigger procedure");
589
590         pltname = PyString_FromString(tdata->tg_trigger->tgname);
591         PyDict_SetItemString(pltdata, "name", pltname);
592         Py_DECREF(pltname);
593
594         stroid = DatumGetCString(DirectFunctionCall1(oidout,
595                                                    ObjectIdGetDatum(tdata->tg_relation->rd_id)));
596         pltrelid = PyString_FromString(stroid);
597         PyDict_SetItemString(pltdata, "relid", pltrelid);
598         Py_DECREF(pltrelid);
599         pfree(stroid);
600
601         if (TRIGGER_FIRED_BEFORE(tdata->tg_event))
602                 pltwhen = PyString_FromString("BEFORE");
603         else if (TRIGGER_FIRED_AFTER(tdata->tg_event))
604                 pltwhen = PyString_FromString("AFTER");
605         else
606         {
607                 elog(ERROR, "unrecognized WHEN tg_event: %u", tdata->tg_event);
608                 pltwhen = NULL;                 /* keep compiler quiet */
609         }
610         PyDict_SetItemString(pltdata, "when", pltwhen);
611         Py_DECREF(pltwhen);
612
613         if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
614         {
615                 pltlevel = PyString_FromString("ROW");
616                 PyDict_SetItemString(pltdata, "level", pltlevel);
617                 Py_DECREF(pltlevel);
618
619                 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
620                 {
621                         pltevent = PyString_FromString("INSERT");
622
623                         PyDict_SetItemString(pltdata, "old", Py_None);
624                         pytnew = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
625                                                                            tdata->tg_relation->rd_att);
626                         PyDict_SetItemString(pltdata, "new", pytnew);
627                         Py_DECREF(pytnew);
628                         *rv = tdata->tg_trigtuple;
629                 }
630                 else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
631                 {
632                         pltevent = PyString_FromString("DELETE");
633
634                         PyDict_SetItemString(pltdata, "new", Py_None);
635                         pytold = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
636                                                                            tdata->tg_relation->rd_att);
637                         PyDict_SetItemString(pltdata, "old", pytold);
638                         Py_DECREF(pytold);
639                         *rv = tdata->tg_trigtuple;
640                 }
641                 else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
642                 {
643                         pltevent = PyString_FromString("UPDATE");
644
645                         pytnew = PLyDict_FromTuple(&(proc->result), tdata->tg_newtuple,
646                                                                            tdata->tg_relation->rd_att);
647                         PyDict_SetItemString(pltdata, "new", pytnew);
648                         Py_DECREF(pytnew);
649                         pytold = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
650                                                                            tdata->tg_relation->rd_att);
651                         PyDict_SetItemString(pltdata, "old", pytold);
652                         Py_DECREF(pytold);
653                         *rv = tdata->tg_newtuple;
654                 }
655                 else
656                 {
657                         elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
658                         pltevent = NULL;        /* keep compiler quiet */
659                 }
660
661                 PyDict_SetItemString(pltdata, "event", pltevent);
662                 Py_DECREF(pltevent);
663         }
664         else if (TRIGGER_FIRED_FOR_STATEMENT(tdata->tg_event))
665         {
666                 pltlevel = PyString_FromString("STATEMENT");
667                 PyDict_SetItemString(pltdata, "level", pltlevel);
668                 Py_DECREF(pltlevel);
669
670                 PyDict_SetItemString(pltdata, "old", Py_None);
671                 PyDict_SetItemString(pltdata, "new", Py_None);
672                 *rv = NULL;
673
674                 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
675                         pltevent = PyString_FromString("INSERT");
676                 else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
677                         pltevent = PyString_FromString("DELETE");
678                 else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
679                         pltevent = PyString_FromString("UPDATE");
680                 else
681                 {
682                         elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
683                         pltevent = NULL;        /* keep compiler quiet */
684                 }
685
686                 PyDict_SetItemString(pltdata, "event", pltevent);
687                 Py_DECREF(pltevent);
688         }
689         else
690                 elog(ERROR, "unrecognized LEVEL tg_event: %u", tdata->tg_event);
691
692         if (tdata->tg_trigger->tgnargs)
693         {
694                 /*
695                  * all strings...
696                  */
697                 int                     i;
698                 PyObject   *pltarg;
699
700                 pltargs = PyList_New(tdata->tg_trigger->tgnargs);
701                 for (i = 0; i < tdata->tg_trigger->tgnargs; i++)
702                 {
703                         pltarg = PyString_FromString(tdata->tg_trigger->tgargs[i]);
704
705                         /*
706                          * stolen, don't Py_DECREF
707                          */
708                         PyList_SetItem(pltargs, i, pltarg);
709                 }
710         }
711         else
712         {
713                 Py_INCREF(Py_None);
714                 pltargs = Py_None;
715         }
716         PyDict_SetItemString(pltdata, "args", pltargs);
717         Py_DECREF(pltargs);
718         }
719         PG_CATCH();
720         {
721                 Py_XDECREF(pltdata);
722                 PG_RE_THROW();
723         }
724         PG_END_TRY();
725
726         return pltdata;
727 }
728
729
730
731 /* function handler and friends
732  */
733 static Datum
734 PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure * proc)
735 {
736         Datum           rv;
737         PyObject   *volatile plargs = NULL;
738         PyObject   *volatile plrv = NULL;
739         PyObject   *volatile plrv_so = NULL;
740         char       *plrv_sc;
741
742         PG_TRY();
743         {
744         plargs = PLy_function_build_args(fcinfo, proc);
745         plrv = PLy_procedure_call(proc, "args", plargs);
746
747         Assert(plrv != NULL);
748         Assert(!PLy_error_in_progress);
749
750         /*
751          * Disconnect from SPI manager and then create the return values datum
752          * (if the input function does a palloc for it this must not be
753          * allocated in the SPI memory context because SPI_finish would free
754          * it).
755          */
756         if (SPI_finish() != SPI_OK_FINISH)
757                 elog(ERROR, "SPI_finish failed");
758
759         /*
760          * convert the python PyObject to a postgresql Datum
761          */
762         if (plrv == Py_None)
763         {
764                 fcinfo->isnull = true;
765                 rv = (Datum) NULL;
766         }
767         else
768         {
769                 fcinfo->isnull = false;
770                 plrv_so = PyObject_Str(plrv);
771                 plrv_sc = PyString_AsString(plrv_so);
772                 rv = FunctionCall3(&proc->result.out.d.typfunc,
773                                                    PointerGetDatum(plrv_sc),
774                                                    ObjectIdGetDatum(proc->result.out.d.typioparam),
775                                                    Int32GetDatum(-1));
776         }
777
778         }
779         PG_CATCH();
780         {
781                 Py_XDECREF(plargs);
782                 Py_XDECREF(plrv);
783                 Py_XDECREF(plrv_so);
784
785                 PG_RE_THROW();
786         }
787         PG_END_TRY();
788
789         Py_XDECREF(plargs);
790         Py_DECREF(plrv);
791         Py_XDECREF(plrv_so);
792
793         return rv;
794 }
795
796 static PyObject *
797 PLy_procedure_call(PLyProcedure * proc, char *kargs, PyObject * vargs)
798 {
799         PyObject   *rv;
800         PLyProcedure *current;
801
802         current = PLy_last_procedure;
803         PLy_last_procedure = proc;
804         PyDict_SetItemString(proc->globals, kargs, vargs);
805         rv = PyEval_EvalCode((PyCodeObject *) proc->code,
806                                                  proc->globals, proc->globals);
807         PLy_last_procedure = current;
808
809         /*
810          * If there was an error in a PG callback, propagate that
811          * no matter what Python claims about its success.
812          */
813         if (PLy_error_in_progress)
814         {
815                 ErrorData *edata = PLy_error_in_progress;
816
817                 PLy_error_in_progress = NULL;
818                 ReThrowError(edata);
819         }
820
821         if ((rv == NULL) || (PyErr_Occurred()))
822         {
823                 Py_XDECREF(rv);
824                 PLy_elog(ERROR, "function \"%s\" failed", proc->proname);
825         }
826
827         return rv;
828 }
829
830 static PyObject *
831 PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure * proc)
832 {
833         PyObject   *volatile arg = NULL;
834         PyObject   *volatile args = NULL;
835         int                     i;
836
837         PG_TRY();
838         {
839         args = PyList_New(proc->nargs);
840         for (i = 0; i < proc->nargs; i++)
841         {
842                 if (proc->args[i].is_rowtype > 0)
843                 {
844                         if (fcinfo->argnull[i])
845                                 arg = NULL;
846                         else
847                         {
848                                 HeapTupleHeader td;
849                                 Oid                     tupType;
850                                 int32           tupTypmod;
851                                 TupleDesc       tupdesc;
852                                 HeapTupleData tmptup;
853
854                                 td = DatumGetHeapTupleHeader(fcinfo->arg[i]);
855                                 /* Extract rowtype info and find a tupdesc */
856                                 tupType = HeapTupleHeaderGetTypeId(td);
857                                 tupTypmod = HeapTupleHeaderGetTypMod(td);
858                                 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
859
860                                 /* Set up I/O funcs if not done yet */
861                                 if (proc->args[i].is_rowtype != 1)
862                                         PLy_input_tuple_funcs(&(proc->args[i]), tupdesc);
863
864                                 /* Build a temporary HeapTuple control structure */
865                                 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
866                                 tmptup.t_data = td;
867
868                                 arg = PLyDict_FromTuple(&(proc->args[i]), &tmptup, tupdesc);
869                         }
870                 }
871                 else
872                 {
873                         if (fcinfo->argnull[i])
874                                 arg = NULL;
875                         else
876                         {
877                                 char       *ct;
878                                 Datum           dt;
879
880                                 dt = FunctionCall3(&(proc->args[i].in.d.typfunc),
881                                                                    fcinfo->arg[i],
882                                                         ObjectIdGetDatum(proc->args[i].in.d.typioparam),
883                                                                    Int32GetDatum(-1));
884                                 ct = DatumGetCString(dt);
885                                 arg = (proc->args[i].in.d.func) (ct);
886                                 pfree(ct);
887                         }
888                 }
889
890                 if (arg == NULL)
891                 {
892                         Py_INCREF(Py_None);
893                         arg = Py_None;
894                 }
895
896                 /*
897                  * FIXME -- error check this
898                  */
899                 PyList_SetItem(args, i, arg);
900         }
901         }
902         PG_CATCH();
903         {
904                 Py_XDECREF(arg);
905                 Py_XDECREF(args);
906
907                 PG_RE_THROW();
908         }
909         PG_END_TRY();
910
911         return args;
912 }
913
914
915 /*
916  * PLyProcedure functions
917  */
918
919 /* PLy_procedure_get: returns a cached PLyProcedure, or creates, stores and
920  * returns a new PLyProcedure.  fcinfo is the call info, tgreloid is the
921  * relation OID when calling a trigger, or InvalidOid (zero) for ordinary
922  * function calls.
923  */
924 static PLyProcedure *
925 PLy_procedure_get(FunctionCallInfo fcinfo, Oid tgreloid)
926 {
927         Oid                     fn_oid;
928         HeapTuple       procTup;
929         char            key[128];
930         PyObject   *plproc;
931         PLyProcedure *proc = NULL;
932         int                     rv;
933
934         fn_oid = fcinfo->flinfo->fn_oid;
935         procTup = SearchSysCache(PROCOID,
936                                                          ObjectIdGetDatum(fn_oid),
937                                                          0, 0, 0);
938         if (!HeapTupleIsValid(procTup))
939                 elog(ERROR, "cache lookup failed for function %u", fn_oid);
940
941         rv = snprintf(key, sizeof(key), "%u_%u", fn_oid, tgreloid);
942         if ((rv >= sizeof(key)) || (rv < 0))
943                 elog(ERROR, "key too long");
944
945         plproc = PyDict_GetItemString(PLy_procedure_cache, key);
946
947         if (plproc != NULL)
948         {
949                 Py_INCREF(plproc);
950                 if (!PyCObject_Check(plproc))
951                         elog(FATAL, "expected a PyCObject, didn't get one");
952
953                 proc = PyCObject_AsVoidPtr(plproc);
954                 if (proc->me != plproc)
955                         elog(FATAL, "proc->me != plproc");
956                 /* did we find an up-to-date cache entry? */
957                 if (proc->fn_xmin != HeapTupleHeaderGetXmin(procTup->t_data) ||
958                         proc->fn_cmin != HeapTupleHeaderGetCmin(procTup->t_data))
959                 {
960                         Py_DECREF(plproc);
961                         proc = NULL;
962                 }
963         }
964
965         if (proc == NULL)
966                 proc = PLy_procedure_create(fcinfo, tgreloid, procTup, key);
967
968         ReleaseSysCache(procTup);
969
970         return proc;
971 }
972
973 static PLyProcedure *
974 PLy_procedure_create(FunctionCallInfo fcinfo, Oid tgreloid,
975                                          HeapTuple procTup, char *key)
976 {
977         char            procName[NAMEDATALEN + 256];
978
979         Form_pg_proc procStruct;
980         PLyProcedure *volatile proc;
981         char       *volatile procSource = NULL;
982         Datum           prosrcdatum;
983         bool            isnull;
984         int                     i,
985                                 rv;
986
987         procStruct = (Form_pg_proc) GETSTRUCT(procTup);
988
989         if (OidIsValid(tgreloid))
990                 rv = snprintf(procName, sizeof(procName),
991                                           "__plpython_procedure_%s_%u_trigger_%u",
992                                           NameStr(procStruct->proname),
993                                           fcinfo->flinfo->fn_oid,
994                                           tgreloid);
995         else
996                 rv = snprintf(procName, sizeof(procName),
997                                           "__plpython_procedure_%s_%u",
998                                           NameStr(procStruct->proname),
999                                           fcinfo->flinfo->fn_oid);
1000         if ((rv >= sizeof(procName)) || (rv < 0))
1001                 elog(ERROR, "procedure name would overrun buffer");
1002
1003         proc = PLy_malloc(sizeof(PLyProcedure));
1004         proc->proname = PLy_malloc(strlen(NameStr(procStruct->proname)) + 1);
1005         strcpy(proc->proname, NameStr(procStruct->proname));
1006         proc->pyname = PLy_malloc(strlen(procName) + 1);
1007         strcpy(proc->pyname, procName);
1008         proc->fn_xmin = HeapTupleHeaderGetXmin(procTup->t_data);
1009         proc->fn_cmin = HeapTupleHeaderGetCmin(procTup->t_data);
1010         PLy_typeinfo_init(&proc->result);
1011         for (i = 0; i < FUNC_MAX_ARGS; i++)
1012                 PLy_typeinfo_init(&proc->args[i]);
1013         proc->nargs = 0;
1014         proc->code = proc->statics = NULL;
1015         proc->globals = proc->me = NULL;
1016
1017         PG_TRY();
1018         {
1019         /*
1020          * get information required for output conversion of the return value,
1021          * but only if this isn't a trigger.
1022          */
1023         if (!CALLED_AS_TRIGGER(fcinfo))
1024         {
1025                 HeapTuple       rvTypeTup;
1026                 Form_pg_type rvTypeStruct;
1027
1028                 rvTypeTup = SearchSysCache(TYPEOID,
1029                                                                 ObjectIdGetDatum(procStruct->prorettype),
1030                                                                    0, 0, 0);
1031                 if (!HeapTupleIsValid(rvTypeTup))
1032                         elog(ERROR, "cache lookup failed for type %u",
1033                                  procStruct->prorettype);
1034
1035                 rvTypeStruct = (Form_pg_type) GETSTRUCT(rvTypeTup);
1036                 if (rvTypeStruct->typtype != 'c')
1037                         PLy_output_datum_func(&proc->result, rvTypeTup);
1038                 else
1039                         ereport(ERROR,
1040                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1041                                          errmsg("tuple return types are not supported yet")));
1042
1043                 ReleaseSysCache(rvTypeTup);
1044         }
1045         else
1046         {
1047                 /*
1048                  * input/output conversion for trigger tuples.  use the result
1049                  * TypeInfo variable to store the tuple conversion info.
1050                  */
1051                 TriggerData *tdata = (TriggerData *) fcinfo->context;
1052
1053                 PLy_input_tuple_funcs(&(proc->result), tdata->tg_relation->rd_att);
1054                 PLy_output_tuple_funcs(&(proc->result), tdata->tg_relation->rd_att);
1055         }
1056
1057         /*
1058          * now get information required for input conversion of the procedures
1059          * arguments.
1060          */
1061         proc->nargs = fcinfo->nargs;
1062         for (i = 0; i < fcinfo->nargs; i++)
1063         {
1064                 HeapTuple       argTypeTup;
1065                 Form_pg_type argTypeStruct;
1066
1067                 argTypeTup = SearchSysCache(TYPEOID,
1068                                                         ObjectIdGetDatum(procStruct->proargtypes[i]),
1069                                                                         0, 0, 0);
1070                 if (!HeapTupleIsValid(argTypeTup))
1071                         elog(ERROR, "cache lookup failed for type %u",
1072                                  procStruct->proargtypes[i]);
1073                 argTypeStruct = (Form_pg_type) GETSTRUCT(argTypeTup);
1074
1075                 if (argTypeStruct->typtype != 'c')
1076                         PLy_input_datum_func(&(proc->args[i]),
1077                                                                  procStruct->proargtypes[i],
1078                                                                  argTypeTup);
1079                 else
1080                         proc->args[i].is_rowtype = 2; /* still need to set I/O funcs */
1081
1082                 ReleaseSysCache(argTypeTup);
1083         }
1084
1085
1086         /*
1087          * get the text of the function.
1088          */
1089         prosrcdatum = SysCacheGetAttr(PROCOID, procTup,
1090                                                                   Anum_pg_proc_prosrc, &isnull);
1091         if (isnull)
1092                 elog(ERROR, "null prosrc");
1093         procSource = DatumGetCString(DirectFunctionCall1(textout,
1094                                                                                                          prosrcdatum));
1095
1096         PLy_procedure_compile(proc, procSource);
1097
1098         pfree(procSource);
1099
1100         proc->me = PyCObject_FromVoidPtr(proc, NULL);
1101         PyDict_SetItemString(PLy_procedure_cache, key, proc->me);
1102         }
1103         PG_CATCH();
1104         {
1105                 PLy_procedure_delete(proc);
1106                 if (procSource)
1107                         pfree(procSource);
1108
1109                 PG_RE_THROW();
1110         }
1111         PG_END_TRY();
1112
1113         return proc;
1114 }
1115
1116 static void
1117 PLy_procedure_compile(PLyProcedure * proc, const char *src)
1118 {
1119         PyObject   *crv = NULL;
1120         char       *msrc;
1121
1122         proc->globals = PyDict_Copy(PLy_interp_globals);
1123
1124         /*
1125          * SD is private preserved data between calls GD is global data shared
1126          * by all functions
1127          */
1128         proc->statics = PyDict_New();
1129         PyDict_SetItemString(proc->globals, "SD", proc->statics);
1130
1131         /*
1132          * insert the function code into the interpreter
1133          */
1134         msrc = PLy_procedure_munge_source(proc->pyname, src);
1135         crv = PyRun_String(msrc, Py_file_input, proc->globals, NULL);
1136         free(msrc);
1137
1138         if ((crv != NULL) && (!PyErr_Occurred()))
1139         {
1140                 int                     clen;
1141                 char            call[NAMEDATALEN + 256];
1142
1143                 Py_DECREF(crv);
1144
1145                 /*
1146                  * compile a call to the function
1147                  */
1148                 clen = snprintf(call, sizeof(call), "%s()", proc->pyname);
1149                 if ((clen < 0) || (clen >= sizeof(call)))
1150                         elog(ERROR, "string would overflow buffer");
1151                 proc->code = Py_CompileString(call, "<string>", Py_eval_input);
1152                 if ((proc->code != NULL) && (!PyErr_Occurred()))
1153                         return;
1154         }
1155         else
1156                 Py_XDECREF(crv);
1157
1158         PLy_elog(ERROR, "could not compile function \"%s\"", proc->proname);
1159 }
1160
1161 static char *
1162 PLy_procedure_munge_source(const char *name, const char *src)
1163 {
1164         char       *mrc,
1165                            *mp;
1166         const char *sp;
1167         size_t          mlen,
1168                                 plen;
1169
1170         /*
1171          * room for function source and the def statement
1172          */
1173         mlen = (strlen(src) * 2) + strlen(name) + 16;
1174
1175         mrc = PLy_malloc(mlen);
1176         plen = snprintf(mrc, mlen, "def %s():\n\t", name);
1177         Assert(plen >= 0 && plen < mlen);
1178
1179         sp = src;
1180         mp = mrc + plen;
1181
1182         while (*sp != '\0')
1183         {
1184                 if (*sp == '\n')
1185                 {
1186                         *mp++ = *sp++;
1187                         *mp++ = '\t';
1188                 }
1189                 else
1190                         *mp++ = *sp++;
1191         }
1192         *mp++ = '\n';
1193         *mp++ = '\n';
1194         *mp = '\0';
1195
1196         if (mp > (mrc + mlen))
1197                 elog(FATAL, "buffer overrun in PLy_munge_source");
1198
1199         return mrc;
1200 }
1201
1202 static void
1203 PLy_procedure_delete(PLyProcedure * proc)
1204 {
1205         int                     i;
1206
1207         Py_XDECREF(proc->code);
1208         Py_XDECREF(proc->statics);
1209         Py_XDECREF(proc->globals);
1210         Py_XDECREF(proc->me);
1211         if (proc->proname)
1212                 PLy_free(proc->proname);
1213         if (proc->pyname)
1214                 PLy_free(proc->pyname);
1215         for (i = 0; i < proc->nargs; i++)
1216                 if (proc->args[i].is_rowtype == 1)
1217                 {
1218                         if (proc->args[i].in.r.atts)
1219                                 PLy_free(proc->args[i].in.r.atts);
1220                         if (proc->args[i].out.r.atts)
1221                                 PLy_free(proc->args[i].out.r.atts);
1222                 }
1223 }
1224
1225 /* conversion functions.  remember output from python is
1226  * input to postgresql, and vis versa.
1227  */
1228 static void
1229 PLy_input_tuple_funcs(PLyTypeInfo * arg, TupleDesc desc)
1230 {
1231         int                     i;
1232
1233         if (arg->is_rowtype == 0)
1234                 elog(ERROR, "PLyTypeInfo struct is initialized for a Datum");
1235
1236         arg->is_rowtype = 1;
1237         arg->in.r.natts = desc->natts;
1238         arg->in.r.atts = malloc(desc->natts * sizeof(PLyDatumToOb));
1239
1240         for (i = 0; i < desc->natts; i++)
1241         {
1242                 HeapTuple       typeTup;
1243
1244                 if (desc->attrs[i]->attisdropped)
1245                         continue;
1246
1247                 typeTup = SearchSysCache(TYPEOID,
1248                                                           ObjectIdGetDatum(desc->attrs[i]->atttypid),
1249                                                                  0, 0, 0);
1250                 if (!HeapTupleIsValid(typeTup))
1251                         elog(ERROR, "cache lookup failed for type %u",
1252                                  desc->attrs[i]->atttypid);
1253
1254                 PLy_input_datum_func2(&(arg->in.r.atts[i]),
1255                                                           desc->attrs[i]->atttypid,
1256                                                           typeTup);
1257
1258                 ReleaseSysCache(typeTup);
1259         }
1260 }
1261
1262 static void
1263 PLy_output_tuple_funcs(PLyTypeInfo * arg, TupleDesc desc)
1264 {
1265         int                     i;
1266
1267         if (arg->is_rowtype == 0)
1268                 elog(ERROR, "PLyTypeInfo struct is initialized for a Datum");
1269
1270         arg->is_rowtype = 1;
1271         arg->out.r.natts = desc->natts;
1272         arg->out.r.atts = malloc(desc->natts * sizeof(PLyDatumToOb));
1273
1274         for (i = 0; i < desc->natts; i++)
1275         {
1276                 HeapTuple       typeTup;
1277
1278                 if (desc->attrs[i]->attisdropped)
1279                         continue;
1280
1281                 typeTup = SearchSysCache(TYPEOID,
1282                                                           ObjectIdGetDatum(desc->attrs[i]->atttypid),
1283                                                                  0, 0, 0);
1284                 if (!HeapTupleIsValid(typeTup))
1285                         elog(ERROR, "cache lookup failed for type %u",
1286                                  desc->attrs[i]->atttypid);
1287
1288                 PLy_output_datum_func2(&(arg->out.r.atts[i]), typeTup);
1289
1290                 ReleaseSysCache(typeTup);
1291         }
1292 }
1293
1294 static void
1295 PLy_output_datum_func(PLyTypeInfo * arg, HeapTuple typeTup)
1296 {
1297         if (arg->is_rowtype > 0)
1298                 elog(ERROR, "PLyTypeInfo struct is initialized for a Tuple");
1299         arg->is_rowtype = 0;
1300         PLy_output_datum_func2(&(arg->out.d), typeTup);
1301 }
1302
1303 static void
1304 PLy_output_datum_func2(PLyObToDatum * arg, HeapTuple typeTup)
1305 {
1306         Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1307
1308         perm_fmgr_info(typeStruct->typinput, &arg->typfunc);
1309         arg->typioparam = getTypeIOParam(typeTup);
1310         arg->typbyval = typeStruct->typbyval;
1311 }
1312
1313 static void
1314 PLy_input_datum_func(PLyTypeInfo * arg, Oid typeOid, HeapTuple typeTup)
1315 {
1316         if (arg->is_rowtype > 0)
1317                 elog(ERROR, "PLyTypeInfo struct is initialized for Tuple");
1318         arg->is_rowtype = 0;
1319         PLy_input_datum_func2(&(arg->in.d), typeOid, typeTup);
1320 }
1321
1322 static void
1323 PLy_input_datum_func2(PLyDatumToOb * arg, Oid typeOid, HeapTuple typeTup)
1324 {
1325         Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1326
1327         /* Get the type's conversion information */
1328         perm_fmgr_info(typeStruct->typoutput, &arg->typfunc);
1329         arg->typioparam = getTypeIOParam(typeTup);
1330         arg->typbyval = typeStruct->typbyval;
1331
1332         /* Determine which kind of Python object we will convert to */
1333         switch (typeOid)
1334         {
1335                 case BOOLOID:
1336                         arg->func = PLyBool_FromString;
1337                         break;
1338                 case FLOAT4OID:
1339                 case FLOAT8OID:
1340                 case NUMERICOID:
1341                         arg->func = PLyFloat_FromString;
1342                         break;
1343                 case INT2OID:
1344                 case INT4OID:
1345                         arg->func = PLyInt_FromString;
1346                         break;
1347                 case INT8OID:
1348                         arg->func = PLyLong_FromString;
1349                         break;
1350                 default:
1351                         arg->func = PLyString_FromString;
1352                         break;
1353         }
1354 }
1355
1356 static void
1357 PLy_typeinfo_init(PLyTypeInfo * arg)
1358 {
1359         arg->is_rowtype = -1;
1360         arg->in.r.natts = arg->out.r.natts = 0;
1361         arg->in.r.atts = NULL;
1362         arg->out.r.atts = NULL;
1363 }
1364
1365 static void
1366 PLy_typeinfo_dealloc(PLyTypeInfo * arg)
1367 {
1368         if (arg->is_rowtype == 1)
1369         {
1370                 if (arg->in.r.atts)
1371                         PLy_free(arg->in.r.atts);
1372                 if (arg->out.r.atts)
1373                         PLy_free(arg->out.r.atts);
1374         }
1375 }
1376
1377 /* assumes that a bool is always returned as a 't' or 'f'
1378  */
1379 static PyObject *
1380 PLyBool_FromString(const char *src)
1381 {
1382         if (src[0] == 't')
1383                 return PyInt_FromLong(1);
1384         return PyInt_FromLong(0);
1385 }
1386
1387 static PyObject *
1388 PLyFloat_FromString(const char *src)
1389 {
1390         double          v;
1391         char       *eptr;
1392
1393         errno = 0;
1394         v = strtod(src, &eptr);
1395         if ((*eptr != '\0') || (errno))
1396                 return NULL;
1397         return PyFloat_FromDouble(v);
1398 }
1399
1400 static PyObject *
1401 PLyInt_FromString(const char *src)
1402 {
1403         long            v;
1404         char       *eptr;
1405
1406         errno = 0;
1407         v = strtol(src, &eptr, 0);
1408         if ((*eptr != '\0') || (errno))
1409                 return NULL;
1410         return PyInt_FromLong(v);
1411 }
1412
1413 static PyObject *
1414 PLyLong_FromString(const char *src)
1415 {
1416         return PyLong_FromString((char *) src, NULL, 0);
1417 }
1418
1419 static PyObject *
1420 PLyString_FromString(const char *src)
1421 {
1422         return PyString_FromString(src);
1423 }
1424
1425 static PyObject *
1426 PLyDict_FromTuple(PLyTypeInfo * info, HeapTuple tuple, TupleDesc desc)
1427 {
1428         PyObject   *volatile dict;
1429         int                     i;
1430
1431         if (info->is_rowtype != 1)
1432                 elog(ERROR, "PLyTypeInfo structure describes a datum");
1433
1434         dict = PyDict_New();
1435         if (dict == NULL)
1436                 PLy_elog(ERROR, "could not create tuple dictionary");
1437
1438         PG_TRY();
1439         {
1440         for (i = 0; i < info->in.r.natts; i++)
1441         {
1442                 char       *key,
1443                                    *vsrc;
1444                 Datum           vattr,
1445                                         vdat;
1446                 bool            is_null;
1447                 PyObject   *value;
1448
1449                 if (desc->attrs[i]->attisdropped)
1450                         continue;
1451
1452                 key = NameStr(desc->attrs[i]->attname);
1453                 vattr = heap_getattr(tuple, (i + 1), desc, &is_null);
1454
1455                 if ((is_null) || (info->in.r.atts[i].func == NULL))
1456                         PyDict_SetItemString(dict, key, Py_None);
1457                 else
1458                 {
1459                         vdat = FunctionCall3(&info->in.r.atts[i].typfunc,
1460                                                                  vattr,
1461                                                         ObjectIdGetDatum(info->in.r.atts[i].typioparam),
1462                                                            Int32GetDatum(desc->attrs[i]->atttypmod));
1463                         vsrc = DatumGetCString(vdat);
1464
1465                         /*
1466                          * no exceptions allowed
1467                          */
1468                         value = info->in.r.atts[i].func(vsrc);
1469                         pfree(vsrc);
1470                         PyDict_SetItemString(dict, key, value);
1471                         Py_DECREF(value);
1472                 }
1473         }
1474         }
1475         PG_CATCH();
1476         {
1477                 Py_DECREF(dict);
1478                 PG_RE_THROW();
1479         }
1480         PG_END_TRY();
1481
1482         return dict;
1483 }
1484
1485 /* initialization, some python variables function declared here
1486  */
1487
1488 /* interface to postgresql elog
1489  */
1490 static PyObject *PLy_debug(PyObject *, PyObject *);
1491 static PyObject *PLy_log(PyObject *, PyObject *);
1492 static PyObject *PLy_info(PyObject *, PyObject *);
1493 static PyObject *PLy_notice(PyObject *, PyObject *);
1494 static PyObject *PLy_warning(PyObject *, PyObject *);
1495 static PyObject *PLy_error(PyObject *, PyObject *);
1496 static PyObject *PLy_fatal(PyObject *, PyObject *);
1497
1498 /* PLyPlanObject, PLyResultObject and SPI interface
1499  */
1500 #define is_PLyPlanObject(x) ((x)->ob_type == &PLy_PlanType)
1501 static PyObject *PLy_plan_new(void);
1502 static void PLy_plan_dealloc(PyObject *);
1503 static PyObject *PLy_plan_getattr(PyObject *, char *);
1504 static PyObject *PLy_plan_status(PyObject *, PyObject *);
1505
1506 static PyObject *PLy_result_new(void);
1507 static void PLy_result_dealloc(PyObject *);
1508 static PyObject *PLy_result_getattr(PyObject *, char *);
1509
1510 #ifdef NOT_USED
1511 /* Appear to be unused */
1512 static PyObject *PLy_result_fetch(PyObject *, PyObject *);
1513 static PyObject *PLy_result_nrows(PyObject *, PyObject *);
1514 static PyObject *PLy_result_status(PyObject *, PyObject *);
1515 #endif
1516 static int      PLy_result_length(PyObject *);
1517 static PyObject *PLy_result_item(PyObject *, int);
1518 static PyObject *PLy_result_slice(PyObject *, int, int);
1519 static int      PLy_result_ass_item(PyObject *, int, PyObject *);
1520 static int      PLy_result_ass_slice(PyObject *, int, int, PyObject *);
1521
1522
1523 static PyObject *PLy_spi_prepare(PyObject *, PyObject *);
1524 static PyObject *PLy_spi_execute(PyObject *, PyObject *);
1525 static PyObject *PLy_spi_execute_query(char *query, int limit);
1526 static PyObject *PLy_spi_execute_plan(PyObject *, PyObject *, int);
1527 static PyObject *PLy_spi_execute_fetch_result(SPITupleTable *, int, int);
1528
1529
1530 static PyTypeObject PLy_PlanType = {
1531         PyObject_HEAD_INIT(NULL)
1532         0,                                                      /* ob_size */
1533         "PLyPlan",                                      /* tp_name */
1534         sizeof(PLyPlanObject),          /* tp_size */
1535         0,                                                      /* tp_itemsize */
1536
1537         /*
1538          * methods
1539          */
1540         (destructor) PLy_plan_dealloc,          /* tp_dealloc */
1541         0,                                                      /* tp_print */
1542         (getattrfunc) PLy_plan_getattr,         /* tp_getattr */
1543         0,                                                      /* tp_setattr */
1544         0,                                                      /* tp_compare */
1545         0,                                                      /* tp_repr */
1546         0,                                                      /* tp_as_number */
1547         0,                                                      /* tp_as_sequence */
1548         0,                                                      /* tp_as_mapping */
1549         0,                                                      /* tp_hash */
1550         0,                                                      /* tp_call */
1551         0,                                                      /* tp_str */
1552         0,                                                      /* tp_getattro */
1553         0,                                                      /* tp_setattro */
1554         0,                                                      /* tp_as_buffer */
1555         0,                                                      /* tp_xxx4 */
1556         PLy_plan_doc,                           /* tp_doc */
1557 };
1558
1559 static PyMethodDef PLy_plan_methods[] = {
1560         {"status", (PyCFunction) PLy_plan_status, METH_VARARGS, NULL},
1561         {NULL, NULL, 0, NULL}
1562 };
1563
1564
1565 static PySequenceMethods PLy_result_as_sequence = {
1566         (inquiry) PLy_result_length,    /* sq_length */
1567         (binaryfunc) 0,                         /* sq_concat */
1568         (intargfunc) 0,                         /* sq_repeat */
1569         (intargfunc) PLy_result_item,           /* sq_item */
1570         (intintargfunc) PLy_result_slice,       /* sq_slice */
1571         (intobjargproc) PLy_result_ass_item,            /* sq_ass_item */
1572         (intintobjargproc) PLy_result_ass_slice,        /* sq_ass_slice */
1573 };
1574
1575 static PyTypeObject PLy_ResultType = {
1576         PyObject_HEAD_INIT(NULL)
1577         0,                                                      /* ob_size */
1578         "PLyResult",                            /* tp_name */
1579         sizeof(PLyResultObject),        /* tp_size */
1580         0,                                                      /* tp_itemsize */
1581
1582         /*
1583          * methods
1584          */
1585         (destructor) PLy_result_dealloc,        /* tp_dealloc */
1586         0,                                                      /* tp_print */
1587         (getattrfunc) PLy_result_getattr,       /* tp_getattr */
1588         0,                                                      /* tp_setattr */
1589         0,                                                      /* tp_compare */
1590         0,                                                      /* tp_repr */
1591         0,                                                      /* tp_as_number */
1592         &PLy_result_as_sequence,        /* tp_as_sequence */
1593         0,                                                      /* tp_as_mapping */
1594         0,                                                      /* tp_hash */
1595         0,                                                      /* tp_call */
1596         0,                                                      /* tp_str */
1597         0,                                                      /* tp_getattro */
1598         0,                                                      /* tp_setattro */
1599         0,                                                      /* tp_as_buffer */
1600         0,                                                      /* tp_xxx4 */
1601         PLy_result_doc,                         /* tp_doc */
1602 };
1603
1604 #ifdef NOT_USED
1605 /* Appear to be unused */
1606 static PyMethodDef PLy_result_methods[] = {
1607         {"fetch", (PyCFunction) PLy_result_fetch, METH_VARARGS, NULL,},
1608         {"nrows", (PyCFunction) PLy_result_nrows, METH_VARARGS, NULL},
1609         {"status", (PyCFunction) PLy_result_status, METH_VARARGS, NULL},
1610         {NULL, NULL, 0, NULL}
1611 };
1612 #endif
1613
1614 static PyMethodDef PLy_methods[] = {
1615         /*
1616          * logging methods
1617          */
1618         {"debug", PLy_debug, METH_VARARGS, NULL},
1619         {"log", PLy_log, METH_VARARGS, NULL},
1620         {"info", PLy_info, METH_VARARGS, NULL},
1621         {"notice", PLy_notice, METH_VARARGS, NULL},
1622         {"warning", PLy_warning, METH_VARARGS, NULL},
1623         {"error", PLy_error, METH_VARARGS, NULL},
1624         {"fatal", PLy_fatal, METH_VARARGS, NULL},
1625
1626         /*
1627          * create a stored plan
1628          */
1629         {"prepare", PLy_spi_prepare, METH_VARARGS, NULL},
1630
1631         /*
1632          * execute a plan or query
1633          */
1634         {"execute", PLy_spi_execute, METH_VARARGS, NULL},
1635
1636         {NULL, NULL, 0, NULL}
1637 };
1638
1639
1640 /* plan object methods
1641  */
1642 static PyObject *
1643 PLy_plan_new(void)
1644 {
1645         PLyPlanObject *ob;
1646
1647         if ((ob = PyObject_NEW(PLyPlanObject, &PLy_PlanType)) == NULL)
1648                 return NULL;
1649
1650         ob->plan = NULL;
1651         ob->nargs = 0;
1652         ob->types = NULL;
1653         ob->args = NULL;
1654
1655         return (PyObject *) ob;
1656 }
1657
1658
1659 static void
1660 PLy_plan_dealloc(PyObject * arg)
1661 {
1662         PLyPlanObject *ob = (PLyPlanObject *) arg;
1663
1664         if (ob->plan)
1665                 SPI_freeplan(ob->plan);
1666         if (ob->types)
1667                 PLy_free(ob->types);
1668         if (ob->args)
1669         {
1670                 int                     i;
1671
1672                 for (i = 0; i < ob->nargs; i++)
1673                         PLy_typeinfo_dealloc(&ob->args[i]);
1674                 PLy_free(ob->args);
1675         }
1676
1677         PyMem_DEL(arg);
1678 }
1679
1680
1681 static PyObject *
1682 PLy_plan_getattr(PyObject * self, char *name)
1683 {
1684         return Py_FindMethod(PLy_plan_methods, self, name);
1685 }
1686
1687 static PyObject *
1688 PLy_plan_status(PyObject * self, PyObject * args)
1689 {
1690         if (PyArg_ParseTuple(args, ""))
1691         {
1692                 Py_INCREF(Py_True);
1693                 return Py_True;
1694                 /* return PyInt_FromLong(self->status); */
1695         }
1696         PyErr_SetString(PLy_exc_error, "plan.status() takes no arguments");
1697         return NULL;
1698 }
1699
1700
1701
1702 /* result object methods
1703  */
1704
1705 static PyObject *
1706 PLy_result_new(void)
1707 {
1708         PLyResultObject *ob;
1709
1710         if ((ob = PyObject_NEW(PLyResultObject, &PLy_ResultType)) == NULL)
1711                 return NULL;
1712
1713         /* ob->tuples = NULL; */
1714
1715         Py_INCREF(Py_None);
1716         ob->status = Py_None;
1717         ob->nrows = PyInt_FromLong(-1);
1718         ob->rows = PyList_New(0);
1719
1720         return (PyObject *) ob;
1721 }
1722
1723 static void
1724 PLy_result_dealloc(PyObject * arg)
1725 {
1726         PLyResultObject *ob = (PLyResultObject *) arg;
1727
1728         Py_XDECREF(ob->nrows);
1729         Py_XDECREF(ob->rows);
1730         Py_XDECREF(ob->status);
1731
1732         PyMem_DEL(ob);
1733 }
1734
1735 static PyObject *
1736 PLy_result_getattr(PyObject * self, char *attr)
1737 {
1738         return NULL;
1739 }
1740
1741 #ifdef NOT_USED
1742 /* Appear to be unused */
1743 static PyObject *
1744 PLy_result_fetch(PyObject * self, PyObject * args)
1745 {
1746         return NULL;
1747 }
1748
1749 static PyObject *
1750 PLy_result_nrows(PyObject * self, PyObject * args)
1751 {
1752         PLyResultObject *ob = (PLyResultObject *) self;
1753
1754         Py_INCREF(ob->nrows);
1755         return ob->nrows;
1756 }
1757
1758 static PyObject *
1759 PLy_result_status(PyObject * self, PyObject * args)
1760 {
1761         PLyResultObject *ob = (PLyResultObject *) self;
1762
1763         Py_INCREF(ob->status);
1764         return ob->status;
1765 }
1766 #endif
1767
1768 static int
1769 PLy_result_length(PyObject * arg)
1770 {
1771         PLyResultObject *ob = (PLyResultObject *) arg;
1772
1773         return PyList_Size(ob->rows);
1774 }
1775
1776 static PyObject *
1777 PLy_result_item(PyObject * arg, int idx)
1778 {
1779         PyObject   *rv;
1780         PLyResultObject *ob = (PLyResultObject *) arg;
1781
1782         rv = PyList_GetItem(ob->rows, idx);
1783         if (rv != NULL)
1784                 Py_INCREF(rv);
1785         return rv;
1786 }
1787
1788 static int
1789 PLy_result_ass_item(PyObject * arg, int idx, PyObject * item)
1790 {
1791         int                     rv;
1792         PLyResultObject *ob = (PLyResultObject *) arg;
1793
1794         Py_INCREF(item);
1795         rv = PyList_SetItem(ob->rows, idx, item);
1796         return rv;
1797 }
1798
1799 static PyObject *
1800 PLy_result_slice(PyObject * arg, int lidx, int hidx)
1801 {
1802         PyObject   *rv;
1803         PLyResultObject *ob = (PLyResultObject *) arg;
1804
1805         rv = PyList_GetSlice(ob->rows, lidx, hidx);
1806         if (rv == NULL)
1807                 return NULL;
1808         Py_INCREF(rv);
1809         return rv;
1810 }
1811
1812 static int
1813 PLy_result_ass_slice(PyObject * arg, int lidx, int hidx, PyObject * slice)
1814 {
1815         int                     rv;
1816         PLyResultObject *ob = (PLyResultObject *) arg;
1817
1818         rv = PyList_SetSlice(ob->rows, lidx, hidx, slice);
1819         return rv;
1820 }
1821
1822 /* SPI interface
1823  */
1824 static PyObject *
1825 PLy_spi_prepare(PyObject * self, PyObject * args)
1826 {
1827         PLyPlanObject *plan;
1828         PyObject   *list = NULL;
1829         PyObject   *volatile optr = NULL;
1830         char       *query;
1831         void       *tmpplan;
1832         MemoryContext oldcontext;
1833
1834         /* Can't execute more if we have an unhandled error */
1835         if (PLy_error_in_progress)
1836         {
1837                 PyErr_SetString(PLy_exc_error, "Transaction aborted.");
1838                 return NULL;
1839         }
1840
1841         if (!PyArg_ParseTuple(args, "s|O", &query, &list))
1842         {
1843                 PyErr_SetString(PLy_exc_spi_error,
1844                                                 "Invalid arguments for plpy.prepare()");
1845                 return NULL;
1846         }
1847
1848         if ((list) && (!PySequence_Check(list)))
1849         {
1850                 PyErr_SetString(PLy_exc_spi_error,
1851                                  "Second argument in plpy.prepare() must be a sequence");
1852                 return NULL;
1853         }
1854
1855         if ((plan = (PLyPlanObject *) PLy_plan_new()) == NULL)
1856                 return NULL;
1857
1858         oldcontext = CurrentMemoryContext;
1859         PG_TRY();
1860         {
1861         if (list != NULL)
1862         {
1863                 int                     nargs,
1864                                         i;
1865
1866                 nargs = PySequence_Length(list);
1867                 if (nargs > 0)
1868                 {
1869                         plan->nargs = nargs;
1870                         plan->types = PLy_malloc(sizeof(Oid) * nargs);
1871                         plan->values = PLy_malloc(sizeof(Datum) * nargs);
1872                         plan->args = PLy_malloc(sizeof(PLyTypeInfo) * nargs);
1873
1874                         /*
1875                          * the other loop might throw an exception, if PLyTypeInfo
1876                          * member isn't properly initialized the Py_DECREF(plan) will
1877                          * go boom
1878                          */
1879                         for (i = 0; i < nargs; i++)
1880                         {
1881                                 PLy_typeinfo_init(&plan->args[i]);
1882                                 plan->values[i] = (Datum) NULL;
1883                         }
1884
1885                         for (i = 0; i < nargs; i++)
1886                         {
1887                                 char       *sptr;
1888                                 HeapTuple       typeTup;
1889                                 Form_pg_type typeStruct;
1890
1891                                 optr = PySequence_GetItem(list, i);
1892                                 if (!PyString_Check(optr))
1893                                         elog(ERROR, "Type names must be strings.");
1894                                 sptr = PyString_AsString(optr);
1895                                 /* XXX should extend this to allow qualified type names */
1896                                 typeTup = typenameType(makeTypeName(sptr));
1897                                 Py_DECREF(optr);
1898                                 optr = NULL;    /* this is important */
1899
1900                                 plan->types[i] = HeapTupleGetOid(typeTup);
1901                                 typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1902                                 if (typeStruct->typtype != 'c')
1903                                         PLy_output_datum_func(&plan->args[i], typeTup);
1904                                 else
1905                                         elog(ERROR, "tuples not handled in plpy.prepare, yet.");
1906                                 ReleaseSysCache(typeTup);
1907                         }
1908                 }
1909         }
1910
1911         plan->plan = SPI_prepare(query, plan->nargs, plan->types);
1912         if (plan->plan == NULL)
1913                 elog(ERROR, "SPI_prepare failed: %s",
1914                          SPI_result_code_string(SPI_result));
1915
1916         /* transfer plan from procCxt to topCxt */
1917         tmpplan = plan->plan;
1918         plan->plan = SPI_saveplan(tmpplan);
1919         SPI_freeplan(tmpplan);
1920         if (plan->plan == NULL)
1921                 elog(ERROR, "SPI_saveplan failed: %s",
1922                          SPI_result_code_string(SPI_result));
1923         }
1924         PG_CATCH();
1925         {
1926                 MemoryContextSwitchTo(oldcontext);
1927                 PLy_error_in_progress = CopyErrorData();
1928                 FlushErrorState();
1929                 Py_DECREF(plan);
1930                 Py_XDECREF(optr);
1931                 if (!PyErr_Occurred())
1932                         PyErr_SetString(PLy_exc_spi_error,
1933                                                         "Unknown error in PLy_spi_prepare");
1934                 /* XXX this oughta be replaced with errcontext mechanism */
1935                 PLy_elog(WARNING, "in function %s:", PLy_procedure_name(PLy_last_procedure));
1936                 return NULL;
1937         }
1938         PG_END_TRY();
1939
1940         return (PyObject *) plan;
1941 }
1942
1943 /* execute(query="select * from foo", limit=5)
1944  * execute(plan=plan, values=(foo, bar), limit=5)
1945  */
1946 static PyObject *
1947 PLy_spi_execute(PyObject * self, PyObject * args)
1948 {
1949         char       *query;
1950         PyObject   *plan;
1951         PyObject   *list = NULL;
1952         int                     limit = 0;
1953
1954         /* Can't execute more if we have an unhandled error */
1955         if (PLy_error_in_progress)
1956         {
1957                 PyErr_SetString(PLy_exc_error, "Transaction aborted.");
1958                 return NULL;
1959         }
1960
1961         if (PyArg_ParseTuple(args, "s|i", &query, &limit))
1962                 return PLy_spi_execute_query(query, limit);
1963
1964         PyErr_Clear();
1965
1966         if ((PyArg_ParseTuple(args, "O|Oi", &plan, &list, &limit)) &&
1967                 (is_PLyPlanObject(plan)))
1968                 return PLy_spi_execute_plan(plan, list, limit);
1969
1970         PyErr_SetString(PLy_exc_error, "Expected a query or plan.");
1971         return NULL;
1972 }
1973
1974 static PyObject *
1975 PLy_spi_execute_plan(PyObject * ob, PyObject * list, int limit)
1976 {
1977         volatile int nargs;
1978         int                     i,
1979                                 rv;
1980         PLyPlanObject *plan;
1981         char       *nulls;
1982         MemoryContext oldcontext;
1983
1984         if (list != NULL)
1985         {
1986                 if ((!PySequence_Check(list)) || (PyString_Check(list)))
1987                 {
1988                         char       *msg = "plpy.execute() takes a sequence as its second argument";
1989
1990                         PyErr_SetString(PLy_exc_spi_error, msg);
1991                         return NULL;
1992                 }
1993                 nargs = PySequence_Length(list);
1994         }
1995         else
1996                 nargs = 0;
1997
1998         plan = (PLyPlanObject *) ob;
1999
2000         if (nargs != plan->nargs)
2001         {
2002                 char       *sv;
2003
2004                 PyObject   *so = PyObject_Str(list);
2005
2006                 sv = PyString_AsString(so);
2007                 PLy_exception_set(PLy_exc_spi_error,
2008                                                   "Expected sequence of %d arguments, got %d. %s",
2009                                                   plan->nargs, nargs, sv);
2010                 Py_DECREF(so);
2011
2012                 return NULL;
2013         }
2014
2015         oldcontext = CurrentMemoryContext;
2016         PG_TRY();
2017         {
2018         nulls = palloc(nargs * sizeof(char));
2019
2020         for (i = 0; i < nargs; i++)
2021         {
2022                 PyObject   *elem,
2023                                    *so;
2024                 char       *sv;
2025
2026                 elem = PySequence_GetItem(list, i);
2027                 if (elem != Py_None)
2028                 {
2029                         so = PyObject_Str(elem);
2030                         sv = PyString_AsString(so);
2031
2032                         /*
2033                          * FIXME -- if this elogs, we have Python reference leak
2034                          */
2035                         plan->values[i] =
2036                                 FunctionCall3(&(plan->args[i].out.d.typfunc),
2037                                                           CStringGetDatum(sv),
2038                                                           ObjectIdGetDatum(plan->args[i].out.d.typioparam),
2039                                                           Int32GetDatum(-1));
2040
2041                         Py_DECREF(so);
2042                         Py_DECREF(elem);
2043
2044                         nulls[i] = ' ';
2045                 }
2046                 else
2047                 {
2048                         Py_DECREF(elem);
2049                         plan->values[i] = (Datum) 0;
2050                         nulls[i] = 'n';
2051                 }
2052         }
2053
2054         rv = SPI_execp(plan->plan, plan->values, nulls, limit);
2055
2056         pfree(nulls);
2057         }
2058         PG_CATCH();
2059         {
2060                 MemoryContextSwitchTo(oldcontext);
2061                 PLy_error_in_progress = CopyErrorData();
2062                 FlushErrorState();
2063                 /*
2064                  * cleanup plan->values array
2065                  */
2066                 for (i = 0; i < nargs; i++)
2067                 {
2068                         if (!plan->args[i].out.d.typbyval &&
2069                                 (plan->values[i] != (Datum) NULL))
2070                         {
2071                                 pfree(DatumGetPointer(plan->values[i]));
2072                                 plan->values[i] = (Datum) NULL;
2073                         }
2074                 }
2075
2076                 if (!PyErr_Occurred())
2077                         PyErr_SetString(PLy_exc_error,
2078                                                         "Unknown error in PLy_spi_execute_plan");
2079                 PLy_elog(WARNING, "in function %s:", PLy_procedure_name(PLy_last_procedure));
2080                 return NULL;
2081         }
2082         PG_END_TRY();
2083
2084         for (i = 0; i < nargs; i++)
2085         {
2086                 if (!plan->args[i].out.d.typbyval &&
2087                         (plan->values[i] != (Datum) NULL))
2088                 {
2089                         pfree(DatumGetPointer(plan->values[i]));
2090                         plan->values[i] = (Datum) NULL;
2091                 }
2092         }
2093
2094         if (rv < 0)
2095         {
2096                 PLy_exception_set(PLy_exc_spi_error,
2097                                                   "SPI_execp failed: %s",
2098                                                   SPI_result_code_string(rv));
2099                 return NULL;
2100         }
2101
2102         return PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
2103 }
2104
2105 static PyObject *
2106 PLy_spi_execute_query(char *query, int limit)
2107 {
2108         int                     rv;
2109         MemoryContext oldcontext;
2110
2111         oldcontext = CurrentMemoryContext;
2112         PG_TRY();
2113         {
2114                 rv = SPI_exec(query, limit);
2115         }
2116         PG_CATCH();
2117         {
2118                 MemoryContextSwitchTo(oldcontext);
2119                 PLy_error_in_progress = CopyErrorData();
2120                 FlushErrorState();
2121                 if (!PyErr_Occurred())
2122                         PyErr_SetString(PLy_exc_spi_error,
2123                                                         "Unknown error in PLy_spi_execute_query");
2124                 PLy_elog(WARNING, "in function %s:", PLy_procedure_name(PLy_last_procedure));
2125                 return NULL;
2126         }
2127         PG_END_TRY();
2128
2129         if (rv < 0)
2130         {
2131                 PLy_exception_set(PLy_exc_spi_error,
2132                                                   "SPI_exec failed: %s",
2133                                                   SPI_result_code_string(rv));
2134                 return NULL;
2135         }
2136
2137         return PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
2138 }
2139
2140 static PyObject *
2141 PLy_spi_execute_fetch_result(SPITupleTable *tuptable, int rows, int status)
2142 {
2143         PLyResultObject *result;
2144         MemoryContext oldcontext;
2145
2146         result = (PLyResultObject *) PLy_result_new();
2147         Py_DECREF(result->status);
2148         result->status = PyInt_FromLong(status);
2149
2150         if (status == SPI_OK_UTILITY)
2151         {
2152                 Py_DECREF(result->nrows);
2153                 result->nrows = PyInt_FromLong(0);
2154         }
2155         else if (status != SPI_OK_SELECT)
2156         {
2157                 Py_DECREF(result->nrows);
2158                 result->nrows = PyInt_FromLong(rows);
2159         }
2160         else
2161         {
2162                 PLyTypeInfo args;
2163                 int                     i;
2164
2165                 PLy_typeinfo_init(&args);
2166                 Py_DECREF(result->nrows);
2167                 result->nrows = PyInt_FromLong(rows);
2168
2169                 oldcontext = CurrentMemoryContext;
2170                 PG_TRY();
2171                 {
2172                         if (rows)
2173                         {
2174                                 Py_DECREF(result->rows);
2175                                 result->rows = PyList_New(rows);
2176
2177                                 PLy_input_tuple_funcs(&args, tuptable->tupdesc);
2178                                 for (i = 0; i < rows; i++)
2179                                 {
2180                                         PyObject   *row = PLyDict_FromTuple(&args, tuptable->vals[i],
2181                                                                                                                 tuptable->tupdesc);
2182
2183                                         PyList_SetItem(result->rows, i, row);
2184                                 }
2185                                 PLy_typeinfo_dealloc(&args);
2186
2187                                 SPI_freetuptable(tuptable);
2188                         }
2189                 }
2190                 PG_CATCH();
2191                 {
2192                         MemoryContextSwitchTo(oldcontext);
2193                         PLy_error_in_progress = CopyErrorData();
2194                         FlushErrorState();
2195                         if (!PyErr_Occurred())
2196                                 PyErr_SetString(PLy_exc_error,
2197                                                                 "Unknown error in PLy_spi_execute_fetch_result");
2198                         Py_DECREF(result);
2199                         PLy_typeinfo_dealloc(&args);
2200                         return NULL;
2201                 }
2202                 PG_END_TRY();
2203         }
2204
2205         return (PyObject *) result;
2206 }
2207
2208
2209 /*
2210  * language handler and interpreter initialization
2211  */
2212
2213 /*
2214  * plpython_init()                      - Initialize everything that can be
2215  *                                                        safely initialized during postmaster
2216  *                                                        startup.
2217  *
2218  * DO NOT make this static --- it has to be callable by preload
2219  */
2220 void
2221 plpython_init(void)
2222 {
2223         static volatile int init_active = 0;
2224
2225         /* Do initialization only once */
2226         if (!PLy_first_call)
2227                 return;
2228
2229         if (init_active)
2230                 elog(FATAL, "initialization of language module failed");
2231         init_active = 1;
2232
2233         Py_Initialize();
2234         PLy_init_interp();
2235         PLy_init_plpy();
2236         if (PyErr_Occurred())
2237                 PLy_elog(FATAL, "untrapped error in initialization");
2238         PLy_procedure_cache = PyDict_New();
2239         if (PLy_procedure_cache == NULL)
2240                 PLy_elog(ERROR, "could not create procedure cache");
2241
2242         PLy_first_call = 0;
2243 }
2244
2245 static void
2246 PLy_init_all(void)
2247 {
2248         /* Execute postmaster-startup safe initialization */
2249         if (PLy_first_call)
2250                 plpython_init();
2251
2252         /*
2253          * Any other initialization that must be done each time a new backend
2254          * starts -- currently none
2255          */
2256
2257 }
2258
2259 static void
2260 PLy_init_interp(void)
2261 {
2262         PyObject   *mainmod;
2263
2264         mainmod = PyImport_AddModule("__main__");
2265         if ((mainmod == NULL) || (PyErr_Occurred()))
2266                 PLy_elog(ERROR, "could not import \"__main__\" module.");
2267         Py_INCREF(mainmod);
2268         PLy_interp_globals = PyModule_GetDict(mainmod);
2269         PLy_interp_safe_globals = PyDict_New();
2270         PyDict_SetItemString(PLy_interp_globals, "GD", PLy_interp_safe_globals);
2271         Py_DECREF(mainmod);
2272         if ((PLy_interp_globals == NULL) || (PyErr_Occurred()))
2273                 PLy_elog(ERROR, "could not initialize globals");
2274 }
2275
2276 static void
2277 PLy_init_plpy(void)
2278 {
2279         PyObject   *main_mod,
2280                            *main_dict,
2281                            *plpy_mod;
2282         PyObject   *plpy,
2283                            *plpy_dict;
2284
2285         /*
2286          * initialize plpy module
2287          */
2288         PLy_PlanType.ob_type = PLy_ResultType.ob_type = &PyType_Type;
2289         plpy = Py_InitModule("plpy", PLy_methods);
2290         plpy_dict = PyModule_GetDict(plpy);
2291
2292         /* PyDict_SetItemString(plpy, "PlanType", (PyObject *) &PLy_PlanType); */
2293
2294         PLy_exc_error = PyErr_NewException("plpy.Error", NULL, NULL);
2295         PLy_exc_fatal = PyErr_NewException("plpy.Fatal", NULL, NULL);
2296         PLy_exc_spi_error = PyErr_NewException("plpy.SPIError", NULL, NULL);
2297         PyDict_SetItemString(plpy_dict, "Error", PLy_exc_error);
2298         PyDict_SetItemString(plpy_dict, "Fatal", PLy_exc_fatal);
2299         PyDict_SetItemString(plpy_dict, "SPIError", PLy_exc_spi_error);
2300
2301         /*
2302          * initialize main module, and add plpy
2303          */
2304         main_mod = PyImport_AddModule("__main__");
2305         main_dict = PyModule_GetDict(main_mod);
2306         plpy_mod = PyImport_AddModule("plpy");
2307         PyDict_SetItemString(main_dict, "plpy", plpy_mod);
2308         if (PyErr_Occurred())
2309                 elog(ERROR, "could not init plpy");
2310 }
2311
2312 /* the python interface to the elog function
2313  * don't confuse these with PLy_elog
2314  */
2315 static PyObject *PLy_output(int, PyObject *, PyObject *);
2316
2317 static PyObject *
2318 PLy_debug(PyObject * self, PyObject * args)
2319 {
2320         return PLy_output(DEBUG2, self, args);
2321 }
2322
2323 static PyObject *
2324 PLy_log(PyObject * self, PyObject * args)
2325 {
2326         return PLy_output(LOG, self, args);
2327 }
2328
2329 static PyObject *
2330 PLy_info(PyObject * self, PyObject * args)
2331 {
2332         return PLy_output(INFO, self, args);
2333 }
2334
2335 static PyObject *
2336 PLy_notice(PyObject * self, PyObject * args)
2337 {
2338         return PLy_output(NOTICE, self, args);
2339 }
2340
2341 static PyObject *
2342 PLy_warning(PyObject * self, PyObject * args)
2343 {
2344         return PLy_output(WARNING, self, args);
2345 }
2346
2347 static PyObject *
2348 PLy_error(PyObject * self, PyObject * args)
2349 {
2350         return PLy_output(ERROR, self, args);
2351 }
2352
2353 static PyObject *
2354 PLy_fatal(PyObject * self, PyObject * args)
2355 {
2356         return PLy_output(FATAL, self, args);
2357 }
2358
2359
2360 static PyObject *
2361 PLy_output(volatile int level, PyObject * self, PyObject * args)
2362 {
2363         PyObject   *so;
2364         char       *volatile sv;
2365         MemoryContext oldcontext;
2366
2367         so = PyObject_Str(args);
2368         if ((so == NULL) || ((sv = PyString_AsString(so)) == NULL))
2369         {
2370                 level = ERROR;
2371                 sv = "Unable to parse error message in `plpy.elog'";
2372         }
2373
2374         oldcontext = CurrentMemoryContext;
2375         PG_TRY();
2376         {
2377                 elog(level, "%s", sv);
2378         }
2379         PG_CATCH();
2380         {
2381                 MemoryContextSwitchTo(oldcontext);
2382                 PLy_error_in_progress = CopyErrorData();
2383                 FlushErrorState();
2384                 Py_XDECREF(so);
2385                 /*
2386                  * returning NULL here causes the python interpreter to bail. when
2387                  * control passes back to PLy_procedure_call, we check for PG
2388                  * exceptions and re-throw the error.
2389                  */
2390                 PyErr_SetString(PLy_exc_error, sv);
2391                 return NULL;
2392         }
2393         PG_END_TRY();
2394
2395         Py_XDECREF(so);
2396
2397         /*
2398          * return a legal object so the interpreter will continue on its merry
2399          * way
2400          */
2401         Py_INCREF(Py_None);
2402         return Py_None;
2403 }
2404
2405
2406 /*
2407  * Get the last procedure name called by the backend ( the innermost,
2408  * If a plpython procedure call calls the backend and the backend calls
2409  * another plpython procedure )
2410  *
2411  * NB: this returns SQL name, not the internal Python procedure name
2412  */
2413
2414 static char *
2415 PLy_procedure_name(PLyProcedure * proc)
2416 {
2417         if (proc == NULL)
2418                 return "<unknown procedure>";
2419         return proc->proname;
2420 }
2421
2422 /* output a python traceback/exception via the postgresql elog
2423  * function.  not pretty.
2424  */
2425 static void
2426 PLy_exception_set(PyObject * exc, const char *fmt,...)
2427 {
2428         char            buf[1024];
2429         va_list         ap;
2430
2431         va_start(ap, fmt);
2432         vsnprintf(buf, sizeof(buf), fmt, ap);
2433         va_end(ap);
2434
2435         PyErr_SetString(exc, buf);
2436 }
2437
2438 /* Emit a PG error or notice, together with any available info about the
2439  * current Python error.  This should be used to propagate Python errors
2440  * into PG.
2441  */
2442 static void
2443 PLy_elog(int elevel, const char *fmt,...)
2444 {
2445         va_list         ap;
2446         char       *xmsg,
2447                            *emsg;
2448         int                     xlevel;
2449
2450         xmsg = PLy_traceback(&xlevel);
2451
2452         va_start(ap, fmt);
2453         emsg = PLy_vprintf(fmt, ap);
2454         va_end(ap);
2455
2456         PG_TRY();
2457         {
2458                 ereport(elevel,
2459                                 (errmsg("plpython: %s", emsg),
2460                                  (xmsg) ? errdetail("%s", xmsg) : 0));
2461         }
2462         PG_CATCH();
2463         {
2464                 PLy_free(emsg);
2465                 if (xmsg)
2466                         PLy_free(xmsg);
2467                 PG_RE_THROW();
2468         }
2469         PG_END_TRY();
2470
2471         PLy_free(emsg);
2472         if (xmsg)
2473                 PLy_free(xmsg);
2474 }
2475
2476 static char *
2477 PLy_traceback(int *xlevel)
2478 {
2479         PyObject   *e,
2480                            *v,
2481                            *tb;
2482         PyObject   *eob,
2483                            *vob = NULL;
2484         char       *vstr,
2485                            *estr,
2486                            *xstr = NULL;
2487
2488         /*
2489          * get the current exception
2490          */
2491         PyErr_Fetch(&e, &v, &tb);
2492
2493         /*
2494          * oops, no exception, return
2495          */
2496         if (e == NULL)
2497         {
2498                 *xlevel = WARNING;
2499                 return NULL;
2500         }
2501
2502         PyErr_NormalizeException(&e, &v, &tb);
2503
2504         eob = PyObject_Str(e);
2505         if ((v) && ((vob = PyObject_Str(v)) != NULL))
2506                 vstr = PyString_AsString(vob);
2507         else
2508                 vstr = "Unknown";
2509
2510         estr = PyString_AsString(eob);
2511         xstr = PLy_printf("%s: %s", estr, vstr);
2512
2513         Py_DECREF(eob);
2514         Py_XDECREF(vob);
2515
2516         /*
2517          * intuit an appropriate error level for based on the exception type
2518          */
2519         if ((PLy_exc_error) && (PyErr_GivenExceptionMatches(e, PLy_exc_error)))
2520                 *xlevel = ERROR;
2521         else if ((PLy_exc_fatal) && (PyErr_GivenExceptionMatches(e, PLy_exc_fatal)))
2522                 *xlevel = FATAL;
2523         else
2524                 *xlevel = ERROR;
2525
2526         return xstr;
2527 }
2528
2529 static char *
2530 PLy_printf(const char *fmt,...)
2531 {
2532         va_list         ap;
2533         char       *emsg;
2534
2535         va_start(ap, fmt);
2536         emsg = PLy_vprintf(fmt, ap);
2537         va_end(ap);
2538         return emsg;
2539 }
2540
2541 static char *
2542 PLy_vprintf(const char *fmt, va_list ap)
2543 {
2544         size_t          blen;
2545         int                     bchar,
2546                                 tries = 2;
2547         char       *buf;
2548
2549         blen = strlen(fmt) * 2;
2550         if (blen < 256)
2551                 blen = 256;
2552         buf = PLy_malloc(blen * sizeof(char));
2553
2554         while (1)
2555         {
2556                 bchar = vsnprintf(buf, blen, fmt, ap);
2557                 if ((bchar > 0) && (bchar < blen))
2558                         return buf;
2559                 if (tries-- <= 0)
2560                         break;
2561                 if (blen > 0)
2562                         blen = bchar + 1;
2563                 else
2564                         blen *= 2;
2565                 buf = PLy_realloc(buf, blen);
2566         }
2567         PLy_free(buf);
2568         return NULL;
2569 }
2570
2571 /* python module code
2572  */
2573
2574
2575 /* some dumb utility functions
2576  */
2577
2578 static void *
2579 PLy_malloc(size_t bytes)
2580 {
2581         void       *ptr = malloc(bytes);
2582
2583         if (ptr == NULL)
2584                 ereport(FATAL,
2585                                 (errcode(ERRCODE_OUT_OF_MEMORY),
2586                                  errmsg("out of memory")));
2587         return ptr;
2588 }
2589
2590 static void *
2591 PLy_realloc(void *optr, size_t bytes)
2592 {
2593         void       *nptr = realloc(optr, bytes);
2594
2595         if (nptr == NULL)
2596                 ereport(FATAL,
2597                                 (errcode(ERRCODE_OUT_OF_MEMORY),
2598                                  errmsg("out of memory")));
2599         return nptr;
2600 }
2601
2602 /* define this away
2603  */
2604 static void
2605 PLy_free(void *ptr)
2606 {
2607         free(ptr);
2608 }