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

# 44.9. 公用函式 \#

### 44.9. 公用函式 [#](#plpython-util) <a href="#plpython-util" id="plpython-util"></a>

`plpy` 模組還提供了下列函式

| `plpy.debug(`*`msg, **kwargs`*`)`   |
| ----------------------------------- |
| `plpy.log(`*`msg, **kwargs`*`)`     |
| `plpy.info(`*`msg, **kwargs`*`)`    |
| `plpy.notice(`*`msg, **kwargs`*`)`  |
| `plpy.warning(`*`msg, **kwargs`*`)` |
| `plpy.error(`*`msg, **kwargs`*`)`   |
| `plpy.fatal(`*`msg, **kwargs`*`)`   |

`plpy.error` 與 `plpy.fatal` 實際上會拋出一個 Python 例外，若未被攔截，就會向外傳播到呼叫的查詢，使目前的交易或子交易中止。`raise plpy.Error(msg)` 與 `raise plpy.Fatal(msg)` 分別等同於呼叫 `plpy.error(msg)` 與 `plpy.fatal(msg)`，但 `raise` 的形式不允許傳入關鍵字引數。其餘的函式則只會產生不同優先層級的訊息。特定優先層級的訊息是否回報給用戶端、寫入伺服器日誌或兩者皆是，由 [log\_min\_messages](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-logging.md#GUC-LOG-MIN-MESSAGES) 與 [client\_min\_messages](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-client.md#GUC-CLIENT-MIN-MESSAGES) 這兩個組態變數控制。更多資訊請參閱[第 19 章](https://docs.postgresql.tw/19/server-administration/runtime-config.md)。

*`msg`* 引數是以位置引數的方式給定。為了向後相容，也可以給定多個位置引數。在那種情況下，這組位置引數所構成之 tuple（值組）的字串表示法就會成為回報給用戶端的訊息。

以下這些只能以關鍵字形式給定的引數是被接受的：

| `detail`          |
| ----------------- |
| `hint`            |
| `sqlstate`        |
| `schema_name`     |
| `table_name`      |
| `column_name`     |
| `datatype_name`   |
| `constraint_name` |

以關鍵字引數傳入之物件的字串表示法，會被用來豐富回報給用戶端的訊息。例如：

```

CREATE FUNCTION raise_custom_exception() RETURNS void AS $$
plpy.error("custom exception message",
           detail="some info about exception",
           hint="hint for users")
$$ LANGUAGE plpython3u;

=# SELECT raise_custom_exception();
ERROR:  plpy.Error: custom exception message
DETAIL:  some info about exception
HINT:  hint for users
CONTEXT:  Traceback (most recent call last):
  PL/Python function "raise_custom_exception", line 4, in <module>
    hint="hint for users")
PL/Python function "raise_custom_exception"
```

另一組公用函式是 `plpy.quote_literal(string)`、`plpy.quote_nullable(string)` 與 `plpy.quote_ident(string)`。它們等同於[第 9.4 節](https://docs.postgresql.tw/19/the-sql-language/functions/functions-string.md)所描述的內建引號處理函式，在組建臨時查詢時相當有用。[範例 41.1](https://docs.postgresql.tw/19/server-programming/plpgsql/plpgsql-statements.md#plpgsql-quote-literal-example) 中的動態 SQL 若改寫成 PL/Python 版本，會是：

```

plpy.execute("UPDATE tbl SET %s = %s WHERE key = %s" % (
    plpy.quote_ident(colname),
    plpy.quote_nullable(newvalue),
    plpy.quote_literal(keyvalue)))
```

***

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


---

# 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/plpython/plpython-util.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.
