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 1qrYdC-004njx-Sc for pgsql-bugs@arkaria.postgresql.org; Sat, 14 Oct 2023 07:00:14 +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 1qrYdA-006X84-JD for pgsql-bugs@arkaria.postgresql.org; Sat, 14 Oct 2023 07:00:13 +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 1qrYdA-006X7w-84 for pgsql-bugs@lists.postgresql.org; Sat, 14 Oct 2023 07:00:13 +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 1qrYd4-000PUZ-6n for pgsql-bugs@lists.postgresql.org; Sat, 14 Oct 2023 07:00:11 +0000 Received: by mail-lf1-x130.google.com with SMTP id 2adb3069b0e04-5045cb9c091so3576510e87.3 for ; Sat, 14 Oct 2023 00:00:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697266803; x=1697871603; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:references:cc:to:from :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=kyg0VA/NbEI9FfEK4UiMDmcKs4J4D9tzs6Fna1GPxN4=; b=THWconkbT8oZ9P3cuYjYuvXM5yyNdLbcRQhuNkdw7t063DMXLObHlgwOoRxdenUXzW MNn1Yr+24DolMfOzU1AZnroeowd/gwt5QsE+B1RrfmR8cy0WcFfAZh1R5G1OO8uJNvP2 2fu5DwySothNrawelUTctd1UMSblKvykCsXsnRMgTLMvJu5/+YMApnrRefdfTN/f/cnC 47egy5TZepIzPZ3IweUb6ScZ1Omzqn3gA7HJup75Zc9YYkpz4BQGgOne26uW2VV5SEUW uPSv16Tz1xbH7OpmWDpobWm2e/5rWrWTIJFDM9S50V9EIM0kth4DCfbsgU5k2ohV821o MX/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697266803; x=1697871603; h=content-transfer-encoding:in-reply-to:references:cc:to:from :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=kyg0VA/NbEI9FfEK4UiMDmcKs4J4D9tzs6Fna1GPxN4=; b=f2Fo9BVxOBNSluwId6NuQP8HlqR/p19nhEbEMe7KWtHwSCExD7eFMZqeDj/SY4+YnL 47vZbq3CdGCUMk1b05u2O17XApwwW4ppYU6ZjZAV/1hNJ6ESEKEIdszMjfJ7cFoSGjqZ hAo5NOmkqpbQXq6u6IJsk2izRdQLIwXjW+rgDQTfRQexuw46X8QO+UiW8UiwnCdoASBk 7bHA0sl1mwBR4YGMj42ZUsD7/eOI+l2tkVkjBR9ah/+bWwxokeyl/ZGmoqEV/nQWCfq1 zsOpmmpD7BT17WDYbP7XkhJWspLB2Gd0Ciw4+aa1gnVKhVW5ITXDrFA+dVCJ4GM3A7FC hGrg== X-Gm-Message-State: AOJu0Yy+uUiqNSolmspjtoGlNkmu0dP3oxfp1VpkCg1RSmrJ0ohcQjIp xXdlznIaydHar+H0r1lO+LM= X-Google-Smtp-Source: AGHT+IGzt4E0fDE8Sl/Dv92pXMwdK0QxoVu9KLMiwJIbx/HjdKUG1/YfD4neHJnR2OS020wCBaTWMg== X-Received: by 2002:a19:4f0f:0:b0:503:389d:1cc1 with SMTP id d15-20020a194f0f000000b00503389d1cc1mr23328395lfb.22.1697266803160; Sat, 14 Oct 2023 00:00:03 -0700 (PDT) Received: from [1.0.0.7] ([81.23.166.137]) by smtp.gmail.com with ESMTPSA id f7-20020a056512360700b004ff6fa3f038sm979904lfs.144.2023.10.14.00.00.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 14 Oct 2023 00:00:02 -0700 (PDT) Message-ID: Date: Sat, 14 Oct 2023 10:00:00 +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 From: Alexander Lakhin To: Tom Lane , Michael Paquier Cc: Kyotaro Horiguchi , pgsql-bugs@lists.postgresql.org References: <00e24aa6-5d03-99b6-ab41-495a35fc93f6@gmail.com> <982076.1689967306@sss.pgh.pa.us> <5bd4cbfe-fc71-c576-851d-3bb21ef5c080@gmail.com> <20230726.112923.27361680552823861.horikyota.ntt@gmail.com> <1821881.1690339317@sss.pgh.pa.us> <1830960.1690343673@sss.pgh.pa.us> <1389919.1697144487@sss.pgh.pa.us> <5df89a35-a985-b287-7210-6ed7d2c31803@gmail.com> In-Reply-To: <5df89a35-a985-b287-7210-6ed7d2c31803@gmail.com> 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 13.10.2023 18:00, Alexander Lakhin wrote: > >> I spent some time looking through existing SearchSysCacheExists calls, >> and I could only find two sets of routines where we seem to be >> depending on SearchSysCacheExists to protect a subsequent lookup >> somewhere else, and there isn't any lock on the object in question. >> Those are the has_foo_privilege functions discussed here, and the >> foo_is_visible functions near the bottom of namespace.c.  I'm not >> sure why we've not heard complaints traceable to the foo_is_visible >> family.  Maybe nobody has tried hard to break them, or maybe they >> are just less likely to be used in ways that are at risk. > > I'll try to research/break xxx_is_visible and share my findings tomorrow. > I tried the script based on the initial reproducer [1]: for ((n=1;n<=30;n++)); do echo "ITERATION $n" numclients=30 for ((c=1;c<=$numclients;c++)); do cat << EOF | psql >psql_$c.log & CREATE SCHEMA testxmlschema_$c; SELECT format('CREATE TABLE testxmlschema_$c.test_%s (a int);', g) FROM generate_series(1, 30) g \\gexec SET parallel_setup_cost = 1; SET min_parallel_table_scan_size = '1kB'; SELECT oid FROM pg_catalog.pg_class WHERE relnamespace = 1 AND  relkind IN ('r', 'm', 'v') AND pg_catalog.pg_table_is_visible(oid); SELECT format('DROP TABLE testxmlschema_$c.test_%s', g) FROM generate_series(1, 30) g \\gexec DROP SCHEMA testxmlschema_$c; EOF done wait grep 'ERROR:' server.log && break; done And couldn't get the error, for multiple runs. (Here SELECT oid ... is based on the query executed by schema_to_xmlschema().) But I could reliably get the error with s/pg_table_is_visible(oid)/has_table_privilege (oid, 'SELECT')/. So there is a difference between these two functions. And the difference is in their costs. If I do "ALTER FUNCTION pg_table_is_visible COST 1" before the script, I get the error as expected. With cost 10 I see the following plan:  Index Scan using pg_class_relname_nsp_index on pg_class (cost=0.42..2922.38 rows=1 width=4)    Index Cond: (relnamespace = '1'::oid)    Filter: ((relkind = ANY ('{r,m,v}'::"char"[])) AND pg_table_is_visible(oid)) But with cost 1:  Gather  (cost=1.00..257.10 rows=1 width=4)    Workers Planned: 2    Workers Launched: 2    ->  Parallel Seq Scan on pg_class  (cost=0.00..256.00 rows=1 width=4)          Filter: (pg_table_is_visible(oid) AND (relnamespace = '1'::oid) AND (relkind = ANY ('{r,m,v}'::"char"[])))          Rows Removed by Filter: 405 The cost of the pg_foo_is_visible functions was increased in a80889a73. But all the has_xxx_privilige functions have cost 1, except for has_any_column_privilege, which cost was also increased in 7449427a1. So to see the issue we need several ingredients: 1) The mode CATCACHE_FORCE_RELEASE enabled (may be some other way is  possible, I don't know of);     - Thanks to prion for that. 2) A function with the coding pattern  "SearchSysCacheExistsX();  SearchSysCacheX();" called in a parallel worker;     - Thanks to "debug_parallel_query = regress" and low cost of       has_table_privilege() called by schema_to_xmlschema(). 3) The catalog cache invalidated by some concurrent activity.     - Thanks to running the test xmlmap in parallel with 16 other tests. [1] https://www.postgresql.org/message-id/18014-28c81cb79d44295d%40postgresql.org Best regards, Alexander