Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Xq3TU-0004gw-GR for pgsql-performance@arkaria.postgresql.org; Sun, 16 Nov 2014 17:18:56 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Xq3TS-0003KA-00 for pgsql-performance@arkaria.postgresql.org; Sun, 16 Nov 2014 17:18:54 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Xq3TP-0003K1-Rt for pgsql-performance@postgresql.org; Sun, 16 Nov 2014 17:18:52 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Xq3TM-0006qt-0Z for pgsql-performance@postgresql.org; Sun, 16 Nov 2014 17:18:49 +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 sAGHIkT0002543; Sun, 16 Nov 2014 12:18:46 -0500 From: Tom Lane To: Yuri Kunde Schlesner cc: pgsql-performance@postgresql.org Subject: Re: Plan uses wrong index, preferring to scan pkey index instead In-reply-to: <1416093360.655916.191452721.03160196@webmail.messagingengine.com> References: <1416093360.655916.191452721.03160196@webmail.messagingengine.com> Comments: In-reply-to Yuri Kunde Schlesner message dated "Sat, 15 Nov 2014 21:16:00 -0200" Date: Sun, 16 Nov 2014 12:18:46 -0500 Message-ID: <2542.1416158326@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org Yuri Kunde Schlesner writes: > Does anyone know if there's any tweaking I can do in Postgres so that it > uses the appropriate plan? I suspect that the reason the planner likes the backlog_pkey is that it's almost perfectly correlated with table order, which greatly reduces the number of table fetches that need to happen over the course of a indexscan compared to using the less-well-correlated bufferid+messageid index. So that way is estimated to be cheaper than using the less-correlated index ... and that may even be true except for outlier bufferid values with no recent messages. You could try fooling around with the planner cost parameters (particularly random_page_cost) to see if that changes the decision; but it's usually a bad idea to alter cost parameters on the basis of tweaking a single query, and even more so for tweaking an outlier case of a single query. What I think might be a workable solution, assuming you can stand a little downtime to do it, is to CLUSTER the table on the bufferid+messageid index. This would reverse the correlation advantage and thereby solve your problem. Now, ordinarily CLUSTER is only a temporary solution because the cluster-induced ordering degrades over time. But I think it would likely be a very long time until you accumulate so many new messages that the table as a whole looks well-correlated on messageid alone. regards, tom lane -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance