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

# 第 59 章 撰寫資料表取樣方法

### 第 59 章 撰寫資料表取樣方法

**目錄**

[59.1. 取樣方法支援函式](https://docs.postgresql.tw/19/internals/tablesample-method/tablesample-support-functions.md)

PostgreSQL 對 `TABLESAMPLE` 子句的實作，除了 SQL 標準所要求的 `BERNOULLI` 與 `SYSTEM` 方法之外，還支援自訂的資料表取樣方法。取樣方法決定了使用 `TABLESAMPLE` 子句時，會選取資料表中的哪些資料列。

在 SQL 層級，資料表取樣方法以單一 SQL 函式表示，該函式通常以 C 實作，其簽章為

```

method_name(internal) RETURNS tsm_handler
```

函式的名稱就是出現在 `TABLESAMPLE` 子句中的方法名稱。`internal` 引數是一個虛設引數 （其值永遠為零），作用只是防止此函式直接從 SQL 命令中被呼叫。 函式的結果必須是以 palloc 配置、型別為 `TsmRoutine` 的結構，其中包含指向該取樣方法各支援函式的指標。這些支援函式是一般的 C 函式，在 SQL 層級既不可見，也無法呼叫。支援函式描述於[第 59.1 節](https://docs.postgresql.tw/19/internals/tablesample-method/tablesample-support-functions.md)。

除了函式指標之外，`TsmRoutine` 結構還必須提供下列額外欄位：

`List *parameterTypes` : 這是一個 OID 清單，包含使用此取樣方法時 `TABLESAMPLE` 子句所接受之參數的資料型別 OID。例如，對於內建方法，此清單只包含一個值為 `FLOAT4OID` 的項目，代表取樣百分比。自訂取樣方法可以有更多或不同的參數。

`bool repeatable_across_queries` : 若為 `true`，則只要每次都提供相同的參數與 `REPEATABLE` 種子值，且資料表內容沒有變更，取樣方法就可以在連續的多個查詢中提供相同的樣本。當此值為 `false` 時， 此取樣方法不接受搭配使用 `REPEATABLE` 子句。

`bool repeatable_across_scans` : 若為 `true`，取樣方法可以在同一個查詢的連續多次掃描中提供相同的樣本（假設參數、種子值與快照都不變）。 當此值為 `false` 時，規劃器不會選擇需要掃描受取樣資料表超過一次的計畫，因為那可能導致查詢輸出不一致。

`TsmRoutine` 結構型別宣告於 `src/include/access/tsmapi.h`，更多細節請參閱該檔案。

在嘗試撰寫自己的資料表取樣方法時，標準發行版中所包含的取樣方法是很好的參考。內建的取樣方法請查看原始碼樹中的 `src/backend/access/tablesample` 子目錄，附加的方法則請查看 `contrib` 子目錄。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/tablesample-method.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/tablesample-method.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.
