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 1lyLOd-0007ey-P5 for pgsql-performance@arkaria.postgresql.org; Tue, 29 Jun 2021 21:35:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lyLOc-00062n-Lo for pgsql-performance@arkaria.postgresql.org; Tue, 29 Jun 2021 21:35:54 +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 1lyLOc-00062f-Bj for pgsql-performance@lists.postgresql.org; Tue, 29 Jun 2021 21:35:54 +0000 Received: from mail-pf1-x42c.google.com ([2607:f8b0:4864:20::42c]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lyLOS-0003oE-Cj for pgsql-performance@lists.postgresql.org; Tue, 29 Jun 2021 21:35:53 +0000 Received: by mail-pf1-x42c.google.com with SMTP id 17so237231pfz.4 for ; Tue, 29 Jun 2021 14:35:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=eE607LJencCiI9WmAGQ9xeIXxrmQsbaJoGi/WyWyxzA=; b=IxP8H2hoejYd33DbvQ48TXv3oUl6pgjKUIyAee0Szmc8OePwegyo/hzHnmBy2tp2pd PraP9IwRKnvAeKv/a+Ab6tXilIihYrH6xFoINl81VnbyU43xPBxhSdDI9m24t016DLcH 3a8uUQ2V5F0wLdFZ89GMTSq4g+Uxj3qJIXUHNS3BQQTEYCsZUh0KEU+wOeMM0GSwYn89 Z+Z9tDNWeavuAeA3HhteGMWh8zVKeia0kNgmXfDO4kYYurJ+McNcMor0Es1th5GVf928 WgAkv+oW8YUN1vVL07sj5b7pSLOYvNLg+Wa5Qgt7J+Gl3eq4KAn9kurgARzz7WXB0Fv8 +R0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=eE607LJencCiI9WmAGQ9xeIXxrmQsbaJoGi/WyWyxzA=; b=qoJSGvc7anrBZK/O3di1Q0PmogCBa+sR4ULUkGsx6fu+hdZVUS8LMUze3ToyOcYNSu sY1UUf0pENY8ZQizfyS8BGuFEiFsPyMLkDf6L9USuXhveQ6bZnfNv3gp1wFBtt+8JgKp ZEen3GOTYcxViIyIcrkxoxKZZxYhQp988SPVzbiPSaoWfWve2ZFm9Err31D8G/RCZHrL hbQWCwiFBwXf5wEua7Of3cj0wVx7AzuJVIc3osnrJsbsHBqMy+8WZ2G/DjwLfbG2G01T cUB7/+9uxKWoxDjX+gP+LM9eO/QFUKi5jbe9JAPFLUXbswQV46U9V1UT7shIr8g4HSLE 9dsA== X-Gm-Message-State: AOAM532COpyR+6I5oK201LRiUYNBjegkUjdP5dMY/NZUyM0x7rgeZuKH g8fsILDXMszpQyFwzqTaXZQ= X-Google-Smtp-Source: ABdhPJwHKTLBLDSz6oy9GKDkPsPFxGjLLdw4gMNj6TmetdpJXnJE/dzADhscjqA04uIXsd+iMdrwpA== X-Received: by 2002:aa7:96bb:0:b029:30c:1ccd:3225 with SMTP id g27-20020aa796bb0000b029030c1ccd3225mr13587022pfk.74.1625002542147; Tue, 29 Jun 2021 14:35:42 -0700 (PDT) Received: from smtpclient.apple (190-21-42-239.baf.movistar.cl. [190.21.42.239]) by smtp.gmail.com with ESMTPSA id lm21sm4431370pjb.8.2021.06.29.14.35.40 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 29 Jun 2021 14:35:41 -0700 (PDT) From: Manuel Weitzman Message-Id: Content-Type: multipart/mixed; boundary="Apple-Mail=_3624CDBD-E171-4000-9B6E-C53FCA854A31" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.100.0.2.22\)) Subject: Re: Planning performance problem (67626.278ms) Date: Tue, 29 Jun 2021 17:35:38 -0400 In-Reply-To: <500142.1624995808@sss.pgh.pa.us> Cc: Ranier Vilela , David Rowley , Jeremy Schneider , "pgsql-performance@lists.postgresql.org" , Andres Freund To: Tom Lane References: <6d58f68e-ed1a-c48e-1440-691554f6e1e7@9livesdata.com> <20190408144210.GG10080@telsasoft.com> <20190408232644.r6xixkeulsmomwfj@alap3.anarazel.de> <3f5e5673-06aa-bf7c-cdb7-0e053c45ba26@amazon.com> <767433.1619015825@sss.pgh.pa.us> <1588548.1624234651@sss.pgh.pa.us> <39C2266C-3143-4A7A-BCAF-E0C8394F9D24@gmail.com> <500142.1624995808@sss.pgh.pa.us> X-Mailer: Apple Mail (2.3654.100.0.2.22) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_3624CDBD-E171-4000-9B6E-C53FCA854A31 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii > On 29-06-2021, at 15:43, Tom Lane wrote: > > Manuel Weitzman writes: >>> On 20-06-2021, at 17:06, Tom Lane wrote: >>> So ... the reason why there's not caching of get_actual_variable_range >>> results already is that I'd supposed it wouldn't be necessary given >>> the caching of selectivity estimates that happens at the RestrictInfo >>> level. I don't have any objection in principle to adding another >>> caching layer if that one's not working well enough, but I think it'd >>> be wise to first understand why it's needed. > >> For what I could make out from the code, the caching done at the >> RestrictInfo level is already saving a lot of work, but there's a >> different RestrictInfo instance for each alternative path created by >> make_one_rel(). > > That seems a bit broken; a given WHERE clause should produce only one > RestrictInfo. Can you provide a more concrete example? > I added some logging to see hits and misses on cached_scansel() for this query > explain (analyze, buffers) > select * from a > join b b1 on (b1.a = a.a) > join b b2 on (b2.a = a.a) > where b1.a in (1,100,10000,1000000,1000001); Apparently there's a RestrictInfo for each possible way of doing merge join (are those created dynamically for planning?), for example: - a join (b1 join b2) - b1 join (a join b2) - b2 join (a join b1) When the cost of a possible mergejoin path hasn't been computed yet, then mergejoinscansel() would have to check the bloated index again. I attached a patch so you can see the hits and misses on cached_scansel(). Each time there's a miss logged, there's also a different RestrictInfo pointer involved. Best regards, Manuel --Apple-Mail=_3624CDBD-E171-4000-9B6E-C53FCA854A31 Content-Disposition: attachment; filename=cached_scansel_hitmiss.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="cached_scansel_hitmiss.patch" Content-Transfer-Encoding: 7bit diff --git i/src/backend/optimizer/path/costsize.c w/src/backend/optimizer/path/costsize.c index ffd2bf8783..180cdc5f10 100644 --- i/src/backend/optimizer/path/costsize.c +++ w/src/backend/optimizer/path/costsize.c @@ -3431,10 +3431,21 @@ cached_scansel(PlannerInfo *root, RestrictInfo *rinfo, PathKey *pathkey) if (cache->opfamily == pathkey->pk_opfamily && cache->collation == pathkey->pk_eclass->ec_collation && cache->strategy == pathkey->pk_strategy && - cache->nulls_first == pathkey->pk_nulls_first) + cache->nulls_first == pathkey->pk_nulls_first) { + elog(INFO, " [HIT][%p]\topfamily: %u, collation: %u, strategy: %d, nulls_first: %s, left_relids:%p, right_relids:%p", + rinfo, + pathkey->pk_opfamily, pathkey->pk_eclass->ec_collation, + pathkey->pk_strategy, pathkey->pk_nulls_first ? "t" : "f", + rinfo->left_relids, rinfo->right_relids); return cache; + } } + elog(INFO, "[MISS][%p]\topfamily:%u, collation: %u, strategy: %d, nulls_first: %s, left_relids:%p, right_relids:%p", + rinfo, + pathkey->pk_opfamily, pathkey->pk_eclass->ec_collation, + pathkey->pk_strategy, pathkey->pk_nulls_first ? "t" : "f", + rinfo->left_relids, rinfo->right_relids); /* Nope, do the computation */ mergejoinscansel(root, (Node *) rinfo->clause, --Apple-Mail=_3624CDBD-E171-4000-9B6E-C53FCA854A31--