> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/19/internals/protocol/protocol-overview.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 54.1. 概觀 \#

### 54.1. 概觀 [#](#protocol-overview) <a href="#protocol-overview" id="protocol-overview"></a>

[54.1.1. 訊息傳遞概觀](#protocol-message-concepts)

[54.1.2. 延伸查詢概觀](#protocol-query-concepts)

[54.1.3. 格式與格式代碼](#protocol-format-codes)

[54.1.4. 協定版本](https://docs.postgresql.tw/19/internals/protocol/protocol-overview/protocol-versions.md#protocol-versions)

此協定在啟動與正常運作時有不同的階段。在啟動階段，前端開啟與伺服器的連線，並以伺服器滿意的方式驗明自己的身分。（依所使用的認證方法而定，這可能只涉及單一訊息，也可能涉及多個訊息。）若一切順利，伺服器接著會將狀態資訊傳送給前端，最後進入正常運作。除了最初的啟動請求訊息之外，協定的這個部分是由伺服器主導的。

在正常運作期間，前端會將查詢與其他命令傳送給後端，而後端會傳回查詢結果與其他回應。在少數情況下（例如 `NOTIFY`），後端會傳送未經請求的訊息，但工作階段的這個部分大多是由前端的請求所主導。

工作階段的終止通常由前端決定，但在某些情況下也可能由後端強制終止。無論如何，當後端關閉連線時，它都會在結束前復原任何未結束（未完成）的交易。

在正常運作中，SQL 命令可以透過兩種子協定之一執行。在「簡單查詢」協定中，前端只傳送一個文字查詢字串，後端會將其剖析並立即執行。在「延伸查詢」協定中，查詢的處理被拆成多個步驟：剖析、參數值的繫結，以及執行。這以額外的複雜度為代價，提供了彈性與效能上的好處。

正常運作還有針對特殊操作（例如 `COPY`）的其他子協定。

#### 54.1.1. 訊息傳遞概觀 [#](#protocol-message-concepts) <a href="#protocol-message-concepts" id="protocol-message-concepts"></a>

所有的通訊都是透過訊息串流進行。訊息的第一個位元組標識訊息類型，接下來的四個位元組指定訊息其餘部分的長度（此長度計數包含它自己，但不包含訊息類型位元組）。訊息的其餘內容由訊息類型決定。基於歷史因素，用戶端傳送的第一則訊息（啟動訊息）沒有開頭的訊息類型位元組。

為了避免與訊息串流失去同步，伺服器與用戶端通常都會先（使用位元組計數）將整則訊息讀入緩衝區，再嘗試處理其內容。這使得在處理內容時偵測到錯誤的情況下能夠輕易復原。在極端情況下（例如沒有足夠的記憶體來緩衝訊息），接收端可以使用位元組計數來判斷在恢復讀取訊息之前要略過多少輸入。

反過來說，伺服器與用戶端都必須注意，絕對不要傳送不完整的訊息。這通常是藉由在開始傳送之前，先將整則訊息整理到緩衝區中來達成。若在傳送或接收訊息的中途發生通訊失敗，唯一合理的回應就是放棄該連線，因為幾乎沒有希望恢復訊息邊界的同步。

#### 54.1.2. 延伸查詢概觀 [#](#protocol-query-concepts) <a href="#protocol-query-concepts" id="protocol-query-concepts"></a>

在延伸查詢協定中，SQL 命令的執行被分成多個步驟。步驟之間保留的狀態由兩種物件表示：*預備陳述式*與 *portal*。預備陳述式代表剖析文字查詢字串並進行語意分析的結果。預備陳述式本身尚未準備好執行，因為它可能缺少*參數*的具體值。portal 代表一個已準備好執行或已部分執行的陳述式，其中所有缺少的參數值都已填入。（對於 `SELECT` 陳述式，portal 相當於一個已開啟的游標，但我們選擇使用不同的術語，因為游標無法處理非 `SELECT` 的陳述式。）

整個執行循環包含：*剖析*（parse）步驟，從文字查詢字串建立預備陳述式；*繫結*（bind）步驟，根據預備陳述式以及所需參數的值建立 portal；以及*執行*（execute）步驟，執行 portal 的查詢。對於會傳回資料列的查詢（`SELECT`、`SHOW` 等），可以要求執行步驟只擷取有限數量的資料列，因此可能需要多個執行步驟才能完成該操作。

後端可以追蹤多個預備陳述式與 portal（但請注意，它們只存在於一個工作階段之內，絕不會在工作階段之間共用）。現有的預備陳述式與 portal 是以建立時所指定的名稱來參照。此外，還存在一個「未命名」的預備陳述式與 portal。雖然它們的行為與具名物件大致相同，但對它們的操作是針對只執行一次查詢就丟棄的情況最佳化，而對具名物件的操作則是以預期會多次使用為前提來最佳化。

#### 54.1.3. 格式與格式代碼 [#](#protocol-format-codes) <a href="#protocol-format-codes" id="protocol-format-codes"></a>

特定資料型別的資料可能以數種不同*格式*中的任何一種傳輸。截至 PostgreSQL 7.4，僅支援「文字」與「二進位」兩種格式，但協定為未來的擴充預留了空間。任何值所需的格式由*格式代碼*指定。用戶端可以為每個傳輸的參數值以及查詢結果的每個欄位指定格式代碼。文字的格式代碼為零，二進位的格式代碼為一，所有其他格式代碼都保留供未來定義。

值的文字表示方式，就是該特定資料型別的輸入／輸出轉換函式所產生與接受的字串。在傳輸的表示方式中，沒有結尾的 null 字元；若前端想將接收到的值當作 C 字串處理，必須自行加上一個。（順帶一提，文字格式不允許內嵌 null。）

整數的二進位表示方式使用網路位元組順序（最高有效位元組在前）。對於其他資料型別，請查閱文件或原始碼以了解其二進位表示方式。請記住，複雜資料型別的二進位表示方式可能會隨伺服器版本而改變；文字格式通常是較具可攜性的選擇。

#### 54.1.4. 協定版本 [#](#protocol-versions) <a href="#protocol-versions" id="protocol-versions"></a>

完整內容請見 [54.1.4. 協定版本](https://docs.postgresql.tw/19/internals/protocol/protocol-overview/protocol-versions.md#protocol-versions)。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/protocol-overview.html)（原文版本：18.6；核對日期：2026-10-12）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/19/internals/protocol/protocol-overview.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
