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 1fRKnj-0003I0-Qp for pgsql-general@arkaria.postgresql.org; Fri, 08 Jun 2018 17:03:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fRKnh-0001xu-QG for pgsql-general@arkaria.postgresql.org; Fri, 08 Jun 2018 17:03:45 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fRKnh-0001xj-Id for pgsql-general@lists.postgresql.org; Fri, 08 Jun 2018 17:03:45 +0000 Received: from mx.nsu.ru ([84.237.50.39]) by magus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fRKnd-0006s5-P4 for pgsql-general@postgresql.org; Fri, 08 Jun 2018 17:03:44 +0000 Received: from [84.237.50.47] (helo=regency.nsu.ru) by mx.nsu.ru with esmtp (Exim 4.72) (envelope-from ) id 1fRKna-00066B-F7 for pgsql-general@postgresql.org; Sat, 09 Jun 2018 00:03:38 +0700 Received: from regency.nsu.ru (localhost [127.0.0.1]) by regency.nsu.ru (8.14.2/8.14.2) with ESMTP id w58HNSdq034060 for ; Fri, 8 Jun 2018 23:23:29 +0600 (NOVT) (envelope-from danfe@regency.nsu.ru) Received: (from danfe@localhost) by regency.nsu.ru (8.14.2/8.14.2/Submit) id w58HNNrM033972 for pgsql-general@postgresql.org; Sat, 9 Jun 2018 00:23:23 +0700 (+07) (envelope-from danfe) Date: Sat, 9 Jun 2018 00:23:23 +0700 From: Alexey Dokuchaev To: pgsql-general@postgresql.org Subject: (2^63 - 1)::bigint => out of range? (because of the double precision) Message-ID: <20180608172323.GA32312@regency.nsu.ru> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.4.2.1i X-KLMS-Rule-ID: 3 X-KLMS-Message-Action: skipped X-KLMS-AntiSpam-Status: not scanned, whitelist X-KLMS-AntiPhishing: not scanned, whitelist X-KLMS-AntiVirus: Kaspersky Security 8.0 for Linux Mail Server, version 8.0.1.705, not scanned, whitelist List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi there, I've decided to run some tests to see how my tables' ids would survive when their yielding sequences would start hitting their MAXVALUE's, by doing some "SELECT setval('foo_id_seq', ~maxbigint)". As I don't like to hardcode numbers (esp. huge numbers, because sequences are always[*] bigint's), I've tried to use (2^63 - 1)::bigint as "maxbigint", to no avail, in contrast to (2^31 - 1)::int (-> below is short mnemonic for "returns"): select (2^31 - 1)::int -> 2147483647 (correct) select (2^63 - 1)::bigint -> bigint out of range (???) select (9223372036854775807)::bigint -> 9223372036854775807 (correct) Apparently, this is because the type of 2^63 is double precision, which is inexact; if I explicitly cast any of 2 or 63 to ::numeric, it behaves as expected: select (2::numeric^63 - 1)::bigint -> 9223372036854775807 (ok) select (2^63::numeric - 1)::bigint -> 9223372036854775807 (ditto) What is the rationale for (int ^ int) to return double precision rather than numeric? I am missing something obvious here? ./danfe P.S. On a tangentally related note, why is "NO CYCLE" is the default for sequences? [*] Per documentation, "The [SQL] standard's AS expression is not supported." Another "why is it so?" question, btw. ;-)