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