LCOV - code coverage report
Current view: top level - src/backend/commands - proclang.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 73 88 83.0 %
Date: 2020-06-01 08:06:25 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * proclang.c
       4             :  *    PostgreSQL LANGUAGE support code.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  * IDENTIFICATION
      10             :  *    src/backend/commands/proclang.c
      11             :  *
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : #include "postgres.h"
      15             : 
      16             : #include "access/table.h"
      17             : #include "catalog/catalog.h"
      18             : #include "catalog/dependency.h"
      19             : #include "catalog/indexing.h"
      20             : #include "catalog/objectaccess.h"
      21             : #include "catalog/pg_language.h"
      22             : #include "catalog/pg_namespace.h"
      23             : #include "catalog/pg_proc.h"
      24             : #include "catalog/pg_type.h"
      25             : #include "commands/defrem.h"
      26             : #include "commands/proclang.h"
      27             : #include "miscadmin.h"
      28             : #include "parser/parse_func.h"
      29             : #include "utils/builtins.h"
      30             : #include "utils/lsyscache.h"
      31             : #include "utils/rel.h"
      32             : #include "utils/syscache.h"
      33             : 
      34             : 
      35             : /*
      36             :  * CREATE LANGUAGE
      37             :  */
      38             : ObjectAddress
      39         406 : CreateProceduralLanguage(CreatePLangStmt *stmt)
      40             : {
      41         406 :     const char *languageName = stmt->plname;
      42         406 :     Oid         languageOwner = GetUserId();
      43             :     Oid         handlerOid,
      44             :                 inlineOid,
      45             :                 valOid;
      46             :     Oid         funcrettype;
      47             :     Oid         funcargtypes[1];
      48             :     Relation    rel;
      49             :     TupleDesc   tupDesc;
      50             :     Datum       values[Natts_pg_language];
      51             :     bool        nulls[Natts_pg_language];
      52             :     bool        replaces[Natts_pg_language];
      53             :     NameData    langname;
      54             :     HeapTuple   oldtup;
      55             :     HeapTuple   tup;
      56             :     Oid         langoid;
      57             :     bool        is_update;
      58             :     ObjectAddress myself,
      59             :                 referenced;
      60             : 
      61             :     /*
      62             :      * Check permission
      63             :      */
      64         406 :     if (!superuser())
      65           0 :         ereport(ERROR,
      66             :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
      67             :                  errmsg("must be superuser to create custom procedural language")));
      68             : 
      69             :     /*
      70             :      * Lookup the PL handler function and check that it is of the expected
      71             :      * return type
      72             :      */
      73             :     Assert(stmt->plhandler);
      74         406 :     handlerOid = LookupFuncName(stmt->plhandler, 0, NULL, false);
      75         406 :     funcrettype = get_func_rettype(handlerOid);
      76         406 :     if (funcrettype != LANGUAGE_HANDLEROID)
      77           0 :         ereport(ERROR,
      78             :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
      79             :                  errmsg("function %s must return type %s",
      80             :                         NameListToString(stmt->plhandler), "language_handler")));
      81             : 
      82             :     /* validate the inline function */
      83         406 :     if (stmt->plinline)
      84             :     {
      85         394 :         funcargtypes[0] = INTERNALOID;
      86         394 :         inlineOid = LookupFuncName(stmt->plinline, 1, funcargtypes, false);
      87             :         /* return value is ignored, so we don't check the type */
      88             :     }
      89             :     else
      90          12 :         inlineOid = InvalidOid;
      91             : 
      92             :     /* validate the validator function */
      93         406 :     if (stmt->plvalidator)
      94             :     {
      95         394 :         funcargtypes[0] = OIDOID;
      96         394 :         valOid = LookupFuncName(stmt->plvalidator, 1, funcargtypes, false);
      97             :         /* return value is ignored, so we don't check the type */
      98             :     }
      99             :     else
     100          12 :         valOid = InvalidOid;
     101             : 
     102             :     /* ok to create it */
     103         406 :     rel = table_open(LanguageRelationId, RowExclusiveLock);
     104         406 :     tupDesc = RelationGetDescr(rel);
     105             : 
     106             :     /* Prepare data to be inserted */
     107         406 :     memset(values, 0, sizeof(values));
     108         406 :     memset(nulls, false, sizeof(nulls));
     109         406 :     memset(replaces, true, sizeof(replaces));
     110             : 
     111         406 :     namestrcpy(&langname, languageName);
     112         406 :     values[Anum_pg_language_lanname - 1] = NameGetDatum(&langname);
     113         406 :     values[Anum_pg_language_lanowner - 1] = ObjectIdGetDatum(languageOwner);
     114         406 :     values[Anum_pg_language_lanispl - 1] = BoolGetDatum(true);
     115         406 :     values[Anum_pg_language_lanpltrusted - 1] = BoolGetDatum(stmt->pltrusted);
     116         406 :     values[Anum_pg_language_lanplcallfoid - 1] = ObjectIdGetDatum(handlerOid);
     117         406 :     values[Anum_pg_language_laninline - 1] = ObjectIdGetDatum(inlineOid);
     118         406 :     values[Anum_pg_language_lanvalidator - 1] = ObjectIdGetDatum(valOid);
     119         406 :     nulls[Anum_pg_language_lanacl - 1] = true;
     120             : 
     121             :     /* Check for pre-existing definition */
     122         406 :     oldtup = SearchSysCache1(LANGNAME, PointerGetDatum(languageName));
     123             : 
     124         406 :     if (HeapTupleIsValid(oldtup))
     125             :     {
     126           0 :         Form_pg_language oldform = (Form_pg_language) GETSTRUCT(oldtup);
     127             : 
     128             :         /* There is one; okay to replace it? */
     129           0 :         if (!stmt->replace)
     130           0 :             ereport(ERROR,
     131             :                     (errcode(ERRCODE_DUPLICATE_OBJECT),
     132             :                      errmsg("language \"%s\" already exists", languageName)));
     133             : 
     134             :         /* This is currently pointless, since we already checked superuser */
     135             : #ifdef NOT_USED
     136             :         if (!pg_language_ownercheck(oldform->oid, languageOwner))
     137             :             aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_LANGUAGE,
     138             :                            languageName);
     139             : #endif
     140             : 
     141             :         /*
     142             :          * Do not change existing oid, ownership or permissions.  Note
     143             :          * dependency-update code below has to agree with this decision.
     144             :          */
     145           0 :         replaces[Anum_pg_language_oid - 1] = false;
     146           0 :         replaces[Anum_pg_language_lanowner - 1] = false;
     147           0 :         replaces[Anum_pg_language_lanacl - 1] = false;
     148             : 
     149             :         /* Okay, do it... */
     150           0 :         tup = heap_modify_tuple(oldtup, tupDesc, values, nulls, replaces);
     151           0 :         CatalogTupleUpdate(rel, &tup->t_self, tup);
     152             : 
     153           0 :         langoid = oldform->oid;
     154           0 :         ReleaseSysCache(oldtup);
     155           0 :         is_update = true;
     156             :     }
     157             :     else
     158             :     {
     159             :         /* Creating a new language */
     160         406 :         langoid = GetNewOidWithIndex(rel, LanguageOidIndexId,
     161             :                                      Anum_pg_language_oid);
     162         406 :         values[Anum_pg_language_oid - 1] = ObjectIdGetDatum(langoid);
     163         406 :         tup = heap_form_tuple(tupDesc, values, nulls);
     164         406 :         CatalogTupleInsert(rel, tup);
     165         406 :         is_update = false;
     166             :     }
     167             : 
     168             :     /*
     169             :      * Create dependencies for the new language.  If we are updating an
     170             :      * existing language, first delete any existing pg_depend entries.
     171             :      * (However, since we are not changing ownership or permissions, the
     172             :      * shared dependencies do *not* need to change, and we leave them alone.)
     173             :      */
     174         406 :     myself.classId = LanguageRelationId;
     175         406 :     myself.objectId = langoid;
     176         406 :     myself.objectSubId = 0;
     177             : 
     178         406 :     if (is_update)
     179           0 :         deleteDependencyRecordsFor(myself.classId, myself.objectId, true);
     180             : 
     181             :     /* dependency on owner of language */
     182         406 :     if (!is_update)
     183         406 :         recordDependencyOnOwner(myself.classId, myself.objectId,
     184             :                                 languageOwner);
     185             : 
     186             :     /* dependency on extension */
     187         406 :     recordDependencyOnCurrentExtension(&myself, is_update);
     188             : 
     189             :     /* dependency on the PL handler function */
     190         406 :     referenced.classId = ProcedureRelationId;
     191         406 :     referenced.objectId = handlerOid;
     192         406 :     referenced.objectSubId = 0;
     193         406 :     recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
     194             : 
     195             :     /* dependency on the inline handler function, if any */
     196         406 :     if (OidIsValid(inlineOid))
     197             :     {
     198         394 :         referenced.classId = ProcedureRelationId;
     199         394 :         referenced.objectId = inlineOid;
     200         394 :         referenced.objectSubId = 0;
     201         394 :         recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
     202             :     }
     203             : 
     204             :     /* dependency on the validator function, if any */
     205         406 :     if (OidIsValid(valOid))
     206             :     {
     207         394 :         referenced.classId = ProcedureRelationId;
     208         394 :         referenced.objectId = valOid;
     209         394 :         referenced.objectSubId = 0;
     210         394 :         recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
     211             :     }
     212             : 
     213             :     /* Post creation hook for new procedural language */
     214         406 :     InvokeObjectPostCreateHook(LanguageRelationId, myself.objectId, 0);
     215             : 
     216         406 :     table_close(rel, RowExclusiveLock);
     217             : 
     218         406 :     return myself;
     219             : }
     220             : 
     221             : /*
     222             :  * Guts of language dropping.
     223             :  */
     224             : void
     225          28 : DropProceduralLanguageById(Oid langOid)
     226             : {
     227             :     Relation    rel;
     228             :     HeapTuple   langTup;
     229             : 
     230          28 :     rel = table_open(LanguageRelationId, RowExclusiveLock);
     231             : 
     232          28 :     langTup = SearchSysCache1(LANGOID, ObjectIdGetDatum(langOid));
     233          28 :     if (!HeapTupleIsValid(langTup)) /* should not happen */
     234           0 :         elog(ERROR, "cache lookup failed for language %u", langOid);
     235             : 
     236          28 :     CatalogTupleDelete(rel, &langTup->t_self);
     237             : 
     238          28 :     ReleaseSysCache(langTup);
     239             : 
     240          28 :     table_close(rel, RowExclusiveLock);
     241          28 : }
     242             : 
     243             : /*
     244             :  * get_language_oid - given a language name, look up the OID
     245             :  *
     246             :  * If missing_ok is false, throw an error if language name not found.  If
     247             :  * true, just return InvalidOid.
     248             :  */
     249             : Oid
     250         954 : get_language_oid(const char *langname, bool missing_ok)
     251             : {
     252             :     Oid         oid;
     253             : 
     254         954 :     oid = GetSysCacheOid1(LANGNAME, Anum_pg_language_oid,
     255             :                           CStringGetDatum(langname));
     256         954 :     if (!OidIsValid(oid) && !missing_ok)
     257          12 :         ereport(ERROR,
     258             :                 (errcode(ERRCODE_UNDEFINED_OBJECT),
     259             :                  errmsg("language \"%s\" does not exist", langname)));
     260         942 :     return oid;
     261             : }

Generated by: LCOV version 1.13