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 1pU8l4-00042P-0G for pgsql-bugs@arkaria.postgresql.org; Mon, 20 Feb 2023 16:11:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pU8l2-0001Q3-Ty for pgsql-bugs@arkaria.postgresql.org; Mon, 20 Feb 2023 16:11:16 +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 1pU8l2-0001Pu-Le for pgsql-bugs@lists.postgresql.org; Mon, 20 Feb 2023 16:11:16 +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 1pU8ky-0007SO-3W for pgsql-bugs@lists.postgresql.org; Mon, 20 Feb 2023 16:11:15 +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 31KGBADZ3573389; Mon, 20 Feb 2023 11:11:10 -0500 From: Tom Lane To: Charles cc: pgsql-bugs@lists.postgresql.org Subject: Re: Query run in 27s with 15.2 vs 37ms with 14.6 In-reply-to: References: Comments: In-reply-to Charles message dated "Mon, 20 Feb 2023 22:55:52 +0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3573387.1676909470.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 20 Feb 2023 11:11:10 -0500 Message-ID: <3573388.1676909470@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Charles writes: > I have just upgraded my postgresql from 14.6 to 15.2 and my query that r= uns > in 37ms in 14.6 run requires 27 seconds to complete. The table is a typi= cal > OHLC table (date date, code text, open int, high int, low int, close int= , > volume bigint, value bigint), 2725207 rows in table. You need to do something to fix this poor row-count estimate: > -> Index Scan using idx_stock_price_date on stock_price s > (cost=3D0.43..1152.53 rows=3D1 width=3D29) (actual time=3D0.180..4.060 = rows=3D779 loops=3D1) > Index Cond: (date =3D '2023-02-20'::date) > Filter: ((open > 0) AND (value > 0) AND (length(code) =3D 4)) > Rows Removed by Filter: 210 Considering that 14.x also estimated this as returning only one row, I'm fairly surprised that you didn't get the same poor plan choice there. The length(code) condition suggests a fairly poor choice of data representation; can you change that? In any case, the planner does not keep statistics that would help it estimate that. It might be useful to try defining extended statistics on length(code) as such, or if that doesn't help then on the combination of open, value, and length(code). But you need to get that estimate up to at least several rows to discourage the choice of nestloop join. regards, tom lane