Re: [PATCH] Handle SQL NULL results from casts in JSON generation

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Andreas Karlsson <andreas(at)proxel(dot)se>
Cc: Neil Chen <carpenter(dot)nail(dot)cz(at)gmail(dot)com>, Postgres hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: [PATCH] Handle SQL NULL results from casts in JSON generation
Date: 2026-12-03 05:59:06
Message-ID: CAKFQuwbByJrcVjvoJpiwbV4w9Vitk3HOiqz2_MrvnC3zVXY5xA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wednesday, December 2, 2026, Andreas Karlsson <andreas(at)proxel(dot)se> wrote:

> On 11/20/26 4:36 AM, Neil Chen wrote:
>
>>
>> SELECT ('unset'::json_cast_flag::json) IS NULL; -- t
>> SELECT json_build_array('unset'::json_cast_flag::json); -- [null]
>>
>> SELECT json_build_array('unset'::json_cast_flag); -- error
>>
>

>
> Does it actually make sense for a cast to return NULL? I personally would
> prefer the error message and if PostgreSQL had been designed from scratch
> that casts are not even allowed to return NULL. So now I am curious: what
> is your use case for a cast which returns NULL? If you want to return a
> JSON null the proper way to do that is the below, but I do not see any use
> case for returning an SQL NULL.
>
> CREATE FUNCTION json_cast_flag_to_json(json_cast_flag) RETURNS json
> LANGUAGE sql AS $$ SELECT 'null'::json $$;

Maybe, but then select ‘unset’::json_cast_flag should fail on its own
merits. Since we allow that to succeed, the other constructs accept sql
NULL, and the path by which we arrived at that NULL does not matter, this
is a bug; regardless of how contrived the example may be. The json
subsystem failed to take this into account and needs to make the changes
like those being made elsewhere:

b7eee09eab738e085d6b62d35788f542dd233877

And

6aecabcd5ed54ac055b4df031336e997c84f7450

Unless the patch proves intractable to produce (seems unlikely) it should
be applied to restore consistency.

David J.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message shveta malik 2026-12-03 05:59:28 Re: DOC: Minor updates for logical replication conflicts
Previous Message Amit Kapila 2026-12-03 05:55:04 Re: DOC: Minor updates for logical replication conflicts