Re: BUG #19472: CAST(-32768::SMALLINT AS REAL) fails with "SMALLINT out of range" but -32768 is valid SMALLINT value - Mailing list pgsql-bugs

From David G. Johnston
Subject Re: BUG #19472: CAST(-32768::SMALLINT AS REAL) fails with "SMALLINT out of range" but -32768 is valid SMALLINT value
Date
Msg-id CAKFQuwY95Rsw=P4a+=NJijCTY=+AmV2W7OEF2T14BiQo4S9i4w@mail.gmail.com
Whole thread
In response to BUG #19472: CAST(-32768::SMALLINT AS REAL) fails with "SMALLINT out of range" but -32768 is valid SMALLINT value  (PG Bug reporting form <noreply@postgresql.org>)
List pgsql-bugs
On Wednesday, May 6, 2026, PG Bug reporting form <noreply@postgresql.org> wrote:
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:       

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).

Workaround:
Use parentheses: SELECT CAST((-32768) :: SMALLINT AS REAL);
Or use standard CAST syntax: SELECT CAST(-32768 AS SMALLINT);

These aren’t really workarounds; this is just how you need to write the expression given our long-established and not going to change operator precedence and literal syntax parsing rules.

David J.

pgsql-bugs by date:

Previous
From: Laurenz Albe
Date:
Subject: Re: BUG #19472: CAST(-32768::SMALLINT AS REAL) fails with "SMALLINT out of range" but -32768 is valid SMALLINT value
Next
From: Xuneng Zhou
Date:
Subject: Re: BUG #19439: pg_stat_xact_user_tables stat not currect during the transaction