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.96) (envelope-from ) id 1wKbfW-001B4C-0Y for pgsql-bugs@arkaria.postgresql.org; Wed, 06 May 2026 12:48:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wKbfV-00GxLB-0O for pgsql-bugs@arkaria.postgresql.org; Wed, 06 May 2026 12:48:01 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wKYGc-00G0hU-1V for pgsql-bugs@lists.postgresql.org; Wed, 06 May 2026 09:10:06 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wKYGa-00000000SjY-08Wq for pgsql-bugs@lists.postgresql.org; Wed, 06 May 2026 09:10:05 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Message-ID:Date:Reply-To:Cc:From:To:Subject: Content-Transfer-Encoding:MIME-Version:Content-Type:Sender:Content-ID: Content-Description:In-Reply-To:References; bh=JmCFSl37SgFqhSmPYnUXQgaskHklQEo37fxnJrfiiiY=; b=k3/i9yNJew31SWA+il6C2QPMN6 K9Rt6X+Uo1pYD/c+NUDs0TEH/+p5XmD/gM/InPXKyIp8/bUSlwwlSwAnWviM8uhPWT5ATr+4lyB2n mIu194P+LhbZORcUosN+X7o+G+E5OIvANTpGrFqrCGPWefjoB5CaZYzNMc6vfnozXaZyFLCkShU3Q XiVbq+WT/56M8arhKhpgzcBjxLZRWwCx0tinulnaBrpuFla5vkE8SXQ26SZL8caH3gETdOL99YTrX 0I7rTAK6XPrEjp126MpcS90s5+r3KODcZlxe/FXMfyoMS1BqftdX6UVPruDwU8+g8TeWoAOrmf5db +aa/EVEQ==; Received: from wrigleys.postgresql.org ([2a02:16a8:dc51::60]) by mahout.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wKYGZ-001JBS-1a for pgsql-bugs@lists.postgresql.org; Wed, 06 May 2026 09:10:04 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wKYGY-004XPv-2C for pgsql-bugs@lists.postgresql.org; Wed, 06 May 2026 09:10:02 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19472: CAST(-32768::SMALLINT AS REAL) fails with "SMALLINT out of range" but -32768 is valid SMALLINT value To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: vectorplanck@gmail.com Reply-To: vectorplanck@gmail.com, pgsql-bugs@lists.postgresql.org Date: Wed, 06 May 2026 09:09:48 +0000 Message-ID: <19472-6b130bcff370911d@postgresql.org> X-Auto-Response-Suppress: All Auto-Submitted: auto-generated List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk The following bug has been logged on the website: Bug reference: 19472 Logged by: Pisces Mar. Email address: vectorplanck@gmail.com PostgreSQL version: 17.6 Operating system: x86_64-windows Description: =20 Issue Description: When casting -32768 to SMALLINT and then to REAL using the :: operator, PostgreSQL throws an error "smallint out of range", even though -32768 is a valid value within the SMALLINT range (-32768 to +32767). Steps to Reproduce: 1. Execute: SELECT CAST(-32768 :: SMALLINT AS REAL); Result: ERROR: SMALLINT out of range 2. Execute: SELECT CAST((-32768) :: SMALLINT AS REAL); Result: Successfully returns -32768 3. Execute: SELECT CAST(CAST(-32768 AS SMALLINT) AS REAL); Result: Successfully returns -32768 Root Cause: The expression -32768 :: SMALLINT is being parsed as -(32768 :: SMALLINT) instead of (-32768) :: SMALLINT. Since 32768 exceeds the maximum positive value for SMALLINT (32767), it causes an out-of-range error. Expected Behavior: -32768 should be recognized as a valid SMALLINT value (the minimum value for smallint is -32768), and the cast to REAL should succeed. Workaround: Use parentheses: SELECT CAST((-32768) :: SMALLINT AS REAL); Or use standard CAST syntax: SELECT CAST(-32768 AS SMALLINT); Environment: PostgreSQL Version: 17.6 Operating System: x86_64-windows