-
Notifications
You must be signed in to change notification settings - Fork 6
/
vectorEngine.c
125 lines (104 loc) · 2.9 KB
/
vectorEngine.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*-------------------------------------------------------------------------
*
* VectorEngine.c
* Portal of vertorized engine for Postgres.
*
* Copyright (c) 1996-2019, PostgreSQL Global Development Group
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "fmgr.h"
#include "optimizer/planner.h"
#include "executor/nodeCustom.h"
#include "utils/guc.h"
#include "nodeUnbatch.h"
#include "nodeSeqscan.h"
#include "nodeAgg.h"
#include "plan.h"
PG_MODULE_MAGIC;
/* static variables */
static bool enable_vectorize_engine;
static bool enable_vectorize_notice;
static planner_hook_type planner_hook_next;
/* static functionss */
static PlannedStmt *vector_post_planner(Query *parse, int cursorOptions,
ParamListInfo boundParams);
void _PG_init(void);
static PlannedStmt *
vector_post_planner(Query *parse,
int cursorOptions,
ParamListInfo boundParams)
{
PlannedStmt *stmt;
Plan *savedPlanTree;
List *savedSubplan;
if (planner_hook_next)
stmt = planner_hook_next(parse, cursorOptions, boundParams);
else
stmt = standard_planner(parse, cursorOptions, boundParams);
if (!enable_vectorize_engine)
return stmt;
/* modify plan by using vectorized nodes */
savedPlanTree = stmt->planTree;
savedSubplan = stmt->subplans;
PG_TRY();
{
List *subplans = NULL;
ListCell *cell;
stmt->planTree = ReplacePlanNodeWalker((Node *) stmt->planTree);
foreach(cell, stmt->subplans)
{
Plan *subplan = ReplacePlanNodeWalker((Node *)lfirst(cell));
subplans = lappend(subplans, subplan);
}
stmt->subplans = subplans;
/*
* vectorize executor exchange batch of tuples between plan nodes
* add unbatch node at top to convert batch to row and send to client.
*/
stmt->planTree = AddUnbatchNodeAtTop(stmt->planTree);
}
PG_CATCH();
{
ErrorData *edata;
edata = CopyErrorData();
FlushErrorState();
if (enable_vectorize_notice)
ereport(NOTICE,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("query can't be vectorized"),
errdetail("%s", edata->message)));
stmt->planTree = savedPlanTree;
stmt->subplans = savedSubplan;
}
PG_END_TRY();
return stmt;
}
void
_PG_init(void)
{
elog(LOG, "Initialize vectorized extension");
/* Register customscan node for vectorized scan and agg */
InitVectorScan();
InitVectorAgg();
InitUnbatch();
/* planner hook registration */
planner_hook_next = planner_hook;
planner_hook = vector_post_planner;
DefineCustomBoolVariable("enable_vectorize_engine",
"Enables vectorize engine.",
NULL,
&enable_vectorize_engine,
false,
PGC_USERSET,
GUC_NOT_IN_SAMPLE,
NULL, NULL, NULL);
DefineCustomBoolVariable("enable_vectorize_notice",
"Enables vectorize engine.",
NULL,
&enable_vectorize_notice,
true,
PGC_USERSET,
GUC_NOT_IN_SAMPLE,
NULL, NULL, NULL);
}