Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eS1Tt-0004jJ-9B for pgsql-bugs@arkaria.postgresql.org; Thu, 21 Dec 2017 14:05:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eS1Ts-0000A5-TH for pgsql-bugs@arkaria.postgresql.org; Thu, 21 Dec 2017 14:05:52 +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.84_2) (envelope-from ) id 1eS1Ts-00009v-Nq for pgsql-bugs@lists.postgresql.org; Thu, 21 Dec 2017 14:05:52 +0000 Received: from mail-qt0-x241.google.com ([2607:f8b0:400d:c0d::241]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eS1Tq-0008HL-5r for pgsql-bugs@postgresql.org; Thu, 21 Dec 2017 14:05:51 +0000 Received: by mail-qt0-x241.google.com with SMTP id r39so32459098qtr.13 for ; Thu, 21 Dec 2017 06:05:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=UPF5dTSyBkVAJmQVhh8nvvmgeClaXDpyD6aTewuM7lk=; b=pN/ui+eoIGOTFLWwmBb8RdNGR0eKr3z4rRksjSdsbp6M1THCGRpkNlH5U6sFi7E0E5 grskeE0ESJ0udJbRL+L/52AQdCay0RhOt36aABQ8iAIF5hQPAP/LNildh/DXj7FUEeZY wFfu9LITZJ7TqK2MPBFPlsxZlmuDi1fuDbCV1qI2iiWlIVrfxoEL48uFJ92ClBW/8WqB dlaOKl7IOtXaTZorlSAwj3S0haTaC692aOQ58CuPbGeoq7Q9j3OK/2nNja9idtlvu73k 0h2EFJ7x7jwAjrOyUC3g4dT3O2DhL9OY9+J065oJUkVLE5/reSqckYKB0cF+2/lrYy/c ZRRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=UPF5dTSyBkVAJmQVhh8nvvmgeClaXDpyD6aTewuM7lk=; b=eaDwrAax1NJ29zgQXk8iHIhpp0VkNkO8UqY+xx6CZPW1FkKWwwYu9mhPBYCJnRYzZ2 m3U7NVcVGvpYfiXwUttdjxIQShG+vkx9p7e53xxa49DVqNzfYauW5/7AmU8ItFhc8q3V +TYvKASXd1UcTPvtQ0yCkoMGb4jna5vj/ioPEQlU+XNYPIU9XgfXpyllnJ7Ke5DUO5JO BY+5fVXx39H+gaGCV+4hMu9Tf9ufC6QdhBte+VaIWD+jeyGDB0kBEhfmNOkXDv/eI6a5 8VbzPOCdiefqiCP3KSPyrtkd05sWgxJ8dvVh0FO62RYofGlyhPaUIRAsK6sGrQsSVqpZ XVKQ== X-Gm-Message-State: AKGB3mLTdsB2aR++6wvdBOgDbEoByYRT4Kc+sCJIvKLpdyyDDi9Jjw4s skzS3hx97ccOxHCD4tnIkYxz538SqtlSXrH/2j4= X-Google-Smtp-Source: ACJfBovJ/RcETf8heh1fIe6Vctlnc8eKOVwKxuYwb37EcihB1gOHDZf6dIB4ffiWboX+4qY660+va8Vnb+QwrkO4354= X-Received: by 10.200.15.2 with SMTP id e2mr14170475qtk.69.1513865148628; Thu, 21 Dec 2017 06:05:48 -0800 (PST) MIME-Version: 1.0 Sender: gsstark@gmail.com Received: by 10.55.102.130 with HTTP; Thu, 21 Dec 2017 06:05:07 -0800 (PST) In-Reply-To: References: <20171220122032.25740.1219@wrigleys.postgresql.org> From: Greg Stark Date: Thu, 21 Dec 2017 14:05:07 +0000 X-Google-Sender-Auth: _8schfWwdklttAri7R5m8_gYURk Message-ID: Subject: Re: BUG #14986: -2147483648 is minimum value of integer but -2147483648::integer fails (out of range). To: Magnus Hagander Cc: binoternary@gmail.com, pgsql-bugs Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 20 December 2017 at 12:25, Magnus Hagander wrote: > In the first query, you are casting 2147483648 to integer, and then > applying the minus. So it overflows the positive integer. You need () around > it: > > postgres=# select (-2147483648)::integer; > int4 > ------------- > -2147483648 > (1 row) Note that -2147483648 is already an integer constant so the ::integer doesn't actually do anything here. It gets removed during query preparation (parse analysis? Not sure). It doesn't appear in the final plan at all. But even if you do arrange for the cast to be called (as it is in the original query) it happens during constant folding preparing the plan (again parse analysis?): =# explain select '-2147483648'::bigint::integer; LOG: 00000: plan: DETAIL: {PLANNEDSTMT ... :targetlist ( {TARGETENTRY :expr {CONST :consttype 23 You can see this by running EXPLAIN on the original query. It overflows even though the query is never run: =# explain select -2147483648::integer; ERROR: 22003: integer out of range LOCATION: int84, int8.c:1298 Time: 0.393 ms The LOCATION line even gives a hint what's going on. 2147483648 was read as an int8 constant and the - operator returned an int8 and then the cast (being run during explain as part of preparing the plan) overflowed. I wonder why the "out of range" error doesn't print the actual value it's trying to cast. That would help the user here... -- greg