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 1tcP0p-00HOM5-JM for pgsql-novice@arkaria.postgresql.org; Mon, 27 Jan 2025 13:18:48 +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 1tcP0n-00CNJc-H7 for pgsql-novice@arkaria.postgresql.org; Mon, 27 Jan 2025 13:18:45 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tcP0n-00CNJP-8g for pgsql-novice@lists.postgresql.org; Mon, 27 Jan 2025 13:18:45 +0000 Received: from mail-yb1-xb2a.google.com ([2607:f8b0:4864:20::b2a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tcP0k-001mvU-16 for pgsql-novice@lists.postgresql.org; Mon, 27 Jan 2025 13:18:44 +0000 Received: by mail-yb1-xb2a.google.com with SMTP id 3f1490d57ef6-e4930eca0d4so6607749276.3 for ; Mon, 27 Jan 2025 05:18:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737983921; x=1738588721; darn=lists.postgresql.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=k0pzsnZ4Eqw45D6mzOrUdcvufs6NgLsGMu1cyODBw8c=; b=TJDGUZpg4OrwA/jrjzN4bqrKKeXPo06V584iBXxZYtJpq0jmOVjS8Qcecw6JgxkRQg 3iwgZuItfVukW6li0OdydJLStSHKuM2jPsS13u37Gpwy1bDLZleWw7lQxaqQ9a7sWDji OiEO9yOYh+e4di/M5Ck/pYnDJRR6tr4DyLxpVBrRIYG6S7/ODXgmUL34DjVkAUYnMrrk cx5JLNalywHD5OVNBNzTMvzAbuE0xwTEagkSqu24QY+ypiiR2YkwNNa70dF7/GLFVo93 dh9mDcFfVRsBlyfkCXs8sSTEAFm4fOJ8fsbPYhBVQyDC9nwzhAv+CDdP3YxfxBlmrbfi j7Mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737983921; x=1738588721; h=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=k0pzsnZ4Eqw45D6mzOrUdcvufs6NgLsGMu1cyODBw8c=; b=a1l0xulN7ALrzEfZvIRbJMZN4IAW42/4OrpcTZHeFwm+dvsLtQSaofwn4D0iOUXgSF 6c2bBJPQj+PJKkjiskmyAc5WwKE5ZGCpd5AYVCOrSZSRQwcxmc6SXnDegJpB5J4uBVXh 1oogq+yCnObP0n2fqRYUBt8762Qf8CYe8JZzFHvLri928t8/nZHTfxn9FFjYgrjITVTs K6LOy9+7GF6vnYWqW8h6MXrIYIPmUQf8KVgdb8kGdsTM2H+8r3LDQm4/eN/yHQ3/zYNL Ya8tIgBfiQDSZ+gv3YjTlMxn/jOjwOWRARRf01XmyXQkHo3g+Ya0dgdjKui+sZMyA2mu WxSA== X-Gm-Message-State: AOJu0YyZBkKOnfzdrzQUb26N+YjH8PgWTRnyRfVODPDNQWMPQhE5KlhX JAJTRrJbw0zz/WoP6P0DST2s/thVi+mtsgtKtGSVu7lqtvm+kQ8aLUyVb54EkU/qtb6TWceLXnV U2akrRl7uYje7IYTBCvShZxjPfUvQ2WjA X-Gm-Gg: ASbGncvi91wtw8y5bXWpgVtGgMT3Fi5/13wfoyJujQa9sjReSMX46T8Oy1YTQSlQTfF 8oabUEKD3xR4PQxQvYdvSA6SdY/ftoJgzdyD97KbUpFqtJKT44GM33lywX5QZRGs= X-Google-Smtp-Source: AGHT+IG8DJhxEirzMEwKNoKeNvMuX/GkZ71w6/49eGXzvCv8RvLfdIHNGeBqtgEZgHwvcAq8KCe7atnx2gI24ocOU8o= X-Received: by 2002:a05:6902:20c4:b0:e56:c350:5973 with SMTP id 3f1490d57ef6-e57b1349f96mr29286600276.45.1737983920903; Mon, 27 Jan 2025 05:18:40 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Pedro Moraes Date: Mon, 27 Jan 2025 10:18:29 -0300 X-Gm-Features: AWEUYZlmVOvp51xV4pJWgoB32BsCFo3RsdOpmGGArl_NEBfYe0Ggy6pfwqP6Sac Message-ID: Subject: Insert on conflict do update fails with duplicate rows - there are no duplicates To: pgsql-novice@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000003b2067062cafea06" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000003b2067062cafea06 Content-Type: text/plain; charset="UTF-8" I managed to replicate the issue with the following data & tables attached download link. I am trying to insert from bugtest.temp_on_conflict_test into bugtest.history, both tables have a primary key on history_id so there cannot be duplicates The insert query also uses distinct on (history_id) INSERT INTO bugtest.history (account_id,appo_id,account_name,resource_owner_name,resource_owner_id,resource_owner_user_id,resource_id,resource_type,preview,resource_date,history_id) select distinct on (history_id) * from bugtest.temp_on_conflict_test limit 2 ON CONFLICT (history_id) DO UPDATE SET account_id = excluded.account_id, resource_owner_name = excluded.resource_owner_name, resource_owner_user_id = excluded.resource_owner_user_id, resource_owner_id = excluded.resource_owner_id, preview = excluded.preview, resource_date = excluded.resource_date, account_name = excluded.account_name RETURNING * Reproducible dump: https://limewire.com/d/2d598eff-f7c9-47d2-80d6-7e522ee1688c#ru2TRVilIqQdqmEjr2-_H4z67PHYe2JZAshDErA8umw --0000000000003b2067062cafea06 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

I managed to replicate the issue with the following data &= amp; tables attached download link.

I am trying to= insert from bugtest.temp_on_conflict_test=20 into=20 bugtest.history, both tables have a primary key on history_id so there cann= ot be duplicates

The insert query also uses distin= ct on (history_id)

INSERT INTO bugtest.history (account_id,appo_id,account_name,resource_= owner_name,resource_owner_id,resource_owner_user_id,resource_id,resource_ty= pe,preview,resource_date,history_id)=20
select distinct on (history_id) * from bugtest.temp_on_conflict_= test limit 2
ON CONFLICT (history_id) DO UPDATE SET=20
account_id =3D excluded.account_id,
resource_owner_name =3D excluded.resource_owner_name,
resource_owner_user_id =3D excluded.resource_owner_user_id,
resource_owner_id =3D excluded.resource_owner_id,
preview =3D excluded.preview,
resource_date =3D excluded.resource_date,
account_name =3D excluded.account_name
RETURNING *
--0000000000003b2067062cafea06--