PostgreSQL: Backend Flowchart
The parser uses gram.y and the tokens to identify the query type, and load the proper query-specific structure, like CreateStmt or SelectStmt. The parser takes a complex query, and creates a Query structure that contains all the elements used by complex queries. The optimizer 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.
Source: www.postgresql.org