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 1tANAl-00FNLf-O9 for pgsql-general@arkaria.postgresql.org; Mon, 11 Nov 2024 05:41:11 +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 1tANAh-00BVuB-9u for pgsql-general@arkaria.postgresql.org; Mon, 11 Nov 2024 05:41:07 +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 1tANAg-00BVu3-TL for pgsql-general@lists.postgresql.org; Mon, 11 Nov 2024 05:41:07 +0000 Received: from mail-oa1-x2c.google.com ([2001:4860:4864:20::2c]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tANAc-001F8Z-9s for pgsql-general@lists.postgresql.org; Mon, 11 Nov 2024 05:41:06 +0000 Received: by mail-oa1-x2c.google.com with SMTP id 586e51a60fabf-28cdd9d8d01so2102627fac.1 for ; Sun, 10 Nov 2024 21:41:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731303661; x=1731908461; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=C3x94nuJzFbTb+caT594GFuJi4yKGyQtAB7ZzHqgYiQ=; b=TEDnKFBdcXouG61QiMrK3F+lmIA5tJly6UbuET1mFyZ/LSTfiyirzxhJwe6F2tE/Pl pMhuOkiHfaNmMhGfDfeb0ZoPt+PnO4SHnNXlDlW8XRYOiicWg1qCza+BvVpoXQqKyWgT ENsaUIO2t3ZLng7frfUePW6nAfrrqjK3+sp88+lZk2n1XRgHKL6duE3LdLPvyzswR8zJ 89jVXCQOCn/rXo6g5jorUt9qs1DFXt8Y356/DMUUHNU/VHTlwhwKKyA7PXQf7WKSHWhq E0lGGPcA/LK2dJZ0z8PnHvXMJboXfxVDivoPW7JKR8uFD7w6r+qSjhIdbEpNlc8JdObo WmDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731303661; x=1731908461; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=C3x94nuJzFbTb+caT594GFuJi4yKGyQtAB7ZzHqgYiQ=; b=rBGNuXsr2uJ6ezVk5MJyyUJn47puxCveOGDghKq3g0l4nSR2bLpkJdv30Id6MSIMh9 Gaw8Xwil9SjG0eZIsPEvTK8YUCnVt8HtxgMNRPCEc/qDCU+Qobtxac12GP9DVPI5JtQW JRLf2V8ENzpDR6voFG3pJfRZdnhqvLB0iewxw7wdj2LETibvpQTacv/6NozHu59Wz26f VcW6fHv4pEa2HCmLoXoxdCPt0hel7N5NtOHQIxFi0gQ2qkht/b7DBE1gPdWcuSQs7PsT GLg94Xe9ZYbJ5bcH/2cCWAGvK7G7hVJfrS7Px2MRcD/PgbEiPIBnUL5rb4X8/XNft6bv SCmw== X-Gm-Message-State: AOJu0Yz/2SvdWA7a0YDOxZfcuv2/bzpM5RRVEu8Ez0MbmUL9L7mlzXm+ S/ABl3vDZ4tArTFzMSODpRUbfAqExAQJkXjPLmnw5Q0yElfH7KLm+FmTY+tHmNXRiG8EGgQM5Mo b6L5Gb5cRUvtpMuyIpiWkflurF8civAntDz0= X-Google-Smtp-Source: AGHT+IEI0DUuu/RRKpU6bkt8JhLQ82Y/+gwdJ2X0KHWrO81fO2ZREE6TzpCK03dqZjnHrKgz6y17Q9FRbX2UdFuMr4E= X-Received: by 2002:a05:6871:b21:b0:278:2c82:e056 with SMTP id 586e51a60fabf-29560130b2amr8715292fac.23.1731303660982; Sun, 10 Nov 2024 21:41:00 -0800 (PST) MIME-Version: 1.0 From: yudhi s Date: Mon, 11 Nov 2024 11:10:48 +0530 Message-ID: Subject: Duplicate key error To: pgsql-general Content-Type: multipart/alternative; boundary="000000000000b6199906269c8ba9" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000b6199906269c8ba9 Content-Type: text/plain; charset="UTF-8" We have a merge query as below for a partition table which is range partitioned on a truncated date column 'part_date'. And the only unique key in this table is a composite primary key on (id, part_date). And this merge queries ON condition is based on one of the columns i.e ID which is the leading column of the PK.So it means it will ensure no duplication happens for ID values, but still we are seeing "duplicate key" error. So what is the possible reason here or are we encountering any buggy behaviour here? WITH source_data (col1, col2, col3.....col29) AS (VALUES ($1, $2::date, $3::timestamptz, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27::timestamptz, $28, $29::timestamptz)) MERGE INTO cpod.TAB1 AS target USING source_data AS source ON target.ID = source.ID WHEN MATCHED THEN UPDATE SET ....) WHEN NOT MATCHED THEN INSERT (...) VALUES (....); Error: ERROR: duplicate key value violates unique constraint "TAB1_pkey" Detail: Key (ID, part_date)=(XXXXXXXXX, 2024-11-04) already exists. Regards Yudhi --000000000000b6199906269c8ba9 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
We have a merge query as below for a partition table which= is range partitioned on a truncated date column 'part_date'. And t= he only unique key in this table is a composite primary key on (id, part_da= te). And this merge queries ON condition is based on one of the columns i.e= ID which is the leading column of the PK.So it means it will ensure no dup= lication happens for ID values, but still we are seeing "duplicate key= " error. So what is the possible reason here or are we encountering an= y buggy behaviour here?

WITH source_data (c= ol1, col2, col3.....col29) AS (VALUES ($1, $2::date, $3::timestamptz, $4, $= 5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $= 21, $22, $23, $24, $25, $26, $27::timestamptz, $28, $29::timestamptz)) MERG= E INTO cpod.TAB1 AS target USING source_data AS source ON target.ID =3D sou= rce.ID WHEN MATCHED THEN UPDATE SET ....) WHEN NOT MATCHED THEN INSERT (...= ) VALUES (....);

Error:
ERROR: duplicate key value violates uniq= ue constraint "TAB1_pkey"
Detail: Key (ID, part_date)=3D(XXXXX= XXXX, 2024-11-04) already exists.


Regards
Yudhi

--000000000000b6199906269c8ba9--