From 2fa2862be5aad98c68b43054be5513f8febddee4 Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Tue, 25 Apr 2017 20:26:40 -0700 Subject: [PATCH] Build DistTableCacheEntry->shardIntervalCompareFunction even for 0 shards. Previously we, unnecessarily, used a the first shard's type information to to look up the comparison function. But that information is already available, so use it. That's helpful because we sometimes want to access the comparator function even if there's no shards. --- .../distributed/utils/metadata_cache.c | 68 ++++--------------- src/include/distributed/metadata_cache.h | 2 +- 2 files changed, 15 insertions(+), 55 deletions(-) diff --git a/src/backend/distributed/utils/metadata_cache.c b/src/backend/distributed/utils/metadata_cache.c index 2939ade41..1b85fa00d 100644 --- a/src/backend/distributed/utils/metadata_cache.c +++ b/src/backend/distributed/utils/metadata_cache.c @@ -123,8 +123,6 @@ static ShardCacheEntry * LookupShardCacheEntry(int64 shardId); static DistTableCacheEntry * LookupDistTableCacheEntry(Oid relationId); static void BuildDistTableCacheEntry(DistTableCacheEntry *cacheEntry); static void BuildCachedShardList(DistTableCacheEntry *cacheEntry); -static FmgrInfo * ShardIntervalCompareFunction(ShardInterval **shardIntervalArray, - char partitionMethod); static ShardInterval ** SortShardIntervalArray(ShardInterval **shardIntervalArray, int shardCount, FmgrInfo * @@ -608,6 +606,10 @@ BuildCachedShardList(DistTableCacheEntry *cacheEntry) List *distShardTupleList = NIL; int shardIntervalArrayLength = 0; int shardIndex = 0; + GetPartitionTypeInputInfo(cacheEntry->partitionKeyString, + cacheEntry->partitionMethod, + &intervalTypeId, + &intervalTypeMod); distShardTupleList = LookupDistShardTuples(cacheEntry->relationId); shardIntervalArrayLength = list_length(distShardTupleList); @@ -617,13 +619,6 @@ BuildCachedShardList(DistTableCacheEntry *cacheEntry) TupleDesc distShardTupleDesc = RelationGetDescr(distShardRelation); ListCell *distShardTupleCell = NULL; int arrayIndex = 0; - Oid intervalTypeId = InvalidOid; - int32 intervalTypeMod = -1; - - GetPartitionTypeInputInfo(cacheEntry->partitionKeyString, - cacheEntry->partitionMethod, - &intervalTypeId, - &intervalTypeMod); shardIntervalArray = MemoryContextAllocZero(CacheMemoryContext, shardIntervalArrayLength * @@ -663,23 +658,19 @@ BuildCachedShardList(DistTableCacheEntry *cacheEntry) } /* decide and allocate interval comparison function */ - if (cacheEntry->partitionMethod == DISTRIBUTE_BY_NONE) + if (intervalTypeId != InvalidOid) + { + /* allocate the comparison function in the cache context */ + MemoryContext oldContext = MemoryContextSwitchTo(CacheMemoryContext); + + shardIntervalCompareFunction = GetFunctionInfo(intervalTypeId, BTREE_AM_OID, + BTORDER_PROC); + MemoryContextSwitchTo(oldContext); + } + else { shardIntervalCompareFunction = NULL; } - else if (shardIntervalArrayLength > 0) - { - MemoryContext oldContext = CurrentMemoryContext; - - /* allocate the comparison function in the cache context */ - oldContext = MemoryContextSwitchTo(CacheMemoryContext); - - shardIntervalCompareFunction = - ShardIntervalCompareFunction(shardIntervalArray, - cacheEntry->partitionMethod); - - MemoryContextSwitchTo(oldContext); - } /* reference tables has a single shard which is not initialized */ if (cacheEntry->partitionMethod == DISTRIBUTE_BY_NONE) @@ -784,37 +775,6 @@ BuildCachedShardList(DistTableCacheEntry *cacheEntry) } -/* - * ShardIntervalCompareFunction returns the appropriate compare function for the - * partition column type. In case of hash-partitioning, it always returns the compare - * function for integers. Callers of this function has to ensure that shardIntervalArray - * has at least one element. - */ -static FmgrInfo * -ShardIntervalCompareFunction(ShardInterval **shardIntervalArray, char partitionMethod) -{ - FmgrInfo *shardIntervalCompareFunction = NULL; - Oid comparisonTypeId = InvalidOid; - - Assert(shardIntervalArray != NULL); - - if (partitionMethod == DISTRIBUTE_BY_HASH) - { - comparisonTypeId = INT4OID; - } - else - { - ShardInterval *shardInterval = shardIntervalArray[0]; - comparisonTypeId = shardInterval->valueTypeId; - } - - shardIntervalCompareFunction = GetFunctionInfo(comparisonTypeId, BTREE_AM_OID, - BTORDER_PROC); - - return shardIntervalCompareFunction; -} - - /* * SortedShardIntervalArray sorts the input shardIntervalArray. Shard intervals with * no min/max values are placed at the end of the array. diff --git a/src/include/distributed/metadata_cache.h b/src/include/distributed/metadata_cache.h index abd1861c0..615da7ed3 100644 --- a/src/include/distributed/metadata_cache.h +++ b/src/include/distributed/metadata_cache.h @@ -47,7 +47,7 @@ typedef struct int shardIntervalArrayLength; ShardInterval **sortedShardIntervalArray; - FmgrInfo *shardIntervalCompareFunction; /* NULL if no shard intervals exist */ + FmgrInfo *shardIntervalCompareFunction; /* NULL if DISTRIBUTE_BY_NONE */ FmgrInfo *hashFunction; /* NULL if table is not distributed by hash */ /* pg_dist_shard_placement metadata */