Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 74438aa

Browse files
committed
light refactoring, index size fetching fixed
1 parent 0a3500c commit 74438aa

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

init.c

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,25 @@ static CustomExecMethods crossmatch_exec_methods;
8585
)
8686

8787

88+
static inline int64
89+
get_index_size(Oid idx)
90+
{
91+
Datum size = DirectFunctionCall2(pg_relation_size,
92+
ObjectIdGetDatum(idx),
93+
PointerGetDatum(cstring_to_text("main")));
94+
return DatumGetInt64(size);
95+
}
96+
97+
static inline Oid
98+
get_dist_func()
99+
{
100+
text *dist_func_name =
101+
cstring_to_text("public.dist(public.spoint, public.spoint)");
102+
103+
return DatumGetObjectId(DirectFunctionCall1(to_regprocedure,
104+
PointerGetDatum(dist_func_name)));
105+
}
106+
88107
static float8
89108
cstring_to_float8(char *str)
90109
{
@@ -166,10 +185,7 @@ pick_suitable_index(Oid relation, AttrNumber column)
166185

167186
if (pg_ind->indkey.values[i] == column)
168187
{
169-
cur_index_size = DatumGetInt64(
170-
DirectFunctionCall2(pg_relation_size,
171-
ObjectIdGetDatum(relation),
172-
PointerGetDatum(cstring_to_text("main"))));
188+
cur_index_size = get_index_size(pg_ind->indexrelid);
173189

174190
if (found_index == InvalidOid || cur_index_size < found_index_size)
175191
{
@@ -263,6 +279,9 @@ create_crossmatch_path(PlannerInfo *root,
263279
Oid outer_idx;
264280
Oid inner_idx;
265281

282+
if (outer_rel == inner_rel)
283+
return;
284+
266285
if ((outer_idx = pick_suitable_index(outer_rel, outer_spoint)) == InvalidOid ||
267286
(inner_idx = pick_suitable_index(inner_rel, inner_spoint)) == InvalidOid)
268287
{
@@ -348,10 +367,17 @@ join_pathlist_hook(PlannerInfo *root,
348367
JoinPathExtraData *extra)
349368
{
350369
ListCell *restr;
351-
text *dist_func_name = cstring_to_text("dist(spoint,spoint)");
352370
Oid dist_func;
353371
Relids required_relids = NULL;
354372

373+
if (set_join_pathlist_next)
374+
set_join_pathlist_next(root, joinrel, outerrel,
375+
innerrel, jointype, extra);
376+
377+
/* Get oid of the dist(spoint, spoint) function */
378+
if ((dist_func = get_dist_func()) == InvalidOid)
379+
return;
380+
355381
if (outerrel->reloptkind == RELOPT_BASEREL &&
356382
innerrel->reloptkind == RELOPT_BASEREL)
357383
{
@@ -360,20 +386,6 @@ join_pathlist_hook(PlannerInfo *root,
360386
}
361387
else return; /* one of relations can't have index */
362388

363-
dist_func = DatumGetObjectId(DirectFunctionCall1(to_regprocedure,
364-
PointerGetDatum(dist_func_name)));
365-
366-
if (dist_func == InvalidOid)
367-
return;
368-
369-
if (set_join_pathlist_next)
370-
set_join_pathlist_next(root,
371-
joinrel,
372-
outerrel,
373-
innerrel,
374-
jointype,
375-
extra);
376-
377389
foreach(restr, extra->restrictlist)
378390
{
379391
RestrictInfo *restrInfo = (RestrictInfo *) lfirst(restr);
@@ -433,7 +445,8 @@ create_crossmatch_plan(PlannerInfo *root,
433445
cscan->scan.plan.targetlist = tlist;
434446
cscan->scan.plan.qual = joinclauses;
435447
cscan->scan.scanrelid = 0;
436-
cscan->custom_scan_tlist = tlist; /* output of this node */
448+
cscan->custom_scan_tlist = tlist; /* tlist of the 'virtual' join rel
449+
we'll have to build and scan */
437450

438451
cscan->flags = best_path->flags;
439452
cscan->methods = &crossmatch_plan_methods;
@@ -465,9 +478,7 @@ crossmatch_create_scan_state(CustomScan *node)
465478
scan_state->css.flags = node->flags;
466479
scan_state->css.methods = &crossmatch_exec_methods;
467480

468-
/* TODO: check if this assignment is redundant */
469-
scan_state->css.ss.ps.ps_TupFromTlist = false;
470-
481+
/* Save scan tlist for join relation */
471482
scan_state->scan_tlist = node->custom_scan_tlist;
472483

473484
scan_state->outer_idx = linitial_oid(linitial(node->custom_private));
@@ -519,6 +530,7 @@ crossmatch_exec(CustomScanState *node)
519530

520531
for(;;)
521532
{
533+
/* Fetch next tid pair */
522534
if (!node->ss.ps.ps_TupFromTlist)
523535
{
524536
Datum *values = scan_state->values;
@@ -541,9 +553,9 @@ crossmatch_exec(CustomScanState *node)
541553
if (scan_state->scan_tlist != NIL)
542554
{
543555
TupleDesc tupdesc = node->ss.ss_ScanTupleSlot->tts_tupleDescriptor;
544-
int col_index = 0;
545556
bool htup_outer_ready = false;
546557
bool htup_inner_ready = false;
558+
int col_index = 0;
547559
ListCell *l;
548560

549561
htup_outer.t_self = p_tids[0];
@@ -645,7 +657,7 @@ crossmatch_end(CustomScanState *node)
645657
static void
646658
crossmatch_rescan(CustomScanState *node)
647659
{
648-
660+
/* NOTE: nothing to do here? */
649661
}
650662

651663
static void

0 commit comments

Comments
 (0)