Rows violating Foreign key constraint exists

From: Nandakumar M <m(dot)nanda92(at)gmail(dot)com>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Rows violating Foreign key constraint exists
Date: 2019-11-28 13:25:19
Message-ID: CANcFUu45XyGTghD0C8H-y3ySU97YA2PB+4Qyvw9C4Tr6hqY5cw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

I am using PG version 10.5.

Saw a table where we have foreign key defined but few thousand rows
violate the foreign key constraint.

I understand that one possibility of this happening is if we had
manually disabled the triggers that do FK integrity checks and re
enabled them afterwards. Is there any way to confirm if this has
happened -- does PG internally maintain something like an audit
history of DDL changes?

I am not sure if this is due to some mistake in our end or if there
are any known issues in PG 10.5 that would cause this.

I tried the ALTER TABLE ... VALIDATE CONSTRAINT for this particular FK
and it doesn't report any errors. The documentation pretty clearly
mentions that 'VALIDATE CONSTRAINT' is used only to check those
constraints created with 'NOT VALID' clause.

It might be useful to have an option to check integrity of any FK (not
just ones created with NOT VALID clause). Please let me know if there
is already any way to do this.

Also, is there any way to make sure the FK checking trigger can never
be disabled (so that such a case will never arise)?

How do I proceed from here - Do I just delete the inconsistent rows or
is there something more I have to do?

Thanks for your help.

Regards,
Nanda

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Laurenz Albe 2019-11-28 14:30:23 Re: Rows violating Foreign key constraint exists
Previous Message Alban Hertroys 2019-11-28 08:03:10 Re: Range contains element filter not using index of the element column