Re: Fwd: Re: Dynamic update of a date field

From: Steve Crawford <scrawford(at)pinpointresearch(dot)com>
To: John R Pierce <pierce(at)hogranch(dot)com>
Cc: pgsql-general(at)postgresql(dot)org, David Salisbury <salisbury(at)globe(dot)gov>
Subject: Re: Fwd: Re: Dynamic update of a date field
Date: 2012-02-17 00:45:36
Message-ID: 4F3DA330.5010807@pinpointresearch.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 02/16/2012 02:45 PM, John R Pierce wrote:
> On 02/16/12 2:34 PM, David Salisbury wrote:
>>
>> Would it not be advantageous to replicate information in the above
>> form if you wanted to, say, get all records in the month of May, and
>> therefore create an index on the month field? I would think that
>> would be more efficient than creating a functional index on a timestamp.
>> And if you're not too picky, that would give an index that couldn't
>> be done on a timestamptz field, as that's mutable.
>
> an index on a timestamp will work just fine on date_trunc('month',
> timestampfield)
>
>
>
You can try but PostgreSQL will respond:
...functions in index expression must be marked IMMUTABLE...

The current month returned by extract or date_trunc depends on the
current time zone. New York will see March 3-hours ahead of us
left-coasters.

Note: storing the month in a separate field does not solve this problem
- it just shuffles it around and requires additional mechanisms to
update that field when the timestamp field changes.

You can, if it is appropriate to the situation, specify the time zone in
which you are interested:
... (date_trunc('month', timestampfield at time zone
'posix/America/Los_Angeles') ) ...

Cheers,
Steve

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jack Christensen 2012-02-17 01:39:28 Functions that return a set in select column list
Previous Message Jack Christensen 2012-02-17 00:34:49 Set returning functions in select column list