+ if (hint->nrels <= i)
+ break;
+ hint->relnames[i] = lfirst(l);
+ i++;
+ }
+
+ /* Retieve rows estimation */
+ rows_str = list_nth(name_list, hint->nrels);
+ hint->rows_str = rows_str; /* store as-is for error logging */
+ if (rows_str[0] == '#')
+ {
+ hint->value_type = RVT_ABSOLUTE;
+ rows_str++;
+ }
+ else if (rows_str[0] == '+')
+ {
+ hint->value_type = RVT_ADD;
+ rows_str++;
+ }
+ else if (rows_str[0] == '-')
+ {
+ hint->value_type = RVT_SUB;
+ rows_str++;
+ }
+ else if (rows_str[0] == '*')
+ {
+ hint->value_type = RVT_MULTI;
+ rows_str++;
+ }
+ else
+ {
+ hint_ereport(rows_str, ("Unrecognized rows value type notation."));
+ hint->base.state = HINT_STATE_ERROR;
+ return str;
+ }
+ hint->rows = strtod(rows_str, &end_ptr);
+ if (*end_ptr)
+ {
+ hint_ereport(rows_str,
+ ("%s hint requires valid number as rows estimation.",
+ hint->base.keyword));
+ hint->base.state = HINT_STATE_ERROR;
+ return str;
+ }
+
+ /* A join hint requires at least two relations */
+ if (hint->nrels < 2)
+ {
+ hint_ereport(str,
+ ("%s hint requires at least two relations.",
+ hint->base.keyword));
+ hint->base.state = HINT_STATE_ERROR;
+ return str;
+ }
+
+ list_free(name_list);
+
+ /* Sort relnames in alphabetical order. */
+ qsort(hint->relnames, hint->nrels, sizeof(char *), RelnameCmp);
+
+ return str;
+}
+
+static const char *
+ParallelHintParse(ParallelHint *hint, HintState *hstate, Query *parse,
+ const char *str)
+{
+ HintKeyword hint_keyword = hint->base.hint_keyword;
+ List *name_list = NIL;
+ int length;
+ char *end_ptr;
+ int nworkers;
+ bool force_parallel = false;
+
+ if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
+ return NULL;
+
+ /* Parse relation name and index name(s) if given hint accepts. */
+ length = list_length(name_list);
+
+ if (length < 2 || length > 3)
+ {
+ hint_ereport(")",
+ ("wrong number of arguments (%d): %s",
+ length, hint->base.keyword));
+ hint->base.state = HINT_STATE_ERROR;
+ return str;
+ }
+
+ hint->relname = linitial(name_list);
+
+ /* The second parameter is number of workers */
+ hint->nworkers_str = list_nth(name_list, 1);
+ nworkers = strtod(hint->nworkers_str, &end_ptr);
+ if (*end_ptr || nworkers < 0 || nworkers > max_worker_processes)
+ {
+ if (*end_ptr)
+ hint_ereport(hint->nworkers_str,
+ ("number of workers must be a number: %s",
+ hint->base.keyword));
+ else if (nworkers < 0)
+ hint_ereport(hint->nworkers_str,
+ ("number of workers must be greater than zero: %s",
+ hint->base.keyword));
+ else if (nworkers > max_worker_processes)
+ hint_ereport(hint->nworkers_str,
+ ("number of workers = %d is larger than max_worker_processes(%d): %s",
+ nworkers, max_worker_processes, hint->base.keyword));
+
+ hint->base.state = HINT_STATE_ERROR;
+ }
+
+ hint->nworkers = nworkers;
+
+ /* optional third parameter is specified */
+ if (length == 3)
+ {
+ const char *modeparam = (const char *)list_nth(name_list, 2);
+ if (pg_strcasecmp(modeparam, "hard") == 0)
+ force_parallel = true;
+ else if (pg_strcasecmp(modeparam, "soft") != 0)
+ {
+ hint_ereport(modeparam,
+ ("enforcement must be soft or hard: %s",
+ hint->base.keyword));
+ hint->base.state = HINT_STATE_ERROR;
+ }
+ }
+
+ hint->force_parallel = force_parallel;
+
+ if (hint->base.state != HINT_STATE_ERROR &&
+ nworkers > max_hint_nworkers)
+ max_hint_nworkers = nworkers;
+
+ return str;
+}
+
+/*
+ * set GUC parameter functions
+ */
+
+static int
+get_current_scan_mask()
+{
+ int mask = 0;
+
+ if (enable_seqscan)
+ mask |= ENABLE_SEQSCAN;
+ if (enable_indexscan)
+ mask |= ENABLE_INDEXSCAN;
+ if (enable_bitmapscan)
+ mask |= ENABLE_BITMAPSCAN;
+ if (enable_tidscan)
+ mask |= ENABLE_TIDSCAN;
+ if (enable_indexonlyscan)
+ mask |= ENABLE_INDEXONLYSCAN;
+
+ return mask;
+}
+
+static int
+get_current_join_mask()
+{
+ int mask = 0;
+
+ if (enable_nestloop)
+ mask |= ENABLE_NESTLOOP;
+ if (enable_mergejoin)
+ mask |= ENABLE_MERGEJOIN;
+ if (enable_hashjoin)
+ mask |= ENABLE_HASHJOIN;
+ if (enable_memoize)
+ mask |= ENABLE_MEMOIZE;
+
+ return mask;
+}
+
+/*
+ * Sets GUC prameters without throwing exception. Reutrns false if something
+ * wrong.
+ */
+static int
+set_config_option_noerror(const char *name, const char *value,
+ GucContext context, GucSource source,
+ GucAction action, bool changeVal, int elevel)
+{
+ int result = 0;
+ MemoryContext ccxt = CurrentMemoryContext;
+
+ PG_TRY();
+ {
+ result = set_config_option(name, value, context, source,
+ action, changeVal, 0, false);
+ }
+ PG_CATCH();
+ {
+ ErrorData *errdata;
+
+ /* Save error info */
+ MemoryContextSwitchTo(ccxt);
+ errdata = CopyErrorData();
+ FlushErrorState();
+
+ ereport(elevel,
+ (errcode(errdata->sqlerrcode),
+ errmsg("%s", errdata->message),
+ errdata->detail ? errdetail("%s", errdata->detail) : 0,
+ errdata->hint ? errhint("%s", errdata->hint) : 0));
+ msgqno = qno;
+ FreeErrorData(errdata);
+ }
+ PG_END_TRY();
+
+ return result;
+}
+
+/*
+ * Sets GUC parameter of int32 type without throwing exceptions. Returns false
+ * if something wrong.
+ */
+static int
+set_config_int32_option(const char *name, int32 value, GucContext context)
+{
+ char buf[16]; /* enough for int32 */
+
+ if (snprintf(buf, 16, "%d", value) < 0)
+ {
+ ereport(pg_hint_plan_parse_message_level,
+ (errmsg ("Failed to convert integer to string: %d", value)));
+ return false;
+ }
+
+ return
+ set_config_option_noerror(name, buf, context,
+ PGC_S_SESSION, GUC_ACTION_SAVE, true,
+ pg_hint_plan_parse_message_level);
+}
+
+/*
+ * Sets GUC parameter of double type without throwing exceptions. Returns false
+ * if something wrong.
+ */
+static int
+set_config_double_option(const char *name, double value, GucContext context)
+{
+ char *buf = float8out_internal(value);
+ int result;
+
+ result = set_config_option_noerror(name, buf, context,
+ PGC_S_SESSION, GUC_ACTION_SAVE, true,
+ pg_hint_plan_parse_message_level);
+ pfree(buf);
+ return result;
+}
+
+/* setup scan method enforcement according to given options */
+static void
+setup_guc_enforcement(SetHint **options, int noptions, GucContext context)
+{
+ int i;
+
+ for (i = 0; i < noptions; i++)
+ {
+ SetHint *hint = options[i];
+ int result;