OSDN Git Service

Fix about unexpectedly living plpgsql query string.
[pghintplan/pg_hint_plan.git] / pg_hint_plan.c
index 11ac127..240a128 100644 (file)
@@ -490,7 +490,9 @@ static const HintParser parsers[] = {
  * PL/pgSQL plugin for retrieving string representation of each query during
  * function execution.
  */
-const char *plpgsql_query_string = NULL;
+static const char *plpgsql_query_string = NULL;
+static enum PLpgSQL_stmt_types plpgsql_query_string_src;
+
 PLpgSQL_plugin  plugin_funcs = {
        NULL,
        NULL,
@@ -3682,7 +3684,10 @@ pg_hint_plan_plpgsql_stmt_beg(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
        }
 
        if (expr)
+       {
                plpgsql_query_string = expr->query;
+               plpgsql_query_string_src = (enum PLpgSQL_stmt_types) stmt->cmd_type;
+       }
 }
 
 /*
@@ -3693,7 +3698,8 @@ pg_hint_plan_plpgsql_stmt_beg(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
 static void
 pg_hint_plan_plpgsql_stmt_end(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
 {
-       if ((enum PLpgSQL_stmt_types) stmt->cmd_type == PLPGSQL_STMT_EXECSQL)
+       if (plpgsql_query_string &&
+               plpgsql_query_string_src == stmt->cmd_type)
                plpgsql_query_string = NULL;
 }