Skip to content

Commit 4e98040

Browse files
committed
Improved query planning
1 parent 3547364 commit 4e98040

4 files changed

Lines changed: 51 additions & 2 deletions

File tree

src/planner/distributed_mobilitydb_explain.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ static void ExplainQueryType(DistributedSpatiotemporalQueryPlan *distPlan, Expla
132132
ExplainPropertyText("Distributed Spatiotemporal Planner", temp->data, es);
133133
}
134134

135+
/* getQueryType renders the combination of chosen strategies as a short human-readable label. */
135136
static char * getQueryType(List *strategies)
136137
{
137138
ListCell *cell = NULL;
@@ -222,6 +223,13 @@ static void ExplainQueryPlan(DistributedSpatiotemporalQueryPlan *distPlan, Expla
222223
}
223224
}
224225

226+
/*
227+
* ExplainPlanStrategies re-runs the executor in explain-only mode
228+
* (RunQueryExecutor with explain=true, so no data-modifying SPI calls
229+
* happen) to obtain the per-strategy tasks, then prints one representative
230+
* task per task type via ExplainOneTask — showing the plan for a single
231+
* tile stands in for all `candidates` tiles that would actually run.
232+
*/
225233
static void
226234
ExplainPlanStrategies(DistributedSpatiotemporalQueryPlan *distPlan, ExplainState *es, int indent_group)
227235
{
@@ -242,6 +250,12 @@ ExplainPlanStrategies(DistributedSpatiotemporalQueryPlan *distPlan, ExplainState
242250
es->indent -= 5;
243251
}
244252
}
253+
/*
254+
* ExplainOneTask plans and (via ExplainWorkerPlan) prints the local plan
255+
* for a single, randomly-chosen tile of `task`, substituting that tile's
256+
* concrete shard identifier into the task's query (GetLocalQuery) so it
257+
* can be planned as an ordinary local query.
258+
*/
245259
static void
246260
ExplainOneTask(ExecutorTask *task, STMultirelation *base,ExplainState *es, int indent_group)
247261
{
@@ -267,6 +281,12 @@ ExplainOneTask(ExecutorTask *task, STMultirelation *base,ExplainState *es, int i
267281
ExplainEndOutput(es);
268282
}
269283

284+
/*
285+
* GetLocalQuery rewrites query_string so that every distributed table name
286+
* it references is replaced by the concrete shard/tile identifier chosen
287+
* for it (GetRandomTileId), producing a plain local query that EXPLAIN can
288+
* plan directly.
289+
*/
270290
static char *
271291
GetLocalQuery(char *query_string, Oid base, ExecTaskType taskType, int rand_tile)
272292
{

src/post_processing/post_processing.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,15 @@ PostProcessingQuery(PostProcessing *postProcessing, List *strategies)
4747
/* The other operations will be added internally based on the query semantics */
4848
}
4949

50-
/* Early rewriting for the distributed functions */
51-
50+
/*
51+
* RewriterDistFuncs rewrites query_string so each registered distributed
52+
* function call (e.g. an aggregate like avg()) is replaced by its worker
53+
* function, producing the query that actually runs on each tile
54+
* (postProcessing->worker). For every rewritten function it also records
55+
* the coordinator-side combiner/final operations (as QOperations) so the
56+
* executor's post-processing phase can merge the per-tile results back
57+
* together (see ProcessIntermediateTasks/ProcessFinalTasks).
58+
*/
5259
extern void
5360
RewriterDistFuncs(Query *parse, PostProcessing *postProcessing, const char *query_string)
5461
{

src/utils/helper_functions.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414

1515
#include "utils/helper_functions.h"
1616

17+
/*
18+
* replaceWord replaces the first occurrence of oldW in s with newW,
19+
* in place, and returns s. Assumes s has enough allocated room to hold the
20+
* result (the local buffer is sized off strlen(s), so newW must not be
21+
* longer than oldW).
22+
*/
1723
extern
1824
char* replaceWord( char* s, char* oldW, char* newW)
1925
{
@@ -35,6 +41,7 @@ char* replaceWord( char* s, char* oldW, char* newW)
3541
return s;
3642
}
3743

44+
/* extract_between returns a newly allocated copy of the substring of str found strictly between markers p1 and p2. */
3845
extern
3946
char * extract_between(const char *str, const char *p1, const char *p2) {
4047
const char *i1 = strstr(str, p1);
@@ -54,6 +61,7 @@ char * extract_between(const char *str, const char *p1, const char *p2) {
5461
}
5562
}
5663

64+
/* change_sentence returns a newly allocated copy of sentence with the first occurrence of find replaced by replace. */
5765
extern char *
5866
change_sentence (char *sentence, char *find, char *replace)
5967
{
@@ -72,6 +80,7 @@ change_sentence (char *sentence, char *find, char *replace)
7280
return dest;
7381
}
7482

83+
/* toLower returns a newly allocated, lowercased copy of str. */
7584
extern
7685
char *toLower(char *str)
7786
{
@@ -84,6 +93,7 @@ char *toLower(char *str)
8493
return str_l;
8594
}
8695

96+
/* IsDatumEmpty reports whether val is the zero/unset Datum (i.e. no value was assigned). */
8797
extern bool
8898
IsDatumEmpty(Datum val)
8999
{

src/utils/planner_utils.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
#include "utils/planner_utils.h"
1818
#include "catalog/pg_dist_spatiotemporal_dist_functions.h"
1919

20+
/*
21+
* GetTilingSchemeInfo loads relationId's pg_dist_spatiotemporal_tables row
22+
* into an in-memory STMultirelationCatalog: its tiling method/type/
23+
* granularity, tile count, distribution column, and related metadata.
24+
*/
2025
extern STMultirelationCatalog
2126
GetTilingSchemeInfo(Oid relationId)
2227
{
@@ -85,6 +90,13 @@ DisFuncRelationId()
8590
return RelationId(Tbl_Dist_Functions);
8691
}
8792

93+
/*
94+
* AddCatalogFilterInfo records into catalogFilter which of tbl's tiles a
95+
* predicate node of predType could match. A NULL node (no analysable
96+
* predicate) conservatively assumes every tile is a candidate. Narrowing
97+
* the candidate count below numTiles (e.g. via a real bounding-box lookup)
98+
* is left as future work — see the Tile Scan Rebalancer TODO below.
99+
*/
88100
extern void
89101
AddCatalogFilterInfo(STMultirelationCatalog tbl, CatalogFilter *catalogFilter, Node *node,
90102
PredicateType predType, bool IsConst)

0 commit comments

Comments
 (0)