UPDATE: I eliminated Hibernate from the problem. I completely reworked description of problem to simplify it as much as possible.
I have master
table with noop trigger and detail
table with two relations between master
and detail
table:
create table detail (
id bigint not null,
code varchar(255) not null,
primary key (id)
);
create table master (
id bigint not null,
name varchar(255),
detail_id bigint, -- "preferred" detail is one-to-one relation
primary key (id),
unique (detail_id),
foreign key (detail_id) references detail(id)
);
create table detail_candidate ( -- "candidate" details = many-to-many relation modeled as join table
master_id bigint not null,
detail_id bigint not null,
primary key (master_id, detail_id),
foreign key (detail_id) references detail(id),
foreign key (master_id) references master(id)
);
create or replace function trgf() returns trigger as $$
begin
return NEW;
end;
$$ language 'plpgsql';
create trigger trg
before insert or update
on master
for each row execute procedure trgf();
insert into master (id, name) values (1000, 'x'); -- this is part of database setup
insert into detail (code, id) values ('a', 1); -- this is part of database setup
In such setup, I open two terminal windows with psql
and perform following steps:
- in first terminal, change master (leave transaction open)
begin;
update master set detail_id=null, name='y' where id=1000;
- in second terminal, add detail candidate to master in own transaction
begin;
set statement_timeout = 4000;
insert into detail_candidate (master_id, detail_id) values (1000, 1);
Last command in second terminal timeouts with message
ERROR: canceling statement due to statement timeout
CONTEXT: while locking tuple (0,1) in relation "master"
SQL statement "SELECT 1 FROM ONLY "public"."master" x WHERE "id" OPERATOR(pg_catalog.=) $1 FOR KEY SHARE OF x"
My observation and questions (changes are independent):
- when the db is setup without trigger, i.e.
drop trigger trg on master;
is called after initial setup, everything works fine. Why the presence of noop trigger has such an influence? I don't get it. - when the db is setup without unique constraint on
master.detail_id
(i.e.alter table master drop constraint master_detail_id_key;
is called after initial setup), everything works fine too. Why? - when I omit explicit
detail=null
assignment in update statement in first terminal (since there's null value from setup anyway), everything works fine too. Why?
Tried on Postgres 9.6.12 (embedded), 9.6.15 (in Docker), 11.5 (in Docker).
Problem is reproducible in Docker image tomaszalusky/trig-example
which is available on DockerHub or can be built from this Dockerfile (instructions inside).
UPDATE 2: I found common behaviour of three observation above. I spawned the query select * from pgrowlocks('master')
from pgrowlocks extension in second transaction. The row-level lock of updated row in master
is FOR UPDATE
in failing case but FOR NO KEY UPDATE
in all three working cases. This is in perfect compliance with mode match table in documentation since FOR UPDATE
mode is the stronger one and mode requested by insert statement is FOR KEY SHARE
(which is apparent from error message, also invoking the select ... for key share
command has same effect as insert
command).
The documentation on FOR UPDATE
mode says:
The FOR UPDATE lock mode is also acquired by (...) an UPDATE that modifies the values on certain columns. Currently, the set of columns considered for the UPDATE case are those that have a unique index on them that can be used in a foreign key (...)
It is true for master.detail_id
column. However, still it's not clear why FOR UPDATE
mode isn't chosen independently on trigger presence and why trigger presence caused it.