Thread: [pgsql-ru-general] duplicate key value violates unique constraint и создание дублей

Привет!
Мы в процессе переезда на новую базу с 9.4.8 -> 9.6.3.1 наше приложение начало бросать эксепшены: "duplicate key value violates unique constraint" при попытке сделать INSERT:
 
INSERT INTO items (ctime, mtime, pubdate, url, title, description, body, status, fulltext_status, orig_id, image_id, video_id, resource_id, priority, checksum) VALUES (%(ctime)s, %(mtime)s, %(pubdate)s, %(url)s, %(title)s, %(description)s, %(body)s, %(status)s, %(fulltext_status)s, %(orig_id)s, %(image_id)s, %(video_id)s, %(resource_id)s, %(priority)s, %(checksum)s) RETURNING items.id'
 
Колонка url - имеет unique constraint
 
 
Также при попытке обновить значение в поле status:
(psycopg2.IntegrityError) duplicate key value violates unique constraint "items_url"
    DETAIL:  Key (url)=(http://www.domainname.ru/ap_module/content/article/400-professional/140-professional/11880) already exists.
     [SQL: 'UPDATE items SET status=%(status)s WHERE items.id IN
 
 
Таблица:
 
 
     Column      |            Type             |                             Modifiers
-----------------+-----------------------------+-------------------------------------------------------------------
 id              | integer                     | not null default nextval(('public.items_id_seq'::text)::regclass)
 ctime           | timestamp without time zone | not null default now()
 pubdate         | timestamp without time zone | not null default now()
 resource_id     | integer                     | not null default 0
 url             | text                        |
 title           | text                        |
 description     | text                        |
 body            | text                        |
 status          | smallint                    | not null default 0
 image           | text                        |
 orig_id         | integer                     | not null default 0
 mtime           | timestamp without time zone | not null default now()
 checksum        | text                        |
 video_url       | text                        |
 audio_url       | text                        |
 content_type    | smallint                    | default 0
 author          | text                        |
 video           | text                        |
 fulltext_status | smallint                    | default 0
 summary         | text                        |
 image_id        | integer                     |
 video_id        | integer                     |
 priority        | smallint                    |
Indexes:
    "items_pkey" PRIMARY KEY, btree (id)
    "items_url" UNIQUE, btree (url)
    "items_resource_id" btree (resource_id)
    "ndx__items__ctime" btree (ctime)
    "ndx__items__image" btree (image_id)
    "ndx__items__mtime" btree (mtime)
    "ndx__items__pubdate" btree (pubdate)
    "ndx__items__video" btree (video_id)
Foreign-key constraints:
    "items_fkey1" FOREIGN KEY (image_id) REFERENCES images(id) ON UPDATE CASCADE ON DELETE SET NULL
    "items_fkey2" FOREIGN KEY (video_id) REFERENCES videos(id) ON UPDATE CASCADE ON DELETE SET NULL
Referenced by:
    TABLE "cluster_image" CONSTRAINT "cluster_image_fkey2" FOREIGN KEY (item_id) REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
    TABLE "cluster_meta" CONSTRAINT "cluster_meta_item_id_fkey" FOREIGN KEY (item_id) REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
    TABLE "events" CONSTRAINT "events_fkey2" FOREIGN KEY (item_id) REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
    TABLE "item_cluster" CONSTRAINT "item_cluster_fkey1" FOREIGN KEY (item_id) REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
    TABLE "items_blogs" CONSTRAINT "items_blogs_fkey1" FOREIGN KEY (item_id) REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
    TABLE "items_reflink" CONSTRAINT "items_reflink_fkey1" FOREIGN KEY (item_id) REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
    TABLE "items_related" CONSTRAINT "items_related_fkey1" FOREIGN KEY (item_id) REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
    TABLE "items_summaries" CONSTRAINT "items_summaries_fkey1" FOREIGN KEY (item_id) REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
    TABLE "quotations" CONSTRAINT "quotations_fkey3" FOREIGN KEY (item_id) REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
    TABLE "summaries" CONSTRAINT "summaries_fkey2" FOREIGN KEY (item_id) REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
 
 
Все было бы не так плохо, если бы в табличке не начали появлятся дубли записей.
 
 
Есть у кого-нибудь объяснения почему такое могло произойти?
Спасибо.
 
 
-- 
Пожалуйста!
Используйте кнопку "ответить всем".
Не удаляйте историю переписки.
Спасибо. С уважением, Timokhin 'maf' Maxim
 
Доброго!

Интересно разобрались сами?

Может "кто-то" отключал уникальность (для ускорения больших вставок)?

29.06.2017, Timokhin Maxim<ncx2@yandex.com> написал(а):
> Привет!
> Мы в процессе переезда на новую базу с 9.4.8 -> 9.6.3.1 наше приложение
> начало бросать эксепшены: "duplicate key value violates unique constraint"
> при попытке сделать INSERT:
>
> INSERT INTO items (ctime, mtime, pubdate, url, title, description, body,
> status, fulltext_status, orig_id, image_id, video_id, resource_id, priority,
> checksum) VALUES (%(ctime)s, %(mtime)s, %(pubdate)s, %(url)s, %(title)s,
> %(description)s, %(body)s, %(status)s, %(fulltext_status)s, %(orig_id)s,
> %(image_id)s, %(video_id)s, %(resource_id)s, %(priority)s, %(checksum)s)
> RETURNING items.id'
>
> Колонка url - имеет unique constraint
>
>
> Также при попытке обновить значение в поле status:
> (psycopg2.IntegrityError) duplicate key value violates unique constraint
> "items_url"
>     DETAIL:  Key
> (url)=(http://www.domainname.ru/ap_module/content/article/400-professional/140-professional/11880)
> already exists.
>      [SQL: 'UPDATE items SET status=%(status)s WHERE items.id IN
>
>
> Таблица:
>
>
>      Column      |            Type             |
> Modifiers
> -----------------+-----------------------------+-------------------------------------------------------------------
>  id              | integer                     | not null default
> nextval(('public.items_id_seq'::text)::regclass)
>  ctime           | timestamp without time zone | not null default now()
>  pubdate         | timestamp without time zone | not null default now()
>  resource_id     | integer                     | not null default 0
>  url             | text                        |
>  title           | text                        |
>  description     | text                        |
>  body            | text                        |
>  status          | smallint                    | not null default 0
>  image           | text                        |
>  orig_id         | integer                     | not null default 0
>  mtime           | timestamp without time zone | not null default now()
>  checksum        | text                        |
>  video_url       | text                        |
>  audio_url       | text                        |
>  content_type    | smallint                    | default 0
>  author          | text                        |
>  video           | text                        |
>  fulltext_status | smallint                    | default 0
>  summary         | text                        |
>  image_id        | integer                     |
>  video_id        | integer                     |
>  priority        | smallint                    |
> Indexes:
>     "items_pkey" PRIMARY KEY, btree (id)
>     "items_url" UNIQUE, btree (url)
>     "items_resource_id" btree (resource_id)
>     "ndx__items__ctime" btree (ctime)
>     "ndx__items__image" btree (image_id)
>     "ndx__items__mtime" btree (mtime)
>     "ndx__items__pubdate" btree (pubdate)
>     "ndx__items__video" btree (video_id)
> Foreign-key constraints:
>     "items_fkey1" FOREIGN KEY (image_id) REFERENCES images(id) ON UPDATE
> CASCADE ON DELETE SET NULL
>     "items_fkey2" FOREIGN KEY (video_id) REFERENCES videos(id) ON UPDATE
> CASCADE ON DELETE SET NULL
> Referenced by:
>     TABLE "cluster_image" CONSTRAINT "cluster_image_fkey2" FOREIGN KEY
> (item_id) REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
>     TABLE "cluster_meta" CONSTRAINT "cluster_meta_item_id_fkey" FOREIGN KEY
> (item_id) REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
>     TABLE "events" CONSTRAINT "events_fkey2" FOREIGN KEY (item_id)
> REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
>     TABLE "item_cluster" CONSTRAINT "item_cluster_fkey1" FOREIGN KEY
> (item_id) REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
>     TABLE "items_blogs" CONSTRAINT "items_blogs_fkey1" FOREIGN KEY (item_id)
> REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
>     TABLE "items_reflink" CONSTRAINT "items_reflink_fkey1" FOREIGN KEY
> (item_id) REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
>     TABLE "items_related" CONSTRAINT "items_related_fkey1" FOREIGN KEY
> (item_id) REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
>     TABLE "items_summaries" CONSTRAINT "items_summaries_fkey1" FOREIGN KEY
> (item_id) REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
>     TABLE "quotations" CONSTRAINT "quotations_fkey3" FOREIGN KEY (item_id)
> REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
>     TABLE "summaries" CONSTRAINT "summaries_fkey2" FOREIGN KEY (item_id)
> REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
>
>
> Все было бы не так плохо, если бы в табличке не начали появлятся дубли
> записей.
>
>
> Есть у кого-нибудь объяснения почему такое могло произойти?
> Спасибо.
>
>
> --
> Пожалуйста!
> Используйте кнопку "ответить всем".
> Не удаляйте историю переписки.
> Спасибо. С уважением, Timokhin 'maf' Maxim
>


-- 
---
С уважением,
Михаил Наседкин

Привет! Проблема была инетересная.
По непоятной причине индекс побился при репликации. Но тем не менее он не был помечен как битый.
В итоге все решилось DROP INDEX; CREATE UNIQUE INDEX

-- 
Пожалуйста!
Используйте кнопку "ответить всем".
Не удаляйте историю переписки.
Спасибо. С уважением, Timokhin 'maf' Maxim



25.07.2017, 07:27, "Михаил" <m.nasedkin@gmail.com>:
> Доброго!
>
> Интересно разобрались сами?
>
> Может "кто-то" отключал уникальность (для ускорения больших вставок)?
>
> 29.06.2017, Timokhin Maxim<ncx2@yandex.com> написал(а):
>>  Привет!
>>  Мы в процессе переезда на новую базу с 9.4.8 -> 9.6.3.1 наше приложение
>>  начало бросать эксепшены: "duplicate key value violates unique constraint"
>>  при попытке сделать INSERT:
>>
>>  INSERT INTO items (ctime, mtime, pubdate, url, title, description, body,
>>  status, fulltext_status, orig_id, image_id, video_id, resource_id, priority,
>>  checksum) VALUES (%(ctime)s, %(mtime)s, %(pubdate)s, %(url)s, %(title)s,
>>  %(description)s, %(body)s, %(status)s, %(fulltext_status)s, %(orig_id)s,
>>  %(image_id)s, %(video_id)s, %(resource_id)s, %(priority)s, %(checksum)s)
>>  RETURNING items.id'
>>
>>  Колонка url - имеет unique constraint
>>
>>  Также при попытке обновить значение в поле status:
>>  (psycopg2.IntegrityError) duplicate key value violates unique constraint
>>  "items_url"
>>      DETAIL:  Key
>>  (url)=(http://www.domainname.ru/ap_module/content/article/400-professional/140-professional/11880)
>>  already exists.
>>       [SQL: 'UPDATE items SET status=%(status)s WHERE items.id IN
>>
>>  Таблица:
>>
>>       Column      |            Type             |
>>  Modifiers
>>  -----------------+-----------------------------+-------------------------------------------------------------------
>>   id              | integer                     | not null default
>>  nextval(('public.items_id_seq'::text)::regclass)
>>   ctime           | timestamp without time zone | not null default now()
>>   pubdate         | timestamp without time zone | not null default now()
>>   resource_id     | integer                     | not null default 0
>>   url             | text                        |
>>   title           | text                        |
>>   description     | text                        |
>>   body            | text                        |
>>   status          | smallint                    | not null default 0
>>   image           | text                        |
>>   orig_id         | integer                     | not null default 0
>>   mtime           | timestamp without time zone | not null default now()
>>   checksum        | text                        |
>>   video_url       | text                        |
>>   audio_url       | text                        |
>>   content_type    | smallint                    | default 0
>>   author          | text                        |
>>   video           | text                        |
>>   fulltext_status | smallint                    | default 0
>>   summary         | text                        |
>>   image_id        | integer                     |
>>   video_id        | integer                     |
>>   priority        | smallint                    |
>>  Indexes:
>>      "items_pkey" PRIMARY KEY, btree (id)
>>      "items_url" UNIQUE, btree (url)
>>      "items_resource_id" btree (resource_id)
>>      "ndx__items__ctime" btree (ctime)
>>      "ndx__items__image" btree (image_id)
>>      "ndx__items__mtime" btree (mtime)
>>      "ndx__items__pubdate" btree (pubdate)
>>      "ndx__items__video" btree (video_id)
>>  Foreign-key constraints:
>>      "items_fkey1" FOREIGN KEY (image_id) REFERENCES images(id) ON UPDATE
>>  CASCADE ON DELETE SET NULL
>>      "items_fkey2" FOREIGN KEY (video_id) REFERENCES videos(id) ON UPDATE
>>  CASCADE ON DELETE SET NULL
>>  Referenced by:
>>      TABLE "cluster_image" CONSTRAINT "cluster_image_fkey2" FOREIGN KEY
>>  (item_id) REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
>>      TABLE "cluster_meta" CONSTRAINT "cluster_meta_item_id_fkey" FOREIGN KEY
>>  (item_id) REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
>>      TABLE "events" CONSTRAINT "events_fkey2" FOREIGN KEY (item_id)
>>  REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
>>      TABLE "item_cluster" CONSTRAINT "item_cluster_fkey1" FOREIGN KEY
>>  (item_id) REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
>>      TABLE "items_blogs" CONSTRAINT "items_blogs_fkey1" FOREIGN KEY (item_id)
>>  REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
>>      TABLE "items_reflink" CONSTRAINT "items_reflink_fkey1" FOREIGN KEY
>>  (item_id) REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
>>      TABLE "items_related" CONSTRAINT "items_related_fkey1" FOREIGN KEY
>>  (item_id) REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
>>      TABLE "items_summaries" CONSTRAINT "items_summaries_fkey1" FOREIGN KEY
>>  (item_id) REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
>>      TABLE "quotations" CONSTRAINT "quotations_fkey3" FOREIGN KEY (item_id)
>>  REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
>>      TABLE "summaries" CONSTRAINT "summaries_fkey2" FOREIGN KEY (item_id)
>>  REFERENCES items(id) ON UPDATE CASCADE ON DELETE CASCADE
>>
>>  Все было бы не так плохо, если бы в табличке не начали появлятся дубли
>>  записей.
>>
>>  Есть у кого-нибудь объяснения почему такое могло произойти?
>>  Спасибо.
>>
>>  --
>>  Пожалуйста!
>>  Используйте кнопку "ответить всем".
>>  Не удаляйте историю переписки.
>>  Спасибо. С уважением, Timokhin 'maf' Maxim
>
> --
> ---
> С уважением,
> Михаил Наседкин


> Привет! Проблема была инетересная.
> По непоятной причине индекс побился при репликации. Но тем не менее он не был помечен как битый.
> В итоге все решилось DROP INDEX; CREATE UNIQUE INDEX

на какой конкретно версии были индексы поломаны при том что помечены
как не поломанные?

я просто совсем недавно обновлял постгри между минорами 9.5 из за
сходных проблем.
то что Вы говорите о проблемах в 9.6 как бы настораживает

--

. ''`.            Dmitry E. Oboukhov <unera@debian.org>
: :’  :
`. `~’               GPG key: 4096R/08EEA756 2014-08-30
  `- 71ED ACFC 6801 0DD9 1AD1  9B86 8D1F 969A 08EE A756

Attachment