Re: BUG #19033: Inconsistency between prepared statement and normal statement when cast bit to integer

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: "798604270(at)qq(dot)com" <798604270(at)qq(dot)com>, "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #19033: Inconsistency between prepared statement and normal statement when cast bit to integer
Date: 2025-08-27 14:38:11
Message-ID: CAKFQuwaBAgHuJHVrf7YvDBTmMbJATn8=AeJcGUuML77o5ma0HQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Wednesday, August 27, 2025, PG Bug reporting form <noreply(at)postgresql(dot)org>
wrote:

> The following bug has been logged on the website:
>
> Bug reference: 19033
> Logged by: Chi Zhang
> Email address: 798604270(at)qq(dot)com
> PostgreSQL version: 18beta3
> Operating system: ubuntu 24.04 with docker
> Description:
>
> Hi,
>
> In the normal statement, casting a bit value to INT is allowed, but in a
> prepared statement, it will trigger an error.
>
> In the following test case, the first query will return 15, but the EXECUTE
> statement will trigger an error: parameter $1 of type bit cannot be coerced
> to the expected type integer
>
> ```
> SELECT (B'1111')::INT;
> int4
> ------
> 15
> (1 row)
>
> PREPARE prepare_query (unknown) AS SELECT $1::INT;
> EXECUTE prepare_query(B'1111');
> ERROR: parameter $1 of type bit cannot be coerced to the expected type
> integer
> ```
>
> If I modify the unknown type to bit, then the query can execute
> successfully
> and return 15.
> ```
> PREPARE prepare_query (bit) AS SELECT $1::INT;
> EXECUTE prepare_query(B'1111');
> int4
> ------
> 15
> (1 row)
> ```
>

This doesn’t feel like a bug, using “unknown” is likely to cause typing
issue, but I’m not sure I can explains exactly why.

> I also find if I first cast B'1111' to bit and then cast it to integer, it
> will become 1. But the correct result should be 15.
>
> ```
> SELECT ((B'1111')::bit)::INT;
> int4
> ------
> 1
> (1 row)
> ```
>

The documentation explicitly contradicts your claim here and explain why
this is correct.

David J.

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2025-08-27 14:54:55 Re: BUG #19033: Inconsistency between prepared statement and normal statement when cast bit to integer
Previous Message Tom Lane 2025-08-27 14:32:26 Re: BUG #19031: pg_trgm infinite loop on certain cases