postgresql上的不一致记录数据
我是postgresql的新手。 我正在尝试学习如何使用触发器和postgresql将sum插入到另一个表中 我想将表invoice\u items中的数据current\u balance插入表tbl\u cb 这是我的函数脚本postgresql上的不一致记录数据,postgresql,database-trigger,Postgresql,Database Trigger,我是postgresql的新手。 我正在尝试学习如何使用触发器和postgresql将sum插入到另一个表中 我想将表invoice\u items中的数据current\u balance插入表tbl\u cb 这是我的函数脚本 CREATE OR REPLACE FUNCTION rec_cb_insert() RETURNS trigger AS $build_cb_insert$ begin if new.type = 'CBA_ADJ' then INSERT IN
CREATE OR REPLACE FUNCTION rec_cb_insert()
RETURNS trigger AS $build_cb_insert$
begin
if new.type = 'CBA_ADJ' then
INSERT INTO tbl_cb(current_balance)
select SUM(amount)FROM invoice_items
WHERE account_id='6015ec0f-4d30-4802-bbcc-ea35c12f93a2'
and type='CBA_ADJ';
end if;
RETURN new;
END;
$build_cb_insert$
LANGUAGE plpgsql;
这是我的扳机
create trigger insert_cb_insert
after insert
on invoice_items
for each row
execute procedure rec_cb_insert()
;
当我插入没有线程数据列的数据时
无螺纹:
但是当我使用线程数据列时,当前的平衡不是一致性
带螺纹:
如何解决
2019年9月20日更新
这是我的新功能
CREATE OR REPLACE FUNCTION public.insert_currentbalance_3()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
DECLARE
current_balance numeric;
BEGIN
-- Check that empname and salary are given
IF new.type IS NULL THEN
RAISE EXCEPTION 'type cannot be null';
END IF;
IF new.account_id IS NULL THEN
RAISE EXCEPTION 'account_id cannot be null';
END IF;
IF new.type = 'CBA_ADJ' then
EXECUTE'SELECT SUM(amount) FROM invoice_items WHERE
account_id= new.account_id and type=new.type new.invoice_id'
into current_balance;
insert into balance (invoice_id, balance, invoice_items_id)
values (new.invoice_id,current_balance,new.invoice_id);
END IF;
-- Remember who changed the payroll when
-- NEW.last_date := current_timestamp;
-- NEW.last_user := current_user;
RETURN NEW;
END;
$function$
;
这是我的扳机
CREATE TRIGGER insert_current_balance AFTER
INSERT
ON
FOR EACH ROW
EXECUTE PROCEDURE function_insert_currentbalance3();
现在我发现了一个错误
原因:org.postgresql.util.psql异常:错误:由于事务之间的读/写依赖关系,无法序列化访问。
如何解决(您能描述一下您认为不一致的内容并显示
CREATE TRIGGER
语句吗?Column current_balance,当我插入不使用线程的记录一致时,但当我插入使用线程的记录不一致时。