-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpostgres.c
More file actions
186 lines (176 loc) · 5.35 KB
/
postgres.c
File metadata and controls
186 lines (176 loc) · 5.35 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
Postgres-backend top-level function call stack.
main(){ //pg程序入口 backend/main/main.c
PostmasterMain(){ // postmaster 入口
SysLogger_Start()
pgstat_init()
autovac_init()
maybe_start_bgworkers()
Serverloop(){
Select() // 运行select机制,
ConnCreate() // 建立连接
BackendStartup(){ // 拉起sql task
fork_process()+ BackendRun() // fork的子进程调用BackendRun,内部接着调用PostgresMain
PostgresMain(){ // backend 入口
ReadCommand() // 获取sql string
exec_simple_query(char* sql[]){ // 执行DML查询
start_xact_command(){
StartTransactionCommand() {
StartTransaction() // 启动事务
}
}
parsetree_list pg_parse_query(){
raw_parser(){ // parse
}
}
for each parsetree_list { // 每个parse tree优化、计划、执行
start_xact_command() // Make sure we are in a transaction command
pg_analyze_and_rewrite(){
parse_analyze(){
transformSelectStmt() {
transformFromClause()
transformTargetList()
}
}
parse_rewrite_query(){
QueryRewrite()
}
}
pg_plan_queries(){
pg_plan_query(){
standard_planner(){ //hook
PlannerInfo subquery_planner(){ //优化入口
SS_process_ctes() //cte 语法处理
pull_up_sublinks() // 上提子连接 (any exist 从句)
pull_up_subqueries() // 上提子查询
flatten_simple_union_all() // 优化sql中union all操作
preprocess_rowmarks()
expand_inherited_tables()
preprocess_expression() // 条件化简
preprocess_qual_conditions()
remove_useless_groupby_columns()
reduce_outer_joins()
grouping_planner(){ // 处理非SPJ语句并生成物理计划
query_planner(){ // 物理计划生成入口。hook
setup_simple_rel_arrays() // 找到所有基表
add_base_rels_to_query() // 生成所有基表的优化信息RelOptInfo
build_base_rel_tlists() // RelOptInfo绑定目标列和条件从句
deconstruct_jointree() // 【重要】join tree分解拉平
make_outerjoininfo()
RelOptInfo make_one_rel(PlannerInfo){ // 【重要】物理优化, 生成路径
set_base_rel_sizes(){ // RangeTblEntry选择率估计等
set_rel_size(){
set_baserel_size_estimates()
}
}
set_base_rel_pathlist(){ //针对RangeTblEntry的单表查询路径选择问题:顺序扫描,索引扫描或tid扫描
set_plain_rel_pathlist(){
set_cheapest() //选取三种路径最优的一种
}
}
make_rel_from_joinlist(){ // 多表join的路径选择: 动态规划或遗传算法
standard_join_search()
}
}
}
}
SS_identify_outer_params()
SS_charge_for_initplans()
}
create_plan(PlannerInfo){
create_scan_plan(){ // 根据path生成对应的plan tree
create_seqscan_plan()
create_samplescan_plan()
create_indexscan_plan()
create_bitmap_scan_plan()
create_tidscan_plan()
create_subqueryscan_plan()
create_functionscan_plan()
create_tablefuncscan_plan()
create_valuesscan_plan()
create_ctescan_plan()
create_namedtuplestorescan_plan()
create_worktablescan_plan()
......
}
create_join_plan()
}
SS_finalize_plan(){
}
}
}
}
CreatePortal()
PortalDefineQuery()
PortalStart() {
CreateQueryDesc(){
ExecutorStart(){
//
}
}
}
PortalRun(){
PortalRunSelect(){
standard_executorRun(){ // 执行器入口
executePlan(){
ExecProcNode(){
ExecLockRows()
ExecScan(){
SeqNext(){
......
}
}
}
}
}
}
PortalRunMulti(){
ProcessQuery(){
standard_executorRun(){
executePlan(){
ExecProcNode(){
ExecModifyTable(){
ExecInsert(){
heap_insert(){
RelationPutHeapTuple(){
PageAddItemExtended(){
}
}
}
}
}
}
}
}
}
PortalRunUtility(){
standard_ProcessUtility(){
ExecVacuum(){
vacuum(){
analyze_rel(){
do_analyze_rel(){
acquire_sample_rows()
}
}
}
}
}
}
}
}
PortalDrop()
}
finish_xact_command(){
CommitTransactionCommand(){
CommitTransaction(){ // 提交事务
RecordTransactionCommit() // 日志落盘
LockReleaseAll() // 锁释放
} // 提交事务
}
}
EndCommand()
}
}
}
}
}
}