Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kpWF9-0006Pf-TN for pgsql-hackers@arkaria.postgresql.org; Wed, 16 Dec 2020 12:49:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kpWF6-0005tg-TS for pgsql-hackers@arkaria.postgresql.org; Wed, 16 Dec 2020 12:49:20 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kpWF6-0005tZ-Lo for pgsql-hackers@lists.postgresql.org; Wed, 16 Dec 2020 12:49:20 +0000 Received: from oss.nttdata.com ([49.212.34.109]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kpWEy-00045z-01 for pgsql-hackers@postgresql.org; Wed, 16 Dec 2020 12:49:18 +0000 Received: from hnk.local (p2430165-ipbf2730funabasi.chiba.ocn.ne.jp [122.24.66.165]) by oss.nttdata.com (Postfix) with ESMTPSA id 61EAE6230C for ; Wed, 16 Dec 2020 21:49:08 +0900 (JST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.102.3 at oss.nttdata.com To: PostgreSQL Hackers From: Fujii Masao Subject: Deadlock between backend and recovery may not be detected Message-ID: <4041d6b6-cf24-a120-36fa-1294220f8243@oss.nttdata.com> Date: Wed, 16 Dec 2020 21:49:07 +0900 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0) Gecko/20100101 Thunderbird/78.5.1 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------1FF2882BB97550101796CF13" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------1FF2882BB97550101796CF13 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Hi, While reviewing the patch proposed at [1], I found that there is the case where deadlock that recovery conflict on lock is involved in may not be detected. This deadlock can happen between backends and the startup process, in the standby server. Please see the following procedure to reproduce the deadlock. #1. Set up streaming replication. #2. Set max_standby_streaming_delay to -1 in the standby. #3. Create two tables in the primary. [PRIMARY: SESSION1] CREATE TABLE t1 (); CREATE TABLE t2 (); #4. Start transaction and access to the table t1. [STANDBY: SESSION2] BEGIN; SELECT * FROM t1; #5. Start transaction and lock table t2 in access exclusive mode, in the primary. Also execute pg_switch_wal() to transfer WAL record for access exclusive lock to the standby. [PRIMARY: SESSION1] BEGIN; LOCK TABLE t2 IN ACCESS EXCLUSIVE MODE; SELECT pg_switch_wal(); #6. Access to the table t2 within the transaction that started at #4, in the standby. [STANDBY: SESSION2] SELECT * FROM t2; #7. Lock table t1 in access exclusive mode within the transaction that started in #5, in the primary. Also execute pg_switch_wal() to transfer WAL record for access exclusive lock to the standby. [PRIMARY: SESSION1] LOCK TABLE t1 IN ACCESS EXCLUSIVE MODE; SELECT pg_switch_wal(); After doing this procedure, you can see the startup process and backend wait for the table lock each other, i.e., deadlock. But this deadlock remains even after deadlock_timeout passes. This seems a bug to me. > * Deadlocks involving the Startup process and an ordinary backend process > * will be detected by the deadlock detector within the ordinary backend. The cause of this issue seems that ResolveRecoveryConflictWithLock() that the startup process calls when recovery conflict on lock happens doesn't take care of deadlock case at all. You can see this fact by reading the above source code comment for ResolveRecoveryConflictWithLock(). To fix this issue, I think that we should enable STANDBY_DEADLOCK_TIMEOUT timer in ResolveRecoveryConflictWithLock() so that the startup process can send PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK signal to the backend. Then if PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK signal arrives, the backend should check whether the deadlock actually happens or not. Attached is the POC patch implimenting this. Thought? Regards, [1] https://postgr.es/m/9a60178c-a853-1440-2cdc-c3af916cff59@amazon.com -- Fujii Masao Advanced Computing Technology Center Research and Development Headquarters NTT DATA CORPORATION --------------1FF2882BB97550101796CF13 Content-Type: text/plain; charset=UTF-8; x-mac-type="0"; x-mac-creator="0"; name="recovery_conflict_lock_deadlock_v1.patch" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="recovery_conflict_lock_deadlock_v1.patch" ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL3N0b3JhZ2UvaXBjL3N0YW5kYnkuYyBiL3NyYy9i YWNrZW5kL3N0b3JhZ2UvaXBjL3N0YW5kYnkuYwppbmRleCA0ZWEzY2YxZjVjLi5jMzk4Zjlh M2E1IDEwMDY0NAotLS0gYS9zcmMvYmFja2VuZC9zdG9yYWdlL2lwYy9zdGFuZGJ5LmMKKysr IGIvc3JjL2JhY2tlbmQvc3RvcmFnZS9pcGMvc3RhbmRieS5jCkBAIC00MDcsNyArNDA3LDcg QEAgUmVzb2x2ZVJlY292ZXJ5Q29uZmxpY3RXaXRoTG9jayhMT0NLVEFHIGxvY2t0YWcpCiAK IAlsdGltZSA9IEdldFN0YW5kYnlMaW1pdFRpbWUoKTsKIAotCWlmIChHZXRDdXJyZW50VGlt ZXN0YW1wKCkgPj0gbHRpbWUpCisJaWYgKEdldEN1cnJlbnRUaW1lc3RhbXAoKSA+PSBsdGlt ZSAmJiBsdGltZSA+IDApCiAJewogCQkvKgogCQkgKiBXZSdyZSBhbHJlYWR5IGJlaGluZCwg c28gY2xlYXIgYSBwYXRoIGFzIHF1aWNrbHkgYXMgcG9zc2libGUuCkBAIC00MzEsMTIgKzQz MSwyMSBAQCBSZXNvbHZlUmVjb3ZlcnlDb25mbGljdFdpdGhMb2NrKExPQ0tUQUcgbG9ja3Rh ZykKIAkJLyoKIAkJICogV2FpdCAob3Igd2FpdCBhZ2FpbikgdW50aWwgbHRpbWUKIAkJICov Ci0JCUVuYWJsZVRpbWVvdXRQYXJhbXMgdGltZW91dHNbMV07CisJCUVuYWJ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