From e511e9a2d6e4b728ddc04f55520f97ff9935943c Mon Sep 17 00:00:00 2001
From: jian he <jian.universality@gmail.com>
Date: Wed, 2 Dec 2026 23:42:14 +0800
Subject: [PATCH v2 1/1] fix JsonExpr node collation

A JsonExpr's collation is determined solely by its RETURNING type. The context
item, path specification, PASSING arguments, and ON EMPTY / ON ERROR expressions
don't contribute to it, but these subexpressions may still need to be assigned
collations of their own, so we process each of them independently.

Discussion: https://postgr.es/m/19872-fe8b70fdefd5d598%40postgresql.org
---
 src/backend/parser/parse_collate.c            | 23 +++++++++++++++++++
 src/backend/parser/parse_expr.c               |  2 ++
 .../regress/expected/sqljson_jsontable.out    |  9 ++++++++
 src/test/regress/sql/sqljson_jsontable.sql    |  5 ++++
 4 files changed, 39 insertions(+)

diff --git a/src/backend/parser/parse_collate.c b/src/backend/parser/parse_collate.c
index ba7df2a7789..0b7c7349e49 100644
--- a/src/backend/parser/parse_collate.c
+++ b/src/backend/parser/parse_collate.c
@@ -691,6 +691,29 @@ assign_collations_walker(Node *node, assign_collations_context *context)
 															&loccontext);
 						}
 						break;
+					case T_JsonExpr:
+						{
+							/*
+							 * A JsonExpr's collation is determined solely by
+							 * its RETURNING type.  The context item, path
+							 * specification, PASSING arguments and ON EMPTY /
+							 * ON ERROR expressions don't contribute to it, so
+							 * process each of them independently.
+							 */
+							JsonExpr   *expr = (JsonExpr *) node;
+
+							assign_expr_collations(context->pstate,
+												   expr->formatted_expr);
+							assign_expr_collations(context->pstate,
+												   expr->path_spec);
+							assign_list_collations(context->pstate,
+												   expr->passing_values);
+							assign_expr_collations(context->pstate,
+												   (Node *) expr->on_empty);
+							assign_expr_collations(context->pstate,
+												   (Node *) expr->on_error);
+						}
+						break;
 					default:
 
 						/*
diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c
index e207b7a1978..49f003d572d 100644
--- a/src/backend/parser/parse_expr.c
+++ b/src/backend/parser/parse_expr.c
@@ -4647,6 +4647,8 @@ transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *func)
 				jsexpr->collation = InvalidOid;
 			}
 
+			jsexpr->collation = get_typcollation(jsexpr->returning->typid);
+
 			/* JSON_TABLE() COLUMNS can specify a non-boolean type. */
 			if (jsexpr->returning->typid != BOOLOID)
 				jsexpr->use_json_coercion = true;
diff --git a/src/test/regress/expected/sqljson_jsontable.out b/src/test/regress/expected/sqljson_jsontable.out
index 1dcb9479de3..c434fc381d1 100644
--- a/src/test/regress/expected/sqljson_jsontable.out
+++ b/src/test/regress/expected/sqljson_jsontable.out
@@ -2049,3 +2049,12 @@ SELECT * FROM JSON_TABLE(jsonb '{}', '$' PASSING ROW(1, 2) < ROW(3, 4) AS x
  t
 (1 row)
 
+-- PASSING arguments needn't agree on their collations (bug #19872)
+SELECT * FROM JSON_TABLE(jsonb '{}', '$'
+	PASSING 'b' COLLATE "POSIX" AS v1, 'c' COLLATE "C" AS v2
+	COLUMNS (n FOR ORDINALITY));
+ n 
+---
+ 1
+(1 row)
+
diff --git a/src/test/regress/sql/sqljson_jsontable.sql b/src/test/regress/sql/sqljson_jsontable.sql
index 73e60411df6..255f955298a 100644
--- a/src/test/regress/sql/sqljson_jsontable.sql
+++ b/src/test/regress/sql/sqljson_jsontable.sql
@@ -1117,3 +1117,8 @@ SELECT * FROM JSON_TABLE(jsonb '{}', '$' PASSING 'a' < 'b' COLLATE "C" AS x
 	COLUMNS (a bool PATH '$x'));
 SELECT * FROM JSON_TABLE(jsonb '{}', '$' PASSING ROW(1, 2) < ROW(3, 4) AS x
 	COLUMNS (a bool PATH '$x'));
+
+-- PASSING arguments needn't agree on their collations (bug #19872)
+SELECT * FROM JSON_TABLE(jsonb '{}', '$'
+	PASSING 'b' COLLATE "POSIX" AS v1, 'c' COLLATE "C" AS v2
+	COLUMNS (n FOR ORDINALITY));
-- 
2.34.1

