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 1jJIiu-0005An-Kw for pgsql-bugs@arkaria.postgresql.org; Tue, 31 Mar 2020 15:22:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jJIit-0000nN-G3 for pgsql-bugs@arkaria.postgresql.org; Tue, 31 Mar 2020 15:22:39 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jJIis-0000nG-Us for pgsql-bugs@lists.postgresql.org; Tue, 31 Mar 2020 15:22:39 +0000 Received: from mail.dalibo.com ([212.85.157.172]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jJIin-0003an-WB for pgsql-bugs@lists.postgresql.org; Tue, 31 Mar 2020 15:22:36 +0000 Received: from firost (larco.ioguix.net [82.66.0.6]) by mail.dalibo.com (Postfix) with ESMTPSA id 82ADC20B18; Tue, 31 Mar 2020 17:22:31 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=dalibo.com; s=a; t=1585668151; bh=6FKCOML5Eb79RnMkAp2zG1Oo/v6H0SRASZwtWpymQ2E=; h=Date:From:To:Cc:Subject:From; b=IDIdzhOU0gUBLZWdPqvsvY5YSHA3Tno2wYtznlBJG1FisDQamDiGXFMjGwgYJQziJ S81Qy6ut8XKX8HQl7wnm7I0XOmIFboY2uIrRVKxklpMFod0DmbAoMtW1CJ2dMFO7oO 2BSbYjd/uJsdGTnZNHFwAUAzDUAjvdX9rqrKSjkA= Date: Tue, 31 Mar 2020 17:22:29 +0200 From: Jehan-Guillaume de Rorthais To: Cc: Michael Paquier Subject: [BUG] non archived WAL removed during production crash recovery Message-ID: <20200331172229.40ee00dc@firost> Organization: Dalibo MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="MP_/WC6WEc8gEbd3qV4j9CEjgao" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --MP_/WC6WEc8gEbd3qV4j9CEjgao Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline Hello, A colleague of mine reported an expected behavior. On production cluster is in crash recovery, eg. after killing a backend, the WALs ready to be archived are removed before being archived. See in attachment the reproduction script "non-arch-wal-on-recovery.bash". This behavior has been introduced in 78ea8b5daab9237fd42d7a8a836c1c451765499f. Function XLogArchiveCheckDone() badly consider the in crashed recovery production cluster as a standby without archive_mode=always. So the check conclude the WAL can be removed safely. bool inRecovery = RecoveryInProgress(); /* * The file is always deletable if archive_mode is "off". On standbys * archiving is disabled if archive_mode is "on", and enabled with * "always". On a primary, archiving is enabled if archive_mode is "on" * or "always". */ if (!((XLogArchivingActive() && !inRecovery) || (XLogArchivingAlways() && inRecovery))) return true; Please find in attachment a patch that fix this issue using the following test instead: if (!((XLogArchivingActive() && !StandbyModeRequested) || (XLogArchivingAlways() && inRecovery))) return true; I'm not sure if we should rely on StandbyModeRequested for the second part of the test as well thought. What was the point to rely on RecoveryInProgress() to get the recovery status from shared mem? Regards, --MP_/WC6WEc8gEbd3qV4j9CEjgao Content-Type: application/octet-stream; name=non-arch-wal-on-recovery.bash Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=non-arch-wal-on-recovery.bash IyEvYmluL2Jhc2gKCnNob3B0IC1zIC1vIGVycmV4aXQgbm91bnNldAoKIyBlbnYKUFJPSj1ub24t YXJjaC13YWwtb24tcmVjb3ZlcnkKUEdQUkVGSVg9L3RtcC8iJFBST0oiClBHQklOPSIkUEdQUkVG SVgiL2JpbgpQR0RBVEE9IiR7UEdQUkVGSVh9L2RhdGEiClBHTE9HPSIke1BHUFJFRklYfS9wb3N0 Z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/x-patch Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=0001-Fix-WAL-retention-during-production-crash-recovery.patch