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 1mQTqV-00033o-9j for pgsql-performance@arkaria.postgresql.org; Wed, 15 Sep 2021 12:16:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mQTqU-0002a7-3v for pgsql-performance@arkaria.postgresql.org; Wed, 15 Sep 2021 12:16:58 +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 1mQTqT-0002Zz-S9 for pgsql-performance@lists.postgresql.org; Wed, 15 Sep 2021 12:16:57 +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 1mQTqR-0008Hv-NO for pgsql-performance@lists.postgresql.org; Wed, 15 Sep 2021 12:16:57 +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 18FCGqm4973796; Wed, 15 Sep 2021 08:16:52 -0400 From: Tom Lane To: Kristjan Mustkivi cc: Jeff Janes , Tomas Vondra , Pgsql Performance Subject: Re: Postgres chooses slow query plan from time to time In-reply-to: References: <31c01752-c4d6-7751-9d25-b1fac8f3a7aa@enterprisedb.com> <589350.1631628930@sss.pgh.pa.us> <632224.1631634441@sss.pgh.pa.us> Comments: In-reply-to Kristjan Mustkivi message dated "Wed, 15 Sep 2021 09:47:34 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <973794.1631708212.1@sss.pgh.pa.us> Date: Wed, 15 Sep 2021 08:16:52 -0400 Message-ID: <973795.1631708212@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Kristjan Mustkivi writes: > Both are of type varchar(30). Ah, right, you showed that back at the top of the thread. > So is this something odd: Filter: (((product_code)::text = ($1)::text) > AND ((balance_type)::text = ($4)::text)) ? Yes, that is very darn odd. When I try this I get: regression=# create table foo(f1 varchar(30), f2 int, primary key (f2,f1)); CREATE TABLE regression=# explain select * from foo where f2 = 11 and f1 = 'bar'; QUERY PLAN -------------------------------------------------------------------------- Index Only Scan using foo_pkey on foo (cost=0.15..8.17 rows=1 width=37) Index Cond: ((f2 = 11) AND (f1 = 'bar'::text)) (2 rows) regression=# explain select * from foo where f2 = 11 and f1::text = 'bar'; QUERY PLAN -------------------------------------------------------------------------- Index Only Scan using foo_pkey on foo (cost=0.15..8.17 rows=1 width=37) Index Cond: ((f2 = 11) AND (f1 = 'bar'::text)) (2 rows) regression=# prepare p as select * from foo where f2 = $1 and f1 = $2; PREPARE regression=# explain execute p(11,'bar'); QUERY PLAN -------------------------------------------------------------------------- Index Only Scan using foo_pkey on foo (cost=0.15..8.17 rows=1 width=37) Index Cond: ((f2 = 11) AND (f1 = 'bar'::text)) (2 rows) -- repeat a few times till it switches to a generic plan ... regression=# explain execute p(11,'bar'); QUERY PLAN -------------------------------------------------------------------------- Index Only Scan using foo_pkey on foo (cost=0.15..8.17 rows=1 width=37) Index Cond: ((f2 = $1) AND (f1 = $2)) (2 rows) Note the lack of any visible cast on the varchar column, in each one of these queries, even where I tried to force one to appear. There is something happening in your database that is not happening in mine. My mind is now running to the possibility that you've got some extension that creates an "=" operator that is capturing the syntax. regards, tom lane