pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: Ken Tanzer <ken.tanzer@gmail.com>
Cc: PG-General Mailing List <pgsql-general@postgresql.org>
Subject: Re: Immutable way to cast timestamp TEXT to DATE? (for index)
Date: Sat, 05 Jan 2019 01:09:50 +0000
Message-ID: <87va33rjx6.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <CAD3a31UPrsEzwqrfRBhRvHoiYGpjgfWPusvfMOFtrGCKVsE3Wg@mail.gmail.com>
References: <CAD3a31XupySutj+RGLxQrOqji5gBZo6Z05rAYgRDRH10wi4Mag@mail.gmail.com>
	<875zv4rquz.fsf@news-spur.riddles.org.uk>
	<CAD3a31UPrsEzwqrfRBhRvHoiYGpjgfWPusvfMOFtrGCKVsE3Wg@mail.gmail.com>

>>>>> "Ken" == Ken Tanzer <ken.tanzer@gmail.com> writes:

 >> If you absolutely can't change the column type, then one option
 >> would be to do your own fixed-format date parsing function (and
 >> label it immutable), e.g.
 >> 
 >> create function iso_timestamp(text)
 >> returns timestamp without time zone
 >> as $$ select case when $1 ~ '^\d\d\d\d-?\d\d-?\d\dT\d\d:?\d\d:?\d\d$'
 >> then $1::timestamp
 >> else null end $$
 >> set DateStyle = 'ISO,YMD'
 >> language sql immutable strict;
 >> 
 >> or
 >> 
 >> create function iso_date(text)
 >> returns date
 >> as $$ select case when $1 ~ '^\d\d\d\d-?\d\d-?\d\d(?![^T])'
 >> then substring($1 from '^\d\d\d\d-?\d\d-?\d\d')::date
 >> else null end $$
 >> set DateStyle = 'ISO,YMD'
 >> language sql immutable strict;

 Ken> Yeah, I thought I might have to do that, but when I create that
 Ken> index it still doesn't seem to use the index for queries.

It won't use the index unless you use the same function in the query
too.

i.e.

CREATE INDEX ON ... (iso_date("Service_Date"));

SELECT * FROM ... WHERE iso_date("Service_Date") BETWEEN ... AND ...;

-- 
Andrew (irc:RhodiumToad)




view thread (7+ messages)

Message-ID: <87va33rjx6.fsf@news-spur.riddles.org.uk>
Permalink:  ../87va33rjx6.fsf@news-spur.riddles.org.uk/
Also on:    postgresql.org/message-id/87va33rjx6.fsf@news-spur.riddles.org.uk

 · 

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-general@postgresql.org
  Cc: andrew@tao11.riddles.org.uk, ken.tanzer@gmail.com
  Subject: Re: Immutable way to cast timestamp TEXT to DATE? (for index)
  In-Reply-To: <87va33rjx6.fsf@news-spur.riddles.org.uk>
  References: <CAD3a31XupySutj+RGLxQrOqji5gBZo6Z05rAYgRDRH10wi4Mag@mail.gmail.com> <875zv4rquz.fsf@news-spur.riddles.org.uk> <CAD3a31UPrsEzwqrfRBhRvHoiYGpjgfWPusvfMOFtrGCKVsE3Wg@mail.gmail.com> <87va33rjx6.fsf@news-spur.riddles.org.uk>

* 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