当前位置:网站首页>Overall query process of PostgreSQL

Overall query process of PostgreSQL

2022-07-07 02:21:00 happytree001

Client pass pg The general process of server query is as follows

 Insert picture description here

One 、 Establishing a connection

The client first needs to connect pg The server , After the server establishes a connection with the client fork A subprocess handles subsequent client requests , The server then waits for the next client to connect .

Two 、 Send a request

The client sends the request data to pg The server ,pg Subprocess read request .

  • The client sends the request
    Like the command line , Read user input , Send to the server
    src/bin/psql/mainloop.c
int
MainLoop(FILE *source)
{
    
    ...
	SendQuery(query_buf->data);
    ...
}
  • Server receives request
void
PostgresMain(int argc, char *argv[],
			 const char *dbname,
			 const char *username)
{
    
    ...
    for (;;)
	{
    
    	...
        firstchar = ReadCommand(&input_message);    
    	...
    }
}

3、 ... and 、 analysis

Take a simple query as an example . The server receives a string , You need to parse and understand this data to execute .

src/backend/tcop/postgres.c

static void
exec_simple_query(const char *query_string)
{
    
    ...
    parsetree_list = pg_parse_query(query_string);
    ...
}

Through lexical and grammatical analysis , Build a primitive syntax tree .

Four 、 Optimize

static void
exec_simple_query(const char *query_string)
{
    
    ...
    querytree_list = pg_analyze_and_rewrite(parsetree, query_string,
                                            NULL, 0, NULL);

    plantree_list = pg_plan_queries(querytree_list, query_string,
                                    CURSOR_OPT_PARALLEL_OK, NULL);

     ..
}

Transform the original syntax tree into a query tree , And optimize and rewrite the query tree , Improve execution speed .

5、 ... and 、 perform

static void
exec_simple_query(const char *query_string)
{
    
    ...
    (void) PortalRun(portal,
                     FETCH_ALL,
                     true,	/* always top level */
                     true,
                     receiver,
                     receiver,
                     &qc);
    ...
}

6、 ... and 、 Return results

The specific function is also found

原网站

版权声明
本文为[happytree001]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207061839087921.html