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 1oCQmL-0007fD-Sm for pgsql-hackers@arkaria.postgresql.org; Fri, 15 Jul 2022 19:15:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oCQmK-0006L3-Nc for pgsql-hackers@arkaria.postgresql.org; Fri, 15 Jul 2022 19:15:08 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oCQmK-0006HT-Dt for pgsql-hackers@lists.postgresql.org; Fri, 15 Jul 2022 19:15:08 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oCQmF-0002XK-Ps for pgsql-hackers@postgresql.org; Fri, 15 Jul 2022 19:15:08 +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 26FJEw2r3438484; Fri, 15 Jul 2022 15:14:58 -0400 From: Tom Lane To: Alvaro Herrera cc: Justin Pryzby , Matthias van de Meent , pgsql-hackers@postgresql.org Subject: Re: MERGE and parsing with prepared statements In-reply-to: <20220715184019.rour3mk33ngc76y6@alvherre.pgsql> References: <20220715184019.rour3mk33ngc76y6@alvherre.pgsql> Comments: In-reply-to Alvaro Herrera message dated "Fri, 15 Jul 2022 20:40:19 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3438482.1657912498.1@sss.pgh.pa.us> Date: Fri, 15 Jul 2022 15:14:58 -0400 Message-ID: <3438483.1657912498@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Alvaro Herrera writes: > On 2022-Jul-15, Justin Pryzby wrote: >> I see now that the same thing can happen with "ON CONFLICT" if used with a >> subselect. >> >> PREPARE p AS INSERT INTO t SELECT a FROM (SELECT $1 AS a)a >> ON CONFLICT (i) DO UPDATE SET i=excluded.i; >> ERROR: column "i" is of type integer but expression is of type text > Right, I didn't think that MERGE was doing anything peculiar in this > respect. Yeah. The current theory about this is that if we haven't assigned a type to an unknown-type parameter (or literal) that is an output column of a sub-SELECT, we will as a rule force it to text. That MERGE USING clause is a sub-SELECT, so that rule applies. There is a hoary old exception to that rule, which is that if you write INSERT INTO tab SELECT ..., $1, ... we will figure out the type of the column of "tab" that $1 is going into, and force $1 to that type instead of text. It looks like this also works in INSERT ... VALUES. You could make a case that MERGE should be equally smart, but it's not clear to me that the info is available sufficiently close by to make it reasonable to do that. It looks like the MERGE syntax has a couple of levels of indirection, which'd probably be enough to put the kibosh on that idea -- in particular, AFAICS there might not be a unique target column corresponding to a given data_source column. regards, tom lane