postgresql/src/backend/access
Alvaro Herrera 8b21b416ed Avoid spurious deadlocks when upgrading a tuple lock
This puts back reverted commit de87a084c0, with some bug fixes.

When two (or more) transactions are waiting for transaction T1 to release a
tuple-level lock, and transaction T1 upgrades its lock to a higher level, a
spurious deadlock can be reported among the waiting transactions when T1
finishes.  The simplest example case seems to be:

T1: select id from job where name = 'a' for key share;
Y: select id from job where name = 'a' for update; -- starts waiting for T1
Z: select id from job where name = 'a' for key share;
T1: update job set name = 'b' where id = 1;
Z: update job set name = 'c' where id = 1; -- starts waiting for T1
T1: rollback;

At this point, transaction Y is rolled back on account of a deadlock: Y
holds the heavyweight tuple lock and is waiting for the Xmax to be released,
while Z holds part of the multixact and tries to acquire the heavyweight
lock (per protocol) and goes to sleep; once T1 releases its part of the
multixact, Z is awakened only to be put back to sleep on the heavyweight
lock that Y is holding while sleeping.  Kaboom.

This can be avoided by having Z skip the heavyweight lock acquisition.  As
far as I can see, the biggest downside is that if there are multiple Z
transactions, the order in which they resume after T1 finishes is not
guaranteed.

Backpatch to 9.6.  The patch applies cleanly on 9.5, but the new tests don't
work there (because isolationtester is not smart enough), so I'm not going
to risk it.

Author: Oleksii Kliukin
Discussion: https://postgr.es/m/B9C9D7CD-EB94-4635-91B6-E558ACEC0EC3@hintbits.com
Discussion: https://postgr.es/m/2815.1560521451@sss.pgh.pa.us
2019-06-18 18:23:16 -04:00
..
brin Update stale comments, and fix comment typos. 2019-06-08 10:12:26 -07:00
common Fix more typos and inconsistencies in the tree 2019-06-17 16:13:16 +09:00
gin Phase 2 pgindent run for v12. 2019-05-22 13:04:48 -04:00
gist Fix typos in various places 2019-06-03 13:44:03 +09:00
hash Fix more typos and inconsistencies in the tree 2019-06-17 16:13:16 +09:00
heap Avoid spurious deadlocks when upgrading a tuple lock 2019-06-18 18:23:16 -04:00
index Phase 2 pgindent run for v12. 2019-05-22 13:04:48 -04:00
nbtree Fix typos and inconsistencies in code comments 2019-06-14 09:34:34 +09:00
rmgrdesc Convert gist to compute page level xid horizon on primary. 2019-04-22 14:28:30 -07:00
spgist Initial pgindent run for v12. 2019-05-22 12:55:34 -04:00
table Don't access catalogs to validate GUCs when not connected to a DB. 2019-06-10 23:34:50 -07:00
tablesample Phase 2 pgindent run for v12. 2019-05-22 13:04:48 -04:00
transam Fix more typos and inconsistencies in the tree 2019-06-17 16:13:16 +09:00
Makefile Introduce access/{table.h, relation.h}, for generic functions from heapam.h. 2019-01-21 10:51:36 -08:00