PostgreSQL Source Code  git master
proclang.h File Reference
Include dependency graph for proclang.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

ObjectAddress CreateProceduralLanguage (CreatePLangStmt *stmt)
 
void DropProceduralLanguageById (Oid langOid)
 
bool PLTemplateExists (const char *languageName)
 
Oid get_language_oid (const char *langname, bool missing_ok)
 

Function Documentation

◆ CreateProceduralLanguage()

ObjectAddress CreateProceduralLanguage ( CreatePLangStmt stmt)

Definition at line 62 of file proclang.c.

References aclcheck_error(), ACLCHECK_NOT_OWNER, buildoidvector(), create_proc_lang(), ereport, errcode(), errhint(), errmsg(), ERROR, find_language_template(), get_database_name(), get_func_rettype(), GetUserId(), InvalidOid, LookupFuncName(), MyDatabaseId, NameListToString(), NIL, NOTICE, OBJECT_DATABASE, ObjectAddress::objectId, OidIsValid, pg_database_ownercheck(), CreatePLangStmt::plhandler, CreatePLangStmt::plinline, CreatePLangStmt::plname, CreatePLangStmt::pltrusted, CreatePLangStmt::plvalidator, PointerGetDatum, ProcedureCreate(), CreatePLangStmt::replace, SetFunctionReturnType(), superuser(), SystemFuncName(), PLTemplate::tmpldbacreate, PLTemplate::tmplhandler, PLTemplate::tmplinline, PLTemplate::tmpllibrary, PLTemplate::tmpltrusted, PLTemplate::tmplvalidator, and WARNING.

Referenced by ProcessUtilitySlow().

63 {
64  PLTemplate *pltemplate;
65  ObjectAddress tmpAddr;
66  Oid handlerOid,
67  inlineOid,
68  valOid;
69  Oid funcrettype;
70  Oid funcargtypes[1];
71 
72  /*
73  * If we have template information for the language, ignore the supplied
74  * parameters (if any) and use the template information.
75  */
76  if ((pltemplate = find_language_template(stmt->plname)) != NULL)
77  {
78  List *funcname;
79 
80  /*
81  * Give a notice if we are ignoring supplied parameters.
82  */
83  if (stmt->plhandler)
85  (errmsg("using pg_pltemplate information instead of CREATE LANGUAGE parameters")));
86 
87  /*
88  * Check permission
89  */
90  if (!superuser())
91  {
92  if (!pltemplate->tmpldbacreate)
93  ereport(ERROR,
94  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
95  errmsg("must be superuser to create procedural language \"%s\"",
96  stmt->plname)));
100  }
101 
102  /*
103  * Find or create the handler function, which we force to be in the
104  * pg_catalog schema. If already present, it must have the correct
105  * return type.
106  */
107  funcname = SystemFuncName(pltemplate->tmplhandler);
108  handlerOid = LookupFuncName(funcname, 0, funcargtypes, true);
109  if (OidIsValid(handlerOid))
110  {
111  funcrettype = get_func_rettype(handlerOid);
112  if (funcrettype != LANGUAGE_HANDLEROID)
113  ereport(ERROR,
114  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
115  errmsg("function %s must return type %s",
116  NameListToString(funcname), "language_handler")));
117  }
118  else
119  {
120  tmpAddr = ProcedureCreate(pltemplate->tmplhandler,
121  PG_CATALOG_NAMESPACE,
122  false, /* replace */
123  false, /* returnsSet */
124  LANGUAGE_HANDLEROID,
125  BOOTSTRAP_SUPERUSERID,
126  ClanguageId,
127  F_FMGR_C_VALIDATOR,
128  pltemplate->tmplhandler,
129  pltemplate->tmpllibrary,
130  PROKIND_FUNCTION,
131  false, /* security_definer */
132  false, /* isLeakProof */
133  false, /* isStrict */
134  PROVOLATILE_VOLATILE,
135  PROPARALLEL_UNSAFE,
136  buildoidvector(funcargtypes, 0),
137  PointerGetDatum(NULL),
138  PointerGetDatum(NULL),
139  PointerGetDatum(NULL),
140  NIL,
141  PointerGetDatum(NULL),
142  PointerGetDatum(NULL),
143  InvalidOid,
144  1,
145  0);
146  handlerOid = tmpAddr.objectId;
147  }
148 
149  /*
150  * Likewise for the anonymous block handler, if required; but we don't
151  * care about its return type.
152  */
153  if (pltemplate->tmplinline)
154  {
155  funcname = SystemFuncName(pltemplate->tmplinline);
156  funcargtypes[0] = INTERNALOID;
157  inlineOid = LookupFuncName(funcname, 1, funcargtypes, true);
158  if (!OidIsValid(inlineOid))
159  {
160  tmpAddr = ProcedureCreate(pltemplate->tmplinline,
161  PG_CATALOG_NAMESPACE,
162  false, /* replace */
163  false, /* returnsSet */
164  VOIDOID,
165  BOOTSTRAP_SUPERUSERID,
166  ClanguageId,
167  F_FMGR_C_VALIDATOR,
168  pltemplate->tmplinline,
169  pltemplate->tmpllibrary,
170  PROKIND_FUNCTION,
171  false, /* security_definer */
172  false, /* isLeakProof */
173  true, /* isStrict */
174  PROVOLATILE_VOLATILE,
175  PROPARALLEL_UNSAFE,
176  buildoidvector(funcargtypes, 1),
177  PointerGetDatum(NULL),
178  PointerGetDatum(NULL),
179  PointerGetDatum(NULL),
180  NIL,
181  PointerGetDatum(NULL),
182  PointerGetDatum(NULL),
183  InvalidOid,
184  1,
185  0);
186  inlineOid = tmpAddr.objectId;
187  }
188  }
189  else
190  inlineOid = InvalidOid;
191 
192  /*
193  * Likewise for the validator, if required; but we don't care about
194  * its return type.
195  */
196  if (pltemplate->tmplvalidator)
197  {
198  funcname = SystemFuncName(pltemplate->tmplvalidator);
199  funcargtypes[0] = OIDOID;
200  valOid = LookupFuncName(funcname, 1, funcargtypes, true);
201  if (!OidIsValid(valOid))
202  {
203  tmpAddr = ProcedureCreate(pltemplate->tmplvalidator,
204  PG_CATALOG_NAMESPACE,
205  false, /* replace */
206  false, /* returnsSet */
207  VOIDOID,
208  BOOTSTRAP_SUPERUSERID,
209  ClanguageId,
210  F_FMGR_C_VALIDATOR,
211  pltemplate->tmplvalidator,
212  pltemplate->tmpllibrary,
213  PROKIND_FUNCTION,
214  false, /* security_definer */
215  false, /* isLeakProof */
216  true, /* isStrict */
217  PROVOLATILE_VOLATILE,
218  PROPARALLEL_UNSAFE,
219  buildoidvector(funcargtypes, 1),
220  PointerGetDatum(NULL),
221  PointerGetDatum(NULL),
222  PointerGetDatum(NULL),
223  NIL,
224  PointerGetDatum(NULL),
225  PointerGetDatum(NULL),
226  InvalidOid,
227  1,
228  0);
229  valOid = tmpAddr.objectId;
230  }
231  }
232  else
233  valOid = InvalidOid;
234 
235  /* ok, create it */
236  return create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
237  handlerOid, inlineOid,
238  valOid, pltemplate->tmpltrusted);
239  }
240  else
241  {
242  /*
243  * No template, so use the provided information. If there's no
244  * handler clause, the user is trying to rely on a template that we
245  * don't have, so complain accordingly.
246  */
247  if (!stmt->plhandler)
248  ereport(ERROR,
249  (errcode(ERRCODE_UNDEFINED_OBJECT),
250  errmsg("unsupported language \"%s\"",
251  stmt->plname),
252  errhint("The supported languages are listed in the pg_pltemplate system catalog.")));
253 
254  /*
255  * Check permission
256  */
257  if (!superuser())
258  ereport(ERROR,
259  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
260  errmsg("must be superuser to create custom procedural language")));
261 
262  /*
263  * Lookup the PL handler function and check that it is of the expected
264  * return type
265  */
266  handlerOid = LookupFuncName(stmt->plhandler, 0, funcargtypes, false);
267  funcrettype = get_func_rettype(handlerOid);
268  if (funcrettype != LANGUAGE_HANDLEROID)
269  {
270  /*
271  * We allow OPAQUE just so we can load old dump files. When we
272  * see a handler function declared OPAQUE, change it to
273  * LANGUAGE_HANDLER. (This is probably obsolete and removable?)
274  */
275  if (funcrettype == OPAQUEOID)
276  {
278  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
279  errmsg("changing return type of function %s from %s to %s",
281  "opaque", "language_handler")));
282  SetFunctionReturnType(handlerOid, LANGUAGE_HANDLEROID);
283  }
284  else
285  ereport(ERROR,
286  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
287  errmsg("function %s must return type %s",
288  NameListToString(stmt->plhandler), "language_handler")));
289  }
290 
291  /* validate the inline function */
292  if (stmt->plinline)
293  {
294  funcargtypes[0] = INTERNALOID;
295  inlineOid = LookupFuncName(stmt->plinline, 1, funcargtypes, false);
296  /* return value is ignored, so we don't check the type */
297  }
298  else
299  inlineOid = InvalidOid;
300 
301  /* validate the validator function */
302  if (stmt->plvalidator)
303  {
304  funcargtypes[0] = OIDOID;
305  valOid = LookupFuncName(stmt->plvalidator, 1, funcargtypes, false);
306  /* return value is ignored, so we don't check the type */
307  }
308  else
309  valOid = InvalidOid;
310 
311  /* ok, create it */
312  return create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
313  handlerOid, inlineOid,
314  valOid, stmt->pltrusted);
315  }
316 }
#define NIL
Definition: pg_list.h:65
List * SystemFuncName(char *name)
int errhint(const char *fmt,...)
Definition: elog.c:974
char * tmpllibrary
Definition: proclang.c:50
Oid GetUserId(void)
Definition: miscinit.c:380
#define PointerGetDatum(X)
Definition: postgres.h:556
bool tmpltrusted
Definition: proclang.c:45
ObjectAddress ProcedureCreate(const char *procedureName, Oid procNamespace, bool replace, bool returnsSet, Oid returnType, Oid proowner, Oid languageObjectId, Oid languageValidator, const char *prosrc, const char *probin, char prokind, bool security_definer, bool isLeakProof, bool isStrict, char volatility, char parallel, oidvector *parameterTypes, Datum allParameterTypes, Datum parameterModes, Datum parameterNames, List *parameterDefaults, Datum trftypes, Datum proconfig, Oid prosupport, float4 procost, float4 prorows)
Definition: pg_proc.c:68
int errcode(int sqlerrcode)
Definition: elog.c:570
bool superuser(void)
Definition: superuser.c:47
unsigned int Oid
Definition: postgres_ext.h:31
static ObjectAddress create_proc_lang(const char *languageName, bool replace, Oid languageOwner, Oid handlerOid, Oid inlineOid, Oid valOid, bool trusted)
Definition: proclang.c:322
#define OidIsValid(objectId)
Definition: c.h:638
Oid get_func_rettype(Oid funcid)
Definition: lsyscache.c:1457
char * tmplinline
Definition: proclang.c:48
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3353
#define ERROR
Definition: elog.h:43
Oid LookupFuncName(List *funcname, int nargs, const Oid *argtypes, bool missing_ok)
Definition: parse_func.c:2100
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2099
#define ereport(elevel, rest)
Definition: elog.h:141
bool pg_database_ownercheck(Oid db_oid, Oid roleid)
Definition: aclchk.c:5173
List * plvalidator
Definition: parsenodes.h:2463
bool tmpldbacreate
Definition: proclang.c:46
#define WARNING
Definition: elog.h:40
char * NameListToString(List *names)
Definition: namespace.c:3094
Oid MyDatabaseId
Definition: globals.c:85
void SetFunctionReturnType(Oid funcOid, Oid newRetType)
#define InvalidOid
Definition: postgres_ext.h:36
#define NOTICE
Definition: elog.h:37
oidvector * buildoidvector(const Oid *oids, int n)
Definition: oid.c:167
int errmsg(const char *fmt,...)
Definition: elog.c:784
char * tmplhandler
Definition: proclang.c:47
static PLTemplate * find_language_template(const char *languageName)
Definition: proclang.c:457
Definition: pg_list.h:50
char * tmplvalidator
Definition: proclang.c:49

◆ DropProceduralLanguageById()

void DropProceduralLanguageById ( Oid  langOid)

Definition at line 534 of file proclang.c.

References CatalogTupleDelete(), elog, ERROR, HeapTupleIsValid, LANGOID, ObjectIdGetDatum, ReleaseSysCache(), RowExclusiveLock, SearchSysCache1(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by doDeletion().

535 {
536  Relation rel;
537  HeapTuple langTup;
538 
539  rel = table_open(LanguageRelationId, RowExclusiveLock);
540 
541  langTup = SearchSysCache1(LANGOID, ObjectIdGetDatum(langOid));
542  if (!HeapTupleIsValid(langTup)) /* should not happen */
543  elog(ERROR, "cache lookup failed for language %u", langOid);
544 
545  CatalogTupleDelete(rel, &langTup->t_self);
546 
547  ReleaseSysCache(langTup);
548 
550 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:269
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1124
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define elog(elevel,...)
Definition: elog.h:226
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ get_language_oid()

Oid get_language_oid ( const char *  langname,
bool  missing_ok 
)

Definition at line 559 of file proclang.c.

References CStringGetDatum, ereport, errcode(), errmsg(), ERROR, GetSysCacheOid1, LANGNAME, and OidIsValid.

Referenced by convert_language_name(), CreateTransform(), get_object_address(), get_object_address_unqualified(), and objectNamesToOids().

560 {
561  Oid oid;
562 
563  oid = GetSysCacheOid1(LANGNAME, Anum_pg_language_oid,
564  CStringGetDatum(langname));
565  if (!OidIsValid(oid) && !missing_ok)
566  ereport(ERROR,
567  (errcode(ERRCODE_UNDEFINED_OBJECT),
568  errmsg("language \"%s\" does not exist", langname)));
569  return oid;
570 }
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition: syscache.h:192
int errcode(int sqlerrcode)
Definition: elog.c:570
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:578
#define ereport(elevel, rest)
Definition: elog.h:141
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ PLTemplateExists()

bool PLTemplateExists ( const char *  languageName)

Definition at line 525 of file proclang.c.

References find_language_template().

Referenced by CreateFunction(), and ExecuteDoStmt().

526 {
527  return (find_language_template(languageName) != NULL);
528 }
static PLTemplate * find_language_template(const char *languageName)
Definition: proclang.c:457