LCOV - code coverage report
Current view: top level - src/pl/plpython - plpy_procedure.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 168 187 89.8 %
Date: 2020-06-01 00:06:26 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          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 "access/htup_details.h"
      10             : #include "access/transam.h"
      11             : #include "funcapi.h"
      12             : #include "catalog/pg_proc.h"
      13             : #include "catalog/pg_type.h"
      14             : #include "plpy_elog.h"
      15             : #include "plpy_main.h"
      16             : #include "plpy_procedure.h"
      17             : #include "plpython.h"
      18             : #include "utils/builtins.h"
      19             : #include "utils/hsearch.h"
      20             : #include "utils/inval.h"
      21             : #include "utils/lsyscache.h"
      22             : #include "utils/memutils.h"
      23             : #include "utils/syscache.h"
      24             : 
      25             : static HTAB *PLy_procedure_cache = NULL;
      26             : 
      27             : static PLyProcedure *PLy_procedure_create(HeapTuple procTup, Oid fn_oid, bool is_trigger);
      28             : static bool PLy_procedure_valid(PLyProcedure *proc, HeapTuple procTup);
      29             : static char *PLy_procedure_munge_source(const char *name, const char *src);
      30             : 
      31             : 
      32             : void
      33          46 : init_procedure_caches(void)
      34             : {
      35             :     HASHCTL     hash_ctl;
      36             : 
      37          46 :     memset(&hash_ctl, 0, sizeof(hash_ctl));
      38          46 :     hash_ctl.keysize = sizeof(PLyProcedureKey);
      39          46 :     hash_ctl.entrysize = sizeof(PLyProcedureEntry);
      40          46 :     PLy_procedure_cache = hash_create("PL/Python procedures", 32, &hash_ctl,
      41             :                                       HASH_ELEM | HASH_BLOBS);
      42          46 : }
      43             : 
      44             : /*
      45             :  * PLy_procedure_name: get the name of the specified procedure.
      46             :  *
      47             :  * NB: this returns the SQL name, not the internal Python procedure name
      48             :  */
      49             : char *
      50         992 : PLy_procedure_name(PLyProcedure *proc)
      51             : {
      52         992 :     if (proc == NULL)
      53           0 :         return "<unknown procedure>";
      54         992 :     return proc->proname;
      55             : }
      56             : 
      57             : /*
      58             :  * PLy_procedure_get: returns a cached PLyProcedure, or creates, stores and
      59             :  * returns a new PLyProcedure.
      60             :  *
      61             :  * fn_oid is the OID of the function requested
      62             :  * fn_rel is InvalidOid or the relation this function triggers on
      63             :  * is_trigger denotes whether the function is a trigger function
      64             :  *
      65             :  * The reason that both fn_rel and is_trigger need to be passed is that when
      66             :  * trigger functions get validated we don't know which relation(s) they'll
      67             :  * be used with, so no sensible fn_rel can be passed.
      68             :  */
      69             : PLyProcedure *
      70        1804 : PLy_procedure_get(Oid fn_oid, Oid fn_rel, bool is_trigger)
      71             : {
      72        1804 :     bool        use_cache = !(is_trigger && fn_rel == InvalidOid);
      73             :     HeapTuple   procTup;
      74             :     PLyProcedureKey key;
      75        1804 :     PLyProcedureEntry *volatile entry = NULL;
      76        1804 :     PLyProcedure *volatile proc = NULL;
      77        1804 :     bool        found = false;
      78             : 
      79        1804 :     procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(fn_oid));
      80        1804 :     if (!HeapTupleIsValid(procTup))
      81           0 :         elog(ERROR, "cache lookup failed for function %u", fn_oid);
      82             : 
      83             :     /*
      84             :      * Look for the function in the cache, unless we don't have the necessary
      85             :      * information (e.g. during validation). In that case we just don't cache
      86             :      * anything.
      87             :      */
      88        1804 :     if (use_cache)
      89             :     {
      90        1758 :         key.fn_oid = fn_oid;
      91        1758 :         key.fn_rel = fn_rel;
      92        1758 :         entry = hash_search(PLy_procedure_cache, &key, HASH_ENTER, &found);
      93        1758 :         proc = entry->proc;
      94             :     }
      95             : 
      96        1804 :     PG_TRY();
      97             :     {
      98        1804 :         if (!found)
      99             :         {
     100             :             /* Haven't found it, create a new procedure */
     101         526 :             proc = PLy_procedure_create(procTup, fn_oid, is_trigger);
     102         518 :             if (use_cache)
     103         472 :                 entry->proc = proc;
     104             :         }
     105        1278 :         else if (!PLy_procedure_valid(proc, procTup))
     106             :         {
     107             :             /* Found it, but it's invalid, free and reuse the cache entry */
     108           0 :             entry->proc = NULL;
     109           0 :             if (proc)
     110           0 :                 PLy_procedure_delete(proc);
     111           0 :             proc = PLy_procedure_create(procTup, fn_oid, is_trigger);
     112           0 :             entry->proc = proc;
     113             :         }
     114             :         /* Found it and it's valid, it's fine to use it */
     115             :     }
     116           8 :     PG_CATCH();
     117             :     {
     118             :         /* Do not leave an uninitialized entry in the cache */
     119           8 :         if (use_cache)
     120           8 :             hash_search(PLy_procedure_cache, &key, HASH_REMOVE, NULL);
     121           8 :         PG_RE_THROW();
     122             :     }
     123        1796 :     PG_END_TRY();
     124             : 
     125        1796 :     ReleaseSysCache(procTup);
     126             : 
     127        1796 :     return proc;
     128             : }
     129             : 
     130             : /*
     131             :  * Create a new PLyProcedure structure
     132             :  */
     133             : static PLyProcedure *
     134         526 : PLy_procedure_create(HeapTuple procTup, Oid fn_oid, bool is_trigger)
     135             : {
     136             :     char        procName[NAMEDATALEN + 256];
     137             :     Form_pg_proc procStruct;
     138             :     PLyProcedure *volatile proc;
     139             :     MemoryContext cxt;
     140             :     MemoryContext oldcxt;
     141             :     int         rv;
     142             :     char       *ptr;
     143             : 
     144         526 :     procStruct = (Form_pg_proc) GETSTRUCT(procTup);
     145        1052 :     rv = snprintf(procName, sizeof(procName),
     146             :                   "__plpython_procedure_%s_%u",
     147         526 :                   NameStr(procStruct->proname),
     148             :                   fn_oid);
     149         526 :     if (rv >= sizeof(procName) || rv < 0)
     150           0 :         elog(ERROR, "procedure name would overrun buffer");
     151             : 
     152             :     /* Replace any not-legal-in-Python-names characters with '_' */
     153       24444 :     for (ptr = procName; *ptr; ptr++)
     154             :     {
     155       23918 :         if (!((*ptr >= 'A' && *ptr <= 'Z') ||
     156       23916 :               (*ptr >= 'a' && *ptr <= 'z') ||
     157        6424 :               (*ptr >= '0' && *ptr <= '9')))
     158        3600 :             *ptr = '_';
     159             :     }
     160             : 
     161             :     /* Create long-lived context that all procedure info will live in */
     162         526 :     cxt = AllocSetContextCreate(TopMemoryContext,
     163             :                                 "PL/Python function",
     164             :                                 ALLOCSET_DEFAULT_SIZES);
     165             : 
     166         526 :     oldcxt = MemoryContextSwitchTo(cxt);
     167             : 
     168         526 :     proc = (PLyProcedure *) palloc0(sizeof(PLyProcedure));
     169         526 :     proc->mcxt = cxt;
     170             : 
     171         526 :     PG_TRY();
     172             :     {
     173             :         Datum       protrftypes_datum;
     174             :         Datum       prosrcdatum;
     175             :         bool        isnull;
     176             :         char       *procSource;
     177             :         int         i;
     178             : 
     179         526 :         proc->proname = pstrdup(NameStr(procStruct->proname));
     180         526 :         MemoryContextSetIdentifier(cxt, proc->proname);
     181         526 :         proc->pyname = pstrdup(procName);
     182         526 :         proc->fn_xmin = HeapTupleHeaderGetRawXmin(procTup->t_data);
     183         526 :         proc->fn_tid = procTup->t_self;
     184         526 :         proc->fn_readonly = (procStruct->provolatile != PROVOLATILE_VOLATILE);
     185         526 :         proc->is_setof = procStruct->proretset;
     186         526 :         proc->is_procedure = (procStruct->prokind == PROKIND_PROCEDURE);
     187         526 :         proc->src = NULL;
     188         526 :         proc->argnames = NULL;
     189         526 :         proc->args = NULL;
     190         526 :         proc->nargs = 0;
     191         526 :         proc->langid = procStruct->prolang;
     192         526 :         protrftypes_datum = SysCacheGetAttr(PROCOID, procTup,
     193             :                                             Anum_pg_proc_protrftypes,
     194             :                                             &isnull);
     195         526 :         proc->trftypes = isnull ? NIL : oid_array_to_list(protrftypes_datum);
     196         526 :         proc->code = NULL;
     197         526 :         proc->statics = NULL;
     198         526 :         proc->globals = NULL;
     199         526 :         proc->calldepth = 0;
     200         526 :         proc->argstack = NULL;
     201             : 
     202             :         /*
     203             :          * get information required for output conversion of the return value,
     204             :          * but only if this isn't a trigger.
     205             :          */
     206         526 :         if (!is_trigger)
     207             :         {
     208         430 :             Oid         rettype = procStruct->prorettype;
     209             :             HeapTuple   rvTypeTup;
     210             :             Form_pg_type rvTypeStruct;
     211             : 
     212         430 :             rvTypeTup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(rettype));
     213         430 :             if (!HeapTupleIsValid(rvTypeTup))
     214           0 :                 elog(ERROR, "cache lookup failed for type %u", rettype);
     215         430 :             rvTypeStruct = (Form_pg_type) GETSTRUCT(rvTypeTup);
     216             : 
     217             :             /* Disallow pseudotype result, except for void or record */
     218         430 :             if (rvTypeStruct->typtype == TYPTYPE_PSEUDO)
     219             :             {
     220         100 :                 if (rettype == VOIDOID ||
     221             :                     rettype == RECORDOID)
     222             :                      /* okay */ ;
     223           2 :                 else if (rettype == TRIGGEROID || rettype == EVTTRIGGEROID)
     224           2 :                     ereport(ERROR,
     225             :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     226             :                              errmsg("trigger functions can only be called as triggers")));
     227             :                 else
     228           0 :                     ereport(ERROR,
     229             :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     230             :                              errmsg("PL/Python functions cannot return type %s",
     231             :                                     format_type_be(rettype))));
     232             :             }
     233             : 
     234             :             /* set up output function for procedure result */
     235         428 :             PLy_output_setup_func(&proc->result, proc->mcxt,
     236             :                                   rettype, -1, proc);
     237             : 
     238         428 :             ReleaseSysCache(rvTypeTup);
     239             :         }
     240             :         else
     241             :         {
     242             :             /*
     243             :              * In a trigger function, we use proc->result and proc->result_in
     244             :              * for converting tuples, but we don't yet have enough info to set
     245             :              * them up.  PLy_exec_trigger will deal with it.
     246             :              */
     247          96 :             proc->result.typoid = InvalidOid;
     248          96 :             proc->result_in.typoid = InvalidOid;
     249             :         }
     250             : 
     251             :         /*
     252             :          * Now get information required for input conversion of the
     253             :          * procedure's arguments.  Note that we ignore output arguments here.
     254             :          * If the function returns record, those I/O functions will be set up
     255             :          * when the function is first called.
     256             :          */
     257         524 :         if (procStruct->pronargs)
     258             :         {
     259             :             Oid        *types;
     260             :             char      **names,
     261             :                        *modes;
     262             :             int         pos,
     263             :                         total;
     264             : 
     265             :             /* extract argument type info from the pg_proc tuple */
     266         206 :             total = get_func_arg_info(procTup, &types, &names, &modes);
     267             : 
     268             :             /* count number of in+inout args into proc->nargs */
     269         206 :             if (modes == NULL)
     270         186 :                 proc->nargs = total;
     271             :             else
     272             :             {
     273             :                 /* proc->nargs was initialized to 0 above */
     274          76 :                 for (i = 0; i < total; i++)
     275             :                 {
     276          56 :                     if (modes[i] != PROARGMODE_OUT &&
     277          34 :                         modes[i] != PROARGMODE_TABLE)
     278          34 :                         (proc->nargs)++;
     279             :                 }
     280             :             }
     281             : 
     282             :             /* Allocate arrays for per-input-argument data */
     283         206 :             proc->argnames = (char **) palloc0(sizeof(char *) * proc->nargs);
     284         206 :             proc->args = (PLyDatumToOb *) palloc0(sizeof(PLyDatumToOb) * proc->nargs);
     285             : 
     286         532 :             for (i = pos = 0; i < total; i++)
     287             :             {
     288             :                 HeapTuple   argTypeTup;
     289             :                 Form_pg_type argTypeStruct;
     290             : 
     291         326 :                 if (modes &&
     292          56 :                     (modes[i] == PROARGMODE_OUT ||
     293          34 :                      modes[i] == PROARGMODE_TABLE))
     294          22 :                     continue;   /* skip OUT arguments */
     295             : 
     296             :                 Assert(types[i] == procStruct->proargtypes.values[pos]);
     297             : 
     298         608 :                 argTypeTup = SearchSysCache1(TYPEOID,
     299         304 :                                              ObjectIdGetDatum(types[i]));
     300         304 :                 if (!HeapTupleIsValid(argTypeTup))
     301           0 :                     elog(ERROR, "cache lookup failed for type %u", types[i]);
     302         304 :                 argTypeStruct = (Form_pg_type) GETSTRUCT(argTypeTup);
     303             : 
     304             :                 /* disallow pseudotype arguments */
     305         304 :                 if (argTypeStruct->typtype == TYPTYPE_PSEUDO)
     306           0 :                     ereport(ERROR,
     307             :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     308             :                              errmsg("PL/Python functions cannot accept type %s",
     309             :                                     format_type_be(types[i]))));
     310             : 
     311             :                 /* set up I/O function info */
     312         608 :                 PLy_input_setup_func(&proc->args[pos], proc->mcxt,
     313         304 :                                      types[i], -1,  /* typmod not known */
     314             :                                      proc);
     315             : 
     316             :                 /* get argument name */
     317         304 :                 proc->argnames[pos] = names ? pstrdup(names[i]) : NULL;
     318             : 
     319         304 :                 ReleaseSysCache(argTypeTup);
     320             : 
     321         304 :                 pos++;
     322             :             }
     323             :         }
     324             : 
     325             :         /*
     326             :          * get the text of the function.
     327             :          */
     328         524 :         prosrcdatum = SysCacheGetAttr(PROCOID, procTup,
     329             :                                       Anum_pg_proc_prosrc, &isnull);
     330         524 :         if (isnull)
     331           0 :             elog(ERROR, "null prosrc");
     332         524 :         procSource = TextDatumGetCString(prosrcdatum);
     333             : 
     334         524 :         PLy_procedure_compile(proc, procSource);
     335             : 
     336         518 :         pfree(procSource);
     337             :     }
     338           8 :     PG_CATCH();
     339             :     {
     340           8 :         MemoryContextSwitchTo(oldcxt);
     341           8 :         PLy_procedure_delete(proc);
     342           8 :         PG_RE_THROW();
     343             :     }
     344         518 :     PG_END_TRY();
     345             : 
     346         518 :     MemoryContextSwitchTo(oldcxt);
     347         518 :     return proc;
     348             : }
     349             : 
     350             : /*
     351             :  * Insert the procedure into the Python interpreter
     352             :  */
     353             : void
     354         564 : PLy_procedure_compile(PLyProcedure *proc, const char *src)
     355             : {
     356         564 :     PyObject   *crv = NULL;
     357             :     char       *msrc;
     358             : 
     359         564 :     proc->globals = PyDict_Copy(PLy_interp_globals);
     360             : 
     361             :     /*
     362             :      * SD is private preserved data between calls. GD is global data shared by
     363             :      * all functions
     364             :      */
     365         564 :     proc->statics = PyDict_New();
     366         564 :     if (!proc->statics)
     367           0 :         PLy_elog(ERROR, NULL);
     368         564 :     PyDict_SetItemString(proc->globals, "SD", proc->statics);
     369             : 
     370             :     /*
     371             :      * insert the function code into the interpreter
     372             :      */
     373         564 :     msrc = PLy_procedure_munge_source(proc->pyname, src);
     374             :     /* Save the mangled source for later inclusion in tracebacks */
     375         564 :     proc->src = MemoryContextStrdup(proc->mcxt, msrc);
     376         564 :     crv = PyRun_String(msrc, Py_file_input, proc->globals, NULL);
     377         564 :     pfree(msrc);
     378             : 
     379         564 :     if (crv != NULL)
     380             :     {
     381             :         int         clen;
     382             :         char        call[NAMEDATALEN + 256];
     383             : 
     384         558 :         Py_DECREF(crv);
     385             : 
     386             :         /*
     387             :          * compile a call to the function
     388             :          */
     389         558 :         clen = snprintf(call, sizeof(call), "%s()", proc->pyname);
     390         558 :         if (clen < 0 || clen >= sizeof(call))
     391           0 :             elog(ERROR, "string would overflow buffer");
     392         558 :         proc->code = Py_CompileString(call, "<string>", Py_eval_input);
     393         558 :         if (proc->code != NULL)
     394         558 :             return;
     395             :     }
     396             : 
     397           6 :     if (proc->proname)
     398           6 :         PLy_elog(ERROR, "could not compile PL/Python function \"%s\"",
     399             :                  proc->proname);
     400             :     else
     401           0 :         PLy_elog(ERROR, "could not compile anonymous PL/Python code block");
     402             : }
     403             : 
     404             : void
     405          48 : PLy_procedure_delete(PLyProcedure *proc)
     406             : {
     407          48 :     Py_XDECREF(proc->code);
     408          48 :     Py_XDECREF(proc->statics);
     409          48 :     Py_XDECREF(proc->globals);
     410          48 :     MemoryContextDelete(proc->mcxt);
     411          48 : }
     412             : 
     413             : /*
     414             :  * Decide whether a cached PLyProcedure struct is still valid
     415             :  */
     416             : static bool
     417        1278 : PLy_procedure_valid(PLyProcedure *proc, HeapTuple procTup)
     418             : {
     419        1278 :     if (proc == NULL)
     420           0 :         return false;
     421             : 
     422             :     /* If the pg_proc tuple has changed, it's not valid */
     423        2556 :     if (!(proc->fn_xmin == HeapTupleHeaderGetRawXmin(procTup->t_data) &&
     424        1278 :           ItemPointerEquals(&proc->fn_tid, &procTup->t_self)))
     425           0 :         return false;
     426             : 
     427        1278 :     return true;
     428             : }
     429             : 
     430             : static char *
     431         564 : PLy_procedure_munge_source(const char *name, const char *src)
     432             : {
     433             :     char       *mrc,
     434             :                *mp;
     435             :     const char *sp;
     436             :     size_t      mlen;
     437             :     int         plen;
     438             : 
     439             :     /*
     440             :      * room for function source and the def statement
     441             :      */
     442         564 :     mlen = (strlen(src) * 2) + strlen(name) + 16;
     443             : 
     444         564 :     mrc = palloc(mlen);
     445         564 :     plen = snprintf(mrc, mlen, "def %s():\n\t", name);
     446             :     Assert(plen >= 0 && plen < mlen);
     447             : 
     448         564 :     sp = src;
     449         564 :     mp = mrc + plen;
     450             : 
     451       61574 :     while (*sp != '\0')
     452             :     {
     453       61010 :         if (*sp == '\r' && *(sp + 1) == '\n')
     454           6 :             sp++;
     455             : 
     456       61010 :         if (*sp == '\n' || *sp == '\r')
     457             :         {
     458        2578 :             *mp++ = '\n';
     459        2578 :             *mp++ = '\t';
     460        2578 :             sp++;
     461             :         }
     462             :         else
     463       58432 :             *mp++ = *sp++;
     464             :     }
     465         564 :     *mp++ = '\n';
     466         564 :     *mp++ = '\n';
     467         564 :     *mp = '\0';
     468             : 
     469         564 :     if (mp > (mrc + mlen))
     470           0 :         elog(FATAL, "buffer overrun in PLy_procedure_munge_source");
     471             : 
     472         564 :     return mrc;
     473             : }

Generated by: LCOV version 1.13