88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/backend/catalog/pg_proc.c,v 1.99 2003/07/01 01:28 :32 tgl Exp $
11+ * $Header: /cvsroot/pgsql/src/backend/catalog/pg_proc.c,v 1.100 2003/07/18 23:20 :32 tgl Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -82,8 +82,10 @@ ProcedureCreate(const char *procedureName,
8282Assert (PointerIsValid (probin ));
8383
8484if (parameterCount < 0 || parameterCount > FUNC_MAX_ARGS )
85- elog (ERROR ,"functions cannot have more than %d arguments" ,
86- FUNC_MAX_ARGS );
85+ ereport (ERROR ,
86+ (errcode (ERRCODE_TOO_MANY_ARGUMENTS ),
87+ errmsg ("functions cannot have more than %d arguments" ,
88+ FUNC_MAX_ARGS )));
8789
8890/*
8991 * Do not allow return type ANYARRAY or ANYELEMENT unless at least one
@@ -104,8 +106,9 @@ ProcedureCreate(const char *procedureName,
104106}
105107
106108if (!genericParam )
107- elog (ERROR ,"functions returning ANYARRAY or ANYELEMENT must " \
108- "have at least one argument of either type" );
109+ ereport (ERROR ,
110+ (errcode (ERRCODE_INVALID_FUNCTION_DEFINITION ),
111+ errmsg ("functions returning ANYARRAY or ANYELEMENT must have at least one argument of either type" )));
109112}
110113
111114/* Make sure we have a zero-padded param type array */
@@ -156,10 +159,12 @@ ProcedureCreate(const char *procedureName,
156159 * existing attributes of the type
157160 */
158161if (parameterCount == 1 && OidIsValid (typev [0 ])&&
159- (relid = typeidTypeRelid (typev [0 ]))!= 0 &&
162+ (relid = typeidTypeRelid (typev [0 ]))!= InvalidOid &&
160163get_attnum (relid , (char * )procedureName )!= InvalidAttrNumber )
161- elog (ERROR ,"method %s already an attribute of type %s" ,
162- procedureName ,format_type_be (typev [0 ]));
164+ ereport (ERROR ,
165+ (errcode (ERRCODE_DUPLICATE_COLUMN ),
166+ errmsg ("\"%s\" is already an attribute of type %s" ,
167+ procedureName ,format_type_be (typev [0 ]))));
163168
164169/*
165170 * All seems OK; prepare the data to be inserted into pg_proc.
@@ -208,30 +213,40 @@ ProcedureCreate(const char *procedureName,
208213Form_pg_proc oldproc = (Form_pg_proc )GETSTRUCT (oldtup );
209214
210215if (!replace )
211- elog (ERROR ,"function %s already exists with same argument types" ,
212- procedureName );
216+ ereport (ERROR ,
217+ (errcode (ERRCODE_DUPLICATE_FUNCTION ),
218+ errmsg ("function \"%s\" already exists with same argument types" ,
219+ procedureName )));
213220if (GetUserId ()!= oldproc -> proowner && !superuser ())
214- elog (ERROR ,"ProcedureCreate: you do not have permission to replace function %s" ,
215- procedureName );
221+ ereport (ERROR ,
222+ (errcode (ERRCODE_INSUFFICIENT_PRIVILEGE ),
223+ errmsg ("you do not have permission to replace function \"%s\"" ,
224+ procedureName )));
216225
217226/*
218227 * Not okay to change the return type of the existing proc, since
219228 * existing rules, views, etc may depend on the return type.
220229 */
221230if (returnType != oldproc -> prorettype ||
222231returnsSet != oldproc -> proretset )
223- elog (ERROR ,"ProcedureCreate: cannot change return type of existing function."
224- "\n\tUse DROP FUNCTION first." );
232+ ereport (ERROR ,
233+ (errcode (ERRCODE_INVALID_FUNCTION_DEFINITION ),
234+ errmsg ("cannot change return type of existing function" ),
235+ errhint ("Use DROP FUNCTION first." )));
225236
226237/* Can't change aggregate status, either */
227238if (oldproc -> proisagg != isAgg )
228239{
229240if (oldproc -> proisagg )
230- elog (ERROR ,"function %s is an aggregate" ,
231- procedureName );
241+ ereport (ERROR ,
242+ (errcode (ERRCODE_WRONG_OBJECT_TYPE ),
243+ errmsg ("function \"%s\" is an aggregate" ,
244+ procedureName )));
232245else
233- elog (ERROR ,"function %s is not an aggregate" ,
234- procedureName );
246+ ereport (ERROR ,
247+ (errcode (ERRCODE_WRONG_OBJECT_TYPE ),
248+ errmsg ("function \"%s\" is not an aggregate" ,
249+ procedureName )));
235250}
236251
237252/* do not change existing ownership or permissions, either */
@@ -347,8 +362,11 @@ check_sql_fn_retval(Oid rettype, char fn_typtype, List *queryTreeList)
347362if (queryTreeList == NIL )
348363{
349364if (rettype != VOIDOID )
350- elog (ERROR ,"function declared to return %s, but no SELECT provided" ,
351- format_type_be (rettype ));
365+ ereport (ERROR ,
366+ (errcode (ERRCODE_INVALID_FUNCTION_DEFINITION ),
367+ errmsg ("return type mismatch in function declared to return %s" ,
368+ format_type_be (rettype )),
369+ errdetail ("Function's final statement must be a SELECT." )));
352370return ;
353371}
354372
@@ -365,14 +383,21 @@ check_sql_fn_retval(Oid rettype, char fn_typtype, List *queryTreeList)
365383if (rettype == VOIDOID )
366384{
367385if (cmd == CMD_SELECT )
368- elog (ERROR ,"function declared to return void, but final statement is a SELECT" );
386+ ereport (ERROR ,
387+ (errcode (ERRCODE_INVALID_FUNCTION_DEFINITION ),
388+ errmsg ("return type mismatch in function declared to return %s" ,
389+ format_type_be (rettype )),
390+ errdetail ("Function's final statement must not be a SELECT." )));
369391return ;
370392}
371393
372394/* by here, the function is declared to return some type */
373395if (cmd != CMD_SELECT )
374- elog (ERROR ,"function declared to return %s, but final statement is not a SELECT" ,
375- format_type_be (rettype ));
396+ ereport (ERROR ,
397+ (errcode (ERRCODE_INVALID_FUNCTION_DEFINITION ),
398+ errmsg ("return type mismatch in function declared to return %s" ,
399+ format_type_be (rettype )),
400+ errdetail ("Function's final statement must be a SELECT." )));
376401
377402/*
378403 * Count the non-junk entries in the result targetlist.
@@ -392,13 +417,20 @@ check_sql_fn_retval(Oid rettype, char fn_typtype, List *queryTreeList)
392417 * (As of Postgres 7.2, we accept binary-compatible types too.)
393418 */
394419if (tlistlen != 1 )
395- elog (ERROR ,"function declared to return %s returns multiple columns in final SELECT" ,
396- format_type_be (rettype ));
420+ ereport (ERROR ,
421+ (errcode (ERRCODE_INVALID_FUNCTION_DEFINITION ),
422+ errmsg ("return type mismatch in function declared to return %s" ,
423+ format_type_be (rettype )),
424+ errdetail ("Final SELECT must return exactly one column." )));
397425
398426restype = ((TargetEntry * )lfirst (tlist ))-> resdom -> restype ;
399427if (!IsBinaryCoercible (restype ,rettype ))
400- elog (ERROR ,"return type mismatch in function: declared to return %s, returns %s" ,
401- format_type_be (rettype ),format_type_be (restype ));
428+ ereport (ERROR ,
429+ (errcode (ERRCODE_INVALID_FUNCTION_DEFINITION ),
430+ errmsg ("return type mismatch in function declared to return %s" ,
431+ format_type_be (rettype )),
432+ errdetail ("Actual return type is %s." ,
433+ format_type_be (restype ))));
402434}
403435else if (fn_typtype == 'c' )
404436{
@@ -445,20 +477,26 @@ check_sql_fn_retval(Oid rettype, char fn_typtype, List *queryTreeList)
445477{
446478colindex ++ ;
447479if (colindex > relnatts )
448- elog (ERROR ,"function declared to return %s does not SELECT the right number of columns (%d)" ,
449- format_type_be (rettype ),rellogcols );
480+ ereport (ERROR ,
481+ (errcode (ERRCODE_INVALID_FUNCTION_DEFINITION ),
482+ errmsg ("return type mismatch in function declared to return %s" ,
483+ format_type_be (rettype )),
484+ errdetail ("Final SELECT returns too many columns." )));
450485attr = reln -> rd_att -> attrs [colindex - 1 ];
451486}while (attr -> attisdropped );
452487rellogcols ++ ;
453488
454489tletype = exprType ((Node * )tle -> expr );
455490atttype = attr -> atttypid ;
456491if (!IsBinaryCoercible (tletype ,atttype ))
457- elog (ERROR ,"function declared to return %s returns %s instead of %s at column %d" ,
458- format_type_be (rettype ),
459- format_type_be (tletype ),
460- format_type_be (atttype ),
461- rellogcols );
492+ ereport (ERROR ,
493+ (errcode (ERRCODE_INVALID_FUNCTION_DEFINITION ),
494+ errmsg ("return type mismatch in function declared to return %s" ,
495+ format_type_be (rettype )),
496+ errdetail ("Final SELECT returns %s instead of %s at column %d." ,
497+ format_type_be (tletype ),
498+ format_type_be (atttype ),
499+ rellogcols )));
462500}
463501
464502for (;;)
@@ -471,8 +509,11 @@ check_sql_fn_retval(Oid rettype, char fn_typtype, List *queryTreeList)
471509}
472510
473511if (tlistlen != rellogcols )
474- elog (ERROR ,"function declared to return %s does not SELECT the right number of columns (%d)" ,
475- format_type_be (rettype ),rellogcols );
512+ ereport (ERROR ,
513+ (errcode (ERRCODE_INVALID_FUNCTION_DEFINITION ),
514+ errmsg ("return type mismatch in function declared to return %s" ,
515+ format_type_be (rettype )),
516+ errdetail ("Final SELECT returns too few columns." )));
476517
477518relation_close (reln ,AccessShareLock );
478519}
@@ -488,15 +529,16 @@ check_sql_fn_retval(Oid rettype, char fn_typtype, List *queryTreeList)
488529}
489530else if (rettype == ANYARRAYOID || rettype == ANYELEMENTOID )
490531{
491- /*
492- * This should already have been caught ...
493- */
494- elog (ERROR ,"functions returning ANYARRAY or ANYELEMENT must " \
495- "have at least one argument of either type" );
532+ /* This should already have been caught ... */
533+ ereport (ERROR ,
534+ (errcode (ERRCODE_INVALID_FUNCTION_DEFINITION ),
535+ errmsg ("functions returning ANYARRAY or ANYELEMENT must have at least one argument of either type" )));
496536}
497537else
498- elog (ERROR ,"return type %s is not supported for SQL functions" ,
499- format_type_be (rettype ));
538+ ereport (ERROR ,
539+ (errcode (ERRCODE_INVALID_FUNCTION_DEFINITION ),
540+ errmsg ("return type %s is not supported for SQL functions" ,
541+ format_type_be (rettype ))));
500542}
501543
502544
@@ -521,7 +563,7 @@ fmgr_internal_validator(PG_FUNCTION_ARGS)
521563ObjectIdGetDatum (funcoid ),
5225640 ,0 ,0 );
523565if (!HeapTupleIsValid (tuple ))
524- elog (ERROR ,"cache lookupof function %u failed " ,funcoid );
566+ elog (ERROR ,"cache lookupfailed for function %u" ,funcoid );
525567proc = (Form_pg_proc )GETSTRUCT (tuple );
526568
527569tmp = SysCacheGetAttr (PROCOID ,tuple ,Anum_pg_proc_prosrc ,& isnull );
@@ -530,7 +572,10 @@ fmgr_internal_validator(PG_FUNCTION_ARGS)
530572prosrc = DatumGetCString (DirectFunctionCall1 (textout ,tmp ));
531573
532574if (fmgr_internal_function (prosrc )== InvalidOid )
533- elog (ERROR ,"there is no built-in function named \"%s\"" ,prosrc );
575+ ereport (ERROR ,
576+ (errcode (ERRCODE_UNDEFINED_FUNCTION ),
577+ errmsg ("there is no built-in function named \"%s\"" ,
578+ prosrc )));
534579
535580ReleaseSysCache (tuple );
536581
@@ -562,7 +607,7 @@ fmgr_c_validator(PG_FUNCTION_ARGS)
562607ObjectIdGetDatum (funcoid ),
5636080 ,0 ,0 );
564609if (!HeapTupleIsValid (tuple ))
565- elog (ERROR ,"cache lookupof function %u failed " ,funcoid );
610+ elog (ERROR ,"cache lookupfailed for function %u" ,funcoid );
566611proc = (Form_pg_proc )GETSTRUCT (tuple );
567612
568613tmp = SysCacheGetAttr (PROCOID ,tuple ,Anum_pg_proc_prosrc ,& isnull );
@@ -608,7 +653,7 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
608653ObjectIdGetDatum (funcoid ),
6096540 ,0 ,0 );
610655if (!HeapTupleIsValid (tuple ))
611- elog (ERROR ,"cache lookupof function %u failed " ,funcoid );
656+ elog (ERROR ,"cache lookupfailed for function %u" ,funcoid );
612657proc = (Form_pg_proc )GETSTRUCT (tuple );
613658
614659functyptype = get_typtype (proc -> prorettype );
@@ -620,8 +665,10 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
620665proc -> prorettype != VOIDOID &&
621666proc -> prorettype != ANYARRAYOID &&
622667proc -> prorettype != ANYELEMENTOID )
623- elog (ERROR ,"SQL functions cannot return type %s" ,
624- format_type_be (proc -> prorettype ));
668+ ereport (ERROR ,
669+ (errcode (ERRCODE_INVALID_FUNCTION_DEFINITION ),
670+ errmsg ("SQL functions cannot return type %s" ,
671+ format_type_be (proc -> prorettype ))));
625672
626673/* Disallow pseudotypes in arguments */
627674/* except for ANYARRAY or ANYELEMENT */
@@ -634,8 +681,10 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
634681proc -> proargtypes [i ]== ANYELEMENTOID )
635682haspolyarg = true;
636683else
637- elog (ERROR ,"SQL functions cannot have arguments of type %s" ,
638- format_type_be (proc -> proargtypes [i ]));
684+ ereport (ERROR ,
685+ (errcode (ERRCODE_INVALID_FUNCTION_DEFINITION ),
686+ errmsg ("SQL functions cannot have arguments of type %s" ,
687+ format_type_be (proc -> proargtypes [i ]))));
639688}
640689}
641690