0
我有下面的函数返回一个触发器,但它可能有时会失败,当它触发将停止所有数据被插入到数据库中。所以,我想抓住例外,但它不起作用。我如何为下面的触发器捕获异常?
如何在postgres中捕获异常?
CREATE OR REPLACE FUNCTION update_function()
RETURNS TRIGGER
AS
'BEGIN
UPDATE
"EVENT"
SET
rules = (
SELECT
string_agg(DISTINCT st ->> ''id'', '', '') AS rules
FROM
"JSON_STORAGE" aes,
jsonb_array_elements("EVENT_DATA" -> ''fields'' #> ''{Tracking}'') AS st
WHERE
(st ->> ''id'' LIKE ''rule_%'') AND
aes."EVENT_ID" = NEW."EVENT_ID")
WHERE
NEW."EVENT_ID" = "EVENT"."EVENT_ID";
UPDATE
"EVENT"
SET
longitude =
SELECT
(CASE
WHEN (trim(both ''" '' FROM replace(regexp_replace("EVENT_DATA"-> ''fields'' -> ''custom_fields'' ->> ''device_data'', ''[\\]{2,}"([^,:])'', ''\1'', ''g''), ''\"'', ''"''))::json -> ''objects'' -> 0 -> ''data'' -> ''device_info_logs'' -> 0 ->> ''device_type'' = ''Android'') THEN
trim(both ''"'' FROM replace(regexp_replace("EVENT_DATA"-> ''fields'' -> ''custom_fields'' ->> ''inauth_device_data'', ''[\\]{2,}"([^,:])'', ''\1'', ''g''), ''\"'', ''"''))::json -> ''objects'' -> 0 -> ''data'' -> ''gps_location_logs'' -> 0 ->> ''location_wifi_longitude''
else
trim(both ''"'' FROM replace(regexp_replace("EVENT_DATA"-> ''fields'' -> ''custom_fields'' ->> ''inauth_device_data'', ''[\\]{2,}"([^,:])'', ''\1'', ''g''), ''\"'', ''"''))::json -> ''objects'' -> 0 -> ''data'' -> ''gps_location_logs'' -> 0 ->> ''longitude''
END),
latitude =
SELECT
(CASE
WHEN (trim(both ''" '' FROM replace(regexp_replace("EVENT_DATA"-> ''fields'' -> ''custom_fields'' ->> ''inauth_device_data'', ''[\\]{2,}"([^,:])'', ''\1'', ''g''), ''\"'', ''"''))::json -> ''objects'' -> 0 -> ''data'' -> ''device_info_logs'' -> 0 ->> ''device_type'' = ''Android'') THEN
trim(both ''"'' FROM replace(regexp_replace("EVENT_DATA"-> ''fields'' -> ''custom_fields'' ->> ''inauth_device_data'', ''[\\]{2,}"([^,:])'', ''\1'', ''g''), ''\"'', ''"''))::json -> ''objects'' -> 0 -> ''data'' -> ''gps_location_logs'' -> 0 ->> ''location_wifi_latitude''
else
trim(both ''"'' FROM replace(regexp_replace("EVENT_DATA"-> ''fields'' -> ''custom_fields'' ->> ''inauth_device_data'', ''[\\]{2,}"([^,:])'', ''\1'', ''g''), ''\"'', ''"''))::json -> ''objects'' -> 0 -> ''data'' -> ''gps_location_logs'' -> 0 ->> ''latitude''
END)
FROM
"JSON_STORAGE" aes
WHERE
aes."EVENT_ID" = NEW."EVENT_ID" AND
aes."EVENT_DATA"-> ''fields'' -> ''custom_fields'' ->> ''device_data'' LIKE ''%{%:%}%'' and
aes."EVENT_DATA"->''fields''->>''event_type''=''transaction_submission''
WHERE
NEW."EVENT_ID" = "EVENT"."EVENT_ID" AND
"EVENT".ip_geo_longitude IS NULL AND
"EVENT".ip_geo_latitude IS NULL;
exception when others then
raise notice ''The transaction is in an uncommittable state. ''
''Transaction was rolled back'';
RETURN
NEW;
END'
LANGUAGE 'plpgsql';
您的触发器中有一些语法错误... – joanolo
您找到了解决方案吗? – joanolo