Here is table ( simplified ):
Table "public.link" Column | Type | Modifiers ---------------+-----------------------------+--------------------------------------------------- id | integer | not null default nextval('link_id_seq'::regclass) page_id | integer | placed_at | timestamp without time zone | default now() Indexes: "link_pkey" PRIMARY KEY, btree (id) "link_page_id_index" btree (page_id) Foreign-key constraints: "link_page_id_foreign_key" FOREIGN KEY (page_id) REFERENCES page(id) ON UPDATE RESTRICT ON DELETE RESTRICT
And here is query ( simplified ):
UPDATE link SET page_id = ?, placed_at = now()
WHERE id IN ( SELECT id FROM link ... ) AND page_id IS NOT NULL
Deadlock message:
ERROR: deadlock detected Detail: Process 5822 waits for ShareLock on transaction 19705; blocked by process 5821. Process 5821 waits for ShareLock on transaction 19706; blocked by process 5822. Hint: See server log for query details.
How can that query, executed in parallel by several processes, lead to deadlock ?
Thanks!
SELECT
may be blocked by update depending on transaction isolation level. If your inner select is affected by either of the columns that you are updating then you have a problem. BTW: the dependency may be throughJOIN
, not just directWHERE
clause... BTW: Why are you updating yourpage_id
at all? It's a foreign key and as such the update may have deeper side-effects too. – Allocution