Re: race condition when writing pg_control
Nathan Bossart <bossartn@amazon.com>
From: "Bossart, Nathan" <bossartn@amazon.com>
To: Fujii Masao <masao.fujii@oss.nttdata.com>, Michael Paquier <michael@paquier.xyz>
Cc: Thomas Munro <thomas.munro@gmail.com>, "pgsql-hackers@postgresql.org" <pgsql-hackers@postgresql.org>
Date: 2020-05-31T21:11:35Z
Lists: pgsql-hackers
Attachments
- 0003-Assert-that-ControlFileLock-is-held-within-UpdateCon.patch (application/octet-stream) patch 0003
- 0002-Acquire-ControlFileLock-within-XLogReportParameters.patch (application/octet-stream) patch 0002
- 0001-Fix-race-condition-that-could-corrupt-pg_control.patch (application/octet-stream) patch 0001
On 5/29/20, 12:24 AM, "Fujii Masao" <masao.fujii@oss.nttdata.com> wrote: > On 2020/05/27 16:10, Michael Paquier wrote: >> On Tue, May 26, 2020 at 07:30:54PM +0000, Bossart, Nathan wrote: >>> While an assertion in UpdateControlFile() would not have helped us >>> catch the problem I initially reported, it does seem worthwhile to add >>> it. I have attached a patch that adds this assertion and also >>> attempts to fix XLogReportParameters(). Since there is only one place >>> where we feel it is safe to call UpdateControlFile() without a lock, I >>> just changed it to take the lock. I don't think this adds any sort of >>> significant contention risk, and IMO it is a bit cleaner than the >>> boolean flag. >> >> Let's see what Fujii-san and Thomas think about that. I'd rather >> avoid taking a lock here because we don't need it and because it makes >> things IMO confusing with the beginning of StartupXLOG() where a lot >> of the fields are read, even if we go without this extra assertion. > > I have no strong opinion about this, but I tend to agree with Michael here. > >>> For the XLogReportParameters() fix, I simply added an exclusive lock >>> acquisition for the portion that updates the values in shared memory >>> and calls UpdateControlFile(). IIUC the first part of this function >>> that accesses several ControlFile values should be safe, as none of >>> them can be updated after server start. >> >> They can get updated when replaying a XLOG_PARAMETER_CHANGE record. >> But you are right as all of this happens in the startup process, so >> your patch looks right to me here. > > LGTM. Thanks for the feedback. I've attached a new set of patches. Nathan
Commits
-
Fix locking bugs that could corrupt pg_control.
- 09dc17486393 9.5.23 landed
- 644cac32a0bc 9.6.19 landed
- fd11b842eba9 10.14 landed
- 48eb6a3c895d 11.9 landed
- 72766ad6397a 12.4 landed
- acefa2cca6a2 13.0 landed
- 57cb8063089a 14.0 landed