Postgres中的AFTER触发器会阻止插入/更新吗? [英] Will an AFTER trigger in Postgres block an insert/update?

查看:118
本文介绍了Postgres中的AFTER触发器会阻止插入/更新吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在PostgreSQL中将AFTER触发器设置为在插入/更新后触发,那么调用软件是否必须等待触发器完成才能将控制权交还给调用软件?还是触发器会在后台自行运行?

If I set up an AFTER trigger in PostgreSQL to fire after an insert/update, will the calling software have to wait for the trigger to finish before returning control to the calling software? Or will the trigger run on its own behind the scenes?

推荐答案

是的,因为它是在同一事务中执行的。如果触发器失败,则插入/更新也会失败。只需做一个执行失败的查询的测试(选择一个不存在的表),就可以看到事情的运行方式以及应用程序的行为。

Yes, because it's executed within the same transaction. If the trigger fails, the insert/update will also fail. Just do a test executing a query that will fail (SELECT a table that does not exist) and you can see how things work and how your application will behave.

CREATE OR REPLACE FUNCTION foo() RETURNS TRIGGER
AS
$$
BEGIN
  EXECUTE 'SELECT fail';
END;
$$
LANGUAGE plpgsql;

这篇关于Postgres中的AFTER触发器会阻止插入/更新吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆