LCOV - code coverage report
Current view: top level - src/pl/plpython - plpy_exec.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 390 417 93.5 %
Date: 2024-10-10 04:14:55 Functions: 15 15 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * executing Python code
       3             :  *
       4             :  * src/pl/plpython/plpy_exec.c
       5             :  */
       6             : 
       7             : #include "postgres.h"
       8             : 
       9             : #include "access/htup_details.h"
      10             : #include "access/xact.h"
      11             : #include "catalog/pg_type.h"
      12             : #include "commands/trigger.h"
      13             : #include "executor/spi.h"
      14             : #include "funcapi.h"
      15             : #include "plpy_elog.h"
      16             : #include "plpy_exec.h"
      17             : #include "plpy_main.h"
      18             : #include "plpy_procedure.h"
      19             : #include "plpy_subxactobject.h"
      20             : #include "plpython.h"
      21             : #include "utils/builtins.h"
      22             : #include "utils/lsyscache.h"
      23             : #include "utils/rel.h"
      24             : #include "utils/typcache.h"
      25             : 
      26             : /* saved state for a set-returning function */
      27             : typedef struct PLySRFState
      28             : {
      29             :     PyObject   *iter;           /* Python iterator producing results */
      30             :     PLySavedArgs *savedargs;    /* function argument values */
      31             :     MemoryContextCallback callback; /* for releasing refcounts when done */
      32             : } PLySRFState;
      33             : 
      34             : static PyObject *PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc);
      35             : static PLySavedArgs *PLy_function_save_args(PLyProcedure *proc);
      36             : static void PLy_function_restore_args(PLyProcedure *proc, PLySavedArgs *savedargs);
      37             : static void PLy_function_drop_args(PLySavedArgs *savedargs);
      38             : static void PLy_global_args_push(PLyProcedure *proc);
      39             : static void PLy_global_args_pop(PLyProcedure *proc);
      40             : static void plpython_srf_cleanup_callback(void *arg);
      41             : static void plpython_return_error_callback(void *arg);
      42             : 
      43             : static PyObject *PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc,
      44             :                                         HeapTuple *rv);
      45             : static HeapTuple PLy_modify_tuple(PLyProcedure *proc, PyObject *pltd,
      46             :                                   TriggerData *tdata, HeapTuple otup);
      47             : static void plpython_trigger_error_callback(void *arg);
      48             : 
      49             : static PyObject *PLy_procedure_call(PLyProcedure *proc, const char *kargs, PyObject *vargs);
      50             : static void PLy_abort_open_subtransactions(int save_subxact_level);
      51             : 
      52             : 
      53             : /* function subhandler */
      54             : Datum
      55        1304 : PLy_exec_function(FunctionCallInfo fcinfo, PLyProcedure *proc)
      56             : {
      57        1304 :     bool        is_setof = proc->is_setof;
      58             :     Datum       rv;
      59        1304 :     PyObject   *volatile plargs = NULL;
      60        1304 :     PyObject   *volatile plrv = NULL;
      61        1304 :     FuncCallContext *volatile funcctx = NULL;
      62        1304 :     PLySRFState *volatile srfstate = NULL;
      63             :     ErrorContextCallback plerrcontext;
      64             : 
      65             :     /*
      66             :      * If the function is called recursively, we must push outer-level
      67             :      * arguments into the stack.  This must be immediately before the PG_TRY
      68             :      * to ensure that the corresponding pop happens.
      69             :      */
      70        1304 :     PLy_global_args_push(proc);
      71             : 
      72        1304 :     PG_TRY();
      73             :     {
      74        1304 :         if (is_setof)
      75             :         {
      76             :             /* First Call setup */
      77         384 :             if (SRF_IS_FIRSTCALL())
      78             :             {
      79          98 :                 funcctx = SRF_FIRSTCALL_INIT();
      80          98 :                 srfstate = (PLySRFState *)
      81          98 :                     MemoryContextAllocZero(funcctx->multi_call_memory_ctx,
      82             :                                            sizeof(PLySRFState));
      83             :                 /* Immediately register cleanup callback */
      84          98 :                 srfstate->callback.func = plpython_srf_cleanup_callback;
      85          98 :                 srfstate->callback.arg = (void *) srfstate;
      86          98 :                 MemoryContextRegisterResetCallback(funcctx->multi_call_memory_ctx,
      87          98 :                                                    &srfstate->callback);
      88          98 :                 funcctx->user_fctx = (void *) srfstate;
      89             :             }
      90             :             /* Every call setup */
      91         384 :             funcctx = SRF_PERCALL_SETUP();
      92             :             Assert(funcctx != NULL);
      93         384 :             srfstate = (PLySRFState *) funcctx->user_fctx;
      94             :             Assert(srfstate != NULL);
      95             :         }
      96             : 
      97        1304 :         if (srfstate == NULL || srfstate->iter == NULL)
      98             :         {
      99             :             /*
     100             :              * Non-SETOF function or first time for SETOF function: build
     101             :              * args, then actually execute the function.
     102             :              */
     103        1018 :             plargs = PLy_function_build_args(fcinfo, proc);
     104        1018 :             plrv = PLy_procedure_call(proc, "args", plargs);
     105         910 :             Assert(plrv != NULL);
     106             :         }
     107             :         else
     108             :         {
     109             :             /*
     110             :              * Second or later call for a SETOF function: restore arguments in
     111             :              * globals dict to what they were when we left off.  We must do
     112             :              * this in case multiple evaluations of the same SETOF function
     113             :              * are interleaved.  It's a bit annoying, since the iterator may
     114             :              * not look at the arguments at all, but we have no way to know
     115             :              * that.  Fortunately this isn't terribly expensive.
     116             :              */
     117         286 :             if (srfstate->savedargs)
     118         286 :                 PLy_function_restore_args(proc, srfstate->savedargs);
     119         286 :             srfstate->savedargs = NULL; /* deleted by restore_args */
     120             :         }
     121             : 
     122             :         /*
     123             :          * If it returns a set, call the iterator to get the next return item.
     124             :          * We stay in the SPI context while doing this, because PyIter_Next()
     125             :          * calls back into Python code which might contain SPI calls.
     126             :          */
     127        1196 :         if (is_setof)
     128             :         {
     129         382 :             if (srfstate->iter == NULL)
     130             :             {
     131             :                 /* first time -- do checks and setup */
     132          96 :                 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
     133             : 
     134          96 :                 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
     135          96 :                     (rsi->allowedModes & SFRM_ValuePerCall) == 0)
     136             :                 {
     137           0 :                     ereport(ERROR,
     138             :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     139             :                              errmsg("unsupported set function return mode"),
     140             :                              errdetail("PL/Python set-returning functions only support returning one value per call.")));
     141             :                 }
     142          96 :                 rsi->returnMode = SFRM_ValuePerCall;
     143             : 
     144             :                 /* Make iterator out of returned object */
     145          96 :                 srfstate->iter = PyObject_GetIter(plrv);
     146             : 
     147          96 :                 Py_DECREF(plrv);
     148          96 :                 plrv = NULL;
     149             : 
     150          96 :                 if (srfstate->iter == NULL)
     151           2 :                     ereport(ERROR,
     152             :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
     153             :                              errmsg("returned object cannot be iterated"),
     154             :                              errdetail("PL/Python set-returning functions must return an iterable object.")));
     155             :             }
     156             : 
     157             :             /* Fetch next from iterator */
     158         380 :             plrv = PyIter_Next(srfstate->iter);
     159         380 :             if (plrv == NULL)
     160             :             {
     161             :                 /* Iterator is exhausted or error happened */
     162          90 :                 bool        has_error = (PyErr_Occurred() != NULL);
     163             : 
     164          90 :                 Py_DECREF(srfstate->iter);
     165          90 :                 srfstate->iter = NULL;
     166             : 
     167          90 :                 if (has_error)
     168           2 :                     PLy_elog(ERROR, "error fetching next item from iterator");
     169             : 
     170             :                 /* Pass a null through the data-returning steps below */
     171          88 :                 Py_INCREF(Py_None);
     172          88 :                 plrv = Py_None;
     173             :             }
     174             :             else
     175             :             {
     176             :                 /*
     177             :                  * This won't be last call, so save argument values.  We do
     178             :                  * this again each time in case the iterator is changing those
     179             :                  * values.
     180             :                  */
     181         290 :                 srfstate->savedargs = PLy_function_save_args(proc);
     182             :             }
     183             :         }
     184             : 
     185             :         /*
     186             :          * Disconnect from SPI manager and then create the return values datum
     187             :          * (if the input function does a palloc for it this must not be
     188             :          * allocated in the SPI memory context because SPI_finish would free
     189             :          * it).
     190             :          */
     191        1192 :         if (SPI_finish() != SPI_OK_FINISH)
     192           0 :             elog(ERROR, "SPI_finish failed");
     193             : 
     194        1192 :         plerrcontext.callback = plpython_return_error_callback;
     195        1192 :         plerrcontext.previous = error_context_stack;
     196        1192 :         error_context_stack = &plerrcontext;
     197             : 
     198             :         /*
     199             :          * For a procedure or function declared to return void, the Python
     200             :          * return value must be None. For void-returning functions, we also
     201             :          * treat a None return value as a special "void datum" rather than
     202             :          * NULL (as is the case for non-void-returning functions).
     203             :          */
     204        1192 :         if (proc->result.typoid == VOIDOID)
     205             :         {
     206          58 :             if (plrv != Py_None)
     207             :             {
     208           4 :                 if (proc->is_procedure)
     209           2 :                     ereport(ERROR,
     210             :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
     211             :                              errmsg("PL/Python procedure did not return None")));
     212             :                 else
     213           2 :                     ereport(ERROR,
     214             :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
     215             :                              errmsg("PL/Python function with return type \"void\" did not return None")));
     216             :             }
     217             : 
     218          54 :             fcinfo->isnull = false;
     219          54 :             rv = (Datum) 0;
     220             :         }
     221        1134 :         else if (plrv == Py_None &&
     222         100 :                  srfstate && srfstate->iter == NULL)
     223             :         {
     224             :             /*
     225             :              * In a SETOF function, the iteration-ending null isn't a real
     226             :              * value; don't pass it through the input function, which might
     227             :              * complain.
     228             :              */
     229          88 :             fcinfo->isnull = true;
     230          88 :             rv = (Datum) 0;
     231             :         }
     232             :         else
     233             :         {
     234             :             /*
     235             :              * Normal conversion of result.  However, if the result is of type
     236             :              * RECORD, we have to set up for that each time through, since it
     237             :              * might be different from last time.
     238             :              */
     239        1046 :             if (proc->result.typoid == RECORDOID)
     240             :             {
     241             :                 TupleDesc   desc;
     242             : 
     243         268 :                 if (get_call_result_type(fcinfo, NULL, &desc) != TYPEFUNC_COMPOSITE)
     244           0 :                     ereport(ERROR,
     245             :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     246             :                              errmsg("function returning record called in context "
     247             :                                     "that cannot accept type record")));
     248         268 :                 PLy_output_setup_record(&proc->result, desc, proc);
     249             :             }
     250             : 
     251        1046 :             rv = PLy_output_convert(&proc->result, plrv,
     252             :                                     &fcinfo->isnull);
     253             :         }
     254             :     }
     255         184 :     PG_CATCH();
     256             :     {
     257             :         /* Pop old arguments from the stack if they were pushed above */
     258         184 :         PLy_global_args_pop(proc);
     259             : 
     260         184 :         Py_XDECREF(plargs);
     261         184 :         Py_XDECREF(plrv);
     262             : 
     263             :         /*
     264             :          * If there was an error within a SRF, the iterator might not have
     265             :          * been exhausted yet.  Clear it so the next invocation of the
     266             :          * function will start the iteration again.  (This code is probably
     267             :          * unnecessary now; plpython_srf_cleanup_callback should take care of
     268             :          * cleanup.  But it doesn't hurt anything to do it here.)
     269             :          */
     270         184 :         if (srfstate)
     271             :         {
     272          10 :             Py_XDECREF(srfstate->iter);
     273          10 :             srfstate->iter = NULL;
     274             :             /* And drop any saved args; we won't need them */
     275          10 :             if (srfstate->savedargs)
     276           4 :                 PLy_function_drop_args(srfstate->savedargs);
     277          10 :             srfstate->savedargs = NULL;
     278             :         }
     279             : 
     280         184 :         PG_RE_THROW();
     281             :     }
     282        1120 :     PG_END_TRY();
     283             : 
     284        1120 :     error_context_stack = plerrcontext.previous;
     285             : 
     286             :     /* Pop old arguments from the stack if they were pushed above */
     287        1120 :     PLy_global_args_pop(proc);
     288             : 
     289        1120 :     Py_XDECREF(plargs);
     290        1120 :     Py_DECREF(plrv);
     291             : 
     292        1120 :     if (srfstate)
     293             :     {
     294             :         /* We're in a SRF, exit appropriately */
     295         374 :         if (srfstate->iter == NULL)
     296             :         {
     297             :             /* Iterator exhausted, so we're done */
     298          88 :             SRF_RETURN_DONE(funcctx);
     299             :         }
     300         286 :         else if (fcinfo->isnull)
     301          12 :             SRF_RETURN_NEXT_NULL(funcctx);
     302             :         else
     303         274 :             SRF_RETURN_NEXT(funcctx, rv);
     304             :     }
     305             : 
     306             :     /* Plain function, just return the Datum value (possibly null) */
     307         746 :     return rv;
     308             : }
     309             : 
     310             : /* trigger subhandler
     311             :  *
     312             :  * the python function is expected to return Py_None if the tuple is
     313             :  * acceptable and unmodified.  Otherwise it should return a PyUnicode
     314             :  * object who's value is SKIP, or MODIFY.  SKIP means don't perform
     315             :  * this action.  MODIFY means the tuple has been modified, so update
     316             :  * tuple and perform action.  SKIP and MODIFY assume the trigger fires
     317             :  * BEFORE the event and is ROW level.  postgres expects the function
     318             :  * to take no arguments and return an argument of type trigger.
     319             :  */
     320             : HeapTuple
     321          98 : PLy_exec_trigger(FunctionCallInfo fcinfo, PLyProcedure *proc)
     322             : {
     323          98 :     HeapTuple   rv = NULL;
     324          98 :     PyObject   *volatile plargs = NULL;
     325          98 :     PyObject   *volatile plrv = NULL;
     326             :     TriggerData *tdata;
     327             :     TupleDesc   rel_descr;
     328             : 
     329             :     Assert(CALLED_AS_TRIGGER(fcinfo));
     330          98 :     tdata = (TriggerData *) fcinfo->context;
     331             : 
     332             :     /*
     333             :      * Input/output conversion for trigger tuples.  We use the result and
     334             :      * result_in fields to store the tuple conversion info.  We do this over
     335             :      * again on each call to cover the possibility that the relation's tupdesc
     336             :      * changed since the trigger was last called.  The PLy_xxx_setup_func
     337             :      * calls should only happen once, but PLy_input_setup_tuple and
     338             :      * PLy_output_setup_tuple are responsible for not doing repetitive work.
     339             :      */
     340          98 :     rel_descr = RelationGetDescr(tdata->tg_relation);
     341          98 :     if (proc->result.typoid != rel_descr->tdtypeid)
     342          52 :         PLy_output_setup_func(&proc->result, proc->mcxt,
     343             :                               rel_descr->tdtypeid,
     344             :                               rel_descr->tdtypmod,
     345             :                               proc);
     346          98 :     if (proc->result_in.typoid != rel_descr->tdtypeid)
     347          52 :         PLy_input_setup_func(&proc->result_in, proc->mcxt,
     348             :                              rel_descr->tdtypeid,
     349             :                              rel_descr->tdtypmod,
     350             :                              proc);
     351          98 :     PLy_output_setup_tuple(&proc->result, rel_descr, proc);
     352          98 :     PLy_input_setup_tuple(&proc->result_in, rel_descr, proc);
     353             : 
     354             :     /*
     355             :      * If the trigger is called recursively, we must push outer-level
     356             :      * arguments into the stack.  This must be immediately before the PG_TRY
     357             :      * to ensure that the corresponding pop happens.
     358             :      */
     359          98 :     PLy_global_args_push(proc);
     360             : 
     361          98 :     PG_TRY();
     362             :     {
     363             :         int         rc PG_USED_FOR_ASSERTS_ONLY;
     364             : 
     365          98 :         rc = SPI_register_trigger_data(tdata);
     366             :         Assert(rc >= 0);
     367             : 
     368          98 :         plargs = PLy_trigger_build_args(fcinfo, proc, &rv);
     369          98 :         plrv = PLy_procedure_call(proc, "TD", plargs);
     370             : 
     371             :         Assert(plrv != NULL);
     372             : 
     373             :         /*
     374             :          * Disconnect from SPI manager
     375             :          */
     376          98 :         if (SPI_finish() != SPI_OK_FINISH)
     377           0 :             elog(ERROR, "SPI_finish failed");
     378             : 
     379             :         /*
     380             :          * return of None means we're happy with the tuple
     381             :          */
     382          98 :         if (plrv != Py_None)
     383             :         {
     384             :             char       *srv;
     385             : 
     386          50 :             if (PyUnicode_Check(plrv))
     387          48 :                 srv = PLyUnicode_AsString(plrv);
     388             :             else
     389             :             {
     390           2 :                 ereport(ERROR,
     391             :                         (errcode(ERRCODE_DATA_EXCEPTION),
     392             :                          errmsg("unexpected return value from trigger procedure"),
     393             :                          errdetail("Expected None or a string.")));
     394             :                 srv = NULL;     /* keep compiler quiet */
     395             :             }
     396             : 
     397          48 :             if (pg_strcasecmp(srv, "SKIP") == 0)
     398           2 :                 rv = NULL;
     399          46 :             else if (pg_strcasecmp(srv, "MODIFY") == 0)
     400             :             {
     401          42 :                 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event) ||
     402          18 :                     TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
     403          40 :                     rv = PLy_modify_tuple(proc, plargs, tdata, rv);
     404             :                 else
     405           2 :                     ereport(WARNING,
     406             :                             (errmsg("PL/Python trigger function returned \"MODIFY\" in a DELETE trigger -- ignored")));
     407             :             }
     408           4 :             else if (pg_strcasecmp(srv, "OK") != 0)
     409             :             {
     410             :                 /*
     411             :                  * accept "OK" as an alternative to None; otherwise, raise an
     412             :                  * error
     413             :                  */
     414           4 :                 ereport(ERROR,
     415             :                         (errcode(ERRCODE_DATA_EXCEPTION),
     416             :                          errmsg("unexpected return value from trigger procedure"),
     417             :                          errdetail("Expected None, \"OK\", \"SKIP\", or \"MODIFY\".")));
     418             :             }
     419             :         }
     420             :     }
     421          18 :     PG_FINALLY();
     422             :     {
     423          98 :         PLy_global_args_pop(proc);
     424          98 :         Py_XDECREF(plargs);
     425          98 :         Py_XDECREF(plrv);
     426             :     }
     427          98 :     PG_END_TRY();
     428             : 
     429          80 :     return rv;
     430             : }
     431             : 
     432             : /* helper functions for Python code execution */
     433             : 
     434             : static PyObject *
     435        1018 : PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc)
     436             : {
     437        1018 :     PyObject   *volatile arg = NULL;
     438             :     PyObject   *args;
     439             :     int         i;
     440             : 
     441             :     /*
     442             :      * Make any Py*_New() calls before the PG_TRY block so that we can quickly
     443             :      * return NULL on failure.  We can't return within the PG_TRY block, else
     444             :      * we'd miss unwinding the exception stack.
     445             :      */
     446        1018 :     args = PyList_New(proc->nargs);
     447        1018 :     if (!args)
     448           0 :         return NULL;
     449             : 
     450        1018 :     PG_TRY();
     451             :     {
     452        2432 :         for (i = 0; i < proc->nargs; i++)
     453             :         {
     454        1414 :             PLyDatumToOb *arginfo = &proc->args[i];
     455             : 
     456        1414 :             if (fcinfo->args[i].isnull)
     457         240 :                 arg = NULL;
     458             :             else
     459        1174 :                 arg = PLy_input_convert(arginfo, fcinfo->args[i].value);
     460             : 
     461        1414 :             if (arg == NULL)
     462             :             {
     463         240 :                 Py_INCREF(Py_None);
     464         240 :                 arg = Py_None;
     465             :             }
     466             : 
     467        1414 :             if (PyList_SetItem(args, i, arg) == -1)
     468           0 :                 PLy_elog(ERROR, "PyList_SetItem() failed, while setting up arguments");
     469             : 
     470        1414 :             if (proc->argnames && proc->argnames[i] &&
     471        1408 :                 PyDict_SetItemString(proc->globals, proc->argnames[i], arg) == -1)
     472           0 :                 PLy_elog(ERROR, "PyDict_SetItemString() failed, while setting up arguments");
     473        1414 :             arg = NULL;
     474             :         }
     475             :     }
     476           0 :     PG_CATCH();
     477             :     {
     478           0 :         Py_XDECREF(arg);
     479           0 :         Py_XDECREF(args);
     480             : 
     481           0 :         PG_RE_THROW();
     482             :     }
     483        1018 :     PG_END_TRY();
     484             : 
     485        1018 :     return args;
     486             : }
     487             : 
     488             : /*
     489             :  * Construct a PLySavedArgs struct representing the current values of the
     490             :  * procedure's arguments in its globals dict.  This can be used to restore
     491             :  * those values when exiting a recursive call level or returning control to a
     492             :  * set-returning function.
     493             :  *
     494             :  * This would not be necessary except for an ancient decision to make args
     495             :  * available via the proc's globals :-( ... but we're stuck with that now.
     496             :  */
     497             : static PLySavedArgs *
     498         312 : PLy_function_save_args(PLyProcedure *proc)
     499             : {
     500             :     PLySavedArgs *result;
     501             : 
     502             :     /* saved args are always allocated in procedure's context */
     503             :     result = (PLySavedArgs *)
     504         312 :         MemoryContextAllocZero(proc->mcxt,
     505         312 :                                offsetof(PLySavedArgs, namedargs) +
     506         312 :                                proc->nargs * sizeof(PyObject *));
     507         312 :     result->nargs = proc->nargs;
     508             : 
     509             :     /* Fetch the "args" list */
     510         312 :     result->args = PyDict_GetItemString(proc->globals, "args");
     511         312 :     Py_XINCREF(result->args);
     512             : 
     513             :     /* If it's a trigger, also save "TD" */
     514         312 :     if (proc->is_trigger)
     515             :     {
     516           2 :         result->td = PyDict_GetItemString(proc->globals, "TD");
     517           2 :         Py_XINCREF(result->td);
     518             :     }
     519             : 
     520             :     /* Fetch all the named arguments */
     521         312 :     if (proc->argnames)
     522             :     {
     523             :         int         i;
     524             : 
     525         602 :         for (i = 0; i < result->nargs; i++)
     526             :         {
     527         416 :             if (proc->argnames[i])
     528             :             {
     529         832 :                 result->namedargs[i] = PyDict_GetItemString(proc->globals,
     530         416 :                                                             proc->argnames[i]);
     531         416 :                 Py_XINCREF(result->namedargs[i]);
     532             :             }
     533             :         }
     534             :     }
     535             : 
     536         312 :     return result;
     537             : }
     538             : 
     539             : /*
     540             :  * Restore procedure's arguments from a PLySavedArgs struct,
     541             :  * then free the struct.
     542             :  */
     543             : static void
     544         308 : PLy_function_restore_args(PLyProcedure *proc, PLySavedArgs *savedargs)
     545             : {
     546             :     /* Restore named arguments into their slots in the globals dict */
     547         308 :     if (proc->argnames)
     548             :     {
     549             :         int         i;
     550             : 
     551         602 :         for (i = 0; i < savedargs->nargs; i++)
     552             :         {
     553         416 :             if (proc->argnames[i] && savedargs->namedargs[i])
     554             :             {
     555         416 :                 PyDict_SetItemString(proc->globals, proc->argnames[i],
     556             :                                      savedargs->namedargs[i]);
     557         416 :                 Py_DECREF(savedargs->namedargs[i]);
     558             :             }
     559             :         }
     560             :     }
     561             : 
     562             :     /* Restore the "args" object, too */
     563         308 :     if (savedargs->args)
     564             :     {
     565         306 :         PyDict_SetItemString(proc->globals, "args", savedargs->args);
     566         306 :         Py_DECREF(savedargs->args);
     567             :     }
     568             : 
     569             :     /* Restore the "TD" object, too */
     570         308 :     if (savedargs->td)
     571             :     {
     572           2 :         PyDict_SetItemString(proc->globals, "TD", savedargs->td);
     573           2 :         Py_DECREF(savedargs->td);
     574             :     }
     575             : 
     576             :     /* And free the PLySavedArgs struct */
     577         308 :     pfree(savedargs);
     578         308 : }
     579             : 
     580             : /*
     581             :  * Free a PLySavedArgs struct without restoring the values.
     582             :  */
     583             : static void
     584           4 : PLy_function_drop_args(PLySavedArgs *savedargs)
     585             : {
     586             :     int         i;
     587             : 
     588             :     /* Drop references for named args */
     589           4 :     for (i = 0; i < savedargs->nargs; i++)
     590             :     {
     591           0 :         Py_XDECREF(savedargs->namedargs[i]);
     592             :     }
     593             : 
     594             :     /* Drop refs to the "args" and "TD" objects, too */
     595           4 :     Py_XDECREF(savedargs->args);
     596           4 :     Py_XDECREF(savedargs->td);
     597             : 
     598             :     /* And free the PLySavedArgs struct */
     599           4 :     pfree(savedargs);
     600           4 : }
     601             : 
     602             : /*
     603             :  * Save away any existing arguments for the given procedure, so that we can
     604             :  * install new values for a recursive call.  This should be invoked before
     605             :  * doing PLy_function_build_args() or PLy_trigger_build_args().
     606             :  *
     607             :  * NB: callers must ensure that PLy_global_args_pop gets invoked once, and
     608             :  * only once, per successful completion of PLy_global_args_push.  Otherwise
     609             :  * we'll end up out-of-sync between the actual call stack and the contents
     610             :  * of proc->argstack.
     611             :  */
     612             : static void
     613        1402 : PLy_global_args_push(PLyProcedure *proc)
     614             : {
     615             :     /* We only need to push if we are already inside some active call */
     616        1402 :     if (proc->calldepth > 0)
     617             :     {
     618             :         PLySavedArgs *node;
     619             : 
     620             :         /* Build a struct containing current argument values */
     621          22 :         node = PLy_function_save_args(proc);
     622             : 
     623             :         /*
     624             :          * Push the saved argument values into the procedure's stack.  Once we
     625             :          * modify either proc->argstack or proc->calldepth, we had better
     626             :          * return without the possibility of error.
     627             :          */
     628          22 :         node->next = proc->argstack;
     629          22 :         proc->argstack = node;
     630             :     }
     631        1402 :     proc->calldepth++;
     632        1402 : }
     633             : 
     634             : /*
     635             :  * Pop old arguments when exiting a recursive call.
     636             :  *
     637             :  * Note: the idea here is to adjust the proc's callstack state before doing
     638             :  * anything that could possibly fail.  In event of any error, we want the
     639             :  * callstack to look like we've done the pop.  Leaking a bit of memory is
     640             :  * tolerable.
     641             :  */
     642             : static void
     643        1402 : PLy_global_args_pop(PLyProcedure *proc)
     644             : {
     645             :     Assert(proc->calldepth > 0);
     646             :     /* We only need to pop if we were already inside some active call */
     647        1402 :     if (proc->calldepth > 1)
     648             :     {
     649          22 :         PLySavedArgs *ptr = proc->argstack;
     650             : 
     651             :         /* Pop the callstack */
     652             :         Assert(ptr != NULL);
     653          22 :         proc->argstack = ptr->next;
     654          22 :         proc->calldepth--;
     655             : 
     656             :         /* Restore argument values, then free ptr */
     657          22 :         PLy_function_restore_args(proc, ptr);
     658             :     }
     659             :     else
     660             :     {
     661             :         /* Exiting call depth 1 */
     662             :         Assert(proc->argstack == NULL);
     663        1380 :         proc->calldepth--;
     664             : 
     665             :         /*
     666             :          * We used to delete the named arguments (but not "args") from the
     667             :          * proc's globals dict when exiting the outermost call level for a
     668             :          * function.  This seems rather pointless though: nothing can see the
     669             :          * dict until the function is called again, at which time we'll
     670             :          * overwrite those dict entries.  So don't bother with that.
     671             :          */
     672             :     }
     673        1402 : }
     674             : 
     675             : /*
     676             :  * Memory context deletion callback for cleaning up a PLySRFState.
     677             :  * We need this in case execution of the SRF is terminated early,
     678             :  * due to error or the caller simply not running it to completion.
     679             :  */
     680             : static void
     681          98 : plpython_srf_cleanup_callback(void *arg)
     682             : {
     683          98 :     PLySRFState *srfstate = (PLySRFState *) arg;
     684             : 
     685             :     /* Release refcount on the iter, if we still have one */
     686          98 :     Py_XDECREF(srfstate->iter);
     687          98 :     srfstate->iter = NULL;
     688             :     /* And drop any saved args; we won't need them */
     689          98 :     if (srfstate->savedargs)
     690           0 :         PLy_function_drop_args(srfstate->savedargs);
     691          98 :     srfstate->savedargs = NULL;
     692          98 : }
     693             : 
     694             : static void
     695          74 : plpython_return_error_callback(void *arg)
     696             : {
     697          74 :     PLyExecutionContext *exec_ctx = PLy_current_execution_context();
     698             : 
     699          74 :     if (exec_ctx->curr_proc &&
     700          74 :         !exec_ctx->curr_proc->is_procedure)
     701          72 :         errcontext("while creating return value");
     702          74 : }
     703             : 
     704             : static PyObject *
     705          98 : PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *rv)
     706             : {
     707          98 :     TriggerData *tdata = (TriggerData *) fcinfo->context;
     708          98 :     TupleDesc   rel_descr = RelationGetDescr(tdata->tg_relation);
     709             :     PyObject   *pltname,
     710             :                *pltevent,
     711             :                *pltwhen,
     712             :                *pltlevel,
     713             :                *pltrelid,
     714             :                *plttablename,
     715             :                *plttableschema,
     716             :                *pltargs,
     717             :                *pytnew,
     718             :                *pytold,
     719             :                *pltdata;
     720             :     char       *stroid;
     721             : 
     722             :     /*
     723             :      * Make any Py*_New() calls before the PG_TRY block so that we can quickly
     724             :      * return NULL on failure.  We can't return within the PG_TRY block, else
     725             :      * we'd miss unwinding the exception stack.
     726             :      */
     727          98 :     pltdata = PyDict_New();
     728          98 :     if (!pltdata)
     729           0 :         return NULL;
     730             : 
     731          98 :     if (tdata->tg_trigger->tgnargs)
     732             :     {
     733          32 :         pltargs = PyList_New(tdata->tg_trigger->tgnargs);
     734          32 :         if (!pltargs)
     735             :         {
     736           0 :             Py_DECREF(pltdata);
     737           0 :             return NULL;
     738             :         }
     739             :     }
     740             :     else
     741             :     {
     742          66 :         Py_INCREF(Py_None);
     743          66 :         pltargs = Py_None;
     744             :     }
     745             : 
     746          98 :     PG_TRY();
     747             :     {
     748          98 :         pltname = PLyUnicode_FromString(tdata->tg_trigger->tgname);
     749          98 :         PyDict_SetItemString(pltdata, "name", pltname);
     750          98 :         Py_DECREF(pltname);
     751             : 
     752          98 :         stroid = DatumGetCString(DirectFunctionCall1(oidout,
     753             :                                                      ObjectIdGetDatum(tdata->tg_relation->rd_id)));
     754          98 :         pltrelid = PLyUnicode_FromString(stroid);
     755          98 :         PyDict_SetItemString(pltdata, "relid", pltrelid);
     756          98 :         Py_DECREF(pltrelid);
     757          98 :         pfree(stroid);
     758             : 
     759          98 :         stroid = SPI_getrelname(tdata->tg_relation);
     760          98 :         plttablename = PLyUnicode_FromString(stroid);
     761          98 :         PyDict_SetItemString(pltdata, "table_name", plttablename);
     762          98 :         Py_DECREF(plttablename);
     763          98 :         pfree(stroid);
     764             : 
     765          98 :         stroid = SPI_getnspname(tdata->tg_relation);
     766          98 :         plttableschema = PLyUnicode_FromString(stroid);
     767          98 :         PyDict_SetItemString(pltdata, "table_schema", plttableschema);
     768          98 :         Py_DECREF(plttableschema);
     769          98 :         pfree(stroid);
     770             : 
     771          98 :         if (TRIGGER_FIRED_BEFORE(tdata->tg_event))
     772          72 :             pltwhen = PLyUnicode_FromString("BEFORE");
     773          26 :         else if (TRIGGER_FIRED_AFTER(tdata->tg_event))
     774          20 :             pltwhen = PLyUnicode_FromString("AFTER");
     775           6 :         else if (TRIGGER_FIRED_INSTEAD(tdata->tg_event))
     776           6 :             pltwhen = PLyUnicode_FromString("INSTEAD OF");
     777             :         else
     778             :         {
     779           0 :             elog(ERROR, "unrecognized WHEN tg_event: %u", tdata->tg_event);
     780             :             pltwhen = NULL;     /* keep compiler quiet */
     781             :         }
     782          98 :         PyDict_SetItemString(pltdata, "when", pltwhen);
     783          98 :         Py_DECREF(pltwhen);
     784             : 
     785          98 :         if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
     786             :         {
     787          88 :             pltlevel = PLyUnicode_FromString("ROW");
     788          88 :             PyDict_SetItemString(pltdata, "level", pltlevel);
     789          88 :             Py_DECREF(pltlevel);
     790             : 
     791             :             /*
     792             :              * Note: In BEFORE trigger, stored generated columns are not
     793             :              * computed yet, so don't make them accessible in NEW row.
     794             :              */
     795             : 
     796          88 :             if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
     797             :             {
     798          42 :                 pltevent = PLyUnicode_FromString("INSERT");
     799             : 
     800          42 :                 PyDict_SetItemString(pltdata, "old", Py_None);
     801          84 :                 pytnew = PLy_input_from_tuple(&proc->result_in,
     802             :                                               tdata->tg_trigtuple,
     803             :                                               rel_descr,
     804          42 :                                               !TRIGGER_FIRED_BEFORE(tdata->tg_event));
     805          42 :                 PyDict_SetItemString(pltdata, "new", pytnew);
     806          42 :                 Py_DECREF(pytnew);
     807          42 :                 *rv = tdata->tg_trigtuple;
     808             :             }
     809          46 :             else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
     810             :             {
     811          12 :                 pltevent = PLyUnicode_FromString("DELETE");
     812             : 
     813          12 :                 PyDict_SetItemString(pltdata, "new", Py_None);
     814          12 :                 pytold = PLy_input_from_tuple(&proc->result_in,
     815             :                                               tdata->tg_trigtuple,
     816             :                                               rel_descr,
     817             :                                               true);
     818          12 :                 PyDict_SetItemString(pltdata, "old", pytold);
     819          12 :                 Py_DECREF(pytold);
     820          12 :                 *rv = tdata->tg_trigtuple;
     821             :             }
     822          34 :             else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
     823             :             {
     824          34 :                 pltevent = PLyUnicode_FromString("UPDATE");
     825             : 
     826          68 :                 pytnew = PLy_input_from_tuple(&proc->result_in,
     827             :                                               tdata->tg_newtuple,
     828             :                                               rel_descr,
     829          34 :                                               !TRIGGER_FIRED_BEFORE(tdata->tg_event));
     830          34 :                 PyDict_SetItemString(pltdata, "new", pytnew);
     831          34 :                 Py_DECREF(pytnew);
     832          34 :                 pytold = PLy_input_from_tuple(&proc->result_in,
     833             :                                               tdata->tg_trigtuple,
     834             :                                               rel_descr,
     835             :                                               true);
     836          34 :                 PyDict_SetItemString(pltdata, "old", pytold);
     837          34 :                 Py_DECREF(pytold);
     838          34 :                 *rv = tdata->tg_newtuple;
     839             :             }
     840             :             else
     841             :             {
     842           0 :                 elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
     843             :                 pltevent = NULL;    /* keep compiler quiet */
     844             :             }
     845             : 
     846          88 :             PyDict_SetItemString(pltdata, "event", pltevent);
     847          88 :             Py_DECREF(pltevent);
     848             :         }
     849          10 :         else if (TRIGGER_FIRED_FOR_STATEMENT(tdata->tg_event))
     850             :         {
     851          10 :             pltlevel = PLyUnicode_FromString("STATEMENT");
     852          10 :             PyDict_SetItemString(pltdata, "level", pltlevel);
     853          10 :             Py_DECREF(pltlevel);
     854             : 
     855          10 :             PyDict_SetItemString(pltdata, "old", Py_None);
     856          10 :             PyDict_SetItemString(pltdata, "new", Py_None);
     857          10 :             *rv = NULL;
     858             : 
     859          10 :             if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
     860           2 :                 pltevent = PLyUnicode_FromString("INSERT");
     861           8 :             else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
     862           2 :                 pltevent = PLyUnicode_FromString("DELETE");
     863           6 :             else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
     864           4 :                 pltevent = PLyUnicode_FromString("UPDATE");
     865           2 :             else if (TRIGGER_FIRED_BY_TRUNCATE(tdata->tg_event))
     866           2 :                 pltevent = PLyUnicode_FromString("TRUNCATE");
     867             :             else
     868             :             {
     869           0 :                 elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
     870             :                 pltevent = NULL;    /* keep compiler quiet */
     871             :             }
     872             : 
     873          10 :             PyDict_SetItemString(pltdata, "event", pltevent);
     874          10 :             Py_DECREF(pltevent);
     875             :         }
     876             :         else
     877           0 :             elog(ERROR, "unrecognized LEVEL tg_event: %u", tdata->tg_event);
     878             : 
     879          98 :         if (tdata->tg_trigger->tgnargs)
     880             :         {
     881             :             /*
     882             :              * all strings...
     883             :              */
     884             :             int         i;
     885             :             PyObject   *pltarg;
     886             : 
     887             :             /* pltargs should have been allocated before the PG_TRY block. */
     888             :             Assert(pltargs && pltargs != Py_None);
     889             : 
     890          90 :             for (i = 0; i < tdata->tg_trigger->tgnargs; i++)
     891             :             {
     892          58 :                 pltarg = PLyUnicode_FromString(tdata->tg_trigger->tgargs[i]);
     893             : 
     894             :                 /*
     895             :                  * stolen, don't Py_DECREF
     896             :                  */
     897          58 :                 PyList_SetItem(pltargs, i, pltarg);
     898             :             }
     899             :         }
     900             :         else
     901             :         {
     902             :             Assert(pltargs == Py_None);
     903             :         }
     904          98 :         PyDict_SetItemString(pltdata, "args", pltargs);
     905          98 :         Py_DECREF(pltargs);
     906             :     }
     907           0 :     PG_CATCH();
     908             :     {
     909           0 :         Py_XDECREF(pltargs);
     910           0 :         Py_XDECREF(pltdata);
     911           0 :         PG_RE_THROW();
     912             :     }
     913          98 :     PG_END_TRY();
     914             : 
     915          98 :     return pltdata;
     916             : }
     917             : 
     918             : /*
     919             :  * Apply changes requested by a MODIFY return from a trigger function.
     920             :  */
     921             : static HeapTuple
     922          40 : PLy_modify_tuple(PLyProcedure *proc, PyObject *pltd, TriggerData *tdata,
     923             :                  HeapTuple otup)
     924             : {
     925             :     HeapTuple   rtup;
     926             :     PyObject   *volatile plntup;
     927             :     PyObject   *volatile plkeys;
     928             :     PyObject   *volatile plval;
     929             :     Datum      *volatile modvalues;
     930             :     bool       *volatile modnulls;
     931             :     bool       *volatile modrepls;
     932             :     ErrorContextCallback plerrcontext;
     933             : 
     934          40 :     plerrcontext.callback = plpython_trigger_error_callback;
     935          40 :     plerrcontext.previous = error_context_stack;
     936          40 :     error_context_stack = &plerrcontext;
     937             : 
     938          40 :     plntup = plkeys = plval = NULL;
     939          40 :     modvalues = NULL;
     940          40 :     modnulls = NULL;
     941          40 :     modrepls = NULL;
     942             : 
     943          40 :     PG_TRY();
     944             :     {
     945             :         TupleDesc   tupdesc;
     946             :         int         nkeys,
     947             :                     i;
     948             : 
     949          40 :         if ((plntup = PyDict_GetItemString(pltd, "new")) == NULL)
     950           2 :             ereport(ERROR,
     951             :                     (errcode(ERRCODE_UNDEFINED_OBJECT),
     952             :                      errmsg("TD[\"new\"] deleted, cannot modify row")));
     953          38 :         Py_INCREF(plntup);
     954          38 :         if (!PyDict_Check(plntup))
     955           2 :             ereport(ERROR,
     956             :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
     957             :                      errmsg("TD[\"new\"] is not a dictionary")));
     958             : 
     959          36 :         plkeys = PyDict_Keys(plntup);
     960          36 :         nkeys = PyList_Size(plkeys);
     961             : 
     962          36 :         tupdesc = RelationGetDescr(tdata->tg_relation);
     963             : 
     964          36 :         modvalues = (Datum *) palloc0(tupdesc->natts * sizeof(Datum));
     965          36 :         modnulls = (bool *) palloc0(tupdesc->natts * sizeof(bool));
     966          36 :         modrepls = (bool *) palloc0(tupdesc->natts * sizeof(bool));
     967             : 
     968          90 :         for (i = 0; i < nkeys; i++)
     969             :         {
     970             :             PyObject   *platt;
     971             :             char       *plattstr;
     972             :             int         attn;
     973             :             PLyObToDatum *att;
     974             : 
     975          62 :             platt = PyList_GetItem(plkeys, i);
     976          62 :             if (PyUnicode_Check(platt))
     977          60 :                 plattstr = PLyUnicode_AsString(platt);
     978             :             else
     979             :             {
     980           2 :                 ereport(ERROR,
     981             :                         (errcode(ERRCODE_DATATYPE_MISMATCH),
     982             :                          errmsg("TD[\"new\"] dictionary key at ordinal position %d is not a string", i)));
     983             :                 plattstr = NULL;    /* keep compiler quiet */
     984             :             }
     985          60 :             attn = SPI_fnumber(tupdesc, plattstr);
     986          60 :             if (attn == SPI_ERROR_NOATTRIBUTE)
     987           4 :                 ereport(ERROR,
     988             :                         (errcode(ERRCODE_UNDEFINED_COLUMN),
     989             :                          errmsg("key \"%s\" found in TD[\"new\"] does not exist as a column in the triggering row",
     990             :                                 plattstr)));
     991          56 :             if (attn <= 0)
     992           0 :                 ereport(ERROR,
     993             :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     994             :                          errmsg("cannot set system attribute \"%s\"",
     995             :                                 plattstr)));
     996          56 :             if (TupleDescAttr(tupdesc, attn - 1)->attgenerated)
     997           2 :                 ereport(ERROR,
     998             :                         (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
     999             :                          errmsg("cannot set generated column \"%s\"",
    1000             :                                 plattstr)));
    1001             : 
    1002          54 :             plval = PyDict_GetItem(plntup, platt);
    1003          54 :             if (plval == NULL)
    1004           0 :                 elog(FATAL, "Python interpreter is probably corrupted");
    1005             : 
    1006          54 :             Py_INCREF(plval);
    1007             : 
    1008             :             /* We assume proc->result is set up to convert tuples properly */
    1009          54 :             att = &proc->result.u.tuple.atts[attn - 1];
    1010             : 
    1011         108 :             modvalues[attn - 1] = PLy_output_convert(att,
    1012             :                                                      plval,
    1013          54 :                                                      &modnulls[attn - 1]);
    1014          54 :             modrepls[attn - 1] = true;
    1015             : 
    1016          54 :             Py_DECREF(plval);
    1017          54 :             plval = NULL;
    1018             :         }
    1019             : 
    1020          28 :         rtup = heap_modify_tuple(otup, tupdesc, modvalues, modnulls, modrepls);
    1021             :     }
    1022          12 :     PG_CATCH();
    1023             :     {
    1024          12 :         Py_XDECREF(plntup);
    1025          12 :         Py_XDECREF(plkeys);
    1026          12 :         Py_XDECREF(plval);
    1027             : 
    1028          12 :         if (modvalues)
    1029           8 :             pfree(modvalues);
    1030          12 :         if (modnulls)
    1031           8 :             pfree(modnulls);
    1032          12 :         if (modrepls)
    1033           8 :             pfree(modrepls);
    1034             : 
    1035          12 :         PG_RE_THROW();
    1036             :     }
    1037          28 :     PG_END_TRY();
    1038             : 
    1039          28 :     Py_DECREF(plntup);
    1040          28 :     Py_DECREF(plkeys);
    1041             : 
    1042          28 :     pfree(modvalues);
    1043          28 :     pfree(modnulls);
    1044          28 :     pfree(modrepls);
    1045             : 
    1046          28 :     error_context_stack = plerrcontext.previous;
    1047             : 
    1048          28 :     return rtup;
    1049             : }
    1050             : 
    1051             : static void
    1052          12 : plpython_trigger_error_callback(void *arg)
    1053             : {
    1054          12 :     PLyExecutionContext *exec_ctx = PLy_current_execution_context();
    1055             : 
    1056          12 :     if (exec_ctx->curr_proc)
    1057          12 :         errcontext("while modifying trigger row");
    1058          12 : }
    1059             : 
    1060             : /* execute Python code, propagate Python errors to the backend */
    1061             : static PyObject *
    1062        1116 : PLy_procedure_call(PLyProcedure *proc, const char *kargs, PyObject *vargs)
    1063             : {
    1064        1116 :     PyObject   *rv = NULL;
    1065        1116 :     int volatile save_subxact_level = list_length(explicit_subtransactions);
    1066             : 
    1067        1116 :     PyDict_SetItemString(proc->globals, kargs, vargs);
    1068             : 
    1069        1116 :     PG_TRY();
    1070             :     {
    1071             : #if PY_VERSION_HEX >= 0x03020000
    1072        1116 :         rv = PyEval_EvalCode(proc->code,
    1073             :                              proc->globals, proc->globals);
    1074             : #else
    1075             :         rv = PyEval_EvalCode((PyCodeObject *) proc->code,
    1076             :                              proc->globals, proc->globals);
    1077             : #endif
    1078             : 
    1079             :         /*
    1080             :          * Since plpy will only let you close subtransactions that you
    1081             :          * started, you cannot *unnest* subtransactions, only *nest* them
    1082             :          * without closing.
    1083             :          */
    1084             :         Assert(list_length(explicit_subtransactions) >= save_subxact_level);
    1085             :     }
    1086           0 :     PG_FINALLY();
    1087             :     {
    1088        1116 :         PLy_abort_open_subtransactions(save_subxact_level);
    1089             :     }
    1090        1116 :     PG_END_TRY();
    1091             : 
    1092             :     /* If the Python code returned an error, propagate it */
    1093        1116 :     if (rv == NULL)
    1094         108 :         PLy_elog(ERROR, NULL);
    1095             : 
    1096        1008 :     return rv;
    1097             : }
    1098             : 
    1099             : /*
    1100             :  * Abort lingering subtransactions that have been explicitly started
    1101             :  * by plpy.subtransaction().start() and not properly closed.
    1102             :  */
    1103             : static void
    1104        1116 : PLy_abort_open_subtransactions(int save_subxact_level)
    1105             : {
    1106             :     Assert(save_subxact_level >= 0);
    1107             : 
    1108        1128 :     while (list_length(explicit_subtransactions) > save_subxact_level)
    1109             :     {
    1110             :         PLySubtransactionData *subtransactiondata;
    1111             : 
    1112             :         Assert(explicit_subtransactions != NIL);
    1113             : 
    1114          12 :         ereport(WARNING,
    1115             :                 (errmsg("forcibly aborting a subtransaction that has not been exited")));
    1116             : 
    1117          12 :         RollbackAndReleaseCurrentSubTransaction();
    1118             : 
    1119          12 :         subtransactiondata = (PLySubtransactionData *) linitial(explicit_subtransactions);
    1120          12 :         explicit_subtransactions = list_delete_first(explicit_subtransactions);
    1121             : 
    1122          12 :         MemoryContextSwitchTo(subtransactiondata->oldcontext);
    1123          12 :         CurrentResourceOwner = subtransactiondata->oldowner;
    1124          12 :         pfree(subtransactiondata);
    1125             :     }
    1126        1116 : }

Generated by: LCOV version 1.14