Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iWaJ1-00056u-41 for pgsql-hackers@arkaria.postgresql.org; Mon, 18 Nov 2019 06:14:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iWaIz-0003ge-Q5 for pgsql-hackers@arkaria.postgresql.org; Mon, 18 Nov 2019 06:14:33 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iWaIz-0003gV-Dv for pgsql-hackers@lists.postgresql.org; Mon, 18 Nov 2019 06:14:33 +0000 Received: from mail-pl1-x644.google.com ([2607:f8b0:4864:20::644]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iWaIw-0005ql-Gb for pgsql-hackers@postgresql.org; Mon, 18 Nov 2019 06:14:31 +0000 Received: by mail-pl1-x644.google.com with SMTP id q18so5458539pls.5 for ; Sun, 17 Nov 2019 22:14:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=EbEU6xFRzNA8573NRfUGnP/3a/GZH3d6m9gFZdPmIKM=; b=EF9RSX1UoJovHrs5jvNh+IxnolZZDq+heicr7lMowIMeo7FTo6anmHyUDSGlDthsAu g3ZWyTv4Ul21ko1EIN1VEuAbnmvCvtd42MPBH882rCjsQF5CO1zxZ27BcAZWwuR45zvM 71OZAWggSDPeKPlA1srOrfq94ZF7lbVVGGmSw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=EbEU6xFRzNA8573NRfUGnP/3a/GZH3d6m9gFZdPmIKM=; b=FrayZKsTt378MYyZ8vXWay/FuKdHz0P/Mu8C5FU50ttIwg+ixKpZwLEKcToZxu3KPO CUmD4Z2K4asPPP4JC+ahTUokJ8JDUfeAEgI9bhncL+A2WSn3nwAFM/6ob1NxDwpB25vi DkyVUMGE9Winj5hbtotgi8TlwbDNQw6/1pUQS0cmaeHzbnJPmsd6BjKFWo7kSfNtr30f QSSUkN6u9NrxqbnBlhLskQCiA9pIWE8IUxTZj3PoQVYE0n3xR/mP1+U7BvrtGES0Zt8v BIKmYXiNw8cNisHh5B8IUeZUy4lSlm3D3ajF+LZYbpBolQ/taM+v7Ng9NxF5HHK0LX0s IDQA== X-Gm-Message-State: APjAAAWrdw1mEuRtiTDWjTtrKEoei5YhZPFuIYs3uKsaJPisvhEid3Y0 vZjUMUQlyNTtvwLnQY158TSK0A== X-Google-Smtp-Source: APXvYqwh/Zf4RpKT5ValcYCr2NYhTHH9ow8u0iMU92QFzu9a6yH2h5WSdpNdw+KCpKoQhvXo306DIw== X-Received: by 2002:a17:902:76c8:: with SMTP id j8mr21729476plt.122.1574057669202; Sun, 17 Nov 2019 22:14:29 -0800 (PST) Received: from rfd.leadboat.com (108-233-125-46.lightspeed.sntcca.sbcglobal.net. [108.233.125.46]) by smtp.gmail.com with ESMTPSA id m68sm7256213pfm.85.2019.11.17.22.14.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 17 Nov 2019 22:14:28 -0800 (PST) Date: Sun, 17 Nov 2019 22:14:26 -0800 From: Noah Misch To: Dmitry Dolgov <9erthalion6@gmail.com> Cc: Tom Lane , pgsql-hackers@postgresql.org Subject: Re: SimpleLruTruncate() mutual exclusion Message-ID: <20191118061426.GA1173376@rfd.leadboat.com> References: <20190218073103.GA1434723@rfd.leadboat.com> <20190628170628.GA1238361@rfd.leadboat.com> <29566.1564419497@sss.pgh.pa.us> <20190801065117.GA2355684@rfd.leadboat.com> <20191117115652.doqc77yu7kbrqwmk@localhost> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20191117115652.doqc77yu7kbrqwmk@localhost> User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Sun, Nov 17, 2019 at 12:56:52PM +0100, Dmitry Dolgov wrote: > > On Wed, Jul 31, 2019 at 11:51:17PM -0700, Noah Misch wrote: > > > Also, if I understand the data-loss hazard properly, it's what you > > > said in the other thread: the latest_page_number could advance after > > > we make our decision about what to truncate, and then maybe we could > > > truncate newly-added data. We surely don't want to lock out the > > > operations that can advance last_page_number, so how does serializing > > > vac_truncate_clog help? > > > > > > I wonder whether what we need to do is add some state in shared > > > memory saying "it is only safe to create pages before here", and > > > make SimpleLruZeroPage or its callers check that. The truncation > > > logic would advance that value, but only after completing a scan. > > > (Oh ..., hmm, maybe the point of serializing truncations is to > > > ensure that we know that we can safely advance that?) > > > > vac_truncate_clog() already records "it is only safe to create pages before > > here" in ShmemVariableCache->xidWrapLimit, which it updates after any unlinks. > > The trouble comes when two vac_truncate_clog() run in parallel and you get a > > sequence of events like this: > > > > vac_truncate_clog() instance 1 starts, considers segment ABCD eligible to unlink > > vac_truncate_clog() instance 2 starts, considers segment ABCD eligible to unlink > > vac_truncate_clog() instance 1 unlinks segment ABCD > > vac_truncate_clog() instance 1 calls SetTransactionIdLimit() > > vac_truncate_clog() instance 1 finishes > > some backend calls SimpleLruZeroPage(), creating segment ABCD > > vac_truncate_clog() instance 2 unlinks segment ABCD > > > > Serializing vac_truncate_clog() fixes that. > > I'm probably missing something, so just wanted to clarify. Do I > understand correctly, that thread [1] and this one are independent, and > it is assumed in the scenario above that we're at the end of XID space, > but not necessarily having rounding issues? I'm a bit confused, since > the reproduce script does something about cutoffPage, and I'm not sure > if it's important or not. I think the repro recipe contained an early fix for the other thread's bug. While they're independent in principle, I likely never reproduced this bug without having a fix in place for the other thread's bug. The bug in the other thread was easier to hit. > > > Can you post whatever script you used to detect/reproduce the problem > > > in the first place? > > > > I've attached it, but it's pretty hackish. Apply the patch on commit 7170268, > > make sure your --bindir is in $PATH, copy "conf-test-pg" to your home > > directory, and run "make trunc-check". This incorporates xid-burn > > acceleration code that Jeff Janes shared with -hackers at some point. > > I've tried to use it to understand the problem better, but somehow > couldn't reproduce via suggested script. I've applied it to 7170268 > (tried also apply rebased to the master with the same results) with the > conf-test-pg in place, but after going through all steps there are no > errors like: That is unfortunate. > Is there anything extra one needs to do to reproduce the problem, maybe > adjust delays or something? It wouldn't surprise me. I did all my testing on one or two systems; the hard-coded delays suffice there, but I'm sure there exist systems needing different delays. Though I did reproduce this bug, I'm motivated by the abstract problem more than any particular way to reproduce it. Commit 996d273 inspired me; by removing a GetCurrentTransactionId(), it allowed the global xmin to advance at times it previously could not. That subtly changed the concurrency possibilities. I think safe, parallel SimpleLruTruncate() is difficult to maintain and helps too rarely to justify such maintenance. That's why I propose eliminating the concurrency. > [1]: https://www.postgresql.org/message-id/flat/20190202083822.GC32531%40gust.leadboat.com