Re: BUG #19625: SQL/JSON boolean DEFAULT expression silently replaced with 'false'

From: Joao Rito <joaovictor(dot)rito(at)gmail(dot)com>
To: Amit Langote <amitlangote09(at)gmail(dot)com>
Cc: Man Zeng <zengman(at)halodbtech(dot)com>, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19625: SQL/JSON boolean DEFAULT expression silently replaced with 'false'
Date: 2026-11-15 03:54:32
Message-ID: 20261115035432.23742-1-joaovictor.rito@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi Amit, Man,

I tested v2 on current master (81b2179b492, built with cassert and tap tests) and it applies cleanly. On master, a boolean DEFAULT ON ERROR or ON EMPTY returned 'false' in every case I ran where the result was not already boolean: DEFAULT true, NULL::bool (the result was not NULL), (1 = 1) and (random() < 2, ON ERROR only), in JSON_VALUE, JSON_QUERY and JSON_TABLE. I used RETURNING text, char(5), varchar(10), json and jsonb. RETURNING bool was already correct on master. With v2 all of them return the expected value, and a view shows DEFAULT to_jsonb(true) and gives the same result. DEFAULT 'abc', 42, a numeric, a date and a jsonb object, and JSON_EXISTS with TRUE/FALSE/UNKNOWN ON ERROR, give the same results as on master. The new test in sqljson_queryfuncs fails on master without the C change (it shows false | false | f where true | false | t is expected), and the regress suite passes with the patch (243 tests).

I could also reproduce what Man reported after v2. On master, CREATE INDEX on JSON_VALUE(j, '$.x' DEFAULT true ON EMPTY) and a STORED generated column with the same expression both work. With v2 they fail with "functions in index expression must be marked IMMUTABLE" and "generation expression is not immutable", because to_jsonb(anyelement) is STABLE. With a non-boolean DEFAULT, the index (text, and RETURNING int) and the generated column (text) still work with v2.

What I did not test: the isolation and tap suites, pgindent, the docs, the back branches (v17 onward), other platforms (I ran one Linux build), and a fix for the immutability issue.

Small suggestion: since the index and generated column cases work today, maybe the fix could avoid a STABLE function for a constant boolean DEFAULT (for example by building the jsonb Const at parse time, as the TRUE and FALSE behaviors do), and the test could cover an index or generated column. I do not have a tested patch for that. I have no objection to the rest of the approach.

Regards,
Joao Rito

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message shihao zhong 2026-11-15 03:59:05 Re: BUG #19703: information_schema.usage_privileges omits a sequence owner's implicit USAGE privilege
Previous Message Joao Rito 2026-11-15 03:53:59 Re: BUG #19703: information_schema.usage_privileges omits a sequence owner's implicit USAGE privilege