Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gfaTQ-0004tY-5f for pgsql-general@arkaria.postgresql.org; Sat, 05 Jan 2019 01:10:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gfaTO-0006Zj-Rm for pgsql-general@arkaria.postgresql.org; Sat, 05 Jan 2019 01:09:58 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gfaTO-0006ZP-GR for pgsql-general@lists.postgresql.org; Sat, 05 Jan 2019 01:09:58 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gfaTL-0008Jq-D5 for pgsql-general@postgresql.org; Sat, 05 Jan 2019 01:09:57 +0000 Received: from [192.168.127.1] (port=15470 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1gfaTI-0007tL-1V; Sat, 05 Jan 2019 01:09:52 +0000 Received: from [127.0.0.1] (port=65111 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1gfaTG-000D7C-RG; Sat, 05 Jan 2019 01:09:50 +0000 From: Andrew Gierth To: Ken Tanzer Cc: PG-General Mailing List Subject: Re: Immutable way to cast timestamp TEXT to DATE? (for index) In-Reply-To: (Ken Tanzer's message of "Fri, 4 Jan 2019 15:20:27 -0800") Message-ID: <87va33rjx6.fsf@news-spur.riddles.org.uk> References: <875zv4rquz.fsf@news-spur.riddles.org.uk> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Sat, 05 Jan 2019 01:09:50 +0000 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Ken" == Ken Tanzer 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)