Re: Domain based on TIMEZONE WITH TIME ZONE

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Ben Hood <ben(at)relops(dot)com>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Domain based on TIMEZONE WITH TIME ZONE
Date: 2018-05-10 14:33:05
Message-ID: 15270.1525962785@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Ben Hood <ben(at)relops(dot)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.

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.

(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 ...

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2018-05-10 14:37:51 Re: Is there any C functions that convert the entry to string?
Previous Message Karsten Hilbert 2018-05-10 14:17:21 Re: Domain based on TIMEZONE WITH TIME ZONE