记录在执行用户定义的函数 (UDF) 期间生成的系统定义的错误和警告消息。
SYS_UDF_LOG 仅对超级用户可见。有关更多信息,请参阅 系统表和视图中的数据可见性。
表列
列名称 | 数据类型 | 描述 |
---|---|---|
query_id | bigint | 查询标识符。 |
function_name | 文本 | 用户定义函数的名称。 |
record_time | timestamp | 创建记录的时间。 |
sequence | 整数 | 单条日志消息的序列。 |
message | 文本 | 日志消息文本。 |
示例查询
以下示例说明 UDF 如何处理系统定义的错误。第一个块显示了返回参数的逆参数的 UDF 函数的定义。在运行函数并提供 0 作为参数时,函数返回错误。最后一条语句返回在 SYS_UDF_LOG 中记录的错误消息。
-- Create a function to find the inverse of a number.
CREATE OR REPLACE FUNCTION f_udf_inv(a int)
RETURNS float
IMMUTABLE AS $$return 1/a
$$ LANGUAGE plpythonu;
-- Run the function with 0 to create an error.
Select f_udf_inv(0);
-- Query SYS_UDF_LOG to view the message.
Select query_id, record_time, message::varchar from sys_udf_log;
query_id | record_time | message
----------+----------------------------+-------------------------------------------------------
2211 | 2023-08-23 15:53:11.360538 | ZeroDivisionError: integer division or modulo by zero line 2, in f_udf_inv\n return 1/a\n
以下示例将日志记录和警告消息添加到 UDF 中,以便被零除运算生成警告消息而不是停止并显示错误消息。
-- Create a function to find the inverse of a number and log a warning if you input 0.
CREATE OR REPLACE FUNCTION f_udf_inv_log(a int)
RETURNS float IMMUTABLE
AS $$
import logging
logger = logging.getLogger() #get root logger
if a==0:
logger.warning('You attempted to divide by zero.\nReturning zero instead of error.\n')
return 0
else:
return 1/a
$$ LANGUAGE plpythonu;
-- Run the function with 0 to trigger the warning.
Select f_udf_inv_log(0);
-- Query SYS_UDF_LOG to view the message.
Select query_id, record_time, message::varchar from sys_udf_log;
query_id | record_time | message
----------+----------------------------+-------------------------------------------------------------------------------
0 | 2023-08-23 16:10:48.833503 | WARNING: You attempted to divide by zero.\nReturning zero instead of error.\n