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

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: "vectorplanck(at)gmail(dot)com" <vectorplanck(at)gmail(dot)com>, "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #19472: CAST(-32768::SMALLINT AS REAL) fails with "SMALLINT out of range" but -32768 is valid SMALLINT value
Date: 2026-05-06 12:53:58
Message-ID: CAKFQuwY95Rsw=P4a+=NJijCTY=+AmV2W7OEF2T14BiQo4S9i4w@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Wednesday, May 6, 2026, PG Bug reporting form <noreply(at)postgresql(dot)org>
wrote:

> The following bug has been logged on the website:
>
> Bug reference: 19472
> Logged by: Pisces Mar.
> Email address: vectorplanck(at)gmail(dot)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.

In response to

Browse pgsql-bugs by date

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