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

# CLOSE

### CLOSE

CLOSE — 關閉游標

### 語法

```

CLOSE { name | ALL }
```

### 說明

`CLOSE` 會釋放與已開啟游標相關聯的資源。游標關閉之後，就不允許再對它進行任何後續操作。游標在不再需要時應該關閉。

當交易以 `COMMIT` 或 `ROLLBACK` 結束時，每個不可保留（non-holdable）的已開啟游標都會被隱含地關閉。若建立可保留（holdable）游標的交易經由 `ROLLBACK` 中止，該游標會被隱含地關閉。若建立它的交易成功提交，可保留游標會維持開啟，直到明確執行 `CLOSE`，或用戶端中斷連線為止。

### 參數

*`name`* : 要關閉的已開啟游標名稱。

`ALL` : 關閉所有已開啟的游標。

### 注意事項

PostgreSQL 沒有明確的 `OPEN` 游標陳述式；游標在宣告時即被視為已開啟。請使用 [`DECLARE`](https://docs.postgresql.tw/19/reference/sql-commands/sql-declare.md) 陳述式宣告游標。

你可以查詢 [`pg_cursors`](https://docs.postgresql.tw/19/internals/views/view-pg-cursors.md) 系統檢視表，查看所有可用的游標。

若游標是在某個儲存點之後關閉，而該儲存點之後被回復，則 `CLOSE` 不會被回復；也就是說，該游標仍維持關閉。

### 範例

關閉游標 `liahona`：

```

CLOSE liahona;
```

### 相容性

`CLOSE` 完全符合 SQL 標準。`CLOSE ALL` 是 PostgreSQL 的擴充語法。

### 另請參閱

[DECLARE](https://docs.postgresql.tw/19/reference/sql-commands/sql-declare.md), [FETCH](https://docs.postgresql.tw/19/reference/sql-commands/sql-fetch.md), [MOVE](https://docs.postgresql.tw/19/reference/sql-commands/sql-move.md)

***

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


---

# 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/reference/sql-commands/sql-close.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.
