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 1ksgHn-0005QY-1F for pgsql-general@arkaria.postgresql.org; Fri, 25 Dec 2020 06:09:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ksgHl-0001KB-L8 for pgsql-general@arkaria.postgresql.org; Fri, 25 Dec 2020 06:09:09 +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 1ksgHl-0001K4-DR for pgsql-general@lists.postgresql.org; Fri, 25 Dec 2020 06:09:09 +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 1ksgHi-0007Xq-Qq for pgsql-general@lists.postgresql.org; Fri, 25 Dec 2020 06:09:08 +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 0BP692D91135858; Fri, 25 Dec 2020 01:09:03 -0500 From: Tom Lane To: Shantanu Shekhar cc: "pgsql-general@lists.postgresql.org" Subject: Re: Stats for indexes on expressions In-reply-to: <1258055935.2714481.1608874336314@mail.yahoo.com> References: <1258055935.2714481.1608874336314.ref@mail.yahoo.com> <1258055935.2714481.1608874336314@mail.yahoo.com> Comments: In-reply-to Shantanu Shekhar message dated "Fri, 25 Dec 2020 05:32:16 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1135856.1608876542.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 25 Dec 2020 01:09:02 -0500 Message-ID: <1135857.1608876542@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Shantanu Shekhar writes: > I am trying to understand how the cost for a query involving indexes on = expressions is calculated. How is the statistics on the expression maintai= ned? For example Postgres documentation on 'Indexes on Expressions' mentio= ns the following example: > CREATE INDEX people_names ON people ((first_name || ' ' || last_name));S= ELECT * FROM people WHERE (first_name || ' ' || last_name) =3D 'John Smith= '; > The index is created on an expression involving first name and last name= . My confusion is basically around: > (1) When using this index how will the query planner get the stats on th= is expression? ANALYZE on a table will (re)build statistics for any expression indexes on that table, in addition to the stats for the table's own columns. (2) Whenever an index on an expression is defined like this, should we reb= uild the stats? If you don't want to wait around for auto-analyze to do it, yes. regards, tom lane