Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

To discard - Pg15 indexonlyscan #76

Open
wants to merge 5 commits into
base: pg15
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions src/postgres/src/backend/executor/nodeIndexonlyscan.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,7 @@ IndexOnlyNext(IndexOnlyScanState *node)
if (scandesc->xs_recheck)
{
econtext->ecxt_scantuple = slot;
ExprState *recheckqual = node->yb_indexqual_for_recheck
? node->yb_indexqual_for_recheck : node->recheckqual;
if (!ExecQualAndReset(recheckqual, econtext))
if (!ExecQual(node->recheckqual, econtext))
{
/* Fails recheck, so drop it and loop back for another */
ResetExprContext(econtext);
Expand Down Expand Up @@ -640,9 +638,6 @@ ExecInitIndexOnlyScan(IndexOnlyScan *node, EState *estate, int eflags)
ExecInitQual(node->scan.plan.qual, (PlanState *) indexstate);
indexstate->recheckqual =
ExecInitQual(node->recheckqual, (PlanState *) indexstate);
indexstate->yb_indexqual_for_recheck = node->yb_indexqual_for_recheck
? ExecInitQual(node->yb_indexqual_for_recheck, (PlanState *) indexstate)
: NULL;

/*
* If we are just doing EXPLAIN (ie, aren't going to run the plan), stop
Expand Down
1 change: 0 additions & 1 deletion src/postgres/src/backend/nodes/copyfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,6 @@ _copyIndexOnlyScan(const IndexOnlyScan *from)
COPY_SCALAR_FIELD(indexorderdir);
COPY_NODE_FIELD(yb_pushdown.quals);
COPY_NODE_FIELD(yb_pushdown.colrefs);
COPY_NODE_FIELD(yb_indexqual_for_recheck);

return newnode;
}
Expand Down
101 changes: 6 additions & 95 deletions src/postgres/src/backend/optimizer/plan/createplan.c
Original file line number Diff line number Diff line change
Expand Up @@ -3961,99 +3961,6 @@ create_samplescan_plan(PlannerInfo *root, Path *best_path,
return scan_plan;
}

static inline bool
YbIsHashCodeFunc(FuncExpr *func)
{
return func && func->funcid == YB_HASH_CODE_OID;
}

/*
* This function changes attribute numbers for each yb_hash_code function
* argument. Initially arguments use attribute numbers from relation.
* After the change attribute numbers will be taken from index which is used
* for the yb_hash_code pushdown.
*/
static bool
YbFixHashCodeFuncArgs(FuncExpr *hash_code_func, const IndexOptInfo *index)
{
Assert(YbIsHashCodeFunc(hash_code_func));
ListCell *l;
int indexcol = 0;
foreach(l, hash_code_func->args)
{
Node *arg_node = (Node *)lfirst(l);
Var *arg_var = (Var *)(IsA(arg_node, Var) ? arg_node : NULL);
if (!arg_var ||
indexcol >= index->nkeycolumns ||
index->rel->relid != arg_var->varno ||
index->indexkeys[indexcol] != arg_var->varattno ||
index->opcintype[indexcol] != arg_var->vartype)
return false;
/*
* Note: In spite of the fact that YSQL will use secodary index for handling
* the yb_hash_code pushdown the arg_var->varno field should not be changed
* to INDEX_VAR as postgres does for its native functional indexes.
* Because from the postgres's point of view neither the yb_hash_code
* function itself not its arguments will not be converted into index
* columns.
*/
arg_var->varattno = indexcol + 1;
++indexcol;
}
return true;
}

static bool
YbFixHashCodeFuncArgsWalker(Node *node, IndexOptInfo *index)
{
FuncExpr *func = (FuncExpr *)(IsA(node, FuncExpr) ? node : NULL);

if (YbIsHashCodeFunc(func) && !YbFixHashCodeFuncArgs(func, index))
elog(ERROR, "bad call of yb_hash_code");

return expression_tree_walker(
node, &YbFixHashCodeFuncArgsWalker, index);
}

static bool
YbHasHashCodeFuncWalker(Node *node, bool *hash_code_func_found)
{
if (*hash_code_func_found)
return true;

FuncExpr *func = (FuncExpr *)(IsA(node, FuncExpr) ? node : NULL);

if (YbIsHashCodeFunc(func))
{
*hash_code_func_found = true;
return true;
}

return expression_tree_walker(
node, &YbHasHashCodeFuncWalker, hash_code_func_found);
}

/*
* In case indexquals has at least one yb_hash_code qual function makes
* a copy of indexquals and alters yb_hash_code function args attrributes.
* In other cases functions returns NULL.
*/
static List*
YbBuildIndexqualForRecheck(List *indexquals, IndexOptInfo* indexinfo)
{
bool has_hash_code_func = false;
expression_tree_walker(
(Node *)indexquals, &YbHasHashCodeFuncWalker, &has_hash_code_func);
if (has_hash_code_func)
{
List *result = copyObject(indexquals);
expression_tree_walker(
(Node *)result, &YbFixHashCodeFuncArgsWalker, indexinfo);
return result;
}
return NULL;
}

/*
* create_indexscan_plan
* Returns an indexscan plan for the base relation scanned by 'best_path'
Expand Down Expand Up @@ -4274,8 +4181,6 @@ create_indexscan_plan(PlannerInfo *root,
fixed_indexorderbys,
indexinfo->indextlist,
best_path->indexscandir);
index_only_scan_plan->yb_indexqual_for_recheck =
YbBuildIndexqualForRecheck(fixed_indexquals, best_path->indexinfo);

scan_plan = (Scan *) index_only_scan_plan;
}
Expand Down Expand Up @@ -6357,6 +6262,9 @@ fix_indexqual_references(PlannerInfo *root, IndexPath *index_path,
}
}

/* YB_TODO: This can be optimized by converting
* yb_get_fixed_batched_indexquals to yb_get_batched_indexquals such that it
* returns both fixed and stripped quals.*/
if(!bms_is_empty(root->yb_cur_batched_relids) && IsYugaByteEnabled())
{
stripped_indexquals = yb_get_actual_batched_clauses(root, rinfos, (Path *) index_path);
Expand Down Expand Up @@ -6419,6 +6327,9 @@ fix_indexqual_clause(PlannerInfo *root, IndexOptInfo *index, int indexcol,
{
OpExpr *op = (OpExpr *) clause;

if (list_length(op->args) != 2)
elog(ERROR, "indexqual clause is not binary opclause");

/* Replace the indexkey expression with an index Var. */
linitial(op->args) = fix_indexqual_operand(linitial(op->args),
index,
Expand Down
6 changes: 0 additions & 6 deletions src/postgres/src/include/nodes/execnodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -1687,12 +1687,6 @@ typedef struct IndexOnlyScanState
/* YB specific attributes. */
bool yb_ioss_might_recheck;
List *yb_ioss_aggrefs;
/*
* yb_indexqual_for_recheck is the modified version of indexqual.
* It is used in tuple recheck step only.
* In majority of cases it is NULL which means that indexqual will be used for tuple recheck.
*/
ExprState *yb_indexqual_for_recheck;
} IndexOnlyScanState;

/* ----------------
Expand Down
6 changes: 0 additions & 6 deletions src/postgres/src/include/nodes/plannodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -494,12 +494,6 @@ typedef struct IndexOnlyScan
List *indextlist; /* TargetEntry list describing index's cols */
ScanDirection indexorderdir; /* forward or backward or don't care */
PushdownExprs yb_pushdown;
/*
* yb_indexqual_for_recheck is the modified version of indexqual.
* It is used in tuple recheck step only.
* In majority of cases it is NULL which means that indexqual will be used for tuple recheck.
*/
List *yb_indexqual_for_recheck;
} IndexOnlyScan;

/* ----------------
Expand Down
34 changes: 34 additions & 0 deletions src/postgres/src/test/regress/expected/yb_pg15.out
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,40 @@ select * from myview;
create temp table prtx2 (a integer, b integer, c integer);
insert into prtx2 select 1 + i%10, i, i from generate_series(1,5000) i, generate_series(1,10) j;
create index on prtx2 (c);
-- testing yb_hash_code pushdown on a secondary index with a text hash column
CREATE TABLE text_table (hr text, ti text, tj text, i int, j int, primary key (hr));
INSERT INTO text_table SELECT i::TEXT, i::TEXT, i::TEXT, i, i FROM generate_series(1,10000) i;
CREATE INDEX textidx ON text_table (tj);
SELECT tj FROM text_table WHERE yb_hash_code(tj) <= 63;
tj
------
4999
8300
6918
912
8646
4946
6920
6785
5659
1363
(10 rows)

SELECT tj FROM text_table WHERE 63 >= yb_hash_code(tj);
tj
------
4999
8300
6918
912
8646
4946
6920
6785
5659
1363
(10 rows)

-- Cleanup
DROP TABLE IF EXISTS address, address2, emp, emp2, emp_par1, emp_par1_1_100, emp_par2, emp_par3,
fastpath, myemp, myemp2, myemp2_101_200, myemp2_1_100, p1, p2, pk_range_int_asc,
Expand Down
7 changes: 7 additions & 0 deletions src/postgres/src/test/regress/sql/yb_pg15.sql
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ create temp table prtx2 (a integer, b integer, c integer);
insert into prtx2 select 1 + i%10, i, i from generate_series(1,5000) i, generate_series(1,10) j;
create index on prtx2 (c);

-- testing yb_hash_code pushdown on a secondary index with a text hash column
CREATE TABLE text_table (hr text, ti text, tj text, i int, j int, primary key (hr));
INSERT INTO text_table SELECT i::TEXT, i::TEXT, i::TEXT, i, i FROM generate_series(1,10000) i;
CREATE INDEX textidx ON text_table (tj);
SELECT tj FROM text_table WHERE yb_hash_code(tj) <= 63;
SELECT tj FROM text_table WHERE 63 >= yb_hash_code(tj);

-- Cleanup
DROP TABLE IF EXISTS address, address2, emp, emp2, emp_par1, emp_par1_1_100, emp_par2, emp_par3,
fastpath, myemp, myemp2, myemp2_101_200, myemp2_1_100, p1, p2, pk_range_int_asc,
Expand Down