1 /*-------------------------------------------------------------------------
2  *
3  * pg_proc.c
4  *	  routines to support manipulation of the pg_proc relation
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *	  src/backend/catalog/pg_proc.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include "access/htup_details.h"
18 #include "access/xact.h"
19 #include "catalog/dependency.h"
20 #include "catalog/indexing.h"
21 #include "catalog/objectaccess.h"
22 #include "catalog/pg_language.h"
23 #include "catalog/pg_namespace.h"
24 #include "catalog/pg_proc.h"
25 #include "catalog/pg_proc_fn.h"
26 #include "catalog/pg_transform.h"
27 #include "catalog/pg_type.h"
28 #include "commands/defrem.h"
29 #include "executor/functions.h"
30 #include "funcapi.h"
31 #include "mb/pg_wchar.h"
32 #include "miscadmin.h"
33 #include "nodes/nodeFuncs.h"
34 #include "parser/parse_type.h"
35 #include "tcop/pquery.h"
36 #include "tcop/tcopprot.h"
37 #include "utils/acl.h"
38 #include "utils/builtins.h"
39 #include "utils/lsyscache.h"
40 #include "utils/regproc.h"
41 #include "utils/rel.h"
42 #include "utils/syscache.h"
43 
44 
45 typedef struct
46 {
47 	char	   *proname;
48 	char	   *prosrc;
49 } parse_error_callback_arg;
50 
51 static void sql_function_parse_error_callback(void *arg);
52 static int match_prosrc_to_query(const char *prosrc, const char *queryText,
53 					  int cursorpos);
54 static bool match_prosrc_to_literal(const char *prosrc, const char *literal,
55 						int cursorpos, int *newcursorpos);
56 
57 
58 /* ----------------------------------------------------------------
59  *		ProcedureCreate
60  *
61  * Note: allParameterTypes, parameterModes, parameterNames, trftypes, and proconfig
62  * are either arrays of the proper types or NULL.  We declare them Datum,
63  * not "ArrayType *", to avoid importing array.h into pg_proc_fn.h.
64  * ----------------------------------------------------------------
65  */
66 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,bool isAgg,bool isWindowFunc,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)67 ProcedureCreate(const char *procedureName,
68 				Oid procNamespace,
69 				bool replace,
70 				bool returnsSet,
71 				Oid returnType,
72 				Oid proowner,
73 				Oid languageObjectId,
74 				Oid languageValidator,
75 				const char *prosrc,
76 				const char *probin,
77 				bool isAgg,
78 				bool isWindowFunc,
79 				bool security_definer,
80 				bool isLeakProof,
81 				bool isStrict,
82 				char volatility,
83 				char parallel,
84 				oidvector *parameterTypes,
85 				Datum allParameterTypes,
86 				Datum parameterModes,
87 				Datum parameterNames,
88 				List *parameterDefaults,
89 				Datum trftypes,
90 				Datum proconfig,
91 				float4 procost,
92 				float4 prorows)
93 {
94 	Oid			retval;
95 	int			parameterCount;
96 	int			allParamCount;
97 	Oid		   *allParams;
98 	char	   *paramModes = NULL;
99 	bool		genericInParam = false;
100 	bool		genericOutParam = false;
101 	bool		anyrangeInParam = false;
102 	bool		anyrangeOutParam = false;
103 	bool		internalInParam = false;
104 	bool		internalOutParam = false;
105 	Oid			variadicType = InvalidOid;
106 	Acl		   *proacl = NULL;
107 	Relation	rel;
108 	HeapTuple	tup;
109 	HeapTuple	oldtup;
110 	bool		nulls[Natts_pg_proc];
111 	Datum		values[Natts_pg_proc];
112 	bool		replaces[Natts_pg_proc];
113 	Oid			relid;
114 	NameData	procname;
115 	TupleDesc	tupDesc;
116 	bool		is_update;
117 	ObjectAddress myself,
118 				referenced;
119 	int			i;
120 	Oid			trfid;
121 
122 	/*
123 	 * sanity checks
124 	 */
125 	Assert(PointerIsValid(prosrc));
126 
127 	parameterCount = parameterTypes->dim1;
128 	if (parameterCount < 0 || parameterCount > FUNC_MAX_ARGS)
129 		ereport(ERROR,
130 				(errcode(ERRCODE_TOO_MANY_ARGUMENTS),
131 				 errmsg_plural("functions cannot have more than %d argument",
132 							   "functions cannot have more than %d arguments",
133 							   FUNC_MAX_ARGS,
134 							   FUNC_MAX_ARGS)));
135 	/* note: the above is correct, we do NOT count output arguments */
136 
137 	/* Deconstruct array inputs */
138 	if (allParameterTypes != PointerGetDatum(NULL))
139 	{
140 		/*
141 		 * We expect the array to be a 1-D OID array; verify that. We don't
142 		 * need to use deconstruct_array() since the array data is just going
143 		 * to look like a C array of OID values.
144 		 */
145 		ArrayType  *allParamArray = (ArrayType *) DatumGetPointer(allParameterTypes);
146 
147 		allParamCount = ARR_DIMS(allParamArray)[0];
148 		if (ARR_NDIM(allParamArray) != 1 ||
149 			allParamCount <= 0 ||
150 			ARR_HASNULL(allParamArray) ||
151 			ARR_ELEMTYPE(allParamArray) != OIDOID)
152 			elog(ERROR, "allParameterTypes is not a 1-D Oid array");
153 		allParams = (Oid *) ARR_DATA_PTR(allParamArray);
154 		Assert(allParamCount >= parameterCount);
155 		/* we assume caller got the contents right */
156 	}
157 	else
158 	{
159 		allParamCount = parameterCount;
160 		allParams = parameterTypes->values;
161 	}
162 
163 	if (parameterModes != PointerGetDatum(NULL))
164 	{
165 		/*
166 		 * We expect the array to be a 1-D CHAR array; verify that. We don't
167 		 * need to use deconstruct_array() since the array data is just going
168 		 * to look like a C array of char values.
169 		 */
170 		ArrayType  *modesArray = (ArrayType *) DatumGetPointer(parameterModes);
171 
172 		if (ARR_NDIM(modesArray) != 1 ||
173 			ARR_DIMS(modesArray)[0] != allParamCount ||
174 			ARR_HASNULL(modesArray) ||
175 			ARR_ELEMTYPE(modesArray) != CHAROID)
176 			elog(ERROR, "parameterModes is not a 1-D char array");
177 		paramModes = (char *) ARR_DATA_PTR(modesArray);
178 	}
179 
180 	/*
181 	 * Detect whether we have polymorphic or INTERNAL arguments.  The first
182 	 * loop checks input arguments, the second output arguments.
183 	 */
184 	for (i = 0; i < parameterCount; i++)
185 	{
186 		switch (parameterTypes->values[i])
187 		{
188 			case ANYARRAYOID:
189 			case ANYELEMENTOID:
190 			case ANYNONARRAYOID:
191 			case ANYENUMOID:
192 				genericInParam = true;
193 				break;
194 			case ANYRANGEOID:
195 				genericInParam = true;
196 				anyrangeInParam = true;
197 				break;
198 			case INTERNALOID:
199 				internalInParam = true;
200 				break;
201 		}
202 	}
203 
204 	if (allParameterTypes != PointerGetDatum(NULL))
205 	{
206 		for (i = 0; i < allParamCount; i++)
207 		{
208 			if (paramModes == NULL ||
209 				paramModes[i] == PROARGMODE_IN ||
210 				paramModes[i] == PROARGMODE_VARIADIC)
211 				continue;		/* ignore input-only params */
212 
213 			switch (allParams[i])
214 			{
215 				case ANYARRAYOID:
216 				case ANYELEMENTOID:
217 				case ANYNONARRAYOID:
218 				case ANYENUMOID:
219 					genericOutParam = true;
220 					break;
221 				case ANYRANGEOID:
222 					genericOutParam = true;
223 					anyrangeOutParam = true;
224 					break;
225 				case INTERNALOID:
226 					internalOutParam = true;
227 					break;
228 			}
229 		}
230 	}
231 
232 	/*
233 	 * Do not allow polymorphic return type unless at least one input argument
234 	 * is polymorphic.  ANYRANGE return type is even stricter: must have an
235 	 * ANYRANGE input (since we can't deduce the specific range type from
236 	 * ANYELEMENT).  Also, do not allow return type INTERNAL unless at least
237 	 * one input argument is INTERNAL.
238 	 */
239 	if ((IsPolymorphicType(returnType) || genericOutParam)
240 		&& !genericInParam)
241 		ereport(ERROR,
242 				(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
243 				 errmsg("cannot determine result data type"),
244 				 errdetail("A function returning a polymorphic type must have at least one polymorphic argument.")));
245 
246 	if ((returnType == ANYRANGEOID || anyrangeOutParam) &&
247 		!anyrangeInParam)
248 		ereport(ERROR,
249 				(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
250 				 errmsg("cannot determine result data type"),
251 				 errdetail("A function returning \"anyrange\" must have at least one \"anyrange\" argument.")));
252 
253 	if ((returnType == INTERNALOID || internalOutParam) && !internalInParam)
254 		ereport(ERROR,
255 				(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
256 				 errmsg("unsafe use of pseudo-type \"internal\""),
257 				 errdetail("A function returning \"internal\" must have at least one \"internal\" argument.")));
258 
259 	/*
260 	 * don't allow functions of complex types that have the same name as
261 	 * existing attributes of the type
262 	 */
263 	if (parameterCount == 1 &&
264 		OidIsValid(parameterTypes->values[0]) &&
265 		(relid = typeidTypeRelid(parameterTypes->values[0])) != InvalidOid &&
266 		get_attnum(relid, procedureName) != InvalidAttrNumber)
267 		ereport(ERROR,
268 				(errcode(ERRCODE_DUPLICATE_COLUMN),
269 				 errmsg("\"%s\" is already an attribute of type %s",
270 						procedureName,
271 						format_type_be(parameterTypes->values[0]))));
272 
273 	if (paramModes != NULL)
274 	{
275 		/*
276 		 * Only the last input parameter can be variadic; if it is, save its
277 		 * element type.  Errors here are just elog since caller should have
278 		 * checked this already.
279 		 */
280 		for (i = 0; i < allParamCount; i++)
281 		{
282 			switch (paramModes[i])
283 			{
284 				case PROARGMODE_IN:
285 				case PROARGMODE_INOUT:
286 					if (OidIsValid(variadicType))
287 						elog(ERROR, "variadic parameter must be last");
288 					break;
289 				case PROARGMODE_OUT:
290 				case PROARGMODE_TABLE:
291 					/* okay */
292 					break;
293 				case PROARGMODE_VARIADIC:
294 					if (OidIsValid(variadicType))
295 						elog(ERROR, "variadic parameter must be last");
296 					switch (allParams[i])
297 					{
298 						case ANYOID:
299 							variadicType = ANYOID;
300 							break;
301 						case ANYARRAYOID:
302 							variadicType = ANYELEMENTOID;
303 							break;
304 						default:
305 							variadicType = get_element_type(allParams[i]);
306 							if (!OidIsValid(variadicType))
307 								elog(ERROR, "variadic parameter is not an array");
308 							break;
309 					}
310 					break;
311 				default:
312 					elog(ERROR, "invalid parameter mode '%c'", paramModes[i]);
313 					break;
314 			}
315 		}
316 	}
317 
318 	/*
319 	 * All seems OK; prepare the data to be inserted into pg_proc.
320 	 */
321 
322 	for (i = 0; i < Natts_pg_proc; ++i)
323 	{
324 		nulls[i] = false;
325 		values[i] = (Datum) 0;
326 		replaces[i] = true;
327 	}
328 
329 	namestrcpy(&procname, procedureName);
330 	values[Anum_pg_proc_proname - 1] = NameGetDatum(&procname);
331 	values[Anum_pg_proc_pronamespace - 1] = ObjectIdGetDatum(procNamespace);
332 	values[Anum_pg_proc_proowner - 1] = ObjectIdGetDatum(proowner);
333 	values[Anum_pg_proc_prolang - 1] = ObjectIdGetDatum(languageObjectId);
334 	values[Anum_pg_proc_procost - 1] = Float4GetDatum(procost);
335 	values[Anum_pg_proc_prorows - 1] = Float4GetDatum(prorows);
336 	values[Anum_pg_proc_provariadic - 1] = ObjectIdGetDatum(variadicType);
337 	values[Anum_pg_proc_protransform - 1] = ObjectIdGetDatum(InvalidOid);
338 	values[Anum_pg_proc_proisagg - 1] = BoolGetDatum(isAgg);
339 	values[Anum_pg_proc_proiswindow - 1] = BoolGetDatum(isWindowFunc);
340 	values[Anum_pg_proc_prosecdef - 1] = BoolGetDatum(security_definer);
341 	values[Anum_pg_proc_proleakproof - 1] = BoolGetDatum(isLeakProof);
342 	values[Anum_pg_proc_proisstrict - 1] = BoolGetDatum(isStrict);
343 	values[Anum_pg_proc_proretset - 1] = BoolGetDatum(returnsSet);
344 	values[Anum_pg_proc_provolatile - 1] = CharGetDatum(volatility);
345 	values[Anum_pg_proc_proparallel - 1] = CharGetDatum(parallel);
346 	values[Anum_pg_proc_pronargs - 1] = UInt16GetDatum(parameterCount);
347 	values[Anum_pg_proc_pronargdefaults - 1] = UInt16GetDatum(list_length(parameterDefaults));
348 	values[Anum_pg_proc_prorettype - 1] = ObjectIdGetDatum(returnType);
349 	values[Anum_pg_proc_proargtypes - 1] = PointerGetDatum(parameterTypes);
350 	if (allParameterTypes != PointerGetDatum(NULL))
351 		values[Anum_pg_proc_proallargtypes - 1] = allParameterTypes;
352 	else
353 		nulls[Anum_pg_proc_proallargtypes - 1] = true;
354 	if (parameterModes != PointerGetDatum(NULL))
355 		values[Anum_pg_proc_proargmodes - 1] = parameterModes;
356 	else
357 		nulls[Anum_pg_proc_proargmodes - 1] = true;
358 	if (parameterNames != PointerGetDatum(NULL))
359 		values[Anum_pg_proc_proargnames - 1] = parameterNames;
360 	else
361 		nulls[Anum_pg_proc_proargnames - 1] = true;
362 	if (parameterDefaults != NIL)
363 		values[Anum_pg_proc_proargdefaults - 1] = CStringGetTextDatum(nodeToString(parameterDefaults));
364 	else
365 		nulls[Anum_pg_proc_proargdefaults - 1] = true;
366 	if (trftypes != PointerGetDatum(NULL))
367 		values[Anum_pg_proc_protrftypes - 1] = trftypes;
368 	else
369 		nulls[Anum_pg_proc_protrftypes - 1] = true;
370 	values[Anum_pg_proc_prosrc - 1] = CStringGetTextDatum(prosrc);
371 	if (probin)
372 		values[Anum_pg_proc_probin - 1] = CStringGetTextDatum(probin);
373 	else
374 		nulls[Anum_pg_proc_probin - 1] = true;
375 	if (proconfig != PointerGetDatum(NULL))
376 		values[Anum_pg_proc_proconfig - 1] = proconfig;
377 	else
378 		nulls[Anum_pg_proc_proconfig - 1] = true;
379 	/* proacl will be determined later */
380 
381 	rel = heap_open(ProcedureRelationId, RowExclusiveLock);
382 	tupDesc = RelationGetDescr(rel);
383 
384 	/* Check for pre-existing definition */
385 	oldtup = SearchSysCache3(PROCNAMEARGSNSP,
386 							 PointerGetDatum(procedureName),
387 							 PointerGetDatum(parameterTypes),
388 							 ObjectIdGetDatum(procNamespace));
389 
390 	if (HeapTupleIsValid(oldtup))
391 	{
392 		/* There is one; okay to replace it? */
393 		Form_pg_proc oldproc = (Form_pg_proc) GETSTRUCT(oldtup);
394 		Datum		proargnames;
395 		bool		isnull;
396 
397 		if (!replace)
398 			ereport(ERROR,
399 					(errcode(ERRCODE_DUPLICATE_FUNCTION),
400 					 errmsg("function \"%s\" already exists with same argument types",
401 							procedureName)));
402 		if (!pg_proc_ownercheck(HeapTupleGetOid(oldtup), proowner))
403 			aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PROC,
404 						   procedureName);
405 
406 		/*
407 		 * Not okay to change the return type of the existing proc, since
408 		 * existing rules, views, etc may depend on the return type.
409 		 */
410 		if (returnType != oldproc->prorettype ||
411 			returnsSet != oldproc->proretset)
412 			ereport(ERROR,
413 					(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
414 					 errmsg("cannot change return type of existing function"),
415 					 errhint("Use DROP FUNCTION %s first.",
416 							 format_procedure(HeapTupleGetOid(oldtup)))));
417 
418 		/*
419 		 * If it returns RECORD, check for possible change of record type
420 		 * implied by OUT parameters
421 		 */
422 		if (returnType == RECORDOID)
423 		{
424 			TupleDesc	olddesc;
425 			TupleDesc	newdesc;
426 
427 			olddesc = build_function_result_tupdesc_t(oldtup);
428 			newdesc = build_function_result_tupdesc_d(allParameterTypes,
429 													  parameterModes,
430 													  parameterNames);
431 			if (olddesc == NULL && newdesc == NULL)
432 				 /* ok, both are runtime-defined RECORDs */ ;
433 			else if (olddesc == NULL || newdesc == NULL ||
434 					 !equalTupleDescs(olddesc, newdesc))
435 				ereport(ERROR,
436 						(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
437 						 errmsg("cannot change return type of existing function"),
438 						 errdetail("Row type defined by OUT parameters is different."),
439 						 errhint("Use DROP FUNCTION %s first.",
440 								 format_procedure(HeapTupleGetOid(oldtup)))));
441 		}
442 
443 		/*
444 		 * If there were any named input parameters, check to make sure the
445 		 * names have not been changed, as this could break existing calls. We
446 		 * allow adding names to formerly unnamed parameters, though.
447 		 */
448 		proargnames = SysCacheGetAttr(PROCNAMEARGSNSP, oldtup,
449 									  Anum_pg_proc_proargnames,
450 									  &isnull);
451 		if (!isnull)
452 		{
453 			Datum		proargmodes;
454 			char	  **old_arg_names;
455 			char	  **new_arg_names;
456 			int			n_old_arg_names;
457 			int			n_new_arg_names;
458 			int			j;
459 
460 			proargmodes = SysCacheGetAttr(PROCNAMEARGSNSP, oldtup,
461 										  Anum_pg_proc_proargmodes,
462 										  &isnull);
463 			if (isnull)
464 				proargmodes = PointerGetDatum(NULL);	/* just to be sure */
465 
466 			n_old_arg_names = get_func_input_arg_names(proargnames,
467 													   proargmodes,
468 													   &old_arg_names);
469 			n_new_arg_names = get_func_input_arg_names(parameterNames,
470 													   parameterModes,
471 													   &new_arg_names);
472 			for (j = 0; j < n_old_arg_names; j++)
473 			{
474 				if (old_arg_names[j] == NULL)
475 					continue;
476 				if (j >= n_new_arg_names || new_arg_names[j] == NULL ||
477 					strcmp(old_arg_names[j], new_arg_names[j]) != 0)
478 					ereport(ERROR,
479 							(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
480 							 errmsg("cannot change name of input parameter \"%s\"",
481 									old_arg_names[j]),
482 							 errhint("Use DROP FUNCTION %s first.",
483 									 format_procedure(HeapTupleGetOid(oldtup)))));
484 			}
485 		}
486 
487 		/*
488 		 * If there are existing defaults, check compatibility: redefinition
489 		 * must not remove any defaults nor change their types.  (Removing a
490 		 * default might cause a function to fail to satisfy an existing call.
491 		 * Changing type would only be possible if the associated parameter is
492 		 * polymorphic, and in such cases a change of default type might alter
493 		 * the resolved output type of existing calls.)
494 		 */
495 		if (oldproc->pronargdefaults != 0)
496 		{
497 			Datum		proargdefaults;
498 			List	   *oldDefaults;
499 			ListCell   *oldlc;
500 			ListCell   *newlc;
501 
502 			if (list_length(parameterDefaults) < oldproc->pronargdefaults)
503 				ereport(ERROR,
504 						(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
505 						 errmsg("cannot remove parameter defaults from existing function"),
506 						 errhint("Use DROP FUNCTION %s first.",
507 								 format_procedure(HeapTupleGetOid(oldtup)))));
508 
509 			proargdefaults = SysCacheGetAttr(PROCNAMEARGSNSP, oldtup,
510 											 Anum_pg_proc_proargdefaults,
511 											 &isnull);
512 			Assert(!isnull);
513 			oldDefaults = castNode(List, stringToNode(TextDatumGetCString(proargdefaults)));
514 			Assert(list_length(oldDefaults) == oldproc->pronargdefaults);
515 
516 			/* new list can have more defaults than old, advance over 'em */
517 			newlc = list_head(parameterDefaults);
518 			for (i = list_length(parameterDefaults) - oldproc->pronargdefaults;
519 				 i > 0;
520 				 i--)
521 				newlc = lnext(newlc);
522 
523 			foreach(oldlc, oldDefaults)
524 			{
525 				Node	   *oldDef = (Node *) lfirst(oldlc);
526 				Node	   *newDef = (Node *) lfirst(newlc);
527 
528 				if (exprType(oldDef) != exprType(newDef))
529 					ereport(ERROR,
530 							(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
531 							 errmsg("cannot change data type of existing parameter default value"),
532 							 errhint("Use DROP FUNCTION %s first.",
533 									 format_procedure(HeapTupleGetOid(oldtup)))));
534 				newlc = lnext(newlc);
535 			}
536 		}
537 
538 		/* Can't change aggregate or window-function status, either */
539 		if (oldproc->proisagg != isAgg)
540 		{
541 			if (oldproc->proisagg)
542 				ereport(ERROR,
543 						(errcode(ERRCODE_WRONG_OBJECT_TYPE),
544 						 errmsg("function \"%s\" is an aggregate function",
545 								procedureName)));
546 			else
547 				ereport(ERROR,
548 						(errcode(ERRCODE_WRONG_OBJECT_TYPE),
549 						 errmsg("function \"%s\" is not an aggregate function",
550 								procedureName)));
551 		}
552 		if (oldproc->proiswindow != isWindowFunc)
553 		{
554 			if (oldproc->proiswindow)
555 				ereport(ERROR,
556 						(errcode(ERRCODE_WRONG_OBJECT_TYPE),
557 						 errmsg("function \"%s\" is a window function",
558 								procedureName)));
559 			else
560 				ereport(ERROR,
561 						(errcode(ERRCODE_WRONG_OBJECT_TYPE),
562 						 errmsg("function \"%s\" is not a window function",
563 								procedureName)));
564 		}
565 
566 		/*
567 		 * Do not change existing ownership or permissions, either.  Note
568 		 * dependency-update code below has to agree with this decision.
569 		 */
570 		replaces[Anum_pg_proc_proowner - 1] = false;
571 		replaces[Anum_pg_proc_proacl - 1] = false;
572 
573 		/* Okay, do it... */
574 		tup = heap_modify_tuple(oldtup, tupDesc, values, nulls, replaces);
575 		CatalogTupleUpdate(rel, &tup->t_self, tup);
576 
577 		ReleaseSysCache(oldtup);
578 		is_update = true;
579 	}
580 	else
581 	{
582 		/* Creating a new procedure */
583 
584 		/* First, get default permissions and set up proacl */
585 		proacl = get_user_default_acl(ACL_OBJECT_FUNCTION, proowner,
586 									  procNamespace);
587 		if (proacl != NULL)
588 			values[Anum_pg_proc_proacl - 1] = PointerGetDatum(proacl);
589 		else
590 			nulls[Anum_pg_proc_proacl - 1] = true;
591 
592 		tup = heap_form_tuple(tupDesc, values, nulls);
593 		CatalogTupleInsert(rel, tup);
594 		is_update = false;
595 	}
596 
597 
598 	retval = HeapTupleGetOid(tup);
599 
600 	/*
601 	 * Create dependencies for the new function.  If we are updating an
602 	 * existing function, first delete any existing pg_depend entries.
603 	 * (However, since we are not changing ownership or permissions, the
604 	 * shared dependencies do *not* need to change, and we leave them alone.)
605 	 */
606 	if (is_update)
607 		deleteDependencyRecordsFor(ProcedureRelationId, retval, true);
608 
609 	myself.classId = ProcedureRelationId;
610 	myself.objectId = retval;
611 	myself.objectSubId = 0;
612 
613 	/* dependency on namespace */
614 	referenced.classId = NamespaceRelationId;
615 	referenced.objectId = procNamespace;
616 	referenced.objectSubId = 0;
617 	recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
618 
619 	/* dependency on implementation language */
620 	referenced.classId = LanguageRelationId;
621 	referenced.objectId = languageObjectId;
622 	referenced.objectSubId = 0;
623 	recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
624 
625 	/* dependency on return type */
626 	referenced.classId = TypeRelationId;
627 	referenced.objectId = returnType;
628 	referenced.objectSubId = 0;
629 	recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
630 
631 	/* dependency on transform used by return type, if any */
632 	if ((trfid = get_transform_oid(returnType, languageObjectId, true)))
633 	{
634 		referenced.classId = TransformRelationId;
635 		referenced.objectId = trfid;
636 		referenced.objectSubId = 0;
637 		recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
638 	}
639 
640 	/* dependency on parameter types */
641 	for (i = 0; i < allParamCount; i++)
642 	{
643 		referenced.classId = TypeRelationId;
644 		referenced.objectId = allParams[i];
645 		referenced.objectSubId = 0;
646 		recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
647 
648 		/* dependency on transform used by parameter type, if any */
649 		if ((trfid = get_transform_oid(allParams[i], languageObjectId, true)))
650 		{
651 			referenced.classId = TransformRelationId;
652 			referenced.objectId = trfid;
653 			referenced.objectSubId = 0;
654 			recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
655 		}
656 	}
657 
658 	/* dependency on parameter default expressions */
659 	if (parameterDefaults)
660 		recordDependencyOnExpr(&myself, (Node *) parameterDefaults,
661 							   NIL, DEPENDENCY_NORMAL);
662 
663 	/* dependency on owner */
664 	if (!is_update)
665 		recordDependencyOnOwner(ProcedureRelationId, retval, proowner);
666 
667 	/* dependency on any roles mentioned in ACL */
668 	if (!is_update)
669 		recordDependencyOnNewAcl(ProcedureRelationId, retval, 0,
670 								 proowner, proacl);
671 
672 	/* dependency on extension */
673 	recordDependencyOnCurrentExtension(&myself, is_update);
674 
675 	heap_freetuple(tup);
676 
677 	/* Post creation hook for new function */
678 	InvokeObjectPostCreateHook(ProcedureRelationId, retval, 0);
679 
680 	heap_close(rel, RowExclusiveLock);
681 
682 	/* Verify function body */
683 	if (OidIsValid(languageValidator))
684 	{
685 		ArrayType  *set_items = NULL;
686 		int			save_nestlevel = 0;
687 
688 		/* Advance command counter so new tuple can be seen by validator */
689 		CommandCounterIncrement();
690 
691 		/*
692 		 * Set per-function configuration parameters so that the validation is
693 		 * done with the environment the function expects.  However, if
694 		 * check_function_bodies is off, we don't do this, because that would
695 		 * create dump ordering hazards that pg_dump doesn't know how to deal
696 		 * with.  (For example, a SET clause might refer to a not-yet-created
697 		 * text search configuration.)	This means that the validator
698 		 * shouldn't complain about anything that might depend on a GUC
699 		 * parameter when check_function_bodies is off.
700 		 */
701 		if (check_function_bodies)
702 		{
703 			set_items = (ArrayType *) DatumGetPointer(proconfig);
704 			if (set_items)		/* Need a new GUC nesting level */
705 			{
706 				save_nestlevel = NewGUCNestLevel();
707 				ProcessGUCArray(set_items,
708 								(superuser() ? PGC_SUSET : PGC_USERSET),
709 								PGC_S_SESSION,
710 								GUC_ACTION_SAVE);
711 			}
712 		}
713 
714 		OidFunctionCall1(languageValidator, ObjectIdGetDatum(retval));
715 
716 		if (set_items)
717 			AtEOXact_GUC(true, save_nestlevel);
718 	}
719 
720 	return myself;
721 }
722 
723 
724 
725 /*
726  * Validator for internal functions
727  *
728  * Check that the given internal function name (the "prosrc" value) is
729  * a known builtin function.
730  */
731 Datum
fmgr_internal_validator(PG_FUNCTION_ARGS)732 fmgr_internal_validator(PG_FUNCTION_ARGS)
733 {
734 	Oid			funcoid = PG_GETARG_OID(0);
735 	HeapTuple	tuple;
736 	bool		isnull;
737 	Datum		tmp;
738 	char	   *prosrc;
739 
740 	if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid, funcoid))
741 		PG_RETURN_VOID();
742 
743 	/*
744 	 * We do not honor check_function_bodies since it's unlikely the function
745 	 * name will be found later if it isn't there now.
746 	 */
747 
748 	tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
749 	if (!HeapTupleIsValid(tuple))
750 		elog(ERROR, "cache lookup failed for function %u", funcoid);
751 
752 	tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosrc, &isnull);
753 	if (isnull)
754 		elog(ERROR, "null prosrc");
755 	prosrc = TextDatumGetCString(tmp);
756 
757 	if (fmgr_internal_function(prosrc) == InvalidOid)
758 		ereport(ERROR,
759 				(errcode(ERRCODE_UNDEFINED_FUNCTION),
760 				 errmsg("there is no built-in function named \"%s\"",
761 						prosrc)));
762 
763 	ReleaseSysCache(tuple);
764 
765 	PG_RETURN_VOID();
766 }
767 
768 
769 
770 /*
771  * Validator for C language functions
772  *
773  * Make sure that the library file exists, is loadable, and contains
774  * the specified link symbol. Also check for a valid function
775  * information record.
776  */
777 Datum
fmgr_c_validator(PG_FUNCTION_ARGS)778 fmgr_c_validator(PG_FUNCTION_ARGS)
779 {
780 	Oid			funcoid = PG_GETARG_OID(0);
781 	void	   *libraryhandle;
782 	HeapTuple	tuple;
783 	bool		isnull;
784 	Datum		tmp;
785 	char	   *prosrc;
786 	char	   *probin;
787 
788 	if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid, funcoid))
789 		PG_RETURN_VOID();
790 
791 	/*
792 	 * It'd be most consistent to skip the check if !check_function_bodies,
793 	 * but the purpose of that switch is to be helpful for pg_dump loading,
794 	 * and for pg_dump loading it's much better if we *do* check.
795 	 */
796 
797 	tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
798 	if (!HeapTupleIsValid(tuple))
799 		elog(ERROR, "cache lookup failed for function %u", funcoid);
800 
801 	tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosrc, &isnull);
802 	if (isnull)
803 		elog(ERROR, "null prosrc for C function %u", funcoid);
804 	prosrc = TextDatumGetCString(tmp);
805 
806 	tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_probin, &isnull);
807 	if (isnull)
808 		elog(ERROR, "null probin for C function %u", funcoid);
809 	probin = TextDatumGetCString(tmp);
810 
811 	(void) load_external_function(probin, prosrc, true, &libraryhandle);
812 	(void) fetch_finfo_record(libraryhandle, prosrc);
813 
814 	ReleaseSysCache(tuple);
815 
816 	PG_RETURN_VOID();
817 }
818 
819 
820 /*
821  * Validator for SQL language functions
822  *
823  * Parse it here in order to be sure that it contains no syntax errors.
824  */
825 Datum
fmgr_sql_validator(PG_FUNCTION_ARGS)826 fmgr_sql_validator(PG_FUNCTION_ARGS)
827 {
828 	Oid			funcoid = PG_GETARG_OID(0);
829 	HeapTuple	tuple;
830 	Form_pg_proc proc;
831 	List	   *raw_parsetree_list;
832 	List	   *querytree_list;
833 	ListCell   *lc;
834 	bool		isnull;
835 	Datum		tmp;
836 	char	   *prosrc;
837 	parse_error_callback_arg callback_arg;
838 	ErrorContextCallback sqlerrcontext;
839 	bool		haspolyarg;
840 	int			i;
841 
842 	if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid, funcoid))
843 		PG_RETURN_VOID();
844 
845 	tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
846 	if (!HeapTupleIsValid(tuple))
847 		elog(ERROR, "cache lookup failed for function %u", funcoid);
848 	proc = (Form_pg_proc) GETSTRUCT(tuple);
849 
850 	/* Disallow pseudotype result */
851 	/* except for RECORD, VOID, or polymorphic */
852 	if (get_typtype(proc->prorettype) == TYPTYPE_PSEUDO &&
853 		proc->prorettype != RECORDOID &&
854 		proc->prorettype != VOIDOID &&
855 		!IsPolymorphicType(proc->prorettype))
856 		ereport(ERROR,
857 				(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
858 				 errmsg("SQL functions cannot return type %s",
859 						format_type_be(proc->prorettype))));
860 
861 	/* Disallow pseudotypes in arguments */
862 	/* except for polymorphic */
863 	haspolyarg = false;
864 	for (i = 0; i < proc->pronargs; i++)
865 	{
866 		if (get_typtype(proc->proargtypes.values[i]) == TYPTYPE_PSEUDO)
867 		{
868 			if (IsPolymorphicType(proc->proargtypes.values[i]))
869 				haspolyarg = true;
870 			else
871 				ereport(ERROR,
872 						(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
873 						 errmsg("SQL functions cannot have arguments of type %s",
874 								format_type_be(proc->proargtypes.values[i]))));
875 		}
876 	}
877 
878 	/* Postpone body checks if !check_function_bodies */
879 	if (check_function_bodies)
880 	{
881 		tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosrc, &isnull);
882 		if (isnull)
883 			elog(ERROR, "null prosrc");
884 
885 		prosrc = TextDatumGetCString(tmp);
886 
887 		/*
888 		 * Setup error traceback support for ereport().
889 		 */
890 		callback_arg.proname = NameStr(proc->proname);
891 		callback_arg.prosrc = prosrc;
892 
893 		sqlerrcontext.callback = sql_function_parse_error_callback;
894 		sqlerrcontext.arg = (void *) &callback_arg;
895 		sqlerrcontext.previous = error_context_stack;
896 		error_context_stack = &sqlerrcontext;
897 
898 		/*
899 		 * We can't do full prechecking of the function definition if there
900 		 * are any polymorphic input types, because actual datatypes of
901 		 * expression results will be unresolvable.  The check will be done at
902 		 * runtime instead.
903 		 *
904 		 * We can run the text through the raw parser though; this will at
905 		 * least catch silly syntactic errors.
906 		 */
907 		raw_parsetree_list = pg_parse_query(prosrc);
908 
909 		if (!haspolyarg)
910 		{
911 			/*
912 			 * OK to do full precheck: analyze and rewrite the queries, then
913 			 * verify the result type.
914 			 */
915 			SQLFunctionParseInfoPtr pinfo;
916 
917 			/* But first, set up parameter information */
918 			pinfo = prepare_sql_fn_parse_info(tuple, NULL, InvalidOid);
919 
920 			querytree_list = NIL;
921 			foreach(lc, raw_parsetree_list)
922 			{
923 				RawStmt    *parsetree = lfirst_node(RawStmt, lc);
924 				List	   *querytree_sublist;
925 
926 				querytree_sublist = pg_analyze_and_rewrite_params(parsetree,
927 																  prosrc,
928 																  (ParserSetupHook) sql_fn_parser_setup,
929 																  pinfo,
930 																  NULL);
931 				querytree_list = list_concat(querytree_list,
932 											 querytree_sublist);
933 			}
934 
935 			(void) check_sql_fn_retval(funcoid, proc->prorettype,
936 									   querytree_list,
937 									   NULL, NULL);
938 		}
939 
940 		error_context_stack = sqlerrcontext.previous;
941 	}
942 
943 	ReleaseSysCache(tuple);
944 
945 	PG_RETURN_VOID();
946 }
947 
948 /*
949  * Error context callback for handling errors in SQL function definitions
950  */
951 static void
sql_function_parse_error_callback(void * arg)952 sql_function_parse_error_callback(void *arg)
953 {
954 	parse_error_callback_arg *callback_arg = (parse_error_callback_arg *) arg;
955 
956 	/* See if it's a syntax error; if so, transpose to CREATE FUNCTION */
957 	if (!function_parse_error_transpose(callback_arg->prosrc))
958 	{
959 		/* If it's not a syntax error, push info onto context stack */
960 		errcontext("SQL function \"%s\"", callback_arg->proname);
961 	}
962 }
963 
964 /*
965  * Adjust a syntax error occurring inside the function body of a CREATE
966  * FUNCTION or DO command.  This can be used by any function validator or
967  * anonymous-block handler, not only for SQL-language functions.
968  * It is assumed that the syntax error position is initially relative to the
969  * function body string (as passed in).  If possible, we adjust the position
970  * to reference the original command text; if we can't manage that, we set
971  * up an "internal query" syntax error instead.
972  *
973  * Returns true if a syntax error was processed, false if not.
974  */
975 bool
function_parse_error_transpose(const char * prosrc)976 function_parse_error_transpose(const char *prosrc)
977 {
978 	int			origerrposition;
979 	int			newerrposition;
980 	const char *queryText;
981 
982 	/*
983 	 * Nothing to do unless we are dealing with a syntax error that has a
984 	 * cursor position.
985 	 *
986 	 * Some PLs may prefer to report the error position as an internal error
987 	 * to begin with, so check that too.
988 	 */
989 	origerrposition = geterrposition();
990 	if (origerrposition <= 0)
991 	{
992 		origerrposition = getinternalerrposition();
993 		if (origerrposition <= 0)
994 			return false;
995 	}
996 
997 	/* We can get the original query text from the active portal (hack...) */
998 	Assert(ActivePortal && ActivePortal->status == PORTAL_ACTIVE);
999 	queryText = ActivePortal->sourceText;
1000 
1001 	/* Try to locate the prosrc in the original text */
1002 	newerrposition = match_prosrc_to_query(prosrc, queryText, origerrposition);
1003 
1004 	if (newerrposition > 0)
1005 	{
1006 		/* Successful, so fix error position to reference original query */
1007 		errposition(newerrposition);
1008 		/* Get rid of any report of the error as an "internal query" */
1009 		internalerrposition(0);
1010 		internalerrquery(NULL);
1011 	}
1012 	else
1013 	{
1014 		/*
1015 		 * If unsuccessful, convert the position to an internal position
1016 		 * marker and give the function text as the internal query.
1017 		 */
1018 		errposition(0);
1019 		internalerrposition(origerrposition);
1020 		internalerrquery(prosrc);
1021 	}
1022 
1023 	return true;
1024 }
1025 
1026 /*
1027  * Try to locate the string literal containing the function body in the
1028  * given text of the CREATE FUNCTION or DO command.  If successful, return
1029  * the character (not byte) index within the command corresponding to the
1030  * given character index within the literal.  If not successful, return 0.
1031  */
1032 static int
match_prosrc_to_query(const char * prosrc,const char * queryText,int cursorpos)1033 match_prosrc_to_query(const char *prosrc, const char *queryText,
1034 					  int cursorpos)
1035 {
1036 	/*
1037 	 * Rather than fully parsing the original command, we just scan the
1038 	 * command looking for $prosrc$ or 'prosrc'.  This could be fooled (though
1039 	 * not in any very probable scenarios), so fail if we find more than one
1040 	 * match.
1041 	 */
1042 	int			prosrclen = strlen(prosrc);
1043 	int			querylen = strlen(queryText);
1044 	int			matchpos = 0;
1045 	int			curpos;
1046 	int			newcursorpos;
1047 
1048 	for (curpos = 0; curpos < querylen - prosrclen; curpos++)
1049 	{
1050 		if (queryText[curpos] == '$' &&
1051 			strncmp(prosrc, &queryText[curpos + 1], prosrclen) == 0 &&
1052 			queryText[curpos + 1 + prosrclen] == '$')
1053 		{
1054 			/*
1055 			 * Found a $foo$ match.  Since there are no embedded quoting
1056 			 * characters in a dollar-quoted literal, we don't have to do any
1057 			 * fancy arithmetic; just offset by the starting position.
1058 			 */
1059 			if (matchpos)
1060 				return 0;		/* multiple matches, fail */
1061 			matchpos = pg_mbstrlen_with_len(queryText, curpos + 1)
1062 				+ cursorpos;
1063 		}
1064 		else if (queryText[curpos] == '\'' &&
1065 				 match_prosrc_to_literal(prosrc, &queryText[curpos + 1],
1066 										 cursorpos, &newcursorpos))
1067 		{
1068 			/*
1069 			 * Found a 'foo' match.  match_prosrc_to_literal() has adjusted
1070 			 * for any quotes or backslashes embedded in the literal.
1071 			 */
1072 			if (matchpos)
1073 				return 0;		/* multiple matches, fail */
1074 			matchpos = pg_mbstrlen_with_len(queryText, curpos + 1)
1075 				+ newcursorpos;
1076 		}
1077 	}
1078 
1079 	return matchpos;
1080 }
1081 
1082 /*
1083  * Try to match the given source text to a single-quoted literal.
1084  * If successful, adjust newcursorpos to correspond to the character
1085  * (not byte) index corresponding to cursorpos in the source text.
1086  *
1087  * At entry, literal points just past a ' character.  We must check for the
1088  * trailing quote.
1089  */
1090 static bool
match_prosrc_to_literal(const char * prosrc,const char * literal,int cursorpos,int * newcursorpos)1091 match_prosrc_to_literal(const char *prosrc, const char *literal,
1092 						int cursorpos, int *newcursorpos)
1093 {
1094 	int			newcp = cursorpos;
1095 	int			chlen;
1096 
1097 	/*
1098 	 * This implementation handles backslashes and doubled quotes in the
1099 	 * string literal.  It does not handle the SQL syntax for literals
1100 	 * continued across line boundaries.
1101 	 *
1102 	 * We do the comparison a character at a time, not a byte at a time, so
1103 	 * that we can do the correct cursorpos math.
1104 	 */
1105 	while (*prosrc)
1106 	{
1107 		cursorpos--;			/* characters left before cursor */
1108 
1109 		/*
1110 		 * Check for backslashes and doubled quotes in the literal; adjust
1111 		 * newcp when one is found before the cursor.
1112 		 */
1113 		if (*literal == '\\')
1114 		{
1115 			literal++;
1116 			if (cursorpos > 0)
1117 				newcp++;
1118 		}
1119 		else if (*literal == '\'')
1120 		{
1121 			if (literal[1] != '\'')
1122 				goto fail;
1123 			literal++;
1124 			if (cursorpos > 0)
1125 				newcp++;
1126 		}
1127 		chlen = pg_mblen(prosrc);
1128 		if (strncmp(prosrc, literal, chlen) != 0)
1129 			goto fail;
1130 		prosrc += chlen;
1131 		literal += chlen;
1132 	}
1133 
1134 	if (*literal == '\'' && literal[1] != '\'')
1135 	{
1136 		/* success */
1137 		*newcursorpos = newcp;
1138 		return true;
1139 	}
1140 
1141 fail:
1142 	/* Must set *newcursorpos to suppress compiler warning */
1143 	*newcursorpos = newcp;
1144 	return false;
1145 }
1146 
1147 List *
oid_array_to_list(Datum datum)1148 oid_array_to_list(Datum datum)
1149 {
1150 	ArrayType  *array = DatumGetArrayTypeP(datum);
1151 	Datum	   *values;
1152 	int			nelems;
1153 	int			i;
1154 	List	   *result = NIL;
1155 
1156 	deconstruct_array(array,
1157 					  OIDOID,
1158 					  sizeof(Oid), true, 'i',
1159 					  &values, NULL, &nelems);
1160 	for (i = 0; i < nelems; i++)
1161 		result = lappend_oid(result, values[i]);
1162 	return result;
1163 }
1164