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

# ALTER OPERATOR

### ALTER OPERATOR

ALTER OPERATOR — 變更運算子的定義

### 語法

```

ALTER OPERATOR name ( { left_type | NONE } , right_type )
    OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }

ALTER OPERATOR name ( { left_type | NONE } , right_type )
    SET SCHEMA new_schema

ALTER OPERATOR name ( { left_type | NONE } , right_type )
    SET ( {  RESTRICT = { res_proc | NONE }
           | JOIN = { join_proc | NONE }
           | COMMUTATOR = com_op
           | NEGATOR = neg_op
           | HASHES
           | MERGES
          } [, ... ] )
```

### 說明

`ALTER OPERATOR` 會變更運算子的定義。

你必須擁有該運算子，才能使用 `ALTER OPERATOR`。若要變更擁有者，你必須能對新的擁有者角色執行 `SET ROLE`，且該角色必須擁有該運算子所在綱要的 `CREATE` 權限。（這些限制確保變更擁有者不會做到任何你無法藉由移除並重新建立運算子而做到的事。不過，超級使用者無論如何都可以變更任何運算子的擁有權。）

### 參數

*`name`* : 現有運算子的名稱（可選擇以綱要限定）。

*`left_type`* : 運算子左運算元的資料型別；若運算子沒有左運算元，請寫 `NONE`。

*`right_type`* : 運算子右運算元的資料型別。

*`new_owner`* : 運算子的新擁有者。

*`new_schema`* : 運算子的新綱要。

*`res_proc`* : 此運算子的限制選擇率估算函式；寫 NONE 可移除既有的選擇率估算函式。

*`join_proc`* : 此運算子的連接選擇率估算函式；寫 NONE 可移除既有的選擇率估算函式。

*`com_op`* : 此運算子的交換子。只有在運算子尚未有交換子時才能變更。

*`neg_op`* : 此運算子的否定子。只有在運算子尚未有否定子時才能變更。

`HASHES` : 表示此運算子可以支援雜湊連接。只能啟用，不能停用。

`MERGES` : 表示此運算子可以支援合併連接。只能啟用，不能停用。

### 注意事項

更多資訊請參閱[第 36.14 節](https://docs.postgresql.tw/19/server-programming/extend/xoper.md)與[第 36.15 節](https://docs.postgresql.tw/19/server-programming/extend/xoper-optimization.md)。

由於交換子是成對出現、互為彼此的交換子，`ALTER OPERATOR SET COMMUTATOR` 也會將 *`com_op`* 的交換子設為目標運算子。同樣地，`ALTER OPERATOR SET NEGATOR` 也會將 *`neg_op`* 的否定子設為目標運算子。因此，除了目標運算子之外，你也必須擁有該交換子或否定子運算子。

### 範例

變更型別 `text` 的自訂運算子 `a @@ b` 的擁有者：

```

ALTER OPERATOR @@ (text, text) OWNER TO joe;
```

變更型別 `int[]` 的自訂運算子 `a && b` 的限制與連接選擇率估算函式：

```

ALTER OPERATOR && (int[], int[]) SET (RESTRICT = _int_contsel, JOIN = _int_contjoinsel);
```

將 `&&` 運算子標記為它自己的交換子：

```

ALTER OPERATOR && (int[], int[]) SET (COMMUTATOR = &&);
```

### 相容性

SQL 標準中沒有 `ALTER OPERATOR` 陳述式。

### 另請參閱

[CREATE OPERATOR](https://docs.postgresql.tw/19/reference/sql-commands/sql-createoperator.md), [DROP OPERATOR](https://docs.postgresql.tw/19/reference/sql-commands/sql-dropoperator.md)

***

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


---

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