Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tVHZN-006zqn-37 for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Jan 2025 21:57:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tVHZM-008rJ4-HL for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Jan 2025 21:57:00 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tVHZM-008rIp-04 for pgsql-hackers@lists.postgresql.org; Tue, 07 Jan 2025 21:56:59 +0000 Received: from mail-pj1-x1034.google.com ([2607:f8b0:4864:20::1034]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tVHZJ-000P2B-0r for pgsql-hackers@lists.postgresql.org; Tue, 07 Jan 2025 21:56:58 +0000 Received: by mail-pj1-x1034.google.com with SMTP id 98e67ed59e1d1-2eeb4d643a5so23948277a91.3 for ; Tue, 07 Jan 2025 13:56:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; t=1736287016; x=1736891816; darn=lists.postgresql.org; h=user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=Nj5EQA65NfJl2lIW9lkON7r5iyGc8F5hU+50pLHBzZs=; b=KE9if/VR209jO9nvwpGpXEVKbZ0HiPpmbB43CS1htq83U9SucTya1Y3j8ltkY6mz3h YrTOskB8d0wHjclN8+7iPepxrkE03Yi7AGGGHnUiUvzfTogysQ6el4V5HgKz8LfGpHJK ENPhmOrGy9HCq4I11peJavUukTaDMqOnPMHEk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736287016; x=1736891816; h=user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Nj5EQA65NfJl2lIW9lkON7r5iyGc8F5hU+50pLHBzZs=; b=R42+EdV3JmMyHGGabPYtj1zX5+9Z+mzyLiB5urieA/QURnNDgI5r5i9R3t3munV8nx lsVcqVDLHmlUgboNJWKf09mv6upCtix0R7wAT/hp8q895aJD96vnsDRYd1W8Yojoy270 Y13JatZsJ+ZvgzyssVzlQpGnG7+DDtSdqlqVg/XbP8JRcxE/rcVobgLLaEDDl5glTYnU onqZp+9KJ9VxowqGPr+5Ru2ODQ5oHZbs55ANdqcbFKNqmRSGOHM4t2qPh9Asd4ry8pP4 5pRbgk9sx6gf7sa3VAyHSjIczSYXO2vJeEIdWwIUCB+aMbFSmAybgLpzHXMyvHleiq4/ YwQQ== X-Forwarded-Encrypted: i=1; AJvYcCWSixBc9hXpnqggvwRXnyBDrmyR+09B3TiBrwoxkC5L9Ec/E5P5cFU3mj0EO5QSNVocCcCN+8tdNzxKWXns@lists.postgresql.org X-Gm-Message-State: AOJu0YzaxNpA8qwTtiyDTBqOXs35TP+lRDVdp7qT0USHkOZMjeSpyzbe nlQ+LJWKImQakrmqvB4ffvW66uec3ydkhRopCzd8VRwH6ZKuu92zmp1cL9d+qg== X-Gm-Gg: ASbGncu70VkU2EpgsS1RwgTB8/SG6k8AedGxzNwMaRTsuIk+7XdHb+FjqypmdHT1XRE LmtvN+Sp+nyKF8giW1eJsSz0JgubE9eY8D97srbNP8dS6KRs9hua0y6q2v3hsg9gQmYuv5jc7V2 7hh36OoHoZBFhATXygKtAKTCMnTf0NKWBvXI+uq6oU7NvjBMAPvhohHAhj5kGC783zmVaaJ9cj3 3zEgXMinr515NIHUj4cFTbDQtjg4lD0BabgfLalQqRpyhXNeYg= X-Google-Smtp-Source: AGHT+IHW+bGV4A5hwDmoZ2mW1tGOl7B+ybq+VbDAwkCLdEeroBHnLoKDs7a8E03oWK1Q69tfG9ynng== X-Received: by 2002:a17:90b:2d43:b0:2ee:fdf3:390d with SMTP id 98e67ed59e1d1-2f5490ed82dmr580079a91.31.1736287016523; Tue, 07 Jan 2025 13:56:56 -0800 (PST) Received: from google.com ([2600:1702:a20:5750::48]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2f54a13e57asm17031a91.0.2025.01.07.13.56.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 07 Jan 2025 13:56:55 -0800 (PST) Date: Tue, 7 Jan 2025 13:56:53 -0800 From: Noah Misch To: Heikki Linnakangas Cc: Tom Lane , Xiaoran Wang , pgsql-hackers@lists.postgresql.org, Andres Freund Subject: Re: Recovering from detoast-related catcache invalidations Message-ID: <20250107215653.82.nmisch@google.com> References: <329632.1705011663@sss.pgh.pa.us> <754826.1705090452@sss.pgh.pa.us> <793469.1705092433@sss.pgh.pa.us> <20240114201411.d0@rfd.leadboat.com> <20240924212036.0a.nmisch@google.com> <1066416.1734103822@sss.pgh.pa.us> <2234dc98-06fe-42ed-b5db-ac17384dc880@iki.fi> <1f11aacf-968c-4b8d-a917-31cb207b7a56@iki.fi> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1f11aacf-968c-4b8d-a917-31cb207b7a56@iki.fi> User-Agent: Mutt/2.2.12 (2023-09-09) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, Dec 24, 2024 at 12:18:09AM +0200, Heikki Linnakangas wrote: > On 14/12/2024 02:06, Heikki Linnakangas wrote: > > Ok, I missed that. It does not handle the 2nd scenario though: If a new > > catalog tuple is concurrently inserted that should be part of the list, > > it is missed. > > Attached is an injection point test case to reproduce that. If you > > change the test so that the function's body is shorter, so that it's not > > toasted, the test passes. Nice discovery. > I'm thinking of the attached to fix this. It changes the strategy for > detecting concurrent cache invalidations. Instead of the "recheck" mechanism > that was introduced in commit ad98fb1422, keep a stack of "build > in-progress" entries, and CatCacheInvalidate() invalidate those > "in-progress" entries in addition to the actual CatCTup and CatCList > entries. > > My first attempt was to insert the CatCTup or CatCList entry to the catcache > before starting to build it, marked with a flag to indicate that the entry > isn't fully built yet. But when I started to write that it got pretty > invasive, and it felt easier to add another structure to hold the > in-progress entries instead. That's similar to how relcache has been doing it (in_progress_list). I see no problem applying that technique here. > not sure I want to > commit the test with the injection point, but it's useful now to demonstrate > the bug.) I'd err on the side of including it. Apart from some copied comments, the test looks ready. On Wed, Dec 25, 2024 at 11:27:38AM +0200, Heikki Linnakangas wrote: > +++ b/src/test/modules/test_misc/t/007_bugs.pl test_misc/t/007_bugs.pl could be home to almost anything. How about naming it 007_catcache_inval.pl? > @@ -744,6 +770,13 @@ ResetCatalogCache(CatCache *cache) > #endif > } > } > + > + /* Also invalidate any entries that are being built */ > + for (CatCCreating *e = catcache_creating_stack; e != NULL; e = e->next) > + { > + if (e->cache == cache) > + e->dead = true; > + } > } With debug_discard_caches=1, "make check" hangs early with some INSERT using 100% CPU. The new test file does likewise. I bet this needs a special case to short-circuit debug_discard_caches, like RelationCacheInvalidate() has. > @@ -1665,6 +1698,8 @@ SearchCatCacheList(CatCache *cache, > HeapTuple ntp; > MemoryContext oldcxt; > int i; > + volatile CatCCreating creating_list; You could drop the volatile by copying catcache_creating_stack to an additional var that you reference after longjmp, instead of referencing creating_list.next after longjmp. Likewise for the instance of volatile in CatalogCacheCreateEntry(). https://pubs.opengroup.org/onlinepubs/9799919799/functions/longjmp.html has the rules. Using volatile is fine, though. > + bool first_iter = true; This is okay as non-volatile, but it could just as easily move inside the PG_TRY. > @@ -2076,38 +2118,33 @@ CatalogCacheCreateEntry(CatCache *cache, HeapTuple ntp, SysScanDesc scandesc, > + PG_TRY(); > { > - matches = equalTuple(before, ntp); > - heap_freetuple(before); > + dtp = toast_flatten_tuple(ntp, cache->cc_tupdesc); gcc 4.8.5 warns: catcache.c: In function ‘CatalogCacheCreateEntry’: catcache.c:2159:29: warning: ‘dtp’ may be used uninitialized in this function [-Wmaybe-uninitialized] ct->tuple.t_tableOid = dtp->t_tableOid; I remember some commit of a gcc 4.x warning fix in a recent year, and we do have buildfarm representation. Consider silencing it. The rest looks good. Thanks.