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 1lov8s-0006IX-2X for pgsql-general@arkaria.postgresql.org; Thu, 03 Jun 2021 21:44:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lov8r-0006Ho-0u for pgsql-general@arkaria.postgresql.org; Thu, 03 Jun 2021 21:44:41 +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 1lov8q-0006Hf-OD for pgsql-general@lists.postgresql.org; Thu, 03 Jun 2021 21:44:40 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lov8o-0007qa-FQ for pgsql-general@lists.postgresql.org; Thu, 03 Jun 2021 21:44:39 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 89C255C020E; Thu, 3 Jun 2021 17:44:37 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Thu, 03 Jun 2021 17:44:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=d +iNTsCKkXXsjtGg9hr9I2EVYqte1z9H9si03k5Avww=; b=iAN5/G59YJl9WycdY 9DM85cFUsgOTJPL8HGvWJRcMU9GFr/TqScc10dCvEK/54K3ajaLeekhR3TYelK+d VVox3Ujmcj+EajjGDcgdmwfgsvjqMQlKTuanp1iicKalWkDQC++tRvzPADpR5PA5 +Sv/gFu60xr97WgCdxmaqI5Rkt60Qvyhgru/KnMVf7scjfqi6LLKyCu1I3qLHIte TkCRpMkrYLx0IaqDWgMDrrYGpOQLd06mPzmK1xMllF9vjFl3NVyC2O2KyZ+iZh3T F2GUMMDR/2pgZVVvDEwyRGYInNKpX+21EwD9vJiFKIuw+lWIEpRoC67eJcvxSQZW ZlcPQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=d+iNTsCKkXXsjtGg9hr9I2EVYqte1z9H9si03k5Av ww=; b=B0ulQDrEjQg/bBkfzbjp8ecb3mJlTRMvEoYUvU25Uiqh340t5hOIfXeYf 237UHV2qH4G1KSkVVt2z7Flx77VW21aEELmoQE1GTAY8KRxKHpPEFcX+JGI9tLTu uJezKR1ak9vFm3g4NB+rxIR7Ch5qMxAIgAvwZ5HtPUVf07GlzDUUwC5tPKcs5pY6 a19oYevxz1Nvi5HBUPQ56l/mCpj5z/pg7MjCVpsfcyBOx+xYR7S7lZnAuG7hSRJc KgOwel8+8em7az8Ez7HVJGYA0lNoLLejUIm3zWoBWFJmDW4QeHknh+C/rGqcG+Ku Usb8t8X9MBga5YFN9666SpywnXA9A== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrvdelledgudeihecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughr ihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtg homheqnecuggftrfgrthhtvghrnhepuddufeejteevffehgfduhedthfdvteekieeuheef veevudetkeelueehgeduhefhnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrgh enucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughr ihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 3 Jun 2021 17:44:36 -0400 (EDT) Subject: =?UTF-8?Q?Re=3a_arbitrary_=22interval=22_expression_OK_with_=22at_t?= =?UTF-8?Q?ime_zone=22_but_not_with_=22set_time_zone=22_=e2=80=94_Why=3f?= To: Bryn Llewellyn , pgsql-general list References: <97334D92-03D8-4066-A807-DEAC9EBF87F5@yugabyte.com> From: Adrian Klaver Message-ID: <8f2d19d1-ec77-6625-9f03-daaded41f22d@aklaver.com> Date: Thu, 3 Jun 2021 14:44:36 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 MIME-Version: 1.0 In-Reply-To: <97334D92-03D8-4066-A807-DEAC9EBF87F5@yugabyte.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 6/3/21 2:40 PM, Bryn Llewellyn wrote: > The "at time zone" clause that can decorate a timetsamp[tz] value seems to allow an argument that’s an arbitrary expression that yields a value whose data type is "interval". Here’s a contrived exotic example: > > select '2021-05-21 12:00:00 UTC'::timestamptz at time zone > ('2015-05-21 17:00:00'::timestamp - '2015-05-21 17:00:00'::timestamp) + > make_interval(mins=>-30) - > '30 minutes'::interval*2; > > It runs without error and gives the answer that I'd expect. > > You can also supply a value whose data type is "interval" when you set the session's timezone. But you must use the special "set time zone" syntax rather than the general "set timezone =" (or "to") syntax. This works: > > set time zone interval '-7 hours'; > > Moreover, the minus sign has the meaning that ordinary mortals (as opposed to native POSIX speakers) expect. That's nice. But even this tiny spelling change: > > set time zone '-7 hours'::interval; > > brings a "42601: syntax error". > > The asymmetry harms usability. And it means that careful reference doc ends up voluminous, tortuous and off-putting. Nobody likes to have to study and remember whimsical rules that seem to have no logical justification. > Not sure how the below is any of the above: https://www.postgresql.org/docs/13/sql-set.html "TIME ZONE SET TIME ZONE value is an alias for SET timezone TO value. The syntax SET TIME ZONE allows special syntax for the time zone specification. Here are examples of valid values: 'PST8PDT' The time zone for Berkeley, California. 'Europe/Rome' The time zone for Italy. -7 The time zone 7 hours west from UTC (equivalent to PDT). Positive values are east from UTC. INTERVAL '-08:00' HOUR TO MINUTE The time zone 8 hours west from UTC (equivalent to PST). LOCAL DEFAULT Set the time zone to your local time zone (that is, the server's default value of timezone). Timezone settings given as numbers or intervals are internally translated to POSIX timezone syntax. For example, after SET TIME ZONE -7, SHOW TIME ZONE would report <-07>+07. See Section 8.5.3 for more information about time zones. " > Am I failing to see that there's a logical parsing paradox that means that arbitrary "interval" expressions are acceptable as the argument of "at time zone" but not as the argument of "set time zone"? > > Meanwhile, I'm writing a "set_timezone() procedure" with a "text" overload (that will check against a list of approved values) and an "interval" overload that will check that the value is in a sensible range* and generate the acceptable syntax to execute "set time zone" dynamically. > ____________________________________________________________ > > * sensible range for "interval" values from this: > > select '~names' as "view", max(utc_offset), min(utc_offset) from pg_timezone_names > union all > select '~abbrevs' as "view", max(utc_offset), min(utc_offset) from pg_timezone_abbrevs > order by 1; > > > -- Adrian Klaver adrian.klaver@aklaver.com