pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Francisco Olarte <folarte@peoplecall.com>
To: Ben Hood <ben@relops.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Domain based on TIMEZONE WITH TIME ZONE
Date: Sat, 12 May 2018 09:42:31 -0700
Message-ID: <2fcedf5b-cac6-40a9-dc83-c3b6833e3a49@aklaver.com> (raw)
In-Reply-To: <CA+bJJbw96Ndugh11T9WWdGzUL+Cm6=CGxChi=4yfxSO7dnneJQ@mail.gmail.com>
References: <06EAD729-DC35-4E48-AB25-C42F7FA19C28@relops.com>
	<CA+bJJbz0Gwo8mkyzHa_4qjcNcMV0M00yTZ_LjUtaJu=M=+bpyQ@mail.gmail.com>
	<188A0270-0A9A-4ADE-A928-9067E99AE7B7@relops.com>
	<CA+bJJbyNTns-AMVUbztjb+zeNZ-z0gH=6vXykZCmjCJiK1GogA@mail.gmail.com>
	<D03AF820-9FFF-414F-8747-B3589FBB9224@relops.com>
	<CA+bJJbw96Ndugh11T9WWdGzUL+Cm6=CGxChi=4yfxSO7dnneJQ@mail.gmail.com>

On 05/12/2018 04:04 AM, Francisco Olarte wrote:
> On Thu, May 10, 2018 at 10:37 PM, Ben Hood <ben@relops.com> wrote:
>> On 10 May 2018, at 16:33, Francisco Olarte <folarte@peoplecall.com> wrote:
>>
>> For what you want to do I think you'll have to parse the text value,
>> maybe by definig a view with a text columns and using some
>> rule/trigger magic for insert / updates.
>>
>>
>> Sorry for being unclear - the solution I have in production appears to work
>> with
>>
>> CREATE DOMAIN UTC_TIMESTAMP AS TIMESTAMP WITH TIME ZONE CHECK
>> (EXTRACT(TIMEZONE FROM VALUE) = 0);
>>
>> This raises an exception when an app doesn’t use UTC.
> ....
>> This is what I wanted to achieve and the approach so far seems to work.
> 
> Can you post an example  ( correct, error inputs, preferrably done with psql ) ?
> 
> At this point I do not know if it is working or if it seems to working
> for you. I see some cases in which it would seem to work, but I would
> like to know what you mean by "sending non utc timestamps to the
> database".
In the post below Ben explains that he realizes it is not working the 
way he thought:

https://www.postgresql.org/message-id/1465CD01-C08A-49BF-BCF3-193E6509A703%40relops.com

 From what I can gather it comes down where UTC is being enforced. Ben 
was hoping that the domain would force timestamps to be only submitted 
with a UTC offset with the idea that this would force storage in the 
field as UTC only. The realization is that if you have a field of type 
timestamp with time zone the value is going to be stored as UTC 
regardless of the offset that is presented(even in cases where there is 
no offset presented, when an implicit one is assigned). That means there 
really is no need for the domain.

> 
> I mean something like the one attached below, which to me seems to
> prove it does not work, it just checks that the session timezone is
> utc, note the 1st transaction tests an explicit timestamp value,
> without conversions, failing in non-utc, the second one checks an
> explicit +0000 zone not working in non UTC and the third one checks
> anything goes , with or without timestamp, when the time zone is utc.
> 
> cdrs=# show timezone;
>     TimeZone
> ---------------
>   Europe/Madrid
> (1 row)
> 
> cdrs=# begin;
> BEGIN
> cdrs=# CREATE DOMAIN UTC_TIMESTAMP AS TIMESTAMP WITH TIME ZONE CHECK
> (EXTRACT(TIMEZONE FROM VALUE) = 0);
> CREATE DOMAIN
> cdrs=# create table t(ts utc_timestamp);
> CREATE TABLE
> cdrs=# insert into t values ( current_timestamp );
> ERROR:  value for domain utc_timestamp violates check constraint
> "utc_timestamp_check"
> cdrs=# rollback;
> ROLLBACK
> cdrs=# select current_timestamp;
>                now
> -------------------------------
>   2018-05-12 12:58:03.616949+02
> (1 row)
> 
> cdrs=# begin;
> BEGIN
> cdrs=# CREATE DOMAIN UTC_TIMESTAMP AS TIMESTAMP WITH TIME ZONE CHECK
> (EXTRACT(TIMEZONE FROM VALUE) = 0);
> CREATE DOMAIN
> cdrs=# create table t(ts utc_timestamp);
> CREATE TABLE
> cdrs=# insert into t values ('2015-05-11 10:20:30+0000');
> ERROR:  value for domain utc_timestamp violates check constraint
> "utc_timestamp_check"
> cdrs=# rollback;
> ROLLBACK
> cdrs=# set timezone TO 'UTC';
> SET
> cdrs=# select current_timestamp;
>                now
> -------------------------------
>   2018-05-12 10:59:47.946338+00
> (1 row)
> 
> cdrs=# begin;
> BEGIN
> cdrs=# CREATE DOMAIN UTC_TIMESTAMP AS TIMESTAMP WITH TIME ZONE CHECK
> (EXTRACT(TIMEZONE FROM VALUE) = 0);
> CREATE DOMAIN
> cdrs=# create table t(ts utc_timestamp);
> CREATE TABLE
> cdrs=# insert into t values ('2015-05-11 10:20:30+0000');
> INSERT 0 1
> cdrs=# insert into t values ('2015-05-11 10:20:30+0200');
> INSERT 0 1
> cdrs=# insert into t values ('2015-05-11 10:20:30');
> INSERT 0 1
> cdrs=# insert into t values (current_timestamp);
> INSERT 0 1
> cdrs=# select * from t;
>                ts
> -------------------------------
>   2015-05-11 10:20:30+00
>   2015-05-11 08:20:30+00
>   2015-05-11 10:20:30+00
>   2018-05-12 10:59:54.289827+00
> (4 rows)
> 
> cdrs=# rollback;
> ROLLBACK
> 
> 
> Francisco Olarte.
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com




view thread (36+ messages)  latest in thread

Message-ID: <2fcedf5b-cac6-40a9-dc83-c3b6833e3a49@aklaver.com>
Permalink:  ../2fcedf5b-cac6-40a9-dc83-c3b6833e3a49@aklaver.com/
Also on:    postgresql.org/message-id/2fcedf5b-cac6-40a9-dc83-c3b6833e3a49@aklaver.com

 · 

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: adrian.klaver@aklaver.com, folarte@peoplecall.com, ben@relops.com
  Subject: Re: Domain based on TIMEZONE WITH TIME ZONE
  In-Reply-To: <2fcedf5b-cac6-40a9-dc83-c3b6833e3a49@aklaver.com>
  References: <06EAD729-DC35-4E48-AB25-C42F7FA19C28@relops.com> <CA+bJJbz0Gwo8mkyzHa_4qjcNcMV0M00yTZ_LjUtaJu=M=+bpyQ@mail.gmail.com> <188A0270-0A9A-4ADE-A928-9067E99AE7B7@relops.com> <CA+bJJbyNTns-AMVUbztjb+zeNZ-z0gH=6vXykZCmjCJiK1GogA@mail.gmail.com> <D03AF820-9FFF-414F-8747-B3589FBB9224@relops.com> <CA+bJJbw96Ndugh11T9WWdGzUL+Cm6=CGxChi=4yfxSO7dnneJQ@mail.gmail.com> <2fcedf5b-cac6-40a9-dc83-c3b6833e3a49@aklaver.com>

* 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