本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用者定義函數 (UDF) 執行期間產生的記錄系統定義的錯誤和警告訊息。
只有超級使用者才能看到 SYS_UDF_LOG。如需詳細資訊,請參閱系統資料表和檢視中資料的可見性。
資料表欄
欄名稱 | 資料類型 | 描述 |
---|---|---|
query_id | bigint | 查詢識別碼。 |
function_name | text | 使用者定義函數的名稱。 |
record_time | timestamp | 建立記錄的時間。 |
sequence | integer | 單一記錄訊息的順序。 |
message | text | 記錄訊息文字。 |
範例查詢
以下範例說明 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