

 Amazon Redshift 將不再支援從修補程式 198 開始建立新的 Python UDFs。現有 Python UDF 將繼續正常運作至 2026 年 6 月 30 日。如需詳細資訊，請參閱[部落格文章](https://aws.amazon.com/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>


| 欄名稱 | 資料類型 | Description | 
| --- | --- | --- | 
| query\_id | bigint | 查詢識別碼。 | 
| function\_name | text | 使用者定義函數的名稱。 | 
| record\_time | timestamp | 建立記錄的時間。 | 
| sequence | integer | 單一記錄訊息的順序。 | 
| message | text | 記錄訊息文字。 | 

## 範例查詢
<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
```