pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Zaremba, Don <dzaremba@ellacoya.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: Use of to_timestamp causes full scan
Date: Tue, 02 Sep 2003 14:06:56 -0400
Message-ID: <23506.1062526016@sss.pgh.pa.us> (raw)
In-Reply-To: <E3A785CF5322D611A20D0003474E0C3C9B5DCF@postal.ellacoya.com>
References: <E3A785CF5322D611A20D0003474E0C3C9B5DCF@postal.ellacoya.com>

"Zaremba, Don" <dzaremba@ellacoya.com> 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



view thread (2+ messages)

Message-ID: <23506.1062526016@sss.pgh.pa.us>
Permalink:  ../23506.1062526016@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/23506.1062526016@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-performance@postgresql.org
  Cc: tgl@sss.pgh.pa.us, dzaremba@ellacoya.com
  Subject: Re: Use of to_timestamp causes full scan
  In-Reply-To: <23506.1062526016@sss.pgh.pa.us>
  References: <E3A785CF5322D611A20D0003474E0C3C9B5DCF@postal.ellacoya.com> <23506.1062526016@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox