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 1sGUuE-00DmGg-K0 for pgsql-hackers@arkaria.postgresql.org; Mon, 10 Jun 2024 02:37:11 +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 1sGUuD-007TZ3-5x for pgsql-hackers@arkaria.postgresql.org; Mon, 10 Jun 2024 02:37:10 +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 1sGUuC-007TYu-ST for pgsql-hackers@lists.postgresql.org; Mon, 10 Jun 2024 02:37:09 +0000 Received: from mail-io1-xd2a.google.com ([2607:f8b0:4864:20::d2a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sGUuB-000Yds-3y for pgsql-hackers@lists.postgresql.org; Mon, 10 Jun 2024 02:37:08 +0000 Received: by mail-io1-xd2a.google.com with SMTP id ca18e2360f4ac-7eb671a835fso139851939f.2 for ; Sun, 09 Jun 2024 19:37:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717987026; x=1718591826; darn=lists.postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=Znz0FYML4AyL92Dom6nhyUZBFzqbu+dPGUUFw4/e+3I=; b=eef9p6k5vA01TxPewFobf3f8LnOXAIzFLQ0rFg9LaT5cFGilgez1HydZPUaK99HyYp QyApesVOzZqmAXJKcdsXAkq//brQkwWVyt/AjzlWfXsw4NILIc+fED3IfwsLeM5EOhBH 7VhRhrOd/V5B8E5dsroyYdV0MwMYW1yYSZEyRe1dHChZjpMwAQpC/L9L2w0LU/E5n9wa W3G2MkH3VnBEWaHer0otqaarMXOWQSvAiIqiFtHZsPx0FPskbKVA+1HhsHXWG5RwNtrU 9BXFNHtyEcYMe48Ql9PDVc0gTvkyPCpCmSLF0XoKaq5HVfSjN8/9xY3E/qZJGABgAkmg NVyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717987026; x=1718591826; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Znz0FYML4AyL92Dom6nhyUZBFzqbu+dPGUUFw4/e+3I=; b=npX5lv90RbRJf9WtUo5LAiuw8pWK873ZGArZ10ZijG2dAsEH5Kjs9fT1Tt5uXuRbI2 imAQdmKcBjRFRqum+R/nYrapQz1SCXdq07gynzDN8AxMHWTZFh96nUvrNw9+wj8iDstx GPT7+lIeYdKPG5+rMES8u8W0CrZXQXhKQ6wTgK8jCB6x4DnAKb3To6RPWKwVTdwrng5S Yg/Ii6oUnsF8ODQpljHndJF7cgIbzl6IAuswbcrR/6VYOCGRC7uS0esNeJUvLbGPqjAq 0mLyo6Xrl/RzmkstN2XQL7z425/sbF7erPgOboG5oxj+drFZygjqI6aitbJsvvESw7y3 R58Q== X-Forwarded-Encrypted: i=1; AJvYcCUQiaJ+LiU/mX8fMiAwc/dWB4DNB9ojmcCthwAxPeaBLBnBWRNGC0/GlTKguQR/5b6gbRq2h7r51af0vzmAOUo/nVzHz2gnMsyrwCdyfwtgBASb X-Gm-Message-State: AOJu0YyGDrZ5XkkU66C+1l8WqljhrnNbcm+aOL/lRwiJyDH+NJCgICfV hNb1qWQQ/OtYmgWFANSygCqgf8MDG5/LhLw/DSzbYyZBnvl+QSIp X-Google-Smtp-Source: AGHT+IEEpcRBnHBABIxFnZVeTAC2lz2r+Nz9pBS75loUn4718BXRQZTuhSnq9txi3aNFNrBbhtV94A== X-Received: by 2002:a05:6602:2dd4:b0:7ea:cd68:d235 with SMTP id ca18e2360f4ac-7eb57262a8cmr916687639f.18.1717987026187; Sun, 09 Jun 2024 19:37:06 -0700 (PDT) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4b7a22123a8sm2302526173.26.2024.06.09.19.37.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Jun 2024 19:37:05 -0700 (PDT) Date: Sun, 9 Jun 2024 21:37:03 -0500 From: Nathan Bossart To: Tom Lane Cc: Joseph Koshakow , PostgreSQL Hackers , Andres Freund Subject: Re: Remove dependence on integer wrapping Message-ID: References: <618777.1717984674@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <618777.1717984674@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sun, Jun 09, 2024 at 09:57:54PM -0400, Tom Lane wrote: > Nathan Bossart writes: >> On Sun, Jun 09, 2024 at 04:59:22PM -0400, Joseph Koshakow wrote: >>> The following comment was in the code for parsing timestamps: >>> /* check for just-barely overflow (okay except time-of-day wraps) */ >>> /* caution: we want to allow 1999-12-31 24:00:00 */ >>> >>> I wasn't able to fully understand it even after staring at it for >>> a while. Is the comment suggesting that it is ok for the months field, >>> for example, to wrap around? That doesn't sound right to me I tested >>> the supplied timestamp, 1999-12-31 24:00:00, and it behaves the same >>> before and after the patch. > >> I haven't stared at this for a while like you, but I am likewise confused >> at first glance. This dates back to commit 84df54b, and it looks like this >> comment was present in the first version of the patch in the thread [0]. I >> CTRL+F'd for any discussion about this but couldn't immediately find >> anything. > > I believe this is a copy-and-paste from 841b4a2d5, which added this: > > + *result = (date * INT64CONST(86400000000)) + time; > + /* check for major overflow */ > + if ((*result - time) / INT64CONST(86400000000) != date) > + return -1; > + /* check for just-barely overflow (okay except time-of-day wraps) */ > + if ((*result < 0) ? (date >= 0) : (date < 0)) > + return -1; > > I think you could replace the whole thing by using overflow-aware > multiplication and addition primitives in the result calculation. > Lines 2-4 basically check for mult overflow and 5-7 for addition > overflow. Ah, I see. Joe's patch does that in one place. It's probably worth doing that in the other places this "just-barefly overflow" comment appears IMHO. I was still confused by the comment about 1999, but I tracked it down to commit 542eeba [0]. IIUC it literally means that we need special handling for that date because POSTGRES_EPOCH_JDATE is 2000-01-01. [0] https://postgr.es/m/CABUevEx5zUO%3DKRUg06a9qnQ_e9EvTKscL6HxAM_L3xj71R7AQw%40mail.gmail.com -- nathan