创建一个函数并捕获异常。您可以提出自己的错误消息:
create or replace function calculate_with_error
(a double precision, b double precision, c double precision, x double precision)
returns double precision language plpgsql as $$
begin
return a * exp(-b * x) + c;
exception when numeric_value_out_of_range then
raise exception 'Value out of range for a = % b= % c= % x= %', a, b, c, x;
end $$;
select calculate_with_error(1, 1, 0, 10000);
ERROR: Value out of range for a = 1 b= 1 c= 0 x= 10000
或设置一些默认值:
create or replace function calculate_with_default
(a double precision, b double precision, c double precision, x double precision)
returns double precision language plpgsql as $$
begin
return a * exp(-b * x) + c;
exception when numeric_value_out_of_range then
return 0;
end $$;
select calculate_with_default(1, 1, 0, 10000);
calculate_with_default
------------------------
0
(1 row)
用法:
update curve_by_wkt
set samplepoint05 = calculate_with_error(a, b, c, 0.05);