Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eHWXN-0000VS-J9 for pgsql-performance@arkaria.postgresql.org; Wed, 22 Nov 2017 15:02:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eHWXN-0003vD-68 for pgsql-performance@arkaria.postgresql.org; Wed, 22 Nov 2017 15:02:05 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eHWXM-0003v4-Ts for pgsql-performance@lists.postgresql.org; Wed, 22 Nov 2017 15:02:05 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eHWXI-0000co-3j for pgsql-performance@lists.postgresql.org; Wed, 22 Nov 2017 15:02:03 +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 vAMF1vqA012729; Wed, 22 Nov 2017 10:01:57 -0500 From: Tom Lane To: =?iso-8859-2?Q?Artur_Zaj=B1c?= cc: pgsql-performance@lists.postgresql.org Subject: Re: Bad estimates In-reply-to: <03b401d3639e$5d7493a0$185dbae0$@ang.com.pl> References: <03b401d3639e$5d7493a0$185dbae0$@ang.com.pl> Comments: In-reply-to =?iso-8859-2?Q?Artur_Zaj=B1c?= message dated "Wed, 22 Nov 2017 15:29:54 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <12727.1511362917.1@sss.pgh.pa.us> Date: Wed, 22 Nov 2017 10:01:57 -0500 Message-ID: <12728.1511362917@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk =?iso-8859-2?Q?Artur_Zaj=B1c?= writes: [ poor estimates for WHERE clauses like "(gs & 1) = 1" ] Don't hold your breath waiting for that to get better on its own. You need to work with the planner, not expect it to perform magic. It has no stats that would help it discover what the behavior of that sort of WHERE clause is; nor is there a good reason for it to think that the selectivity of such a clause is only 0.5 rather than something more in line with the usual behavior of an equality constraint on an integer value. One way you could attack the problem, if you're wedded to this data representation, is to create expression indexes on the terms "(gs & x)" for all the values of x you use. Not only would that result in better estimates (after an ANALYZE) but it would also open the door to satisfying this type of query through an index search. A downside is that updating all those indexes could make DML on the table pretty expensive. If you're not wedded to this data representation, consider replacing that integer flags column with a bunch of boolean columns. You might or might not want indexes on the booleans, but in any case ANALYZE would create stats that would allow decent estimates for "WHERE boolval". regards, tom lane