@@ -56,6 +56,13 @@ typedef struct
56
56
List * rangeset ;
57
57
}WrapperNode ;
58
58
59
+ typedef struct
60
+ {
61
+ const PartRelationInfo * prel ;
62
+ Datum least ;
63
+ Datum greatest ;
64
+ }WalkerContext ;
65
+
59
66
bool pg_pathman_enable ;
60
67
PathmanState * pmstate ;
61
68
@@ -86,12 +93,12 @@ static void disable_inheritance_subselect(Query *parse);
86
93
bool inheritance_disabled ;
87
94
88
95
/* Expression tree handlers */
89
- static WrapperNode * walk_expr_tree (Expr * expr ,const PartRelationInfo * prel );
96
+ static WrapperNode * walk_expr_tree (Expr * expr ,WalkerContext * context );
90
97
static int make_hash (const PartRelationInfo * prel ,int value );
91
- static void handle_binary_opexpr (const PartRelationInfo * prel ,WrapperNode * result ,const Var * v ,const Const * c );
92
- static WrapperNode * handle_opexpr (const OpExpr * expr ,const PartRelationInfo * prel );
93
- static WrapperNode * handle_boolexpr (const BoolExpr * expr ,const PartRelationInfo * prel );
94
- static WrapperNode * handle_arrexpr (const ScalarArrayOpExpr * expr ,const PartRelationInfo * prel );
98
+ static void handle_binary_opexpr (WalkerContext * context ,WrapperNode * result ,const Var * v ,const Const * c );
99
+ static WrapperNode * handle_opexpr (const OpExpr * expr ,WalkerContext * context );
100
+ static WrapperNode * handle_boolexpr (const BoolExpr * expr ,WalkerContext * context );
101
+ static WrapperNode * handle_arrexpr (const ScalarArrayOpExpr * expr ,WalkerContext * context );
95
102
static void change_varnos_in_restrinct_info (RestrictInfo * rinfo ,change_varno_context * context );
96
103
static void change_varnos (Node * node ,Oid old_varno ,Oid new_varno );
97
104
static bool change_varno_walker (Node * node ,change_varno_context * context );
@@ -359,12 +366,13 @@ disable_inheritance_subselect(Query *parse)
359
366
static void
360
367
handle_modification_query (Query * parse )
361
368
{
362
- PartRelationInfo * prel ;
363
- List * ranges ;
364
- RangeTblEntry * rte ;
365
- WrapperNode * wrap ;
366
- Expr * expr ;
367
- bool found ;
369
+ PartRelationInfo * prel ;
370
+ List * ranges ;
371
+ RangeTblEntry * rte ;
372
+ WrapperNode * wrap ;
373
+ Expr * expr ;
374
+ bool found ;
375
+ WalkerContext context ;
368
376
369
377
Assert (parse -> commandType == CMD_UPDATE ||
370
378
parse -> commandType == CMD_DELETE );
@@ -383,7 +391,8 @@ handle_modification_query(Query *parse)
383
391
return ;
384
392
385
393
/* Parse syntax tree and extract partition ranges */
386
- wrap = walk_expr_tree (expr ,prel );
394
+ context .prel = prel ;
395
+ wrap = walk_expr_tree (expr ,& context );
387
396
ranges = irange_list_intersect (ranges ,wrap -> rangeset );
388
397
389
398
/* If only one partition is affected then substitute parent table with partition */
@@ -424,11 +433,11 @@ pathman_shmem_startup(void)
424
433
void
425
434
pathman_set_rel_pathlist_hook (PlannerInfo * root ,RelOptInfo * rel ,Index rti ,RangeTblEntry * rte )
426
435
{
427
- PartRelationInfo * prel = NULL ;
428
- RelOptInfo * * new_rel_array ;
429
- RangeTblEntry * * new_rte_array ;
430
- int len ;
431
- bool found ;
436
+ PartRelationInfo * prel = NULL ;
437
+ RelOptInfo * * new_rel_array ;
438
+ RangeTblEntry * * new_rte_array ;
439
+ int len ;
440
+ bool found ;
432
441
433
442
/* Invoke original hook if needed */
434
443
if (set_rel_pathlist_hook_original != NULL )
@@ -497,11 +506,13 @@ pathman_set_rel_pathlist_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, Ran
497
506
wrappers = NIL ;
498
507
foreach (lc ,rel -> baserestrictinfo )
499
508
{
500
- WrapperNode * wrap ;
509
+ WrapperNode * wrap ;
510
+ WalkerContext context ;
501
511
502
512
RestrictInfo * rinfo = (RestrictInfo * )lfirst (lc );
503
513
504
- wrap = walk_expr_tree (rinfo -> clause ,prel );
514
+ context .prel = prel ;
515
+ wrap = walk_expr_tree (rinfo -> clause ,& context );
505
516
wrappers = lappend (wrappers ,wrap );
506
517
ranges = irange_list_intersect (ranges ,wrap -> rangeset );
507
518
}
@@ -523,11 +534,11 @@ pathman_set_rel_pathlist_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, Ran
523
534
palloc0 ((root -> simple_rel_array_size + len )* sizeof (RangeTblEntry * ));
524
535
525
536
/* Copy relations to the new arrays */
526
- for (i = 0 ;i < root -> simple_rel_array_size ;i ++ )
527
- {
528
- new_rel_array [i ]= root -> simple_rel_array [i ];
529
- new_rte_array [i ]= root -> simple_rte_array [i ];
530
- }
537
+ for (i = 0 ;i < root -> simple_rel_array_size ;i ++ )
538
+ {
539
+ new_rel_array [i ]= root -> simple_rel_array [i ];
540
+ new_rte_array [i ]= root -> simple_rte_array [i ];
541
+ }
531
542
532
543
/* Free old arrays */
533
544
pfree (root -> simple_rel_array );
@@ -564,8 +575,8 @@ static void
564
575
set_append_rel_size (PlannerInfo * root ,RelOptInfo * rel ,
565
576
Index rti ,RangeTblEntry * rte )
566
577
{
567
- double parent_rows = 0 ;
568
- double parent_size = 0 ;
578
+ double parent_rows = 0 ;
579
+ double parent_size = 0 ;
569
580
ListCell * l ;
570
581
571
582
foreach (l ,root -> append_rel_list )
@@ -943,7 +954,7 @@ change_varnos_in_restrinct_info(RestrictInfo *rinfo, change_varno_context *conte
943
954
* Recursive function to walk through conditions tree
944
955
*/
945
956
static WrapperNode *
946
- walk_expr_tree (Expr * expr ,const PartRelationInfo * prel )
957
+ walk_expr_tree (Expr * expr ,WalkerContext * context )
947
958
{
948
959
BoolExpr * boolexpr ;
949
960
OpExpr * opexpr ;
@@ -955,20 +966,20 @@ walk_expr_tree(Expr *expr, const PartRelationInfo *prel)
955
966
/* AND, OR, NOT expressions */
956
967
case T_BoolExpr :
957
968
boolexpr = (BoolExpr * )expr ;
958
- return handle_boolexpr (boolexpr ,prel );
969
+ return handle_boolexpr (boolexpr ,context );
959
970
/* =, !=, <, > etc. */
960
971
case T_OpExpr :
961
972
opexpr = (OpExpr * )expr ;
962
- return handle_opexpr (opexpr ,prel );
973
+ return handle_opexpr (opexpr ,context );
963
974
/* IN expression */
964
975
case T_ScalarArrayOpExpr :
965
976
arrexpr = (ScalarArrayOpExpr * )expr ;
966
- return handle_arrexpr (arrexpr ,prel );
977
+ return handle_arrexpr (arrexpr ,context );
967
978
default :
968
979
result = (WrapperNode * )palloc (sizeof (WrapperNode ));
969
980
result -> orig = (const Node * )expr ;
970
981
result -> args = NIL ;
971
- result -> rangeset = list_make1_irange (make_irange (0 ,prel -> children_count - 1 , true));
982
+ result -> rangeset = list_make1_irange (make_irange (0 ,context -> prel -> children_count - 1 , true));
972
983
return result ;
973
984
}
974
985
}
@@ -977,7 +988,7 @@ walk_expr_tree(Expr *expr, const PartRelationInfo *prel)
977
988
*This function determines which partitions should appear in query plan
978
989
*/
979
990
static void
980
- handle_binary_opexpr (const PartRelationInfo * prel ,WrapperNode * result ,
991
+ handle_binary_opexpr (WalkerContext * context ,WrapperNode * result ,
981
992
const Var * v ,const Const * c )
982
993
{
983
994
HashRelationKey key ;
@@ -992,6 +1003,7 @@ handle_binary_opexpr(const PartRelationInfo *prel, WrapperNode *result,
992
1003
Oid cmp_proc_oid ;
993
1004
const OpExpr * expr = (const OpExpr * )result -> orig ;
994
1005
TypeCacheEntry * tce ;
1006
+ const PartRelationInfo * prel = context -> prel ;
995
1007
996
1008
/* Determine operator type */
997
1009
tce = lookup_type_cache (v -> vartype ,
@@ -1240,11 +1252,12 @@ range_binary_search(const RangeRelation *rangerel, FmgrInfo *cmp_func, Datum val
1240
1252
* Operator expression handler
1241
1253
*/
1242
1254
static WrapperNode *
1243
- handle_opexpr (const OpExpr * expr ,const PartRelationInfo * prel )
1255
+ handle_opexpr (const OpExpr * expr ,WalkerContext * context )
1244
1256
{
1245
1257
WrapperNode * result = (WrapperNode * )palloc (sizeof (WrapperNode ));
1246
1258
Node * firstarg = NULL ,
1247
1259
* secondarg = NULL ;
1260
+ const PartRelationInfo * prel = context -> prel ;
1248
1261
1249
1262
result -> orig = (const Node * )expr ;
1250
1263
result -> args = NIL ;
@@ -1257,13 +1270,13 @@ handle_opexpr(const OpExpr *expr, const PartRelationInfo *prel)
1257
1270
if (IsA (firstarg ,Var )&& IsA (secondarg ,Const )&&
1258
1271
((Var * )firstarg )-> varattno == prel -> attnum )
1259
1272
{
1260
- handle_binary_opexpr (prel ,result , (Var * )firstarg , (Const * )secondarg );
1273
+ handle_binary_opexpr (context ,result , (Var * )firstarg , (Const * )secondarg );
1261
1274
return result ;
1262
1275
}
1263
1276
else if (IsA (secondarg ,Var )&& IsA (firstarg ,Const )&&
1264
1277
((Var * )secondarg )-> varattno == prel -> attnum )
1265
1278
{
1266
- handle_binary_opexpr (prel ,result , (Var * )secondarg , (Const * )firstarg );
1279
+ handle_binary_opexpr (context ,result , (Var * )secondarg , (Const * )firstarg );
1267
1280
return result ;
1268
1281
}
1269
1282
}
@@ -1276,10 +1289,11 @@ handle_opexpr(const OpExpr *expr, const PartRelationInfo *prel)
1276
1289
* Boolean expression handler
1277
1290
*/
1278
1291
static WrapperNode *
1279
- handle_boolexpr (const BoolExpr * expr ,const PartRelationInfo * prel )
1292
+ handle_boolexpr (const BoolExpr * expr ,WalkerContext * context )
1280
1293
{
1281
1294
WrapperNode * result = (WrapperNode * )palloc (sizeof (WrapperNode ));
1282
1295
ListCell * lc ;
1296
+ const PartRelationInfo * prel = context -> prel ;
1283
1297
1284
1298
result -> orig = (const Node * )expr ;
1285
1299
result -> args = NIL ;
@@ -1293,7 +1307,7 @@ handle_boolexpr(const BoolExpr *expr, const PartRelationInfo *prel)
1293
1307
{
1294
1308
WrapperNode * arg ;
1295
1309
1296
- arg = walk_expr_tree ((Expr * )lfirst (lc ),prel );
1310
+ arg = walk_expr_tree ((Expr * )lfirst (lc ),context );
1297
1311
result -> args = lappend (result -> args ,arg );
1298
1312
switch (expr -> boolop )
1299
1313
{
@@ -1316,12 +1330,13 @@ handle_boolexpr(const BoolExpr *expr, const PartRelationInfo *prel)
1316
1330
* Scalar array expression
1317
1331
*/
1318
1332
static WrapperNode *
1319
- handle_arrexpr (const ScalarArrayOpExpr * expr ,const PartRelationInfo * prel )
1333
+ handle_arrexpr (const ScalarArrayOpExpr * expr ,WalkerContext * context )
1320
1334
{
1321
1335
WrapperNode * result = (WrapperNode * )palloc (sizeof (WrapperNode ));
1322
1336
Node * varnode = (Node * )linitial (expr -> args );
1323
1337
Node * arraynode = (Node * )lsecond (expr -> args );
1324
1338
int hash ;
1339
+ const PartRelationInfo * prel = context -> prel ;
1325
1340
1326
1341
result -> orig = (const Node * )expr ;
1327
1342
result -> args = NIL ;