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 1xNpuh-00000002lw8-24Jj for pgsql-bugs@arkaria.postgresql.org; Mon, 02 Nov 2026 11:09:20 +0000 Received: from localhost ([127.0.0.1]:55870 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xNpuh-00000003HjH-005D for pgsql-bugs@arkaria.postgresql.org; Mon, 02 Nov 2026 11:09:19 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]:34916) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xNWkA-0000000Gt71-0z9o for pgsql-bugs@lists.postgresql.org; Sun, 01 Nov 2026 14:41:10 +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 1xNWk7-000000020wc-2R91 for pgsql-bugs@lists.postgresql.org; Sun, 01 Nov 2026 14:41:09 +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=MHvJ/0T+ymoBjoB2Rdr1UHrCvLMqcS61e7akUnkL8mo=; b=6N6tvQMvUIYJ4jjRmYbFYdPipu xDEMZfd0IFIsKRcQpr56/xiwEIBawI3FDqHvTLXhOSs734MONncTryLWFaXjelHSn3SHu7T07NZJ/ Srz+vrWwUZT3JnnZAYq4zrOB84Rlwo1HfQutzK2uSYEL2DiNh/yCxglf5I/GlRZCOAzmoISopbZuI wrZvQg9GIkBBSqqAyu8eeBz9VMDebwWJskx94mutLgwoV2q2V5E2sbZPmBduQ7Xo5mEeRlC6tJ5e3 BI3h1S2q0J6tvdemYN9n5z5M1khg6qv9avJVc1IOJK27ty4j4d2zKTs/egFKyhRGnQNWR/siwSmFH 9HqkfUAQ==; 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 1xNWk5-00000005rwk-3uam for pgsql-bugs@lists.postgresql.org; Sun, 01 Nov 2026 14:41:07 +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 1xNWk1-0000000AWqU-49tB for pgsql-bugs@lists.postgresql.org; Sun, 01 Nov 2026 14:41:02 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19825: JSON_VALUE(... RETURNING ) loses the re-quoting of scalar strings and silent 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 14:40:41 +0000 Message-ID: <19825-809e30f37ee98bf4@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: 19825 Logged by: chunling qin Email address: 303677365@qq.com PostgreSQL version: 18.6 Operating system: Linux (TencentOS 4.4, x86_64) Description: =20 Hello, For JSON_VALUE with RETURNING json / jsonb, a scalar *string* result is re-quoted on output (locked in by the regression suite: SELECT JSON_VALUE(jsonb '"aaa"', '$' RETURNING jsonb) -> "aaa"). When the RETURNING type is a domain over json / jsonb -- semantically equivalent, no constraint -- that re-quoting is lost and the value silently becomes NULL: CREATE DOMAIN djb AS jsonb; CREATE DOMAIN djs AS json; SELECT JSON_VALUE(jsonb '"x"', '$' RETURNING jsonb); -- "x" (correct) SELECT JSON_VALUE(jsonb '"x"', '$' RETURNING djb); -- NULL (wrong, want "x") SELECT JSON_VALUE(jsonb '"x"', '$' RETURNING json); -- "x" (correct) SELECT JSON_VALUE(jsonb '"x"', '$' RETURNING djs); -- NULL (wrong, want "x") SELECT JSON_VALUE(jsonb '"x"', '$' RETURNING djb ERROR ON ERROR); -- ERROR: invalid input syntax for type json -- DETAIL: Token "x" is invalid. Both coercion routes fail: adding a trivially-true CHECK switches the domain to the use_json_coercion path (json_populate_type()), which still returns NULL / the same error, because JSON_VALUE always sets omit_quotes =3D true (src/backend/parser/parse_expr.c:4694), so json_populate_type() unquotes first and then tries to parse "x" as jsonb. Controls that isolate the defect to string values over domains: JSON_VALUE(jsonb '1', '$' RETURNING jsonb) =3D 1 and RETURNING djb = =3D 1 JSON_QUERY(... RETURNING jsonb) and RETURNING djb both -> "x" non-scalar inputs fail identically on both sides Expected: a domain over json / jsonb should behave exactly like its base type here (the domain carries no constraint); i.e. RETURNING djb should return "x", as the base-type path and the JSON_QUERY path do. Actual: the same query with the domain as RETURNING type silently drops the value (NULL under the default NULL ON ERROR), or fails with an error that points at JSON parsing. Anyone using domains over json/jsonb to tighten a schema loses the extracted string values. Root cause pointer: the special-casing in ExecEvalJsonExprPath() (src/backend/executor/execExpr.c) tests "typid =3D=3D JSONOID || typid =3D=3D JSONBOID" and does not include domains over those types (compare getBaseType()). Reproduced on: - PostgreSQL 18.6 (REL_18_STABLE, self-built, snapshot 2026-10-26) - PostgreSQL 20devel (snapshot of 2026-10-29) Regards, Chunling Qin