Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fHXbY-0003N2-6u for pgsql-general@arkaria.postgresql.org; Sat, 12 May 2018 16:42:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fHXbW-0005Uw-VL for pgsql-general@arkaria.postgresql.org; Sat, 12 May 2018 16:42:42 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fHXbU-0005Um-W5 for pgsql-general@lists.postgresql.org; Sat, 12 May 2018 16:42:42 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fHXbR-0001pA-Ma for pgsql-general@lists.postgresql.org; Sat, 12 May 2018 16:42:39 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id CB01F25D5A; Sat, 12 May 2018 12:42:33 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Sat, 12 May 2018 12:42:33 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=o8a5b2/RO+fZXCHvEZs3CcWoxF+Ex d226zq2QldEoAk=; b=cSRoGrj6/dXi4IqgWBJJ0OtTUESlB0UVFw+z5DDQkg1Gk AoJC9tmZvSE7qkSP99Fw/L8jRHu3gTdNsRjGhaacGChTBzn+j058B2Uuc1EKQZfs GSdrXkN22GqSkKiOfXzNWHgUqqaknq1HvKkpK1JkqllctUAVsKRoA36MT6XcAxQG jrdtd4gmeR5In3W8ofqINTYqWgtFBAhqv6HwCMzC1jth4t0mBlRVnOtBSdVHe88K tiUGfHyCFOg2MdKQW3DsQTL0ln5AncnnwJzN9hBcVwnqeOwq2Y0tN5H5suGGFBty 9xTJkRvXlQEpRaz6trYTwiRIcQ2jOc2QBcSZtcalA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=o8a5b2 /RO+fZXCHvEZs3CcWoxF+Exd226zq2QldEoAk=; b=SwZk4Gf2x8G9uN0PPNeSPl wlFMz1VcZ8VuZQ5b+nvyX72+5MKRiqxIrkPzPfZJCiJ9oOu6Gmm+VqlBB+BA3D5r GR9OrOmZhNrxywiR6qT4HZvrXOJ4lxAxBz/pDOEd/T4IWJ8G59ISQu6JTYSYwSvH 9r5bUeaHaFD8pCC2pDO7apAjn7E2Iv1YjyNrul6j6PULDY2uSttU/wyOA61izlex XC8shtIJfZHe03esUYAQTPaZqGh9FNOHWs64XQSr3CYrNOV3+757fSQhhLyb1wlw VVxA/0wkYuwdbIzSMB5TAAw6ULkCWj0CLz2BL1+UQRIMUSls3PIeUDKEh6wJCyhQ == X-ME-Sender: Received: from [192.168.1.2] (unknown [71.212.135.74]) by mail.messagingengine.com (Postfix) with ESMTPA id D9409E43C8; Sat, 12 May 2018 12:42:32 -0400 (EDT) Subject: Re: Domain based on TIMEZONE WITH TIME ZONE To: Francisco Olarte , Ben Hood Cc: pgsql-general@lists.postgresql.org References: <06EAD729-DC35-4E48-AB25-C42F7FA19C28@relops.com> <188A0270-0A9A-4ADE-A928-9067E99AE7B7@relops.com> From: Adrian Klaver Message-ID: <2fcedf5b-cac6-40a9-dc83-c3b6833e3a49@aklaver.com> Date: Sat, 12 May 2018 09:42:31 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 05/12/2018 04:04 AM, Francisco Olarte wrote: > On Thu, May 10, 2018 at 10:37 PM, Ben Hood wrote: >> On 10 May 2018, at 16:33, Francisco Olarte 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