From: | Greg Sabino Mullane <htamfids(at)gmail(dot)com> |
---|---|
To: | mark bradley <markbradyju(at)outlook(dot)com> |
Cc: | Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>, pgsql-general <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: Duplicate Key Values |
Date: | 2025-03-07 15:52:40 |
Message-ID: | CAKAnmm+BBBaXGN2xPHhXywkwb72UWzinWu2wQ5WadcMw3_57rQ@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On Fri, Mar 7, 2025 at 9:35 AM mark bradley <markbradyju(at)outlook(dot)com> wrote:
> 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.
>
> 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 | Date | Subject | |
---|---|---|---|
Next Message | Adrian Klaver | 2025-03-07 15:55:13 | Re: Duplicate Key Values |
Previous Message | Atapys Sharsh | 2025-03-07 15:25:54 | Changing column filter for existing publication with WAL backlog |