From b35153f8ddb8d47bbb8ef5af62115f9f9287f309 Mon Sep 17 00:00:00 2001
From: Andy Fan <zhihui.fan1213@gmail.com>
Date: Mon, 21 Aug 2023 18:37:31 +0800
Subject: [PATCH v10 3/3] relabel the arg and resultvalue with INTERNALOID.

---
 src/backend/utils/adt/jsonb.c       | 12 ++++++++++--
 src/test/regress/expected/jsonb.out | 12 ++++++------
 2 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/src/backend/utils/adt/jsonb.c b/src/backend/utils/adt/jsonb.c
index d893e9c14b5..87350c6c912 100644
--- a/src/backend/utils/adt/jsonb.c
+++ b/src/backend/utils/adt/jsonb.c
@@ -2091,12 +2091,20 @@ jsonb_cast_support(PG_FUNCTION_ARGS)
 
 		if (OidIsValid(new_func_id))
 		{
-			Const * target_typ = makeConst(OIDOID, -1, InvalidOid, sizeof(Oid),
+			Const *target_typ = makeConst(OIDOID, -1, InvalidOid, sizeof(Oid),
 										   ObjectIdGetDatum(fexpr->funcresulttype),
 										   false, true);
+			/* Let others knows I'm an internal. */
+			RelabelType *rTarget = makeRelabelType((Expr *)target_typ,
+												   INTERNALOID, -1,
+												   InvalidOid,
+												   COERCE_IMPLICIT_CAST);
 			fexpr->funcid = new_func_id;
 			fexpr->args = opexpr->args;
-			fexpr->args = list_insert_nth(fexpr->args, 0, (void *) target_typ);
+			fexpr->args = list_insert_nth(fexpr->args, 0, (void *) rTarget);
+
+			fexpr = (FuncExpr *)makeRelabelType((Expr *) fexpr, INTERNALOID,
+												0, InvalidOid, COERCE_IMPLICIT_CAST);
 		}
 
 		PG_RETURN_POINTER(fexpr);
diff --git a/src/test/regress/expected/jsonb.out b/src/test/regress/expected/jsonb.out
index 8ed80a11176..ad1af16bb3b 100644
--- a/src/test/regress/expected/jsonb.out
+++ b/src/test/regress/expected/jsonb.out
@@ -510,10 +510,10 @@ SELECT test_json -> 2, test_json -> 0 FROM test_jsonb WHERE json_type = 'scalar'
 
 explain (verbose, costs off)
 SELECT (test_json -> 0)::int4, test_json -> 0 FROM test_jsonb WHERE json_type = 'scalarint';
-                                        QUERY PLAN                                        
-------------------------------------------------------------------------------------------
+                                        QUERY PLAN                                         
+-------------------------------------------------------------------------------------------
  Seq Scan on pg_temp.test_jsonb
-   Output: pg_catalog.jsonb_array_element_type('23'::oid, test_json, 0), (test_json -> 0)
+   Output: jsonb_array_element_type(('23'::oid)::internal, test_json, 0), (test_json -> 0)
    Filter: (test_jsonb.json_type = 'scalarint'::text)
 (3 rows)
 
@@ -3571,10 +3571,10 @@ SELECT (j->'a')::numeric,
 (j #> '{"a"}')::numeric,
 (j->0)::numeric
 FROM testjsonb;
-                                                                                                                                                                                                                                                                                     QUERY PLAN                                                                                                                                                                                                                                                                                     
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+                                                                                                                                                                                                                                                                                               QUERY PLAN                                                                                                                                                                                                                                                                                               
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  Seq Scan on public.testjsonb
-   Output: pg_catalog.jsonb_object_field_type('1700'::oid, j, 'a'::text), pg_catalog.jsonb_object_field_type('21'::oid, j, 'a'::text), pg_catalog.jsonb_object_field_type('23'::oid, j, 'a'::text), pg_catalog.jsonb_object_field_type('20'::oid, j, 'a'::text), pg_catalog.jsonb_object_field_type('700'::oid, j, 'a'::text), pg_catalog.jsonb_object_field_type('701'::oid, j, 'a'::text), pg_catalog.jsonb_object_field_type('16'::oid, j, 'a'::text), pg_catalog.jsonb_extract_path_type('1700'::oid, j, '{a}'::text[]), pg_catalog.jsonb_array_element_type('1700'::oid, j, 0)
+   Output: jsonb_object_field_type(('1700'::oid)::internal, j, 'a'::text), jsonb_object_field_type(('21'::oid)::internal, j, 'a'::text), jsonb_object_field_type(('23'::oid)::internal, j, 'a'::text), jsonb_object_field_type(('20'::oid)::internal, j, 'a'::text), jsonb_object_field_type(('700'::oid)::internal, j, 'a'::text), jsonb_object_field_type(('701'::oid)::internal, j, 'a'::text), jsonb_object_field_type(('16'::oid)::internal, j, 'a'::text), pg_catalog.jsonb_extract_path_type(('1700'::oid)::internal, j, '{a}'::text[]), jsonb_array_element_type(('1700'::oid)::internal, j, 0)
 (2 rows)
 
 -- nested tests
-- 
2.21.0

