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

  • SEARCH
  • Home
  • 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 742187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:43:10+00:00 2026-05-14T08:43:10+00:00

I have a table with approx 5 million rows which has a fk constraint

  • 0

I have a table with approx 5 million rows which has a fk constraint referencing the primary key of another table (also approx 5 million rows).

I need to delete about 75000 rows from both tables. I know that if I try doing this with the fk constraint enabled it’s going to take an unacceptable amount of time.

Coming from an Oracle background my first thought was to disable the constraint, do the delete & then reenable the constraint. PostGres appears to let me disable constraint triggers if I am a super user (I’m not, but I am logging in as the user that owns/created the objects) but that doesn’t seem to be quite what I want.

The other option is to drop the constraint and then reinstate it. I’m worried that rebuilding the constraint is going to take ages given the size of my tables.

Any thoughts?

edit: after Billy’s encouragement I’ve tried doing the delete without changing any constraints and it takes in excess of 10 minutes. However, I have discovered that the table from which I’m trying to delete has a self referential foreign key … duplicated (& non indexed).

Final update – I dropped the self referential foreign key, did my delete and added it back in. Billy’s right all round but unfortunately I can’t accept his comment as the answer!

  • 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-14T08:43:10+00:00Added an answer on May 14, 2026 at 8:43 am

    Per previous comments, it should be a problem. That said, there is a command that may be what you’re looking to – it’ll set the constraints to deferred so they’re checked on COMMIT, not on every delete. If you’re doing just one big DELETE of all the rows, it won’t make a difference, but if you’re doing it in pieces, it will.

    SET CONSTRAINTS ALL DEFERRED
    

    is what you are looking for in that case. Note that constraints must be marked as DEFERRABLE before they can be deferred. For example:

    ALTER TABLE table_name
      ADD CONSTRAINT constraint_uk UNIQUE(column_1, column_2)
      DEFERRABLE INITIALLY IMMEDIATE;
    

    The constraint can then be deferred in a transaction or function as follows:

    CREATE OR REPLACE FUNCTION f() RETURNS void AS
    $BODY$
    BEGIN
      SET CONSTRAINTS ALL DEFERRED;
    
      -- Code that temporarily violates the constraint...
      -- UPDATE table_name ...
    END;
    $BODY$
      LANGUAGE plpgsql VOLATILE
      COST 100;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly simple table with approx a million rows. id | my_col
I have a table that contains approx 10 million rows. This table is periodically
I have a table in which approx 100,000 rows are added every day. I
I have a view which displays content in a table (approx. 30 rows). I
I have a table (table variable in-fact) that holds several thousand (50k approx) rows
I have approx 10M rows of data across 20 tables, which I process once
I have table in which I am inserting rows for employee but next time
I have a set of approx 1 million rows (approx rowsize: 1.5kb) that needs
I have a huge table with more than one hundred million of rows and
So I have this gigantic table, containing approx 7 million records, in MS Access

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.