Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id AA5221337D70 for ; Thu, 14 Oct 2010 12:37:38 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 06417-04-2 for ; Thu, 14 Oct 2010 15:37:23 +0000 (UTC) X-Greylist: delayed 00:05:11.901043 by SQLgrey-1.7.6 Received: from forward15.mail.yandex.net (forward15.mail.yandex.net [95.108.130.119]) by mail.postgresql.org (Postfix) with ESMTP id 205591337D24 for ; Thu, 14 Oct 2010 12:34:11 -0300 (ADT) Received: from smtp11.mail.yandex.net (smtp11.mail.yandex.net [95.108.130.67]) by forward15.mail.yandex.net (Yandex) with ESMTP id 5E06B44589A6 for ; Thu, 14 Oct 2010 19:28:54 +0400 (MSD) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1287070134; bh=xyV7VfTj5Zea9VrtTSVg39OSsqNLu0V6Hueq7VFuz14=; h=Message-ID:Date:From:MIME-Version:To:Subject:Content-Type: Content-Transfer-Encoding; b=WCSV/miBK9Z9DgcrjO2eFrIqQGs2AuridmY1iersBvHs+UN2wyGyIJjnjbydz8257 Cwxa/gHJG5Abrmt09h3nGQqbZN0eroD1w4/z1eH4hy4R8aArtljq0eGPjSzEfmc/Nw sIdO7spebOtHTXOOTW0nKUBpbqfocaJ8kr7nb0Wc= Received: from [192.168.0.91] (unknown [94.242.50.174]) by smtp11.mail.yandex.net (Yandex) with ESMTPSA id 35A3C44D807A for ; Thu, 14 Oct 2010 19:28:54 +0400 (MSD) Message-ID: <4CB7268D.4060901@yandex.ru> Date: Thu, 14 Oct 2010 19:49:33 +0400 From: Nikolai Zhubr User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.1.5) Gecko/20091204 Thunderbird/3.0 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Index scan / Index cond limitation or ? Content-Type: text/plain; charset=KOI8-R; format=flowed Content-Transfer-Encoding: 7bit X-Yandex-TimeMark: 1287070134 X-Yandex-Spam: 1 X-Yandex-Front: smtp11.mail.yandex.net X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.001 tagged_above=-10 required=5 tests=BAYES_20=-0.001 X-Spam-Level: X-Archive-Number: 201010/255 X-Sequence-Number: 40673 Hello people, I'm having trouble to persuade index scan to check all of the conditions I specify _inside_ index cond. That is, _some_ condition always get pushed out of index cond and applied later (which will often result, for my real table contents, in too many unwanted rows initially hit by index scan and hence randomly slow queries) An index with all relevant columns does exist of course. Here goes an example. create table foo ( id serial primary key, rec_time timestamp with time zone DEFAULT now(), some_value integer, some_data text ); CREATE INDEX foo_test ON foo (id, rec_time, some_value); set enable_seqscan = false; set enable_bitmapscan = true; explain select id from foo where true and rec_time > '2010-01-01 22:00:06' --and rec_time < '2010-10-14 23:59' and some_value in (1, 2) and id > 123 This one works perfectly as I want it (and note "and rec_time < ... " condition is commented out): Bitmap Heap Scan on foo (cost=13.18..17.19 rows=1 width=4) Recheck Cond: ((id > 123) AND (rec_time > '2010-01-01 22:00:06+03'::timestamp with time zone) AND (some_value = ANY ('{1,2}'::integer[]))) -> Bitmap Index Scan on foo_test (cost=0.00..13.18 rows=1 width=0) Index Cond: ((id > 123) AND (rec_time > '2010-01-01 22:00:06+03'::timestamp with time zone) AND (some_value = ANY ('{1,2}'::integer[])))" Now, as soon as I enable "and rec_time < ... " condition, I get the following: explain select id from foo where true and rec_time > '2010-01-01 22:00:06' and rec_time < '2010-10-14 23:59' and some_value in (1, 2) and id > 123 Bitmap Heap Scan on foo (cost=8.59..13.94 rows=1 width=4) Recheck Cond: ((id > 123) AND (rec_time > '2010-01-01 22:00:06+03'::timestamp with time zone) AND (rec_time < '2010-10-14 23:59:00+04'::timestamp with time zone)) Filter: (some_value = ANY ('{1,2}'::integer[])) -> Bitmap Index Scan on foo_test (cost=0.00..8.59 rows=2 width=0) Index Cond: ((id > 123) AND (rec_time > '2010-01-01 22:00:06+03'::timestamp with time zone) AND (rec_time < '2010-10-14 23:59:00+04'::timestamp with time zone)) So, "in (1, 2)" condition is not in Index Cond anymore! Why is that? How can I push it back? SELECT version(); PostgreSQL 8.3.1, compiled by Visual C++ build 1400 but the behaviour seems exactly the same in 9.0 (just checked it briefly). Thank you! Please CC me, I'm not on the list. Nikolai