LCOV - differential code coverage report
Current view: top level - src/pl/plpython - plpy_exec.c (source / functions) Coverage Total Hit LBC UIC UBC GBC GIC GNC CBC EUB ECB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 93.6 % 419 392 10 10 7 8 137 247 12 133 2
Current Date: 2023-04-08 17:13:01 Functions: 100.0 % 15 15 13 1 1 13
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 (240..) days: 93.6 % 419 392 10 10 7 8 137 247 12 133
Legend: Lines: hit not hit Function coverage date bins:
(240..) days: 53.6 % 28 15 13 1 1 13

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

Generated by: LCOV version v1.16-55-g56c0a2a