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

# DISCARD

### DISCARD

DISCARD — 捨棄工作階段狀態

### 語法

```

DISCARD { ALL | PLANS | SEQUENCES | TEMPORARY | TEMP }
```

### 說明

`DISCARD` 會釋放與資料庫工作階段相關聯的內部資源。此命令可用於部分或完全重設工作階段的狀態。有數個子命令可分別釋放不同類型的資源；`DISCARD ALL` 這個變體涵蓋了其他所有子命令，並且還會重設額外的狀態。

### 參數

`PLANS` : 釋放所有快取的查詢計畫，迫使下次使用相關聯的預備陳述式時重新進行規劃。

`SEQUENCES` : 捨棄所有快取的序列相關狀態，包括 `currval()`/`lastval()` 資訊，以及任何已預先配置但尚未由 `nextval()` 傳回的序列值。（預先配置序列值的說明請參閱 [CREATE SEQUENCE](https://docs.postgresql.tw/19/reference/sql-commands/sql-createsequence.md)。）

`TEMPORARY` 或 `TEMP` : 移除目前工作階段中建立的所有暫存資料表。

`ALL`

釋放與目前工作階段相關聯的所有暫時性資源，並將工作階段重設為其初始狀態。目前，這與執行下列陳述式序列的效果相同：

```

    CLOSE ALL;
    SET SESSION AUTHORIZATION DEFAULT;
    RESET ALL;
    DEALLOCATE ALL;
    UNLISTEN *;
    SELECT pg_advisory_unlock_all();
    DISCARD PLANS;
    DISCARD TEMP;
    DISCARD SEQUENCES;
```

### 注意事項

`DISCARD ALL` 不能在交易區塊內執行。

### 相容性

`DISCARD` 是 PostgreSQL 擴充功能。

***

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


---

# 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-discard.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.
