Postgresql 中文操作指南
52.2. How Connections Are Established #
PostgreSQL 实施了“每用户一个进程”的客户端/服务器模型。在这个模型中,每个 [role="bare"]glossary.html#GLOSSARY-CLIENT client process 连接到一个 [role="bare"]glossary.html#GLOSSARY-BACKEND backend process 。由于我们无法提前得知将建立多少连接,我们必须使用一个“监控进程”,它会在每次请求连接时生成一个新的后端进程。此监控进程称为 [role="bare"]glossary.html#GLOSSARY-POSTMASTER postmaster ,它在指定的 TCP/IP 端口侦听传入连接。每当它检测到连接请求时,它就会生成一个新的后端进程。这些后端进程通过 semaphores 和 [role="bare"]glossary.html#GLOSSARY-SHARED-MEMORY shared memory 互相通信并与 [role="bare"]glossary.html#GLOSSARY-INSTANCE instance 的其他进程通信,以确保在并发数据访问过程中数据完整性。
PostgreSQL implements a “process per user” client/server model. In this model, every [role="bare"]glossary.html#GLOSSARY-CLIENTclient process connects to exactly one [role="bare"]glossary.html#GLOSSARY-BACKENDbackend process. As we do not know ahead of time how many connections will be made, we have to use a “supervisor process” that spawns a new backend process every time a connection is requested. This supervisor process is called [role="bare"]glossary.html#GLOSSARY-POSTMASTERpostmaster and listens at a specified TCP/IP port for incoming connections. Whenever it detects a request for a connection, it spawns a new backend process. Those backend processes communicate with each other and with other processes of the [role="bare"]glossary.html#GLOSSARY-INSTANCEinstance using semaphores and [role="bare"]glossary.html#GLOSSARY-SHARED-MEMORYshared memory to ensure data integrity throughout concurrent data access.
客户端进程可以是任何理解 Chapter 55 中描述的 PostgreSQL 协议的程序。许多客户端基于 C 语言库 libpq,但有一些协议的独立实现,例如 Java JDBC 驱动程序。
The client process can be any program that understands the PostgreSQL protocol described in Chapter 55. Many clients are based on the C-language library libpq, but several independent implementations of the protocol exist, such as the Java JDBC driver.
一旦建立连接,客户端进程可以向其连接的后端进程发送查询。查询使用纯文本传输,即,客户端不进行解析。后端进程解析查询,创建一个 execution plan,执行该计划,并通过已建立的连接将检索到的行传输给客户端。
Once a connection is established, the client process can send a query to the backend process it’s connected to. The query is transmitted using plain text, i.e., there is no parsing done in the client. The backend process parses the query, creates an execution plan, executes the plan, and returns the retrieved rows to the client by transmitting them over the established connection.