DEV Community

Mahina Sheikh
Mahina Sheikh

Posted on

PostgreSQL Architecture

PostgreSQL is a popular open-source relational database management system that is widely used by organizations of all sizes for its robustness, reliability, and extensibility. In this blog, we will explore the architecture of PostgreSQL and how it executes statements.

Server Architecture

PostgreSQL follows a process-based server architecture, where each connection from a client is handled by a separate process called the backend process. The backend process is responsible for processing queries and returning the results to the client. The server also has a number of other components, including the Process Manager, Connection Manager, Parser, Query Optimizer, and Executor, which work together to handle client requests.

Configuring PostgreSQL

PostgreSQL can be configured using its configuration file, which is located in the root of the data directory. The data directory is specified using the ‘-D’ or ‘–pgdata’ switches when starting PostgreSQL using the ‘pg_ctl start’ command.

Client-Server Connection

A client connects to a unique, private database process on the server, which is commonly referred to as the backend. The connection is established using PostgreSQL's wire protocol version 3, which has been in use since PostgreSQL version 7.

Query Modes

When a query is sent from a client to the server for execution, it can be sent in two query modes - simple query protocol mode and extended query protocol mode.

Server Statement Execution

The backend process executes a query based on a memory tree structure called the ‘plan tree.’ The following are the steps involved in executing a statement in PostgreSQL:

  1. Parse: In this step, the backend process performs a syntactical parse of the statement to create a parse tree from the query.

  2. Rewrite / Parse: The backend process performs a semantic parse of the statement, which includes the validation of the existence of the table and columns, etc. to create a query tree from the query.

  3. Rewrite / Rule: In this step, the rule system can rewrite views from the view definition to the table definition.

  4. Plan: The planner or optimizer takes a query tree and evaluates all access paths for a given query tree based on ‘cost’. This results in a plan tree of the most optimal path.

  5. Execute: The executor takes the plan tree and executes each plan node. It represents a principal step in the plan tree and returns the result to the client.

Conclusion

PostgreSQL is a robust and reliable database management system that is widely used in many industries. Its process-based server architecture and use of a memory tree structure for executing statements make it highly efficient and scalable. By understanding the architecture and statement execution process of PostgreSQL, developers can optimize their database performance and build more efficient applications.

References
https://www.yugabyte.com/postgresql/postgresql-architecture

Top comments (0)