Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iaKoV-0001CK-H6 for pgsql-general@arkaria.postgresql.org; Thu, 28 Nov 2019 14:30:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iaKoU-00056K-4w for pgsql-general@arkaria.postgresql.org; Thu, 28 Nov 2019 14:30:34 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iaKoT-00053F-Li for pgsql-general@lists.postgresql.org; Thu, 28 Nov 2019 14:30:33 +0000 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iaKoM-0000rv-Ik for pgsql-general@lists.postgresql.org; Thu, 28 Nov 2019 14:30:32 +0000 Received: by mail-wr1-x444.google.com with SMTP id z3so31356371wru.3 for ; Thu, 28 Nov 2019 06:30:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=0+KRdwb6i1IChFF0GSQKXlerU3SVzONtLs45GLtagRA=; b=Fwqv7B23E3nMYeXRoYO9oZpL991g0tkKwisu/oZt6Z5iwrGiInfMpEVHemB4gtBvKE CHisx/0nzD/n2dld337mazPhMjgrOzCImGxw+9LsMieqWIIBJug/AcHhQq9t3l3EglZq 6+fpApBX0bAzujBfpH+aEXjZpbZQNLeL1a8mIqPGwnKZ3gJloMnpSIupdClKYtCShLHH h7PVzg6w8hK9wPs3QY1EtPGHm/OhdnWpv4xOMmMlE6RxSJARESBX0MH7tJWOs1fh/xxP iSpviW9InWtY0hneztWxnvpuzdvDFEpPTpaQb0d3sFl+t45etqR66oU26YH7hdBdwUoa 3IuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=0+KRdwb6i1IChFF0GSQKXlerU3SVzONtLs45GLtagRA=; b=FHsVKnw/bP6yAou02O42fZU8TFALtH1M6nh4SXP3mo8cEdlNoJroPyMyYLlzkIE28i zEmOgiJ+sIXX9VuDFOuFqwFioBvFQYFWYHibRu9uUUIe/OOp5UyQaxWgNfeckDqHyaWr Ga1/gEkAdtlSHLlgFr2GHRSBOjSFIVwBuPMsFVjzYR5vuvqOvUF7OtqgDBwLzY5h02fE 49jFIFAUFjZTNHOvdxY+wlA1zdtg44Sf/QhsuzbOlS0tKHrGgkCEzQq/X8yYHoc5hEEm oPrGTWj7DfMLJDM5ED0V//ogUkKFv0YORybd3zzqg5noPeA931h2HOa+EbR08x8xnxsj 0htw== X-Gm-Message-State: APjAAAVrnOxopjDFikjaOre90AqUE6E0aqcV/PH4lj7O6wMkrYOFIRMk EeFEr4aTmj9/k3HMsdykIRGkFw== X-Google-Smtp-Source: APXvYqzXpyrSMRMc6mVn3ufWMpVMLUiSYGRTSn7Jhkl1cGlAwHTpVCM10alMPymrk69WpNwAV4Pftw== X-Received: by 2002:adf:fe4d:: with SMTP id m13mr25405558wrs.179.1574951424696; Thu, 28 Nov 2019 06:30:24 -0800 (PST) Received: from localhost.localdomain (217-149-168-19.nat.highway.telekom.at. [217.149.168.19]) by smtp.gmail.com with ESMTPSA id f2sm3789894wmh.46.2019.11.28.06.30.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Nov 2019 06:30:24 -0800 (PST) Message-ID: Subject: Re: Rows violating Foreign key constraint exists From: Laurenz Albe To: Nandakumar M , pgsql-general@lists.postgresql.org Date: Thu, 28 Nov 2019 15:30:23 +0100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.32.5 (3.32.5-1.fc30) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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