Re: how could duplicate pkey exist in psql?

From: Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>
To: Yan Chunlu <springrider(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org, Edson Richter <richter(at)simkorp(dot)com(dot)br>
Subject: Re: how could duplicate pkey exist in psql?
Date: 2011-11-22 02:35:48
Message-ID: 201111211835.49316.adrian.klaver@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Monday, November 21, 2011 4:53:21 pm Yan Chunlu wrote:
> and database will stop receiving the following data after detected an
> error?
> that means while using pg_restore, no error allowed to happen, otherwise
> the database will stop receiving data and the import will fail.
>
> I found only one record in psql's log:
>
> duplicate key value violates unique constraint "account_pkey"
>
> does that means one duplicate record will prevent all other records to
> import?

For that table yes. Though if that table is the parent in FK relationships with
other tables, those tables will fail to import also because the keys they refer
to do not exist.

To get around this you have several options:
1) Find the duplicate entry(s) in the original table and eliminate them before
dumping.
2) Dump the table by itself to a plain text format and eliminate the
duplicate(s) in the plain text file before restoring.
3) By default pg_dump uses COPY to load data into tables. As you have found out
that runs as a single transaction and rollbacks if there is an error. You can
specify --insert to the pg_dump command to get it to output INSERT(s) for each
row. The up side is each INSERT is a separate transaction. The down side is if
there is a lot of data it will take a long time to load because each INSERT is a
separate transaction.
4) Use pgloader (http://pgfoundry.org/projects/pgloader/). It is a Python
program that 'manages' COPY. It will kick out bad rows and keep loading data.

--
Adrian Klaver
adrian(dot)klaver(at)gmail(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Pavel Stehule 2011-11-22 04:14:03 Re: stored function data structures - difficulty
Previous Message Tom Lane 2011-11-22 02:17:54 Re: successive select statements