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 1fGoa4-0004zk-5s for pgsql-general@arkaria.postgresql.org; Thu, 10 May 2018 16:38:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fGoa2-000111-H2 for pgsql-general@arkaria.postgresql.org; Thu, 10 May 2018 16:38:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fGoa2-00010q-84 for pgsql-general@lists.postgresql.org; Thu, 10 May 2018 16:38:10 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fGoZz-0003yf-1E for pgsql-general@lists.postgresql.org; Thu, 10 May 2018 16:38:09 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id E83CB20DC7; Thu, 10 May 2018 12:38:05 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Thu, 10 May 2018 12:38:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=relops.com; h= content-type:date:from:in-reply-to:message-id:mime-version :references:subject:to:x-me-sender:x-me-sender:x-sasl-enc; s= mesmtp; bh=QTtbNwbTx+ylmmO+jYQmDzWjVanWu4VAmoAOnaFyxQs=; b=iCHTj xUhl29I60J7cZpkyW7SCXoVYbWuYNbj3wOWrlVadKCjI7kFC3JDehhe41AfECnR9 DY8C9hv+qYXfzQhN5Y+kR6L7o/oibEvsYD8cyxvfAlbCiGmJtGZJIAr888WbKivB V/T738WLEz1WH+mD7UrDoQx0kkQYVB2/gkjz9U= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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=QTtbNwbTx+ylmmO+jYQmDzWjVanWu 4VAmoAOnaFyxQs=; b=Twh2/R/0FxxiSBiCCdTR+7VBM6LgMIcFAKvvPpwvopdix XRO4FRFw5xyH11sbtonSz9qdtwYcXzRaelM6fzwp0XSTulDDOZrKQUkVDQVrT3DE xcHgAcpkeMknqCAQSvwhIWWauVarZTJtmvvpZ5UrDqSSqpLbjY1CDFaS7qVSCiHG 61dtC7gZGIdxIe8NuMcpOHEaAXo7W70WxagTHDcKpRw9uYxlhgTD9/+cJ/ER9W6j m+ZxiETbymAmrX4YeDV2VWPDNjJCxDY2HxF35z1gAYVcgjhZAA+APFbpSG8wOdr8 YdfXuhzOVcheYRXP8mzNJf33vfMtWa+NO4XVUH9fw== X-ME-Sender: Received: from [192.168.15.94] (unknown [161.12.41.14]) by mail.messagingengine.com (Postfix) with ESMTPA id 5D629E479F for ; Thu, 10 May 2018 12:38:05 -0400 (EDT) From: Ben Hood Content-Type: multipart/alternative; boundary="Apple-Mail=_EA94EC7A-E218-4C21-9C6B-44B66D60D930" Mime-Version: 1.0 (Mac OS X Mail 11.3 \(3445.6.18\)) Subject: Re: Domain based on TIMEZONE WITH TIME ZONE Date: Thu, 10 May 2018 17:38:04 +0100 References: <06EAD729-DC35-4E48-AB25-C42F7FA19C28@relops.com> <188A0270-0A9A-4ADE-A928-9067E99AE7B7@relops.com> <15270.1525962785@sss.pgh.pa.us> To: pgsql-general@lists.postgresql.org In-Reply-To: <15270.1525962785@sss.pgh.pa.us> Message-Id: X-Mailer: Apple Mail (2.3445.6.18) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_EA94EC7A-E218-4C21-9C6B-44B66D60D930 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On 10 May 2018, at 15:33, Tom Lane wrote: >=20 > Ben Hood writes: >> So the question is not how does the timestamp get stored, rather, is = it an anti-pattern to use Postgres as a linter for apps that forget to = use UTC exclusively? >=20 > Well, using a domain to enforce additional constraints on a field's = value > is certainly not an anti-pattern in itself. But you have to realize = that > the processing consists of first creating a value of the base type and > then applying the constraint expressions of the domain to it. This = means > you cannot check any details that are lost in the input conversion, > because you don't have access to the original input string, only the > stored value. Fair point. In the case of the domain based on TIMESTAMP WITH TIME ZONE, if you are = mandating UTC, what further details could get lost that may have been in = the original input string? The semantics are that inserting into a column of this domain with a = non-UTC or absent zone will be rejected and hence is data you don=E2=80=99= t want in the database in any case. >=20 > As others have explained, Postgres' TIMESTAMP WITH TIME ZONE type = doesn't > preserve the input's timezone specification (if any) but forcibly = rotates > to UTC and stores just a scalar UTC value. So you can't use a domain = to > check anything about whether the input had a timezone field and if so = what > it was. OK, point taken. The intention of the domain is prevent any non-UTC or = absent zones, so do you need to check anything after you have inserted = it? >=20 > (This behavior is nonstandard --- the SQL spec evidently expects the > timezone to be stored explicitly in some fashion --- but I don't = foresee > us changing it; we've accumulated too much backwards-compatibility > baggage now.) >=20 > If you're sufficiently intent on having checking of that sort, you = could > invent your own datatype with your own input function, and then make = it > binary-compatible with timestamptz so that you don't need to provide = much > else besides the I/O functions. varchar(n) has the same sort of > relationship with text, so there's precedent =E2=80=A6 Many thanks for your feedback, much appreciated. What I hear you saying = is that is possible and similar things have been done before. But by the = same token, just because you can do it, doesn=E2=80=99t necssarily mean = it buys you that much.=20 So if a person of your experience hasn=E2=80=99t come across something = like this before, it might mean that on balance it=E2=80=99s not worth = the effort and/or potential deviation from standard DB tooling. Be = pragmatic. As an aside, this whole discussion has been super valuable for me to get = a better understanding of exactly what is going on. This is precisely = the intention of the domain in the first place - to be explicit about = timestamps everywhere. So if I get timestamp UTC explicitness everywhere in my database by = actually understanding what is going on as opposed to using a domain, = then I have achieved my goal. I appreciate everybody chiming in on this topic :-) --Apple-Mail=_EA94EC7A-E218-4C21-9C6B-44B66D60D930 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8

On 10 May 2018, at 15:33, Tom Lane <tgl@sss.pgh.pa.us> = wrote:

Ben Hood <ben@relops.com> writes:
So the question is not how does the timestamp = get stored, rather, is it an anti-pattern to use Postgres as a linter = for apps that forget to use UTC exclusively?

Well, using a domain to enforce = additional constraints on a field's value
is certainly not = an anti-pattern in itself.  But you have to realize that
the processing consists of first creating a value of the base = type and
then applying the constraint expressions of the = domain to it.  This means
you cannot check any = details that are lost in the input conversion,
because you = don't have access to the original input string, only the
stored value.

Fair point.

In = the case of the domain based on TIMESTAMP WITH TIME ZONE, if you are = mandating UTC, what further details could get lost that may have been in = the original input string?

The = semantics are that inserting into a column of this domain with a non-UTC = or absent zone will be rejected and hence is data you don=E2=80=99t want = in the database in any case.



As others have explained, Postgres' TIMESTAMP = WITH TIME ZONE type doesn't
preserve the input's timezone = specification (if any) but forcibly rotates
to UTC and = stores just a scalar UTC value.  So you can't use a domain to
check anything about whether the input had a timezone field = and if so what
it was.


OK, point taken. The intention of the domain is = prevent any non-UTC or absent zones, so do you need to check anything = after you have inserted it?



(This behavior is nonstandard --- the SQL spec = evidently expects the
timezone to be stored explicitly in = some fashion --- but I don't foresee
us changing it; we've = accumulated too much backwards-compatibility
baggage = now.)

If you're sufficiently intent on = having checking of that sort, you could
invent your own = datatype with your own input function, and then make it
binary-compatible with timestamptz so that you don't need to = provide much
else besides the I/O functions. =  varchar(n) has the same sort of
relationship with = text, so there's precedent =E2=80=A6

Many thanks for your feedback, much appreciated. = What I hear you saying is that is possible and similar things have been = done before. But by the same token, just because you can do it, = doesn=E2=80=99t necssarily mean it buys you that much. 

So if a person of your experience hasn=E2=80=99= t come across something like this before, it might mean that on balance = it=E2=80=99s not worth the effort and/or potential deviation from = standard DB tooling. Be pragmatic.

As an aside, this whole discussion has = been super valuable for me to get a better understanding of exactly what = is going on. This is precisely the intention of the domain in the first = place - to be explicit about timestamps everywhere.

So if I get timestamp = UTC explicitness everywhere in my database by actually understanding = what is going on as opposed to using a domain, then I have achieved my = goal.

I = appreciate everybody chiming in on this topic :-)

= --Apple-Mail=_EA94EC7A-E218-4C21-9C6B-44B66D60D930--