Postgresql 忽略触发器上的 'when' 条件

Postgresql ignoring 'when' condition on trigger

触发器似乎忽略了我定义中的 'when condition',但我不确定为什么。我是 运行 以下人:

create trigger trigger_update_candidate_location
after update on candidates
for each row
when (
  OLD.address1 is distinct from NEW.address1
  or
  OLD.address2 is distinct from NEW.address2
  or
  OLD.city is distinct from NEW.city
  or
  OLD.state is distinct from NEW.state
  or
  OLD.zip is distinct from NEW.zip
  or
  OLD.country is distinct from NEW.country

)
execute procedure entities.tf_update_candidate_location();

但是当我重新登录时,我得到以下信息:

-- auto-generated definition
create trigger trigger_update_candidate_location
  after update
  on candidates
  for each row
execute procedure tf_update_candidate_location();

这是有问题的,因为我调用的过程最终对不同列 (lat/lng) 的相同 table 进行了更新。由于 'when' 条件被忽略,这会产生一个无限循环。

我的目的是观察地址变化,对另一个 table 进行查找以获得 lat/lng 值。

PostgreSQL 版本:10.6 IDE:DataGrip 2018.1.3

你是如何创建和"check back"的?带数据夹?

WHEN 条件是 Postgres 9.0 添加的。一些老的(或差的)客户可能已经过时了。可以肯定的是,使用以下命令检查 pgsql:

SELECT pg_get_triggerdef(oid, true)
FROM   pg_trigger
WHERE  tgrelid = 'candidates'::regclass  -- schema-qualify name to be sure
AND    NOT tgisinternal;

任何实际的 WHEN 资格都以内部格式存储在 pg_trigger.tgqual 中,顺便说一句。手册中的详细信息 here.

另外,您当前的 search_path 是什么,table candidates 的架构是什么?

很明显 table candidates 是不合格的,而触发函数 entities.tf_update_candidate_location() 具有模式限定...您没有混淆 tables在不同的数据库模式中具有相同的名称,是吗?

此外,您可以使用这个更短的等效语法来简化:

create trigger trigger_update_candidate_location
after update on candidates   -- schema-qualify??
for each row
when (
   (OLD.address1, OLD.address2, OLD.city, OLD.state, OLD.zip, OLD.country)
   IS DISTINCT FROM
   (NEW.address1, NEW.address2, NEW.city, NEW.state, NEW.zip, NEW.country)
   )
execute procedure entities.tf_update_candidate_location();

不幸的是,这是 DataGrip 的问题。请关注工单,待修好后通知。 https://youtrack.jetbrains.com/issue/DBE-7247