Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sdJhs-00EccY-2X for pgsql-general@arkaria.postgresql.org; Mon, 12 Aug 2024 01:18:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sdJhp-00Ct8J-S4 for pgsql-general@arkaria.postgresql.org; Mon, 12 Aug 2024 01:18:41 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sdJhp-00Ct8A-D8 for pgsql-general@lists.postgresql.org; Mon, 12 Aug 2024 01:18:41 +0000 Received: from mail-lf1-x12a.google.com ([2a00:1450:4864:20::12a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sdJhm-004GhY-9G for pgsql-general@lists.postgresql.org; Mon, 12 Aug 2024 01:18:39 +0000 Received: by mail-lf1-x12a.google.com with SMTP id 2adb3069b0e04-52efef496ccso3251742e87.1 for ; Sun, 11 Aug 2024 18:18:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723425516; x=1724030316; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=5JqN10Rud6MlOjX5h+VqD9gGwGcp4MpPmuoC+c6I7rE=; b=L0m44ZyJfiYx2iFPwLPlatGCzsdhxj+AcZMtXwB9+O2kit1E2aP+o+iyITY3hqcF5K A1VwE5f/j+F1ewd4hYwehi2MPQqDjt1to01RKdWyCEFg3hjtIAgiIWKnLhkP0QBoXkqK RhBlgQF8bPUCiMTThFUbC6IxvDYKs3TajeS+//wgVwkchH1bXa41cN/11UDwmKL2Ar12 LfZ4LBT32RW+SqbVYsDBTR+pEknHTCYZPIHICrN0F3bnakYvlnYEOgyChk60TZBFy7dV Bu5ilILWkjdn+TgjllBtmpw4A0mSU8NTySxaVFagIIpR732TfGl1PXTqYm30a6GJZW+6 oJPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723425516; x=1724030316; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=5JqN10Rud6MlOjX5h+VqD9gGwGcp4MpPmuoC+c6I7rE=; b=Ee9VR/DTCblyYB3MGGE55V7VYcDhBdoH+cj+jcupGRvcKhWBz24JXtVCmBzoZuDXx1 On7ASM8q6ErWaB0V7bRjQOz2eReyaMfJz+FGUAzbJOLzKceNwK1UAklZ4szubh5ZJ0iz rF4XZwdA3zuMKGiI1KrBEgZspV0qfpCbdXMnYX0zcbzSVj+28T5PYvFw/Zj8zmuxZNkd 0WwC4YRk85mq4KO7gv1glV2P7HZ+iTBNEzB82scg2H8jKI5El0ELd4aQ0xLjWPBWRTRw LENTRO3gbQ/QUF39XHLZktHf9lKWCpael83fyoxgvIEaaMig8qzh69QNZlx1fVqKW7Ls JRNg== X-Forwarded-Encrypted: i=1; AJvYcCWPV70hKZWXUIL8jKOHAcJBcpRe/28a/lTkWWBViVuUjQhspe/ylilZ166XC2ufx9+NFRxOX9FrWZrlc4Meg2Dv1R6j976sgRjDW9kDCmKuNCRx X-Gm-Message-State: AOJu0YwB9QrurVAvJH6VLo4nJhAUGxzvtjidfgFyHMDoE8EQmBYy6aIm aRHk0Ls4RKOwzOsT/51b5qEyB/IYn50Rfv2qQSn2lH5yrB1wg084KvDXlXhAEBh+eLrzIT2+o61 awm7zACpCXr2uaB0G0uLahTgfh0Q= X-Google-Smtp-Source: AGHT+IG6Q1rkGglGc4CSL7vHQhmChFD6PNtTgkVdZ5+qZyy9C/RGCiaoEDZ79LKb0yVC5rZ3/Y3iTArzkNn8bOQ6DyM= X-Received: by 2002:a05:6512:3b98:b0:52c:a070:944 with SMTP id 2adb3069b0e04-530e5dc6fa1mr3304182e87.23.1723425516096; Sun, 11 Aug 2024 18:18:36 -0700 (PDT) MIME-Version: 1.0 References: <37e09717-f121-4192-b152-18df17713414@aklaver.com> <9dc702ca-8bb3-442e-bd2b-12abe81a84d0@aklaver.com> In-Reply-To: From: Greg Sabino Mullane Date: Sun, 11 Aug 2024 21:18:00 -0400 Message-ID: Subject: Re: Insert works but fails for merge To: Adrian Klaver Cc: Alban Hertroys , yudhi s , David G Johnston , pgsql-general Content-Type: multipart/alternative; boundary="000000000000af12b3061f724512" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000af12b3061f724512 Content-Type: text/plain; charset="UTF-8" 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 --000000000000af12b3061f724512 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
So it looks like the OP does not mind updating more than o= ne 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 a= nd cast once, and have your app send the raw uncasted strings many, many ti= mes:

prepare foo(text,int,timestamptz) as with x as (upd= ate tab1 set mid=3D$2 where id=3D$1 returning 1)
=C2=A0 insert in= to tab1 select $1,$2,$3 where not exists (select 1 from x);
<= br>
execute foo('5efd= 4c91-ef93-4477-840c-a723ae212d99&= #39;, 123,=C2=A0'2024-08-09T1= 1:33:49.402585600Z');<= /div>
execute foo('some_other_id= ', 456,=C2=A0'2024-08-11T= 21: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 ver= sion 15.4 postgres.

Keep on the latest revi= sion. Right now, that's 15.8. Upgrading revisions is quick and painless= .=C2=A0

Cheers,
Greg

--000000000000af12b3061f724512--