X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 3BF6FD1B4E4 for ; Wed, 22 Oct 2003 22:46:02 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 10865-02 for ; Wed, 22 Oct 2003 19:45:32 -0300 (ADT) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by svr1.postgresql.org (Postfix) with ESMTP id BBA4DD1B4FB for ; Wed, 22 Oct 2003 19:45:29 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.10/8.12.10) with ESMTP id h9MMjKit024642; Wed, 22 Oct 2003 18:45:20 -0400 (EDT) To: josh@agliodbs.com Cc: "scott.marlowe" , Chris Gamache , pgsql-sql Subject: Re: Query planner: current_* vs. explicit date In-reply-to: <200310221325.18470.josh@agliodbs.com> References: <200310221325.18470.josh@agliodbs.com> Comments: In-reply-to Josh Berkus message dated "Wed, 22 Oct 2003 13:25:18 -0700" Date: Wed, 22 Oct 2003 18:45:20 -0400 Message-ID: <24641.1066862720@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200310/295 X-Sequence-Number: 15695 Josh Berkus writes: >> I'd guess that the planner doesn't know what current_date::timestamp is >> ahead of time, so it chooses a seq scan. > Yes, this is a known problem. There was a single-query workaround, but I > can't remember it right now. Right; the problem from the planner's point of view is that it sees WHERE datecol >= unknownvalue and it has no idea how much of the table will be fetched by this query. Its default guess is that a fair fraction of the table will be fetched (one-third, I think) and that dissuades it from using an indexscan. This seems reasonable to me, since the worst-case behavior if it were to default to an indexscan could be awful. The easiest workaround is to convert the query into a range query, for example WHERE datecol >= current_date AND datecol <= current_date + 1000; being careful that both comparison values are nonconstant (don't use 'infinity'::timestamp, for instance, even though that might seem like a reasonable thing to do). The planner still has no idea how many rows will be fetched exactly, but it does realize that this is a range condition, and its default assumption about the number of matching rows is small enough to encourage indexscan use. Of course this workaround assumes that you can pick an upper bound that you are sure is past the end of the available values, but that's usually not hard in the sort of context where you would have thought that the one-sided inequality test is a sane thing to do anyway. regards, tom lane