pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Nandakumar M <m.nanda92@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Rows violating Foreign key constraint exists
Date: Thu, 28 Nov 2019 15:30:23 +0100
Message-ID: <a193fa767847c1c3d8677b7ff4cb56880d6bece1.camel@cybertec.at> (raw)
In-Reply-To: <CANcFUu45XyGTghD0C8H-y3ySU97YA2PB+4Qyvw9C4Tr6hqY5cw@mail.gmail.com>
References: <CANcFUu45XyGTghD0C8H-y3ySU97YA2PB+4Qyvw9C4Tr6hqY5cw@mail.gmail.com>

On Thu, 2019-11-28 at 18:55 +0530, Nandakumar M wrote:
> 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.

It could be that somebody disabled the triggers, but that would have to
be a superuser.  And I hope that people randomly disabling system triggers
on tables don't have superuser access to your database.

There is no way to ascertain that that did happen.
It could be in the log if you have "log_statement = 'ddl'" and keep old
logs around.

The other option is that you are suppering from data corruption, perhaps
because of a software bug, but most likely because of hardware problems.

If you don't know better, assume the worst.

I would test the hardware for problems.
Once you are sure the hardware is fine, manually fix the corruption
by deleting rows that violate the constraint.
Then create a new PostgreSQL cluster with "initdb", dump the original
database with "pg_dumpall" and restore it to the new cluster.
That should get rid of all data corruption.

Make sure you upgrade to 10.11.

Yours,
Laurenz Albe

-- 
Cybertec | https://www.cybertec-postgresql.com






view thread (9+ messages)  latest in thread

Message-ID: <a193fa767847c1c3d8677b7ff4cb56880d6bece1.camel@cybertec.at>
Permalink:  ../a193fa767847c1c3d8677b7ff4cb56880d6bece1.camel@cybertec.at/
Also on:    postgresql.org/message-id/a193fa767847c1c3d8677b7ff4cb56880d6bece1.camel@cybertec.at

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: laurenz.albe@cybertec.at, m.nanda92@gmail.com
  Subject: Re: Rows violating Foreign key constraint exists
  In-Reply-To: <a193fa767847c1c3d8677b7ff4cb56880d6bece1.camel@cybertec.at>
  References: <CANcFUu45XyGTghD0C8H-y3ySU97YA2PB+4Qyvw9C4Tr6hqY5cw@mail.gmail.com> <a193fa767847c1c3d8677b7ff4cb56880d6bece1.camel@cybertec.at>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox