

 从补丁 198 开始，Amazon Redshift 将不再支持创建新的 Python UDF。现有的 Python UDF 将继续正常运行至 2026 年 6 月 30 日。有关更多信息，请参阅[博客文章](https://www.amazonaws.cn/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)。

# SYS\_UDF\_LOG
<a name="SYS_UDF_LOG"></a>

记录在执行用户定义的函数 (UDF) 期间生成的系统定义的错误和警告消息。

SYS\_UDF\_LOG 仅对超级用户可见。有关更多信息，请参阅 [系统表和视图中的数据可见性](cm_chap_system-tables.md#c_visibility-of-data)。

## 表列
<a name="SYS_UDF_LOG-table-rows"></a>


| 列名称 | 数据类型 | 说明 | 
| --- | --- | --- | 
| query\_id | bigint | 查询标识符。 | 
| function\_name | 文本 | 用户定义函数的名称。 | 
| record\_time | timestamp | 创建记录的时间。 | 
| sequence | 整数 | 单条日志消息的序列。 | 
| message | 文本 | 日志消息文本。 | 

## 示例查询
<a name="SYS_UDF_LOG-sample-queries"></a>

以下示例说明 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
```