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 1fRLWz-00066D-Fv for pgsql-general@arkaria.postgresql.org; Fri, 08 Jun 2018 17:50:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fRLWx-0005CR-Tb for pgsql-general@arkaria.postgresql.org; Fri, 08 Jun 2018 17:50:31 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fRLWO-0001J1-M6 for pgsql-general@lists.postgresql.org; Fri, 08 Jun 2018 17:49:56 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fRLWL-0000cJ-Bv for pgsql-general@postgresql.org; Fri, 08 Jun 2018 17:49:54 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w58HnTBk030539; Fri, 8 Jun 2018 13:49:29 -0400 From: Tom Lane To: Alexey Dokuchaev cc: pgsql-general@postgresql.org Subject: Re: (2^63 - 1)::bigint => out of range? (because of the double precision) In-reply-to: <20180608172323.GA32312@regency.nsu.ru> References: <20180608172323.GA32312@regency.nsu.ru> Comments: In-reply-to Alexey Dokuchaev message dated "Sat, 09 Jun 2018 00:23:23 +0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <30537.1528480169.1@sss.pgh.pa.us> Date: Fri, 08 Jun 2018 13:49:29 -0400 Message-ID: <30538.1528480169@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Alexey Dokuchaev writes: > What is the rationale for (int ^ int) to return double precision rather > than numeric? I am missing something obvious here? There are two ^ operators, one taking float8 and one taking numeric. Since float8 is the preferred datatype in the numeric category (i.e. the top of the implicit-casting hierarchy), the float8 operator will be chosen unless one of the earlier disambiguation rules applies: https://www.postgresql.org/docs/current/static/typeconv-oper.html In this case, you need at least one input to be numeric, so that rule 3c fires before rule 3d can. You might argue that numeric should be the preferred type, but that falls foul of the SQL standard, which is quite clear that only numeric -> float8 can be an implicit cast, not the other direction. (They word it in terms of casts between exact and approximate numeric types, but that's the outcome.) The type resolution heuristics break down pretty badly if the preferred type in a category doesn't have implicit casts from every other type in the category. BTW, you could also just write 2.0^63 to get numeric. > P.S. On a tangentally related note, why is "NO CYCLE" is the default > for sequences? (a) the SQL standard says so, (b) most people tend to expect serial columns to not repeat values. > [*] Per documentation, "The [SQL] standard's AS expression > is not supported." Another "why is it so?" question, btw. ;-) We didn't get around to implementing that till v10. regards, tom lane