Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xNpqD-00000002ltS-1Kf6 for pgsql-bugs@arkaria.postgresql.org; Mon, 02 Nov 2026 11:04:41 +0000 Received: from localhost ([127.0.0.1]:42002 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xNpqB-00000003B7K-3BvX for pgsql-bugs@arkaria.postgresql.org; Mon, 02 Nov 2026 11:04:39 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]:42770) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xNQRH-0000000Ga5L-3jKe for pgsql-bugs@lists.postgresql.org; Sun, 01 Nov 2026 07:57:15 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xNQRE-00000001yTt-1lbJ for pgsql-bugs@lists.postgresql.org; Sun, 01 Nov 2026 07:57:14 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Message-ID:Date:Reply-To:Cc:From:To:Subject: Content-Transfer-Encoding:MIME-Version:Content-Type:Sender:Content-ID: Content-Description:In-Reply-To:References; bh=0aUw1U46WCIGjvYTZzaAq8DCBn1grmCU0TLA3ZineVM=; b=CEb2qSatsnybPsX+5uhAgbzUJM 05jZPUYG5NCA/I/Is/mRMftetrUeC/6lLWCSWi+ZmBFzQCB1hDorOz+ZkrIbFpKgyMSlfnGDOrmnz xfSVGGFTI7V5Z9S2CjCBkhE9Cn2nHW86lv3QFzv4ooAZoc4TKWpvyyqqegiijXf0FanhnETAsQJmL /1ci9NUpX65I2sbC7k3M8Xc+PM7du8QXVD/eNQAqizPcdi5tcwnGhf02w41f4kQBaPyTLubWpzrAu bvX4RAL/WmSGU0vMGfKTBEqkal09Es6iRuOdE1122woy0Pa2x0Pyh1u5S2UaUcWs5HbC5CAdpMIpe 4oXjo3aA==; Received: from wrigleys.postgresql.org ([2a02:16a8:dc51::60]) by mahout.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xNQRC-00000005jmo-23CA for pgsql-bugs@lists.postgresql.org; Sun, 01 Nov 2026 07:57:11 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xNQR4-00000009sql-3eJi for pgsql-bugs@lists.postgresql.org; Sun, 01 Nov 2026 07:57:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19822: DEFAULT boolean expressions in SQL/JSON functions are re-encoded as JSON booleans and always yield f To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: 303677365@qq.com Reply-To: 303677365@qq.com, pgsql-bugs@lists.postgresql.org Date: Sun, 01 Nov 2026 07:56:47 +0000 Message-ID: <19822-8bdbbe6b7cdc9048@postgresql.org> X-Auto-Response-Suppress: All Auto-Submitted: auto-generated List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk The following bug has been logged on the website: Bug reference: 19822 Logged by: chunling qin Email address: 303677365@qq.com PostgreSQL version: 18.6 Operating system: Linux (TencentOS 4.4, x86_64) Description: =20 Hello, In transformJsonBehavior() (src/backend/parser/parse_expr.c), the "coerce at runtime" branch replaces a boolean DEFAULT expression with a JSON boolean constant using the *clause behavior type* instead of the expression's actual value: char *val =3D btype =3D=3D JSON_BEHAVIOR_TRUE ? "true" : "false"; For a DEFAULT clause, btype is JSON_BEHAVIOR_DEFAULT, so the constant is always "false", regardless of whether the expression evaluates to true, false, or NULL. The replacement is also not guarded by !isnull, so a NULL boolean constant becomes false as well. The trigger is: the DEFAULT expression's type is boolean, and the RETURNING type's base type is not int4 (the ordinary bool->int4 cast path is unaffected). Reproducer (self-contained, on 18.6 and on 20devel): -- G1: DEFAULT true must yield 'true ' (padded to char(5)) SELECT JSON_VALUE(jsonb '"xyzzy!"', '$' RETURNING char(5) DEFAULT true ON ERROR); -- actual: false <- the boolean true was replaced by JSON false -- expected: 'true ' (SQL boolean cast to bpchar(5)) -- G4: same with a domain over bool CREATE DOMAIN dbool AS bool; SELECT JSON_VALUE(jsonb '"xyzzy!"', '$' RETURNING dbool DEFAULT true ON ERROR); -- actual: f -- expected: t -- H1: a NULL boolean constant is also replaced (no !isnull guard) SELECT JSON_VALUE(jsonb '"xyzzy!"', '$' RETURNING char(5) DEFAULT NULL::bool ON ERROR); -- actual: false -- expected: NULL -- Controls that show the normal paths are fine: SELECT JSON_VALUE(jsonb '"xyzzy!"', '$' RETURNING char(5) DEFAULT false ON ERROR); -- false (correct by accident) SELECT JSON_VALUE(jsonb '"xyzzy!"', '$' RETURNING int DEFAULT true ON ERROR); -- 1 (bool->int4 path, correct) Expected: the value of a DEFAULT expression should be determined by the expression itself (true -> "true", NULL -> NULL), not by the clause behavior type; JSON boolean encoding should only apply to an explicit FORMAT JSON. Actual: DEFAULT true/false/NULL all come back as JSON false whenever the runtime-coercion path is taken, silently and with no warning. A common use of DEFAULT true is a fallback flag (feature switch, approval marker); getting false there instead of true produces wrong business decisions with no error anywhere. Reproduced on: - PostgreSQL 18.6 (REL_18_STABLE, self-built, snapshot 2026-10-26) - PostgreSQL 20devel (snapshot of 2026-10-29) A draft fix location would be transformJsonBehavior() (src/backend/parser/parse_expr.c): use the actual constant value for boolean DEFAULT expressions (and guard the replacement with !isnull). Regards, Chunling Qin