X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 4D60AD1B8BA for ; Tue, 2 Sep 2003 18:07:13 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 86965-07 for ; Tue, 2 Sep 2003 15:07:03 -0300 (ADT) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by svr1.postgresql.org (Postfix) with ESMTP id 6930BD1B53A for ; Tue, 2 Sep 2003 15:07:02 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.9/8.12.9) with ESMTP id h82I6ujX023507; Tue, 2 Sep 2003 14:06:56 -0400 (EDT) To: "Zaremba, Don" Cc: pgsql-performance@postgresql.org Subject: Re: Use of to_timestamp causes full scan In-reply-to: References: Comments: In-reply-to "Zaremba, Don" message dated "Tue, 02 Sep 2003 13:50:14 -0400" Date: Tue, 02 Sep 2003 14:06:56 -0400 Message-ID: <23506.1062526016@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/15 X-Sequence-Number: 3381 "Zaremba, Don" writes: > This does a full sequential scan > select id from details where begin_time > to_timestamp('03/08/25 > 18:30'); to_timestamp('foo') is not a constant, so the planner doesn't know how much of the table this is going to select. In the absence of that knowledge, its default guess favors a seqscan. > This uses the index > select id from details where begin_time > '03/08/25 18:30'; Here the planner can consult pg_stats to get a pretty good idea how much of the table will be scanned; if the percentage is small enough it will pick an indexscan. There are various ways to deal with this --- one thing you might consider is making a wrapper function for to_timestamp that is marked "immutable", so that it will be constant-folded on sight. That has potential gotchas if you want to put the query in a function though. Another tack is to make the query into a range query: where begin_time > ... AND begin_time < 'infinity'; See the archives for more discussion. regards, tom lane