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 1vFb3L-00ESSW-F5 for pgsql-bugs@arkaria.postgresql.org; Sun, 02 Nov 2025 16:35:38 +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 1vFb3K-00CHgh-1y for pgsql-bugs@arkaria.postgresql.org; Sun, 02 Nov 2025 16:35:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1vFb3J-00CHgY-Pl for pgsql-bugs@lists.postgresql.org; Sun, 02 Nov 2025 16:35:36 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vFb3G-005b8f-1J for pgsql-bugs@lists.postgresql.org; Sun, 02 Nov 2025 16:35:36 +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 5A2GZQBN427382; Sun, 2 Nov 2025 11:35:26 -0500 From: Tom Lane To: dqetool@126.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #19101: Ceil on BIGINT could lost precision in decil function In-reply-to: <19101-8f44b0e27aa65d40@postgresql.org> References: <19101-8f44b0e27aa65d40@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Sun, 02 Nov 2025 15:16:09 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <427380.1762101326.1@sss.pgh.pa.us> Date: Sun, 02 Nov 2025 11:35:26 -0500 Message-ID: <427381.1762101326@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk PG Bug reporting form writes: > I try to store a large number in `BIGINT` and run `ceil(c1)` command. > However, the result lost some precision due to calling `decil` function. > ```sql > CREATE TABLE t1 (c1 BIGINT); > INSERT INTO t1 VALUES (4854233034440979799); > -- dceil > SELECT ceil(c1) FROM t1; -- {4.854233034440979e+18} > ``` This is not a bug. There are two versions of ceil() and you're invoking the wrong one. You'd need to explicitly cast the argument to numeric if you want ceil(numeric) to be used. In this context it's a bit unfortunate that the parser's type preference rules [1] prefer float8 to numeric. But we're pretty much stuck with that behavior because (a) the SQL standard says so [2], and (b) even if it didn't, we have a couple of decades of history to be backwards compatible with. regards, tom lane [1] https://www.postgresql.org/docs/current/typeconv.html [2] Well, what it really says is that expressions that mix exact and inexact numeric types produce inexact results. We interpret that as meaning that float8 is the preferred type in the numeric category, so it wins ambiguous cases.