Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1is9nX-0000xU-UV for pgsql-performance@arkaria.postgresql.org; Thu, 16 Jan 2020 18:23:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1is9nW-0005vj-Oc for pgsql-performance@arkaria.postgresql.org; Thu, 16 Jan 2020 18:23:14 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1is9nW-0005vc-CI for pgsql-performance@lists.postgresql.org; Thu, 16 Jan 2020 18:23:14 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1is9nQ-0002yj-D6 for pgsql-performance@lists.postgresql.org; Thu, 16 Jan 2020 18:23:13 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id 00GIN5Nv004835; Thu, 16 Jan 2020 13:23:06 -0500 From: Tom Lane To: Cosmin Prund cc: pgsql-performance@lists.postgresql.org Subject: Re: Bad query plan decision when using multiple column index - postgresql uses only first column then filters In-reply-to: References: <19059.1579187486@sss.pgh.pa.us> Comments: In-reply-to Cosmin Prund message dated "Thu, 16 Jan 2020 19:18:24 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <4833.1579198985.1@sss.pgh.pa.us> Date: Thu, 16 Jan 2020 13:23:05 -0500 Message-ID: <4834.1579198985@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Cosmin Prund writes: > I know it's an odd choice of plan - that's why I'm here! Indeed. I cannot reproduce it here on 10.11: regression=# create table bb(f1 smallint, f2 serial, primary key(f1,f2)); CREATE TABLE regression=# explain select * from bb where f1 = 92 and f2 in (10,11); QUERY PLAN ----------------------------------------------------------------------- Index Only Scan using bb_pkey on bb (cost=0.15..8.34 rows=1 width=6) Index Cond: ((f1 = 92) AND (f2 = ANY ('{10,11}'::integer[]))) (2 rows) As I said before, as long as it chooses an indexscan at all, I wouldn't expect variation in what clauses it chooses to use with the index. So I don't see why this trivial example doesn't replicate your result. If you try exactly the above on your database, do you get my result, or a plan more like yours? I wonder if you have some extension installed that's causing the operators to be interpreted differently. BTW, why do you have two identical indexes on the table? > Indexes: > "PK_LucrareBugetDate" PRIMARY KEY, btree ("LucrareBugetVersiuneId", > "LucrareBugetDateId") > "IX_LucrareBugetDate_LucrareBugetVersiuneId_LucrareBugetDateId" btree > ("LucrareBugetVersiuneId", "LucrareBugetDateId") That shouldn't be affecting this either, but it seems wasteful. regards, tom lane