Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 276FC1337A67 for ; Fri, 15 Apr 2011 14:25:38 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 66255-08 for ; Fri, 15 Apr 2011 17:25:17 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from exprod8og106.obsmtp.com (exprod8og106.obsmtp.com [64.18.3.92]) by mail.postgresql.org (Postfix) with ESMTP id CC81E133616B for ; Fri, 15 Apr 2011 14:25:16 -0300 (ADT) Received: from mail.jiveland.com ([70.98.39.50]) (using TLSv1) by exprod8ob106.postini.com ([64.18.7.12]) with SMTP ID DSNKTah/fEgSPvjiibTFdVSUz7EylCkPRrHG@postini.com; Fri, 15 Apr 2011 10:25:17 PDT Received: from [10.61.130.191] (70.98.39.250) by mail.jiveland.com (10.61.65.25) with Microsoft SMTP Server (TLS) id 8.2.254.0; Fri, 15 Apr 2011 17:18:33 +0000 Message-ID: <4DA87DAC.30006@jivesoftware.com> Date: Fri, 15 Apr 2011 10:17:32 -0700 From: Mark Williams User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.14) Gecko/20110223 Thunderbird/3.1.8 MIME-Version: 1.0 To: Subject: Bad Query Plan with Range Query Content-Type: text/plain; charset="ISO-8859-1"; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201104/222 X-Sequence-Number: 43284 We are experiencing a problem with our query plans when using a range query in Postgresql 8.3. The query we are executing attempts to select the minimum primary key id after a certain date. Our date columns are bigint's holding a unix epoch representation of the date. We have an index on the primary key and the date column. For the following query just specified the predicate modificationDate >= ? explain SELECT min(messageID) FROM Message WHERE modificationDate >= 1302627793988; QUERY PLAN --------------------------------------------------------------------------------------------------------- Result (cost=2640.96..2640.97 rows=1 width=0) InitPlan -> Limit (cost=0.00..2640.96 rows=1 width=8) -> Index Scan using message_pk on message (cost=0.00..3298561.09 rows=1249 width=8) Filter: ((messageid IS NOT NULL) AND (modificationdate >= 1302627793988::bigint)) (5 rows) For some reason it is deciding to scan the primary key column of the table. This results in scanning the entire table which is huge (10 million records). However, if we specify a fake upper bound then the planner will correctly use the date column index: explain SELECT min(messageID) FROM Message WHERE modificationDate >= 1302627793988 and modificationDate < 9999999999999999; QUERY PLAN --------------------------------------------------------------------------------------------------------------------- Aggregate (cost=9.64..9.65 rows=1 width=8) -> Index Scan using jvmssg_mdate_idx on message (cost=0.00..9.64 rows=1 width=8) Index Cond: ((modificationdate >= 1302627793988::bigint) AND (modificationdate < 9999999999999999::bigint)) (3 rows) We have carried out all the usual maintenance tasks. We have increase the statistics_target on both indexes to the maximum (1000) and performed a vacuum analyze on the table. Our resource configurations are very good since this is our production server. Interestingly this does not appear to happen with exactly the same database when using 8.4. Instead we get the correct plan without having to add the upper bound. Here is the full description of the the table. It contains upwards of 10 million rows. Table "public.message" Column | Type | Modifiers ------------------+------------------------+----------- messageid | bigint | not null parentmessageid | bigint | threadid | bigint | not null containertype | integer | not null containerid | bigint | not null userid | bigint | subject | character varying(255) | body | text | modvalue | integer | not null rewardpoints | integer | not null creationdate | bigint | not null modificationdate | bigint | not null status | integer | not null Indexes: "message_pk" PRIMARY KEY, btree (messageid) "jvmssg_cdate_idx" btree (creationdate) "jvmssg_cidctmd_idx" btree (containerid, containertype, modificationdate) "jvmssg_mdate_idx" btree (modificationdate) "jvmssg_mdvle_idx" btree (modvalue) "jvmssg_prntid_idx" btree (parentmessageid) "jvmssg_thrd_idx" btree (threadid) "jvmssg_usrid_idx" btree (userid) Referenced by: TABLE "answer" CONSTRAINT "answer_mid_fk" FOREIGN KEY (messageid) REFERENCES message(messageid) TABLE "messageprop" CONSTRAINT "jmp_msgid_fk" FOREIGN KEY (messageid) REFERENCES message(messageid) Any insight into this would be greatly appreciated. We are not able to upgrade our databases to 8.4. We are reluctant to re-write all our range queries if possible. -m