非易失性函数postgres中不允许更新
update is not allowed in a non volatile function postgres
我尝试使用带有多个参数的游标,函数创建没有任何问题,但是当我使用
调用我的函数时
select scratchpad.update_status()
我收到以下错误:
update is not allowed in a non volatile function
函数:
create function scrat.update_status() returns void
as
$$
DECLARE
day_to_process record;
BEGIN
FOR day_to_process in (SELECT distinct inst_status.date,inst_status.code,scrat.inst_status.id
FROM scrat.inst_status
WHERE inst_status.status ='S'
ORDER BY 1)
LOOP
raise notice 'Processing Date %', day_to_process.date::text;
update scrat.inst_status
set status = (select a.status from
(select status, max(date)
FROM scrat.inst_status
where status <> 'S'
and date::date < day_to_process.date
group by status
order by 2 desc
limit 1)a)
where inst_status.date = day_to_process.date
and id =day_to_process.id
and code=day_to_process.code;
END LOOP;
END ;
$$ LANGUAGE plpgsql STABLE;
STABLE
indicates that the function cannot modify the database, and that within a single table scan it will consistently return the same result for the same argument values, but that its result could change across SQL statements.
因此您必须将函数标记为 VOLATILE
。
我尝试使用带有多个参数的游标,函数创建没有任何问题,但是当我使用
调用我的函数时select scratchpad.update_status()
我收到以下错误:
update is not allowed in a non volatile function
函数:
create function scrat.update_status() returns void
as
$$
DECLARE
day_to_process record;
BEGIN
FOR day_to_process in (SELECT distinct inst_status.date,inst_status.code,scrat.inst_status.id
FROM scrat.inst_status
WHERE inst_status.status ='S'
ORDER BY 1)
LOOP
raise notice 'Processing Date %', day_to_process.date::text;
update scrat.inst_status
set status = (select a.status from
(select status, max(date)
FROM scrat.inst_status
where status <> 'S'
and date::date < day_to_process.date
group by status
order by 2 desc
limit 1)a)
where inst_status.date = day_to_process.date
and id =day_to_process.id
and code=day_to_process.code;
END LOOP;
END ;
$$ LANGUAGE plpgsql STABLE;
STABLE
indicates that the function cannot modify the database, and that within a single table scan it will consistently return the same result for the same argument values, but that its result could change across SQL statements.
因此您必须将函数标记为 VOLATILE
。