diff --git a/src/backend/distributed/planner/fast_path_plan_cache.c b/src/backend/distributed/planner/fast_path_plan_cache.c index 6df08f2ad..f503529a0 100644 --- a/src/backend/distributed/planner/fast_path_plan_cache.c +++ b/src/backend/distributed/planner/fast_path_plan_cache.c @@ -28,6 +28,8 @@ static FastPathPlanCache * GetFastPathCachedPlan(Task *task, DistributedPlan *distributedPlan); static Query * GetLocalShardQueryForCache(Query *jobQuery, Task *task, ParamListInfo paramListInfo); +static char * GetFastPathQueryStringForCache(Query *jobQuery, Task *task, + ParamListInfo orig_paramListInfo); static char * DeparseLocalShardQuery(Query *jobQuery, List *relationShardList, Oid anchorDistributedTableId, int64 anchorShardId); static int ExtractParameterTypesForParamListInfo(ParamListInfo originalParamListInfo, @@ -67,6 +69,8 @@ CacheFastPathPlanForShardQuery(Task *task, DistributedPlan *originalDistributedP * functions/params to have been evaluated in the cached plan. */ Query *jobQuery = copyObject(originalDistributedPlan->workerJob->jobQuery); + FastPathPlanCache *fastPathPlanCache = CitusMakeNode(FastPathPlanCache); + PlannedStmt *localPlan = NULL; if (TaskAccessesLocalNode(task)) @@ -95,12 +99,16 @@ CacheFastPathPlanForShardQuery(Task *task, DistributedPlan *originalDistributedP LockRelationOid(rangeTableEntry->relid, lockMode); localPlan = planner(localShardQuery, NULL, 0, NULL); + fastPathPlanCache->localPlan = localPlan; } else - { } + { + char *queryString = + GetFastPathQueryStringForCache(jobQuery, task, paramListInfo); + + fastPathPlanCache->queryString = queryString; + } - FastPathPlanCache *fastPathPlanCache = CitusMakeNode(FastPathPlanCache); - fastPathPlanCache->localPlan = localPlan; fastPathPlanCache->shardId = task->anchorShardId; fastPathPlanCache->placementGroupIds = TaskGroupIdAccesses(task); @@ -148,6 +156,28 @@ GetLocalShardQueryForCache(Query *jobQuery, Task *task, ParamListInfo orig_param } +/* + * GetFastPathQueryStringForCache is a helper function which generates + * the local shard string based on the jobQuery. The function should + * not be used for generic purposes, it is specialized for local cached + * queries. + * + */ +static char * +GetFastPathQueryStringForCache(Query *jobQuery, Task *task, ParamListInfo + orig_paramListInfo) +{ + char *shardQueryString = + DeparseLocalShardQuery(jobQuery, task->relationShardList, + task->anchorDistributedTableId, + task->anchorShardId); + ereport(DEBUG5, (errmsg("Local shard query that is going to be cached: %s", + shardQueryString))); + + return shardQueryString; +} + + /* * DeparseLocalShardQuery is a helper function to deparse given jobQuery for the shard(s) * identified by the relationShardList, anchorDistributedTableId and anchorShardId. diff --git a/src/backend/distributed/utils/citus_copyfuncs.c b/src/backend/distributed/utils/citus_copyfuncs.c index 0faadb2fa..d32f30632 100644 --- a/src/backend/distributed/utils/citus_copyfuncs.c +++ b/src/backend/distributed/utils/citus_copyfuncs.c @@ -340,6 +340,7 @@ CopyNodeFastPathPlanCache(COPYFUNC_ARGS) COPY_SCALAR_FIELD(shardId); COPY_NODE_FIELD(placementGroupIds); COPY_NODE_FIELD(localPlan); + COPY_STRING_FIELD(queryString); } diff --git a/src/include/distributed/multi_physical_planner.h b/src/include/distributed/multi_physical_planner.h index ecee58fde..f487f610f 100644 --- a/src/include/distributed/multi_physical_planner.h +++ b/src/include/distributed/multi_physical_planner.h @@ -120,7 +120,24 @@ typedef struct FastPathPlanCache uint64 shardId; List *placementGroupIds; + + /* + * Cache can be found in different ways and therefore stored differently on the + * definition. + * + * TODO: Ideally we only have either of the fields. + * investigate UNION of a C struct and CopyNodeFastPathPlanCache. + */ + + /* union */ + /*{*/ + /* if fast path cache is for a local shard */ PlannedStmt *localPlan; + + /* if fast path cache is for a remote shard */ + char *queryString; + + /*} data;*/ } FastPathPlanCache;