↑↓ 选择 ↵ 打开 ⌫ 改范围 完整检索页

pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。

不受支持的版本: 6.5 / 6.4
历史版本PostgreSQL 6.4 已于 2003 年 10 月结束社区维护,本页译文保留供仍在使用旧版本的读者参考。新系统请看当前版本手册首页。

59.2. Protocol

This section describes the message flow. There are four different types of flows depending on the state of the connection: startup, query, function call, and termination. There are also special provisions for notification responses and command cancellation, which can occur at any time after the startup phase.

59.2.1. Startup

Startup is divided into an authentication phase and a backend startup phase.

Initially, the frontend sends a StartupPacket. The postmaster uses this info and the contents of the pg_hba.conf(5) file to determine what authentication method the frontend must use. The postmaster then responds with one of the following messages:

ErrorResponse

然后 postmaster 立即关闭连接。

AuthenticationOk

然后 postmaster 移交给后端。postmaster 不再参与通信。

AuthenticationKerberosV4

然后前端必须与 postmaster 进行 Kerberos V4 认证对话(此处不描述)。如果成功,postmaster 以 AuthenticationOk 响应,否则以 ErrorResponse 响应。

AuthenticationKerberosV5

然后前端必须与 postmaster 进行 Kerberos V5 认证对话(此处不描述)。如果成功,postmaster 以 AuthenticationOk 响应,否则以 ErrorResponse 响应。

AuthenticationUnencryptedPassword

然后前端必须发送一个 UnencryptedPasswordPacket。如果密码正确,postmaster 以 AuthenticationOk 响应,否则以 ErrorResponse 响应。

AuthenticationEncryptedPassword

然后前端必须发送一个 EncryptedPasswordPacket。如果密码正确,postmaster 以 AuthenticationOk 响应,否则以 ErrorResponse 响应。

如果前端不支持 postmaster 请求的认证方法,那么它应当立即关闭连接。

After sending AuthenticationOk, the postmaster attempts to launch a backend process. Since this might fail, or the backend might encounter a failure during startup, the frontend must wait for the backend to acknowledge successful startup. The frontend should send no messages at this point. The possible messages from the backend during this phase are:

BackendKeyData

This message is issued after successful backend startup. It provides secret-key data that the frontend must save if it wants to be able to issue cancel requests later. The frontend should not respond to this message, but should continue listening for a ReadyForQuery message.

ReadyForQuery

后端启动成功。前端现在可以发出查询或函数调用消息。

ErrorResponse

后端启动失败。发送此消息后连接被关闭。

NoticeResponse

发出了警告消息。前端应显示该消息,但继续等待 ReadyForQuery 或 ErrorResponse。

ReadyForQuery 消息与后端在每个查询周期之后发出的 消息相同。 Depending on the coding needs of the frontend, it is reasonable to consider ReadyForQuery as starting a query cycle (and then BackendKeyData indicates successful conclusion of the startup phase), or to consider ReadyForQuery as ending the startup phase and each subsequent query cycle.

59.2.2. Query

查询周期由前端向 backend 发送 Query 消息发起。然后后端根据查询命令串的内容发送一个或多个响应消息,最后发送 ReadyForQuery 响应消息。 ReadyForQuery informs the frontend that it may safely send a new query or function call.

The possible response messages from the backend are:

CompletedResponse

An SQL command completed normally.

CopyInResponse

后端准备好把数据从前端复制到关系。前端然后应发送 CopyDataRows 消息。后端随后将以带 "COPY" 标签的 CompletedResponse 消息响应。

CopyOutResponse

后端准备好把数据从关系复制到前端。然后它发送 CopyDataRows 消息,再发送带 "COPY" 标签的 CompletedResponse 消息。

CursorResponse

查询是 insert(l)、delete(l)、update(l)、fetch(l) 或 select(l) 命令之一。如果事务已被中止,则后端发送带 "*ABORT STATE*" 标签的 CompletedResponse 消息。否则发送以下响应。

对于 insert(l) 命令,后端随后发送一条 带 "INSERT oid rows" 标签的 CompletedResponse 消息, 其中 rows 是插入的行数,oid 在 rows 为 1 时是所插入行的对象 ID,否则 oid 为 0。

对于 delete(l) 命令,后端随后发送一条 带 "DELETE rows" 标签的 CompletedResponse 消息, 其中 rows 是删除的行数。

对于 update(l) 命令,后端随后发送一条 带 "UPDATE rows" 标签的 CompletedResponse 消息, 其中 rows 是更新的行数。

对于 fetch(l) 或 select(l) 命令,后端发送一条 RowDescription 消息。然后对返回给前端的每一行, 跟着一条 AsciiRow 或 BinaryRow 消息(取决于是否指定了 二进制游标)。最后,后端发送一条带 "SELECT" 标签的 CompletedResponse 消息。

EmptyQueryResponse

识别到一个空查询串。(需要特别区分这种情况是历史原因。)

ErrorResponse

发生了一个错误。

ReadyForQuery

查询串的处理完成。发送单独的消息来指示这一点,因为查询串可能包含多条 SQL 命令。 (CompletedResponse marks the end of processing one SQL command, not the whole string.) ReadyForQuery will always be sent, whether processing terminates successfully or with an error.

NoticeResponse

发出了与查询有关的警告消息。通知是其他响应之外的附加内容,即后端将继续处理该命令。

每当期待任何其他类型的消息时,前端必须准备好接受 ErrorResponse 和 NoticeResponse 消息。

Actually, it is possible for NoticeResponse to arrive even when the frontend is not expecting any kind of message, that is, the backend is nominally idle. (In particular, the backend can be commanded to terminate by its postmaster. In that case it will send a NoticeResponse before closing the connection.) It is recommended that the frontend check for such asynchronous notices just before issuing any new command.

此外,如果前端发出任何 listen(l) 命令,那么它必须准备好在任何时间接受 NotificationResponse 消息;见下文。

59.2.3. 函数调用

A Function Call cycle is initiated by the frontend sending a FunctionCall message to the backend. The backend then sends one or more response messages depending on the results of the function call, and finally a ReadyForQuery response message. ReadyForQuery informs the frontend that it may safely send a new query or function call.

The possible response messages from the backend are:

ErrorResponse

发生了一个错误。

FunctionResultResponse

函数调用已执行并返回了结果。

FunctionVoidResponse

函数调用已执行但没有返回结果。

ReadyForQuery

函数调用的处理完成。无论处理成功还是出错,ReadyForQuery 都将被发送。

NoticeResponse

发出了与函数调用有关的警告消息。通知是其他响应之外的附加内容,即后端将继续处理该命令。

每当期待任何其他类型的消息时,前端必须准备好接受 ErrorResponse 和 NoticeResponse 消息。 Also, if it issues any listen(l) commands then it must be prepared to accept NotificationResponse messages at any time; see below.

59.2.4. Notification Responses

If a frontend issues a listen(l) command, then the backend will send a NotificationResponse message (not to be confused with NoticeResponse!) whenever a notify(l) command is executed for the same notification name.

Notification responses are permitted at any point in the protocol (after startup), except within another backend message. Thus, the frontend must be prepared to recognize a NotificationResponse message whenever it is expecting any message. Indeed, it should be able to handle NotificationResponse messages even when it is not engaged in a query.

NotificationResponse

A notify(l) command has been executed for a name for which a previous listen(l) command was executed. Notifications may be sent at any time.

值得指出的是,listen 和 notify 命令中使用的名字不必与 SQL 数据库中关系(表)的名字 有任何关系。 Notification names are simply arbitrarily chosen condition names.

59.2.5. Cancelling Requests in Progress

During the processing of a query, the frontend may request cancellation of the query by sending an appropriate request to the postmaster. The cancel request is not sent directly to the backend for reasons of implementation efficiency: we don't want to have the backend constantly checking for new input from the frontend during query processing. Cancel requests should be relatively infrequent, so we make them slightly cumbersome in order to avoid a penalty in the normal case.

To issue a cancel request, the frontend opens a new connection to the postmaster and sends a CancelRequest message, rather than the StartupPacket message that would ordinarily be sent across a new connection. The postmaster will process this request and then close the connection. For security reasons, no direct reply is made to the cancel request message.

A CancelRequest message will be ignored unless it contains the same key data (PID and secret key) passed to the frontend during connection startup. If the request matches the PID and secret key for a currently executing backend, the postmaster signals the backend to abort processing of the current query.

The cancellation signal may or may not have any effect --- for example, if it arrives after the backend has finished processing the query, then it will have no effect. If the cancellation is effective, it results in the current command being terminated early with an error message.

The upshot of all this is that for reasons of both security and efficiency, the frontend has no direct way to tell whether a cancel request has succeeded. It must continue to wait for the backend to respond to the query. Issuing a cancel simply improves the odds that the current query will finish soon, and improves the odds that it will fail with an error message instead of succeeding.

Since the cancel request is sent to the postmaster and not across the regular frontend/backend communication link, it is possible for the cancel request to be issued by any process, not just the frontend whose query is to be canceled. This may have some benefits of flexibility in building multiple-process applications. It also introduces a security risk, in that unauthorized persons might try to cancel queries. The security risk is addressed by requiring a dynamically generated secret key to be supplied in cancel requests.

59.2.6. Termination

The normal, graceful termination procedure is that the frontend sends a Terminate message and immediately closes the connection. On receipt of the message, the backend immediately closes the connection and terminates.

不优雅的终止可能由于任一端的软件故障(即核心转储)而发生。如果前端或后端看到连接意外关闭,应当清理并终止。 The frontend has the option of launching a new backend by recontacting the postmaster, if it doesn't want to terminate itself.

提交更正

译文有误、术语不当或页面显示问题,请到译文仓库 pgsty/pgdoc 报告译文问题。 英文原文本身的问题,请在当前版本的对应页面向上游反馈;上游不再修订已结束维护的版本。