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 1guBPl-000222-Q8 for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Feb 2019 07:26:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1guBPk-0001ii-GP for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Feb 2019 07:26:32 +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 1guBPj-0001ib-UI for pgsql-hackers@lists.postgresql.org; Thu, 14 Feb 2019 07:26:32 +0000 Received: from mail-pg1-x534.google.com ([2607:f8b0:4864:20::534]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1guBPh-0006wR-0f for pgsql-hackers@postgresql.org; Thu, 14 Feb 2019 07:26:30 +0000 Received: by mail-pg1-x534.google.com with SMTP id h11so237975pgl.0 for ; Wed, 13 Feb 2019 23:26:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; h=date:from:to:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=gC0hFqxbgOhu8uLGDm+7UfVOkD2OXdYFWwOJBaUa4DI=; b=WQv0WKIOMPK6nL82Cm94XOHjA4PIlgGtMa6kvu5qvTbO23cHfT3T5jCg93VRYSDL+m +cpgWfu2UCZWMgM2DuYBcytBbtX13fr3iscqEMOuj0UffjRJlNc1qxfX3/0Y75hIzhud fECZxdkvzrrPlp0iuQAYqWyegpxNU5/v3KgHM= 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:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=gC0hFqxbgOhu8uLGDm+7UfVOkD2OXdYFWwOJBaUa4DI=; b=YE9Yyoa0vu+GsiG8jVvmH93yEIQphUwOVgo30ELtu0Vt3MFqL08B/it2FohaiC7ypz mphrnScAT4fhfAbnX+cEswY0/dQkwvCwX3SEh3UMRwpuwEFNRXrPSBHRFi/2ZJRMvq7n s2QrWczgSZEalHIbkp3JB6x1lz3qSyQS0mt9QnnESL1l6x8lOIcugcAzTBg727nSLqTH gwzCom8FykV+/T/ego6IbsUCHt6gVjryn2TvRCp9yyped4d6GN/iI4drKXUJuqnAC0q7 sBk+omMldeDcEwCxdW1nMyv8ZyBxtrMtIHbQ2w7ICn9Mmvg7MTahlouipiX8kD7kXEiE dUGw== X-Gm-Message-State: AHQUAua8OGPH9NK3iKFqGMq+DMJU8+3me5yN54fC2DgcoyAhYhMSr2gu tuppLQlhdamqi3yw/lFI90w67P1r95A= X-Google-Smtp-Source: AHgI3IZCYk7pVXU/nHnOiVhzNGc/LPmj3v1hsBB7rL++mIn01j/cKNEF/TVY1vT5XMGnPbJ/GFXodA== X-Received: by 2002:a63:d413:: with SMTP id a19mr2369210pgh.199.1550129187207; Wed, 13 Feb 2019 23:26:27 -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 v9sm2053389pfg.144.2019.02.13.23.26.25 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Feb 2019 23:26:25 -0800 (PST) Date: Wed, 13 Feb 2019 23:26:23 -0800 From: Noah Misch To: pgsql-hackers@postgresql.org Subject: Re: Spurious "apparent wraparound" via SimpleLruTruncate() rounding Message-ID: <20190214072623.GA1139206@rfd.leadboat.com> References: <20190202083822.GC32531@gust.leadboat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190202083822.GC32531@gust.leadboat.com> User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Sat, Feb 02, 2019 at 03:38:22AM -0500, Noah Misch wrote: > The main consequence is the false alarm. That conclusion was incorrect. On further study, I was able to reproduce data loss via either of two weaknesses in the "apparent wraparound" test: 1. The result of the test is valid only until we release the SLRU ControlLock, which we do before SlruScanDirCbDeleteCutoff() uses the cutoff to evaluate segments for deletion. Once we release that lock, latest_page_number can advance. This creates a TOCTOU race condition, allowing excess deletion: [local] test=# table trunc_clog_concurrency ; ERROR: could not access status of transaction 2149484247 DETAIL: Could not open file "pg_xact/0801": No such file or directory. 2. By the time the "apparent wraparound" test fires, we've already WAL-logged the truncation. clog_redo() suppresses the "apparent wraparound" test, then deletes too much. Startup then fails: 881997 2019-02-10 02:53:32.105 GMT FATAL: could not access status of transaction 708112327 881997 2019-02-10 02:53:32.105 GMT DETAIL: Could not open file "pg_xact/02A3": No such file or directory. 881855 2019-02-10 02:53:32.107 GMT LOG: startup process (PID 881997) exited with exit code 1 Fixes are available: 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. e. Have the SLRU track a "low cutoff" for an ongoing truncation. Initially, the low cutoff is the page furthest in the past relative to cutoffPage (the "high cutoff"). If SimpleLruZeroPage() wishes to use a page in the truncation range, it would acquire an LWLock and increment the low cutoff. Before unlinking any segment, SlruScanDirCbDeleteCutoff() would take the same LWLock and recheck the segment against the latest low cutoff. 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. Can you see a way to improve on that plan? Can you see other bugs of this nature that this plan does not fix? Thanks, nm