简单查询和扩展查询是PG前端和后端交互的时候,2种不同交互方法。简单查询时,前端发一个包含SQL的包过去,后端执行后返回结果。扩展查询则是把简单查询切分成Parse,Bind,Describe,Execute,Close等几个步骤,以达到执行计划复用的目的。扩展查询还有防止SQL注入,减少通信数据量(使用binary形式传参)的效果。
更详细的描述,可参考手册:
http://58.58.27.50:8079/doc/html/9.3.1_zh/protocol-flow.html#AEN98678
下面是服务端处理简单查询和扩展查询的代码概要
更详细的描述,可参考手册:
http://58.58.27.50:8079/doc/html/9.3.1_zh/protocol-flow.html#AEN98678
下面是服务端处理简单查询和扩展查询的代码概要
1.简单查询
点击(
此处
)折叠或打开
bind消息的处理:
GetCachedPlan()会决定新创建一个定制的执行计划还是使用之前保存的通用的执行计划。
execute消息的处理:
close消息的处理:
http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=20726500&id=4150218
src/backend/tcop/postgres.c
- exec_simple_query(const char *query_string)
- ->parsetree_list = pg_parse_query(query_string);
- ->foreach(parsetree_item, parsetree_list)
- ->querytree_list = pg_analyze_and_rewrite(parsetree, query_string,NULL, 0);
- ->plantree_list = pg_plan_queries(querytree_list, 0, NULL);
- ->portal = CreatePortal("", true, true);
- ->PortalDefineQuery(portal,NULL,query_string,commandTag,plantree_list,NULL);
- ->PortalStart(portal, NULL, 0, InvalidSnapshot);
- ->PortalRun(portal,FETCH_ALL,isTopLevel,receiver,receiver,completionTag);
- ->PortalDrop(portal, false);
2.扩展查询
parse消息的处理(命名语句):点击(此处)折叠或打开
- exec_parse_message(const char *query_string,const char *stmt_name,Oid *paramTypes,int numParams)
- ->parsetree_list = pg_parse_query(query_string);
- ->raw_parse_tree = (Node *) linitial(parsetree_list);
- ->psrc = CreateCachedPlan(raw_parse_tree, query_string, commandTag);
- ->query = parse_analyze_varparams(raw_parse_tree,query_string,¶mTypes,&numParams);
- ->querytree_list = pg_rewrite_query(query);
- ->CompleteCachedPlan(psrc,querytree_list,unnamed_stmt_context,paramTypes,numParams,NULL,NULL,0,true);
- ->StorePreparedStatement(stmt_name, psrc, false);
bind消息的处理:
点击(此处)折叠或打开
- exec_bind_message(StringInfo input_message)
- ->portal_name = pq_getmsgstring(input_message);
- ->stmt_name = pq_getmsgstring(input_message);
- ->pstmt = FetchPreparedStatement(stmt_name, true);
- ->psrc = pstmt->plansource;
- ->cplan = GetCachedPlan(psrc, params, false);
- ->portal = CreatePortal(portal_name, true, true);
- ->PortalDefineQuery(portal,saved_stmt_name,query_string,psrc->commandTag,cplan->stmt_list,cplan);
- ->PortalStart(portal, params, 0, InvalidSnapshot);
execute消息的处理:
点击(此处)折叠或打开
- exec_execute_message(const char *portal_name, long max_rows)
- ->portal = GetPortalByName(portal_name);
- ->completed = PortalRun(portal,max_rows,true,receiver,receiver,completionTag);
close消息的处理:
点击(此处)折叠或打开
- PostgresMain(int argc, char *argv[],const char *dbname,const char *username)
- case 'C':
- ->DropPreparedStatement(close_target, false);
- or
- ->PortalDrop(portal, false);
3.参考
http://58.58.27.50:8079/doc/html/9.3.1_zh/protocol-flow.html#AEN98678http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=20726500&id=4150218
src/backend/tcop/postgres.c