> 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/indexam/index-scanning.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 63.3. 索引掃描 \#

### 63.3. 索引掃描 [#](#index-scanning) <a href="#index-scanning" id="index-scanning"></a>

在索引掃描中，索引存取方法負責吐回它被告知的所有元組（tuple）中，符合*掃描鍵*（scan key）者的 TID。存取方法*不*參與實際從索引的父資料表擷取這些元組，也不參與判斷這些元組是否通過掃描的可見性測試或其他條件。

掃描鍵是 `WHERE` 子句的內部表示形式，其格式為 *`index_key`* *`operator`* *`constant`*，其中索引鍵是該索引的某個欄位，而運算子是與該索引欄位相關聯之運算子家族的成員之一。一次索引掃描有零個或多個掃描鍵，這些掃描鍵隱含地以 AND 結合——傳回的元組預期要滿足所有指定的條件。

存取方法可以針對特定查詢回報該索引是*有損的*（lossy），或者說需要重新檢查。這表示索引掃描會傳回所有通過掃描鍵的項目，另外可能還有未通過的其他項目。核心系統的索引掃描機制接著會對堆積元組再次套用索引條件，以確認它是否真的應該被選取。若未指定重新檢查選項，索引掃描就必須精確地傳回符合的項目集合。

請注意，確保正確找出所有、且只有通過全部給定掃描鍵的項目，完全是存取方法的責任。此外，核心系統只會把所有符合索引鍵與運算子家族的 `WHERE` 子句直接交出去，不做任何語意分析來判斷它們是否多餘或互相矛盾。舉例來說，給定 `WHERE x > 4 AND x > 14`，其中 `x` 是建有 b-tree 索引的欄位，就要由 b-tree 的 `amrescan` 函式自行察覺第一個掃描鍵是多餘的、可以捨棄。`amrescan` 期間所需的前置處理程度，取決於索引存取方法需要將掃描鍵化簡為「正規化」形式的程度。

有些存取方法會以明確定義的順序傳回索引項目，有些則不會。存取方法實際上有兩種不同的方式可以支援排序後的輸出：

* 總是以其資料的自然順序傳回項目的存取方法（例如 btree）應該將 `amcanorder` 設為 true。 目前，這類存取方法的相等與排序運算子必須使用與 btree 相容的策略編號。
* 支援排序運算子的存取方法應該將 `amcanorderbyop` 設為 true。 這表示該索引能夠以滿足 `ORDER BY` *`index_key`* *`operator`* *`constant`* 的順序傳回項目。該形式的掃描修飾項可以如前所述傳給 `amrescan`。

`amgettuple` 函式有一個 `direction` 引數，其值可以是 `ForwardScanDirection`（一般情況）或 `BackwardScanDirection`。若 `amrescan` 之後的第一次呼叫指定 `BackwardScanDirection`，則符合的索引項目集合要由後往前掃描，而不是一般的由前往後方向，因此 `amgettuple` 必須傳回索引中最後一個符合的元組，而不是一般情況下的第一個。（這只會發生在將 `amcanorder` 設為 true 的存取方法上。）第一次呼叫之後，`amgettuple` 必須準備好從最近一次傳回的項目往任一方向推進掃描。（但若 `amcanbackward` 為 false，則後續所有呼叫的方向都會與第一次相同。）

支援有序掃描的存取方法必須支援在掃描中「標記」一個位置，並於稍後回到該標記位置。同一個位置可能會被還原多次。不過，每次掃描只需要記住一個位置；新的 `ammarkpos` 呼叫會覆寫先前標記的位置。不支援有序掃描的存取方法不需要在 `IndexAmRoutine` 中提供 `ammarkpos` 與 `amrestrpos` 函式；改為將這些指標設為 NULL 即可。

面對索引中並行的插入或刪除，掃描位置與標記位置（若有的話）都必須維持一致。若某個新插入的項目，在掃描開始時就已存在的話會被該掃描找到，但掃描並未傳回它，這是可以接受的；掃描在重新掃描或倒退時傳回這樣的項目，即使第一次經過時並未傳回它，也同樣可以接受。同樣地，並行的刪除可能反映、也可能不反映在掃描結果中。重要的是，插入或刪除不能讓掃描遺漏或重複傳回那些本身並未被插入或刪除的項目。

若索引儲存的是原始的索引資料值（而不是它們某種有損的表示形式），支援[僅索引掃描](https://docs.postgresql.tw/19/the-sql-language/indexes/indexes-index-only-scans.md)就很有用，此時索引會傳回實際的資料，而不只是堆積元組的 TID。只有在可見性對應表顯示該 TID 位於全部可見（all-visible）的頁面上時，這才能避免 I/O；否則仍必須存取堆積元組以檢查 MVCC 可見性。不過這不是存取方法需要關心的事。

除了使用 `amgettuple`，索引掃描也可以改用 `amgetbitmap`，在一次呼叫中擷取所有元組。這可能明顯比 `amgettuple` 更有效率，因為它可以避免在存取方法內部反覆進行鎖定／解鎖。原則上，`amgetbitmap` 應該與重複呼叫 `amgettuple` 有相同的效果，但我們加上了幾項限制以簡化問題。首先，`amgetbitmap` 會一次傳回所有元組，且不支援標記或還原掃描位置。其次，元組是以點陣圖（bitmap）的形式傳回，點陣圖沒有任何特定順序，這就是 `amgetbitmap` 不接受 `direction` 引數的原因。（這類掃描也永遠不會被提供排序運算子。）此外，`amgetbitmap` 沒有提供僅索引掃描的機制，因為無法傳回索引元組的內容。最後，`amgetbitmap` 不保證對傳回的元組做任何鎖定，其影響詳見[第 63.4 節](https://docs.postgresql.tw/19/internals/indexam/index-locking.md)。

請注意，如果存取方法的內部實作不適合其中某一種 API，允許它只實作 `amgetbitmap` 而不實作 `amgettuple`，反之亦然。

***

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


---

# 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/indexam/index-scanning.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.
