Skip to content

Commit 6a1f91a

Browse files
committed
Refresh upstream code copy with update_copied_funcs.pl
This is required for the next release, to be in sync with the planner code pg_hint_plan depends on. Backpatch-through: 17
1 parent 3c0464f commit 6a1f91a

File tree

4 files changed

+81
-66
lines changed

4 files changed

+81
-66
lines changed

core.c

+75-46
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
169169
/*
170170
* Except for the topmost scan/join rel, consider gathering
171171
* partial paths. We'll do the same for the topmost scan/join rel
172-
* once we know the final targetlist (see grouping_planner).
172+
* once we know the final targetlist (see grouping_planner's and
173+
* its call to apply_scanjoin_target_to_paths).
173174
*/
174175
if (!bms_equal(rel->relids, root->all_query_rels))
175176
generate_useful_gather_paths(root, rel, false);
@@ -178,7 +179,7 @@ standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
178179
set_cheapest(rel);
179180

180181
#ifdef OPTIMIZER_DEBUG
181-
debug_print_rel(root, rel);
182+
pprint(rel);
182183
#endif
183184
}
184185
}
@@ -258,6 +259,8 @@ join_search_one_level(PlannerInfo *root, int level)
258259
if (old_rel->joininfo != NIL || old_rel->has_eclass_joins ||
259260
has_join_restriction(root, old_rel))
260261
{
262+
int first_rel;
263+
261264
/*
262265
* There are join clauses or join order restrictions relevant to
263266
* this rel, so consider joins between this rel and (only) those
@@ -271,24 +274,12 @@ join_search_one_level(PlannerInfo *root, int level)
271274
* to each initial rel they don't already include but have a join
272275
* clause or restriction with.
273276
*/
274-
List *other_rels_list;
275-
ListCell *other_rels;
276-
277277
if (level == 2) /* consider remaining initial rels */
278-
{
279-
other_rels_list = joinrels[level - 1];
280-
other_rels = lnext(other_rels_list, r);
281-
}
282-
else /* consider all initial rels */
283-
{
284-
other_rels_list = joinrels[1];
285-
other_rels = list_head(other_rels_list);
286-
}
278+
first_rel = foreach_current_index(r) + 1;
279+
else
280+
first_rel = 0;
287281

288-
make_rels_by_clause_joins(root,
289-
old_rel,
290-
other_rels_list,
291-
other_rels);
282+
make_rels_by_clause_joins(root, old_rel, joinrels[1], first_rel);
292283
}
293284
else
294285
{
@@ -332,8 +323,7 @@ join_search_one_level(PlannerInfo *root, int level)
332323
foreach(r, joinrels[k])
333324
{
334325
RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
335-
List *other_rels_list;
336-
ListCell *other_rels;
326+
int first_rel;
337327
ListCell *r2;
338328

339329
/*
@@ -345,19 +335,12 @@ join_search_one_level(PlannerInfo *root, int level)
345335
!has_join_restriction(root, old_rel))
346336
continue;
347337

348-
if (k == other_level)
349-
{
350-
/* only consider remaining rels */
351-
other_rels_list = joinrels[k];
352-
other_rels = lnext(other_rels_list, r);
353-
}
338+
if (k == other_level) /* only consider remaining rels */
339+
first_rel = foreach_current_index(r) + 1;
354340
else
355-
{
356-
other_rels_list = joinrels[other_level];
357-
other_rels = list_head(other_rels_list);
358-
}
341+
first_rel = 0;
359342

360-
for_each_cell(r2, other_rels_list, other_rels)
343+
for_each_from(r2, joinrels[other_level], first_rel)
361344
{
362345
RelOptInfo *new_rel = (RelOptInfo *) lfirst(r2);
363346

@@ -452,22 +435,21 @@ join_search_one_level(PlannerInfo *root, int level)
452435
* automatically ensures that each new joinrel is only added to the list once.
453436
*
454437
* 'old_rel' is the relation entry for the relation to be joined
455-
* 'other_rels_list': a list containing the other
456-
* rels to be considered for joining
457-
* 'other_rels': the first cell to be considered
438+
* 'other_rels': a list containing the other rels to be considered for joining
439+
* 'first_rel_idx': the first rel to be considered in 'other_rels'
458440
*
459441
* Currently, this is only used with initial rels in other_rels, but it
460442
* will work for joining to joinrels too.
461443
*/
462444
static void
463445
make_rels_by_clause_joins(PlannerInfo *root,
464446
RelOptInfo *old_rel,
465-
List *other_rels_list,
466-
ListCell *other_rels)
447+
List *other_rels,
448+
int first_rel_idx)
467449
{
468450
ListCell *l;
469451

470-
for_each_cell(l, other_rels_list, other_rels)
452+
for_each_from(l, other_rels, first_rel_idx)
471453
{
472454
RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
473455

@@ -942,6 +924,9 @@ restriction_is_constant_false(List *restrictlist,
942924
* Construct the SpecialJoinInfo for a child-join by translating
943925
* SpecialJoinInfo for the join between parents. left_relids and right_relids
944926
* are the relids of left and right side of the join respectively.
927+
*
928+
* If translations are added to or removed from this function, consider
929+
* updating free_child_join_sjinfo() accordingly.
945930
*/
946931
static SpecialJoinInfo *
947932
build_child_join_sjinfo(PlannerInfo *root, SpecialJoinInfo *parent_sjinfo,
@@ -953,6 +938,14 @@ build_child_join_sjinfo(PlannerInfo *root, SpecialJoinInfo *parent_sjinfo,
953938
AppendRelInfo **right_appinfos;
954939
int right_nappinfos;
955940

941+
/* Dummy SpecialJoinInfos can be created without any translation. */
942+
if (parent_sjinfo->jointype == JOIN_INNER)
943+
{
944+
Assert(parent_sjinfo->ojrelid == 0);
945+
init_dummy_sjinfo(sjinfo, left_relids, right_relids);
946+
return sjinfo;
947+
}
948+
956949
memcpy(sjinfo, parent_sjinfo, sizeof(SpecialJoinInfo));
957950
left_appinfos = find_appinfos_by_relids(root, left_relids,
958951
&left_nappinfos);
@@ -1260,7 +1253,6 @@ try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
12601253
SpecialJoinInfo *child_sjinfo;
12611254
List *child_restrictlist;
12621255
RelOptInfo *child_joinrel;
1263-
Relids child_joinrelids;
12641256
AppendRelInfo **appinfos;
12651257
int nappinfos;
12661258

@@ -1357,13 +1349,11 @@ try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
13571349
child_rel1->relids,
13581350
child_rel2->relids);
13591351

1360-
/* Build correct join relids for child join */
1361-
child_joinrelids = bms_union(child_rel1->relids, child_rel2->relids);
1362-
child_joinrelids = add_outer_joins_to_relids(root, child_joinrelids,
1363-
child_sjinfo, NULL);
1364-
13651352
/* Find the AppendRelInfo structures */
1366-
appinfos = find_appinfos_by_relids(root, child_joinrelids, &nappinfos);
1353+
appinfos = find_appinfos_by_relids(root,
1354+
bms_union(child_rel1->relids,
1355+
child_rel2->relids),
1356+
&nappinfos);
13671357

13681358
/*
13691359
* Construct restrictions applicable to the child join from those
@@ -1373,8 +1363,8 @@ try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
13731363
(List *) adjust_appendrel_attrs(root,
13741364
(Node *) parent_restrictlist,
13751365
nappinfos, appinfos);
1376-
pfree(appinfos);
13771366

1367+
/* Find or construct the child join's RelOptInfo */
13781368
child_joinrel = joinrel->part_rels[cnt_parts];
13791369
if (!child_joinrel)
13801370
{
@@ -1387,10 +1377,49 @@ try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
13871377
child_joinrel->relids);
13881378
}
13891379

1390-
Assert(bms_equal(child_joinrel->relids, child_joinrelids));
1380+
/* Assert we got the right one */
1381+
Assert(bms_equal(child_joinrel->relids,
1382+
adjust_child_relids(joinrel->relids,
1383+
nappinfos, appinfos)));
13911384

1385+
/* And make paths for the child join */
13921386
populate_joinrel_with_paths(root, child_rel1, child_rel2,
13931387
child_joinrel, child_sjinfo,
13941388
child_restrictlist);
1389+
1390+
pfree(appinfos);
1391+
free_child_join_sjinfo(child_sjinfo);
1392+
}
1393+
}
1394+
1395+
1396+
/*
1397+
* free_child_join_sjinfo
1398+
* Free memory consumed by a SpecialJoinInfo created by
1399+
* build_child_join_sjinfo()
1400+
*
1401+
* Only members that are translated copies of their counterpart in the parent
1402+
* SpecialJoinInfo are freed here.
1403+
*/
1404+
static void
1405+
free_child_join_sjinfo(SpecialJoinInfo *sjinfo)
1406+
{
1407+
/*
1408+
* Dummy SpecialJoinInfos of inner joins do not have any translated fields
1409+
* and hence no fields that to be freed.
1410+
*/
1411+
if (sjinfo->jointype != JOIN_INNER)
1412+
{
1413+
bms_free(sjinfo->min_lefthand);
1414+
bms_free(sjinfo->min_righthand);
1415+
bms_free(sjinfo->syn_lefthand);
1416+
bms_free(sjinfo->syn_righthand);
1417+
1418+
/*
1419+
* semi_rhs_exprs may in principle be freed, but a simple pfree() does
1420+
* not suffice, so we leave it alone.
1421+
*/
13951422
}
1423+
1424+
pfree(sjinfo);
13961425
}

make_join_rel.c

+1-17
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,7 @@ make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
112112
if (sjinfo == NULL)
113113
{
114114
sjinfo = &sjinfo_data;
115-
sjinfo->type = T_SpecialJoinInfo;
116-
sjinfo->min_lefthand = rel1->relids;
117-
sjinfo->min_righthand = rel2->relids;
118-
sjinfo->syn_lefthand = rel1->relids;
119-
sjinfo->syn_righthand = rel2->relids;
120-
sjinfo->jointype = JOIN_INNER;
121-
sjinfo->ojrelid = 0;
122-
sjinfo->commute_above_l = NULL;
123-
sjinfo->commute_above_r = NULL;
124-
sjinfo->commute_below_l = NULL;
125-
sjinfo->commute_below_r = NULL;
126-
/* we don't bother trying to make the remaining fields valid */
127-
sjinfo->lhs_strict = false;
128-
sjinfo->semi_can_btree = false;
129-
sjinfo->semi_can_hash = false;
130-
sjinfo->semi_operators = NIL;
131-
sjinfo->semi_rhs_exprs = NIL;
115+
init_dummy_sjinfo(sjinfo, rel1->relids, rel2->relids);
132116
}
133117

134118
/*

pg_hint_plan.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -493,14 +493,15 @@ void pg_hint_plan_set_rel_pathlist(PlannerInfo * root, RelOptInfo *rel,
493493
static void create_plain_partial_paths(PlannerInfo *root,
494494
RelOptInfo *rel);
495495
static void make_rels_by_clause_joins(PlannerInfo *root, RelOptInfo *old_rel,
496-
List *other_rels_list,
497-
ListCell *other_rels);
496+
List *other_rels,
497+
int first_rel_idx);
498498
static void make_rels_by_clauseless_joins(PlannerInfo *root,
499499
RelOptInfo *old_rel,
500500
List *other_rels);
501501
static bool has_join_restriction(PlannerInfo *root, RelOptInfo *rel);
502502
static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
503503
RangeTblEntry *rte);
504+
static void free_child_join_sjinfo(SpecialJoinInfo *sjinfo);
504505
RelOptInfo *pg_hint_plan_make_join_rel(PlannerInfo *root, RelOptInfo *rel1,
505506
RelOptInfo *rel2);
506507

update_copied_funcs.pl

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
'build_child_join_sjinfo',
2222
'get_matching_part_pairs',
2323
'compute_partition_bounds',
24-
'try_partitionwise_join'],
24+
'try_partitionwise_join',
25+
'free_child_join_sjinfo'],
2526
head => core_c_head()},
2627
'make_join_rel.c'
2728
=> {protos => [],

0 commit comments

Comments
 (0)