2025年9月25日: PostgreSQL 18 发布!
支持版本: 当前 (18) / 17 / 16 / 15 / 14 / 13
开发版本: 开发
不支持版本: 12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0

44.9. 实用函数 #

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.errorplpy.fatal 实际上会引发一个 Python 异常,如果该异常未被捕获,它将传播到调用查询,导致当前事务或子事务被中止。raise plpy.Error(msg)raise plpy.Fatal(msg) 分别等同于调用 plpy.error(msg)plpy.fatal(msg),但 raise 形式不允许传递关键字参数。其他函数仅生成不同优先级级别的消息。消息是否会报告给客户端、写入服务器日志或两者都会,由 log_min_messagesclient_min_messages 配置变量控制。有关更多信息,请参阅 第 19 章

msg 参数作为位置参数给出。为了向后兼容,可以给出多个位置参数。在这种情况下,位置参数元组的字符串表示形式将成为报告给客户端的消息。

接受以下仅关键字参数:

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 节 中描述的内置引用函数。它们在构建临时查询时很有用。来自 示例 41.1 的动态 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)))

提交更正

如果您在文档中看到任何不正确、与您对特定功能的体验不符或需要进一步澄清的内容,请使用 此表格 报告文档问题。