Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oWKDx-00017r-NP for pgsql-bugs@arkaria.postgresql.org; Thu, 08 Sep 2022 16:17:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oWKDv-0006KJ-GZ for pgsql-bugs@arkaria.postgresql.org; Thu, 08 Sep 2022 16:17:51 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oWKDv-0006K7-7z for pgsql-bugs@lists.postgresql.org; Thu, 08 Sep 2022 16:17:51 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oWKDj-00080h-2t for pgsql-bugs@lists.postgresql.org; Thu, 08 Sep 2022 16:17:47 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 288GHEvY4048044; Thu, 8 Sep 2022 12:17:14 -0400 From: Tom Lane To: Japin Li cc: mjurca@centrum.cz, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #17610: Use of multiple composite types incompatible with record-typed function parameter In-reply-to: References: <17610-fb1eef75bf6c2364@postgresql.org> Comments: In-reply-to Japin Li message dated "Thu, 08 Sep 2022 23:21:51 +0800" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <4047990.1662653813.0@sss.pgh.pa.us> Date: Thu, 08 Sep 2022 12:17:14 -0400 Message-ID: <4048043.1662653834@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <4047990.1662653813.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Japin Li writes: > On Thu, 08 Sep 2022 at 18:19, PG Bug reporting form wrote: >> The SQL code at the end of this bug report ends with the following erro= r: >> 2022-09-08 10:09:02.173 GMT [174] ERROR: type of parameter 1 >> (composite_type_2) does not match that when preparing the plan >> (composite_type_1) > The documentation says: > The mutable nature of record variables presents another problem in thi= s > connection. When fields of a record variable are used in expressions o= r > statements, **the data types of the fields must not change from one ca= ll > of the function to the next**, since each expression will be analyzed > using the data type that is present when the expression is first reach= ed. I think that is specifically referring to function internal variables of type RECORD, which are handled specially. For function input arguments, we could get around this by treating RECORD like a polymorphic type, as attached. (For some reason I thought we already did that, but nope.) regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="treat-record-inputs-as-polymorphism.patch"; charset="us-ascii" Content-ID: <4047990.1662653813.2@sss.pgh.pa.us> Content-Description: treat-record-inputs-as-polymorphism.patch diff --git a/src/pl/plpgsql/src/pl_comp.c b/src/pl/plpgsql/src/pl_comp.c index 61fbdf0686..4d68f030e4 100644 --- a/src/pl/plpgsql/src/pl_comp.c +++ b/src/pl/plpgsql/src/pl_comp.c @@ -2498,9 +2498,15 @@ compute_function_hashkey(FunctionCallInfo fcinfo, /* * This is the same as the standard resolve_polymorphic_argtypes() function, - * but with a special case for validation: assume that polymorphic arguments - * are integer, integer-array or integer-range. Also, we go ahead and report - * the error if we can't resolve the types. + * except that: + * 1. We go ahead and report the error if we can't resolve the types. + * 2. We treat RECORD-type input parameters (not output parameters) as if + * they were polymorphic, replacing their types with the actual input + * types if we can determine those. This allows us to create a separate + * function cache entry for each named composite type passed to such a + * parameter. + * 3. In validation mode, we have no inputs to look at, so assume that + * polymorphic arguments are integer, integer-array or integer-range. */ static void plpgsql_resolve_polymorphic_argtypes(int numargs, @@ -2512,6 +2518,8 @@ plpgsql_resolve_polymorphic_argtypes(int numargs, if (!forValidator) { + int inargno; + /* normal case, pass to standard routine */ if (!resolve_polymorphic_argtypes(numargs, argtypes, argmodes, call_expr)) @@ -2520,10 +2528,28 @@ plpgsql_resolve_polymorphic_argtypes(int numargs, errmsg("could not determine actual argument " "type for polymorphic function \"%s\"", proname))); + /* also, treat RECORD inputs (but not outputs) as polymorphic */ + inargno = 0; + for (i = 0; i < numargs; i++) + { + char argmode = argmodes ? argmodes[i] : PROARGMODE_IN; + + if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE) + continue; + if (argtypes[i] == RECORDOID || argtypes[i] == RECORDARRAYOID) + { + Oid resolvedtype = get_call_expr_argtype(call_expr, + inargno); + + if (OidIsValid(resolvedtype)) + argtypes[i] = resolvedtype; + } + inargno++; + } } else { - /* special validation case */ + /* special validation case (no need to do anything for RECORD) */ for (i = 0; i < numargs; i++) { switch (argtypes[i]) ------- =_aaaaaaaaaa0--