]> granicus.if.org Git - postgresql/commitdiff
Modify error context callback functions to not assume that they can fetch
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 19 Mar 2010 22:54:49 +0000 (22:54 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 19 Mar 2010 22:54:49 +0000 (22:54 +0000)
catalog entries via SearchSysCache and related operations.  Although, at the
time that these callbacks are called by elog.c, we have not officially aborted
the current transaction, it still seems rather risky to initiate any new
catalog fetches.  In all these cases the needed information is readily
available in the caller and so it's just a matter of a bit of extra notation
to pass it to the callback.

Per crash report from Dennis Koegel.  I've concluded that the real fix for
his problem is to clear the error context stack at entry to proc_exit, but
it still seems like a good idea to make the callbacks a bit less fragile
for other cases.

Backpatch to 8.4.  We could go further back, but the patch doesn't apply
cleanly.  In the absence of proof that this fixes something and isn't just
paranoia, I'm not going to expend the effort.

src/backend/catalog/pg_proc.c
src/backend/executor/functions.c
src/backend/optimizer/util/clauses.c

index 397482d7f813602442cc9a3907dafd104d8a179e..18e8b28627f53d258212c9420b3fc281dab52efb 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/catalog/pg_proc.c,v 1.164.2.2 2009/12/14 02:16:03 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/catalog/pg_proc.c,v 1.164.2.3 2010/03/19 22:54:49 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -41,6 +41,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);
@@ -672,6 +678,7 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
        bool            isnull;
        Datum           tmp;
        char       *prosrc;
+       parse_error_callback_arg callback_arg;
        ErrorContextCallback sqlerrcontext;
        bool            haspolyarg;
        int                     i;
@@ -723,8 +730,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;
 
@@ -763,25 +773,14 @@ 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);
 }
 
 /*
index 9d4ca42fdc19b0f9d92ed31f83aad676e32ce5e9..502edc7d97f339b0826f8811220145b2c4273383 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/executor/functions.c,v 1.135.2.2 2009/12/29 17:41:09 heikki Exp $
+ *       $PostgreSQL: pgsql/src/backend/executor/functions.c,v 1.135.2.3 2010/03/19 22:54:49 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -74,6 +74,7 @@ typedef struct execution_state
  */
 typedef struct
 {
+       char       *fname;                      /* function name (for error msgs) */
        char       *src;                        /* function body text (for error msgs) */
 
        Oid                *argtypes;           /* resolved types of arguments */
@@ -228,6 +229,7 @@ init_sql_fcache(FmgrInfo *finfo, bool lazyEvalOK)
        bool            isNull;
 
        fcache = (SQLFunctionCachePtr) palloc0(sizeof(SQLFunctionCache));
+       finfo->fn_extra = (void *) fcache;
 
        /*
         * get the procedure tuple corresponding to the given function Oid
@@ -239,6 +241,11 @@ init_sql_fcache(FmgrInfo *finfo, bool lazyEvalOK)
                elog(ERROR, "cache lookup failed for function %u", foid);
        procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
 
+       /*
+        * copy function name immediately for use by error reporting callback
+        */
+       fcache->fname = pstrdup(NameStr(procedureStruct->proname));
+
        /*
         * get the result type from the procedure tuple, and check for polymorphic
         * result type; if so, find out the actual result type.
@@ -363,8 +370,6 @@ init_sql_fcache(FmgrInfo *finfo, bool lazyEvalOK)
                                                                                          lazyEvalOK);
 
        ReleaseSysCache(procedureTuple);
-
-       finfo->fn_extra = (void *) fcache;
 }
 
 /* Start up execution of one execution_state node */
@@ -874,39 +879,24 @@ sql_exec_error_callback(void *arg)
 {
        FmgrInfo   *flinfo = (FmgrInfo *) arg;
        SQLFunctionCachePtr fcache = (SQLFunctionCachePtr) flinfo->fn_extra;
-       HeapTuple       func_tuple;
-       Form_pg_proc functup;
-       char       *fn_name;
        int                     syntaxerrposition;
 
-       /* Need access to function's pg_proc tuple */
-       func_tuple = SearchSysCache(PROCOID,
-                                                               ObjectIdGetDatum(flinfo->fn_oid),
-                                                               0, 0, 0);
-       if (!HeapTupleIsValid(func_tuple))
-               return;                                 /* shouldn't happen */
-       functup = (Form_pg_proc) GETSTRUCT(func_tuple);
-       fn_name = NameStr(functup->proname);
+       /*
+        * We can do nothing useful if init_sql_fcache() didn't get as far as
+        * saving the function name
+        */
+       if (fcache == NULL || fcache->fname == NULL)
+               return;
 
        /*
         * If there is a syntax error position, convert to internal syntax error
         */
        syntaxerrposition = geterrposition();
-       if (syntaxerrposition > 0)
+       if (syntaxerrposition > 0 && fcache->src != NULL)
        {
-               bool            isnull;
-               Datum           tmp;
-               char       *prosrc;
-
-               tmp = SysCacheGetAttr(PROCOID, func_tuple, Anum_pg_proc_prosrc,
-                                                         &isnull);
-               if (isnull)
-                       elog(ERROR, "null prosrc");
-               prosrc = TextDatumGetCString(tmp);
                errposition(0);
                internalerrposition(syntaxerrposition);
-               internalerrquery(prosrc);
-               pfree(prosrc);
+               internalerrquery(fcache->src);
        }
 
        /*
@@ -916,7 +906,7 @@ sql_exec_error_callback(void *arg)
         * ExecutorEnd are blamed on the appropriate query; see postquel_start and
         * postquel_end.)
         */
-       if (fcache)
+       if (fcache->func_state)
        {
                execution_state *es;
                int                     query_num;
@@ -928,7 +918,7 @@ sql_exec_error_callback(void *arg)
                        if (es->qd)
                        {
                                errcontext("SQL function \"%s\" statement %d",
-                                                  fn_name, query_num);
+                                                  fcache->fname, query_num);
                                break;
                        }
                        es = es->next;
@@ -940,16 +930,18 @@ sql_exec_error_callback(void *arg)
                         * couldn't identify a running query; might be function entry,
                         * function exit, or between queries.
                         */
-                       errcontext("SQL function \"%s\"", fn_name);
+                       errcontext("SQL function \"%s\"", fcache->fname);
                }
        }
        else
        {
-               /* must have failed during init_sql_fcache() */
-               errcontext("SQL function \"%s\" during startup", fn_name);
+               /*
+                * Assume we failed during init_sql_fcache().  (It's possible that
+                * the function actually has an empty body, but in that case we may
+                * as well report all errors as being "during startup".)
+                */
+               errcontext("SQL function \"%s\" during startup", fcache->fname);
        }
-
-       ReleaseSysCache(func_tuple);
 }
 
 
index ddb1e22b681efbd89933e5623ff7049361d1c268..5f6c08c0134793e2f6afcadef59e6f015e0a6b0f 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.277.2.1 2009/12/14 02:16:03 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/optimizer/util/clauses.c,v 1.277.2.2 2010/03/19 22:54:49 tgl Exp $
  *
  * HISTORY
  *       AUTHOR                        DATE                    MAJOR EVENT
@@ -71,6 +71,12 @@ typedef struct
        int                     sublevels_up;
 } substitute_actual_srf_parameters_context;
 
+typedef struct
+{
+       char       *proname;
+       char       *prosrc;
+} inline_error_callback_arg;
+
 static bool contain_agg_clause_walker(Node *node, void *context);
 static bool pull_agg_clause_walker(Node *node, List **context);
 static bool count_agg_clauses_walker(Node *node, AggClauseCounts *counts);
@@ -3487,6 +3493,7 @@ inline_function(Oid funcid, Oid result_type, List *args,
        bool            modifyTargetList;
        MemoryContext oldcxt;
        MemoryContext mycxt;
+       inline_error_callback_arg callback_arg;
        ErrorContextCallback sqlerrcontext;
        List       *raw_parsetree_list;
        Query      *querytree;
@@ -3514,15 +3521,6 @@ inline_function(Oid funcid, Oid result_type, List *args,
        if (pg_proc_aclcheck(funcid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
                return NULL;
 
-       /*
-        * Setup error traceback support for ereport().  This is so that we can
-        * finger the function that bad information came from.
-        */
-       sqlerrcontext.callback = sql_inline_error_callback;
-       sqlerrcontext.arg = func_tuple;
-       sqlerrcontext.previous = error_context_stack;
-       error_context_stack = &sqlerrcontext;
-
        /*
         * Make a temporary memory context, so that we don't leak all the stuff
         * that parsing might create.
@@ -3534,6 +3532,27 @@ inline_function(Oid funcid, Oid result_type, List *args,
                                                                  ALLOCSET_DEFAULT_MAXSIZE);
        oldcxt = MemoryContextSwitchTo(mycxt);
 
+       /* Fetch the function body */
+       tmp = SysCacheGetAttr(PROCOID,
+                                                 func_tuple,
+                                                 Anum_pg_proc_prosrc,
+                                                 &isNull);
+       if (isNull)
+               elog(ERROR, "null prosrc for function %u", funcid);
+       src = TextDatumGetCString(tmp);
+
+       /*
+        * Setup error traceback support for ereport().  This is so that we can
+        * finger the function that bad information came from.
+        */
+       callback_arg.proname = NameStr(funcform->proname);
+       callback_arg.prosrc = src;
+
+       sqlerrcontext.callback = sql_inline_error_callback;
+       sqlerrcontext.arg = (void *) &callback_arg;
+       sqlerrcontext.previous = error_context_stack;
+       error_context_stack = &sqlerrcontext;
+
        /* Check for polymorphic arguments, and substitute actual arg types */
        argtypes = (Oid *) palloc(funcform->pronargs * sizeof(Oid));
        memcpy(argtypes, funcform->proargtypes.values,
@@ -3546,15 +3565,6 @@ inline_function(Oid funcid, Oid result_type, List *args,
                }
        }
 
-       /* Fetch and parse the function body */
-       tmp = SysCacheGetAttr(PROCOID,
-                                                 func_tuple,
-                                                 Anum_pg_proc_prosrc,
-                                                 &isNull);
-       if (isNull)
-               elog(ERROR, "null prosrc for function %u", funcid);
-       src = TextDatumGetCString(tmp);
-
        /*
         * We just do parsing and parse analysis, not rewriting, because rewriting
         * will not affect table-free-SELECT-only queries, which is all that we
@@ -3774,30 +3784,19 @@ substitute_actual_parameters_mutator(Node *node,
 static void
 sql_inline_error_callback(void *arg)
 {
-       HeapTuple       func_tuple = (HeapTuple) arg;
-       Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
+       inline_error_callback_arg *callback_arg = (inline_error_callback_arg *) arg;
        int                     syntaxerrposition;
 
        /* If it's a syntax error, convert to internal syntax error report */
        syntaxerrposition = geterrposition();
        if (syntaxerrposition > 0)
        {
-               bool            isnull;
-               Datum           tmp;
-               char       *prosrc;
-
-               tmp = SysCacheGetAttr(PROCOID, func_tuple, Anum_pg_proc_prosrc,
-                                                         &isnull);
-               if (isnull)
-                       elog(ERROR, "null prosrc");
-               prosrc = TextDatumGetCString(tmp);
                errposition(0);
                internalerrposition(syntaxerrposition);
-               internalerrquery(prosrc);
+               internalerrquery(callback_arg->prosrc);
        }
 
-       errcontext("SQL function \"%s\" during inlining",
-                          NameStr(funcform->proname));
+       errcontext("SQL function \"%s\" during inlining", callback_arg->proname);
 }
 
 /*
@@ -3904,6 +3903,7 @@ inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
        bool            modifyTargetList;
        MemoryContext oldcxt;
        MemoryContext mycxt;
+       inline_error_callback_arg callback_arg;
        ErrorContextCallback sqlerrcontext;
        List       *raw_parsetree_list;
        List       *querytree_list;
@@ -3980,15 +3980,6 @@ inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
                return NULL;
        }
 
-       /*
-        * Setup error traceback support for ereport().  This is so that we can
-        * finger the function that bad information came from.
-        */
-       sqlerrcontext.callback = sql_inline_error_callback;
-       sqlerrcontext.arg = func_tuple;
-       sqlerrcontext.previous = error_context_stack;
-       error_context_stack = &sqlerrcontext;
-
        /*
         * Make a temporary memory context, so that we don't leak all the stuff
         * that parsing might create.
@@ -4000,6 +3991,27 @@ inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
                                                                  ALLOCSET_DEFAULT_MAXSIZE);
        oldcxt = MemoryContextSwitchTo(mycxt);
 
+       /* Fetch the function body */
+       tmp = SysCacheGetAttr(PROCOID,
+                                                 func_tuple,
+                                                 Anum_pg_proc_prosrc,
+                                                 &isNull);
+       if (isNull)
+               elog(ERROR, "null prosrc for function %u", fexpr->funcid);
+       src = TextDatumGetCString(tmp);
+
+       /*
+        * Setup error traceback support for ereport().  This is so that we can
+        * finger the function that bad information came from.
+        */
+       callback_arg.proname = NameStr(funcform->proname);
+       callback_arg.prosrc = src;
+
+       sqlerrcontext.callback = sql_inline_error_callback;
+       sqlerrcontext.arg = (void *) &callback_arg;
+       sqlerrcontext.previous = error_context_stack;
+       error_context_stack = &sqlerrcontext;
+
        /* Check for polymorphic arguments, and substitute actual arg types */
        argtypes = (Oid *) palloc(funcform->pronargs * sizeof(Oid));
        memcpy(argtypes, funcform->proargtypes.values,
@@ -4012,15 +4024,6 @@ inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
                }
        }
 
-       /* Fetch and parse the function body */
-       tmp = SysCacheGetAttr(PROCOID,
-                                                 func_tuple,
-                                                 Anum_pg_proc_prosrc,
-                                                 &isNull);
-       if (isNull)
-               elog(ERROR, "null prosrc for function %u", fexpr->funcid);
-       src = TextDatumGetCString(tmp);
-
        /*
         * Parse, analyze, and rewrite (unlike inline_function(), we can't skip
         * rewriting here).  We can fail as soon as we find more than one query,