Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nIv3Q-0007ET-Vk for pgsql-bugs@arkaria.postgresql.org; Sat, 12 Feb 2022 16:15:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nIv3O-0007IU-3z for pgsql-bugs@arkaria.postgresql.org; Sat, 12 Feb 2022 16:15:18 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nIv3N-0007I8-RA for pgsql-bugs@lists.postgresql.org; Sat, 12 Feb 2022 16:15:17 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nIv3K-0001ck-Pp for pgsql-bugs@lists.postgresql.org; Sat, 12 Feb 2022 16:15:16 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 21CGFCuE2280017; Sat, 12 Feb 2022 11:15:12 -0500 From: Tom Lane To: Troy Frericks cc: pgsql-bugs@lists.postgresql.org Subject: Re: A bug with the TimeStampTZ data type and the 'AT TIME ZONE' clause In-reply-to: References: Comments: In-reply-to Troy Frericks message dated "Fri, 11 Feb 2022 17:30:40 -0600" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2280015.1644682512.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sat, 12 Feb 2022 11:15:12 -0500 Message-ID: <2280016.1644682512@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Troy Frericks writes: > *CREATE* *TABLE* tsTable (tsCol *timestamptz*); > *INSERT* *INTO* tsTable *VALUES* ('2022-02-07 11:00:00' *AT* *TIME* *ZON= E* > 'EST'); -- 11:00 AM EST All these asterisks make your example unreadable, not to mention very difficult to copy-and-paste. Please do not do that in future. Anyway, the problem here is that you are invoking the wrong one of the two AT TIME ZONE operators. There's one that takes timestamp and returns timestamptz, which is what you meant to use (or should have, anyway), but there's also one that takes timestamptz and returns timestamp. In the absence of any type decoration, the second one will win because timestamptz is a preferred type. So what you actually got out of that was (a) the undecorated literal was presumed to be of time timestamptz, and since it doesn't mention a UTC offset, was assumed to be in your timezone, so you got the equivalent of: regression=3D# show timezone; TimeZone = ----------------- America/Chicago (1 row) regression=3D# select '2022-02-07 11:00:00'::timestamptz; timestamptz = ------------------------ 2022-02-07 11:00:00-06 (1 row) (b) AT TIME ZONE rotates that to a timestamp-without-tz, expressed in the EST zone: regression=3D# select '2022-02-07 11:00:00'::timestamptz AT TIME ZONE 'EST= '; timezone = --------------------- 2022-02-07 12:00:00 (1 row) Note the lack of any zone indicator in the output --- that's the easiest way to tell whether a value is timestamp or timestamptz, though you could also try applying pg_typeof() to the expression. (c) INSERT casts that back to timestamptz, again assuming your prevailing zone, so what actually goes into the table is regression=3D# select ('2022-02-07 11:00:00'::timestamptz AT TIME ZONE 'ES= T')::timestamptz; timezone = ------------------------ 2022-02-07 12:00:00-06 (1 row) If you'd cast the unmarked literal to timestamp explicitly, you'd have got the behavior you're after: regression=3D# select ('2022-02-07 11:00:00'::timestamp AT TIME ZONE 'EST'= )::timestamptz; timezone = ------------------------ 2022-02-07 10:00:00-06 (1 row) > CONFIDENTIALITY NOTICE: > This message and any attached files from iSeatz, = > Inc. contain information that is confidential and proprietary under = > applicable agreements and/or law. You really need to point out to your corporate lawyers that this sort of thing is unenforceable, counterproductive, and silly-looking. You submitted to a publicly-archived mailing list. If I thought this notice actually meant anything, I've have been afraid to answer at all. regards, tom lane