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 1lNLOk-0003hA-GM for pgsql-hackers@arkaria.postgresql.org; Fri, 19 Mar 2021 20:07:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lNLOj-0000Ki-AZ for pgsql-hackers@arkaria.postgresql.org; Fri, 19 Mar 2021 20:07:05 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lNLOj-0000KZ-3x for pgsql-hackers@lists.postgresql.org; Fri, 19 Mar 2021 20:07:05 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lNLOh-0007ub-1z for pgsql-hackers@postgresql.org; Fri, 19 Mar 2021 20:07:04 +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 12JK6vN94158032; Fri, 19 Mar 2021 16:06:57 -0400 From: Tom Lane To: Peter Eisentraut cc: David Steele , Pavel Stehule , Thomas Munro , Petr Fedorov , pgsql-hackers Subject: Re: Since '2001-09-09 01:46:40'::timestamp microseconds are lost when extracting epoch In-reply-to: <901bcfb8-36c8-27b2-b148-042392992eeb@2ndquadrant.com> References: <1551357.1599435992@sss.pgh.pa.us> <097a4242-0748-98e3-06ab-ded1d366cd76@2ndquadrant.com> <1893468.1599658711@sss.pgh.pa.us> <38fc499a-ae52-efb6-aad6-82f812d6782d@2ndquadrant.com> <20200917055327.GU2873@paquier.xyz> <20200930071543.GL1996@paquier.xyz> <2fb99632-9302-6509-78ce-3aaed3fd7f40@2ndquadrant.com> <3ff4be02-1e2b-564f-9b53-3dc561da8218@pgmasters.net> <3013402.1615829712@sss.pgh.pa.us> <8fae8673-0d5b-12d9-7342-daed0d590594@2ndquadrant.com> <901bcfb8-36c8-27b2-b148-042392992eeb@2ndquadrant.com> Comments: In-reply-to Peter Eisentraut message dated "Fri, 19 Mar 2021 20:37:04 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <4158030.1616184417.1@sss.pgh.pa.us> Date: Fri, 19 Mar 2021 16:06:57 -0400 Message-ID: <4158031.1616184417@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Peter Eisentraut writes: > Well, I had an idea that I put to work. In most of these cases where we > need division, we divide an integer by a power of 10. That can be done > with numeric very quickly by just shifting the weight and scale around. > So I wrote a function that does that specifically (look for > int64_div_fast_to_numeric()). With that, the slow cases I mentioned now > have the same performance as the other cases that didn't have any > numeric division. You just get the overhead for constructing and > passing around a numeric instead of a double, which can't be avoided. Yeah, I was wondering if we could do something like that, but I hadn't got as far as figuring a way to deal with divisors not a multiple of NBASE. Looking at the proposed code, I wonder if it wouldn't be better to define the function as taking the base-10-log of the divisor, so that you'd have the number of digits to shift (and the dscale) immediately instead of needing repeated integer divisions to get that. Also, the risk of intermediate overflow here seems annoying: + if (unlikely(pg_mul_s64_overflow(val1, NBASE/x, &val1))) + elog(ERROR, "overflow"); Maybe that's unreachable for the ranges of inputs the current patch could create, but it seems like it makes the function distinctly less general-purpose than one would think from its comment. Maybe, if that overflows, we could handle the failure by making that adjustment after we've converted to numeric? > So here is an intermediate patch that does this. I haven't gotten rid > of all numeric_div_opt_error() calls yet, but if this seems acceptable, > I can work on the remaining ones. I guess the immediate question is how much of a performance gap there is now between the float and numeric implementations. regards, tom lane