PostgreSQL Source Code git master
Loading...
Searching...
No Matches
proclang.c
Go to the documentation of this file.
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"
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 */
39{
40 const char *languageName = stmt->plname;
44 valOid;
47 Relation rel;
48 TupleDesc tupDesc;
50 bool nulls[Natts_pg_language];
52 NameData langname;
56 bool is_update;
59 ObjectAddresses *addrs;
60
61 /*
62 * Check permission
63 */
64 if (!superuser())
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 handlerOid = LookupFuncName(stmt->plhandler, 0, NULL, false);
79 errmsg("function %s must return type %s",
80 NameListToString(stmt->plhandler), "language_handler")));
81
82 /* validate the inline function */
83 if (stmt->plinline)
84 {
86 inlineOid = LookupFuncName(stmt->plinline, 1, funcargtypes, false);
87 /* return value is ignored, so we don't check the type */
88 }
89 else
91
92 /* validate the validator function */
93 if (stmt->plvalidator)
94 {
96 valOid = LookupFuncName(stmt->plvalidator, 1, funcargtypes, false);
97 /* return value is ignored, so we don't check the type */
98 }
99 else
101
102 /* ok to create it */
104 tupDesc = RelationGetDescr(rel);
105
106 /* Prepare data to be inserted */
107 memset(values, 0, sizeof(values));
108 memset(nulls, false, sizeof(nulls));
109 memset(replaces, true, sizeof(replaces));
110
111 namestrcpy(&langname, languageName);
119 nulls[Anum_pg_language_lanacl - 1] = true;
120
121 /* Check for pre-existing definition */
123
125 {
127
128 /* There is one; okay to replace it? */
129 if (!stmt->replace)
132 errmsg("language \"%s\" already exists", languageName)));
133
134 /* This is currently pointless, since we already checked superuser */
135#ifdef NOT_USED
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 replaces[Anum_pg_language_oid - 1] = false;
148
149 /* Okay, do it... */
150 tup = heap_modify_tuple(oldtup, tupDesc, values, nulls, replaces);
151 CatalogTupleUpdate(rel, &tup->t_self, tup);
152
153 langoid = oldform->oid;
155 is_update = true;
156 }
157 else
158 {
159 /* Creating a new language */
163 tup = heap_form_tuple(tupDesc, values, nulls);
165 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 myself.classId = LanguageRelationId;
175 myself.objectId = langoid;
176 myself.objectSubId = 0;
177
178 if (is_update)
179 deleteDependencyRecordsFor(myself.classId, myself.objectId, true);
180
181 /* dependency on owner of language */
182 if (!is_update)
183 recordDependencyOnOwner(myself.classId, myself.objectId,
185
186 /* dependency on extension */
188
189 addrs = new_object_addresses();
190
191 /* dependency on the PL handler function */
194
195 /* dependency on the inline handler function, if any */
197 {
200 }
201
202 /* dependency on the validator function, if any */
203 if (OidIsValid(valOid))
204 {
207 }
208
211
212 /* Post creation hook for new procedural language */
214
216
217 return myself;
218}
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 */
226Oid
227get_language_oid(const char *langname, bool missing_ok)
228{
229 Oid oid;
230
232 CStringGetDatum(langname));
233 if (!OidIsValid(oid) && !missing_ok)
236 errmsg("language \"%s\" does not exist", langname)));
237 return oid;
238}
@ ACLCHECK_NOT_OWNER
Definition acl.h:185
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition aclchk.c:2654
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition aclchk.c:4090
static Datum values[MAXATTR]
Definition bootstrap.c:155
#define Assert(condition)
Definition c.h:873
#define OidIsValid(objectId)
Definition c.h:788
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition catalog.c:448
void record_object_address_dependencies(const ObjectAddress *depender, ObjectAddresses *referenced, DependencyType behavior)
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
ObjectAddresses * new_object_addresses(void)
void free_object_addresses(ObjectAddresses *addrs)
@ DEPENDENCY_NORMAL
Definition dependency.h:33
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
Definition heaptuple.c:1210
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition heaptuple.c:1117
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
#define stmt
void CatalogTupleUpdate(Relation heapRel, const ItemPointerData *otid, HeapTuple tup)
Definition indexing.c:313
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition indexing.c:233
#define RowExclusiveLock
Definition lockdefs.h:38
Oid get_func_rettype(Oid funcid)
Definition lsyscache.c:1805
Oid GetUserId(void)
Definition miscinit.c:469
void namestrcpy(Name name, const char *str)
Definition name.c:233
char * NameListToString(const List *names)
Definition namespace.c:3664
#define InvokeObjectPostCreateHook(classId, objectId, subId)
#define ObjectAddressSet(addr, class_id, object_id)
Oid LookupFuncName(List *funcname, int nargs, const Oid *argtypes, bool missing_ok)
@ OBJECT_LANGUAGE
long deleteDependencyRecordsFor(Oid classId, Oid objectId, bool skipExtensionDeps)
Definition pg_depend.c:301
void recordDependencyOnCurrentExtension(const ObjectAddress *object, bool isReplace)
Definition pg_depend.c:193
FormData_pg_language * Form_pg_language
Definition pg_language.h:65
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
static Datum BoolGetDatum(bool X)
Definition postgres.h:112
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
static Datum NameGetDatum(const NameData *X)
Definition postgres.h:403
uint64_t Datum
Definition postgres.h:70
static Datum CStringGetDatum(const char *X)
Definition postgres.h:380
#define InvalidOid
unsigned int Oid
static int fb(int x)
Oid get_language_oid(const char *langname, bool missing_ok)
Definition proclang.c:227
ObjectAddress CreateProceduralLanguage(CreatePLangStmt *stmt)
Definition proclang.c:38
#define RelationGetDescr(relation)
Definition rel.h:540
#define ERRCODE_DUPLICATE_OBJECT
Definition streamutil.c:30
Definition c.h:760
bool superuser(void)
Definition superuser.c:46
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:264
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition syscache.c:220
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition syscache.h:109
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40