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 1nHmsj-0005DZ-PJ for pgsql-hackers@arkaria.postgresql.org; Wed, 09 Feb 2022 13:19:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nHmsf-0003nB-Nn for pgsql-hackers@arkaria.postgresql.org; Wed, 09 Feb 2022 13:19:33 +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 1nHfjm-0000lX-TS for pgsql-hackers@lists.postgresql.org; Wed, 09 Feb 2022 05:41:54 +0000 Received: from mail-ot1-x330.google.com ([2607:f8b0:4864:20::330]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nHfjf-0000XH-BQ for pgsql-hackers@postgresql.org; Wed, 09 Feb 2022 05:41:54 +0000 Received: by mail-ot1-x330.google.com with SMTP id g19-20020a9d6213000000b005ab7489371aso832977otj.6 for ; Tue, 08 Feb 2022 21:41:46 -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=JlZc9U0N8zXjbq8DdcSLDoLJQ8/FEH15iWTJVHzeNAw=; b=VXFMAMGR2JvJni8IoVJaBwv2fR9ho2oeN5BwblJkyrIDF5bZ9WotEp5b7fx4In0uJK D9hFprvXZgsU8dIlKlE4Hi+98cwYT1vdF2o7Q/jE1BEwGMMKoPg4lYQqtE+yUaGU6rWD J8DSAbTRLlGRDdOO7Q0mcOVaUaTn7P03EfY2E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=JlZc9U0N8zXjbq8DdcSLDoLJQ8/FEH15iWTJVHzeNAw=; b=moNccfvTzBCyBkxhfY/q9Xv/qZUe609vPc6g66gN3KzdbH4zFQb9vMm7/CdZZRKubw wrfyYqMYsIucLLqvhMOs8vGtpc5fG/zQzCc9Vu6JNA9mcqLqr66Pbc+z7k5fQF8sA7Il /PaJwGt3bxnc5EG+/smycbVfMGMgB9Bpwzo1ujwYUWHv4RRFF6wjCylsuHdYrEGJz1Jk O3eSbkRmGH8iqU527dccn2u66owgVULHzDAJwLn9uoP2+ZGkbFJeCCcFb3IDoISs5mNj +7pb25YaI5qkblDMmiy4m5ZnM81RKXRx6OeaEQIb+jw0OKmt8g3Iv6WKyW7/SpPYFi8B vWbg== X-Gm-Message-State: AOAM533YPoG2g78vd/EH1aBVo/4Y6QxusQlQhFWh34ir/l2B5497UpeB c9JfToHItbSrtAOYC8oJf0wqEe+LgdVZkA== X-Google-Smtp-Source: ABdhPJx6u57LPEmPdihf43g5a4xLh7HhdwHttbZg5qZztuVQrClbSgwAf761QGrORsXlpY4coGBZSw== X-Received: by 2002:a9d:4806:: with SMTP id c6mr292228otf.317.1644385305174; Tue, 08 Feb 2022 21:41:45 -0800 (PST) Received: from rfd.leadboat.com ([2600:1702:a20:5750::2e]) by smtp.gmail.com with ESMTPSA id bg34sm6542755oob.14.2022.02.08.21.41.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Feb 2022 21:41:44 -0800 (PST) Date: Tue, 8 Feb 2022 21:41:41 -0800 From: Noah Misch To: Tomas Vondra Cc: Andres Freund , pgsql-hackers@postgresql.org, pgsql-committers@lists.postgresql.org Subject: Re: pgsql: Avoid race in RelationBuildDesc() affecting CREATE INDEX CONCURR Message-ID: <20220209054141.GA3058164@rfd.leadboat.com> References: <20220209004347.mnwaoiv5x7dntuvi@alap3.anarazel.de> <20220209020403.GA3002372@rfd.leadboat.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="mYCpIKhGyMATD0i+" Content-Disposition: inline In-Reply-To: <20220209020403.GA3002372@rfd.leadboat.com> User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --mYCpIKhGyMATD0i+ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, Feb 08, 2022 at 06:04:03PM -0800, Noah Misch wrote: > On Tue, Feb 08, 2022 at 04:43:47PM -0800, Andres Freund wrote: > > On 2022-02-08 22:13:01 +0100, Tomas Vondra wrote: > > > On 10/24/21 03:40, Noah Misch wrote: > > > > Avoid race in RelationBuildDesc() affecting CREATE INDEX CONCURRENTLY. > > > > > > > > CIC and REINDEX CONCURRENTLY assume backends see their catalog changes > > > > no later than each backend's next transaction start. That failed to > > > > hold when a backend absorbed a relevant invalidation in the middle of > > > > running RelationBuildDesc() on the CIC index. Queries that use the > > > > resulting index can silently fail to find rows. Fix this for future > > > > index builds by making RelationBuildDesc() loop until it finishes > > > > without accepting a relevant invalidation. It may be necessary to > > > > reindex to recover from past occurrences; REINDEX CONCURRENTLY suffices. > > > > Back-patch to 9.6 (all supported versions). > > > > > > > > Noah Misch and Andrey Borodin, reviewed (in earlier versions) by Andres > > > > Freund. > > > > > > > > Discussion: https://postgr.es/m/20210730022548.GA1940096@gust.leadboat.com > > > > > > > > > > Unfortunately, this seems to have broken CLOBBER_CACHE_ALWAYS builds. Since > > > this commit, initdb never completes due to infinite retrying over and over > > > (on the first RelationBuildDesc call). > > Thanks for the report. I had added the debug_discard arguments of > InvalidateSystemCachesExtended() and RelationCacheInvalidate() to make the new > code survive a CREATE TABLE at debug_discard_caches=5. Apparently that's not > enough for initdb. I'll queue a task to look at it. The explanation was more boring than that. v13 and earlier have an additional InvalidateSystemCaches() call site, which I neglected to update. Here's the fix I intend to push. --mYCpIKhGyMATD0i+ Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="fix-inval-build-race-v1.patch" Author: Noah Misch Commit: Noah Misch Fix back-patch of "Avoid race in RelationBuildDesc() ..." The back-patch of commit fdd965d074d46765c295223b119ca437dbcac973 broke CLOBBER_CACHE_ALWAYS for v9.6 through v13, because it updated the InvalidateSystemCaches() call pertaining to CLOBBER_CACHE_RECURSIVELY and not also the one pertaining to CLOBBER_CACHE_ALWAYS. Back-patch to v13, v12, v11, and v10. Reported by Tomas Vondra. Reviewed by FIXME. Discussion: https://postgr.es/m/df7b4c0b-7d92-f03f-75c4-9e08b269a716@enterprisedb.com diff --git a/src/backend/utils/cache/inval.c b/src/backend/utils/cache/inval.c index 8b0503f..8fe1f0d 100644 --- a/src/backend/utils/cache/inval.c +++ b/src/backend/utils/cache/inval.c @@ -715,27 +715,27 @@ AcceptInvalidationMessages(void) * slows things by at least a factor of 10000, so I wouldn't suggest * trying to run the entire regression tests that way. It's useful to try * a few simple tests, to make sure that cache reload isn't subject to * internal cache-flush hazards, but after you've done a few thousand * recursive reloads it's unlikely you'll learn more. */ #if defined(CLOBBER_CACHE_ALWAYS) { static bool in_recursion = false; if (!in_recursion) { in_recursion = true; - InvalidateSystemCaches(); + InvalidateSystemCachesExtended(true); in_recursion = false; } } #elif defined(CLOBBER_CACHE_RECURSIVELY) { static int recursion_depth = 0; /* Maximum depth is arbitrary depending on your threshold of pain */ if (recursion_depth < 3) { recursion_depth++; InvalidateSystemCachesExtended(true); recursion_depth--; --mYCpIKhGyMATD0i+--