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