Re: inconsistent jsonb 'null' conversions

From: Sehrope Sarkuni <sehrope(at)jackdb(dot)com>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: inconsistent jsonb 'null' conversions
Date: 2026-07-05 15:13:58
Message-ID: CAH7T-ap9HuP7RucnhADTTqfkyj0tJN4KADw+9fJV=UDFHT0VWA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sun, Jul 5, 2026 at 8:59 AM Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> wrote:
>
> I found inconsistency in casting the value "null" of jsonb type to scalar types.
>
> (2026-07-05 14:54:08) postgres=# select 'null'::jsonb::int is null;
> ┌──────────┐
> │ ?column? │
> ╞══════════╡
> │ t │
> └──────────┘
> (1 row)
>
> (2026-07-05 14:56:26) postgres=# select 'null'::jsonb::varchar is null;
> ┌──────────┐
> │ ?column? │
> ╞══════════╡
> │ f │
> └──────────┘
> (1 row)
>
> Is it expected behaviour?

I'm not sure if this is expected, but the cast from json to int not
giving an error for null is a change from v17. That cast previously
failed.

The json-to-int (as opposed to jsonb) fails for both v17 and v18. Also
interesting that the json-to-int provides the error location but
jsonb-to-int does not.

SELECT version()
version
--------------------------------------------------------------------------------------------------------------------
PostgreSQL 18.3 (Debian 18.3-1.pgdg13+1) on x86_64-pc-linux-gnu,
compiled by gcc (Debian 14.2.0-19) 14.2.0, 64-bit
(1 row)

SELECT 'null'::jsonb::int IS NULL
?column?
----------
t
(1 row)

SELECT 'null'::json::int IS NULL
ERROR: cannot cast type json to integer
LINE 1: SELECT 'null'::json::int IS NULL
^
SELECT 'null'::json::text IS NULL
?column?
----------
f
(1 row)

SELECT version()
version
---------------------------------------------------------------------------------------------------------------------
PostgreSQL 17.5 (Debian 17.5-1.pgdg120+1) on x86_64-pc-linux-gnu,
compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
(1 row)

SELECT 'null'::jsonb::int IS NULL
ERROR: cannot cast jsonb null to type integer

SELECT 'null'::json::int IS NULL
ERROR: cannot cast type json to integer
LINE 1: SELECT 'null'::json::int IS NULL
^
SELECT 'null'::json::text IS NULL
?column?
----------
f
(1 row)

Regards,
-- Sehrope Sarkuni
Founder & CEO | JackDB, Inc. | https://www.jackdb.com/

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2026-07-05 15:18:28 Re: Fixing MSVC's inability to detect elog(ERROR) does not return
Previous Message Dongpo Liu 2026-07-05 13:27:20 [PATCH] Remove unused include from analyze.c