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 1qMvMH-003c2V-Kr for pgsql-bugs@arkaria.postgresql.org; Fri, 21 Jul 2023 19:00:09 +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 1qMvMG-006ZgR-4K for pgsql-bugs@arkaria.postgresql.org; Fri, 21 Jul 2023 19:00:08 +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 1qMvMF-006ZgI-Sa for pgsql-bugs@lists.postgresql.org; Fri, 21 Jul 2023 19:00:07 +0000 Received: from mail-lf1-x130.google.com ([2a00:1450:4864:20::130]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qMvMD-000jQc-DH for pgsql-bugs@lists.postgresql.org; Fri, 21 Jul 2023 19:00:06 +0000 Received: by mail-lf1-x130.google.com with SMTP id 2adb3069b0e04-4fb7589b187so3612621e87.1 for ; Fri, 21 Jul 2023 12:00:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689966004; x=1690570804; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=nmDdqiwZFiLKhOxDCkngaiPBRRsXhMpro/W4pWWmElU=; b=Ep9mLa3prRoRqwwoI5Yd7clSj4+5qt3GyNLHn/12g0aad5U//jnDe+zbqzlTnrXLFT 5a3K9xJvkQSCdHKGRB+YPZdr6vyPxn4OxVBMrTpGqBrUTaevTKBsf/HgR3LbgnqodBn0 h5ga3tv3NSjLGTQ3/JYh5tmcEfpTRforgxOaA/3kyeHKdcFzZNsjFpb7Nfhi0j8jBigU wGKMReMXpSfh2x4+gHhdTgFABNANv+2y+wpvBLbJkQqVZsVf809xx9brIXfrqBTwek2o J6ttve/yB0JX45GSy0BsmrVmY646LJgBOFc8Hae058U1+wl709KrDBVsy4TRJoiD2adG PHzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689966004; x=1690570804; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=nmDdqiwZFiLKhOxDCkngaiPBRRsXhMpro/W4pWWmElU=; b=S1z+xcgkvxzwIYu99cOpYRf6xRkTq5GOybqLRkYp2MpmBXX6myGoOdNrHeJz/PgZL7 0AsNpyt5fzLCjNtgSfcSw4O6iprrMM152YNYLSY2OcJdJEUBgolu1wMsGBDn5b/8reGX 8QjjF29WexhmrnBvbDf/h/WM6rRnPwWB9fp4lbjdL6qD4kAak7XuwkNRyM6CJ0j1QcPo gI/YtaB208elVfxVnx0GAnEIDWgKizU1aYDYI37C+qNlVK6K4leBou1Yl154uCgaPC3s PUFihzTWVwsAKTR2Rkict83NE1Uy1/YYq6GmKSRci0YdRed4Gmvc7q0TuqdGeX0Sp/1Y 9sjA== X-Gm-Message-State: ABy/qLYxLp9/Kd3rXSxt+vkHgZVhtR8yn/9GHdRZ1uzMKnFR/gvG0RAE dYj6cPxF4ngrz8RYwa7XJnppl3Q+e5A= X-Google-Smtp-Source: APBJJlH+7GdPtSBcRH73MeNBaLNuRPRlcKVmIfEC+oKHtbv+0P4SjlDWK6gaVftHzhQPKHk0tgq95g== X-Received: by 2002:a05:6512:3142:b0:4fd:d1c0:6439 with SMTP id s2-20020a056512314200b004fdd1c06439mr1664074lfi.41.1689966003440; Fri, 21 Jul 2023 12:00:03 -0700 (PDT) Received: from [1.0.0.7] ([178.155.25.194]) by smtp.gmail.com with ESMTPSA id w23-20020a19c517000000b004fba82dde47sm860095lfe.123.2023.07.21.12.00.02 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 21 Jul 2023 12:00:02 -0700 (PDT) Message-ID: <00e24aa6-5d03-99b6-ab41-495a35fc93f6@gmail.com> Date: Fri, 21 Jul 2023 22:00:01 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: BUG #18014: Releasing catcache entries makes schema_to_xmlschema() fail when parallel workers are used Content-Language: en-US To: Tom Lane Cc: Kyotaro Horiguchi , pgsql-bugs@lists.postgresql.org References: <20230720.162802.905550494964943231.horikyota.ntt@gmail.com> <20230720.164459.2183367718473165651.horikyota.ntt@gmail.com> <20230720.172924.535889842187000361.horikyota.ntt@gmail.com> <1a2d5aba-df29-ab8d-65ac-6ffd54d1ac62@gmail.com> <969017.1689960055@sss.pgh.pa.us> From: Alexander Lakhin In-Reply-To: <969017.1689960055@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello Tom, Thank you for your considerations! 21.07.2023 20:20, Tom Lane wrote: > Alexander Lakhin writes: >> I think that we need to determine the level where the problem that should >> be fixed is: >> 1) test xmlmap fails sporadically due to the catalog changes caused by >>  parallel tests activity >> 2) schema_to_xmlschemaX() can fail when parallel workers are used >> 3) has_table_privilegeX() can fail sporadically when executed within a >>  parallel worker >> 4) SearchSysCacheX(RELOID, ...) can switch to a newer catalog snapshot, >>  when repeated in a parallel worker > Yeah, that's not immediately obvious. IIUC, the situation we are > looking at is that SearchSysCacheExists can succeed even though the > tuple we found is already dead at the instant that the function > exits (thanks to absorption of inval messages during relation_close). > The fact that that only happens in parallel workers is pure chance > really. It is not okay for has_table_privilegeX to depend on the > fact that the surrounding query already has some lock on pg_class. > So this means that the approach has_table_privilegeX uses of > assuming that successful SearchSysCacheExists means it can call > pg_class_aclcheck without fear is just broken. > > If we suppose that that assumption is only being made in the > has_foo_privilege functions, then one way we could fix it is to extend > the API of pg_class_aclcheck etc to add a no-error-on-not-found flag, > and get rid of the separate SearchSysCacheExists check. However, > I can't avoid the suspicion that we have other places assuming the > same thing. Running through SearchSysCacheExistsX() calls, I've found an interesting (and optimistic) comment in src/backend/catalog/namespace.c:  * ...  The underlying IsVisible functions  * always use an up-to-date snapshot and so might see the object as already  * gone when it's still visible to the transaction snapshot.  (There is no race  * condition in the current coding because we don't accept sinval messages  * between the SearchSysCacheExists test and the subsequent lookup.)  */ Datum pg_table_is_visible(PG_FUNCTION_ARGS) {     Oid         oid = PG_GETARG_OID(0);     if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(oid)))         PG_RETURN_NULL();     PG_RETURN_BOOL(RelationIsVisible(oid)); } ... bool RelationIsVisible(Oid relid) {     HeapTuple   reltup;     Form_pg_class relform;     Oid         relnamespace;     bool        visible;     reltup = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));     if (!HeapTupleIsValid(reltup))         elog(ERROR, "cache lookup failed for relation %u", relid); So that's exactly the same coding pattern, thus fixing pg_class_aclcheck is not an option, apparently. > So I think what we really ought to be doing is one > of two things: > > 1. Hack SearchSysCacheExists to account for this issue, by making it > loop if it finds a syscache entry but sees that the entry is already > dead. (We have to loop, not just return false, in case the row was > updated rather than deleted.) Maybe all the syscache lookup > functions need to do likewise; it's certainly not intuitively > reasonable for them to return already-known-stale entries. > > 2. Figure out how come we are executing a cache inval on the way > out of syscache entry creation, and stop that from happening. I wrote about LockRelationOid() before, and I still think that invalidation messages are processed in that call (reached via SearchCatCacheMiss() -> systable_beginscan() -> index_open() -> relation_open(), not via relation_close()). So it seems that SearchSysCacheX calls find an entry, that is not dead, but that entry can be dead (not found) for the next call if invalidation messages are processed. > I like #2 better if it's not hard to do cleanly. However, I'm not > quite sure how we are getting to an inval during relation close; > maybe that's not something we want to prevent. Yes, there is a detailed comment in LockRelationOid(), that explains why AcceptInvalidationMessages() is called. (I've tried to remove that call now, just for testing, and get 6 tests failed during `make check`.) Best regards, Alexander