Skip to content

Commit

Permalink
[#25268] YSQL: Simplify ExecLockRows code
Browse files Browse the repository at this point in the history
Summary:
The `ExecLockRows` function has the code to check that row locks are not requested for YB and non-YB tables at same time. It is reasonable to move this code into initialization step of the  Lock Rows node to avoid wasting of CPU cycles on each row processing.
Jira: DB-14457

Test Plan: Jenkins

Reviewers: pjain

Reviewed By: pjain

Subscribers: yql

Tags: #jenkins-ready

Differential Revision: https://phorge.dev.yugabyte.com/D40632
  • Loading branch information
d-uspenskiy committed Dec 18, 2024
1 parent e1e83ea commit 39088f7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
42 changes: 22 additions & 20 deletions src/postgres/src/backend/executor/nodeLockRows.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,9 @@ ExecLockRows(PlanState *pstate)
lnext:
slot = ExecProcNode(outerPlan);

int n_yb_relations = 0;
int n_relations = 0;
foreach(lc, node->lr_arowMarks)
if (node->yb_are_row_marks_for_yb_rels &&
XactIsoLevel == XACT_SERIALIZABLE)
{
ExecAuxRowMark *aerm = (ExecAuxRowMark *) lfirst(lc);
ExecRowMark *erm = aerm->rowmark;
if (IsYBBackedRelation(erm->relation))
{
n_yb_relations++;
}
n_relations++;
}

if (n_yb_relations > 0 && n_yb_relations != n_relations)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("Mixing Yugabyte relations and not Yugabyte relations with "
"row locks is not supported")));

if (n_yb_relations > 0 && XactIsoLevel == XACT_SERIALIZABLE) {
/*
* For YB relations, we don't lock tuples using this node in SERIALIZABLE level. Instead we take
* predicate locks by setting the row mark in read requests sent to txn participants.
Expand Down Expand Up @@ -211,7 +194,9 @@ ExecLockRows(PlanState *pstate)
if (!IsolationUsesXactSnapshot())
lockflags |= TUPLE_LOCK_FLAG_FIND_LAST_VERSION;

if (IsYBBackedRelation(erm->relation))
Assert(
IsYBBackedRelation(erm->relation) == node->yb_are_row_marks_for_yb_rels);
if (node->yb_are_row_marks_for_yb_rels)
{
test = YBCLockTuple(
erm->relation, datum, erm->markType, erm->waitPolicy, estate);
Expand Down Expand Up @@ -398,6 +383,8 @@ ExecInitLockRows(LockRows *node, EState *estate, int eflags)
*/
lrstate->lr_arowMarks = NIL;
epq_arowmarks = NIL;
bool row_lock_for_yb_rel_found = false;
bool row_lock_for_non_yb_rel_found = false;
foreach(lc, node->rowMarks)
{
PlanRowMark *rc = lfirst_node(PlanRowMark, lc);
Expand All @@ -419,11 +406,26 @@ ExecInitLockRows(LockRows *node, EState *estate, int eflags)
* do an EPQ recheck.
*/
if (RowMarkRequiresRowShareLock(erm->markType))
{
if (IsYBBackedRelation(erm->relation))
row_lock_for_yb_rel_found = true;
else
row_lock_for_non_yb_rel_found = true;

lrstate->lr_arowMarks = lappend(lrstate->lr_arowMarks, aerm);
}
else
epq_arowmarks = lappend(epq_arowmarks, aerm);
}

if (row_lock_for_yb_rel_found && row_lock_for_non_yb_rel_found)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("Mixing Yugabyte relations and not Yugabyte "
"relations with row locks is not supported")));

lrstate->yb_are_row_marks_for_yb_rels = row_lock_for_yb_rel_found;

/* Now we have the info needed to set up EPQ state */
EvalPlanQualInit(&lrstate->lr_epqstate, estate,
outerPlan, epq_arowmarks, node->epqParam);
Expand Down
3 changes: 3 additions & 0 deletions src/postgres/src/include/nodes/execnodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -2998,6 +2998,9 @@ typedef struct LockRowsState
PlanState ps; /* its first field is NodeTag */
List *lr_arowMarks; /* List of ExecAuxRowMarks */
EPQState lr_epqstate; /* for evaluating EvalPlanQual rechecks */

bool yb_are_row_marks_for_yb_rels; /* lr_arowMarks relates to YB
* relations */
} LockRowsState;

/* ----------------
Expand Down

0 comments on commit 39088f7

Please sign in to comment.