LCOV - differential code coverage report
Current view: top level - src/pl/plpython - plpy_procedure.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DUB DCB
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 95.8 % 191 183 8 4 179 2 18
Current Date: 2026-11-28 16:48:44 +0900 Functions: 100.0 % 10 10 1 9
Baseline: lcov-20261128-baseline Branches: 74.3 % 136 101 2 33 4 97
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 96.3 % 27 26 1 2 24
(30,360] days: 95.7 % 47 45 2 2 43
(360..) days: 95.7 % 117 112 5 112
Function coverage date bins:
(7,30] days: 100.0 % 1 1 1
(30,360] days: 100.0 % 5 5 1 4
(360..) days: 100.0 % 4 4 4
Branch coverage date bins:
(7,30] days: 75.0 % 24 18 2 4 4 14
(30,360] days: 73.3 % 30 22 8 22
(360..) days: 74.4 % 82 61 21 61

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*
                                  2                 :                :  * Python procedure manipulation for plpython
                                  3                 :                :  *
                                  4                 :                :  * src/pl/plpython/plpy_procedure.c
                                  5                 :                :  */
                                  6                 :                : 
                                  7                 :                : #include "postgres.h"
                                  8                 :                : 
                                  9                 :                : #include "catalog/pg_proc.h"
                                 10                 :                : #include "catalog/pg_type.h"
                                 11                 :                : #include "commands/event_trigger.h"
                                 12                 :                : #include "commands/trigger.h"
                                 13                 :                : #include "funcapi.h"
                                 14                 :                : #include "plpy_elog.h"
                                 15                 :                : #include "plpy_exec.h"
                                 16                 :                : #include "plpy_main.h"
                                 17                 :                : #include "plpy_procedure.h"
                                 18                 :                : #include "plpy_util.h"
                                 19                 :                : #include "utils/builtins.h"
                                 20                 :                : #include "utils/funccache.h"
                                 21                 :                : #include "utils/hsearch.h"
                                 22                 :                : #include "utils/syscache.h"
                                 23                 :                : 
                                 24                 :                : /*
                                 25                 :                :  * SD dictionaries, one per function (and per relation, for DML triggers).
                                 26                 :                :  * funccache.c may build several PLyProcedures for one function, for example
                                 27                 :                :  * one per input collation, and they must all see the same SD.
                                 28                 :                :  */
                                 29                 :                : typedef struct PLyStaticsKey
                                 30                 :                : {
                                 31                 :                :     Oid         fn_oid;         /* function OID */
                                 32                 :                :     Oid         fn_rel;         /* triggered-on relation or InvalidOid */
                                 33                 :                : } PLyStaticsKey;
                                 34                 :                : 
                                 35                 :                : typedef struct PLyStaticsEntry
                                 36                 :                : {
                                 37                 :                :     PLyStaticsKey key;          /* hash key, must be first */
                                 38                 :                :     PyObject   *statics;        /* the SD dictionary */
                                 39                 :                : } PLyStaticsEntry;
                                 40                 :                : 
                                 41                 :                : static HTAB *PLy_statics_hash = NULL;
                                 42                 :                : 
                                 43                 :                : static void PLy_procedure_create(PLyProcedure *proc,
                                 44                 :                :                                  HeapTuple procTup,
                                 45                 :                :                                  Oid fn_oid,
                                 46                 :                :                                  Oid fn_rel,
                                 47                 :                :                                  PLyTrigType is_trigger);
                                 48                 :                : static PyObject *PLy_procedure_get_statics(Oid fn_oid, Oid fn_rel);
                                 49                 :                : static char *PLy_procedure_munge_source(const char *name, const char *src);
                                 50                 :                : static void PLy_compile_callback(FunctionCallInfo fcinfo,
                                 51                 :                :                                  HeapTuple procTup,
                                 52                 :                :                                  const CachedFunctionHashKey *hashkey,
                                 53                 :                :                                  CachedFunction *cfunc,
                                 54                 :                :                                  bool forValidator);
                                 55                 :                : static void PLy_delete_callback(CachedFunction *cfunc);
                                 56                 :                : static void RemovePLyProcedureCache(void *arg);
                                 57                 :                : 
                                 58                 :                : 
                                 59                 :                : /*
                                 60                 :                :  * PLy_procedure_name: get the name of the specified procedure.
                                 61                 :                :  *
                                 62                 :                :  * NB: this returns the SQL name, not the internal Python procedure name
                                 63                 :                :  */
                                 64                 :                : char *
 5459 peter_e@gmx.net            65                 :CBC         552 : PLy_procedure_name(PLyProcedure *proc)
                                 66                 :                : {
                                 67         [ -  + ]:            552 :     if (proc == NULL)
 5459 peter_e@gmx.net            68                 :UBC           0 :         return "<unknown procedure>";
 5459 peter_e@gmx.net            69                 :CBC         552 :     return proc->proname;
                                 70                 :                : }
                                 71                 :                : 
                                 72                 :                : /*
                                 73                 :                :  * PLy_procedure_get: returns a PLyProcedureCache struct for the function,
                                 74                 :                :  * making it valid if necessary.
                                 75                 :                :  *
                                 76                 :                :  * The PLyProcedureCache contains a pointer to the long-lived PLyProcedure
                                 77                 :                :  * (managed by funccache.c) and execution-specific state like SRF state.
                                 78                 :                :  *
                                 79                 :                :  * For SRFs, if we are resuming execution (srfstate->iter != NULL), we skip
                                 80                 :                :  * revalidation and continue using the same PLyProcedure to ensure consistent
                                 81                 :                :  * behavior throughout the SRF execution.
                                 82                 :                :  */
                                 83                 :                : PLyProcedureCache *
  160 tgl@sss.pgh.pa.us          84                 :           1005 : PLy_procedure_get(FunctionCallInfo fcinfo, bool forValidator)
                                 85                 :                : {
                                 86                 :           1005 :     FmgrInfo   *finfo = fcinfo->flinfo;
                                 87                 :                :     PLyProcedureCache *pcache;
                                 88                 :                :     PLyProcedure *proc;
                                 89                 :                : 
                                 90                 :                :     /*
                                 91                 :                :      * If this is the first execution for this FmgrInfo, set up a cache struct
                                 92                 :                :      * (initially containing null pointers).  The cache must live as long as
                                 93                 :                :      * the FmgrInfo, so it goes in fn_mcxt.  Also set up a memory context
                                 94                 :                :      * callback that will be invoked when fn_mcxt is reset/deleted.
                                 95                 :                :      */
                                 96                 :           1005 :     pcache = finfo->fn_extra;
                                 97         [ +  + ]:           1005 :     if (pcache == NULL)
                                 98                 :                :     {
                                 99                 :                :         pcache = (PLyProcedureCache *)
                                100                 :            810 :             MemoryContextAllocZero(finfo->fn_mcxt, sizeof(PLyProcedureCache));
                                101                 :                : 
                                102                 :            810 :         pcache->fcontext = finfo->fn_mcxt;
                                103                 :            810 :         pcache->mcb.func = RemovePLyProcedureCache;
                                104                 :            810 :         pcache->mcb.arg = pcache;
                                105                 :                : 
                                106                 :            810 :         MemoryContextRegisterResetCallback(finfo->fn_mcxt, &pcache->mcb);
                                107                 :                : 
                                108                 :            810 :         finfo->fn_extra = pcache;
                                109                 :                :     }
                                110                 :                : 
                                111                 :                :     /*
                                112                 :                :      * If we are resuming execution of a set-returning function, just keep
                                113                 :                :      * using the same cache.  We do not ask funccache.c to re-validate the
                                114                 :                :      * PLyProcedure: we want to run to completion using the function's initial
                                115                 :                :      * definition.
                                116                 :                :      *
                                117                 :                :      * A live iterator (srfstate->iter != NULL) reliably means a genuine
                                118                 :                :      * resume: when an iteration ends for any reason, srfstate->iter is reset
                                119                 :                :      * to NULL (see comments for PLy_function_cleanup_srfstate).
                                120                 :                :      */
                                121   [ +  +  +  - ]:           1005 :     if (pcache->srfstate != NULL && pcache->srfstate->iter != NULL)
                                122                 :                :     {
                                123         [ -  + ]:            156 :         Assert(pcache->proc != NULL);
                                124                 :            156 :         return pcache;
                                125                 :                :     }
                                126                 :                : 
                                127                 :                :     /*
                                128                 :                :      * Look up, or re-validate, the long-lived hash entry.  Like SQL-language
                                129                 :                :      * functions, make the hash key depend on the result of
                                130                 :                :      * get_call_result_type() when that's composite, so that we can safely
                                131                 :                :      * assume that we'll build a new hash entry if the composite rowtype
                                132                 :                :      * changes.
                                133                 :                :      */
                                134                 :                :     proc = (PLyProcedure *)
                                135                 :            849 :         cached_function_compile(fcinfo,
                                136                 :            849 :                                 (CachedFunction *) pcache->proc,
                                137                 :                :                                 PLy_compile_callback,
                                138                 :                :                                 PLy_delete_callback,
                                139                 :                :                                 sizeof(PLyProcedure),
                                140                 :                :                                 true,
                                141                 :                :                                 forValidator);
                                142                 :                : 
                                143                 :                :     /*
                                144                 :                :      * Install the hash pointer in the PLyProcedureCache, and increment its
                                145                 :                :      * use count to reflect that.  If cached_function_compile gave us back a
                                146                 :                :      * different hash entry than we were using before, we must decrement that
                                147                 :                :      * one's use count.
                                148                 :                :      */
                                149         [ +  + ]:            845 :     if (proc != pcache->proc)
                                150                 :                :     {
                                151         [ -  + ]:            806 :         if (pcache->proc != NULL)
                                152                 :                :         {
  160 tgl@sss.pgh.pa.us         153         [ #  # ]:UBC           0 :             Assert(pcache->proc->cfunc.use_count > 0);
                                154                 :              0 :             pcache->proc->cfunc.use_count--;
                                155                 :                :         }
  160 tgl@sss.pgh.pa.us         156                 :CBC         806 :         pcache->proc = proc;
                                157                 :            806 :         proc->cfunc.use_count++;
                                158                 :                :     }
                                159                 :                : 
                                160                 :            845 :     return pcache;
                                161                 :                : }
                                162                 :                : 
                                163                 :                : /*
                                164                 :                :  * Create (well, fill in) a new PLyProcedure structure
                                165                 :                :  */
                                166                 :                : static void
                                167                 :            383 : PLy_procedure_create(PLyProcedure *proc,
                                168                 :                :                      HeapTuple procTup,
                                169                 :                :                      Oid fn_oid,
                                170                 :                :                      Oid fn_rel,
                                171                 :                :                      PLyTrigType is_trigger)
                                172                 :                : {
                                173                 :                :     char        procName[NAMEDATALEN + 256];
                                174                 :                :     Form_pg_proc procStruct;
                                175                 :                :     MemoryContext cxt;
                                176                 :                :     MemoryContext oldcxt;
                                177                 :                :     int         rv;
                                178                 :                :     char       *ptr;
                                179                 :                : 
 5459 peter_e@gmx.net           180                 :            383 :     procStruct = (Form_pg_proc) GETSTRUCT(procTup);
                                181                 :            766 :     rv = snprintf(procName, sizeof(procName),
                                182                 :                :                   "__plpython_procedure_%s_%u",
                                183                 :            383 :                   NameStr(procStruct->proname),
                                184                 :                :                   fn_oid);
                                185   [ +  -  -  + ]:            383 :     if (rv >= sizeof(procName) || rv < 0)
 5459 peter_e@gmx.net           186         [ #  # ]:UBC           0 :         elog(ERROR, "procedure name would overrun buffer");
                                187                 :                : 
                                188                 :                :     /* Replace any not-legal-in-Python-names characters with '_' */
 3938 tgl@sss.pgh.pa.us         189         [ +  + ]:CBC       17867 :     for (ptr = procName; *ptr; ptr++)
                                190                 :                :     {
                                191   [ +  +  +  + ]:          17484 :         if (!((*ptr >= 'A' && *ptr <= 'Z') ||
                                192   [ +  +  -  + ]:          17482 :               (*ptr >= 'a' && *ptr <= 'z') ||
                                193   [ +  +  +  + ]:           4702 :               (*ptr >= '0' && *ptr <= '9')))
                                194                 :           2662 :             *ptr = '_';
                                195                 :                :     }
                                196                 :                : 
                                197                 :                :     /* Create long-lived context that all procedure info will live in */
 3168                           198                 :            383 :     cxt = AllocSetContextCreate(TopMemoryContext,
                                199                 :                :                                 "PL/Python function",
                                200                 :                :                                 ALLOCSET_DEFAULT_SIZES);
                                201                 :                : 
 4041                           202                 :            383 :     oldcxt = MemoryContextSwitchTo(cxt);
                                203                 :                : 
                                204                 :            383 :     proc->mcxt = cxt;
                                205                 :                : 
 5459 peter_e@gmx.net           206         [ +  + ]:            383 :     PG_TRY();
                                207                 :                :     {
                                208                 :                :         Datum       protrftypes_datum;
                                209                 :                :         Datum       prosrcdatum;
                                210                 :                :         bool        isnull;
                                211                 :                :         char       *procSource;
                                212                 :                :         int         i;
                                213                 :                : 
 4041 tgl@sss.pgh.pa.us         214                 :            383 :         proc->proname = pstrdup(NameStr(procStruct->proname));
 3168                           215                 :            383 :         MemoryContextSetIdentifier(cxt, proc->proname);
 4041                           216                 :            383 :         proc->pyname = pstrdup(procName);
 3889                           217                 :            383 :         proc->fn_readonly = (procStruct->provolatile != PROVOLATILE_VOLATILE);
                                218                 :            383 :         proc->is_setof = procStruct->proretset;
 3193 peter_e@gmx.net           219                 :            383 :         proc->is_procedure = (procStruct->prokind == PROKIND_PROCEDURE);
  935 tgl@sss.pgh.pa.us         220                 :            383 :         proc->is_trigger = is_trigger;
 3889                           221                 :            383 :         proc->src = NULL;
                                222                 :            383 :         proc->argnames = NULL;
 3299                           223                 :            383 :         proc->args = NULL;
 4041                           224                 :            383 :         proc->nargs = 0;
                                225                 :            383 :         proc->langid = procStruct->prolang;
                                226                 :            383 :         protrftypes_datum = SysCacheGetAttr(PROCOID, procTup,
                                227                 :                :                                             Anum_pg_proc_protrftypes,
                                228                 :                :                                             &isnull);
                                229         [ +  + ]:            383 :         proc->trftypes = isnull ? NIL : oid_array_to_list(protrftypes_datum);
 3889                           230                 :            383 :         proc->code = NULL;
                                231                 :            383 :         proc->statics = NULL;
 4041                           232                 :            383 :         proc->globals = NULL;
 3889                           233                 :            383 :         proc->calldepth = 0;
                                234                 :            383 :         proc->argstack = NULL;
                                235                 :                : 
                                236                 :                :         /*
                                237                 :                :          * get information required for output conversion of the return value,
                                238                 :                :          * but only if this isn't a trigger.
                                239                 :                :          */
  464 peter@eisentraut.org      240         [ +  + ]:            383 :         if (is_trigger == PLPY_NOT_TRIGGER)
                                241                 :                :         {
 3299 tgl@sss.pgh.pa.us         242                 :            329 :             Oid         rettype = procStruct->prorettype;
                                243                 :                : 
                                244                 :                :             /* reject if function is in fact a trigger function */
   27 tgl@sss.pgh.pa.us         245   [ +  +  -  + ]:GNC         329 :             if (rettype == TRIGGEROID || rettype == EVENT_TRIGGEROID)
                                246         [ +  - ]:              1 :                 ereport(ERROR,
                                247                 :                :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                248                 :                :                          errmsg("trigger functions can only be called as triggers")));
                                249                 :                : 
                                250                 :                :             /* set up output function for procedure result */
 3299 tgl@sss.pgh.pa.us         251                 :CBC         328 :             PLy_output_setup_func(&proc->result, proc->mcxt,
                                252                 :                :                                   rettype, -1, proc);
                                253                 :                :         }
                                254                 :                :         else
                                255                 :                :         {
                                256                 :                :             /*
                                257                 :                :              * In a trigger function, we use proc->result and proc->result_in
                                258                 :                :              * for converting tuples, but we don't yet have enough info to set
                                259                 :                :              * them up.  PLy_exec_trigger will deal with it.
                                260                 :                :              */
                                261                 :             54 :             proc->result.typoid = InvalidOid;
                                262                 :             54 :             proc->result_in.typoid = InvalidOid;
                                263                 :                :         }
                                264                 :                : 
                                265                 :                :         /*
                                266                 :                :          * Now get information required for input conversion of the
                                267                 :                :          * procedure's arguments.  Note that we ignore output arguments here.
                                268                 :                :          * If the function returns record, those I/O functions will be set up
                                269                 :                :          * when the function is first called.
                                270                 :                :          */
 5459 peter_e@gmx.net           271         [ +  + ]:            382 :         if (procStruct->pronargs)
                                272                 :                :         {
                                273                 :                :             Oid        *types;
                                274                 :                :             char      **names,
                                275                 :                :                        *modes;
                                276                 :                :             int         pos,
                                277                 :                :                         total;
                                278                 :                : 
                                279                 :                :             /* extract argument type info from the pg_proc tuple */
                                280                 :            175 :             total = get_func_arg_info(procTup, &types, &names, &modes);
                                281                 :                : 
                                282                 :                :             /* count number of in+inout args into proc->nargs */
                                283         [ +  + ]:            175 :             if (modes == NULL)
                                284                 :            151 :                 proc->nargs = total;
                                285                 :                :             else
                                286                 :                :             {
                                287                 :                :                 /* proc->nargs was initialized to 0 above */
                                288         [ +  + ]:             88 :                 for (i = 0; i < total; i++)
                                289                 :                :                 {
 1997 tgl@sss.pgh.pa.us         290         [ +  + ]:             64 :                     if (modes[i] != PROARGMODE_OUT &&
 5459 peter_e@gmx.net           291         [ +  - ]:             38 :                         modes[i] != PROARGMODE_TABLE)
                                292                 :             38 :                         (proc->nargs)++;
                                293                 :                :                 }
                                294                 :                :             }
                                295                 :                : 
                                296                 :                :             /* Allocate arrays for per-input-argument data */
  103 michael@paquier.xyz       297                 :GNC         175 :             proc->argnames = palloc0_array(char *, proc->nargs);
                                298                 :            175 :             proc->args = palloc0_array(PLyDatumToOb, proc->nargs);
                                299                 :                : 
 5459 peter_e@gmx.net           300         [ +  + ]:CBC         465 :             for (i = pos = 0; i < total; i++)
                                301                 :                :             {
                                302         [ +  + ]:            290 :                 if (modes &&
 1997 tgl@sss.pgh.pa.us         303         [ +  + ]:             64 :                     (modes[i] == PROARGMODE_OUT ||
 5459 peter_e@gmx.net           304         [ -  + ]:             38 :                      modes[i] == PROARGMODE_TABLE))
                                305                 :             26 :                     continue;   /* skip OUT arguments */
                                306                 :                : 
                                307         [ -  + ]:            264 :                 Assert(types[i] == procStruct->proargtypes.values[pos]);
                                308                 :                : 
                                309                 :                :                 /* set up I/O function info */
 3299 tgl@sss.pgh.pa.us         310                 :            264 :                 PLy_input_setup_func(&proc->args[pos], proc->mcxt,
                                311                 :            264 :                                      types[i], -1,  /* typmod not known */
                                312                 :                :                                      proc);
                                313                 :                : 
                                314                 :                :                 /* get argument name */
   16                           315                 :            264 :                 proc->argnames[pos] =
                                316   [ +  +  +  - ]:            264 :                     (names && names[i][0] != '\0') ? pstrdup(names[i]) : NULL;
                                317                 :                : 
 5459 peter_e@gmx.net           318                 :            264 :                 pos++;
                                319                 :                :             }
                                320                 :                :         }
                                321                 :                : 
                                322                 :                :         /*
                                323                 :                :          * get the text of the function.
                                324                 :                :          */
 1344 dgustafsson@postgres      325                 :            382 :         prosrcdatum = SysCacheGetAttrNotNull(PROCOID, procTup,
                                326                 :                :                                              Anum_pg_proc_prosrc);
 5459 peter_e@gmx.net           327                 :            382 :         procSource = TextDatumGetCString(prosrcdatum);
                                328                 :                : 
                                329                 :                :         /* SD is shared with other PLyProcedures of the function */
   16 tgl@sss.pgh.pa.us         330                 :            382 :         proc->statics = PLy_procedure_get_statics(fn_oid, fn_rel);
                                331                 :                : 
 5459 peter_e@gmx.net           332                 :            382 :         PLy_procedure_compile(proc, procSource);
                                333                 :                : 
                                334                 :            379 :         pfree(procSource);
                                335                 :                :     }
                                336                 :              4 :     PG_CATCH();
                                337                 :                :     {
 4041 tgl@sss.pgh.pa.us         338                 :              4 :         MemoryContextSwitchTo(oldcxt);
 5459 peter_e@gmx.net           339                 :              4 :         PLy_procedure_delete(proc);
                                340                 :              4 :         PG_RE_THROW();
                                341                 :                :     }
                                342         [ -  + ]:            379 :     PG_END_TRY();
                                343                 :                : 
 4041 tgl@sss.pgh.pa.us         344                 :            379 :     MemoryContextSwitchTo(oldcxt);
 5459 peter_e@gmx.net           345                 :            379 : }
                                346                 :                : 
                                347                 :                : /*
                                348                 :                :  * Insert the procedure into the Python interpreter
                                349                 :                :  */
                                350                 :                : void
                                351                 :            403 : PLy_procedure_compile(PLyProcedure *proc, const char *src)
                                352                 :                : {
                                353                 :            403 :     PyObject   *crv = NULL;
                                354                 :                :     char       *msrc;
                                355                 :                :     PyObject   *code0;
                                356                 :                : 
                                357                 :            403 :     proc->globals = PyDict_Copy(PLy_interp_globals);
                                358                 :                : 
                                359                 :                :     /*
                                360                 :                :      * SD is private preserved data between calls. GD is global data shared by
                                361                 :                :      * all functions.  The caller supplies SD.
                                362                 :                :      */
   16 tgl@sss.pgh.pa.us         363         [ -  + ]:            403 :     Assert(proc->statics != NULL);
 5459 peter_e@gmx.net           364                 :            403 :     PyDict_SetItemString(proc->globals, "SD", proc->statics);
                                365                 :                : 
                                366                 :                :     /*
                                367                 :                :      * insert the function code into the interpreter
                                368                 :                :      */
                                369                 :            403 :     msrc = PLy_procedure_munge_source(proc->pyname, src);
                                370                 :                :     /* Save the mangled source for later inclusion in tracebacks */
 4041 tgl@sss.pgh.pa.us         371                 :            403 :     proc->src = MemoryContextStrdup(proc->mcxt, msrc);
  626 peter@eisentraut.org      372                 :            403 :     code0 = Py_CompileString(msrc, "<string>", Py_file_input);
                                373         [ +  + ]:            403 :     if (code0)
                                374                 :            400 :         crv = PyEval_EvalCode(code0, proc->globals, NULL);
 5459 peter_e@gmx.net           375                 :            403 :     pfree(msrc);
                                376                 :                : 
                                377         [ +  + ]:            403 :     if (crv != NULL)
                                378                 :                :     {
                                379                 :                :         int         clen;
                                380                 :                :         char        call[NAMEDATALEN + 256];
                                381                 :                : 
                                382                 :                :         Py_DECREF(crv);
                                383                 :                : 
                                384                 :                :         /*
                                385                 :                :          * compile a call to the function
                                386                 :                :          */
                                387                 :            400 :         clen = snprintf(call, sizeof(call), "%s()", proc->pyname);
                                388   [ +  -  -  + ]:            400 :         if (clen < 0 || clen >= sizeof(call))
 5459 peter_e@gmx.net           389         [ #  # ]:UBC           0 :             elog(ERROR, "string would overflow buffer");
 5459 peter_e@gmx.net           390                 :CBC         400 :         proc->code = Py_CompileString(call, "<string>", Py_eval_input);
                                391         [ +  - ]:            400 :         if (proc->code != NULL)
                                392                 :            400 :             return;
                                393                 :                :     }
                                394                 :                : 
                                395         [ +  - ]:              3 :     if (proc->proname)
                                396                 :              3 :         PLy_elog(ERROR, "could not compile PL/Python function \"%s\"",
                                397                 :                :                  proc->proname);
                                398                 :                :     else
 5459 peter_e@gmx.net           399                 :UBC           0 :         PLy_elog(ERROR, "could not compile anonymous PL/Python code block");
                                400                 :                : }
                                401                 :                : 
                                402                 :                : void
 5459 peter_e@gmx.net           403                 :CBC          32 : PLy_procedure_delete(PLyProcedure *proc)
                                404                 :                : {
                                405                 :             32 :     Py_XDECREF(proc->code);
                                406                 :             32 :     Py_XDECREF(proc->statics);
                                407                 :             32 :     Py_XDECREF(proc->globals);
 4041 tgl@sss.pgh.pa.us         408                 :             32 :     MemoryContextDelete(proc->mcxt);
 5459 peter_e@gmx.net           409                 :             32 : }
                                410                 :                : 
                                411                 :                : static char *
                                412                 :            403 : PLy_procedure_munge_source(const char *name, const char *src)
                                413                 :                : {
                                414                 :                :     char       *mrc,
                                415                 :                :                *mp;
                                416                 :                :     const char *sp;
                                417                 :                :     size_t      mlen;
                                418                 :                :     int         plen;
                                419                 :                : 
                                420                 :                :     /*
                                421                 :                :      * room for function source and the def statement
                                422                 :                :      */
                                423                 :            403 :     mlen = (strlen(src) * 2) + strlen(name) + 16;
                                424                 :                : 
                                425                 :            403 :     mrc = palloc(mlen);
                                426                 :            403 :     plen = snprintf(mrc, mlen, "def %s():\n\t", name);
                                427   [ +  -  -  + ]:            403 :     Assert(plen >= 0 && plen < mlen);
                                428                 :                : 
                                429                 :            403 :     sp = src;
                                430                 :            403 :     mp = mrc + plen;
                                431                 :                : 
                                432         [ +  + ]:          45003 :     while (*sp != '\0')
                                433                 :                :     {
                                434   [ +  +  +  + ]:          44600 :         if (*sp == '\r' && *(sp + 1) == '\n')
                                435                 :              3 :             sp++;
                                436                 :                : 
                                437   [ +  +  +  + ]:          44600 :         if (*sp == '\n' || *sp == '\r')
                                438                 :                :         {
                                439                 :           1908 :             *mp++ = '\n';
                                440                 :           1908 :             *mp++ = '\t';
                                441                 :           1908 :             sp++;
                                442                 :                :         }
                                443                 :                :         else
                                444                 :          42692 :             *mp++ = *sp++;
                                445                 :                :     }
                                446                 :            403 :     *mp++ = '\n';
                                447                 :            403 :     *mp++ = '\n';
                                448                 :            403 :     *mp = '\0';
                                449                 :                : 
                                450         [ -  + ]:            403 :     if (mp > (mrc + mlen))
 2672 michael@paquier.xyz       451         [ #  # ]:UBC           0 :         elog(FATAL, "buffer overrun in PLy_procedure_munge_source");
                                452                 :                : 
 5459 peter_e@gmx.net           453                 :CBC         403 :     return mrc;
                                454                 :                : }
                                455                 :                : 
                                456                 :                : /*
                                457                 :                :  * Compile callback for funccache.c.
                                458                 :                :  *
                                459                 :                :  * cached_function_compile() calls this when it needs to (re)compile the
                                460                 :                :  * long-lived PLyProcedure for a function.  The CachedFunction handed to us is
                                461                 :                :  * pre-zeroed workspace of size sizeof(PLyProcedure); we just have to fill in
                                462                 :                :  * the PL/Python-specific fields.
                                463                 :                :  */
                                464                 :                : static void
  160 tgl@sss.pgh.pa.us         465                 :            383 : PLy_compile_callback(FunctionCallInfo fcinfo,
                                466                 :                :                      HeapTuple procTup,
                                467                 :                :                      const CachedFunctionHashKey *hashkey,
                                468                 :                :                      CachedFunction *cfunc,
                                469                 :                :                      bool forValidator)
                                470                 :                : {
                                471                 :            383 :     PLyProcedure *proc = (PLyProcedure *) cfunc;
                                472                 :            383 :     Oid         fn_oid = fcinfo->flinfo->fn_oid;
   16                           473                 :            383 :     Oid         fn_rel = InvalidOid;
                                474                 :                :     PLyTrigType is_trigger;
                                475                 :                : 
                                476                 :                :     /*
                                477                 :                :      * Derive the trigger type from the call context, matching what
                                478                 :                :      * plpython3_call_handler dispatches on.  DML triggers get one SD per
                                479                 :                :      * relation.  The validator has no relation.
                                480                 :                :      */
  160                           481   [ +  +  +  + ]:            383 :     if (CALLED_AS_TRIGGER(fcinfo))
                                482                 :                :     {
                                483                 :             53 :         is_trigger = PLPY_TRIGGER;
   16                           484         [ +  + ]:             53 :         if (!forValidator)
                                485                 :             29 :             fn_rel = RelationGetRelid(((TriggerData *) fcinfo->context)->tg_relation);
                                486                 :                :     }
  160                           487   [ +  +  +  + ]:            330 :     else if (CALLED_AS_EVENT_TRIGGER(fcinfo))
                                488                 :              1 :         is_trigger = PLPY_EVENT_TRIGGER;
                                489                 :                :     else
                                490                 :            329 :         is_trigger = PLPY_NOT_TRIGGER;
                                491                 :                : 
   16                           492                 :            383 :     PLy_procedure_create(proc, procTup, fn_oid, fn_rel, is_trigger);
                                493                 :            379 : }
                                494                 :                : 
                                495                 :                : /*
                                496                 :                :  * Get the SD dictionary of a function, as a new reference.
                                497                 :                :  */
                                498                 :                : static PyObject *
                                499                 :            382 : PLy_procedure_get_statics(Oid fn_oid, Oid fn_rel)
                                500                 :                : {
                                501                 :                :     PLyStaticsKey key;
                                502                 :                :     PLyStaticsEntry *entry;
                                503                 :                :     bool        found;
                                504                 :                : 
                                505         [ +  + ]:            382 :     if (PLy_statics_hash == NULL)
                                506                 :                :     {
                                507                 :                :         HASHCTL     ctl;
                                508                 :                : 
                                509                 :             22 :         ctl.keysize = sizeof(PLyStaticsKey);
                                510                 :             22 :         ctl.entrysize = sizeof(PLyStaticsEntry);
                                511                 :             22 :         PLy_statics_hash = hash_create("PL/Python SD dictionaries", 32,
                                512                 :                :                                        &ctl, HASH_ELEM | HASH_BLOBS);
                                513                 :                :     }
                                514                 :                : 
                                515                 :            382 :     key.fn_oid = fn_oid;
                                516                 :            382 :     key.fn_rel = fn_rel;
                                517                 :            382 :     entry = hash_search(PLy_statics_hash, &key, HASH_ENTER, &found);
                                518         [ +  + ]:            382 :     if (!found)
                                519                 :            291 :         entry->statics = NULL;
                                520                 :                : 
                                521                 :                :     /* A failed PyDict_New leaves the entry empty, so retry it here */
                                522         [ +  + ]:            382 :     if (entry->statics == NULL)
                                523                 :                :     {
                                524                 :            291 :         entry->statics = PyDict_New();
                                525         [ -  + ]:            291 :         if (entry->statics == NULL)
   16 tgl@sss.pgh.pa.us         526                 :UBC           0 :             PLy_elog(ERROR, NULL);
                                527                 :                :     }
                                528                 :                : 
   16 tgl@sss.pgh.pa.us         529         [ -  + ]:CBC         382 :     Py_INCREF(entry->statics);
                                530                 :            382 :     return entry->statics;
                                531                 :                : }
                                532                 :                : 
                                533                 :                : /*
                                534                 :                :  * Deletion callback for funccache.c.
                                535                 :                :  *
                                536                 :                :  * cached_function_compile() calls this when it discards a cache entry, which
                                537                 :                :  * only happens once the entry's use count has dropped to zero.  We must free
                                538                 :                :  * the subsidiary data but not the CachedFunction struct itself.
                                539                 :                :  */
                                540                 :                : static void
  160                           541                 :              7 : PLy_delete_callback(CachedFunction *cfunc)
                                542                 :                : {
                                543                 :              7 :     PLyProcedure *proc = (PLyProcedure *) cfunc;
                                544                 :                : 
                                545         [ -  + ]:              7 :     Assert(proc->cfunc.use_count == 0);
                                546         [ -  + ]:              7 :     Assert(proc->calldepth == 0);
                                547                 :                : 
                                548                 :              7 :     PLy_procedure_delete(proc);
                                549                 :              7 : }
                                550                 :                : 
                                551                 :                : /*
                                552                 :                :  * MemoryContext callback function
                                553                 :                :  *
                                554                 :                :  * We register this in the memory context that contains a PLyProcedureCache
                                555                 :                :  * struct (that is, the FmgrInfo's fn_mcxt).  When the memory context is reset
                                556                 :                :  * or deleted, we release the reference count (if any) that the cache holds on
                                557                 :                :  * the long-lived hash entry.  Note that this will happen even during error
                                558                 :                :  * aborts.
                                559                 :                :  *
                                560                 :                :  * This is also our opportunity to release the Python references held by an
                                561                 :                :  * interrupted set-returning function.  ShutdownPLyFunction() handles that for
                                562                 :                :  * routine in-query cancellation cases, but it does not run during an error
                                563                 :                :  * abort; this callback does, so it is the backstop that prevents leaking the
                                564                 :                :  * SRF's iterator and saved arguments when a query errors out mid-iteration.
                                565                 :                :  */
                                566                 :                : static void
                                567                 :            810 : RemovePLyProcedureCache(void *arg)
                                568                 :                : {
                                569                 :            810 :     PLyProcedureCache *pcache = (PLyProcedureCache *) arg;
                                570                 :                : 
                                571                 :                :     /* Release any Python state left behind by an interrupted SRF */
                                572                 :            810 :     PLy_function_cleanup_srfstate(pcache);
                                573                 :                : 
                                574                 :                :     /* Release reference count on PLyProcedure */
                                575         [ +  + ]:            810 :     if (pcache->proc != NULL)
                                576                 :                :     {
                                577         [ -  + ]:            536 :         Assert(pcache->proc->cfunc.use_count > 0);
                                578                 :            536 :         pcache->proc->cfunc.use_count--;
                                579                 :            536 :         pcache->proc = NULL;
                                580                 :                :     }
                                581                 :                : 
                                582                 :                :     /* We needn't free the pcache object itself, context cleanup does that */
                                583                 :            810 : }
        

Generated by: LCOV version 2.0-1