我目前对立即约束检查的确切时间感到困惑。希望下面的例子能解决我的困惑:
create table a (
id int primary key
);
create table b (
id int primary key,
a_id int not null references a
);
/* violates foreign key constraint "b_a_id_fkey" */
with t1 as (insert into b values (100, 200) returning id, a_id)
select * from t1;
/* ERROR: expensive_exception_thrower */
with t1 as (insert into b values (100, 200) returning id, a_id)
select * from t1 where expensive_exception_thrower(t1.a_id) = true;
Run Code Online (Sandbox Code Playgroud)
在第二个查询中,尽管引用了 t1,但expensive_exception_thrower仍会首先抛出其异常,这会导致 fkey 异常被吞没。当然,有解决方法,但是当 Postgres …