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 1tcQfh-0001cY-Ab for pgsql-novice@arkaria.postgresql.org; Mon, 27 Jan 2025 15:05:05 +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 1tcQff-00D1Dy-KO for pgsql-novice@arkaria.postgresql.org; Mon, 27 Jan 2025 15:05:03 +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 1tcQff-00D1Dq-BI for pgsql-novice@lists.postgresql.org; Mon, 27 Jan 2025 15:05:03 +0000 Received: from mail-ej1-x62f.google.com ([2a00:1450:4864:20::62f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tcQfc-001nub-08 for pgsql-novice@lists.postgresql.org; Mon, 27 Jan 2025 15:05:02 +0000 Received: by mail-ej1-x62f.google.com with SMTP id a640c23a62f3a-ab633d9582aso811018666b.1 for ; Mon, 27 Jan 2025 07:05:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1737990299; x=1738595099; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=QSFHMSvmgyENKLvt7lKovaAEKaep5E328pCnVEwYPUM=; b=A2y5KzyqoSPVm1Of3BoTLhmyfLMCcAefWTyMnijju2qlV9+wDXN6SvKyRI7BJUt4Hb zzUc8N9b2L/y0JWlFWEtveYWPTvyxQgykKMMmrdLzOSRKNdCi1yBgUtYHaFMkiK3OI2v 3EGKV94udH4KfwREf+KF/G7E3Onr5cpiNw3c9KZZ5J01IaWlcxXGTD0x2w+JPyfllooB 1JrRM4m144B892SHKQYclbDn2Y7yopmIWTi+29nTkbv20J5zLkNXFDL0L9687dwQUpqj uEbwlCIVCCYy6hfDcOe0JnD7nAiqVb2EePpnSg630RnSp3fDhpJjyFclBzHVHYW9Phcb xsVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737990299; x=1738595099; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=QSFHMSvmgyENKLvt7lKovaAEKaep5E328pCnVEwYPUM=; b=p9QBAkrxGY+aFQT707uCXROGggNTUzxfAo4JbNN3ZMTdU6AHnhEY2LG/jSF5C/Nvtx t+q/sZyrNjEWNUowHMWC10+DwH3U2/GEXNbmz5qGLZcRY2Wh82P1MrocZkMgU/C/sgtN Gdy9xCiUMp8ABu112U9cH2t3EbN7ilKVTh85eauoFcpLZQhOtMY+utREGUkTVDNLQucd XpA9MUs17OK9w+cIdIzQyFUdKUNnrWhNZS5jFrhPUBwplABm5z4gF6/SBCgCtUCF8161 +N//D0vcW4XiB7/Oj9iirSS03L8rajCwo9f6fhpNO3lfA+myOdApK2JE9+lMFwcRy0CH bxrA== X-Forwarded-Encrypted: i=1; AJvYcCV/H7bkUhc2rUzLTXDNL0Ea+cuWDBFC1hxA+cOYk6trmWO2dpa468LAHRhAVdlH96VzLcqtsvyWKrFvKXI=@lists.postgresql.org X-Gm-Message-State: AOJu0YzSmkNmOThxXZ7QkT2EUkIultK2nblSooHyhuhgzHolFr98tWCV bSvbNfuN1ltNLELDjY77AUWcK0roRCz9363SiYpxBFJaqJQYUa2pBpg1xNhuMfuq1hrP5Mtmccd a X-Gm-Gg: ASbGncs6qpPd6ZQiChG4ZSBxjZgM7YVm81V/f6QiCUgo0i7h20xF0kKWwbY2f2TJyuL liMiSmjsN/gcqAfLWng0gAMikFIoLQxfb2cuVkGtvzqrr7kSCiMzJnF6f3gf0p17tne3hLwYzNB 7oYjXJIfF/rsmqwBr7xxtXmZpzIiTA8SIcQsTdaWAz2c7udbVun2UcaRlzn4n0ho2ZOoyZG8PHx x14B0SPjvrxWEwDpiMZd7C7anhOe7q43GkoPygXrzTbSCHtFOT64hnnbqtyMiWIC/hvXEMjuCUj 6dyXvBxAWThDR5VLq0zJtJJc X-Google-Smtp-Source: AGHT+IGKyYtblKgKx/fZEfPmLr5AsaFzsSA9lVP0vms+Qa0SKvAKDFSrGKyrJ6KZDNeA+EkbpPvf5g== X-Received: by 2002:a17:906:d555:b0:aa6:aa8e:c89c with SMTP id a640c23a62f3a-ab38b44d49emr4288962166b.39.1737990297561; Mon, 27 Jan 2025 07:04:57 -0800 (PST) Received: from localhost.localdomain ([2001:871:5e:d10a:515c:df26:bd3e:3870]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ab676117260sm588739566b.185.2025.01.27.07.04.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jan 2025 07:04:57 -0800 (PST) Message-ID: Subject: Re: Insert on conflict do update fails with duplicate rows - there are no duplicates From: Laurenz Albe To: Pedro Moraes , pgsql-novice@lists.postgresql.org Date: Mon, 27 Jan 2025 16:04:56 +0100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.54.3 (3.54.3-1.fc41) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 2025-01-27 at 10:18 -0300, Pedro Moraes wrote: >=20 > I managed to replicate the issue with the following data & tables attache= d download link. >=20 > I am trying to insert from bugtest.temp_on_conflict_test into bugtest.his= tory, both tables have a primary key on history_id so there cannot be dupli= cates >=20 > The insert query also uses distinct on (history_id) >=20 > INSERT INTO bugtest.history (account_id,appo_id,account_name,resource_own= er_name,resource_owner_id,resource_owner_user_id,resource_id,resource_type,= preview,resource_date,history_id)=20 > select distinct on (history_id) * from bugtest.temp_on_conflict_test limi= t 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 * >=20 > Reproducible dump:=20 > https://limewire.com/d/2d598eff-f7c9-47d2-80d6-7e522ee1688c#ru2TRVilIqQdq= mEjr2-_H4z67PHYe2JZAshDErA8umw I get this error message: ERROR: ON CONFLICT DO UPDATE command cannot affect row a second time HINT: Ensure that no rows proposed for insertion within the same command h= ave duplicate constrained values. The problem is that you are using "SELECT *", which means that you will get= the columns in the order they were defined in the table. So you end up inserting "temp_on_conflict_test.account_name" into "history.history_id", which cause= s the error, because all rows in "temp_on_conflict_test" have the same value in that col= umn. Lessons to learn: - don't use "SELECT *" - use more appropriate data types In the case at hand, using type "uuid" for "history_id" would have got yo= u a more understandable error message. Yours, Laurenz Albe