Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 650081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:00:51+00:00 2026-05-13T22:00:51+00:00

I am doing a bulk insert of records into a database from a log

  • 0

I am doing a bulk insert of records into a database from a log file. Occasionally (~1 row out of every thousand) one of the rows violates the primary key and causes the transaction to fail. Currently, the user has to manually go through the file that caused the failure and remove the offending row before attempting to re-import. Given that there are hundreds of these files to import it is impractical.

My question: How can I skip the insertion of records that will violate the primary key constraint, without having to do a SELECT statement before each row to see if it already exists?

Note: I am aware of the very similar question #1054695, but it appears to be a SQL Server specific answer and I am using PostgreSQL (importing via Python/psycopg2).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:00:51+00:00Added an answer on May 13, 2026 at 10:00 pm

    You can also use SAVEPOINTs in a transaction.

    Pythonish pseudocode is illustrate from the application side:

    database.execute("BEGIN")
    foreach data_row in input_data_dictionary:
        database.execute("SAVEPOINT bulk_savepoint")
        try:
            database.execute("INSERT", table, data_row)
        except:
            database.execute("ROLLBACK TO SAVEPOINT bulk_savepoint")
            log_error(data_row)
            error_count = error_count + 1
        else:
            database.execute("RELEASE SAVEPOINT bulk_savepoint")
    
    if error_count > error_threshold:
        database.execute("ROLLBACK")
    else:
        database.execute("COMMIT")
    

    Edit: Here’s an actual example of this in action in psql based on a slight variation of the example in the documentation (SQL statements prefixed by “>”):

    > CREATE TABLE table1 (test_field INTEGER NOT NULL PRIMARY KEY);
    NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "table1_pkey" for table "table1"
    CREATE TABLE
    
    > BEGIN;
    BEGIN
    > INSERT INTO table1 VALUES (1);
    INSERT 0 1
    > SAVEPOINT my_savepoint;
    SAVEPOINT
    > INSERT INTO table1 VALUES (1);
    ERROR:  duplicate key value violates unique constraint "table1_pkey"
    > ROLLBACK TO SAVEPOINT my_savepoint;
    ROLLBACK
    > INSERT INTO table1 VALUES (3);
    INSERT 0 1
    > COMMIT;
    COMMIT
    > SELECT * FROM table1;  
     test_field 
    ------------
              1
              3
    (2 rows)
    

    Note that the value 3 was inserted after the error, but still inside the same transaction!

    The documentation for SAVEPOINT is at http://www.postgresql.org/docs/8.4/static/sql-savepoint.html.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.