Subject: Re: Duplicate Key Values

From: Adrian Klaver <[email-masked]>
Date: Thu, 6 Mar 2025 10:22:05 -0800

On 3/6/25 10:11, mark bradley wrote:
> Here is the table definition:
> 
> 
> And here is the error message I get when I try to delete a duplicate:

Please answer the following:

1) Did you not see duplicates with the old version of pgAdmin4?

2) What do you see if you use psql?

3) Did you upgrade/move the Postgres server or the underlying OS?

4) Have you tried reindexing the node_id field?

> 
> 
> Mark Brady,
> _amazon.com/author/markjbrady <https://amazon.com/author/markjbrady>_
> ------------------------------------------------------------------------

-- 
Adrian Klaver
[email-masked]

---

From: Adrian Klaver <[email-masked]>
Date: Thu, 6 Mar 2025 12:00:58 -0800

On 3/6/25 10:36, mark bradley wrote:
Reply to list also.
Ccing list.
> I did see it in my last update but have done a couple, so it should have 
> happened then.
> 
> Here is the PSQL output:

Supply information as copy and paste text.

> No
> No, but I'll try that.
> *From:* Adrian Klaver <[email-masked]>
> *Sent:* Thursday, March 6, 2025 1:22 PM
> *To:* mark bradley <[email-masked]>; Ron Johnson 
> <[email-masked]>; pgsql-general <[email-masked]>
> *Subject:* Re: Duplicate Key Values
>> _amazon.com/author/markjbrady <https://amazon.com/author/markjbrady 
> <https://amazon.com/author/markjbrady>>_

---

From: Adrian Klaver <[email-masked]>
Date: Thu, 6 Mar 2025 12:34:59 -0800

On 3/6/25 12:06, mark bradley wrote:

My mistake I forgot to Cc list on my previous post, which was:

That would be an issue and also would mean it is not a PK.

In psql  do:

\d dataset

and show the results as text in your reply.

Ccing list

The below shows there is an index("dataset_pkey") on node_id.

Note, reindexing will take a lock on the table that prevents changing 
data while the operation is running. See the below for more information:

https://www.postgresql.org/docs/current/sql-reindex.html

If the table is not to big and you can interrupt access to it then the 
simplest command to run would be:

REINDEX  TABLE dataset;

> Universal Metadata Schema=# \d dataset
>                                   Table "public.dataset"
>            Column           |           Type            | Collation | 
> Nullable | Defau
> lt
> ---------------------------+---------------------------+-----------+----------+------
> ---
>   node_id                   | integer                   |           | 
> not null |
>   dataset_name              | character varying(25)     |           | 
> not null |
>   notes                     | text                      |           |   
>         |
>   dataset_type              | database_type             |           | 
> not null |
>   dataset_maturity          | database_maturity_type    |           | 
> not null |
>   disposition               | disposition_type          |           | 
> not null |
>   start_date                | date                      |           |   
>         |
>   end_date                  | date                      |           |   
>         |
>   most_recent_update        | date                      |           |   
>         |
>   update_periodicity        | interval                  |           |   
>         |
>   system_of_record          | text                      |           |   
>         |
>   point_of_contact          | integer                   |           | 
> not null |
>   dataset_url               | text                      |           |   
>         |
>   classification_level      | classification_level_type |           | 
> not null |
>   physical_location         | text                      |           |   
>         |
>   quality_control           | yes_no_type               |           | 
> not null |
>   dataset_documentation_url | text                      |           | 
> not null |
>   description               | text                      |           |   
>         |
>   node_type                 | node_type                 |           |   
>         |
>   dummy                     | integer                   |           |   
>         |
> Indexes:
>      "dataset_pkey" PRIMARY KEY, btree (node_id)
> Foreign-key constraints:
>      "node_id" FOREIGN KEY (node_id) REFERENCES node(node_id) NOT VALID
>      "poc" FOREIGN KEY (point_of_contact) REFERENCES poc(poc_id) NOT VALID
> Referenced by:
>      TABLE "dataset_table" CONSTRAINT "dataset" FOREIGN KEY (node_id) 
> REFERENCES datas
> et(node_id) NOT VALID
>      TABLE "dataset_subject" CONSTRAINT "dataset_subject_node_id_fkey" 
> FOREIGN KEY (no
> de_id) REFERENCES dataset(node_id)
>      TABLE "system_dataset" CONSTRAINT "system_dataset_node_id_fkey" 
> FOREIGN KEY (node
> _id) REFERENCES dataset(node_id) NOT VALID
> Inherits: node
> 
> 
> Best regards,
> Mark Brady
> *Sent:* Thursday, March 6, 2025 3:03 PM
> *To:* mark bradley <[email-masked]>
> On 3/6/25 10:51, mark bradley wrote:
> Reply to list alos.
> Ccing list.
> 
>> Looks like there is no index on node_id at the moment
> 
> That would be an issue and also would mean it is not a PK.
> 
> In psql  do:
> 
> \d dataset
> 
> and show the results as text in your reply.
> 
>> 
>> 
>> Mark Brady, Ph.D.
>> Deputy Chief Data Officer, TRMC
>> <https://amazon.com/author/markjbrady 
> <https://amazon.com/author/markjbrady>>>_

---

From: mark bradley <[email-masked]>
Date: Fri, 7 Mar 2025 14:34:49 +0000

This is what MS Copilot has to say about this apparent bug where Postgres inserts extra rows violating a primary keys uniqueness constraint:

Yes, this issue has been encountered by others. There are a few potential reasons why this might happen:

  1.
Sequence Out of Sync: Sometimes, the sequence that generates unique values for the primary key can become out of sync, especially after a bulk import or a database restore. You can check if the sequence is out of sync and reset it if necessary.
  2.
Index Corruption: Index corruption can occur due to various reasons, such as hardware failures or bugs in earlier versions of PostgreSQL. This can lead to duplicate primary keys being inserted.
  3.
Table Inheritance: If you are using table inheritance, primary keys are not enforced among inherited tables. This can lead to duplicates if not handled correctly.
  4.
Application Logic: Sometimes, the application logic might inadvertently insert duplicate records. Reviewing the application code and insert statements can help identify and resolve such issues.

To resolve the issue, you can:

  *
Check and reset the sequence if it's out of sync.
  *
Rebuild the index if it's corrupted.

Any of the first 3 could be involved.  There isn't an application involved other than pgAdmin.

  1.
Originally, the key in the node table was a sequence, but I changed it to a non-sequence.
  2.
There is no index on the primary key node_id, and I understand there should be one.
  3.
I didn't explicitly use Postgres inheritance but there are two tables that are subclasses of node.  There are dataset nodes and processing_node [s] tables.  Each is a type of node and have primary keys that are foreign keys from the node table.  This key is node_id.

What to do?  I hesitate to just delete my tables and start over because this error will reoccur.

Best regards,
Mark Brady
amazon.com/author/markjbrady<https://amazon.com/author/markjbrady>
________________________________
From: Adrian Klaver <[email-masked]>
Sent: Thursday, March 6, 2025 3:34 PM
To: mark bradley <[email-masked]>
Cc: pgsql-general <[email-masked]>
Subject: Re: Duplicate Key Values

In psql  do:

\d dataset

Ccing list

REINDEX  TABLE dataset;

---

From: mark bradley <[email-masked]>
Date: Fri, 7 Mar 2025 14:55:18 +0000

Here are some of the references from Copilot

https://dba.stackexchange.com/questions/62675/why-does-my-table-hold-duplicate-primary-keys

https://stackoverflow.com/questions/55551461/having-duplicate-rows-on-a-primary-key-and-unique-constraints-in-postgres

Best regards,
Mark Brady
amazon.com/author/markjbrady<https://amazon.com/author/markjbrady>
________________________________
From: mark bradley <[email-masked]>
Sent: Friday, March 7, 2025 9:34 AM
To: Adrian Klaver <[email-masked]>
Cc: pgsql-general <[email-masked]>
Subject: Re: Duplicate Key Values

  1.

  *

  1.

Best regards,
Mark Brady

In psql  do:

\d dataset

Ccing list

REINDEX  TABLE dataset;

---

From: Greg Sabino Mullane <[email-masked]>
Date: Fri, 7 Mar 2025 10:52:40 -0500

On Fri, Mar 7, 2025 at 9:35 AM mark bradley <[email-masked]> wrote:

> This is what MS Copilot has to say about this apparent bug where Postgres
> inserts extra rows violating a primary keys uniqueness constraint:
>
>    1. *Sequence Out of Sync*: Sometimes, the sequence that generates
>    unique values for the primary key can become out of sync, especially after
>    a bulk import or a database restore. You can check if the sequence is out
>    of sync and reset it if necessary.
>    2. *Index Corruption*: Index corruption can occur due to various
>    reasons, such as hardware failures or bugs in earlier versions of
>    PostgreSQL. This can lead to duplicate primary keys being inserted.
>    3. *Table Inheritance*: If you are using table inheritance, primary
>    keys are not enforced among inherited tables. This can lead to duplicates
>    if not handled correctly.
>    4. *Application Logic*: Sometimes, the application logic might
>    inadvertently insert duplicate records. Reviewing the application code and
>    insert statements can help identify and resolve such issues.
>
> This is AI gobbledygook, and can be ignored. The only real option
is number 2 (index corruption).

>    1. There is no index on the primary key *node_id*, and I understand
>    there should be one.
>
>
There is an index, as your table definition showed.

What to do?  I hesitate to just delete my tables and start over because
> this error will reoccur.

The error should not reoccur. At least, a normal Postgres system will
prevent this from happening in the first place. To clean it up, carefully
run the below. If an error appears, or something does not look right,
rollback and stop.

-- Encourage not using indexes:
set enable_indexscan = 0;
set enable_bitmapscan = 0;
set enable_indexonlyscan = 0;

-- Sanity check. This should return a number greater than 1. If not, stop.
set search_path = public;
select count(*) from dataset where node_id = 26;

-- Make a backup:
create table dataset_backup as select * from dataset;

-- Test out the process on a subset of the data:
create table test_dataset as select * from dataset where node_id < 30;
create table test_dataset_duperows_20250307 (like dataset);
begin;
set local session_replication_role = 'replica';
with goodctids as (select min(ctid) from TEST_dataset group by node_id)
 , mydelete as (delete from TEST_dataset where not exists (select 1 from
goodctids where min=ctid)
   returning *)
insert into test_dataset_duperows_20250307 select * from mydelete;
reset session_replication_role;
commit;

-- STOP HERE and examine the test_dataset and
test_dataset_duperows_20250307 tables
--
-- If ZERO rows were deleted, then you should no go further,
-- as some of the underlying assumptions must be wrong.

-- Do the real table:
create table dataset_duperows_20250307 (like dataset);
begin;
set local session_replication_role = 'replica';
with goodctids as (select min(ctid) from dataset group by node_id)
  , mydelete as (delete from dataset where not exists (select 1 from
goodctids where min=ctid)
    returning *)
insert into dataset_duperows_20250307 select * from mydelete;
reset session_replication_role;
commit;

-- Rebuild the index
reindex index concurrently dataset_pkey;

-- Put things back from good measure:
reset enable_indexscan;
reset enable_bitmapscan;
reset enable_indexonlyscan;

drop table test_dataset;
drop table test_dataset_duperows;

Given the issues, I would keep the dataset_backup table around for a while.
And make sure your backups are running and up to date.

You might also want to reindex all the tables in your database, to see if
any other issues are lurking.

Check your Postgres logs closely to see if anything else unusual has
appeared.

Look over your OS logs to see if there are clues as to how the corruption
happened. Maybe you recently upgraded your OS?

Cheers,
Greg

--
Crunchy Data - https://www.crunchydata.com
Enterprise Postgres Software Products & Tech Support

---

From: Adrian Klaver <[email-masked]>
Date: Fri, 7 Mar 2025 07:55:13 -0800

On 3/7/25 06:34, mark bradley wrote:
> This is what MS Copilot has to say about this apparent bug where 
> Postgres inserts extra rows violating a primary keys uniqueness constraint:

> What to do?  I hesitate to just delete my tables and start over because 

Let the AI solve it.

---

From: mark bradley <[email-masked]>
Date: Fri, 7 Mar 2025 19:47:36 +0000

Wouldn't that be nice 🙂

Best regards,
Mark Brady
Sent: Friday, March 7, 2025 10:55 AM

Let the AI solve it.

---

From: Adrian Klaver <[email-masked]>
Date: Fri, 7 Mar 2025 12:25:51 -0800

> Wouldn't that be nice 🙂

No, because you would end up with a mess.

My AI rant:

AI is neither artificial or intelligent. It is human code that pattern 
matches and then throws the matches against the wall and hope something 
sticks. It is left to the human to clean up.

At any rate, in this post:

https://www.postgresql.org/message-id/75b33741-ee99-4524-b63a-edad21c1266d%40aklaver.com

You where provided an answer, which is further extended here:

https://www.postgresql.org/message-id/CAKAnmm%2BBBBaXGN2xPHhXywkwb72UWzinWu2wQ5WadcMw3_57rQ%40mail.gmail.com

> *Sent:* Friday, March 7, 2025 10:55 AM
> *Cc:* pgsql-general <[email-masked]>