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 1lXNiG-0002Fn-Mk for pgsql-bugs@arkaria.postgresql.org; Fri, 16 Apr 2021 12:36:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lXNiF-0004Kx-6S for pgsql-bugs@arkaria.postgresql.org; Fri, 16 Apr 2021 12:36:43 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lXNiF-0004Kq-0Z for pgsql-bugs@lists.postgresql.org; Fri, 16 Apr 2021 12:36:43 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lXNiA-0004BI-0R for pgsql-bugs@lists.postgresql.org; Fri, 16 Apr 2021 12:36:42 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 13GCaZoO4005579; Fri, 16 Apr 2021 08:36:35 -0400 From: Tom Lane To: zebburkeconte@gmail.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #16966: Nested loop joining across tables with varchar -> bpchar cast always scans varchar table first In-reply-to: <16966-f3ebf098ec2889b7@postgresql.org> References: <16966-f3ebf098ec2889b7@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Thu, 15 Apr 2021 21:13:03 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <4005577.1618576595.1@sss.pgh.pa.us> Date: Fri, 16 Apr 2021 08:36:35 -0400 Message-ID: <4005578.1618576595@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk PG Bug reporting form writes: > I'm seeing a performance issue when joining across two tables on columns > that require a cast from varchar to bpchar. No matter how selective the > condition is on the bpchar table, the outer scan will be on the table with > the varchar column. I don't think this is a planner problem. It can't generate the plan you are hoping for because of index mismatch. The query's join condition is effectively "a.varcharcol::bpchar = b.bpcharcol", and the construct "a.varcharcol::bpchar" doesn't match your index on a.varcharcol, so it can't use an inner indexscan on that side of the equality. Possibly you could work around this by providing an expression index on "a.varcharcol::bpchar". But TBH my recommendation would be to nuke the bpchar columns from orbit. They're almost never the semantics you want, especially not if you're sometimes comparing them to non-bpchar columns. regards, tom lane