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 1fnsAb-0007bN-AD for pgsql-general@arkaria.postgresql.org; Thu, 09 Aug 2018 21:08:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fnsAV-00077h-Md for pgsql-general@arkaria.postgresql.org; Thu, 09 Aug 2018 21:08:27 +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 1fnsAV-00077X-76 for pgsql-general@lists.postgresql.org; Thu, 09 Aug 2018 21:08:27 +0000 Received: from mail-pg1-x543.google.com ([2607:f8b0:4864:20::543]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fnsAS-0003g2-1M for pgsql-general@lists.postgresql.org; Thu, 09 Aug 2018 21:08:25 +0000 Received: by mail-pg1-x543.google.com with SMTP id h12-v6so3323158pgs.3 for ; Thu, 09 Aug 2018 14:08:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=illuminatedcomputing-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=pdAFkuCbGeFwM+CBxhFX16HAwkOPukAcFAT/rOC9PDY=; b=XUWjKbZEnx0u+nIr52+MnekBZwZh6HViojHtmLBywd2RY+KMEc/5kcTzeM4c0fCCgl PAAaBUy5gIpumZbSOXKXvMRYZ91ZFRn2MUNwzaKK1wTeZhHqg07VlQAJxXTkLy60uy/P S01Pex7Pe13V9oR/N1oTmbt3CAli+Vx4H7Seo8Mj39Owh91Hsz/js48v/XpFk26fxfr8 liZlxdXcbvNbJDeFKM6QIATLXBqsFQp4dqjYvw4CeZf7E6X0HD8pGfhsFmRBXQh7U56o 8iYp+iHNP54jLRT1NK0V5/UiosHhB0rieEoId/bUQ4959a6fekW9ZUoATrSjJoRjRdTr gpgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=pdAFkuCbGeFwM+CBxhFX16HAwkOPukAcFAT/rOC9PDY=; b=KtidViU8o/qB/6a2gxNYlbhX4bUv1dy+j6m0FdAYGs/TGBf6ltTAPKI79O1DobQEeo 8KgQ5TYYiQ6SweXA3M1DqcpCkOd/7o5mDloQeZz0h/goL1GPSbhkUenKOv8uJzpccfss oFtwNKYmDh/r3d8ODUqa03bkQOQrycYA1usmQwOsH71g8RZ5d3PRCJ20nNIKe9NB2mbY sKfmmpnCSudhag84QQtIdg/KWYl/Qvd85h7dox/0/4zXvaJfFvZE2ennK4hET97YUFDM teswsQcUCEvWFQfsCMwsPwlMuRPkLq0ge8bJXn6P0ktnIpM+GnqedgJhWUOeMMIqVBGL Y5RA== X-Gm-Message-State: AOUpUlFISdHO5oxKs/9vEutRrMsITEFm8SWaNCzJeBoYAdr2gNppY4GU 7Fxpav2FlbK9NHlqmmMbp2TVJebSQ4XdOA== X-Google-Smtp-Source: AA+uWPw6FhRS5jvCdYyrS0yMlPp/RWHEgUfY3T67kVWo2dVEclgy8F3sQab3I9SktUWu6agFuTpo4w== X-Received: by 2002:a63:c60:: with SMTP id 32-v6mr3646284pgm.155.1533848902412; Thu, 09 Aug 2018 14:08:22 -0700 (PDT) Received: from [192.168.2.201] (static-50-53-109-254.bvtn.or.frontiernet.net. [50.53.109.254]) by smtp.googlemail.com with ESMTPSA id u71-v6sm27877251pfk.174.2018.08.09.14.08.21 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 09 Aug 2018 14:08:21 -0700 (PDT) Subject: Re: When exactly is a TIMESTAMPTZ converted to the sessions time zone? To: pgsql-general@lists.postgresql.org References: <65821.1530651617@sss.pgh.pa.us> <20180809204944.GC14030@momjian.us> From: Paul Jungwirth Message-ID: <6ccea066-aadc-0289-e229-4c83edd4f8f8@illuminatedcomputing.com> Date: Thu, 9 Aug 2018 14:08:20 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: <20180809204944.GC14030@momjian.us> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 08/09/2018 01:49 PM, Bruce Momjian wrote: > On Tue, Jul 3, 2018 at 05:00:17PM -0400, Tom Lane wrote: >> Thomas Kellerer writes: >>> A recent discussion around timestamptz behaviour has lead me to question my own understanding on how a TIMESTAMPTZ is converted to the session's time zone. >>> I assumed this conversion happens *on the server* before the value is sent to the client. >> >> It's done in the datatype's output conversion function. >> >>> A co-worker of mine claims that this is purely a client side thing, and that the server will always send the "plain" UTC value that is stored in a timestamptz column. >> >> Your co-worker is certainly wrong so far as text output is concerned. >> If you retrieve data in binary format, though, it looks like you get >> the raw (un-rotated) timestamp value, so that any conversion would have >> to be done on the client side. > > Wow, I am kind of surprised by that. Do any other data types have this > behavior? This isn't related to binary-vs-string format, but I think it's often overlooked that timestamptz considers your timezone not just to stringify the value, but also to truncate it: db=> create table t (ts timestamp, tstz timestamptz); CREATE TABLE Time: 3.154 ms db=> set timezone='America/Los_Angeles'; SET Time: 0.303 ms db=> insert into t (ts, tstz) values ('2018-06-09 19:00:00', '2018-06-09 19:00:00'); INSERT 0 1 Time: 2.653 ms db=> select ts, date_trunc('day', ts), tstz, date_trunc('day', tstz) from t; ts | date_trunc | tstz | date_trunc ---------------------+---------------------+------------------------+------------------------ 2018-06-09 19:00:00 | 2018-06-09 00:00:00 | 2018-06-09 19:00:00-07 | 2018-06-09 00:00:00-07 (1 row) Time: 0.438 ms db=> set timezone='UTC'; SET Time: 0.227 ms db=> select ts, date_trunc('day', ts), tstz, date_trunc('day', tstz) from t; ts | date_trunc | tstz | date_trunc ---------------------+---------------------+------------------------+------------------------ 2018-06-09 19:00:00 | 2018-06-09 00:00:00 | 2018-06-10 02:00:00+00 | 2018-06-10 00:00:00+00 (1 row) Are there any other places where timestamptz consults your timezone? -- Paul ~{:-) pj@illuminatedcomputing.com