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 1oCRAk-0000nG-90 for pgsql-hackers@arkaria.postgresql.org; Fri, 15 Jul 2022 19:40:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oCRAi-0006sk-5w for pgsql-hackers@arkaria.postgresql.org; Fri, 15 Jul 2022 19:40:20 +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 1oCRAh-0006qs-PD for pgsql-hackers@lists.postgresql.org; Fri, 15 Jul 2022 19:40:19 +0000 Received: from mail-qt1-x82b.google.com ([2607:f8b0:4864:20::82b]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oCRAe-0007Du-MC for pgsql-hackers@postgresql.org; Fri, 15 Jul 2022 19:40:18 +0000 Received: by mail-qt1-x82b.google.com with SMTP id r21so3838526qtn.11 for ; Fri, 15 Jul 2022 12:40:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=0Mox/DWa+ZUZYXaMmGalOM3M47PYX5/vp7AI0j/XG94=; b=IZMyGi4DbC0mmsTNai0q9DGWklpBhG8005oMSqT+e9SQEROK0D+fcWxxN3+YQd2yul P40GrquO2QI92bLeWQGgfY+7Qg75ecI9JcJcaETZ6k1edYdvWDggP7aNSop8McP7ASwg ehuxqKoBwb06GXFa+6wlP9ECwvggy1z7hjsxSYYKDaQ5YYazhpG9AcZgWFwxhsC4+mY9 NOwZJ5upGudBlDgBNnj/qOvQ00L0XTW7pNqFxW0GS3V+MmLOqW/HauLHGDBMvWfOGzzc iLxdzR+UIjAVqweFUu6EwpcVEdBBYQHkLAr9jxrO8Zxp6l3FRV7iKyaI2S522bqD5Pw8 2BJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=0Mox/DWa+ZUZYXaMmGalOM3M47PYX5/vp7AI0j/XG94=; b=u8ZSj0i7u1hZ8t2eVvSjpPHCzGrrbxYTurKswKdFjRJ4bP54nJ0WH7FvDkfAz7PdOu 9ecGm05BcskabmbVSuwfcqNILX6cZwoxqg+DxpmOaQwrS2hUK46J7HEtFD/WYZgtw1+Q CC+kCwQ/NywS0rq4nv1G0JaAfg1/LyY0r8uc1QYHXW+J3/RosvyHbZG5gYmAc9sK2QAu 4PAIeZlBCpE5WnHDngAsVW1ommDSKycLY6mdNRmYc8Pdleww3yXeTUwERiPUiPq6q83N WCACIn9XjEV0gnWHyYKUQ75b8mmw77P6MKgcq1pwVjc2EA92JNnWKwESppGsakU9wTO2 FiWQ== X-Gm-Message-State: AJIora+ic4PW+RDZc/qRbsBgnIKxv46TdKUuTb3x607sj05ckuivD1qG 7IgQKOGddy18XX1pDWKhC5joFA== X-Google-Smtp-Source: AGRyM1s1Jp05lFKnRGK59qzM4Wphii1RS7ZgaLrDogRGJlLDIVavvh5MVE2Cjc8VFMgJD3BB45vrHw== X-Received: by 2002:a05:622a:1355:b0:31e:b6b5:6789 with SMTP id w21-20020a05622a135500b0031eb6b56789mr13694071qtk.499.1657914015732; Fri, 15 Jul 2022 12:40:15 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id d22-20020a05620a159600b006b5517da3casm4486817qkk.22.2022.07.15.12.40.14 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 15 Jul 2022 12:40:15 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 29089800D6A; Fri, 15 Jul 2022 14:40:14 -0500 (CDT) Date: Fri, 15 Jul 2022 14:40:14 -0500 From: Justin Pryzby To: "David G. Johnston" Cc: Alvaro Herrera , Matthias van de Meent , pgsql-hackers@postgresql.org Subject: Re: MERGE and parsing with prepared statements Message-ID: <20220715194013.GN18011@telsasoft.com> References: <20220715175854.GM18011@telsasoft.com> <20220715184019.rour3mk33ngc76y6@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Jul 15, 2022 at 12:17:51PM -0700, David G. Johnston wrote: > On Fri, Jul 15, 2022 at 11:40 AM Alvaro Herrera wrote: > > On 2022-Jul-15, Justin Pryzby wrote: > > > > > It seems a bit odd that it's impossible to use merge with prepared statements > > > without specifically casting the source types (which I did now to continue my > > > experiment). > > > > I have no comments on this. Maybe it can be improved, but I don't know > > how. > > Not tested, but the example prepare command fails to make use of the > optional data types specification. Using that should remove the need to > cast the parameter placeholder within the query itself. What optional data type specification ? > That said, in theory the INSERT specification of the MERGE could be used to > either resolve unknowns or even forcibly convert the data types of the > relation produced by the USING clause to match the actual types required > for the INSERT (since that will happen at insert time anyway). Yeah. I hadn't looked before, but just noticed this: https://www.postgresql.org/docs/devel/sql-merge.html | If the expression for any column is not of the correct data type, automatic type conversion will be attempted. That appears to be copied from the INSERT page. What does that mean, if not that data types will be resolved as needed ? Note that if I add casts to the "ON" condition, MERGE complains about the INSERT VALUES. PREPARE p AS MERGE INTO CustomerAccount CA USING (SELECT $1 AS CustomerId, $2 AS TransactionValue) AS T ON CA.CustomerId = T.CustomerId::int WHEN NOT MATCHED THEN INSERT (CustomerId, Balance) VALUES (T.CustomerId, T.TransactionValue) WHEN MATCHED THEN UPDATE SET Balance = Balance + TransactionValue; ERROR: column "customerid" is of type integer but expression is of type text LINE 7: VALUES (T.CustomerId, T.TransactionValue) postgres: pryzbyj postgres [local] PREPARE(transformAssignedExpr+0x3b6) [0x5605f9699e8e] postgres: pryzbyj postgres [local] PREPARE(transformInsertRow+0x2ce) [0x5605f9653a47] postgres: pryzbyj postgres [local] PREPARE(transformMergeStmt+0x7ec) [0x5605f968fe3b] postgres: pryzbyj postgres [local] PREPARE(transformStmt+0x70) [0x5605f9656071] postgres: pryzbyj postgres [local] PREPARE(+0x1fa350) [0x5605f9657350] postgres: pryzbyj postgres [local] PREPARE(transformTopLevelStmt+0x11) [0x5605f9657385] postgres: pryzbyj postgres [local] PREPARE(parse_analyze_varparams+0x5b) [0x5605f96574f4] postgres: pryzbyj postgres [local] PREPARE(pg_analyze_and_rewrite_varparams+0x38) [0x5605f991edfe] postgres: pryzbyj postgres [local] PREPARE(PrepareQuery+0xcc) [0x5605f96f4155] postgres: pryzbyj postgres [local] PREPARE(standard_ProcessUtility+0x4ea) [0x5605f99251a0] postgres: pryzbyj postgres [local] PREPARE(ProcessUtility+0xdb) [0x5605f992587e] -- Justin