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

# SPI\_saveplan

### SPI\_saveplan

SPI\_saveplan — 儲存已準備的陳述式

### 語法

```

SPIPlanPtr SPI_saveplan(SPIPlanPtr plan)
```

### 說明

`SPI_saveplan` 會將傳入、由 `SPI_prepare` 準備的陳述式複製到不會由 `SPI_finish` 或交易管理器釋放的記憶體，並傳回已複製陳述式的指標。因此可在目前工作階段後續呼叫 C 函式時重複使用已準備的陳述式。

### 引數

`SPIPlanPtr plan` : 要儲存的已準備陳述式。

### 回傳值

傳回已複製陳述式的指標；失敗時傳回 `NULL`。發生錯誤時，`SPI_result` 設定如下：

`SPI_ERROR_ARGUMENT` : *`plan`* 為 `NULL` 或無效。

`SPI_ERROR_UNCONNECTED` : 從未連線的 C 函式呼叫。

### 注意事項

原始傳入的陳述式不會被釋放，因此你可能要對它執行 `SPI_freeplan`，以避免在 `SPI_finish` 前發生記憶體洩漏。

大多數情況下應優先使用 `SPI_keepplan`，因為它不需實際複製已準備陳述式的資料結構，就能大致達成相同結果。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/spi-spi-saveplan.html)（原文版本：18.6；核對日期：2026-09-11）


---

# 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/server-programming/spi/spi-interface/spi-spi-saveplan.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.
