Skip to content

Commit

Permalink
Replace recursive call with case-by-case calls
Browse files Browse the repository at this point in the history
The recursive call looked kinda scary even though it could only be made
one level deep. This just unrolls what it does explicitly. I moved some
logic into a separate function as we had hit three repetitions.
  • Loading branch information
jasonmp85 committed Feb 25, 2015
1 parent 8d610f9 commit 4f4af5b
Showing 1 changed file with 34 additions and 31 deletions.
65 changes: 34 additions & 31 deletions pg_shard.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ static DistributedPlan * BuildDistributedPlan(Query *query, List *shardIntervalL
/* executor functions forward declarations */
static void PgShardExecutorStart(QueryDesc *queryDesc, int eflags);
static bool IsPgShardPlan(PlannedStmt *plannedStmt);
static void NextExecutorStartHook(QueryDesc *queryDesc, int eflags);
static LOCKMODE CommutativityRuleToLockMode(CmdType commandType);
static void AcquireExecutorShardLocks(List *taskList, LOCKMODE lockMode);
static int CompareTasksByShardId(const void *leftElement, const void *rightElement);
Expand Down Expand Up @@ -1104,24 +1105,22 @@ PgShardExecutorStart(QueryDesc *queryDesc, int eflags)
if (pgShardExecution)
{
DistributedPlan *distributedPlan = (DistributedPlan *) plannedStatement->planTree;

bool selectFromMultipleShards = distributedPlan->selectFromMultipleShards;
if (!selectFromMultipleShards)
bool zeroShardQuery = (list_length(distributedPlan->taskList) == 0);

if (zeroShardQuery)
{
/* if zero shards are involved, let query hit local table */
Plan *originalPlan = distributedPlan->originalPlan;
plannedStatement->planTree = originalPlan;

NextExecutorStartHook(queryDesc, eflags);
}
else if (!selectFromMultipleShards)
{
bool topLevel = true;
LOCKMODE lockMode = NoLock;
EState *executorState = NULL;
bool zeroShardQuery = list_length(distributedPlan->taskList) == 0;

/* if query involves zero shards, just let it hit local table */
if (zeroShardQuery)
{
Plan *originalPlan = distributedPlan->originalPlan;
plannedStatement->planTree = originalPlan;

PgShardExecutorStart(queryDesc, eflags);
return;
}

/* disallow transactions and triggers during distributed commands */
PreventTransactionChain(topLevel, "distributed commands");
Expand Down Expand Up @@ -1182,28 +1181,12 @@ PgShardExecutorStart(QueryDesc *queryDesc, int eflags)
originalPlan = distributedPlan->originalPlan;
plannedStatement->planTree = originalPlan;

/* call into the standard executor start, or hook if set */
if (PreviousExecutorStartHook != NULL)
{
PreviousExecutorStartHook(queryDesc, eflags);
}
else
{
standard_ExecutorStart(queryDesc, eflags);
}
NextExecutorStartHook(queryDesc, eflags);
}
}
else
{
/* call the next hook in the chain or the standard one, if no other hook was set */
if (PreviousExecutorStartHook != NULL)
{
PreviousExecutorStartHook(queryDesc, eflags);
}
else
{
standard_ExecutorStart(queryDesc, eflags);
}
NextExecutorStartHook(queryDesc, eflags);
}
}

Expand All @@ -1223,6 +1206,26 @@ IsPgShardPlan(PlannedStmt *plannedStmt)
}


/*
* NextExecutorStartHook simply encapsulates the common logic of calling the
* next executor start hook in the chain or the standard executor start hook
* if no other hooks are present.
*/
static void
NextExecutorStartHook(QueryDesc *queryDesc, int eflags)
{
/* call into the standard executor start, or hook if set */
if (PreviousExecutorStartHook != NULL)
{
PreviousExecutorStartHook(queryDesc, eflags);
}
else
{
standard_ExecutorStart(queryDesc, eflags);
}
}


/*
* CommutativityRuleToLockMode determines the commutativity rule for the given
* command and returns the appropriate lock mode to enforce that rule. The
Expand Down

0 comments on commit 4f4af5b

Please sign in to comment.