LCOV - code coverage report
Current view: top level - src/backend/commands - proclang.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 85.0 % 100 85
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 2 2
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 47.7 % 44 21

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * proclang.c
       4                 :             :  *        PostgreSQL LANGUAGE support code.
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, 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/htup_details.h"
      17                 :             : #include "access/table.h"
      18                 :             : #include "catalog/catalog.h"
      19                 :             : #include "catalog/dependency.h"
      20                 :             : #include "catalog/indexing.h"
      21                 :             : #include "catalog/objectaccess.h"
      22                 :             : #include "catalog/pg_language.h"
      23                 :             : #include "catalog/pg_proc.h"
      24                 :             : #include "catalog/pg_type.h"
      25                 :             : #include "commands/proclang.h"
      26                 :             : #include "miscadmin.h"
      27                 :             : #include "parser/parse_func.h"
      28                 :             : #include "utils/builtins.h"
      29                 :             : #include "utils/lsyscache.h"
      30                 :             : #include "utils/rel.h"
      31                 :             : #include "utils/syscache.h"
      32                 :             : 
      33                 :             : 
      34                 :             : /*
      35                 :             :  * CREATE LANGUAGE
      36                 :             :  */
      37                 :             : ObjectAddress
      38                 :           3 : CreateProceduralLanguage(CreatePLangStmt *stmt)
      39                 :             : {
      40                 :           3 :         const char *languageName = stmt->plname;
      41                 :           3 :         Oid                     languageOwner = GetUserId();
      42                 :           3 :         Oid                     handlerOid,
      43                 :             :                                 inlineOid,
      44                 :             :                                 valOid;
      45                 :           3 :         Oid                     funcrettype;
      46                 :           3 :         Oid                     funcargtypes[1];
      47                 :           3 :         Relation        rel;
      48                 :           3 :         TupleDesc       tupDesc;
      49                 :           3 :         Datum           values[Natts_pg_language];
      50                 :           3 :         bool            nulls[Natts_pg_language];
      51                 :           3 :         bool            replaces[Natts_pg_language];
      52                 :           3 :         NameData        langname;
      53                 :           3 :         HeapTuple       oldtup;
      54                 :           3 :         HeapTuple       tup;
      55                 :           3 :         Oid                     langoid;
      56                 :           3 :         bool            is_update;
      57                 :           3 :         ObjectAddress myself,
      58                 :             :                                 referenced;
      59                 :           3 :         ObjectAddresses *addrs;
      60                 :             : 
      61                 :             :         /*
      62                 :             :          * Check permission
      63                 :             :          */
      64         [ +  - ]:           3 :         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         [ +  - ]:           3 :         Assert(stmt->plhandler);
      74                 :           3 :         handlerOid = LookupFuncName(stmt->plhandler, 0, NULL, false);
      75                 :           3 :         funcrettype = get_func_rettype(handlerOid);
      76         [ +  - ]:           3 :         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         [ +  + ]:           3 :         if (stmt->plinline)
      84                 :             :         {
      85                 :           1 :                 funcargtypes[0] = INTERNALOID;
      86                 :           1 :                 inlineOid = LookupFuncName(stmt->plinline, 1, funcargtypes, false);
      87                 :             :                 /* return value is ignored, so we don't check the type */
      88                 :           1 :         }
      89                 :             :         else
      90                 :           2 :                 inlineOid = InvalidOid;
      91                 :             : 
      92                 :             :         /* validate the validator function */
      93         [ +  + ]:           3 :         if (stmt->plvalidator)
      94                 :             :         {
      95                 :           1 :                 funcargtypes[0] = OIDOID;
      96                 :           1 :                 valOid = LookupFuncName(stmt->plvalidator, 1, funcargtypes, false);
      97                 :             :                 /* return value is ignored, so we don't check the type */
      98                 :           1 :         }
      99                 :             :         else
     100                 :           2 :                 valOid = InvalidOid;
     101                 :             : 
     102                 :             :         /* ok to create it */
     103                 :           3 :         rel = table_open(LanguageRelationId, RowExclusiveLock);
     104                 :           3 :         tupDesc = RelationGetDescr(rel);
     105                 :             : 
     106                 :             :         /* Prepare data to be inserted */
     107                 :           3 :         memset(values, 0, sizeof(values));
     108                 :           3 :         memset(nulls, false, sizeof(nulls));
     109                 :           3 :         memset(replaces, true, sizeof(replaces));
     110                 :             : 
     111                 :           3 :         namestrcpy(&langname, languageName);
     112                 :           3 :         values[Anum_pg_language_lanname - 1] = NameGetDatum(&langname);
     113                 :           3 :         values[Anum_pg_language_lanowner - 1] = ObjectIdGetDatum(languageOwner);
     114                 :           3 :         values[Anum_pg_language_lanispl - 1] = BoolGetDatum(true);
     115                 :           3 :         values[Anum_pg_language_lanpltrusted - 1] = BoolGetDatum(stmt->pltrusted);
     116                 :           3 :         values[Anum_pg_language_lanplcallfoid - 1] = ObjectIdGetDatum(handlerOid);
     117                 :           3 :         values[Anum_pg_language_laninline - 1] = ObjectIdGetDatum(inlineOid);
     118                 :           3 :         values[Anum_pg_language_lanvalidator - 1] = ObjectIdGetDatum(valOid);
     119                 :           3 :         nulls[Anum_pg_language_lanacl - 1] = true;
     120                 :             : 
     121                 :             :         /* Check for pre-existing definition */
     122                 :           3 :         oldtup = SearchSysCache1(LANGNAME, PointerGetDatum(languageName));
     123                 :             : 
     124         [ -  + ]:           3 :         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 (!object_ownercheck(LanguageRelationId, 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                 :           0 :         }
     157                 :             :         else
     158                 :             :         {
     159                 :             :                 /* Creating a new language */
     160                 :           3 :                 langoid = GetNewOidWithIndex(rel, LanguageOidIndexId,
     161                 :             :                                                                          Anum_pg_language_oid);
     162                 :           3 :                 values[Anum_pg_language_oid - 1] = ObjectIdGetDatum(langoid);
     163                 :           3 :                 tup = heap_form_tuple(tupDesc, values, nulls);
     164                 :           3 :                 CatalogTupleInsert(rel, tup);
     165                 :           3 :                 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                 :           3 :         myself.classId = LanguageRelationId;
     175                 :           3 :         myself.objectId = langoid;
     176                 :           3 :         myself.objectSubId = 0;
     177                 :             : 
     178         [ +  - ]:           3 :         if (is_update)
     179                 :           0 :                 deleteDependencyRecordsFor(myself.classId, myself.objectId, true);
     180                 :             : 
     181                 :             :         /* dependency on owner of language */
     182         [ -  + ]:           3 :         if (!is_update)
     183                 :           6 :                 recordDependencyOnOwner(myself.classId, myself.objectId,
     184                 :           3 :                                                                 languageOwner);
     185                 :             : 
     186                 :             :         /* dependency on extension */
     187                 :           3 :         recordDependencyOnCurrentExtension(&myself, is_update);
     188                 :             : 
     189                 :           3 :         addrs = new_object_addresses();
     190                 :             : 
     191                 :             :         /* dependency on the PL handler function */
     192                 :           3 :         ObjectAddressSet(referenced, ProcedureRelationId, handlerOid);
     193                 :           3 :         add_exact_object_address(&referenced, addrs);
     194                 :             : 
     195                 :             :         /* dependency on the inline handler function, if any */
     196         [ +  + ]:           3 :         if (OidIsValid(inlineOid))
     197                 :             :         {
     198                 :           1 :                 ObjectAddressSet(referenced, ProcedureRelationId, inlineOid);
     199                 :           1 :                 add_exact_object_address(&referenced, addrs);
     200                 :           1 :         }
     201                 :             : 
     202                 :             :         /* dependency on the validator function, if any */
     203         [ +  + ]:           3 :         if (OidIsValid(valOid))
     204                 :             :         {
     205                 :           1 :                 ObjectAddressSet(referenced, ProcedureRelationId, valOid);
     206                 :           1 :                 add_exact_object_address(&referenced, addrs);
     207                 :           1 :         }
     208                 :             : 
     209                 :           3 :         record_object_address_dependencies(&myself, addrs, DEPENDENCY_NORMAL);
     210                 :           3 :         free_object_addresses(addrs);
     211                 :             : 
     212                 :             :         /* Post creation hook for new procedural language */
     213         [ +  - ]:           3 :         InvokeObjectPostCreateHook(LanguageRelationId, myself.objectId, 0);
     214                 :             : 
     215                 :           3 :         table_close(rel, RowExclusiveLock);
     216                 :             : 
     217                 :             :         return myself;
     218                 :           3 : }
     219                 :             : 
     220                 :             : /*
     221                 :             :  * get_language_oid - given a language name, look up the OID
     222                 :             :  *
     223                 :             :  * If missing_ok is false, throw an error if language name not found.  If
     224                 :             :  * true, just return InvalidOid.
     225                 :             :  */
     226                 :             : Oid
     227                 :          28 : get_language_oid(const char *langname, bool missing_ok)
     228                 :             : {
     229                 :          28 :         Oid                     oid;
     230                 :             : 
     231                 :          28 :         oid = GetSysCacheOid1(LANGNAME, Anum_pg_language_oid,
     232                 :             :                                                   CStringGetDatum(langname));
     233   [ +  +  +  + ]:          28 :         if (!OidIsValid(oid) && !missing_ok)
     234   [ +  -  +  - ]:           2 :                 ereport(ERROR,
     235                 :             :                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
     236                 :             :                                  errmsg("language \"%s\" does not exist", langname)));
     237                 :          52 :         return oid;
     238                 :          26 : }
        

Generated by: LCOV version 2.3.2-1