Re: Insert works but fails for merge - Mailing list pgsql-general

From Greg Sabino Mullane
Subject Re: Insert works but fails for merge
Date
Msg-id CAKAnmm+UgOUpGeurzeFg6r-q7RwhwuY-rQLd2nhSMk2sFfTJQA@mail.gmail.com
Whole thread Raw
In response to Re: Insert works but fails for merge  (Adrian Klaver <adrian.klaver@aklaver.com>)
Responses Re: Insert works but fails for merge
List pgsql-general
So it looks like the OP does not mind updating more than one row. If you want to keep it simple and not do a lot of casting, consider using a CTE to do a reverse-upsert and use a prepared statement. Prepare and cast once, and have your app send the raw uncasted strings many, many times:

prepare foo(text,int,timestamptz) as with x as (update tab1 set mid=$2 where id=$1 returning 1)
  insert into tab1 select $1,$2,$3 where not exists (select 1 from x);

execute foo('5efd4c91-ef93-4477-840c-a723ae212d99', 123, '2024-08-09T11:33:49.402585600Z');
execute foo('some_other_id', 456, '2024-08-11T21:44:55.8675309Z');
etc.

Your app/driver may or may not already do protocol-level statement prepare/execute automagically, so test that way first.

It's version 15.4 postgres.

Keep on the latest revision. Right now, that's 15.8. Upgrading revisions is quick and painless. 

Cheers,
Greg

pgsql-general by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: Insert works but fails for merge
Next
From: 王瞿
Date:
Subject: 回复:searching for libpq5-14.1-42PGDG.rhel8.x86_64