49

PostgreSQL: Backend Flowchart

 5 years ago
source link: https://www.tuicool.com/articles/hit/Frum2q3
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Backend Flowchart

Click on an item to see more detail or look at the fullindex.

ymMnUbi.gif

A query comes to the backend via data packets arriving through TCP/IP or Unix Domain sockets. It is loaded into a string, and passed to theparser, where the lexical scanner,scan.l, breaks the query up into tokens(words). The parser usesgram.y and the tokens to identify the query type, and load the proper query-specific structure, likeCreateStmt orSelectStmt.

The statement is then identified as complex ( SELECT / INSERT / UPDATE / DELETE ) or simple, e.g CREATE USER, ANALYZE, etc. Simple utility commands that do not require the executor are processed by statement-specific functions in thecommands module. Complex statements require more handling.

The parser takes a complex query, and creates aQuery structure that contains all the elements used by complex queries. Query.qual holds the WHERE clause qualification, which is filled in by transformWhereClause(). Each table referenced in the query is represented by aRangeTableEntry, and they are linked together to form the range table of the query, which is generated by transformFromClause(). Query.rtable holds the query's range table.

Certain queries, like SELECT, return columns of data. Other queries, like INSERT and UPDATE, specify the columns modified by the query. These column references are converted toTargetEntry entries, which are linked together to make up the target list of the query. The target list is stored in Query.targetList, which is generated by transformTargetList().

Other query elements, like aggregates( SUM() ), GROUP BY, and ORDER BY are also stored in their own Query fields.

The next step is for the Query to be modified by any VIEWS or RULES that may apply to the query. This is performed by therewrite system.

Theoptimizer uses the Query structure to determine the best table join order and join type of each table in the RangeTable, using Query.qual( WHERE clause) to consider optimal index usage.

Thepath module then generates an optimal Plan,

which contains the operations to be performed to execute the query.

The Plan is then passed to theexecutor for execution, and the result returned to the client. The Plan is actually as set of nodes, arranged in a tree structure with a top-level node, and various sub-nodes as children.

There are many other modules that support this basic functionality. They can be accessed by clicking on the flowchart.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK