* pg_hint_plan.c
* hinting on how to execute a query for PostgreSQL
*
- * Copyright (c) 2012-2018, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
+ * Copyright (c) 2012-2020, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
*
*-------------------------------------------------------------------------
*/
#include <string.h>
#include "postgres.h"
+#include "access/genam.h"
+#include "access/heapam.h"
+#include "access/relation.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_index.h"
#include "commands/prepare.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "nodes/params.h"
-#include "nodes/relation.h"
+#include "optimizer/appendinfo.h"
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
#include "optimizer/geqo.h"
#include "optimizer/joininfo.h"
+#include "optimizer/optimizer.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/plancat.h"
#include "partitioning/partbounds.h"
#include "tcop/utility.h"
#include "utils/builtins.h"
+#include "utils/float.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/rel.h"
int init_min_para_tablescan_size;
/* min_parallel_index_scan_size*/
int init_min_para_indexscan_size;
- int init_paratup_cost; /* parallel_tuple_cost */
- int init_parasetup_cost;/* parallel_setup_cost */
+ double init_paratup_cost; /* parallel_tuple_cost */
+ double init_parasetup_cost;/* parallel_setup_cost */
PlannerInfo *current_root; /* PlannerInfo for the followings */
Index parent_relid; /* inherit parent of table relid */
static void push_hint(HintState *hstate);
static void pop_hint(void);
-static void pg_hint_plan_post_parse_analyze(ParseState *pstate, Query *query);
-static PlannedStmt *pg_hint_plan_planner(Query *parse, int cursorOptions,
+static PlannedStmt *pg_hint_plan_planner(Query *parse, const char *query_string,
+ int cursorOptions,
ParamListInfo boundParams);
static RelOptInfo *pg_hint_plan_join_search(PlannerInfo *root,
int levels_needed,
static void create_plain_partial_paths(PlannerInfo *root,
RelOptInfo *rel);
static void make_rels_by_clause_joins(PlannerInfo *root, RelOptInfo *old_rel,
+ List *other_rels_list,
ListCell *other_rels);
static void make_rels_by_clauseless_joins(PlannerInfo *root,
RelOptInfo *old_rel,
- ListCell *other_rels);
+ List *other_rels);
static bool has_join_restriction(PlannerInfo *root, RelOptInfo *rel);
static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
RangeTblEntry *rte);
-static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
- Index rti, RangeTblEntry *rte);
RelOptInfo *pg_hint_plan_make_join_rel(PlannerInfo *root, RelOptInfo *rel1,
RelOptInfo *rel2);
HintState *state);
static int set_config_int32_option(const char *name, int32 value,
GucContext context);
+static int set_config_double_option(const char *name, double value,
+ GucContext context);
/* GUC variables */
static bool pg_hint_plan_enable_hint = true;
static bool pg_hint_plan_enable_hint_table = false;
static int plpgsql_recurse_level = 0; /* PLpgSQL recursion level */
+static int recurse_level = 0; /* recursion level incl. direct SPI calls */
static int hint_inhibit_level = 0; /* Inhibit hinting if this is above 0 */
/* (This could not be above 1) */
static int max_hint_nworkers = -1; /* Maximum nworkers of Workers hints */
};
/* Saved hook values in case of unload */
-static post_parse_analyze_hook_type prev_post_parse_analyze_hook = NULL;
static planner_hook_type prev_planner = NULL;
static join_search_hook_type prev_join_search = NULL;
static set_rel_pathlist_hook_type prev_set_rel_pathlist = NULL;
NULL);
/* Install hooks. */
- prev_post_parse_analyze_hook = post_parse_analyze_hook;
- post_parse_analyze_hook = pg_hint_plan_post_parse_analyze;
prev_planner = planner_hook;
planner_hook = pg_hint_plan_planner;
prev_join_search = join_search_hook;
PLpgSQL_plugin **var_ptr;
/* Uninstall hooks. */
- post_parse_analyze_hook = prev_post_parse_analyze_hook;
planner_hook = prev_planner;
join_search_hook = prev_join_search;
set_rel_pathlist_hook = prev_set_rel_pathlist;
{
ParallelHint *hint;
- hint = palloc(sizeof(ScanMethodHint));
+ hint = palloc(sizeof(ParallelHint));
hint->base.hint_str = hint_str;
hint->base.keyword = keyword;
hint->base.hint_keyword = hint_keyword;
quote_value(buf, hint->relnames[i]);
}
}
- appendStringInfo(buf, " %s", hint->rows_str);
+ if (hint->rows_str != NULL)
+ appendStringInfo(buf, " %s", hint->rows_str);
appendStringInfoString(buf, ")");
if (!nolf)
appendStringInfoChar(buf, '\n');
}
/*
- * Get client-supplied query string. Addtion to that the jumbled query is
- * supplied if the caller requested. From the restriction of JumbleQuery, some
- * kind of query needs special amendments. Reutrns NULL if this query doesn't
- * change the current hint. This function returns NULL also when something
- * wrong has happend and let the caller continue using the current hints.
- */
-static const char *
-get_query_string(ParseState *pstate, Query *query, Query **jumblequery)
-{
- const char *p = debug_query_string;
-
- /*
- * If debug_query_string is set, it is the top level statement. But in some
- * cases we reach here with debug_query_string set NULL for example in the
- * case of DESCRIBE message handling. We may still see a candidate
- * top-level query in pstate in the case.
- */
- if (!p)
- {
- /* We don't see a query string, return NULL */
- if (!pstate->p_sourcetext)
- return NULL;
-
- p = pstate->p_sourcetext;
- }
-
- if (jumblequery != NULL)
- *jumblequery = query;
-
- if (query->commandType == CMD_UTILITY)
- {
- Query *target_query = (Query *)query->utilityStmt;
-
- /*
- * Some CMD_UTILITY statements have a subquery that we can hint on.
- * Since EXPLAIN can be placed before other kind of utility statements
- * and EXECUTE can be contained other kind of utility statements, these
- * conditions are not mutually exclusive and should be considered in
- * this order.
- */
- if (IsA(target_query, ExplainStmt))
- {
- ExplainStmt *stmt = (ExplainStmt *)target_query;
-
- Assert(IsA(stmt->query, Query));
- target_query = (Query *)stmt->query;
-
- /* strip out the top-level query for further processing */
- if (target_query->commandType == CMD_UTILITY &&
- target_query->utilityStmt != NULL)
- target_query = (Query *)target_query->utilityStmt;
- }
-
- if (IsA(target_query, DeclareCursorStmt))
- {
- DeclareCursorStmt *stmt = (DeclareCursorStmt *)target_query;
- Query *query = (Query *)stmt->query;
-
- /* the target must be CMD_SELECT in this case */
- Assert(IsA(query, Query) && query->commandType == CMD_SELECT);
- target_query = query;
- }
-
- if (IsA(target_query, CreateTableAsStmt))
- {
- CreateTableAsStmt *stmt = (CreateTableAsStmt *) target_query;
-
- Assert(IsA(stmt->query, Query));
- target_query = (Query *) stmt->query;
-
- /* strip out the top-level query for further processing */
- if (target_query->commandType == CMD_UTILITY &&
- target_query->utilityStmt != NULL)
- target_query = (Query *)target_query->utilityStmt;
- }
-
- if (IsA(target_query, ExecuteStmt))
- {
- /*
- * Use the prepared query for EXECUTE. The Query for jumble
- * also replaced with the corresponding one.
- */
- ExecuteStmt *stmt = (ExecuteStmt *)target_query;
- PreparedStatement *entry;
-
- entry = FetchPreparedStatement(stmt->name, true);
- p = entry->plansource->query_string;
- target_query = (Query *) linitial (entry->plansource->query_list);
- }
-
- /* JumbleQuery accespts only a non-utility Query */
- if (!IsA(target_query, Query) ||
- target_query->utilityStmt != NULL)
- target_query = NULL;
-
- if (jumblequery)
- *jumblequery = target_query;
- }
- /* Return NULL if the pstate is not identical to the top-level query */
- else if (strcmp(pstate->p_sourcetext, p) != 0)
- p = NULL;
-
- return p;
-}
-
-/*
* Get hints from the head block comment in client-supplied query string.
*/
static const char *
hstate->num_hints[HINT_TYPE_LEADING]);
hstate->rows_hints = (RowsHint **) (hstate->set_hints +
hstate->num_hints[HINT_TYPE_SET]);
- hstate->parallel_hints = (ParallelHint **) (hstate->set_hints +
+ hstate->parallel_hints = (ParallelHint **) (hstate->rows_hints +
hstate->num_hints[HINT_TYPE_ROWS]);
return hstate;
List *name_list = NIL;
char *rows_str;
char *end_ptr;
+ ListCell *l;
+ int i = 0;
if ((str = parse_parentheses(str, &name_list, hint_keyword)) == NULL)
return NULL;
/* Last element must be rows specification */
hint->nrels = list_length(name_list) - 1;
- if (hint->nrels > 0)
+ if (hint->nrels < 1)
{
- ListCell *l;
- int i = 0;
+ hint_ereport(str,
+ ("%s hint needs at least one relation followed by one correction term.",
+ hint->base.keyword));
+ hint->base.state = HINT_STATE_ERROR;
- /*
- * Transform relation names from list to array to sort them with qsort
- * after.
- */
- hint->relnames = palloc(sizeof(char *) * hint->nrels);
- foreach (l, name_list)
- {
- if (hint->nrels <= i)
- break;
- hint->relnames[i] = lfirst(l);
- i++;
- }
+ return str;
+ }
+
+
+ /*
+ * Transform relation names from list to array to sort them with qsort
+ * after.
+ */
+ hint->relnames = palloc(sizeof(char *) * hint->nrels);
+ foreach (l, name_list)
+ {
+ if (hint->nrels <= i)
+ break;
+ hint->relnames[i] = lfirst(l);
+ i++;
}
/* Retieve rows estimation */
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)
/* force means that enforce parallel as far as possible */
if (hint && hint->force_parallel && hint->nworkers > 0)
{
- set_config_int32_option("parallel_tuple_cost", 0, state->context);
- set_config_int32_option("parallel_setup_cost", 0, state->context);
+ set_config_double_option("parallel_tuple_cost", 0.0, state->context);
+ set_config_double_option("parallel_setup_cost", 0.0, state->context);
set_config_int32_option("min_parallel_table_scan_size", 0,
state->context);
set_config_int32_option("min_parallel_index_scan_size", 0,
}
else
{
- set_config_int32_option("parallel_tuple_cost",
+ set_config_double_option("parallel_tuple_cost",
state->init_paratup_cost, state->context);
- set_config_int32_option("parallel_setup_cost",
+ set_config_double_option("parallel_setup_cost",
state->init_parasetup_cost, state->context);
set_config_int32_option("min_parallel_table_scan_size",
state->init_min_para_tablescan_size,
SET_CONFIG_OPTION("enable_nestloop", ENABLE_NESTLOOP);
SET_CONFIG_OPTION("enable_mergejoin", ENABLE_MERGEJOIN);
SET_CONFIG_OPTION("enable_hashjoin", ENABLE_HASHJOIN);
+
+ /*
+ * Hash join may be rejected for the reason of estimated memory usage. Try
+ * getting rid of that limitation.
+ */
+ if (enforce_mask == ENABLE_HASHJOIN)
+ {
+ char buf[32];
+ int new_multipler;
+
+ /* See final_cost_hashjoin(). */
+ new_multipler = MAX_KILOBYTES / work_mem;
+
+ /* See guc.c for the upper limit */
+ if (new_multipler >= 1000)
+ new_multipler = 1000;
+
+ if (new_multipler > hash_mem_multiplier)
+ {
+ snprintf(buf, sizeof(buf), UINT64_FORMAT, (uint64)new_multipler);
+ set_config_option_noerror("hash_mem_multiplier", buf,
+ context, PGC_S_SESSION, GUC_ACTION_SAVE,
+ true, ERROR);
+ }
+ }
}
/*
}
/*
- * 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.
+ * Retrieve and store hint string from given query or from the hint table.
*/
static void
-pg_hint_plan_post_parse_analyze(ParseState *pstate, Query *query)
+get_current_hint_string(Query *query, const char *query_str)
{
- 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 */
+ /* do nothing while scanning hint table */
if (hint_inhibit_level > 0)
return;
- if (!pg_hint_plan_enable_hint)
+ /* Make sure trashing old hint string */
+ if (current_hint_str)
{
- if (current_hint_str)
- {
- pfree((void *)current_hint_str);
- current_hint_str = NULL;
- }
- return;
+ pfree((void *)current_hint_str);
+ current_hint_str = NULL;
}
+ /* Return if nothing to do. */
+ if (!pg_hint_plan_enable_hint || !query_str)
+ return;
+
/* increment the query number */
qnostr[0] = 0;
if (debug_level > 1)
/* search the hint table for a hint if requested */
if (pg_hint_plan_enable_hint_table)
{
+ JumbleState *jstate;
int query_len;
- pgssJumbleState jstate;
- Query *jumblequery;
- char *normalized_query = NULL;
+ char *normalized_query;
- query_str = get_query_string(pstate, query, &jumblequery);
+ jstate = JumbleQuery(query, query_str);
- /* 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->stmt_location,
- &query_len,
- GetDatabaseEncoding());
+ /*
+ * 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, 0, &query_len);
- /*
- * 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);
+ /*
+ * 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_debug_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),
- errhidecontext(msgqno != qno)));
- else
- ereport(pg_hint_plan_debug_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),
- errhidecontext(msgqno != qno)));
-
- msgqno = qno;
- }
+ if (debug_level > 1)
+ {
+ if (current_hint_str)
+ ereport(pg_hint_plan_debug_message_level,
+ (errmsg("pg_hint_plan[qno=0x%x]: "
+ "hints from table: \"%s\": "
+ "normalized_query=\"%s\", "
+ "application name =\"%s\"",
+ qno, current_hint_str,
+ normalized_query, application_name),
+ errhidestmt(msgqno != qno),
+ errhidecontext(msgqno != qno)));
+ else
+ ereport(pg_hint_plan_debug_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),
+ errhidecontext(msgqno != qno)));
+
+ msgqno = qno;
}
- /* retrun if we have hint here */
+ /* retrun if we have hint string 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);
- }
+ /* get hints from the comment */
+ oldcontext = MemoryContextSwitchTo(TopMemoryContext);
+ current_hint_str = get_hints_from_comment(query_str);
+ MemoryContextSwitchTo(oldcontext);
if (debug_level > 1)
{
* Read and set up hint information
*/
static PlannedStmt *
-pg_hint_plan_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
+pg_hint_plan_planner(Query *parse, const char *query_string, int cursorOptions, ParamListInfo boundParams)
{
int save_nestlevel;
PlannedStmt *result;
HintState *hstate;
+ const char *prev_hint_str = NULL;
/*
* Use standard planner if pg_hint_plan is disabled or current nesting
goto standard_planner_proc;
}
- /*
- * Support for nested plpgsql functions. This is quite ugly but this is the
- * only point I could find where I can get the query string.
- */
- if (plpgsql_recurse_level > 0)
+ /* always retrieve hint from the top-level query string */
+ if (plpgsql_recurse_level == 0 && current_hint_str)
{
- MemoryContext oldcontext;
-
- if (current_hint_str)
- pfree((void *)current_hint_str);
-
- oldcontext = MemoryContextSwitchTo(TopMemoryContext);
- current_hint_str =
- get_hints_from_comment((char *)error_context_stack->arg);
- MemoryContextSwitchTo(oldcontext);
+ pfree((void *)current_hint_str);
+ current_hint_str = NULL;
}
+ get_current_hint_string(parse, query_string);
+
+ /* No hint, go the normal way */
if (!current_hint_str)
goto standard_planner_proc;
/* parse the hint into hint state struct */
hstate = create_hintstate(parse, pstrdup(current_hint_str));
- /* run standard planner if the statement has not valid hint */
+ /* run standard planner if we're given with no valid hints */
if (!hstate)
+ {
+ /* forget invalid hint string */
+ if (current_hint_str)
+ {
+ pfree((void *)current_hint_str);
+ current_hint_str = NULL;
+ }
+
goto standard_planner_proc;
+ }
/*
* Push new hint struct to the hint stack to disable previous hint context.
}
/*
+ * The planner call below may replace current_hint_str. Store and restore
+ * it so that the subsequent planning in the upper level doesn't get
+ * confused.
+ */
+ recurse_level++;
+ prev_hint_str = current_hint_str;
+ current_hint_str = NULL;
+
+ /*
* Use PG_TRY mechanism to recover GUC parameters and current_hint_state to
* the state when this planner started when error occurred in planner.
*/
PG_TRY();
{
if (prev_planner)
- result = (*prev_planner) (parse, cursorOptions, boundParams);
+ result = (*prev_planner) (parse, query_string,
+ cursorOptions, boundParams);
else
- result = standard_planner(parse, cursorOptions, boundParams);
+ result = standard_planner(parse, query_string,
+ cursorOptions, boundParams);
+
+ current_hint_str = prev_hint_str;
+ recurse_level--;
}
PG_CATCH();
{
* Rollback changes of GUC parameters, and pop current hint context
* from hint stack to rewind the state.
*/
+ current_hint_str = prev_hint_str;
+ recurse_level--;
AtEOXact_GUC(true, save_nestlevel);
pop_hint();
PG_RE_THROW();
}
PG_END_TRY();
+
+ /*
+ * current_hint_str is useless after planning of the top-level query.
+ */
+ if (recurse_level < 1 && current_hint_str)
+ {
+ pfree((void *)current_hint_str);
+ current_hint_str = NULL;
+ }
+
/* Print hint in debug mode. */
if (debug_level == 1)
HintStateDump(current_hint_state);
}
current_hint_state = NULL;
if (prev_planner)
- return (*prev_planner) (parse, cursorOptions, boundParams);
+ result = (*prev_planner) (parse, query_string,
+ cursorOptions, boundParams);
else
- return standard_planner(parse, cursorOptions, boundParams);
+ result = standard_planner(parse, query_string,
+ cursorOptions, boundParams);
+
+ /* The upper-level planner still needs the current hint state */
+ if (HintStateStack != NIL)
+ current_hint_state = (HintState *) lfirst(list_head(HintStateStack));
+
+ return result;
}
/*
bool using_parent_hint)
{
ListCell *cell;
- ListCell *prev;
ListCell *next;
StringInfoData buf;
RangeTblEntry *rte = root->simple_rte_array[rel->relid];
* Leaving only an specified index, we delete it from a IndexOptInfo list
* other than it.
*/
- prev = NULL;
if (debug_level > 0)
initStringInfo(&buf);
ListCell *l;
bool use_index = false;
- next = lnext(cell);
-
+ next = lnext(rel->indexlist, cell);
foreach(l, hint->indexnames)
{
char *hintname = (char *) lfirst(l);
}
if (!use_index)
- rel->indexlist = list_delete_cell(rel->indexlist, cell, prev);
- else
- prev = cell;
+ {
+ rel->indexlist = list_delete_cell(rel->indexlist, cell);
+ /*
+ * The cells after the deleted cell have been moved towards the
+ * list head by 1 element. the next iteration should visit the
+ * cell at the same address if any.
+ */
+ if (next)
+ next = cell;
+ }
+
pfree(indexname);
}
return 0;
}
- /* Forget about the parent of another subquery */
- if (root != current_hint_state->current_root)
- current_hint_state->parent_relid = 0;
-
- /* Find the parent for this relation other than the registered parent */
- foreach (l, root->append_rel_list)
+ if (bms_num_members(rel->top_parent_relids) == 1)
{
- AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
-
- if (appinfo->child_relid == rel->relid)
- {
- if (current_hint_state->parent_relid != appinfo->parent_relid)
- {
- new_parent_relid = appinfo->parent_relid;
- current_hint_state->current_root = root;
- }
- break;
- }
+ new_parent_relid = bms_next_member(rel->top_parent_relids, -1);
+ current_hint_state->current_root = root;
+ Assert(new_parent_relid > 0);
}
-
- if (!l)
+ else
{
/* This relation doesn't have a parent. Cancel current_hint_state. */
current_hint_state->parent_relid = 0;
parentrel_oid =
root->simple_rte_array[current_hint_state->parent_relid]->relid;
- parent_rel = heap_open(parentrel_oid, NoLock);
+ parent_rel = table_open(parentrel_oid, NoLock);
/* Search the parent relation for indexes match the hint spec */
foreach(l, RelationGetIndexList(parent_rel))
lappend(current_hint_state->parent_index_infos,
parent_index_info);
}
- heap_close(parent_rel, NoLock);
+ table_close(parent_rel, NoLock);
}
}
}
leading_hint->base.hint_str));
}
- outer_rels = lfirst(outer_inner->outer_inner_pair->head);
- inner_rels = lfirst(outer_inner->outer_inner_pair->tail);
+ outer_rels = linitial(outer_inner->outer_inner_pair);
+ inner_rels = llast(outer_inner->outer_inner_pair);
outer_relids = OuterInnerJoinCreate(outer_rels,
leading_hint,
{
if (hstate->join_hint_level[i] != NIL)
{
- ListCell *prev = NULL;
ListCell *next = NULL;
for(l = list_head(hstate->join_hint_level[i]); l; l = next)
{
JoinMethodHint *hint = (JoinMethodHint *)lfirst(l);
- next = lnext(l);
+ next = lnext(hstate->join_hint_level[i], l);
if (hint->inner_nrels == 0 &&
!(bms_intersect(hint->joinrelids, joinrelids) == NULL ||
hint->joinrelids)))
{
hstate->join_hint_level[i] =
- list_delete_cell(hstate->join_hint_level[i], l,
- prev);
+ list_delete_cell(hstate->join_hint_level[i], l);
+ /*
+ * The cells after the deleted cell have been moved
+ * towards the list head by 1 element. the next
+ * iteration should visit the cell at the same address
+ * if any.
+ */
+ if (next)
+ next = l;
}
- else
- prev = l;
}
}
}
}
/* Generate gather paths */
- if (rel->reloptkind == RELOPT_BASEREL)
+ if (rel->reloptkind == RELOPT_BASEREL &&
+ bms_membership(root->all_baserels) != BMS_SINGLETON)
generate_gather_paths(root, rel, false);
}
}
}
/*
- * set_rel_pathlist
- * Build access paths for a base relation
- *
- * This function was copied and edited from set_rel_pathlist() in
- * src/backend/optimizer/path/allpaths.c in order not to copy other static
- * functions not required here.
- */
-static void
-set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
- Index rti, RangeTblEntry *rte)
-{
- if (IS_DUMMY_REL(rel))
- {
- /* We already proved the relation empty, so nothing more to do */
- }
- else if (rte->inh)
- {
- /* It's an "append relation", process accordingly */
- set_append_rel_pathlist(root, rel, rti, rte);
- }
- else
- {
- if (rel->rtekind == RTE_RELATION)
- {
- if (rte->relkind == RELKIND_RELATION)
- {
- if(rte->tablesample != NULL)
- elog(ERROR, "sampled relation is not supported");
-
- /* Plain relation */
- set_plain_rel_pathlist(root, rel, rte);
- }
- else
- elog(ERROR, "unexpected relkind: %c", rte->relkind);
- }
- else
- elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
- }
-
- /*
- * Allow a plugin to editorialize on the set of Paths for this base
- * relation. It could add new paths (such as CustomPaths) by calling
- * add_path(), or delete or modify paths added by the core code.
- */
- if (set_rel_pathlist_hook)
- (*set_rel_pathlist_hook) (root, rel, rti, rte);
-
- /* Now find the cheapest of the paths for this rel */
- set_cheapest(rel);
-}
-
-/*
* stmt_beg callback is called when each query in PL/pgSQL function is about
* to be executed. At that timing, we save query string in the global variable
* plpgsql_query_string to use it in planner hook. It's safe to use one global
plpgsql_recurse_level = 0;
}
+
+/* include core static functions */
+static void populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1,
+ RelOptInfo *rel2, RelOptInfo *joinrel,
+ SpecialJoinInfo *sjinfo, List *restrictlist);
+static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
+ Index rti, RangeTblEntry *rte);
+
#define standard_join_search pg_hint_plan_standard_join_search
#define join_search_one_level pg_hint_plan_join_search_one_level
#define make_join_rel make_join_rel_wrapper