Skip to content

Commit

Permalink
Merge 2a49c31 into 1607944
Browse files Browse the repository at this point in the history
  • Loading branch information
onderkalaci committed Jan 20, 2015
2 parents 1607944 + 2a49c31 commit 030c8c0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
57 changes: 56 additions & 1 deletion prune_shard_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,20 @@
#include "utils/errcodes.h"
#include "utils/lsyscache.h"
#include "utils/typcache.h"
#include "utils/memutils.h"


/*
* OperatorTypeCache is used for caching base operator types for given typeId,
* accessMethodId and strategyNumber. It is initialized to empty list as
* there are no items in the cache.
*/
static List *OperatorTypeCache = NIL;


/* local function forward declarations */
static Oid GetOperatorByType(Oid typeId, Oid accessMethodId, int16 strategyNumber);
static Oid LookupOperatorByType(Oid typeId, Oid accessMethodId, int16 strategyNumber);
static bool SimpleOpExpression(Expr *clause);
static Node * HashableClauseMutator(Node *originalNode, Var *partitionColumn);
static bool OpExpressionContainsColumn(OpExpr *operatorExpression, Var *partitionColumn);
Expand Down Expand Up @@ -218,7 +228,7 @@ MakeOpExpression(Var *variable, int16 strategyNumber)
OpExpr *expression = NULL;

/* Load the operator from system catalogs */
operatorId = GetOperatorByType(typeId, accessMethodId, strategyNumber);
operatorId = LookupOperatorByType(typeId, accessMethodId, strategyNumber);

constantValue = makeNullConst(typeId, typeModId, collationId);

Expand All @@ -238,6 +248,51 @@ MakeOpExpression(Var *variable, int16 strategyNumber)
}


/*
* LookupOperatorByType is a wrapper around GetOperatorByType that uses a cache
* to avoid multiple lookups of operators by their types.
*/
static Oid
LookupOperatorByType(Oid typeId, Oid accessMethodId, int16 strategyNumber)
{
OperatorTypeCacheEntry *matchingCacheEntry = NULL;
ListCell *cacheEntryCell = NULL;

/* search the cache */
foreach(cacheEntryCell, OperatorTypeCache)
{
OperatorTypeCacheEntry *cacheEntry = lfirst(cacheEntryCell);

if (cacheEntry->typeId == typeId && cacheEntry->accessMethodId == accessMethodId
&& cacheEntry->strategyNumber == strategyNumber)
{
matchingCacheEntry = cacheEntry;
break;
}
}

/* if not found in the cache, call GetOperatorByType and put the result in cache */
if (matchingCacheEntry == NULL)
{
Oid operatorId = GetOperatorByType(typeId, accessMethodId, strategyNumber);
MemoryContext oldContext = MemoryContextSwitchTo(CacheMemoryContext);

matchingCacheEntry = palloc0(sizeof(OperatorTypeCacheEntry));
matchingCacheEntry->typeId = typeId;
matchingCacheEntry->accessMethodId = accessMethodId;
matchingCacheEntry->strategyNumber = strategyNumber;

matchingCacheEntry->operatorId = operatorId;

OperatorTypeCache = lappend(OperatorTypeCache, matchingCacheEntry);

MemoryContextSwitchTo(oldContext);
}

return matchingCacheEntry->operatorId;
}


/*
* GetOperatorByType returns the operator oid for the given type, access
* method, and strategy number.
Expand Down
15 changes: 15 additions & 0 deletions prune_shard_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@
#define RESERVED_HASHED_COLUMN_ID MaxAttrNumber


/*
* OperatorTypeCacheEntry contains the information for a cache entry in
* operator type cache.
*/
typedef struct OperatorTypeCacheEntry
{
/* cache key consists of typeId, accessMethodId and strategyNumber */
Oid typeId;
Oid accessMethodId;
int16 strategyNumber;

Oid operatorId;
} OperatorTypeCacheEntry;


/* function declarations for shard pruning */
extern List * PruneShardList(Oid relationId, List *whereClauseList,
List *shardIntervalList);
Expand Down

0 comments on commit 030c8c0

Please sign in to comment.