OSDN Git Service

Add notion of a "transform function" that can simplify function calls.
[pg-rex/syncrep.git] / src / backend / catalog / pg_proc.c
index 41c6d1e..92be0a7 100644 (file)
@@ -3,12 +3,12 @@
  * pg_proc.c
  *       routines to support manipulation of the pg_proc relation
  *
- * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/catalog/pg_proc.c,v 1.165 2009/09/22 23:43:37 tgl Exp $
+ *       src/backend/catalog/pg_proc.c
  *
  *-------------------------------------------------------------------------
  */
@@ -18,6 +18,7 @@
 #include "access/xact.h"
 #include "catalog/dependency.h"
 #include "catalog/indexing.h"
+#include "catalog/objectaccess.h"
 #include "catalog/pg_language.h"
 #include "catalog/pg_namespace.h"
 #include "catalog/pg_proc.h"
@@ -41,6 +42,12 @@ Datum                fmgr_internal_validator(PG_FUNCTION_ARGS);
 Datum          fmgr_c_validator(PG_FUNCTION_ARGS);
 Datum          fmgr_sql_validator(PG_FUNCTION_ARGS);
 
+typedef struct
+{
+       char       *proname;
+       char       *prosrc;
+} parse_error_callback_arg;
+
 static void sql_function_parse_error_callback(void *arg);
 static int match_prosrc_to_query(const char *prosrc, const char *queryText,
                                          int cursorpos);
@@ -89,6 +96,8 @@ ProcedureCreate(const char *procedureName,
        bool            internalInParam = false;
        bool            internalOutParam = false;
        Oid                     variadicType = InvalidOid;
+       Oid                     proowner = GetUserId();
+       Acl                *proacl = NULL;
        Relation        rel;
        HeapTuple       tup;
        HeapTuple       oldtup;
@@ -290,11 +299,12 @@ ProcedureCreate(const char *procedureName,
        namestrcpy(&procname, procedureName);
        values[Anum_pg_proc_proname - 1] = NameGetDatum(&procname);
        values[Anum_pg_proc_pronamespace - 1] = ObjectIdGetDatum(procNamespace);
-       values[Anum_pg_proc_proowner - 1] = ObjectIdGetDatum(GetUserId());
+       values[Anum_pg_proc_proowner - 1] = ObjectIdGetDatum(proowner);
        values[Anum_pg_proc_prolang - 1] = ObjectIdGetDatum(languageObjectId);
        values[Anum_pg_proc_procost - 1] = Float4GetDatum(procost);
        values[Anum_pg_proc_prorows - 1] = Float4GetDatum(prorows);
        values[Anum_pg_proc_provariadic - 1] = ObjectIdGetDatum(variadicType);
+       values[Anum_pg_proc_protransform - 1] = ObjectIdGetDatum(InvalidOid);
        values[Anum_pg_proc_proisagg - 1] = BoolGetDatum(isAgg);
        values[Anum_pg_proc_proiswindow - 1] = BoolGetDatum(isWindowFunc);
        values[Anum_pg_proc_prosecdef - 1] = BoolGetDatum(security_definer);
@@ -330,30 +340,30 @@ ProcedureCreate(const char *procedureName,
                values[Anum_pg_proc_proconfig - 1] = proconfig;
        else
                nulls[Anum_pg_proc_proconfig - 1] = true;
-       /* start out with empty permissions */
-       nulls[Anum_pg_proc_proacl - 1] = true;
+       /* proacl will be determined later */
 
        rel = heap_open(ProcedureRelationId, RowExclusiveLock);
        tupDesc = RelationGetDescr(rel);
 
        /* Check for pre-existing definition */
-       oldtup = SearchSysCache(PROCNAMEARGSNSP,
-                                                       PointerGetDatum(procedureName),
-                                                       PointerGetDatum(parameterTypes),
-                                                       ObjectIdGetDatum(procNamespace),
-                                                       0);
+       oldtup = SearchSysCache3(PROCNAMEARGSNSP,
+                                                        PointerGetDatum(procedureName),
+                                                        PointerGetDatum(parameterTypes),
+                                                        ObjectIdGetDatum(procNamespace));
 
        if (HeapTupleIsValid(oldtup))
        {
                /* There is one; okay to replace it? */
                Form_pg_proc oldproc = (Form_pg_proc) GETSTRUCT(oldtup);
+               Datum           proargnames;
+               bool            isnull;
 
                if (!replace)
                        ereport(ERROR,
                                        (errcode(ERRCODE_DUPLICATE_FUNCTION),
                        errmsg("function \"%s\" already exists with same argument types",
                                   procedureName)));
-               if (!pg_proc_ownercheck(HeapTupleGetOid(oldtup), GetUserId()))
+               if (!pg_proc_ownercheck(HeapTupleGetOid(oldtup), proowner))
                        aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PROC,
                                                   procedureName);
 
@@ -393,6 +403,49 @@ ProcedureCreate(const char *procedureName,
                }
 
                /*
+                * If there were any named input parameters, check to make sure the
+                * names have not been changed, as this could break existing calls. We
+                * allow adding names to formerly unnamed parameters, though.
+                */
+               proargnames = SysCacheGetAttr(PROCNAMEARGSNSP, oldtup,
+                                                                         Anum_pg_proc_proargnames,
+                                                                         &isnull);
+               if (!isnull)
+               {
+                       Datum           proargmodes;
+                       char      **old_arg_names;
+                       char      **new_arg_names;
+                       int                     n_old_arg_names;
+                       int                     n_new_arg_names;
+                       int                     j;
+
+                       proargmodes = SysCacheGetAttr(PROCNAMEARGSNSP, oldtup,
+                                                                                 Anum_pg_proc_proargmodes,
+                                                                                 &isnull);
+                       if (isnull)
+                               proargmodes = PointerGetDatum(NULL);    /* just to be sure */
+
+                       n_old_arg_names = get_func_input_arg_names(proargnames,
+                                                                                                          proargmodes,
+                                                                                                          &old_arg_names);
+                       n_new_arg_names = get_func_input_arg_names(parameterNames,
+                                                                                                          parameterModes,
+                                                                                                          &new_arg_names);
+                       for (j = 0; j < n_old_arg_names; j++)
+                       {
+                               if (old_arg_names[j] == NULL)
+                                       continue;
+                               if (j >= n_new_arg_names || new_arg_names[j] == NULL ||
+                                       strcmp(old_arg_names[j], new_arg_names[j]) != 0)
+                                       ereport(ERROR,
+                                                       (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
+                                          errmsg("cannot change name of input parameter \"%s\"",
+                                                         old_arg_names[j]),
+                                                        errhint("Use DROP FUNCTION first.")));
+                       }
+               }
+
+               /*
                 * If there are existing defaults, check compatibility: redefinition
                 * must not remove any defaults nor change their types.  (Removing a
                 * default might cause a function to fail to satisfy an existing call.
@@ -403,7 +456,6 @@ ProcedureCreate(const char *procedureName,
                if (oldproc->pronargdefaults != 0)
                {
                        Datum           proargdefaults;
-                       bool            isnull;
                        List       *oldDefaults;
                        ListCell   *oldlc;
                        ListCell   *newlc;
@@ -471,7 +523,10 @@ ProcedureCreate(const char *procedureName,
                                                                procedureName)));
                }
 
-               /* do not change existing ownership or permissions, either */
+               /*
+                * Do not change existing ownership or permissions, either.  Note
+                * dependency-update code below has to agree with this decision.
+                */
                replaces[Anum_pg_proc_proowner - 1] = false;
                replaces[Anum_pg_proc_proacl - 1] = false;
 
@@ -485,6 +540,15 @@ ProcedureCreate(const char *procedureName,
        else
        {
                /* Creating a new procedure */
+
+               /* First, get default permissions and set up proacl */
+               proacl = get_user_default_acl(ACL_OBJECT_FUNCTION, proowner,
+                                                                         procNamespace);
+               if (proacl != NULL)
+                       values[Anum_pg_proc_proacl - 1] = PointerGetDatum(proacl);
+               else
+                       nulls[Anum_pg_proc_proacl - 1] = true;
+
                tup = heap_form_tuple(tupDesc, values, nulls);
                simple_heap_insert(rel, tup);
                is_update = false;
@@ -498,12 +562,12 @@ ProcedureCreate(const char *procedureName,
        /*
         * Create dependencies for the new function.  If we are updating an
         * existing function, first delete any existing pg_depend entries.
+        * (However, since we are not changing ownership or permissions, the
+        * shared dependencies do *not* need to change, and we leave them alone.
+        * We also don't change any pre-existing extension-membership dependency.)
         */
        if (is_update)
-       {
-               deleteDependencyRecordsFor(ProcedureRelationId, retval);
-               deleteSharedDependencyRecordsFor(ProcedureRelationId, retval, 0);
-       }
+               deleteDependencyRecordsFor(ProcedureRelationId, retval, true);
 
        myself.classId = ProcedureRelationId;
        myself.objectId = retval;
@@ -537,18 +601,59 @@ ProcedureCreate(const char *procedureName,
        }
 
        /* dependency on owner */
-       recordDependencyOnOwner(ProcedureRelationId, retval, GetUserId());
+       if (!is_update)
+               recordDependencyOnOwner(ProcedureRelationId, retval, proowner);
+
+       /* dependency on any roles mentioned in ACL */
+       if (!is_update && proacl != NULL)
+       {
+               int                     nnewmembers;
+               Oid                *newmembers;
+
+               nnewmembers = aclmembers(proacl, &newmembers);
+               updateAclDependencies(ProcedureRelationId, retval, 0,
+                                                         proowner,
+                                                         0, NULL,
+                                                         nnewmembers, newmembers);
+       }
+
+       /* dependency on extension */
+       if (!is_update)
+               recordDependencyOnCurrentExtension(&myself);
 
        heap_freetuple(tup);
 
+       /* Post creation hook for new function */
+       InvokeObjectAccessHook(OAT_POST_CREATE, ProcedureRelationId, retval, 0);
+
        heap_close(rel, RowExclusiveLock);
 
        /* Verify function body */
        if (OidIsValid(languageValidator))
        {
+               ArrayType  *set_items;
+               int                     save_nestlevel;
+
                /* Advance command counter so new tuple can be seen by validator */
                CommandCounterIncrement();
+
+               /* Set per-function configuration parameters */
+               set_items = (ArrayType *) DatumGetPointer(proconfig);
+               if (set_items)                  /* Need a new GUC nesting level */
+               {
+                       save_nestlevel = NewGUCNestLevel();
+                       ProcessGUCArray(set_items,
+                                                       (superuser() ? PGC_SUSET : PGC_USERSET),
+                                                       PGC_S_SESSION,
+                                                       GUC_ACTION_SAVE);
+               }
+               else
+                       save_nestlevel = 0; /* keep compiler quiet */
+
                OidFunctionCall1(languageValidator, ObjectIdGetDatum(retval));
+
+               if (set_items)
+                       AtEOXact_GUC(true, save_nestlevel);
        }
 
        return retval;
@@ -567,7 +672,6 @@ fmgr_internal_validator(PG_FUNCTION_ARGS)
 {
        Oid                     funcoid = PG_GETARG_OID(0);
        HeapTuple       tuple;
-       Form_pg_proc proc;
        bool            isnull;
        Datum           tmp;
        char       *prosrc;
@@ -577,12 +681,9 @@ fmgr_internal_validator(PG_FUNCTION_ARGS)
         * name will be found later if it isn't there now.
         */
 
-       tuple = SearchSysCache(PROCOID,
-                                                  ObjectIdGetDatum(funcoid),
-                                                  0, 0, 0);
+       tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
        if (!HeapTupleIsValid(tuple))
                elog(ERROR, "cache lookup failed for function %u", funcoid);
-       proc = (Form_pg_proc) GETSTRUCT(tuple);
 
        tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosrc, &isnull);
        if (isnull)
@@ -615,7 +716,6 @@ fmgr_c_validator(PG_FUNCTION_ARGS)
        Oid                     funcoid = PG_GETARG_OID(0);
        void       *libraryhandle;
        HeapTuple       tuple;
-       Form_pg_proc proc;
        bool            isnull;
        Datum           tmp;
        char       *prosrc;
@@ -627,12 +727,9 @@ fmgr_c_validator(PG_FUNCTION_ARGS)
         * and for pg_dump loading it's much better if we *do* check.
         */
 
-       tuple = SearchSysCache(PROCOID,
-                                                  ObjectIdGetDatum(funcoid),
-                                                  0, 0, 0);
+       tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
        if (!HeapTupleIsValid(tuple))
                elog(ERROR, "cache lookup failed for function %u", funcoid);
-       proc = (Form_pg_proc) GETSTRUCT(tuple);
 
        tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosrc, &isnull);
        if (isnull)
@@ -664,17 +761,18 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
        Oid                     funcoid = PG_GETARG_OID(0);
        HeapTuple       tuple;
        Form_pg_proc proc;
+       List       *raw_parsetree_list;
        List       *querytree_list;
+       ListCell   *lc;
        bool            isnull;
        Datum           tmp;
        char       *prosrc;
+       parse_error_callback_arg callback_arg;
        ErrorContextCallback sqlerrcontext;
        bool            haspolyarg;
        int                     i;
 
-       tuple = SearchSysCache(PROCOID,
-                                                  ObjectIdGetDatum(funcoid),
-                                                  0, 0, 0);
+       tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
        if (!HeapTupleIsValid(tuple))
                elog(ERROR, "cache lookup failed for function %u", funcoid);
        proc = (Form_pg_proc) GETSTRUCT(tuple);
@@ -719,8 +817,11 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
                /*
                 * Setup error traceback support for ereport().
                 */
+               callback_arg.proname = NameStr(proc->proname);
+               callback_arg.prosrc = prosrc;
+
                sqlerrcontext.callback = sql_function_parse_error_callback;
-               sqlerrcontext.arg = tuple;
+               sqlerrcontext.arg = (void *) &callback_arg;
                sqlerrcontext.previous = error_context_stack;
                error_context_stack = &sqlerrcontext;
 
@@ -733,17 +834,37 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
                 * We can run the text through the raw parser though; this will at
                 * least catch silly syntactic errors.
                 */
+               raw_parsetree_list = pg_parse_query(prosrc);
+
                if (!haspolyarg)
                {
-                       querytree_list = pg_parse_and_rewrite(prosrc,
-                                                                                                 proc->proargtypes.values,
-                                                                                                 proc->pronargs);
+                       /*
+                        * OK to do full precheck: analyze and rewrite the queries, then
+                        * verify the result type.
+                        */
+                       SQLFunctionParseInfoPtr pinfo;
+
+                       /* But first, set up parameter information */
+                       pinfo = prepare_sql_fn_parse_info(tuple, NULL, InvalidOid);
+
+                       querytree_list = NIL;
+                       foreach(lc, raw_parsetree_list)
+                       {
+                               Node       *parsetree = (Node *) lfirst(lc);
+                               List       *querytree_sublist;
+
+                               querytree_sublist = pg_analyze_and_rewrite_params(parsetree,
+                                                                                                                                 prosrc,
+                                                                          (ParserSetupHook) sql_fn_parser_setup,
+                                                                                                                                 pinfo);
+                               querytree_list = list_concat(querytree_list,
+                                                                                        querytree_sublist);
+                       }
+
                        (void) check_sql_fn_retval(funcoid, proc->prorettype,
                                                                           querytree_list,
-                                                                          false, NULL);
+                                                                          NULL, NULL);
                }
-               else
-                       querytree_list = pg_parse_query(prosrc);
 
                error_context_stack = sqlerrcontext.previous;
        }
@@ -759,30 +880,19 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
 static void
 sql_function_parse_error_callback(void *arg)
 {
-       HeapTuple       tuple = (HeapTuple) arg;
-       Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(tuple);
-       bool            isnull;
-       Datum           tmp;
-       char       *prosrc;
+       parse_error_callback_arg *callback_arg = (parse_error_callback_arg *) arg;
 
        /* See if it's a syntax error; if so, transpose to CREATE FUNCTION */
-       tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosrc, &isnull);
-       if (isnull)
-               elog(ERROR, "null prosrc");
-       prosrc = TextDatumGetCString(tmp);
-
-       if (!function_parse_error_transpose(prosrc))
+       if (!function_parse_error_transpose(callback_arg->prosrc))
        {
                /* If it's not a syntax error, push info onto context stack */
-               errcontext("SQL function \"%s\"", NameStr(proc->proname));
+               errcontext("SQL function \"%s\"", callback_arg->proname);
        }
-
-       pfree(prosrc);
 }
 
 /*
  * Adjust a syntax error occurring inside the function body of a CREATE
- * FUNCTION or DO command.  This can be used by any function validator or
+ * FUNCTION or DO command.     This can be used by any function validator or
  * anonymous-block handler, not only for SQL-language functions.
  * It is assumed that the syntax error position is initially relative to the
  * function body string (as passed in).  If possible, we adjust the position