]> granicus.if.org Git - postgresql/blob - src/backend/catalog/pg_proc.c
Pgindent run for 8.0.
[postgresql] / src / backend / catalog / pg_proc.c
1 /*-------------------------------------------------------------------------
2  *
3  * pg_proc.c
4  *        routines to support manipulation of the pg_proc relation
5  *
6  * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/catalog/pg_proc.c,v 1.119 2004/08/29 05:06:41 momjian Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include "access/heapam.h"
18 #include "catalog/catname.h"
19 #include "catalog/dependency.h"
20 #include "catalog/indexing.h"
21 #include "catalog/pg_language.h"
22 #include "catalog/pg_proc.h"
23 #include "executor/executor.h"
24 #include "fmgr.h"
25 #include "miscadmin.h"
26 #include "mb/pg_wchar.h"
27 #include "parser/parse_coerce.h"
28 #include "parser/parse_expr.h"
29 #include "parser/parse_type.h"
30 #include "tcop/pquery.h"
31 #include "tcop/tcopprot.h"
32 #include "utils/acl.h"
33 #include "utils/builtins.h"
34 #include "utils/lsyscache.h"
35 #include "utils/syscache.h"
36
37
38 /* GUC parameter */
39 bool            check_function_bodies = true;
40
41
42 Datum           fmgr_internal_validator(PG_FUNCTION_ARGS);
43 Datum           fmgr_c_validator(PG_FUNCTION_ARGS);
44 Datum           fmgr_sql_validator(PG_FUNCTION_ARGS);
45
46 static Datum create_parameternames_array(int parameterCount,
47                                                         const char *parameterNames[]);
48 static void sql_function_parse_error_callback(void *arg);
49 static int match_prosrc_to_query(const char *prosrc, const char *queryText,
50                                           int cursorpos);
51 static bool match_prosrc_to_literal(const char *prosrc, const char *literal,
52                                                 int cursorpos, int *newcursorpos);
53
54
55 /* ----------------------------------------------------------------
56  *              ProcedureCreate
57  * ----------------------------------------------------------------
58  */
59 Oid
60 ProcedureCreate(const char *procedureName,
61                                 Oid procNamespace,
62                                 bool replace,
63                                 bool returnsSet,
64                                 Oid returnType,
65                                 Oid languageObjectId,
66                                 Oid languageValidator,
67                                 const char *prosrc,
68                                 const char *probin,
69                                 bool isAgg,
70                                 bool security_definer,
71                                 bool isStrict,
72                                 char volatility,
73                                 int parameterCount,
74                                 const Oid *parameterTypes,
75                                 const char *parameterNames[])
76 {
77         int                     i;
78         Relation        rel;
79         HeapTuple       tup;
80         HeapTuple       oldtup;
81         char            nulls[Natts_pg_proc];
82         Datum           values[Natts_pg_proc];
83         char            replaces[Natts_pg_proc];
84         Oid                     typev[FUNC_MAX_ARGS];
85         Datum           namesarray;
86         Oid                     relid;
87         NameData        procname;
88         TupleDesc       tupDesc;
89         Oid                     retval;
90         bool            is_update;
91         ObjectAddress myself,
92                                 referenced;
93
94         /*
95          * sanity checks
96          */
97         Assert(PointerIsValid(prosrc));
98         Assert(PointerIsValid(probin));
99
100         if (parameterCount < 0 || parameterCount > FUNC_MAX_ARGS)
101                 ereport(ERROR,
102                                 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
103                                  errmsg("functions cannot have more than %d arguments",
104                                                 FUNC_MAX_ARGS)));
105
106         /*
107          * Do not allow return type ANYARRAY or ANYELEMENT unless at least one
108          * argument is also ANYARRAY or ANYELEMENT
109          */
110         if (returnType == ANYARRAYOID || returnType == ANYELEMENTOID)
111         {
112                 bool            genericParam = false;
113
114                 for (i = 0; i < parameterCount; i++)
115                 {
116                         if (parameterTypes[i] == ANYARRAYOID ||
117                                 parameterTypes[i] == ANYELEMENTOID)
118                         {
119                                 genericParam = true;
120                                 break;
121                         }
122                 }
123
124                 if (!genericParam)
125                         ereport(ERROR,
126                                         (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
127                                          errmsg("cannot determine result data type"),
128                                          errdetail("A function returning \"anyarray\" or \"anyelement\" must have at least one argument of either type.")));
129         }
130
131         /* Make sure we have a zero-padded param type array */
132         MemSet(typev, 0, FUNC_MAX_ARGS * sizeof(Oid));
133         if (parameterCount > 0)
134                 memcpy(typev, parameterTypes, parameterCount * sizeof(Oid));
135
136         /* Process param names, if given */
137         namesarray = create_parameternames_array(parameterCount, parameterNames);
138
139         /*
140          * don't allow functions of complex types that have the same name as
141          * existing attributes of the type
142          */
143         if (parameterCount == 1 && OidIsValid(typev[0]) &&
144                 (relid = typeidTypeRelid(typev[0])) != InvalidOid &&
145                 get_attnum(relid, (char *) procedureName) != InvalidAttrNumber)
146                 ereport(ERROR,
147                                 (errcode(ERRCODE_DUPLICATE_COLUMN),
148                                  errmsg("\"%s\" is already an attribute of type %s",
149                                                 procedureName, format_type_be(typev[0]))));
150
151         /*
152          * All seems OK; prepare the data to be inserted into pg_proc.
153          */
154
155         for (i = 0; i < Natts_pg_proc; ++i)
156         {
157                 nulls[i] = ' ';
158                 values[i] = (Datum) NULL;
159                 replaces[i] = 'r';
160         }
161
162         i = 0;
163         namestrcpy(&procname, procedureName);
164         values[i++] = NameGetDatum(&procname);          /* proname */
165         values[i++] = ObjectIdGetDatum(procNamespace);          /* pronamespace */
166         values[i++] = Int32GetDatum(GetUserId());       /* proowner */
167         values[i++] = ObjectIdGetDatum(languageObjectId);       /* prolang */
168         values[i++] = BoolGetDatum(isAgg);      /* proisagg */
169         values[i++] = BoolGetDatum(security_definer);           /* prosecdef */
170         values[i++] = BoolGetDatum(isStrict);           /* proisstrict */
171         values[i++] = BoolGetDatum(returnsSet);         /* proretset */
172         values[i++] = CharGetDatum(volatility);         /* provolatile */
173         values[i++] = UInt16GetDatum(parameterCount);           /* pronargs */
174         values[i++] = ObjectIdGetDatum(returnType); /* prorettype */
175         values[i++] = PointerGetDatum(typev);           /* proargtypes */
176         values[i++] = namesarray;       /* proargnames */
177         if (namesarray == PointerGetDatum(NULL))
178                 nulls[Anum_pg_proc_proargnames - 1] = 'n';
179         values[i++] = DirectFunctionCall1(textin,       /* prosrc */
180                                                                           CStringGetDatum(prosrc));
181         values[i++] = DirectFunctionCall1(textin,       /* probin */
182                                                                           CStringGetDatum(probin));
183         /* proacl will be handled below */
184
185         rel = heap_openr(ProcedureRelationName, RowExclusiveLock);
186         tupDesc = rel->rd_att;
187
188         /* Check for pre-existing definition */
189         oldtup = SearchSysCache(PROCNAMENSP,
190                                                         PointerGetDatum(procedureName),
191                                                         UInt16GetDatum(parameterCount),
192                                                         PointerGetDatum(typev),
193                                                         ObjectIdGetDatum(procNamespace));
194
195         if (HeapTupleIsValid(oldtup))
196         {
197                 /* There is one; okay to replace it? */
198                 Form_pg_proc oldproc = (Form_pg_proc) GETSTRUCT(oldtup);
199
200                 if (!replace)
201                         ereport(ERROR,
202                                         (errcode(ERRCODE_DUPLICATE_FUNCTION),
203                                          errmsg("function \"%s\" already exists with same argument types",
204                                                         procedureName)));
205                 if (GetUserId() != oldproc->proowner && !superuser())
206                         aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PROC,
207                                                    procedureName);
208
209                 /*
210                  * Not okay to change the return type of the existing proc, since
211                  * existing rules, views, etc may depend on the return type.
212                  */
213                 if (returnType != oldproc->prorettype ||
214                         returnsSet != oldproc->proretset)
215                         ereport(ERROR,
216                                         (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
217                                 errmsg("cannot change return type of existing function"),
218                                          errhint("Use DROP FUNCTION first.")));
219
220                 /* Can't change aggregate status, either */
221                 if (oldproc->proisagg != isAgg)
222                 {
223                         if (oldproc->proisagg)
224                                 ereport(ERROR,
225                                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
226                                                  errmsg("function \"%s\" is an aggregate",
227                                                                 procedureName)));
228                         else
229                                 ereport(ERROR,
230                                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
231                                                  errmsg("function \"%s\" is not an aggregate",
232                                                                 procedureName)));
233                 }
234
235                 /* do not change existing ownership or permissions, either */
236                 replaces[Anum_pg_proc_proowner - 1] = ' ';
237                 replaces[Anum_pg_proc_proacl - 1] = ' ';
238
239                 /* Okay, do it... */
240                 tup = heap_modifytuple(oldtup, rel, values, nulls, replaces);
241                 simple_heap_update(rel, &tup->t_self, tup);
242
243                 ReleaseSysCache(oldtup);
244                 is_update = true;
245         }
246         else
247         {
248                 /* Creating a new procedure */
249
250                 /* start out with empty permissions */
251                 nulls[Anum_pg_proc_proacl - 1] = 'n';
252
253                 tup = heap_formtuple(tupDesc, values, nulls);
254                 simple_heap_insert(rel, tup);
255                 is_update = false;
256         }
257
258         /* Need to update indexes for either the insert or update case */
259         CatalogUpdateIndexes(rel, tup);
260
261         retval = HeapTupleGetOid(tup);
262
263         /*
264          * Create dependencies for the new function.  If we are updating an
265          * existing function, first delete any existing pg_depend entries.
266          */
267         if (is_update)
268                 deleteDependencyRecordsFor(RelOid_pg_proc, retval);
269
270         myself.classId = RelOid_pg_proc;
271         myself.objectId = retval;
272         myself.objectSubId = 0;
273
274         /* dependency on namespace */
275         referenced.classId = get_system_catalog_relid(NamespaceRelationName);
276         referenced.objectId = procNamespace;
277         referenced.objectSubId = 0;
278         recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
279
280         /* dependency on implementation language */
281         referenced.classId = get_system_catalog_relid(LanguageRelationName);
282         referenced.objectId = languageObjectId;
283         referenced.objectSubId = 0;
284         recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
285
286         /* dependency on return type */
287         referenced.classId = RelOid_pg_type;
288         referenced.objectId = returnType;
289         referenced.objectSubId = 0;
290         recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
291
292         /* dependency on input types */
293         for (i = 0; i < parameterCount; i++)
294         {
295                 referenced.classId = RelOid_pg_type;
296                 referenced.objectId = typev[i];
297                 referenced.objectSubId = 0;
298                 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
299         }
300
301         heap_freetuple(tup);
302
303         heap_close(rel, RowExclusiveLock);
304
305         /* Verify function body */
306         if (OidIsValid(languageValidator))
307         {
308                 /* Advance command counter so new tuple can be seen by validator */
309                 CommandCounterIncrement();
310                 OidFunctionCall1(languageValidator, ObjectIdGetDatum(retval));
311         }
312
313         return retval;
314 }
315
316
317 /*
318  * create_parameternames_array - build proargnames value from an array
319  * of C strings.  Returns a NULL pointer if no names provided.
320  */
321 static Datum
322 create_parameternames_array(int parameterCount, const char *parameterNames[])
323 {
324         Datum           elems[FUNC_MAX_ARGS];
325         bool            found = false;
326         ArrayType  *names;
327         int                     i;
328
329         if (!parameterNames)
330                 return PointerGetDatum(NULL);
331
332         for (i = 0; i < parameterCount; i++)
333         {
334                 const char *s = parameterNames[i];
335
336                 if (s && *s)
337                         found = true;
338                 else
339                         s = "";
340
341                 elems[i] = DirectFunctionCall1(textin, CStringGetDatum(s));
342         }
343
344         if (!found)
345                 return PointerGetDatum(NULL);
346
347         names = construct_array(elems, parameterCount, TEXTOID, -1, false, 'i');
348
349         return PointerGetDatum(names);
350 }
351
352
353 /*
354  * check_sql_fn_retval() -- check return value of a list of sql parse trees.
355  *
356  * The return value of a sql function is the value returned by
357  * the final query in the function.  We do some ad-hoc type checking here
358  * to be sure that the user is returning the type he claims.
359  *
360  * This is normally applied during function definition, but in the case
361  * of a function with polymorphic arguments, we instead apply it during
362  * function execution startup.  The rettype is then the actual resolved
363  * output type of the function, rather than the declared type.  (Therefore,
364  * we should never see ANYARRAY or ANYELEMENT as rettype.)
365  *
366  * The return value is true if the function returns the entire tuple result
367  * of its final SELECT, and false otherwise.  Note that because we allow
368  * "SELECT rowtype_expression", this may be false even when the declared
369  * function return type is a rowtype.
370  */
371 bool
372 check_sql_fn_retval(Oid rettype, char fn_typtype, List *queryTreeList)
373 {
374         Query      *parse;
375         int                     cmd;
376         List       *tlist;
377         ListCell   *tlistitem;
378         int                     tlistlen;
379         Oid                     typerelid;
380         Oid                     restype;
381         Relation        reln;
382         int                     relnatts;               /* physical number of columns in rel */
383         int                     rellogcols;             /* # of nondeleted columns in rel */
384         int                     colindex;               /* physical column index */
385
386         /* guard against empty function body; OK only if void return type */
387         if (queryTreeList == NIL)
388         {
389                 if (rettype != VOIDOID)
390                         ereport(ERROR,
391                                         (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
392                                          errmsg("return type mismatch in function declared to return %s",
393                                                         format_type_be(rettype)),
394                          errdetail("Function's final statement must be a SELECT.")));
395                 return false;
396         }
397
398         /* find the final query */
399         parse = (Query *) lfirst(list_tail(queryTreeList));
400
401         cmd = parse->commandType;
402         tlist = parse->targetList;
403
404         /*
405          * The last query must be a SELECT if and only if return type isn't
406          * VOID.
407          */
408         if (rettype == VOIDOID)
409         {
410                 if (cmd == CMD_SELECT)
411                         ereport(ERROR,
412                                         (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
413                                          errmsg("return type mismatch in function declared to return %s",
414                                                         format_type_be(rettype)),
415                                          errdetail("Function's final statement must not be a SELECT.")));
416                 return false;
417         }
418
419         /* by here, the function is declared to return some type */
420         if (cmd != CMD_SELECT)
421                 ereport(ERROR,
422                                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
423                  errmsg("return type mismatch in function declared to return %s",
424                                 format_type_be(rettype)),
425                          errdetail("Function's final statement must be a SELECT.")));
426
427         /*
428          * Count the non-junk entries in the result targetlist.
429          */
430         tlistlen = ExecCleanTargetListLength(tlist);
431
432         typerelid = typeidTypeRelid(rettype);
433
434         if (fn_typtype == 'b' || fn_typtype == 'd')
435         {
436                 /* Shouldn't have a typerelid */
437                 Assert(typerelid == InvalidOid);
438
439                 /*
440                  * For base-type returns, the target list should have exactly one
441                  * entry, and its type should agree with what the user declared.
442                  * (As of Postgres 7.2, we accept binary-compatible types too.)
443                  */
444                 if (tlistlen != 1)
445                         ereport(ERROR,
446                                         (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
447                                          errmsg("return type mismatch in function declared to return %s",
448                                                         format_type_be(rettype)),
449                          errdetail("Final SELECT must return exactly one column.")));
450
451                 restype = ((TargetEntry *) linitial(tlist))->resdom->restype;
452                 if (!IsBinaryCoercible(restype, rettype))
453                         ereport(ERROR,
454                                         (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
455                                          errmsg("return type mismatch in function declared to return %s",
456                                                         format_type_be(rettype)),
457                                          errdetail("Actual return type is %s.",
458                                                            format_type_be(restype))));
459         }
460         else if (fn_typtype == 'c')
461         {
462                 /* Must have a typerelid */
463                 Assert(typerelid != InvalidOid);
464
465                 /*
466                  * If the target list is of length 1, and the type of the varnode
467                  * in the target list matches the declared return type, this is
468                  * okay. This can happen, for example, where the body of the
469                  * function is 'SELECT func2()', where func2 has the same return
470                  * type as the function that's calling it.
471                  */
472                 if (tlistlen == 1)
473                 {
474                         restype = ((TargetEntry *) linitial(tlist))->resdom->restype;
475                         if (IsBinaryCoercible(restype, rettype))
476                                 return false;   /* NOT returning whole tuple */
477                 }
478
479                 /*
480                  * Otherwise verify that the targetlist matches the return tuple
481                  * type. This part of the typechecking is a hack. We look up the
482                  * relation that is the declared return type, and scan the
483                  * non-deleted attributes to ensure that they match the datatypes
484                  * of the non-resjunk columns.
485                  */
486                 reln = relation_open(typerelid, AccessShareLock);
487                 relnatts = reln->rd_rel->relnatts;
488                 rellogcols = 0;                 /* we'll count nondeleted cols as we go */
489                 colindex = 0;
490
491                 foreach(tlistitem, tlist)
492                 {
493                         TargetEntry *tle = (TargetEntry *) lfirst(tlistitem);
494                         Form_pg_attribute attr;
495                         Oid                     tletype;
496                         Oid                     atttype;
497
498                         if (tle->resdom->resjunk)
499                                 continue;
500
501                         do
502                         {
503                                 colindex++;
504                                 if (colindex > relnatts)
505                                         ereport(ERROR,
506                                                         (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
507                                                          errmsg("return type mismatch in function declared to return %s",
508                                                                         format_type_be(rettype)),
509                                         errdetail("Final SELECT returns too many columns.")));
510                                 attr = reln->rd_att->attrs[colindex - 1];
511                         } while (attr->attisdropped);
512                         rellogcols++;
513
514                         tletype = exprType((Node *) tle->expr);
515                         atttype = attr->atttypid;
516                         if (!IsBinaryCoercible(tletype, atttype))
517                                 ereport(ERROR,
518                                                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
519                                                  errmsg("return type mismatch in function declared to return %s",
520                                                                 format_type_be(rettype)),
521                                                  errdetail("Final SELECT returns %s instead of %s at column %d.",
522                                                                    format_type_be(tletype),
523                                                                    format_type_be(atttype),
524                                                                    rellogcols)));
525                 }
526
527                 for (;;)
528                 {
529                         colindex++;
530                         if (colindex > relnatts)
531                                 break;
532                         if (!reln->rd_att->attrs[colindex - 1]->attisdropped)
533                                 rellogcols++;
534                 }
535
536                 if (tlistlen != rellogcols)
537                         ereport(ERROR,
538                                         (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
539                                          errmsg("return type mismatch in function declared to return %s",
540                                                         format_type_be(rettype)),
541                                          errdetail("Final SELECT returns too few columns.")));
542
543                 relation_close(reln, AccessShareLock);
544
545                 /* Report that we are returning entire tuple result */
546                 return true;
547         }
548         else if (rettype == RECORDOID)
549         {
550                 /*
551                  * If the target list is of length 1, and the type of the varnode
552                  * in the target list matches the declared return type, this is
553                  * okay. This can happen, for example, where the body of the
554                  * function is 'SELECT func2()', where func2 has the same return
555                  * type as the function that's calling it.
556                  */
557                 if (tlistlen == 1)
558                 {
559                         restype = ((TargetEntry *) linitial(tlist))->resdom->restype;
560                         if (IsBinaryCoercible(restype, rettype))
561                                 return false;   /* NOT returning whole tuple */
562                 }
563
564                 /*
565                  * Otherwise assume we are returning the whole tuple.
566                  * Crosschecking against what the caller expects will happen at
567                  * runtime.
568                  */
569                 return true;
570         }
571         else if (rettype == ANYARRAYOID || rettype == ANYELEMENTOID)
572         {
573                 /* This should already have been caught ... */
574                 ereport(ERROR,
575                                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
576                                  errmsg("cannot determine result data type"),
577                                  errdetail("A function returning \"anyarray\" or \"anyelement\" must have at least one argument of either type.")));
578         }
579         else
580                 ereport(ERROR,
581                                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
582                           errmsg("return type %s is not supported for SQL functions",
583                                          format_type_be(rettype))));
584
585         return false;
586 }
587
588
589
590 /*
591  * Validator for internal functions
592  *
593  * Check that the given internal function name (the "prosrc" value) is
594  * a known builtin function.
595  */
596 Datum
597 fmgr_internal_validator(PG_FUNCTION_ARGS)
598 {
599         Oid                     funcoid = PG_GETARG_OID(0);
600         HeapTuple       tuple;
601         Form_pg_proc proc;
602         bool            isnull;
603         Datum           tmp;
604         char       *prosrc;
605
606         /*
607          * We do not honor check_function_bodies since it's unlikely the
608          * function name will be found later if it isn't there now.
609          */
610
611         tuple = SearchSysCache(PROCOID,
612                                                    ObjectIdGetDatum(funcoid),
613                                                    0, 0, 0);
614         if (!HeapTupleIsValid(tuple))
615                 elog(ERROR, "cache lookup failed for function %u", funcoid);
616         proc = (Form_pg_proc) GETSTRUCT(tuple);
617
618         tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosrc, &isnull);
619         if (isnull)
620                 elog(ERROR, "null prosrc");
621         prosrc = DatumGetCString(DirectFunctionCall1(textout, tmp));
622
623         if (fmgr_internal_function(prosrc) == InvalidOid)
624                 ereport(ERROR,
625                                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
626                                  errmsg("there is no built-in function named \"%s\"",
627                                                 prosrc)));
628
629         ReleaseSysCache(tuple);
630
631         PG_RETURN_VOID();
632 }
633
634
635
636 /*
637  * Validator for C language functions
638  *
639  * Make sure that the library file exists, is loadable, and contains
640  * the specified link symbol. Also check for a valid function
641  * information record.
642  */
643 Datum
644 fmgr_c_validator(PG_FUNCTION_ARGS)
645 {
646         Oid                     funcoid = PG_GETARG_OID(0);
647         void       *libraryhandle;
648         HeapTuple       tuple;
649         Form_pg_proc proc;
650         bool            isnull;
651         Datum           tmp;
652         char       *prosrc;
653         char       *probin;
654
655         /*
656          * It'd be most consistent to skip the check if
657          * !check_function_bodies, but the purpose of that switch is to be
658          * helpful for pg_dump loading, and for pg_dump loading it's much
659          * better if we *do* check.
660          */
661
662         tuple = SearchSysCache(PROCOID,
663                                                    ObjectIdGetDatum(funcoid),
664                                                    0, 0, 0);
665         if (!HeapTupleIsValid(tuple))
666                 elog(ERROR, "cache lookup failed for function %u", funcoid);
667         proc = (Form_pg_proc) GETSTRUCT(tuple);
668
669         tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosrc, &isnull);
670         if (isnull)
671                 elog(ERROR, "null prosrc");
672         prosrc = DatumGetCString(DirectFunctionCall1(textout, tmp));
673
674         tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_probin, &isnull);
675         if (isnull)
676                 elog(ERROR, "null probin");
677         probin = DatumGetCString(DirectFunctionCall1(textout, tmp));
678
679         (void) load_external_function(probin, prosrc, true, &libraryhandle);
680         (void) fetch_finfo_record(libraryhandle, prosrc);
681
682         ReleaseSysCache(tuple);
683
684         PG_RETURN_VOID();
685 }
686
687
688 /*
689  * Validator for SQL language functions
690  *
691  * Parse it here in order to be sure that it contains no syntax errors.
692  */
693 Datum
694 fmgr_sql_validator(PG_FUNCTION_ARGS)
695 {
696         Oid                     funcoid = PG_GETARG_OID(0);
697         HeapTuple       tuple;
698         Form_pg_proc proc;
699         List       *querytree_list;
700         bool            isnull;
701         Datum           tmp;
702         char       *prosrc;
703         ErrorContextCallback sqlerrcontext;
704         char            functyptype;
705         bool            haspolyarg;
706         int                     i;
707
708         tuple = SearchSysCache(PROCOID,
709                                                    ObjectIdGetDatum(funcoid),
710                                                    0, 0, 0);
711         if (!HeapTupleIsValid(tuple))
712                 elog(ERROR, "cache lookup failed for function %u", funcoid);
713         proc = (Form_pg_proc) GETSTRUCT(tuple);
714
715         functyptype = get_typtype(proc->prorettype);
716
717         /* Disallow pseudotype result */
718         /* except for RECORD, VOID, ANYARRAY, or ANYELEMENT */
719         if (functyptype == 'p' &&
720                 proc->prorettype != RECORDOID &&
721                 proc->prorettype != VOIDOID &&
722                 proc->prorettype != ANYARRAYOID &&
723                 proc->prorettype != ANYELEMENTOID)
724                 ereport(ERROR,
725                                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
726                                  errmsg("SQL functions cannot return type %s",
727                                                 format_type_be(proc->prorettype))));
728
729         /* Disallow pseudotypes in arguments */
730         /* except for ANYARRAY or ANYELEMENT */
731         haspolyarg = false;
732         for (i = 0; i < proc->pronargs; i++)
733         {
734                 if (get_typtype(proc->proargtypes[i]) == 'p')
735                 {
736                         if (proc->proargtypes[i] == ANYARRAYOID ||
737                                 proc->proargtypes[i] == ANYELEMENTOID)
738                                 haspolyarg = true;
739                         else
740                                 ereport(ERROR,
741                                                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
742                                  errmsg("SQL functions cannot have arguments of type %s",
743                                                 format_type_be(proc->proargtypes[i]))));
744                 }
745         }
746
747         /* Postpone body checks if !check_function_bodies */
748         if (check_function_bodies)
749         {
750                 tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosrc, &isnull);
751                 if (isnull)
752                         elog(ERROR, "null prosrc");
753
754                 prosrc = DatumGetCString(DirectFunctionCall1(textout, tmp));
755
756                 /*
757                  * Setup error traceback support for ereport().
758                  */
759                 sqlerrcontext.callback = sql_function_parse_error_callback;
760                 sqlerrcontext.arg = tuple;
761                 sqlerrcontext.previous = error_context_stack;
762                 error_context_stack = &sqlerrcontext;
763
764                 /*
765                  * We can't do full prechecking of the function definition if
766                  * there are any polymorphic input types, because actual datatypes
767                  * of expression results will be unresolvable.  The check will be
768                  * done at runtime instead.
769                  *
770                  * We can run the text through the raw parser though; this will at
771                  * least catch silly syntactic errors.
772                  */
773                 if (!haspolyarg)
774                 {
775                         querytree_list = pg_parse_and_rewrite(prosrc,
776                                                                                                   proc->proargtypes,
777                                                                                                   proc->pronargs);
778                         (void) check_sql_fn_retval(proc->prorettype, functyptype,
779                                                                            querytree_list);
780                 }
781                 else
782                         querytree_list = pg_parse_query(prosrc);
783
784                 error_context_stack = sqlerrcontext.previous;
785         }
786
787         ReleaseSysCache(tuple);
788
789         PG_RETURN_VOID();
790 }
791
792 /*
793  * Error context callback for handling errors in SQL function definitions
794  */
795 static void
796 sql_function_parse_error_callback(void *arg)
797 {
798         HeapTuple       tuple = (HeapTuple) arg;
799         Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(tuple);
800         bool            isnull;
801         Datum           tmp;
802         char       *prosrc;
803
804         /* See if it's a syntax error; if so, transpose to CREATE FUNCTION */
805         tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosrc, &isnull);
806         if (isnull)
807                 elog(ERROR, "null prosrc");
808         prosrc = DatumGetCString(DirectFunctionCall1(textout, tmp));
809
810         if (!function_parse_error_transpose(prosrc))
811         {
812                 /* If it's not a syntax error, push info onto context stack */
813                 errcontext("SQL function \"%s\"", NameStr(proc->proname));
814         }
815
816         pfree(prosrc);
817 }
818
819 /*
820  * Adjust a syntax error occurring inside the function body of a CREATE
821  * FUNCTION command.  This can be used by any function validator, not only
822  * for SQL-language functions.  It is assumed that the syntax error position
823  * is initially relative to the function body string (as passed in).  If
824  * possible, we adjust the position to reference the original CREATE command;
825  * if we can't manage that, we set up an "internal query" syntax error instead.
826  *
827  * Returns true if a syntax error was processed, false if not.
828  */
829 bool
830 function_parse_error_transpose(const char *prosrc)
831 {
832         int                     origerrposition;
833         int                     newerrposition;
834         const char *queryText;
835
836         /*
837          * Nothing to do unless we are dealing with a syntax error that has a
838          * cursor position.
839          *
840          * Some PLs may prefer to report the error position as an internal error
841          * to begin with, so check that too.
842          */
843         origerrposition = geterrposition();
844         if (origerrposition <= 0)
845         {
846                 origerrposition = getinternalerrposition();
847                 if (origerrposition <= 0)
848                         return false;
849         }
850
851         /* We can get the original query text from the active portal (hack...) */
852         Assert(ActivePortal && ActivePortal->status == PORTAL_ACTIVE);
853         queryText = ActivePortal->sourceText;
854
855         /* Try to locate the prosrc in the original text */
856         newerrposition = match_prosrc_to_query(prosrc, queryText, origerrposition);
857
858         if (newerrposition > 0)
859         {
860                 /* Successful, so fix error position to reference original query */
861                 errposition(newerrposition);
862                 /* Get rid of any report of the error as an "internal query" */
863                 internalerrposition(0);
864                 internalerrquery(NULL);
865         }
866         else
867         {
868                 /*
869                  * If unsuccessful, convert the position to an internal position
870                  * marker and give the function text as the internal query.
871                  */
872                 errposition(0);
873                 internalerrposition(origerrposition);
874                 internalerrquery(prosrc);
875         }
876
877         return true;
878 }
879
880 /*
881  * Try to locate the string literal containing the function body in the
882  * given text of the CREATE FUNCTION command.  If successful, return the
883  * character (not byte) index within the command corresponding to the
884  * given character index within the literal.  If not successful, return 0.
885  */
886 static int
887 match_prosrc_to_query(const char *prosrc, const char *queryText,
888                                           int cursorpos)
889 {
890         /*
891          * Rather than fully parsing the CREATE FUNCTION command, we just scan
892          * the command looking for $prosrc$ or 'prosrc'.  This could be fooled
893          * (though not in any very probable scenarios), so fail if we find
894          * more than one match.
895          */
896         int                     prosrclen = strlen(prosrc);
897         int                     querylen = strlen(queryText);
898         int                     matchpos = 0;
899         int                     curpos;
900         int                     newcursorpos;
901
902         for (curpos = 0; curpos < querylen - prosrclen; curpos++)
903         {
904                 if (queryText[curpos] == '$' &&
905                         strncmp(prosrc, &queryText[curpos + 1], prosrclen) == 0 &&
906                         queryText[curpos + 1 + prosrclen] == '$')
907                 {
908                         /*
909                          * Found a $foo$ match.  Since there are no embedded quoting
910                          * characters in a dollar-quoted literal, we don't have to do
911                          * any fancy arithmetic; just offset by the starting position.
912                          */
913                         if (matchpos)
914                                 return 0;               /* multiple matches, fail */
915                         matchpos = pg_mbstrlen_with_len(queryText, curpos + 1)
916                                 + cursorpos;
917                 }
918                 else if (queryText[curpos] == '\'' &&
919                                  match_prosrc_to_literal(prosrc, &queryText[curpos + 1],
920                                                                                  cursorpos, &newcursorpos))
921                 {
922                         /*
923                          * Found a 'foo' match.  match_prosrc_to_literal() has
924                          * adjusted for any quotes or backslashes embedded in the
925                          * literal.
926                          */
927                         if (matchpos)
928                                 return 0;               /* multiple matches, fail */
929                         matchpos = pg_mbstrlen_with_len(queryText, curpos + 1)
930                                 + newcursorpos;
931                 }
932         }
933
934         return matchpos;
935 }
936
937 /*
938  * Try to match the given source text to a single-quoted literal.
939  * If successful, adjust newcursorpos to correspond to the character
940  * (not byte) index corresponding to cursorpos in the source text.
941  *
942  * At entry, literal points just past a ' character.  We must check for the
943  * trailing quote.
944  */
945 static bool
946 match_prosrc_to_literal(const char *prosrc, const char *literal,
947                                                 int cursorpos, int *newcursorpos)
948 {
949         int                     newcp = cursorpos;
950         int                     chlen;
951
952         /*
953          * This implementation handles backslashes and doubled quotes in the
954          * string literal.      It does not handle the SQL syntax for literals
955          * continued across line boundaries.
956          *
957          * We do the comparison a character at a time, not a byte at a time, so
958          * that we can do the correct cursorpos math.
959          */
960         while (*prosrc)
961         {
962                 cursorpos--;                    /* characters left before cursor */
963
964                 /*
965                  * Check for backslashes and doubled quotes in the literal; adjust
966                  * newcp when one is found before the cursor.
967                  */
968                 if (*literal == '\\')
969                 {
970                         literal++;
971                         if (cursorpos > 0)
972                                 newcp++;
973                 }
974                 else if (*literal == '\'')
975                 {
976                         if (literal[1] != '\'')
977                                 return false;
978                         literal++;
979                         if (cursorpos > 0)
980                                 newcp++;
981                 }
982                 chlen = pg_mblen(prosrc);
983                 if (strncmp(prosrc, literal, chlen) != 0)
984                         return false;
985                 prosrc += chlen;
986                 literal += chlen;
987         }
988
989         *newcursorpos = newcp;
990
991         if (*literal == '\'' && literal[1] != '\'')
992                 return true;
993         return false;
994 }