Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sRCv6-00Ds2e-Vp for pgsql-hackers@arkaria.postgresql.org; Tue, 09 Jul 2024 15:38:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sRCv5-00GBm5-IM for pgsql-hackers@arkaria.postgresql.org; Tue, 09 Jul 2024 15:38:19 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sRCv5-00GBlx-16 for pgsql-hackers@lists.postgresql.org; Tue, 09 Jul 2024 15:38:19 +0000 Received: from pb-smtp2.pobox.com ([64.147.108.71]) by makus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sRCv2-001E06-2b for pgsql-hackers@lists.postgresql.org; Tue, 09 Jul 2024 15:38:17 +0000 Received: from pb-smtp2.pobox.com (unknown [127.0.0.1]) by pb-smtp2.pobox.com (Postfix) with ESMTP id 7A0E52AB2E; Tue, 9 Jul 2024 11:38:14 -0400 (EDT) (envelope-from david@justatheory.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=pobox.com; h= content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; s=sasl; bh= FilctFNLEB/CntiP1ONrpvRbS4z6rnRhn0v+8TN4Eh8=; b=ydUtpXlsuH5/ST0E dmq6X8js1OxC1FZlI6Xr5WnyDGNy/LCV2I3+3V8hJ78BPbkrwjUh2ekRfZrEC2Vl EUfbYPs1IEtwS8codGf3bPDolNZRwWswcXSR36yfI8KUFxXmDixQmsSaS6AgYpAR w6I6CgS/ebhGuMW6FQpZ7J+/7Pk= Received: from pb-smtp2.nyi.icgroup.com (unknown [127.0.0.1]) by pb-smtp2.pobox.com (Postfix) with ESMTP id 7166C2AB2D; Tue, 9 Jul 2024 11:38:14 -0400 (EDT) (envelope-from david@justatheory.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=justatheory.com; h=content-type:mime-version:subject:from:in-reply-to:date:cc:content-transfer-encoding:message-id:references:to; s=2016-12.pbsmtp; bh=FilctFNLEB/CntiP1ONrpvRbS4z6rnRhn0v+8TN4Eh8=; b=bwcbWrIpFRiz1F9BVEw5sigwBc3ZQSIundLHhoSRpKLYDUTd8fKld6Nee691rkGvpcS3Nez/hJTDg2HOJGqzc8aYci+cf2JIyul4w79GhyED+hE35o7mJkUKgwg3Qcj/yVZ41K3uoJgy/r9RY02wixvZuLwCxJPi8WcOoLdOtNA= Received: from smtpclient.apple (unknown [158.222.197.125]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pb-smtp2.pobox.com (Postfix) with ESMTPSA id CFE342AB2B; Tue, 9 Jul 2024 11:38:13 -0400 (EDT) (envelope-from david@justatheory.com) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3774.600.62\)) Subject: Re: jsonpath: Inconsistency of timestamp_tz() Output From: "David E. Wheeler" In-Reply-To: Date: Tue, 9 Jul 2024 11:38:02 -0400 Cc: PostgreSQL Hackers Content-Transfer-Encoding: quoted-printable Message-Id: References: <7DE080CE-6D8C-4794-9BD1-7D9699172FAB@justatheory.com> <53E4B67F-70FF-441E-A48B-DE520966A312@justatheory.com> To: Junwang Zhao X-Mailer: Apple Mail (2.3774.600.62) X-Pobox-Relay-ID: 411375D0-3E09-11EF-894E-965B910A682E-76319746!pb-smtp2.pobox.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Jul 9, 2024, at 11:08, Junwang Zhao wrote: > In JsonbValue.val.datatime, there is a tz field, I think that's where > the offset stored, it is 18000 in the first example >=20 > struct > { > Datum value; > Oid typid; > int32 typmod; > int tz; /* Numeric time zone, in seconds, for > * TimestampTz data type */ > } datetime; Oooh, okay, so it=E2=80=99s a jsonb variant of the type. Interesting. = Ah, and it=E2=80=99s assigned here[1]: jb->val.datetime.tz =3D tz; It seems like JSONB timestamptz values want to display the recorded time = zone, so I suspect we need to set it when the converting from a non-tz = to a local tz setting, something like this: ``` patch diff --git a/src/backend/utils/adt/jsonpath_exec.c = b/src/backend/utils/adt/jsonpath_exec.c index d79c929822..f63b3b9330 100644 --- a/src/backend/utils/adt/jsonpath_exec.c +++ b/src/backend/utils/adt/jsonpath_exec.c @@ -2707,12 +2707,16 @@ executeDateTimeMethod(JsonPathExecContext *cxt, = JsonPathItem *jsp, break; case jpiTimestampTz: { + struct pg_tm *tm; /* Convert result type to timestamp with = time zone */ switch (typid) { case DATEOID: = checkTimezoneIsUsedForCast(cxt->useTz, = "date", "timestamptz"); + if = (timestamp2tm(DatumGetTimestamp(value), NULL, tm, NULL, NULL, NULL) =3D=3D= 0) { + tz =3D = DetermineTimeZoneOffset(tm, session_timezone); + } value =3D = DirectFunctionCall1(date_timestamptz, = value); break; @@ -2726,6 +2730,9 @@ executeDateTimeMethod(JsonPathExecContext *cxt, = JsonPathItem *jsp, case TIMESTAMPOID: = checkTimezoneIsUsedForCast(cxt->useTz, = "timestamp", "timestamptz"); + if = (timestamp2tm(DatumGetTimestamp(value), NULL, tm, NULL, NULL, NULL) =3D=3D= 0) { + tz =3D = DetermineTimeZoneOffset(tm, session_timezone); + } value =3D = DirectFunctionCall1(timestamp_timestamptz, = value); break; ``` Only, you know, doesn=E2=80=99t crash the server. Best, David [1]: = https://github.com/postgres/postgres/blob/629520be5f9da9d0192c7f6c8796bfdd= b4746760/src/backend/utils/adt/jsonpath_exec.c#L2784