Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 1C40A682EAC for ; Sat, 3 Dec 2011 11:10:19 -0400 (AST) Received: from [2001:1528:137::f1] (helo=mail.gransy.com) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RWrEH-0007LU-TN for pgsql-performance@postgresql.org; Sat, 03 Dec 2011 15:10:18 +0000 Received: from antispam.gransy.com (antispam.gransy.com [77.78.104.124]) by mail.gransy.com (Postfix) with ESMTP id 9BA1E4A20D1 for ; Sat, 3 Dec 2011 16:10:05 +0100 (CET) Received: from mail.gransy.com ([82.208.29.194]) by antispam.gransy.com (antispam.gransy.com [77.78.104.124]) (amavisd-new, port 10024) with ESMTP id peNTKoJ3-Z2n for ; Sat, 3 Dec 2011 16:10:05 +0100 (CET) Received: from [192.168.1.237] (ip-78-45-133-255.net.upcbroadband.cz [78.45.133.255]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.gransy.com (Postfix) with ESMTPSA id 70D144A20BE for ; Sat, 3 Dec 2011 16:10:05 +0100 (CET) Message-ID: <4EDA3BCD.30507@fuzzy.cz> Date: Sat, 03 Dec 2011 16:10:05 +0100 From: Tomas Vondra User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:8.0) Gecko/20111105 Thunderbird/8.0 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: manually force planner to use of index A vs index B References: <1322922869210-5044616.post@n5.nabble.com> In-Reply-To: <1322922869210-5044616.post@n5.nabble.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 2001:1528:137::f1 (failed) X-Pg-Spam-Score: -1.1 (-) X-Archive-Number: 201112/18 X-Sequence-Number: 45818 On 3.12.2011 15:34, MirrorX wrote: > > but, at some point there are some bulk inserts with a different date. when > this happens and i run the query mentioned above the planner is using index > A and not index B. > i guess this happens b/c the planner due to the last analyze statistics has > no values of the new date and so it thinks that it is faster to use index A > than index B since the rows that it will search are few. but that's not the > case so this query takes much longer to finish than it would take if it used > the index B. Probably. But you haven't posted any explain plans and I've broken my crystall ball yesterday, so I can only guess. Do this: 1) get EXPLAIN ANALYZE of the query running fine 2) do the bulk update 3) get EXPLAIN ANALYZE of the query (this time it uses the wrong index) 4) run ANALYZE on the table 5) get EXPLAIN ANALYZE of the query (should be using the right index) and post the tree explain plans. > i have thought of some work-arounds to resolve this situation. for example i > could change the definition of index A to (columnA,date) and i could also > run an analyze command after every bulk insert. Another option would be to > reduce autovacuum_analyze_scale_factor to a very low value so that analyze > would be forced to be made much more often. That is not a workaround, that is a solution. The database needs reasonably accurate statistics to prepare good plans, that's how it works. If you know that the bulk insert is going to make the statistics inaccurate, you should run ANALYZE manually at the end. Or you might let autovacuum take care of that. But the autovacuum won't fix that immediately - it's running each minute or so, so the queries executed before that will see the stale stats. You may lower the autocacuum naptime, you may make it more aggressive, but that means more overhead. Tomas