LCOV - code coverage report
Current view: top level - src/pl/plpython - plpy_plpymodule.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 179 197 90.9 %
Date: 2019-11-13 23:06:49 Functions: 17 18 94.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * the plpy module
       3             :  *
       4             :  * src/pl/plpython/plpy_plpymodule.c
       5             :  */
       6             : 
       7             : #include "postgres.h"
       8             : 
       9             : #include "access/xact.h"
      10             : #include "mb/pg_wchar.h"
      11             : #include "utils/builtins.h"
      12             : #include "utils/snapmgr.h"
      13             : 
      14             : #include "plpython.h"
      15             : 
      16             : #include "plpy_plpymodule.h"
      17             : 
      18             : #include "plpy_cursorobject.h"
      19             : #include "plpy_elog.h"
      20             : #include "plpy_main.h"
      21             : #include "plpy_planobject.h"
      22             : #include "plpy_resultobject.h"
      23             : #include "plpy_spi.h"
      24             : #include "plpy_subxactobject.h"
      25             : 
      26             : 
      27             : HTAB       *PLy_spi_exceptions = NULL;
      28             : 
      29             : 
      30             : static void PLy_add_exceptions(PyObject *plpy);
      31             : static PyObject *PLy_create_exception(char *name,
      32             :                                       PyObject *base, PyObject *dict,
      33             :                                       const char *modname, PyObject *mod);
      34             : static void PLy_generate_spi_exceptions(PyObject *mod, PyObject *base);
      35             : 
      36             : /* module functions */
      37             : static PyObject *PLy_debug(PyObject *self, PyObject *args, PyObject *kw);
      38             : static PyObject *PLy_log(PyObject *self, PyObject *args, PyObject *kw);
      39             : static PyObject *PLy_info(PyObject *self, PyObject *args, PyObject *kw);
      40             : static PyObject *PLy_notice(PyObject *self, PyObject *args, PyObject *kw);
      41             : static PyObject *PLy_warning(PyObject *self, PyObject *args, PyObject *kw);
      42             : static PyObject *PLy_error(PyObject *self, PyObject *args, PyObject *kw);
      43             : static PyObject *PLy_fatal(PyObject *self, PyObject *args, PyObject *kw);
      44             : static PyObject *PLy_quote_literal(PyObject *self, PyObject *args);
      45             : static PyObject *PLy_quote_nullable(PyObject *self, PyObject *args);
      46             : static PyObject *PLy_quote_ident(PyObject *self, PyObject *args);
      47             : static PyObject *PLy_commit(PyObject *self, PyObject *args);
      48             : static PyObject *PLy_rollback(PyObject *self, PyObject *args);
      49             : 
      50             : 
      51             : /* A list of all known exceptions, generated from backend/utils/errcodes.txt */
      52             : typedef struct ExceptionMap
      53             : {
      54             :     char       *name;
      55             :     char       *classname;
      56             :     int         sqlstate;
      57             : } ExceptionMap;
      58             : 
      59             : static const ExceptionMap exception_map[] = {
      60             : #include "spiexceptions.h"
      61             :     {NULL, NULL, 0}
      62             : };
      63             : 
      64             : static PyMethodDef PLy_methods[] = {
      65             :     /*
      66             :      * logging methods
      67             :      */
      68             :     {"debug", (PyCFunction) PLy_debug, METH_VARARGS | METH_KEYWORDS, NULL},
      69             :     {"log", (PyCFunction) PLy_log, METH_VARARGS | METH_KEYWORDS, NULL},
      70             :     {"info", (PyCFunction) PLy_info, METH_VARARGS | METH_KEYWORDS, NULL},
      71             :     {"notice", (PyCFunction) PLy_notice, METH_VARARGS | METH_KEYWORDS, NULL},
      72             :     {"warning", (PyCFunction) PLy_warning, METH_VARARGS | METH_KEYWORDS, NULL},
      73             :     {"error", (PyCFunction) PLy_error, METH_VARARGS | METH_KEYWORDS, NULL},
      74             :     {"fatal", (PyCFunction) PLy_fatal, METH_VARARGS | METH_KEYWORDS, NULL},
      75             : 
      76             :     /*
      77             :      * create a stored plan
      78             :      */
      79             :     {"prepare", PLy_spi_prepare, METH_VARARGS, NULL},
      80             : 
      81             :     /*
      82             :      * execute a plan or query
      83             :      */
      84             :     {"execute", PLy_spi_execute, METH_VARARGS, NULL},
      85             : 
      86             :     /*
      87             :      * escaping strings
      88             :      */
      89             :     {"quote_literal", PLy_quote_literal, METH_VARARGS, NULL},
      90             :     {"quote_nullable", PLy_quote_nullable, METH_VARARGS, NULL},
      91             :     {"quote_ident", PLy_quote_ident, METH_VARARGS, NULL},
      92             : 
      93             :     /*
      94             :      * create the subtransaction context manager
      95             :      */
      96             :     {"subtransaction", PLy_subtransaction_new, METH_NOARGS, NULL},
      97             : 
      98             :     /*
      99             :      * create a cursor
     100             :      */
     101             :     {"cursor", PLy_cursor, METH_VARARGS, NULL},
     102             : 
     103             :     /*
     104             :      * transaction control
     105             :      */
     106             :     {"commit", PLy_commit, METH_NOARGS, NULL},
     107             :     {"rollback", PLy_rollback, METH_NOARGS, NULL},
     108             : 
     109             :     {NULL, NULL, 0, NULL}
     110             : };
     111             : 
     112             : static PyMethodDef PLy_exc_methods[] = {
     113             :     {NULL, NULL, 0, NULL}
     114             : };
     115             : 
     116             : #if PY_MAJOR_VERSION >= 3
     117             : static PyModuleDef PLy_module = {
     118             :     PyModuleDef_HEAD_INIT,
     119             :     .m_name = "plpy",
     120             :     .m_size = -1,
     121             :     .m_methods = PLy_methods,
     122             : };
     123             : 
     124             : static PyModuleDef PLy_exc_module = {
     125             :     PyModuleDef_HEAD_INIT,
     126             :     .m_name = "spiexceptions",
     127             :     .m_size = -1,
     128             :     .m_methods = PLy_exc_methods,
     129             : };
     130             : 
     131             : /*
     132             :  * Must have external linkage, because PyMODINIT_FUNC does dllexport on
     133             :  * Windows-like platforms.
     134             :  */
     135             : PyMODINIT_FUNC
     136             : PyInit_plpy(void)
     137             : {
     138             :     PyObject   *m;
     139             : 
     140             :     m = PyModule_Create(&PLy_module);
     141             :     if (m == NULL)
     142             :         return NULL;
     143             : 
     144             :     PLy_add_exceptions(m);
     145             : 
     146             :     return m;
     147             : }
     148             : #endif                          /* PY_MAJOR_VERSION >= 3 */
     149             : 
     150             : void
     151          46 : PLy_init_plpy(void)
     152             : {
     153             :     PyObject   *main_mod,
     154             :                *main_dict,
     155             :                *plpy_mod;
     156             : 
     157             : #if PY_MAJOR_VERSION < 3
     158             :     PyObject   *plpy;
     159             : #endif
     160             : 
     161             :     /*
     162             :      * initialize plpy module
     163             :      */
     164          46 :     PLy_plan_init_type();
     165          46 :     PLy_result_init_type();
     166          46 :     PLy_subtransaction_init_type();
     167          46 :     PLy_cursor_init_type();
     168             : 
     169             : #if PY_MAJOR_VERSION >= 3
     170             :     PyModule_Create(&PLy_module);
     171             :     /* for Python 3 we initialized the exceptions in PyInit_plpy */
     172             : #else
     173          46 :     plpy = Py_InitModule("plpy", PLy_methods);
     174          46 :     PLy_add_exceptions(plpy);
     175             : #endif
     176             : 
     177             :     /* PyDict_SetItemString(plpy, "PlanType", (PyObject *) &PLy_PlanType); */
     178             : 
     179             :     /*
     180             :      * initialize main module, and add plpy
     181             :      */
     182          46 :     main_mod = PyImport_AddModule("__main__");
     183          46 :     main_dict = PyModule_GetDict(main_mod);
     184          46 :     plpy_mod = PyImport_AddModule("plpy");
     185          46 :     if (plpy_mod == NULL)
     186           0 :         PLy_elog(ERROR, "could not import \"plpy\" module");
     187          46 :     PyDict_SetItemString(main_dict, "plpy", plpy_mod);
     188          46 :     if (PyErr_Occurred())
     189           0 :         PLy_elog(ERROR, "could not import \"plpy\" module");
     190          46 : }
     191             : 
     192             : static void
     193          46 : PLy_add_exceptions(PyObject *plpy)
     194             : {
     195             :     PyObject   *excmod;
     196             :     HASHCTL     hash_ctl;
     197             : 
     198             : #if PY_MAJOR_VERSION < 3
     199          46 :     excmod = Py_InitModule("spiexceptions", PLy_exc_methods);
     200             : #else
     201             :     excmod = PyModule_Create(&PLy_exc_module);
     202             : #endif
     203          46 :     if (excmod == NULL)
     204           0 :         PLy_elog(ERROR, "could not create the spiexceptions module");
     205             : 
     206             :     /*
     207             :      * PyModule_AddObject does not add a refcount to the object, for some odd
     208             :      * reason; we must do that.
     209             :      */
     210          46 :     Py_INCREF(excmod);
     211          46 :     if (PyModule_AddObject(plpy, "spiexceptions", excmod) < 0)
     212           0 :         PLy_elog(ERROR, "could not add the spiexceptions module");
     213             : 
     214          46 :     PLy_exc_error = PLy_create_exception("plpy.Error", NULL, NULL,
     215             :                                          "Error", plpy);
     216          46 :     PLy_exc_fatal = PLy_create_exception("plpy.Fatal", NULL, NULL,
     217             :                                          "Fatal", plpy);
     218          46 :     PLy_exc_spi_error = PLy_create_exception("plpy.SPIError", NULL, NULL,
     219             :                                              "SPIError", plpy);
     220             : 
     221          46 :     memset(&hash_ctl, 0, sizeof(hash_ctl));
     222          46 :     hash_ctl.keysize = sizeof(int);
     223          46 :     hash_ctl.entrysize = sizeof(PLyExceptionEntry);
     224          46 :     PLy_spi_exceptions = hash_create("PL/Python SPI exceptions", 256,
     225             :                                      &hash_ctl, HASH_ELEM | HASH_BLOBS);
     226             : 
     227          46 :     PLy_generate_spi_exceptions(excmod, PLy_exc_spi_error);
     228          46 : }
     229             : 
     230             : /*
     231             :  * Create an exception object and add it to the module
     232             :  */
     233             : static PyObject *
     234       11500 : PLy_create_exception(char *name, PyObject *base, PyObject *dict,
     235             :                      const char *modname, PyObject *mod)
     236             : {
     237             :     PyObject   *exc;
     238             : 
     239       11500 :     exc = PyErr_NewException(name, base, dict);
     240       11500 :     if (exc == NULL)
     241           0 :         PLy_elog(ERROR, NULL);
     242             : 
     243             :     /*
     244             :      * PyModule_AddObject does not add a refcount to the object, for some odd
     245             :      * reason; we must do that.
     246             :      */
     247       11500 :     Py_INCREF(exc);
     248       11500 :     PyModule_AddObject(mod, modname, exc);
     249             : 
     250             :     /*
     251             :      * The caller will also store a pointer to the exception object in some
     252             :      * permanent variable, so add another ref to account for that.  This is
     253             :      * probably excessively paranoid, but let's be sure.
     254             :      */
     255       11500 :     Py_INCREF(exc);
     256       11500 :     return exc;
     257             : }
     258             : 
     259             : /*
     260             :  * Add all the autogenerated exceptions as subclasses of SPIError
     261             :  */
     262             : static void
     263          46 : PLy_generate_spi_exceptions(PyObject *mod, PyObject *base)
     264             : {
     265             :     int         i;
     266             : 
     267       11408 :     for (i = 0; exception_map[i].name != NULL; i++)
     268             :     {
     269             :         bool        found;
     270             :         PyObject   *exc;
     271             :         PLyExceptionEntry *entry;
     272             :         PyObject   *sqlstate;
     273       11362 :         PyObject   *dict = PyDict_New();
     274             : 
     275       11362 :         if (dict == NULL)
     276           0 :             PLy_elog(ERROR, NULL);
     277             : 
     278       11362 :         sqlstate = PyString_FromString(unpack_sql_state(exception_map[i].sqlstate));
     279       11362 :         if (sqlstate == NULL)
     280           0 :             PLy_elog(ERROR, "could not generate SPI exceptions");
     281             : 
     282       11362 :         PyDict_SetItemString(dict, "sqlstate", sqlstate);
     283       11362 :         Py_DECREF(sqlstate);
     284             : 
     285       11362 :         exc = PLy_create_exception(exception_map[i].name, base, dict,
     286       11362 :                                    exception_map[i].classname, mod);
     287             : 
     288       11362 :         entry = hash_search(PLy_spi_exceptions, &exception_map[i].sqlstate,
     289             :                             HASH_ENTER, &found);
     290             :         Assert(!found);
     291       11362 :         entry->exc = exc;
     292             :     }
     293          46 : }
     294             : 
     295             : 
     296             : /*
     297             :  * the python interface to the elog function
     298             :  * don't confuse these with PLy_elog
     299             :  */
     300             : static PyObject *PLy_output(volatile int level, PyObject *self,
     301             :                             PyObject *args, PyObject *kw);
     302             : 
     303             : static PyObject *
     304           4 : PLy_debug(PyObject *self, PyObject *args, PyObject *kw)
     305             : {
     306           4 :     return PLy_output(DEBUG2, self, args, kw);
     307             : }
     308             : 
     309             : static PyObject *
     310           4 : PLy_log(PyObject *self, PyObject *args, PyObject *kw)
     311             : {
     312           4 :     return PLy_output(LOG, self, args, kw);
     313             : }
     314             : 
     315             : static PyObject *
     316         222 : PLy_info(PyObject *self, PyObject *args, PyObject *kw)
     317             : {
     318         222 :     return PLy_output(INFO, self, args, kw);
     319             : }
     320             : 
     321             : static PyObject *
     322         404 : PLy_notice(PyObject *self, PyObject *args, PyObject *kw)
     323             : {
     324         404 :     return PLy_output(NOTICE, self, args, kw);
     325             : }
     326             : 
     327             : static PyObject *
     328          10 : PLy_warning(PyObject *self, PyObject *args, PyObject *kw)
     329             : {
     330          10 :     return PLy_output(WARNING, self, args, kw);
     331             : }
     332             : 
     333             : static PyObject *
     334          22 : PLy_error(PyObject *self, PyObject *args, PyObject *kw)
     335             : {
     336          22 :     return PLy_output(ERROR, self, args, kw);
     337             : }
     338             : 
     339             : static PyObject *
     340           0 : PLy_fatal(PyObject *self, PyObject *args, PyObject *kw)
     341             : {
     342           0 :     return PLy_output(FATAL, self, args, kw);
     343             : }
     344             : 
     345             : static PyObject *
     346          12 : PLy_quote_literal(PyObject *self, PyObject *args)
     347             : {
     348             :     const char *str;
     349             :     char       *quoted;
     350             :     PyObject   *ret;
     351             : 
     352          12 :     if (!PyArg_ParseTuple(args, "s:quote_literal", &str))
     353           0 :         return NULL;
     354             : 
     355          12 :     quoted = quote_literal_cstr(str);
     356          12 :     ret = PyString_FromString(quoted);
     357          12 :     pfree(quoted);
     358             : 
     359          12 :     return ret;
     360             : }
     361             : 
     362             : static PyObject *
     363          12 : PLy_quote_nullable(PyObject *self, PyObject *args)
     364             : {
     365             :     const char *str;
     366             :     char       *quoted;
     367             :     PyObject   *ret;
     368             : 
     369          12 :     if (!PyArg_ParseTuple(args, "z:quote_nullable", &str))
     370           0 :         return NULL;
     371             : 
     372          12 :     if (str == NULL)
     373           2 :         return PyString_FromString("NULL");
     374             : 
     375          10 :     quoted = quote_literal_cstr(str);
     376          10 :     ret = PyString_FromString(quoted);
     377          10 :     pfree(quoted);
     378             : 
     379          10 :     return ret;
     380             : }
     381             : 
     382             : static PyObject *
     383           6 : PLy_quote_ident(PyObject *self, PyObject *args)
     384             : {
     385             :     const char *str;
     386             :     const char *quoted;
     387             :     PyObject   *ret;
     388             : 
     389           6 :     if (!PyArg_ParseTuple(args, "s:quote_ident", &str))
     390           0 :         return NULL;
     391             : 
     392           6 :     quoted = quote_identifier(str);
     393           6 :     ret = PyString_FromString(quoted);
     394             : 
     395           6 :     return ret;
     396             : }
     397             : 
     398             : /* enforce cast of object to string */
     399             : static char *
     400         116 : object_to_string(PyObject *obj)
     401             : {
     402         116 :     if (obj)
     403             :     {
     404         116 :         PyObject   *so = PyObject_Str(obj);
     405             : 
     406         116 :         if (so != NULL)
     407             :         {
     408             :             char       *str;
     409             : 
     410         116 :             str = pstrdup(PyString_AsString(so));
     411         116 :             Py_DECREF(so);
     412             : 
     413         116 :             return str;
     414             :         }
     415             :     }
     416             : 
     417           0 :     return NULL;
     418             : }
     419             : 
     420             : static PyObject *
     421         666 : PLy_output(volatile int level, PyObject *self, PyObject *args, PyObject *kw)
     422             : {
     423         666 :     int         sqlstate = 0;
     424         666 :     char       *volatile sqlstatestr = NULL;
     425         666 :     char       *volatile message = NULL;
     426         666 :     char       *volatile detail = NULL;
     427         666 :     char       *volatile hint = NULL;
     428         666 :     char       *volatile column_name = NULL;
     429         666 :     char       *volatile constraint_name = NULL;
     430         666 :     char       *volatile datatype_name = NULL;
     431         666 :     char       *volatile table_name = NULL;
     432         666 :     char       *volatile schema_name = NULL;
     433             :     volatile MemoryContext oldcontext;
     434             :     PyObject   *key,
     435             :                *value;
     436             :     PyObject   *volatile so;
     437         666 :     Py_ssize_t  pos = 0;
     438             : 
     439         666 :     if (PyTuple_Size(args) == 1)
     440             :     {
     441             :         /*
     442             :          * Treat single argument specially to avoid undesirable ('tuple',)
     443             :          * decoration.
     444             :          */
     445             :         PyObject   *o;
     446             : 
     447         494 :         if (!PyArg_UnpackTuple(args, "plpy.elog", 1, 1, &o))
     448           0 :             PLy_elog(ERROR, "could not unpack arguments in plpy.elog");
     449         494 :         so = PyObject_Str(o);
     450             :     }
     451             :     else
     452         172 :         so = PyObject_Str(args);
     453             : 
     454         666 :     if (so == NULL || ((message = PyString_AsString(so)) == NULL))
     455             :     {
     456           0 :         level = ERROR;
     457           0 :         message = dgettext(TEXTDOMAIN, "could not parse error message in plpy.elog");
     458             :     }
     459         666 :     message = pstrdup(message);
     460             : 
     461         666 :     Py_XDECREF(so);
     462             : 
     463         666 :     if (kw != NULL)
     464             :     {
     465         204 :         while (PyDict_Next(kw, &pos, &key, &value))
     466             :         {
     467         122 :             char       *keyword = PyString_AsString(key);
     468             : 
     469         122 :             if (strcmp(keyword, "message") == 0)
     470             :             {
     471             :                 /* the message should not be overwritten */
     472          18 :                 if (PyTuple_Size(args) != 0)
     473             :                 {
     474           4 :                     PLy_exception_set(PyExc_TypeError, "argument 'message' given by name and position");
     475           4 :                     return NULL;
     476             :                 }
     477             : 
     478          14 :                 if (message)
     479          14 :                     pfree(message);
     480          14 :                 message = object_to_string(value);
     481             :             }
     482         104 :             else if (strcmp(keyword, "detail") == 0)
     483          30 :                 detail = object_to_string(value);
     484          74 :             else if (strcmp(keyword, "hint") == 0)
     485          14 :                 hint = object_to_string(value);
     486          60 :             else if (strcmp(keyword, "sqlstate") == 0)
     487          14 :                 sqlstatestr = object_to_string(value);
     488          46 :             else if (strcmp(keyword, "schema_name") == 0)
     489           8 :                 schema_name = object_to_string(value);
     490          38 :             else if (strcmp(keyword, "table_name") == 0)
     491          10 :                 table_name = object_to_string(value);
     492          28 :             else if (strcmp(keyword, "column_name") == 0)
     493           8 :                 column_name = object_to_string(value);
     494          20 :             else if (strcmp(keyword, "datatype_name") == 0)
     495          10 :                 datatype_name = object_to_string(value);
     496          10 :             else if (strcmp(keyword, "constraint_name") == 0)
     497           8 :                 constraint_name = object_to_string(value);
     498             :             else
     499             :             {
     500           2 :                 PLy_exception_set(PyExc_TypeError,
     501             :                                   "'%s' is an invalid keyword argument for this function",
     502             :                                   keyword);
     503           2 :                 return NULL;
     504             :             }
     505             :         }
     506             :     }
     507             : 
     508         660 :     if (sqlstatestr != NULL)
     509             :     {
     510          14 :         if (strlen(sqlstatestr) != 5)
     511             :         {
     512           2 :             PLy_exception_set(PyExc_ValueError, "invalid SQLSTATE code");
     513           2 :             return NULL;
     514             :         }
     515             : 
     516          12 :         if (strspn(sqlstatestr, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != 5)
     517             :         {
     518           0 :             PLy_exception_set(PyExc_ValueError, "invalid SQLSTATE code");
     519           0 :             return NULL;
     520             :         }
     521             : 
     522          12 :         sqlstate = MAKE_SQLSTATE(sqlstatestr[0],
     523             :                                  sqlstatestr[1],
     524             :                                  sqlstatestr[2],
     525             :                                  sqlstatestr[3],
     526             :                                  sqlstatestr[4]);
     527             :     }
     528             : 
     529         658 :     oldcontext = CurrentMemoryContext;
     530         658 :     PG_TRY();
     531             :     {
     532         658 :         if (message != NULL)
     533         658 :             pg_verifymbstr(message, strlen(message), false);
     534         658 :         if (detail != NULL)
     535          30 :             pg_verifymbstr(detail, strlen(detail), false);
     536         658 :         if (hint != NULL)
     537          14 :             pg_verifymbstr(hint, strlen(hint), false);
     538         658 :         if (schema_name != NULL)
     539           8 :             pg_verifymbstr(schema_name, strlen(schema_name), false);
     540         658 :         if (table_name != NULL)
     541          10 :             pg_verifymbstr(table_name, strlen(table_name), false);
     542         658 :         if (column_name != NULL)
     543           8 :             pg_verifymbstr(column_name, strlen(column_name), false);
     544         658 :         if (datatype_name != NULL)
     545          10 :             pg_verifymbstr(datatype_name, strlen(datatype_name), false);
     546         658 :         if (constraint_name != NULL)
     547           8 :             pg_verifymbstr(constraint_name, strlen(constraint_name), false);
     548             : 
     549         658 :         ereport(level,
     550             :                 ((sqlstate != 0) ? errcode(sqlstate) : 0,
     551             :                  (message != NULL) ? errmsg_internal("%s", message) : 0,
     552             :                  (detail != NULL) ? errdetail_internal("%s", detail) : 0,
     553             :                  (hint != NULL) ? errhint("%s", hint) : 0,
     554             :                  (column_name != NULL) ?
     555             :                  err_generic_string(PG_DIAG_COLUMN_NAME, column_name) : 0,
     556             :                  (constraint_name != NULL) ?
     557             :                  err_generic_string(PG_DIAG_CONSTRAINT_NAME, constraint_name) : 0,
     558             :                  (datatype_name != NULL) ?
     559             :                  err_generic_string(PG_DIAG_DATATYPE_NAME, datatype_name) : 0,
     560             :                  (table_name != NULL) ?
     561             :                  err_generic_string(PG_DIAG_TABLE_NAME, table_name) : 0,
     562             :                  (schema_name != NULL) ?
     563             :                  err_generic_string(PG_DIAG_SCHEMA_NAME, schema_name) : 0));
     564             :     }
     565          22 :     PG_CATCH();
     566             :     {
     567             :         ErrorData  *edata;
     568             : 
     569          22 :         MemoryContextSwitchTo(oldcontext);
     570          22 :         edata = CopyErrorData();
     571          22 :         FlushErrorState();
     572             : 
     573          22 :         PLy_exception_set_with_details(PLy_exc_error, edata);
     574          22 :         FreeErrorData(edata);
     575             : 
     576          22 :         return NULL;
     577             :     }
     578         636 :     PG_END_TRY();
     579             : 
     580             :     /*
     581             :      * return a legal object so the interpreter will continue on its merry way
     582             :      */
     583         636 :     Py_RETURN_NONE;
     584             : }
     585             : 
     586             : static PyObject *
     587          48 : PLy_commit(PyObject *self, PyObject *args)
     588             : {
     589          48 :     PLyExecutionContext *exec_ctx = PLy_current_execution_context();
     590             : 
     591          48 :     SPI_commit();
     592          40 :     SPI_start_transaction();
     593             : 
     594             :     /* was cleared at transaction end, reset pointer */
     595          40 :     exec_ctx->scratch_ctx = NULL;
     596             : 
     597          40 :     Py_RETURN_NONE;
     598             : }
     599             : 
     600             : static PyObject *
     601          34 : PLy_rollback(PyObject *self, PyObject *args)
     602             : {
     603          34 :     PLyExecutionContext *exec_ctx = PLy_current_execution_context();
     604             : 
     605          34 :     SPI_rollback();
     606          34 :     SPI_start_transaction();
     607             : 
     608             :     /* was cleared at transaction end, reset pointer */
     609          34 :     exec_ctx->scratch_ctx = NULL;
     610             : 
     611          34 :     Py_RETURN_NONE;
     612             : }

Generated by: LCOV version 1.13