From: Justin Pryzby <pryzby@telsasoft.com>
To: David G. Johnston <david.g.johnston@gmail.com>
Cc: Alvaro Herrera <alvherre@alvh.no-ip.org>
Cc: Matthias van de Meent <boekewurm+postgres@gmail.com>
Cc: pgsql-hackers@postgresql.org
Subject: Re: MERGE and parsing with prepared statements
Date: Fri, 15 Jul 2022 14:40:14 -0500
Message-ID: <20220715194013.GN18011@telsasoft.com> (raw)
In-Reply-To: <CAKFQuwbtQk52zWLYsXvO2+ZGTPu+MPvWgaCS7jQKuGhrwn0-rA@mail.gmail.com>
References: <20220715175854.GM18011@telsasoft.com>
<20220715184019.rour3mk33ngc76y6@alvherre.pgsql>
<CAKFQuwbtQk52zWLYsXvO2+ZGTPu+MPvWgaCS7jQKuGhrwn0-rA@mail.gmail.com>
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 <alvherre@alvh.no-ip.org> 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: pryzby@telsasoft.com, david.g.johnston@gmail.com, alvherre@alvh.no-ip.org, boekewurm+postgres@gmail.com
Subject: Re: MERGE and parsing with prepared statements
In-Reply-To: <20220715194013.GN18011@telsasoft.com>
References: <20220715175854.GM18011@telsasoft.com> <20220715184019.rour3mk33ngc76y6@alvherre.pgsql> <CAKFQuwbtQk52zWLYsXvO2+ZGTPu+MPvWgaCS7jQKuGhrwn0-rA@mail.gmail.com> <20220715194013.GN18011@telsasoft.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox