1 /*-------------------------------------------------------------------------
4 * Routines copied from PostgreSQL core distribution with some
7 * src/backend/optimizer/path/joinrels.c
10 * Portions Copyright (c) 2013-2018, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
11 * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
12 * Portions Copyright (c) 1994, Regents of the University of California
14 *-------------------------------------------------------------------------
18 * adjust_rows: tweak estimated row numbers according to the hint.
21 adjust_rows(double rows, RowsHint *hint)
23 double result = 0.0; /* keep compiler quiet */
25 if (hint->value_type == RVT_ABSOLUTE)
27 else if (hint->value_type == RVT_ADD)
28 result = rows + hint->rows;
29 else if (hint->value_type == RVT_SUB)
30 result = rows - hint->rows;
31 else if (hint->value_type == RVT_MULTI)
32 result = rows * hint->rows;
34 Assert(false); /* unrecognized rows value type */
36 hint->base.state = HINT_STATE_USED;
39 (errmsg("Force estimate to be at least one row, to avoid possible divide-by-zero when interpolating costs : %s",
40 hint->base.hint_str)));
41 result = clamp_row_est(result);
42 elog(DEBUG1, "adjusted rows %d to %d", (int) rows, (int) result);
49 * Find or create a join RelOptInfo that represents the join of
50 * the two given rels, and add to it path information for paths
51 * created with the two rels as outer and inner rel.
52 * (The join rel may already contain paths generated from other
53 * pairs of rels that add up to the same set of base rels.)
55 * NB: will return NULL if attempted join is not valid. This can happen
56 * when working with outer joins, or with IN or EXISTS clauses that have been
60 make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
63 SpecialJoinInfo *sjinfo;
65 SpecialJoinInfo sjinfo_data;
69 /* We should never try to join two overlapping sets of rels. */
70 Assert(!bms_overlap(rel1->relids, rel2->relids));
72 /* Construct Relids set that identifies the joinrel. */
73 joinrelids = bms_union(rel1->relids, rel2->relids);
75 /* Check validity and determine join type. */
76 if (!join_is_legal(root, rel1, rel2, joinrelids,
79 /* invalid join path */
84 /* Swap rels if needed to match the join info. */
87 RelOptInfo *trel = rel1;
94 * If it's a plain inner join, then we won't have found anything in
95 * join_info_list. Make up a SpecialJoinInfo so that selectivity
96 * estimation functions will know what's being joined.
100 sjinfo = &sjinfo_data;
101 sjinfo->type = T_SpecialJoinInfo;
102 sjinfo->min_lefthand = rel1->relids;
103 sjinfo->min_righthand = rel2->relids;
104 sjinfo->syn_lefthand = rel1->relids;
105 sjinfo->syn_righthand = rel2->relids;
106 sjinfo->jointype = JOIN_INNER;
107 /* we don't bother trying to make the remaining fields valid */
108 sjinfo->lhs_strict = false;
109 sjinfo->delay_upper_joins = false;
110 sjinfo->semi_can_btree = false;
111 sjinfo->semi_can_hash = false;
112 sjinfo->semi_operators = NIL;
113 sjinfo->semi_rhs_exprs = NIL;
117 * Find or build the join RelOptInfo, and compute the restrictlist that
118 * goes with this particular joining.
120 joinrel = build_join_rel(root, joinrelids, rel1, rel2, sjinfo,
123 /* !!! START: HERE IS THE PART WHICH ADDED FOR PG_HINT_PLAN !!! */
125 RowsHint *rows_hint = NULL;
127 RowsHint *justforme = NULL;
128 RowsHint *domultiply = NULL;
130 /* Search for applicable rows hint for this join node */
131 for (i = 0; i < current_hint_state->num_hints[HINT_TYPE_ROWS]; i++)
133 rows_hint = current_hint_state->rows_hints[i];
136 * Skip this rows_hint if it is invalid from the first or it
137 * doesn't target any join rels.
139 if (!rows_hint->joinrelids ||
140 rows_hint->base.state == HINT_STATE_ERROR)
143 if (bms_equal(joinrelids, rows_hint->joinrelids))
146 * This joinrel is just the target of this rows_hint, so tweak
147 * rows estimation according to the hint.
149 justforme = rows_hint;
151 else if (!(bms_is_subset(rows_hint->joinrelids, rel1->relids) ||
152 bms_is_subset(rows_hint->joinrelids, rel2->relids)) &&
153 bms_is_subset(rows_hint->joinrelids, joinrelids) &&
154 rows_hint->value_type == RVT_MULTI)
157 * If the rows_hint's target relids is not a subset of both of
158 * component rels and is a subset of this joinrel, ths hint's
159 * targets spread over both component rels. This menas that
160 * this hint has been never applied so far and this joinrel is
161 * the first (and only) chance to fire in current join tree.
162 * Only the multiplication hint has the cumulative nature so we
163 * apply only RVT_MULTI in this way.
165 domultiply = rows_hint;
172 * If a hint just for me is found, no other adjust method is
173 * useles, but this cannot be more than twice becuase this joinrel
174 * is already adjusted by this hint.
176 if (justforme->base.state == HINT_STATE_NOTUSED)
177 joinrel->rows = adjust_rows(joinrel->rows, justforme);
184 * If we have multiple routes up to this joinrel which are not
185 * applicable this hint, this multiply hint will applied more
186 * than twice. But there's no means to know of that,
187 * re-estimate the row number of this joinrel always just
188 * before applying the hint. This is a bit different from
189 * normal planner behavior but it doesn't harm so much.
191 set_joinrel_size_estimates(root, joinrel, rel1, rel2, sjinfo,
194 joinrel->rows = adjust_rows(joinrel->rows, domultiply);
199 /* !!! END: HERE IS THE PART WHICH ADDED FOR PG_HINT_PLAN !!! */
202 * If we've already proven this join is empty, we needn't consider any
205 if (is_dummy_rel(joinrel))
207 bms_free(joinrelids);
211 /* Add paths to the join relation. */
212 populate_joinrel_with_paths(root, rel1, rel2, joinrel, sjinfo,
215 bms_free(joinrelids);
221 * populate_joinrel_with_paths
222 * Add paths to the given joinrel for given pair of joining relations. The
223 * SpecialJoinInfo provides details about the join and the restrictlist
224 * contains the join clauses and the other clauses applicable for given pair
225 * of the joining relations.
228 populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1,
229 RelOptInfo *rel2, RelOptInfo *joinrel,
230 SpecialJoinInfo *sjinfo, List *restrictlist)
233 * Consider paths using each rel as both outer and inner. Depending on
234 * the join type, a provably empty outer or inner rel might mean the join
235 * is provably empty too; in which case throw away any previously computed
236 * paths and mark the join as dummy. (We do it this way since it's
237 * conceivable that dummy-ness of a multi-element join might only be
238 * noticeable for certain construction paths.)
240 * Also, a provably constant-false join restriction typically means that
241 * we can skip evaluating one or both sides of the join. We do this by
242 * marking the appropriate rel as dummy. For outer joins, a
243 * constant-false restriction that is pushed down still means the whole
244 * join is dummy, while a non-pushed-down one means that no inner rows
245 * will join so we can treat the inner rel as dummy.
247 * We need only consider the jointypes that appear in join_info_list, plus
250 switch (sjinfo->jointype)
253 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
254 restriction_is_constant_false(restrictlist, joinrel, false))
256 mark_dummy_rel(joinrel);
259 add_paths_to_joinrel(root, joinrel, rel1, rel2,
262 add_paths_to_joinrel(root, joinrel, rel2, rel1,
267 if (is_dummy_rel(rel1) ||
268 restriction_is_constant_false(restrictlist, joinrel, true))
270 mark_dummy_rel(joinrel);
273 if (restriction_is_constant_false(restrictlist, joinrel, false) &&
274 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
275 mark_dummy_rel(rel2);
276 add_paths_to_joinrel(root, joinrel, rel1, rel2,
279 add_paths_to_joinrel(root, joinrel, rel2, rel1,
284 if ((is_dummy_rel(rel1) && is_dummy_rel(rel2)) ||
285 restriction_is_constant_false(restrictlist, joinrel, true))
287 mark_dummy_rel(joinrel);
290 add_paths_to_joinrel(root, joinrel, rel1, rel2,
293 add_paths_to_joinrel(root, joinrel, rel2, rel1,
298 * If there are join quals that aren't mergeable or hashable, we
299 * may not be able to build any valid plan. Complain here so that
300 * we can give a somewhat-useful error message. (Since we have no
301 * flexibility of planning for a full join, there's no chance of
302 * succeeding later with another pair of input rels.)
304 if (joinrel->pathlist == NIL)
306 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
307 errmsg("FULL JOIN is only supported with merge-joinable or hash-joinable join conditions")));
312 * We might have a normal semijoin, or a case where we don't have
313 * enough rels to do the semijoin but can unique-ify the RHS and
314 * then do an innerjoin (see comments in join_is_legal). In the
315 * latter case we can't apply JOIN_SEMI joining.
317 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
318 bms_is_subset(sjinfo->min_righthand, rel2->relids))
320 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
321 restriction_is_constant_false(restrictlist, joinrel, false))
323 mark_dummy_rel(joinrel);
326 add_paths_to_joinrel(root, joinrel, rel1, rel2,
332 * If we know how to unique-ify the RHS and one input rel is
333 * exactly the RHS (not a superset) we can consider unique-ifying
334 * it and then doing a regular join. (The create_unique_path
335 * check here is probably redundant with what join_is_legal did,
336 * but if so the check is cheap because it's cached. So test
337 * anyway to be sure.)
339 if (bms_equal(sjinfo->syn_righthand, rel2->relids) &&
340 create_unique_path(root, rel2, rel2->cheapest_total_path,
343 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
344 restriction_is_constant_false(restrictlist, joinrel, false))
346 mark_dummy_rel(joinrel);
349 add_paths_to_joinrel(root, joinrel, rel1, rel2,
350 JOIN_UNIQUE_INNER, sjinfo,
352 add_paths_to_joinrel(root, joinrel, rel2, rel1,
353 JOIN_UNIQUE_OUTER, sjinfo,
358 if (is_dummy_rel(rel1) ||
359 restriction_is_constant_false(restrictlist, joinrel, true))
361 mark_dummy_rel(joinrel);
364 if (restriction_is_constant_false(restrictlist, joinrel, false) &&
365 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
366 mark_dummy_rel(rel2);
367 add_paths_to_joinrel(root, joinrel, rel1, rel2,
372 /* other values not expected here */
373 elog(ERROR, "unrecognized join type: %d", (int) sjinfo->jointype);
377 /* Apply partitionwise join technique, if possible. */
378 try_partitionwise_join(root, rel1, rel2, joinrel, sjinfo, restrictlist);