Skip to content

Commit 6e75ffb

Browse files
committed
check rel->baserestrictinfo for params, move support functions to utils.c
1 parent 3ede4fa commit 6e75ffb

File tree

5 files changed

+150
-52
lines changed

5 files changed

+150
-52
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# contrib/pg_pathman/Makefile
22

33
MODULE_big = pg_pathman
4-
OBJS = init.o runtimeappend.o runtime_merge_append.o pg_pathman.o dsm_array.o \
4+
OBJS = init.o utils.o runtimeappend.o runtime_merge_append.o pg_pathman.o dsm_array.o \
55
rangeset.o pl_funcs.o worker.o hooks.o nodes_common.o $(WIN32RES)
66

77
EXTENSION = pg_pathman

hooks.c

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
#include "pathman.h"
1616
#include "runtimeappend.h"
1717
#include "runtime_merge_append.h"
18+
#include "utils.h"
1819

1920

20-
static int cmp_tlist_vars(const void *a, const void *b);
21-
static List * sort_rel_tlist(List *tlist);
22-
2321
set_join_pathlist_hook_type set_join_pathlist_next = NULL;
2422
set_rel_pathlist_hook_type set_rel_pathlist_hook_next = NULL;
2523

@@ -279,6 +277,10 @@ pathman_rel_pathlist_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTb
279277
pg_pathman_enable_runtime_merge_append))
280278
return;
281279

280+
/* RuntimeAppend is pointless if there are no params in clauses */
281+
if (!clause_contains_params((Node *) get_actual_clauses(rel->baserestrictinfo)))
282+
return;
283+
282284
foreach (lc, rel->pathlist)
283285
{
284286
AppendPath *cur_path = (AppendPath *) lfirst(lc);
@@ -324,50 +326,3 @@ void pg_pathman_enable_assign_hook(bool newval, void *extra)
324326
newval ? "enabled" : "disabled");
325327
}
326328

327-
/*
328-
* Sorts reltargetlist by Var's varattno (physical order) since
329-
* we can't use static build_path_tlist() for our custom nodes.
330-
*
331-
* See create_scan_plan & use_physical_tlist for more details.
332-
*/
333-
static List *
334-
sort_rel_tlist(List *tlist)
335-
{
336-
int i;
337-
int plain_tlist_size = list_length(tlist);
338-
Var **plain_tlist = palloc(plain_tlist_size * sizeof(Var *));
339-
ListCell *tlist_cell;
340-
List *result = NIL;
341-
342-
i = 0;
343-
foreach (tlist_cell, tlist)
344-
plain_tlist[i++] = lfirst(tlist_cell);
345-
346-
qsort(plain_tlist, plain_tlist_size, sizeof(Var *), cmp_tlist_vars);
347-
348-
for (i = 0; i < plain_tlist_size; i++)
349-
result = lappend(result, plain_tlist[i]);
350-
351-
return result;
352-
}
353-
354-
/* Compare Vars by varattno */
355-
static int
356-
cmp_tlist_vars(const void *a, const void *b)
357-
{
358-
Var *v1 = *(Var **) a;
359-
Var *v2 = *(Var **) b;
360-
361-
Assert(IsA(v1, Var) && IsA(v2, Var));
362-
363-
if (v1->varattno > v2->varattno)
364-
return 1;
365-
else if (v1->varattno < v2->varattno)
366-
return -1;
367-
else
368-
{
369-
/* XXX: I really doubt this case is ok */
370-
Assert(v1->varattno != v2->varattno);
371-
return 0;
372-
}
373-
}

nodes_common.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ create_append_path_common(PlannerInfo *root,
230230
result->cpath.flags = 0;
231231
result->cpath.methods = path_methods;
232232

233-
/* TODO: real costs */
234233
result->cpath.path.startup_cost = 0.0;
235234
result->cpath.path.total_cost = 0.0;
236235

utils.c

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/* ------------------------------------------------------------------------
2+
*
3+
* utils.c
4+
* definitions of various support functions
5+
*
6+
* Copyright (c) 2016, Postgres Professional
7+
*
8+
* ------------------------------------------------------------------------
9+
*/
10+
#include "utils.h"
11+
#include "nodes/nodeFuncs.h"
12+
#include "parser/parse_param.h"
13+
#include "utils/builtins.h"
14+
#include "rewrite/rewriteManip.h"
15+
16+
17+
static bool clause_contains_params_walker(Node *node, void *context);
18+
19+
bool
20+
clause_contains_params(Node *clause)
21+
{
22+
return expression_tree_walker(clause,
23+
clause_contains_params_walker,
24+
NULL);
25+
}
26+
27+
static bool
28+
clause_contains_params_walker(Node *node, void *context)
29+
{
30+
if (node == NULL)
31+
return false;
32+
if (IsA(node, Param))
33+
return true;
34+
return expression_tree_walker(node,
35+
clause_contains_params_walker,
36+
context);
37+
}
38+
39+
static Node *
40+
replace_child_var(Var *var, replace_rte_variables_context *context)
41+
{
42+
ReplaceVarsContext *cxt = (ReplaceVarsContext *) context->callback_arg;
43+
Var *new_var;
44+
45+
new_var = makeNode(Var);
46+
memcpy(new_var, var, sizeof(Var));
47+
48+
/*
49+
* Replace a partition's Var with a Var
50+
* pointing to the RuntimeAppend's results
51+
*/
52+
new_var->varno = cxt->parent->relid;
53+
new_var->location = -1;
54+
new_var->varnoold = 0;
55+
56+
return (Node *) new_var;
57+
}
58+
59+
Node *
60+
replace_child_vars_with_parent_var(Node *node, ReplaceVarsContext *context)
61+
{
62+
return replace_rte_variables(node, context->child->relid, context->sublevels_up,
63+
replace_child_var, (void *) context, NULL);
64+
}
65+
66+
/*
67+
* Sorts reltargetlist by Var's varattno (physical order) since
68+
* we can't use static build_path_tlist() for our custom nodes.
69+
*
70+
* See create_scan_plan & use_physical_tlist for more details.
71+
*/
72+
List *
73+
sort_rel_tlist(List *tlist)
74+
{
75+
int i;
76+
int plain_tlist_size = list_length(tlist);
77+
Var **plain_tlist = palloc(plain_tlist_size * sizeof(Var *));
78+
ListCell *tlist_cell;
79+
List *result = NIL;
80+
81+
i = 0;
82+
foreach (tlist_cell, tlist)
83+
plain_tlist[i++] = lfirst(tlist_cell);
84+
85+
qsort(plain_tlist, plain_tlist_size, sizeof(Var *), cmp_tlist_vars);
86+
87+
for (i = 0; i < plain_tlist_size; i++)
88+
result = lappend(result, plain_tlist[i]);
89+
90+
return result;
91+
}
92+
93+
/* Compare Vars by varattno */
94+
int
95+
cmp_tlist_vars(const void *a, const void *b)
96+
{
97+
Var *v1 = *(Var **) a;
98+
Var *v2 = *(Var **) b;
99+
100+
Assert(IsA(v1, Var) && IsA(v2, Var));
101+
102+
if (v1->varattno > v2->varattno)
103+
return 1;
104+
else if (v1->varattno < v2->varattno)
105+
return -1;
106+
else
107+
{
108+
/* XXX: I really doubt this case is ok */
109+
Assert(v1->varattno != v2->varattno);
110+
return 0;
111+
}
112+
}

utils.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* ------------------------------------------------------------------------
2+
*
3+
* utils.h
4+
* prototypes of various support functions
5+
*
6+
* Copyright (c) 2016, Postgres Professional
7+
*
8+
* ------------------------------------------------------------------------
9+
*/
10+
#ifndef UTILS_H
11+
#define UTILS_H
12+
13+
#include "postgres.h"
14+
#include "nodes/relation.h"
15+
#include "nodes/nodeFuncs.h"
16+
17+
typedef struct
18+
{
19+
RelOptInfo *child;
20+
RelOptInfo *parent;
21+
int sublevels_up;
22+
} ReplaceVarsContext;
23+
24+
bool clause_contains_params(Node *clause);
25+
26+
Node * replace_child_vars_with_parent_var(Node *node,
27+
ReplaceVarsContext *context);
28+
29+
int cmp_tlist_vars(const void *a, const void *b);
30+
List * sort_rel_tlist(List *tlist);
31+
32+
#endif

0 commit comments

Comments
 (0)