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 1inuZm-00059P-D3 for pgsql-hackers@arkaria.postgresql.org; Sun, 05 Jan 2020 01:19:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1inuZl-000830-1t for pgsql-hackers@arkaria.postgresql.org; Sun, 05 Jan 2020 01:19:29 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1inuZk-00082s-HD for pgsql-hackers@lists.postgresql.org; Sun, 05 Jan 2020 01:19:28 +0000 Received: from mail-pl1-x641.google.com ([2607:f8b0:4864:20::641]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1inuZd-0002uu-TT for pgsql-hackers@postgresql.org; Sun, 05 Jan 2020 01:19:28 +0000 Received: by mail-pl1-x641.google.com with SMTP id c13so20443829pls.0 for ; Sat, 04 Jan 2020 17:19:21 -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=/QoXfGgAYv1tpjYgsVEwF8UPIVB/lbl0CIih5/+45TM=; b=PlGjk2Tz7337zTLA+1AZEE4BWIx3UthBxMQQEkw/r6OqkqXZi8zaYFwDTYs+BOu6at 6O5p2SDctCiwOr+DfPgopcD9VIz4stYGPwj2SPD4ML5GLlvpQjL/DdfVb06VqWndL24X eJBjtyWozgZlAuHsthsGf0MWAGET+VGZpkdWI= 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=/QoXfGgAYv1tpjYgsVEwF8UPIVB/lbl0CIih5/+45TM=; b=iWf5tS6TIu44/6kB7w/ZzAJEVZEgUcOKc5cYY69XC9nnqqsgmu70j4cfGM7j8yBR4U UI049htibKDKuU4tc9hlwYUztRBVSPe5AfrTg3fUoNBp8VCSlNVulmNZMMUaN1pmTbwJ Dec5+lawfEABOOm7GmeyOOTnT2NkMKMxip/m9WtZTSU+75euoZAy5ojwzvrRTaNMrbCb DscbL0UE2wrWy2wi1GFf7Ot6m+WDhPpvs5QwVxOCMiGn1I7418VJQt/fyZItSiIOJJhz WLCKNRXMgG4BZO8S9mSlxzUFU4VuDga5iqGCK5z35LXGpzUlBFbuTaslwlCtuytwf6Ec 7aKQ== X-Gm-Message-State: APjAAAWReTUOFQcWT9rMqod5niEUYZbUIJnmIumQ5zjLyzhM9rg/KTiM FHcYvKiIsBq/jiB4N6ft7qkgRQ== X-Google-Smtp-Source: APXvYqzyvazhJ9W+m2YrT/eNsppa7cwud5LRd0dOXojxipCqYNO0Gnl9xy8WGlfD3ekkOh4evKrsYg== X-Received: by 2002:a17:902:8211:: with SMTP id x17mr78134908pln.252.1578187158843; Sat, 04 Jan 2020 17:19:18 -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 d3sm31434806pfn.113.2020.01.04.17.19.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 04 Jan 2020 17:19:18 -0800 (PST) Date: Sat, 4 Jan 2020 17:19:16 -0800 From: Noah Misch To: Tomas Vondra Cc: pgsql-hackers@postgresql.org Subject: Re: Spurious "apparent wraparound" via SimpleLruTruncate() rounding Message-ID: <20200105011916.GC2270238@rfd.leadboat.com> References: <20190202083822.GC32531@gust.leadboat.com> <20190214072623.GA1139206@rfd.leadboat.com> <20200105003355.n2atv7thx7dqiqbt@development> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200105003355.n2atv7thx7dqiqbt@development> 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, Jan 05, 2020 at 01:33:55AM +0100, Tomas Vondra wrote: > On Wed, Feb 13, 2019 at 11:26:23PM -0800, Noah Misch wrote: > >a. Fix the rounding in SimpleLruTruncate(). (The patch I posted upthread is > > wrong; I will correct it in a separate message.) > > > >b. Arrange so only one backend runs vac_truncate_clog() at a time. Other than > > AsyncCtl, every SLRU truncation appears in vac_truncate_clog(), in a > > checkpoint, or in the startup process. Hence, also arrange for only one > > backend to call SimpleLruTruncate(AsyncCtl) at a time. > > > >c. Test "apparent wraparound" before writing WAL, and don't WAL-log > > truncations that "apparent wraparound" forces us to skip. > > > >d. Hold the ControlLock for the entirety of SimpleLruTruncate(). This removes > > the TOCTOU race condition, but TransactionIdDidCommit() and other key > > operations would be waiting behind filesystem I/O. > >With both (a) and (b), the only way I'd know to reach the "apparent > >wraparound" message is to restart in single-user mode and burn XIDs to the > >point of bona fide wraparound. Hence, I propose to back-patch (a) and (b), > >and I propose (c) for HEAD only. I don't want (d), which threatens > >performance too much. I would rather not have (e), because I expect it's more > >complex than (b) and fixes strictly less than (b) fixes. > Seems reasonable, although I wonder how much more expensive would just > doing (d) be. It seems by far the least complex solution, and it moves > "just" the SlruScanDirectory() call before the lock. It's true it adds > I/O requests, OTOH it's just unlink() without fsync() and I'd expect the > number of files to be relatively low. Plus we already do SimpleLruWaitIO > and SlruInternalWritePage in the loop. Trivial read-only transactions often need CLogControlLock to check tuple visibility. If an unlink() takes 1s, stalling read-only transactions for that 1s is a big problem. SimpleLruWaitIO() and SlruInternalWritePage() release the control lock during I/O, then re-acquire it. (Moreover, SimpleLruTruncate() rarely calls them. Calling them implies a page old enough to truncate was modified after the most recent checkpoint.) > BTW isn't that an issue that SlruInternalDeleteSegment does not do any > fsync calls after unlinking the segments? If the system crashes/reboots > before this becomes persistent (i.e. some of the segments reappear, > won't that cause a problem)? I think not; we could turn SlruInternalDeleteSegment() into a no-op, and the only SQL-visible consequence would be extra disk usage. CheckPoint fields tell the server what region of slru data is meaningful, and segments outside that range merely waste disk space. (If that's not true and can't be made true, we'd also need to stop ignoring the unlink() return value.) > It's a bit unfortunate that a patch for a data corruption / loss issue > (even if a low-probability one) fell through multiple commitfests. Thanks for investing in steps to fix that.