PostgreSQL Source Code  git master
proclang.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_authid.h"
#include "catalog/pg_language.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_pltemplate.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "commands/dbcommands.h"
#include "commands/defrem.h"
#include "commands/proclang.h"
#include "miscadmin.h"
#include "parser/parse_func.h"
#include "parser/parser.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/tqual.h"
Include dependency graph for proclang.c:

Go to the source code of this file.

Data Structures

struct  PLTemplate
 

Functions

static ObjectAddress create_proc_lang (const char *languageName, bool replace, Oid languageOwner, Oid handlerOid, Oid inlineOid, Oid valOid, bool trusted)
 
static PLTemplatefind_language_template (const char *languageName)
 
ObjectAddress CreateProceduralLanguage (CreatePLangStmt *stmt)
 
bool PLTemplateExists (const char *languageName)
 
void DropProceduralLanguageById (Oid langOid)
 
Oid get_language_oid (const char *langname, bool missing_ok)
 

Function Documentation

◆ create_proc_lang()

static ObjectAddress create_proc_lang ( const char *  languageName,
bool  replace,
Oid  languageOwner,
Oid  handlerOid,
Oid  inlineOid,
Oid  valOid,
bool  trusted 
)
static

Definition at line 320 of file proclang.c.

References aclcheck_error(), ACLCHECK_NOT_OWNER, BoolGetDatum, CatalogTupleInsert(), CatalogTupleUpdate(), ObjectAddress::classId, deleteDependencyRecordsFor(), DEPENDENCY_NORMAL, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, heap_close, heap_form_tuple(), heap_modify_tuple(), heap_open(), HeapTupleGetOid, HeapTupleIsValid, InvokeObjectPostCreateHook, LANGNAME, NameGetDatum, namestrcpy(), OBJECT_LANGUAGE, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, OidIsValid, pg_language_ownercheck(), PointerGetDatum, recordDependencyOn(), recordDependencyOnCurrentExtension(), recordDependencyOnOwner(), RelationGetDescr, ReleaseSysCache(), RowExclusiveLock, SearchSysCache1(), HeapTupleData::t_self, and values.

Referenced by CreateProceduralLanguage().

323 {
324  Relation rel;
325  TupleDesc tupDesc;
326  Datum values[Natts_pg_language];
327  bool nulls[Natts_pg_language];
328  bool replaces[Natts_pg_language];
329  NameData langname;
330  HeapTuple oldtup;
331  HeapTuple tup;
332  bool is_update;
333  ObjectAddress myself,
334  referenced;
335 
336  rel = heap_open(LanguageRelationId, RowExclusiveLock);
337  tupDesc = RelationGetDescr(rel);
338 
339  /* Prepare data to be inserted */
340  memset(values, 0, sizeof(values));
341  memset(nulls, false, sizeof(nulls));
342  memset(replaces, true, sizeof(replaces));
343 
344  namestrcpy(&langname, languageName);
345  values[Anum_pg_language_lanname - 1] = NameGetDatum(&langname);
346  values[Anum_pg_language_lanowner - 1] = ObjectIdGetDatum(languageOwner);
347  values[Anum_pg_language_lanispl - 1] = BoolGetDatum(true);
348  values[Anum_pg_language_lanpltrusted - 1] = BoolGetDatum(trusted);
349  values[Anum_pg_language_lanplcallfoid - 1] = ObjectIdGetDatum(handlerOid);
350  values[Anum_pg_language_laninline - 1] = ObjectIdGetDatum(inlineOid);
351  values[Anum_pg_language_lanvalidator - 1] = ObjectIdGetDatum(valOid);
352  nulls[Anum_pg_language_lanacl - 1] = true;
353 
354  /* Check for pre-existing definition */
355  oldtup = SearchSysCache1(LANGNAME, PointerGetDatum(languageName));
356 
357  if (HeapTupleIsValid(oldtup))
358  {
359  /* There is one; okay to replace it? */
360  if (!replace)
361  ereport(ERROR,
363  errmsg("language \"%s\" already exists", languageName)));
364  if (!pg_language_ownercheck(HeapTupleGetOid(oldtup), languageOwner))
366  languageName);
367 
368  /*
369  * Do not change existing ownership or permissions. Note
370  * dependency-update code below has to agree with this decision.
371  */
372  replaces[Anum_pg_language_lanowner - 1] = false;
373  replaces[Anum_pg_language_lanacl - 1] = false;
374 
375  /* Okay, do it... */
376  tup = heap_modify_tuple(oldtup, tupDesc, values, nulls, replaces);
377  CatalogTupleUpdate(rel, &tup->t_self, tup);
378 
379  ReleaseSysCache(oldtup);
380  is_update = true;
381  }
382  else
383  {
384  /* Creating a new language */
385  tup = heap_form_tuple(tupDesc, values, nulls);
386  CatalogTupleInsert(rel, tup);
387  is_update = false;
388  }
389 
390  /*
391  * Create dependencies for the new language. If we are updating an
392  * existing language, first delete any existing pg_depend entries.
393  * (However, since we are not changing ownership or permissions, the
394  * shared dependencies do *not* need to change, and we leave them alone.)
395  */
396  myself.classId = LanguageRelationId;
397  myself.objectId = HeapTupleGetOid(tup);
398  myself.objectSubId = 0;
399 
400  if (is_update)
401  deleteDependencyRecordsFor(myself.classId, myself.objectId, true);
402 
403  /* dependency on owner of language */
404  if (!is_update)
406  languageOwner);
407 
408  /* dependency on extension */
409  recordDependencyOnCurrentExtension(&myself, is_update);
410 
411  /* dependency on the PL handler function */
412  referenced.classId = ProcedureRelationId;
413  referenced.objectId = handlerOid;
414  referenced.objectSubId = 0;
415  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
416 
417  /* dependency on the inline handler function, if any */
418  if (OidIsValid(inlineOid))
419  {
420  referenced.classId = ProcedureRelationId;
421  referenced.objectId = inlineOid;
422  referenced.objectSubId = 0;
423  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
424  }
425 
426  /* dependency on the validator function, if any */
427  if (OidIsValid(valOid))
428  {
429  referenced.classId = ProcedureRelationId;
430  referenced.objectId = valOid;
431  referenced.objectSubId = 0;
432  recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
433  }
434 
435  /* Post creation hook for new procedural language */
436  InvokeObjectPostCreateHook(LanguageRelationId, myself.objectId, 0);
437 
439 
440  return myself;
441 }
#define NameGetDatum(X)
Definition: postgres.h:578
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:145
#define RelationGetDescr(relation)
Definition: rel.h:433
bool pg_language_ownercheck(Oid lan_oid, Oid roleid)
Definition: aclchk.c:4852
#define PointerGetDatum(X)
Definition: postgres.h:539
long deleteDependencyRecordsFor(Oid classId, Oid objectId, bool skipExtensionDeps)
Definition: pg_depend.c:191
int errcode(int sqlerrcode)
Definition: elog.c:575
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:44
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1074
#define heap_close(r, l)
Definition: heapam.h:97
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:159
int namestrcpy(Name name, const char *str)
Definition: name.c:216
#define OidIsValid(objectId)
Definition: c.h:605
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3348
#define ObjectIdGetDatum(X)
Definition: postgres.h:490
#define ERROR
Definition: elog.h:43
Oid CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:163
ItemPointerData t_self
Definition: htup.h:65
Definition: c.h:570
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ereport(elevel, rest)
Definition: elog.h:122
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
uintptr_t Datum
Definition: postgres.h:365
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
#define BoolGetDatum(X)
Definition: postgres.h:385
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition: pg_depend.c:139
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:211
static Datum values[MAXATTR]
Definition: bootstrap.c:164
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:712
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1173
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32

◆ CreateProceduralLanguage()

ObjectAddress CreateProceduralLanguage ( CreatePLangStmt stmt)

Definition at line 63 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().

64 {
65  PLTemplate *pltemplate;
66  ObjectAddress tmpAddr;
67  Oid handlerOid,
68  inlineOid,
69  valOid;
70  Oid funcrettype;
71  Oid funcargtypes[1];
72 
73  /*
74  * If we have template information for the language, ignore the supplied
75  * parameters (if any) and use the template information.
76  */
77  if ((pltemplate = find_language_template(stmt->plname)) != NULL)
78  {
79  List *funcname;
80 
81  /*
82  * Give a notice if we are ignoring supplied parameters.
83  */
84  if (stmt->plhandler)
86  (errmsg("using pg_pltemplate information instead of CREATE LANGUAGE parameters")));
87 
88  /*
89  * Check permission
90  */
91  if (!superuser())
92  {
93  if (!pltemplate->tmpldbacreate)
94  ereport(ERROR,
95  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
96  errmsg("must be superuser to create procedural language \"%s\"",
97  stmt->plname)));
101  }
102 
103  /*
104  * Find or create the handler function, which we force to be in the
105  * pg_catalog schema. If already present, it must have the correct
106  * return type.
107  */
108  funcname = SystemFuncName(pltemplate->tmplhandler);
109  handlerOid = LookupFuncName(funcname, 0, funcargtypes, true);
110  if (OidIsValid(handlerOid))
111  {
112  funcrettype = get_func_rettype(handlerOid);
113  if (funcrettype != LANGUAGE_HANDLEROID)
114  ereport(ERROR,
115  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
116  errmsg("function %s must return type %s",
117  NameListToString(funcname), "language_handler")));
118  }
119  else
120  {
121  tmpAddr = ProcedureCreate(pltemplate->tmplhandler,
122  PG_CATALOG_NAMESPACE,
123  false, /* replace */
124  false, /* returnsSet */
125  LANGUAGE_HANDLEROID,
126  BOOTSTRAP_SUPERUSERID,
127  ClanguageId,
128  F_FMGR_C_VALIDATOR,
129  pltemplate->tmplhandler,
130  pltemplate->tmpllibrary,
131  PROKIND_FUNCTION,
132  false, /* security_definer */
133  false, /* isLeakProof */
134  false, /* isStrict */
135  PROVOLATILE_VOLATILE,
136  PROPARALLEL_UNSAFE,
137  buildoidvector(funcargtypes, 0),
138  PointerGetDatum(NULL),
139  PointerGetDatum(NULL),
140  PointerGetDatum(NULL),
141  NIL,
142  PointerGetDatum(NULL),
143  PointerGetDatum(NULL),
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  1,
184  0);
185  inlineOid = tmpAddr.objectId;
186  }
187  }
188  else
189  inlineOid = InvalidOid;
190 
191  /*
192  * Likewise for the validator, if required; but we don't care about
193  * its return type.
194  */
195  if (pltemplate->tmplvalidator)
196  {
197  funcname = SystemFuncName(pltemplate->tmplvalidator);
198  funcargtypes[0] = OIDOID;
199  valOid = LookupFuncName(funcname, 1, funcargtypes, true);
200  if (!OidIsValid(valOid))
201  {
202  tmpAddr = ProcedureCreate(pltemplate->tmplvalidator,
203  PG_CATALOG_NAMESPACE,
204  false, /* replace */
205  false, /* returnsSet */
206  VOIDOID,
207  BOOTSTRAP_SUPERUSERID,
208  ClanguageId,
209  F_FMGR_C_VALIDATOR,
210  pltemplate->tmplvalidator,
211  pltemplate->tmpllibrary,
212  PROKIND_FUNCTION,
213  false, /* security_definer */
214  false, /* isLeakProof */
215  true, /* isStrict */
216  PROVOLATILE_VOLATILE,
217  PROPARALLEL_UNSAFE,
218  buildoidvector(funcargtypes, 1),
219  PointerGetDatum(NULL),
220  PointerGetDatum(NULL),
221  PointerGetDatum(NULL),
222  NIL,
223  PointerGetDatum(NULL),
224  PointerGetDatum(NULL),
225  1,
226  0);
227  valOid = tmpAddr.objectId;
228  }
229  }
230  else
231  valOid = InvalidOid;
232 
233  /* ok, create it */
234  return create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
235  handlerOid, inlineOid,
236  valOid, pltemplate->tmpltrusted);
237  }
238  else
239  {
240  /*
241  * No template, so use the provided information. If there's no
242  * handler clause, the user is trying to rely on a template that we
243  * don't have, so complain accordingly.
244  */
245  if (!stmt->plhandler)
246  ereport(ERROR,
247  (errcode(ERRCODE_UNDEFINED_OBJECT),
248  errmsg("unsupported language \"%s\"",
249  stmt->plname),
250  errhint("The supported languages are listed in the pg_pltemplate system catalog.")));
251 
252  /*
253  * Check permission
254  */
255  if (!superuser())
256  ereport(ERROR,
257  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
258  errmsg("must be superuser to create custom procedural language")));
259 
260  /*
261  * Lookup the PL handler function and check that it is of the expected
262  * return type
263  */
264  handlerOid = LookupFuncName(stmt->plhandler, 0, funcargtypes, false);
265  funcrettype = get_func_rettype(handlerOid);
266  if (funcrettype != LANGUAGE_HANDLEROID)
267  {
268  /*
269  * We allow OPAQUE just so we can load old dump files. When we
270  * see a handler function declared OPAQUE, change it to
271  * LANGUAGE_HANDLER. (This is probably obsolete and removable?)
272  */
273  if (funcrettype == OPAQUEOID)
274  {
276  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
277  errmsg("changing return type of function %s from %s to %s",
279  "opaque", "language_handler")));
280  SetFunctionReturnType(handlerOid, LANGUAGE_HANDLEROID);
281  }
282  else
283  ereport(ERROR,
284  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
285  errmsg("function %s must return type %s",
286  NameListToString(stmt->plhandler), "language_handler")));
287  }
288 
289  /* validate the inline function */
290  if (stmt->plinline)
291  {
292  funcargtypes[0] = INTERNALOID;
293  inlineOid = LookupFuncName(stmt->plinline, 1, funcargtypes, false);
294  /* return value is ignored, so we don't check the type */
295  }
296  else
297  inlineOid = InvalidOid;
298 
299  /* validate the validator function */
300  if (stmt->plvalidator)
301  {
302  funcargtypes[0] = OIDOID;
303  valOid = LookupFuncName(stmt->plvalidator, 1, funcargtypes, false);
304  /* return value is ignored, so we don't check the type */
305  }
306  else
307  valOid = InvalidOid;
308 
309  /* ok, create it */
310  return create_proc_lang(stmt->plname, stmt->replace, GetUserId(),
311  handlerOid, inlineOid,
312  valOid, stmt->pltrusted);
313  }
314 }
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, float4 procost, float4 prorows)
Definition: pg_proc.c:66
#define NIL
Definition: pg_list.h:69
List * SystemFuncName(char *name)
int errhint(const char *fmt,...)
Definition: elog.c:987
char * tmpllibrary
Definition: proclang.c:50
Oid GetUserId(void)
Definition: miscinit.c:379
#define PointerGetDatum(X)
Definition: postgres.h:539
bool tmpltrusted
Definition: proclang.c:45
int errcode(int sqlerrcode)
Definition: elog.c:575
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:320
#define OidIsValid(objectId)
Definition: c.h:605
Oid get_func_rettype(Oid funcid)
Definition: lsyscache.c:1444
char * tmplinline
Definition: proclang.c:48
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3348
#define ERROR
Definition: elog.h:43
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2056
#define ereport(elevel, rest)
Definition: elog.h:122
bool pg_database_ownercheck(Oid db_oid, Oid roleid)
Definition: aclchk.c:5166
List * plvalidator
Definition: parsenodes.h:2425
bool tmpldbacreate
Definition: proclang.c:46
#define WARNING
Definition: elog.h:40
char * NameListToString(List *names)
Definition: namespace.c:3082
Oid MyDatabaseId
Definition: globals.c:86
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
Oid LookupFuncName(List *funcname, int nargs, const Oid *argtypes, bool noError)
Definition: parse_func.c:1974
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * tmplhandler
Definition: proclang.c:47
static PLTemplate * find_language_template(const char *languageName)
Definition: proclang.c:447
Definition: pg_list.h:45
char * tmplvalidator
Definition: proclang.c:49

◆ DropProceduralLanguageById()

void DropProceduralLanguageById ( Oid  langOid)

Definition at line 524 of file proclang.c.

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

Referenced by doDeletion().

525 {
526  Relation rel;
527  HeapTuple langTup;
528 
529  rel = heap_open(LanguageRelationId, RowExclusiveLock);
530 
531  langTup = SearchSysCache1(LANGOID, ObjectIdGetDatum(langOid));
532  if (!HeapTupleIsValid(langTup)) /* should not happen */
533  elog(ERROR, "cache lookup failed for language %u", langOid);
534 
535  CatalogTupleDelete(rel, &langTup->t_self);
536 
537  ReleaseSysCache(langTup);
538 
540 }
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:256
#define heap_close(r, l)
Definition: heapam.h:97
#define ObjectIdGetDatum(X)
Definition: postgres.h:490
#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:1112
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define elog
Definition: elog.h:219

◆ find_language_template()

static PLTemplate * find_language_template ( const char *  languageName)
static

Definition at line 447 of file proclang.c.

References AccessShareLock, BTEqualStrategyNumber, CStringGetDatum, GETSTRUCT, heap_close, heap_getattr, heap_open(), HeapTupleIsValid, palloc0(), PLTemplateNameIndexId, RelationGetDescr, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), TextDatumGetCString, PLTemplate::tmpldbacreate, PLTemplate::tmplhandler, PLTemplate::tmplinline, PLTemplate::tmpllibrary, PLTemplate::tmpltrusted, and PLTemplate::tmplvalidator.

Referenced by CreateProceduralLanguage(), and PLTemplateExists().

448 {
449  PLTemplate *result;
450  Relation rel;
451  SysScanDesc scan;
452  ScanKeyData key;
453  HeapTuple tup;
454 
455  rel = heap_open(PLTemplateRelationId, AccessShareLock);
456 
457  ScanKeyInit(&key,
458  Anum_pg_pltemplate_tmplname,
459  BTEqualStrategyNumber, F_NAMEEQ,
460  CStringGetDatum(languageName));
461  scan = systable_beginscan(rel, PLTemplateNameIndexId, true,
462  NULL, 1, &key);
463 
464  tup = systable_getnext(scan);
465  if (HeapTupleIsValid(tup))
466  {
468  Datum datum;
469  bool isnull;
470 
471  result = (PLTemplate *) palloc0(sizeof(PLTemplate));
472  result->tmpltrusted = tmpl->tmpltrusted;
473  result->tmpldbacreate = tmpl->tmpldbacreate;
474 
475  /* Remaining fields are variable-width so we need heap_getattr */
476  datum = heap_getattr(tup, Anum_pg_pltemplate_tmplhandler,
477  RelationGetDescr(rel), &isnull);
478  if (!isnull)
479  result->tmplhandler = TextDatumGetCString(datum);
480 
481  datum = heap_getattr(tup, Anum_pg_pltemplate_tmplinline,
482  RelationGetDescr(rel), &isnull);
483  if (!isnull)
484  result->tmplinline = TextDatumGetCString(datum);
485 
486  datum = heap_getattr(tup, Anum_pg_pltemplate_tmplvalidator,
487  RelationGetDescr(rel), &isnull);
488  if (!isnull)
489  result->tmplvalidator = TextDatumGetCString(datum);
490 
491  datum = heap_getattr(tup, Anum_pg_pltemplate_tmpllibrary,
492  RelationGetDescr(rel), &isnull);
493  if (!isnull)
494  result->tmpllibrary = TextDatumGetCString(datum);
495 
496  /* Ignore template if handler or library info is missing */
497  if (!result->tmplhandler || !result->tmpllibrary)
498  result = NULL;
499  }
500  else
501  result = NULL;
502 
503  systable_endscan(scan);
504 
506 
507  return result;
508 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:502
#define GETSTRUCT(TUP)
Definition: htup_details.h:673
char * tmpllibrary
Definition: proclang.c:50
#define RelationGetDescr(relation)
Definition: rel.h:433
bool tmpltrusted
Definition: proclang.c:45
#define AccessShareLock
Definition: lockdefs.h:36
#define heap_close(r, l)
Definition: heapam.h:97
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:331
char * tmplinline
Definition: proclang.c:48
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:419
FormData_pg_pltemplate * Form_pg_pltemplate
Definition: pg_pltemplate.h:50
#define CStringGetDatum(X)
Definition: postgres.h:561
bool tmpldbacreate
Definition: proclang.c:46
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:786
#define TextDatumGetCString(d)
Definition: builtins.h:96
void * palloc0(Size size)
Definition: mcxt.c:955
uintptr_t Datum
Definition: postgres.h:365
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
#define PLTemplateNameIndexId
Definition: indexing.h:215
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
char * tmplhandler
Definition: proclang.c:47
char * tmplvalidator
Definition: proclang.c:49
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ get_language_oid()

Oid get_language_oid ( const char *  langname,
bool  missing_ok 
)

Definition at line 549 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().

550 {
551  Oid oid;
552 
553  oid = GetSysCacheOid1(LANGNAME, CStringGetDatum(langname));
554  if (!OidIsValid(oid) && !missing_ok)
555  ereport(ERROR,
556  (errcode(ERRCODE_UNDEFINED_OBJECT),
557  errmsg("language \"%s\" does not exist", langname)));
558  return oid;
559 }
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:605
#define GetSysCacheOid1(cacheId, key1)
Definition: syscache.h:191
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:561
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ PLTemplateExists()

bool PLTemplateExists ( const char *  languageName)

Definition at line 515 of file proclang.c.

References find_language_template().

Referenced by CreateFunction(), and ExecuteDoStmt().

516 {
517  return (find_language_template(languageName) != NULL);
518 }
static PLTemplate * find_language_template(const char *languageName)
Definition: proclang.c:447