+/*
+ * Retrieve and store a hint string from given query or from the hint table.
+ * If we are using the hint table, the query string is needed to be normalized.
+ * However, ParseState, which is not available in planner_hook, is required to
+ * check if the query tree (Query) is surely corresponding to the target query.
+ */
+static void
+pg_hint_plan_post_parse_analyze(ParseState *pstate, Query *query)
+{
+ const char *query_str;
+ MemoryContext oldcontext;
+
+ if (prev_post_parse_analyze_hook)
+ prev_post_parse_analyze_hook(pstate, query);
+
+ /* do nothing under hint table search */
+ if (hint_inhibit_level > 0)
+ return;
+
+ if (!pg_hint_plan_enable_hint)
+ {
+ if (current_hint_str)
+ {
+ pfree((void *)current_hint_str);
+ current_hint_str = NULL;
+ }
+ return;
+ }
+
+ /* increment the query number */
+ qnostr[0] = 0;
+ if (debug_level > 1)
+ snprintf(qnostr, sizeof(qnostr), "[qno=0x%x]", qno++);
+ qno++;
+
+ /* search the hint table for a hint if requested */
+ if (pg_hint_plan_enable_hint_table)
+ {
+ int query_len;
+ pgssJumbleState jstate;
+ Query *jumblequery;
+ char *normalized_query = NULL;
+
+ query_str = get_query_string(pstate, query, &jumblequery);
+
+ /* If this query is not for hint, just return */
+ if (!query_str)
+ return;
+
+ /* clear the previous hint string */
+ if (current_hint_str)
+ {
+ pfree((void *)current_hint_str);
+ current_hint_str = NULL;
+ }
+
+ if (jumblequery)
+ {
+ /*
+ * XXX: normalizing code is copied from pg_stat_statements.c, so be
+ * careful to PostgreSQL's version up.
+ */
+ jstate.jumble = (unsigned char *) palloc(JUMBLE_SIZE);
+ jstate.jumble_len = 0;
+ jstate.clocations_buf_size = 32;
+ jstate.clocations = (pgssLocationLen *)
+ palloc(jstate.clocations_buf_size * sizeof(pgssLocationLen));
+ jstate.clocations_count = 0;
+
+ JumbleQuery(&jstate, jumblequery);
+
+ /*
+ * Normalize the query string by replacing constants with '?'
+ */
+ /*
+ * Search hint string which is stored keyed by query string
+ * and application name. The query string is normalized to allow
+ * fuzzy matching.
+ *
+ * Adding 1 byte to query_len ensures that the returned string has
+ * a terminating NULL.
+ */
+ query_len = strlen(query_str) + 1;
+ normalized_query =
+ generate_normalized_query(&jstate, query_str,
+ &query_len,
+ GetDatabaseEncoding());
+
+ /*
+ * find a hint for the normalized query. the result should be in
+ * TopMemoryContext
+ */
+ oldcontext = MemoryContextSwitchTo(TopMemoryContext);
+ current_hint_str =
+ get_hints_from_table(normalized_query, application_name);
+ MemoryContextSwitchTo(oldcontext);
+
+ if (debug_level > 1)
+ {
+ if (current_hint_str)
+ ereport(pg_hint_plan_message_level,
+ (errmsg("pg_hint_plan[qno=0x%x]: "
+ "post_parse_analyze_hook: "
+ "hints from table: \"%s\": "
+ "normalized_query=\"%s\", "
+ "application name =\"%s\"",
+ qno, current_hint_str,
+ normalized_query, application_name),
+ errhidestmt(msgqno != qno)));
+ else
+ ereport(pg_hint_plan_message_level,
+ (errmsg("pg_hint_plan[qno=0x%x]: "
+ "no match found in table: "
+ "application name = \"%s\", "
+ "normalized_query=\"%s\"",
+ qno, application_name,
+ normalized_query),
+ errhidestmt(msgqno != qno)));
+
+ msgqno = qno;
+ }
+ }
+
+ /* retrun if we have hint here */
+ if (current_hint_str)
+ return;
+ }
+ else
+ query_str = get_query_string(pstate, query, NULL);
+
+ if (query_str)
+ {
+ /*
+ * get hints from the comment. However we may have the same query
+ * string with the previous call, but just retrieving hints is expected
+ * to be faster than checking for identicalness before retrieval.
+ */
+ if (current_hint_str)
+ pfree((void *)current_hint_str);
+
+ oldcontext = MemoryContextSwitchTo(TopMemoryContext);
+ current_hint_str = get_hints_from_comment(query_str);
+ MemoryContextSwitchTo(oldcontext);
+ }
+
+ if (debug_level > 1)
+ {
+ if (debug_level == 1 &&
+ (stmt_name || strcmp(query_str, debug_query_string)))
+ ereport(pg_hint_plan_message_level,
+ (errmsg("hints in comment=\"%s\"",
+ current_hint_str ? current_hint_str : "(none)"),
+ errhidestmt(msgqno != qno)));
+ else
+ ereport(pg_hint_plan_message_level,
+ (errmsg("hints in comment=\"%s\", stmt=\"%s\", query=\"%s\", debug_query_string=\"%s\"",
+ current_hint_str ? current_hint_str : "(none)",
+ stmt_name, query_str, debug_query_string),
+ errhidestmt(msgqno != qno)));
+ msgqno = qno;
+ }
+}
+
+/*
+ * Read and set up hint information
+ */