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 1lPehs-0001ps-Dp for pgsql-general@arkaria.postgresql.org; Fri, 26 Mar 2021 05:08:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lPehr-0001bd-3O for pgsql-general@arkaria.postgresql.org; Fri, 26 Mar 2021 05:08:23 +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 1lPehq-0001bW-S6 for pgsql-general@lists.postgresql.org; Fri, 26 Mar 2021 05:08:22 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lPehj-0000Dy-V6 for pgsql-general@lists.postgresql.org; Fri, 26 Mar 2021 05:08:22 +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 12Q58Drt1556145; Fri, 26 Mar 2021 01:08:13 -0400 From: Tom Lane To: Jagmohan Kaintura cc: "pgsql-generallists.postgresql.org" Subject: Re: PostgreSQL Views Where Clause Character Columns getting Typecasted to Text and Indexes not getting Used In-reply-to: References: Comments: In-reply-to Jagmohan Kaintura message dated "Fri, 26 Mar 2021 10:01:45 +0530" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1556143.1616735293.1@sss.pgh.pa.us> Date: Fri, 26 Mar 2021 01:08:13 -0400 Message-ID: <1556144.1616735293@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Jagmohan Kaintura writes: > When we are compiling View definition into database , the where clause on > the Character Columns is getting internally typecasted to text. That's normal. In Postgres, varchar has no operators of its own; everything you might do with it involves a (no-cost) cast to text. You might think of varchar as being a domain over text, though for historical reasons it's not implemented exactly that way. > These Columns are Primary Key columns and because of this Type Casting on > those columns Index scan is not happening and we are always getting > Sequential Scan. This, on the other hand, is just nonsense. You haven't presented any examples in which an indexscan looks likely to be a win. Postgres generally won't bother with an index when scanning a tiny table. Nor is an index necessarily helpful when doing a join, unless the join is designed to access just a small part of the table. > Please help us in identifying the solution. Please read https://wiki.postgresql.org/wiki/Slow_Query_Questions regards, tom lane