Received: from localhost (unknown [200.46.204.183]) by developer.postgresql.org (Postfix) with ESMTP id 7DF242E00BE for ; Mon, 9 Jun 2008 21:26:46 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 77159-01-4 for ; Mon, 9 Jun 2008 21:26:22 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from outmail136171.authsmtp.net (outmail136171.authsmtp.net [62.13.136.171]) by developer.postgresql.org (Postfix) with ESMTP id 37E202E0083 for ; Mon, 9 Jun 2008 21:12:59 -0300 (ADT) Received: from mail.authsmtp.com (mail.authsmtp.com [62.13.128.187]) by punt6.authsmtp.com (8.14.2/8.14.2/Kp) with ESMTP id m5A0CcTE051292; Tue, 10 Jun 2008 01:12:38 +0100 (BST) Received: from [192.168.0.3] (85-211-228-245.dyn.gotadsl.co.uk [85.211.228.245]) (authenticated bits=0) by mail.authsmtp.com (8.14.2/8.14.2/Kp) with ESMTP id m5A0CSCW049642; Tue, 10 Jun 2008 01:12:29 +0100 (BST) Subject: Re: pg_dump restore time and Foreign Keys From: Simon Riggs To: Alvaro Herrera Cc: Robert Treat , Tom Lane , Decibel! , Andrew Dunstan , pgsql-hackers@postgresql.org In-Reply-To: <20080609180728.GA10034@alvh.no-ip.org> References: <1212647003.19964.19.camel@ebony.site> <1213026485.12046.123.camel@ebony.site> <20406.1213027167@sss.pgh.pa.us> <200806091237.49153.xzilla@users.sourceforge.net> <1213034097.12046.150.camel@ebony.site> <20080609180728.GA10034@alvh.no-ip.org> Content-Type: text/plain Date: Tue, 10 Jun 2008 01:14:25 +0100 Message-Id: <1213056865.12046.167.camel@ebony.site> Mime-Version: 1.0 X-Mailer: Evolution 2.12.0 Content-Transfer-Encoding: 7bit X-Server-Quench: ea40dd0d-3681-11dd-8155-001185d377ca X-AuthRoute: OCdxZQATClZOTQEd DAteCiNZVAwpPBRK HVkIKg5MJUcNSQVJ NksacBtFaQVbZ0xf HGQLW1xEUFx7WGF/ aQMfZQBDYEtPQQdo WFZLRldNFgBqBAMB SEMbIxkBFHYheHp5 Y0JgEHJfWUI0IBd5 QhoAFTkbZmFoaX0e URQMagtVdQZXfh9E a1h6AHAKZjZWKBg1 TUcAHxkaHhhlExEd Wg46IU8XWQ4REyUg QAoPVSkuGEBNTiM/ ZzIhMFMdE0BZEUgj KjNh X-Authentic-SMTP: 61633235383639.squirrel.dmpriest.net.uk:1045/Kp X-Report-SPAM: If SPAM / abuse - report it at: http://www.authsmtp.com/abuse X-Virus-Status: No virus detected - but ensure you scan with your own anti-virus system! X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200806/426 X-Sequence-Number: 119539 On Mon, 2008-06-09 at 14:07 -0400, Alvaro Herrera wrote: > Simon Riggs wrote: > > > If we break down the action into two parts. > > > > ALTER TABLE ... ADD CONSTRAINT foo FOREIGN KEY ... NOVALIDATE; > > which holds exclusive lock, but only momentarily > > After this runs any new data is validated at moment of data change, but > > the older data has yet to be validated. > > > > ALTER TABLE ... VALIDATE CONSTRAINT foo > > which runs lengthy check, though only grabs lock as last part of action > > The problem I see with this approach in general (two-phase FK creation) > is that you have to keep the same transaction for the first and second > command, but you really want concurrent backends to see the tuple for > the not-yet-validated constraint row. Well, they *must* be in separate transactions if we are to avoid holding an AccessExclusiveLock while we perform the check. Plus the whole idea is to perform the second part at some other non-critical time, though we all agree that never performing the check at all is foolhardy. Maybe we say that you can defer the check, but after a while autovacuum runs it for you if you haven't done so. It would certainly be useful to run the VALIDATE part as a background task with vacuum wait enabled. > Another benefit that could arise from this is that the hypothetical > VALIDATE CONSTRAINT step could validate more than one constraint at a > time, possibly processing all the constraints with a single table scan. Good thought, though not as useful for FK checks. -- Simon Riggs www.2ndQuadrant.com PostgreSQL Training, Services and Support