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 1fRLIP-0005Ew-0Y for pgsql-general@arkaria.postgresql.org; Fri, 08 Jun 2018 17:35:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fRLIN-0003Yf-Cq for pgsql-general@arkaria.postgresql.org; Fri, 08 Jun 2018 17:35:27 +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 1fRLDx-0001jb-TG for pgsql-general@lists.postgresql.org; Fri, 08 Jun 2018 17:30:54 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fRLDs-0007UF-TD for pgsql-general@postgresql.org; Fri, 08 Jun 2018 17:30:52 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 11FCC21957; Fri, 8 Jun 2018 13:30:47 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Fri, 08 Jun 2018 13:30:47 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=MAmsPp+FOv/ayrd5vrwiLHm8zPZq5 vllVeOJR3E8gC8=; b=L0H9UsP4OPSLxfUJrSuWHLo0Y7+bKuDlixEOijeMiM5jp Wcjfw/NRe4GdsUQFTvqRp5SA+e2I7Qr7JWjoMiJ9A5vtd63GF3izWH9av5GhSbli Q3iR69trFv/tnWbxhubGc+ooIwr1R0AZ5S6el0NKLx5lQwFLV+jzbvBDqRGnvH+L 3pyfttN2NWsBG+2rHvNIKjMlE3YAQK6pW1DVWbMUNj7rZRf6aUv9Pg4q1bzeEej/ xmiTz+ADjWZG+RaXC2cgknQg8MkDYpbB0rJJOZZ/ebgKr5k4IuqFXaee8HmTGAEx 0cqapd9VhNp0mroJ6OK1jPVT4Bp+DVG4QiTm1cpmA== 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-sender:x-me-sender:x-sasl-enc; s=fm2; bh=MAmsPp +FOv/ayrd5vrwiLHm8zPZq5vllVeOJR3E8gC8=; b=aQWpxxUiE7uf2hR7tS5EUi /Eh0NM5J8N+g6SJdRm6HBdwUBSTL1oeeNsU5bJdRO7Hk726Su8PHGk7b1aFMj1X9 qk6/lQI52WjTJMCdj66WqNgnzBVLX1ue6ysibfh6MZAtPocLIg4f78OHZnCV3X9f wySOsYgj/ikF1vK0v+ArD4/HonBajFexz6BXa8p+CNHYu3yEj093I7fDhjBbFoCf tQzSfpjhCuuV4q1p652aJ+PbJv7lLM1qvL5PmFU9CB905VX7BmPtFFIMRDRNA1H6 aLDL8UYxWXFfNXvGpbA8eoy2GxL0KGjIzt7G8AqkdQhDHs7w8uFfUsFez1+/Hbmw == X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Sender: Received: from [192.168.1.3] (unknown [71.212.159.209]) by mail.messagingengine.com (Postfix) with ESMTPA id 36EE3E47A0; Fri, 8 Jun 2018 13:30:46 -0400 (EDT) Subject: Re: (2^63 - 1)::bigint => out of range? (because of the double precision) To: Alexey Dokuchaev , pgsql-general@postgresql.org References: <20180608172323.GA32312@regency.nsu.ru> From: Adrian Klaver Message-ID: Date: Fri, 8 Jun 2018 10:30:45 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: <20180608172323.GA32312@regency.nsu.ru> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 06/08/2018 10:23 AM, Alexey Dokuchaev wrote: > 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 Not always, bigints are just the default. All those cases where folks use the serial 'type' are getting an int sequence: create table serial_test2(id serial); \d serial_test2 Table "public.serial_test2" Column | Type | Collation | Nullable | Default --------+---------+-----------+----------+------------------------------------------ id | integer | | not null | nextval('serial_test2_id_seq'::regclass) \d+ serial_test2_id_seq Sequence "public.serial_test2_id_seq" Type | Start | Minimum | Maximum | Increment | Cycles? | Cache ---------+-------+---------+------------+-----------+---------+------- integer | 1 | 1 | 2147483647 | 1 | no | 1 Owned by: public.serial_test2.id > 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? Not sure, someone else will have to explain. > > ./danfe > > P.S. On a tangentally related note, why is "NO CYCLE" is the default > for sequences? My guess is because sequences are often used to provide numbers for a PRIMARY KEY and NO CYCLE is a heads up for key duplication before the PK code kicks in. > > [*] Per documentation, "The [SQL] standard's AS expression > is not supported." Another "why is it so?" question, btw. ;-) > Where in the docs are you seeing this? -- Adrian Klaver adrian.klaver@aklaver.com