LCOV - code coverage report
Current view: top level - src/pl/plpython - plpy_spi.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 214 265 80.8 %
Date: 2024-04-26 00:11:43 Functions: 11 11 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * interface to SPI functions
       3             :  *
       4             :  * src/pl/plpython/plpy_spi.c
       5             :  */
       6             : 
       7             : #include "postgres.h"
       8             : 
       9             : #include <limits.h>
      10             : 
      11             : #include "access/htup_details.h"
      12             : #include "access/xact.h"
      13             : #include "catalog/pg_type.h"
      14             : #include "executor/spi.h"
      15             : #include "mb/pg_wchar.h"
      16             : #include "parser/parse_type.h"
      17             : #include "plpy_elog.h"
      18             : #include "plpy_main.h"
      19             : #include "plpy_planobject.h"
      20             : #include "plpy_plpymodule.h"
      21             : #include "plpy_procedure.h"
      22             : #include "plpy_resultobject.h"
      23             : #include "plpy_spi.h"
      24             : #include "plpython.h"
      25             : #include "utils/memutils.h"
      26             : #include "utils/syscache.h"
      27             : 
      28             : static PyObject *PLy_spi_execute_query(char *query, long limit);
      29             : static PyObject *PLy_spi_execute_fetch_result(SPITupleTable *tuptable,
      30             :                                               uint64 rows, int status);
      31             : static void PLy_spi_exception_set(PyObject *excclass, ErrorData *edata);
      32             : 
      33             : 
      34             : /* prepare(query="select * from foo")
      35             :  * prepare(query="select * from foo where bar = $1", params=["text"])
      36             :  * prepare(query="select * from foo where bar = $1", params=["text"], limit=5)
      37             :  */
      38             : PyObject *
      39          52 : PLy_spi_prepare(PyObject *self, PyObject *args)
      40             : {
      41             :     PLyPlanObject *plan;
      42          52 :     PyObject   *list = NULL;
      43          52 :     PyObject   *volatile optr = NULL;
      44             :     char       *query;
      45          52 :     PLyExecutionContext *exec_ctx = PLy_current_execution_context();
      46             :     volatile MemoryContext oldcontext;
      47             :     volatile ResourceOwner oldowner;
      48             :     volatile int nargs;
      49             : 
      50          52 :     if (!PyArg_ParseTuple(args, "s|O:prepare", &query, &list))
      51           0 :         return NULL;
      52             : 
      53          52 :     if (list && (!PySequence_Check(list)))
      54             :     {
      55           0 :         PLy_exception_set(PyExc_TypeError,
      56             :                           "second argument of plpy.prepare must be a sequence");
      57           0 :         return NULL;
      58             :     }
      59             : 
      60          52 :     if ((plan = (PLyPlanObject *) PLy_plan_new()) == NULL)
      61           0 :         return NULL;
      62             : 
      63          52 :     plan->mcxt = AllocSetContextCreate(TopMemoryContext,
      64             :                                        "PL/Python plan context",
      65             :                                        ALLOCSET_DEFAULT_SIZES);
      66          52 :     oldcontext = MemoryContextSwitchTo(plan->mcxt);
      67             : 
      68          52 :     nargs = list ? PySequence_Length(list) : 0;
      69             : 
      70          52 :     plan->nargs = nargs;
      71          52 :     plan->types = nargs ? palloc0(sizeof(Oid) * nargs) : NULL;
      72          52 :     plan->values = nargs ? palloc0(sizeof(Datum) * nargs) : NULL;
      73          52 :     plan->args = nargs ? palloc0(sizeof(PLyObToDatum) * nargs) : NULL;
      74             : 
      75          52 :     MemoryContextSwitchTo(oldcontext);
      76             : 
      77          52 :     oldcontext = CurrentMemoryContext;
      78          52 :     oldowner = CurrentResourceOwner;
      79             : 
      80          52 :     PLy_spi_subtransaction_begin(oldcontext, oldowner);
      81             : 
      82          52 :     PG_TRY();
      83             :     {
      84             :         int         i;
      85             : 
      86          80 :         for (i = 0; i < nargs; i++)
      87             :         {
      88             :             char       *sptr;
      89             :             Oid         typeId;
      90             :             int32       typmod;
      91             : 
      92          34 :             optr = PySequence_GetItem(list, i);
      93          34 :             if (PyUnicode_Check(optr))
      94          34 :                 sptr = PLyUnicode_AsString(optr);
      95             :             else
      96             :             {
      97           0 :                 ereport(ERROR,
      98             :                         (errmsg("plpy.prepare: type name at ordinal position %d is not a string", i)));
      99             :                 sptr = NULL;    /* keep compiler quiet */
     100             :             }
     101             : 
     102             :             /********************************************************
     103             :              * Resolve argument type names and then look them up by
     104             :              * oid in the system cache, and remember the required
     105             :              *information for input conversion.
     106             :              ********************************************************/
     107             : 
     108          34 :             (void) parseTypeString(sptr, &typeId, &typmod, NULL);
     109             : 
     110          28 :             Py_DECREF(optr);
     111             : 
     112             :             /*
     113             :              * set optr to NULL, so we won't try to unref it again in case of
     114             :              * an error
     115             :              */
     116          28 :             optr = NULL;
     117             : 
     118          28 :             plan->types[i] = typeId;
     119          28 :             PLy_output_setup_func(&plan->args[i], plan->mcxt,
     120             :                                   typeId, typmod,
     121          28 :                                   exec_ctx->curr_proc);
     122             :         }
     123             : 
     124          46 :         pg_verifymbstr(query, strlen(query), false);
     125          46 :         plan->plan = SPI_prepare(query, plan->nargs, plan->types);
     126          46 :         if (plan->plan == NULL)
     127           0 :             elog(ERROR, "SPI_prepare failed: %s",
     128             :                  SPI_result_code_string(SPI_result));
     129             : 
     130             :         /* transfer plan from procCxt to topCxt */
     131          46 :         if (SPI_keepplan(plan->plan))
     132           0 :             elog(ERROR, "SPI_keepplan failed");
     133             : 
     134          46 :         PLy_spi_subtransaction_commit(oldcontext, oldowner);
     135             :     }
     136           6 :     PG_CATCH();
     137             :     {
     138           6 :         Py_DECREF(plan);
     139           6 :         Py_XDECREF(optr);
     140             : 
     141           6 :         PLy_spi_subtransaction_abort(oldcontext, oldowner);
     142           6 :         return NULL;
     143             :     }
     144          46 :     PG_END_TRY();
     145             : 
     146             :     Assert(plan->plan != NULL);
     147          46 :     return (PyObject *) plan;
     148             : }
     149             : 
     150             : /* execute(query="select * from foo", limit=5)
     151             :  * execute(plan=plan, values=(foo, bar), limit=5)
     152             :  */
     153             : PyObject *
     154         324 : PLy_spi_execute(PyObject *self, PyObject *args)
     155             : {
     156             :     char       *query;
     157             :     PyObject   *plan;
     158         324 :     PyObject   *list = NULL;
     159         324 :     long        limit = 0;
     160             : 
     161         324 :     if (PyArg_ParseTuple(args, "s|l", &query, &limit))
     162         282 :         return PLy_spi_execute_query(query, limit);
     163             : 
     164          42 :     PyErr_Clear();
     165             : 
     166          84 :     if (PyArg_ParseTuple(args, "O|Ol", &plan, &list, &limit) &&
     167          42 :         is_PLyPlanObject(plan))
     168          42 :         return PLy_spi_execute_plan(plan, list, limit);
     169             : 
     170           0 :     PLy_exception_set(PLy_exc_error, "plpy.execute expected a query or a plan");
     171           0 :     return NULL;
     172             : }
     173             : 
     174             : PyObject *
     175          44 : PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit)
     176             : {
     177             :     volatile int nargs;
     178             :     int         i,
     179             :                 rv;
     180             :     PLyPlanObject *plan;
     181             :     volatile MemoryContext oldcontext;
     182             :     volatile ResourceOwner oldowner;
     183             :     PyObject   *ret;
     184             : 
     185          44 :     if (list != NULL)
     186             :     {
     187          32 :         if (!PySequence_Check(list) || PyUnicode_Check(list))
     188             :         {
     189           0 :             PLy_exception_set(PyExc_TypeError, "plpy.execute takes a sequence as its second argument");
     190           0 :             return NULL;
     191             :         }
     192          32 :         nargs = PySequence_Length(list);
     193             :     }
     194             :     else
     195          12 :         nargs = 0;
     196             : 
     197          44 :     plan = (PLyPlanObject *) ob;
     198             : 
     199          44 :     if (nargs != plan->nargs)
     200             :     {
     201             :         char       *sv;
     202           0 :         PyObject   *so = PyObject_Str(list);
     203             : 
     204           0 :         if (!so)
     205           0 :             PLy_elog(ERROR, "could not execute plan");
     206           0 :         sv = PLyUnicode_AsString(so);
     207           0 :         PLy_exception_set_plural(PyExc_TypeError,
     208             :                                  "Expected sequence of %d argument, got %d: %s",
     209             :                                  "Expected sequence of %d arguments, got %d: %s",
     210           0 :                                  plan->nargs,
     211             :                                  plan->nargs, nargs, sv);
     212           0 :         Py_DECREF(so);
     213             : 
     214           0 :         return NULL;
     215             :     }
     216             : 
     217          44 :     oldcontext = CurrentMemoryContext;
     218          44 :     oldowner = CurrentResourceOwner;
     219             : 
     220          44 :     PLy_spi_subtransaction_begin(oldcontext, oldowner);
     221             : 
     222          44 :     PG_TRY();
     223             :     {
     224          44 :         PLyExecutionContext *exec_ctx = PLy_current_execution_context();
     225             :         char       *volatile nulls;
     226             :         volatile int j;
     227             : 
     228          44 :         if (nargs > 0)
     229          30 :             nulls = palloc(nargs * sizeof(char));
     230             :         else
     231          14 :             nulls = NULL;
     232             : 
     233          72 :         for (j = 0; j < nargs; j++)
     234             :         {
     235          32 :             PLyObToDatum *arg = &plan->args[j];
     236             :             PyObject   *elem;
     237             : 
     238          32 :             elem = PySequence_GetItem(list, j);
     239          32 :             PG_TRY(2);
     240             :             {
     241             :                 bool        isnull;
     242             : 
     243          32 :                 plan->values[j] = PLy_output_convert(arg, elem, &isnull);
     244          28 :                 nulls[j] = isnull ? 'n' : ' ';
     245             :             }
     246           4 :             PG_FINALLY(2);
     247             :             {
     248          32 :                 Py_DECREF(elem);
     249             :             }
     250          32 :             PG_END_TRY(2);
     251             :         }
     252             : 
     253          80 :         rv = SPI_execute_plan(plan->plan, plan->values, nulls,
     254          40 :                               exec_ctx->curr_proc->fn_readonly, limit);
     255          40 :         ret = PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
     256             : 
     257          40 :         if (nargs > 0)
     258          26 :             pfree(nulls);
     259             : 
     260          40 :         PLy_spi_subtransaction_commit(oldcontext, oldowner);
     261             :     }
     262           4 :     PG_CATCH();
     263             :     {
     264             :         int         k;
     265             : 
     266             :         /*
     267             :          * cleanup plan->values array
     268             :          */
     269           8 :         for (k = 0; k < nargs; k++)
     270             :         {
     271           4 :             if (!plan->args[k].typbyval &&
     272           0 :                 (plan->values[k] != PointerGetDatum(NULL)))
     273             :             {
     274           0 :                 pfree(DatumGetPointer(plan->values[k]));
     275           0 :                 plan->values[k] = PointerGetDatum(NULL);
     276             :             }
     277             :         }
     278             : 
     279           4 :         PLy_spi_subtransaction_abort(oldcontext, oldowner);
     280           4 :         return NULL;
     281             :     }
     282          40 :     PG_END_TRY();
     283             : 
     284          68 :     for (i = 0; i < nargs; i++)
     285             :     {
     286          28 :         if (!plan->args[i].typbyval &&
     287          20 :             (plan->values[i] != PointerGetDatum(NULL)))
     288             :         {
     289          20 :             pfree(DatumGetPointer(plan->values[i]));
     290          20 :             plan->values[i] = PointerGetDatum(NULL);
     291             :         }
     292             :     }
     293             : 
     294          40 :     if (rv < 0)
     295             :     {
     296           2 :         PLy_exception_set(PLy_exc_spi_error,
     297             :                           "SPI_execute_plan failed: %s",
     298             :                           SPI_result_code_string(rv));
     299           2 :         return NULL;
     300             :     }
     301             : 
     302          38 :     return ret;
     303             : }
     304             : 
     305             : static PyObject *
     306         282 : PLy_spi_execute_query(char *query, long limit)
     307             : {
     308             :     int         rv;
     309             :     volatile MemoryContext oldcontext;
     310             :     volatile ResourceOwner oldowner;
     311         282 :     PyObject   *ret = NULL;
     312             : 
     313         282 :     oldcontext = CurrentMemoryContext;
     314         282 :     oldowner = CurrentResourceOwner;
     315             : 
     316         282 :     PLy_spi_subtransaction_begin(oldcontext, oldowner);
     317             : 
     318         282 :     PG_TRY();
     319             :     {
     320         282 :         PLyExecutionContext *exec_ctx = PLy_current_execution_context();
     321             : 
     322         282 :         pg_verifymbstr(query, strlen(query), false);
     323         282 :         rv = SPI_execute(query, exec_ctx->curr_proc->fn_readonly, limit);
     324         240 :         ret = PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
     325             : 
     326         240 :         PLy_spi_subtransaction_commit(oldcontext, oldowner);
     327             :     }
     328          42 :     PG_CATCH();
     329             :     {
     330          42 :         PLy_spi_subtransaction_abort(oldcontext, oldowner);
     331          42 :         return NULL;
     332             :     }
     333         240 :     PG_END_TRY();
     334             : 
     335         240 :     if (rv < 0)
     336             :     {
     337           2 :         Py_XDECREF(ret);
     338           2 :         PLy_exception_set(PLy_exc_spi_error,
     339             :                           "SPI_execute failed: %s",
     340             :                           SPI_result_code_string(rv));
     341           2 :         return NULL;
     342             :     }
     343             : 
     344         238 :     return ret;
     345             : }
     346             : 
     347             : static PyObject *
     348         280 : PLy_spi_execute_fetch_result(SPITupleTable *tuptable, uint64 rows, int status)
     349             : {
     350             :     PLyResultObject *result;
     351         280 :     PLyExecutionContext *exec_ctx = PLy_current_execution_context();
     352             :     volatile MemoryContext oldcontext;
     353             : 
     354         280 :     result = (PLyResultObject *) PLy_result_new();
     355         280 :     if (!result)
     356             :     {
     357           0 :         SPI_freetuptable(tuptable);
     358           0 :         return NULL;
     359             :     }
     360         280 :     Py_DECREF(result->status);
     361         280 :     result->status = PyLong_FromLong(status);
     362             : 
     363         280 :     if (status > 0 && tuptable == NULL)
     364             :     {
     365         158 :         Py_DECREF(result->nrows);
     366         158 :         result->nrows = PyLong_FromUnsignedLongLong(rows);
     367             :     }
     368         122 :     else if (status > 0 && tuptable != NULL)
     369             :     {
     370             :         PLyDatumToOb ininfo;
     371             :         MemoryContext cxt;
     372             : 
     373         118 :         Py_DECREF(result->nrows);
     374         118 :         result->nrows = PyLong_FromUnsignedLongLong(rows);
     375             : 
     376         118 :         cxt = AllocSetContextCreate(CurrentMemoryContext,
     377             :                                     "PL/Python temp context",
     378             :                                     ALLOCSET_DEFAULT_SIZES);
     379             : 
     380             :         /* Initialize for converting result tuples to Python */
     381         118 :         PLy_input_setup_func(&ininfo, cxt, RECORDOID, -1,
     382         118 :                              exec_ctx->curr_proc);
     383             : 
     384         118 :         oldcontext = CurrentMemoryContext;
     385         118 :         PG_TRY();
     386             :         {
     387             :             MemoryContext oldcontext2;
     388             : 
     389         118 :             if (rows)
     390             :             {
     391             :                 uint64      i;
     392             : 
     393             :                 /*
     394             :                  * PyList_New() and PyList_SetItem() use Py_ssize_t for list
     395             :                  * size and list indices; so we cannot support a result larger
     396             :                  * than PY_SSIZE_T_MAX.
     397             :                  */
     398         114 :                 if (rows > (uint64) PY_SSIZE_T_MAX)
     399           0 :                     ereport(ERROR,
     400             :                             (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
     401             :                              errmsg("query result has too many rows to fit in a Python list")));
     402             : 
     403         114 :                 Py_DECREF(result->rows);
     404         114 :                 result->rows = PyList_New(rows);
     405         114 :                 if (result->rows)
     406             :                 {
     407         114 :                     PLy_input_setup_tuple(&ininfo, tuptable->tupdesc,
     408         114 :                                           exec_ctx->curr_proc);
     409             : 
     410         262 :                     for (i = 0; i < rows; i++)
     411             :                     {
     412         296 :                         PyObject   *row = PLy_input_from_tuple(&ininfo,
     413         148 :                                                                tuptable->vals[i],
     414             :                                                                tuptable->tupdesc,
     415             :                                                                true);
     416             : 
     417         148 :                         PyList_SetItem(result->rows, i, row);
     418             :                     }
     419             :                 }
     420             :             }
     421             : 
     422             :             /*
     423             :              * Save tuple descriptor for later use by result set metadata
     424             :              * functions.  Save it in TopMemoryContext so that it survives
     425             :              * outside of an SPI context.  We trust that PLy_result_dealloc()
     426             :              * will clean it up when the time is right.  (Do this as late as
     427             :              * possible, to minimize the number of ways the tupdesc could get
     428             :              * leaked due to errors.)
     429             :              */
     430         118 :             oldcontext2 = MemoryContextSwitchTo(TopMemoryContext);
     431         118 :             result->tupdesc = CreateTupleDescCopy(tuptable->tupdesc);
     432         118 :             MemoryContextSwitchTo(oldcontext2);
     433             :         }
     434           0 :         PG_CATCH();
     435             :         {
     436           0 :             MemoryContextSwitchTo(oldcontext);
     437           0 :             MemoryContextDelete(cxt);
     438           0 :             Py_DECREF(result);
     439           0 :             PG_RE_THROW();
     440             :         }
     441         118 :         PG_END_TRY();
     442             : 
     443         118 :         MemoryContextDelete(cxt);
     444         118 :         SPI_freetuptable(tuptable);
     445             : 
     446             :         /* in case PyList_New() failed above */
     447         118 :         if (!result->rows)
     448             :         {
     449           0 :             Py_DECREF(result);
     450           0 :             result = NULL;
     451             :         }
     452             :     }
     453             : 
     454         280 :     return (PyObject *) result;
     455             : }
     456             : 
     457             : PyObject *
     458          52 : PLy_commit(PyObject *self, PyObject *args)
     459             : {
     460          52 :     MemoryContext oldcontext = CurrentMemoryContext;
     461          52 :     PLyExecutionContext *exec_ctx = PLy_current_execution_context();
     462             : 
     463          52 :     PG_TRY();
     464             :     {
     465          52 :         SPI_commit();
     466             : 
     467             :         /* was cleared at transaction end, reset pointer */
     468          40 :         exec_ctx->scratch_ctx = NULL;
     469             :     }
     470          12 :     PG_CATCH();
     471             :     {
     472             :         ErrorData  *edata;
     473             :         PLyExceptionEntry *entry;
     474             :         PyObject   *exc;
     475             : 
     476             :         /* Save error info */
     477          12 :         MemoryContextSwitchTo(oldcontext);
     478          12 :         edata = CopyErrorData();
     479          12 :         FlushErrorState();
     480             : 
     481             :         /* was cleared at transaction end, reset pointer */
     482          12 :         exec_ctx->scratch_ctx = NULL;
     483             : 
     484             :         /* Look up the correct exception */
     485          12 :         entry = hash_search(PLy_spi_exceptions, &(edata->sqlerrcode),
     486             :                             HASH_FIND, NULL);
     487             : 
     488             :         /*
     489             :          * This could be a custom error code, if that's the case fallback to
     490             :          * SPIError
     491             :          */
     492          12 :         exc = entry ? entry->exc : PLy_exc_spi_error;
     493             :         /* Make Python raise the exception */
     494          12 :         PLy_spi_exception_set(exc, edata);
     495          12 :         FreeErrorData(edata);
     496             : 
     497          12 :         return NULL;
     498             :     }
     499          40 :     PG_END_TRY();
     500             : 
     501          40 :     Py_RETURN_NONE;
     502             : }
     503             : 
     504             : PyObject *
     505          34 : PLy_rollback(PyObject *self, PyObject *args)
     506             : {
     507          34 :     MemoryContext oldcontext = CurrentMemoryContext;
     508          34 :     PLyExecutionContext *exec_ctx = PLy_current_execution_context();
     509             : 
     510          34 :     PG_TRY();
     511             :     {
     512          34 :         SPI_rollback();
     513             : 
     514             :         /* was cleared at transaction end, reset pointer */
     515          34 :         exec_ctx->scratch_ctx = NULL;
     516             :     }
     517           0 :     PG_CATCH();
     518             :     {
     519             :         ErrorData  *edata;
     520             :         PLyExceptionEntry *entry;
     521             :         PyObject   *exc;
     522             : 
     523             :         /* Save error info */
     524           0 :         MemoryContextSwitchTo(oldcontext);
     525           0 :         edata = CopyErrorData();
     526           0 :         FlushErrorState();
     527             : 
     528             :         /* was cleared at transaction end, reset pointer */
     529           0 :         exec_ctx->scratch_ctx = NULL;
     530             : 
     531             :         /* Look up the correct exception */
     532           0 :         entry = hash_search(PLy_spi_exceptions, &(edata->sqlerrcode),
     533             :                             HASH_FIND, NULL);
     534             : 
     535             :         /*
     536             :          * This could be a custom error code, if that's the case fallback to
     537             :          * SPIError
     538             :          */
     539           0 :         exc = entry ? entry->exc : PLy_exc_spi_error;
     540             :         /* Make Python raise the exception */
     541           0 :         PLy_spi_exception_set(exc, edata);
     542           0 :         FreeErrorData(edata);
     543             : 
     544           0 :         return NULL;
     545             :     }
     546          34 :     PG_END_TRY();
     547             : 
     548          34 :     Py_RETURN_NONE;
     549             : }
     550             : 
     551             : /*
     552             :  * Utilities for running SPI functions in subtransactions.
     553             :  *
     554             :  * Usage:
     555             :  *
     556             :  *  MemoryContext oldcontext = CurrentMemoryContext;
     557             :  *  ResourceOwner oldowner = CurrentResourceOwner;
     558             :  *
     559             :  *  PLy_spi_subtransaction_begin(oldcontext, oldowner);
     560             :  *  PG_TRY();
     561             :  *  {
     562             :  *      <call SPI functions>
     563             :  *      PLy_spi_subtransaction_commit(oldcontext, oldowner);
     564             :  *  }
     565             :  *  PG_CATCH();
     566             :  *  {
     567             :  *      <do cleanup>
     568             :  *      PLy_spi_subtransaction_abort(oldcontext, oldowner);
     569             :  *      return NULL;
     570             :  *  }
     571             :  *  PG_END_TRY();
     572             :  *
     573             :  * These utilities take care of restoring connection to the SPI manager and
     574             :  * setting a Python exception in case of an abort.
     575             :  */
     576             : void
     577         504 : PLy_spi_subtransaction_begin(MemoryContext oldcontext, ResourceOwner oldowner)
     578             : {
     579         504 :     BeginInternalSubTransaction(NULL);
     580             :     /* Want to run inside function's memory context */
     581         504 :     MemoryContextSwitchTo(oldcontext);
     582         504 : }
     583             : 
     584             : void
     585         450 : PLy_spi_subtransaction_commit(MemoryContext oldcontext, ResourceOwner oldowner)
     586             : {
     587             :     /* Commit the inner transaction, return to outer xact context */
     588         450 :     ReleaseCurrentSubTransaction();
     589         450 :     MemoryContextSwitchTo(oldcontext);
     590         450 :     CurrentResourceOwner = oldowner;
     591         450 : }
     592             : 
     593             : void
     594          54 : PLy_spi_subtransaction_abort(MemoryContext oldcontext, ResourceOwner oldowner)
     595             : {
     596             :     ErrorData  *edata;
     597             :     PLyExceptionEntry *entry;
     598             :     PyObject   *exc;
     599             : 
     600             :     /* Save error info */
     601          54 :     MemoryContextSwitchTo(oldcontext);
     602          54 :     edata = CopyErrorData();
     603          54 :     FlushErrorState();
     604             : 
     605             :     /* Abort the inner transaction */
     606          54 :     RollbackAndReleaseCurrentSubTransaction();
     607          54 :     MemoryContextSwitchTo(oldcontext);
     608          54 :     CurrentResourceOwner = oldowner;
     609             : 
     610             :     /* Look up the correct exception */
     611          54 :     entry = hash_search(PLy_spi_exceptions, &(edata->sqlerrcode),
     612             :                         HASH_FIND, NULL);
     613             : 
     614             :     /*
     615             :      * This could be a custom error code, if that's the case fallback to
     616             :      * SPIError
     617             :      */
     618          54 :     exc = entry ? entry->exc : PLy_exc_spi_error;
     619             :     /* Make Python raise the exception */
     620          54 :     PLy_spi_exception_set(exc, edata);
     621          54 :     FreeErrorData(edata);
     622          54 : }
     623             : 
     624             : /*
     625             :  * Raise a SPIError, passing in it more error details, like the
     626             :  * internal query and error position.
     627             :  */
     628             : static void
     629          66 : PLy_spi_exception_set(PyObject *excclass, ErrorData *edata)
     630             : {
     631          66 :     PyObject   *args = NULL;
     632          66 :     PyObject   *spierror = NULL;
     633          66 :     PyObject   *spidata = NULL;
     634             : 
     635          66 :     args = Py_BuildValue("(s)", edata->message);
     636          66 :     if (!args)
     637           0 :         goto failure;
     638             : 
     639             :     /* create a new SPI exception with the error message as the parameter */
     640          66 :     spierror = PyObject_CallObject(excclass, args);
     641          66 :     if (!spierror)
     642           0 :         goto failure;
     643             : 
     644          66 :     spidata = Py_BuildValue("(izzzizzzzz)", edata->sqlerrcode, edata->detail, edata->hint,
     645             :                             edata->internalquery, edata->internalpos,
     646             :                             edata->schema_name, edata->table_name, edata->column_name,
     647             :                             edata->datatype_name, edata->constraint_name);
     648          66 :     if (!spidata)
     649           0 :         goto failure;
     650             : 
     651          66 :     if (PyObject_SetAttrString(spierror, "spidata", spidata) == -1)
     652           0 :         goto failure;
     653             : 
     654          66 :     PyErr_SetObject(excclass, spierror);
     655             : 
     656          66 :     Py_DECREF(args);
     657          66 :     Py_DECREF(spierror);
     658          66 :     Py_DECREF(spidata);
     659          66 :     return;
     660             : 
     661           0 : failure:
     662           0 :     Py_XDECREF(args);
     663           0 :     Py_XDECREF(spierror);
     664           0 :     Py_XDECREF(spidata);
     665           0 :     elog(ERROR, "could not convert SPI error to Python exception");
     666             : }

Generated by: LCOV version 1.14