Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VO7Hr-0005hY-6i for pgsql-bugs@arkaria.postgresql.org; Mon, 23 Sep 2013 14:38:55 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VO7Hq-0001yO-Bd for pgsql-bugs@arkaria.postgresql.org; Mon, 23 Sep 2013 14:38:54 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VO2ef-0005Dr-JV for pgsql-bugs@postgresql.org; Mon, 23 Sep 2013 09:42:09 +0000 Received: from nms.sk ([93.184.65.245] helo=smtp.inf-it.com) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VO2eX-0003WP-Rb for pgsql-bugs@postgresql.org; Mon, 23 Sep 2013 09:42:09 +0000 X-Spam-Flag: NO X-Spam-Score: -2.886 X-Spam-Level: X-Spam-Status: No, score=-2.886 tagged_above=-999 required=4.8 tests=[ALL_TRUSTED=-1, AWL=0.012, BAYES_00=-1.9, HTML_MESSAGE=0.001, URIBL_BLOCKED=0.001] autolearn=ham Received: from [10.0.1.172] (office.nms.sk [195.168.89.130]) by smtp.inf-it.com (Postfix) with ESMTPSA id 8FBEA4E6FE; Mon, 23 Sep 2013 11:40:08 +0200 (CEST) Content-Type: multipart/signed; boundary="Apple-Mail=_0CB658BA-E566-4F68-B7DD-64477D749391"; protocol="application/pkcs7-signature"; micalg=sha1 Mime-Version: 1.0 (Mac OS X Mail 6.6 \(1510\)) Subject: Re: BUG #8465: major dump/reload problem From: =?iso-8859-1?Q?J=E1n_M=E1t=E9?= In-Reply-To: <523FF667.6040202@vmware.com> Date: Mon, 23 Sep 2013 11:41:18 +0200 Cc: pgsql-bugs@postgresql.org Message-Id: References: <523FF667.6040202@vmware.com> To: Heikki Linnakangas X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --Apple-Mail=_0CB658BA-E566-4F68-B7DD-64477D749391 Content-Type: multipart/alternative; boundary="Apple-Mail=_CC6C5CE6-9D34-43D8-9AFE-8A43BB6929D7" --Apple-Mail=_CC6C5CE6-9D34-43D8-9AFE-8A43BB6929D7 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Hi Keikki, it is clear what causes this problem, but pg_upgradecluster is an = operation which simply MUST work (if you have several hundred of user = databases, it is not an option to check all of them for this kind of = error before the cluster upgrade). Database upgrade is performed by the server administrator and a simple = check constraint (created by a random database user) CANNOT break it. The real solution is to change the pg_dump result from: CREATE TABLE activity ( _id bigint NOT NULL, name text NOT NULL, country bigint NOT NULL, currency bigint NOT NULL, CONSTRAINT activity_country_check CHECK (codebook_check((1)::bigint, = country, false)), CONSTRAINT activity_currency_check CHECK = (codebook_check((2)::bigint, currency, false)) ); <> to CREATE TABLE activity ( _id bigint NOT NULL, name text NOT NULL, country bigint NOT NULL, currency bigint NOT NULL ); <> ALTER TABLE activity ADD CHECK (codebook_check((1)::bigint, country, = false)); ALTER TABLE activity ADD CHECK (codebook_check((2)::bigint, currency, = false)); JM On Sep 23, 2013, at 10:05 AM, Heikki Linnakangas = wrote: > On 21.09.2013 20:16, jan.mate@inf-it.com wrote: >> today I tried to upgrade from 9.2 to 9.3 (pg_upgradecluster 9.2 main) = and >> the upgrade of one of my databases failed with the following error: = "ERROR: >> new row for relation ... violates check constraint ...". >>=20 >> I created an example to reproduce this bug: >>=20 >> http://www.inf-it.com/fixes/postgres-bugreport2/schema.sql >=20 > The problem is that when the database is dumped with pg_dump and = reloaded, the activity table is loaded first, and codebook table second. = The check constraint checks that when a row is inserted into activity = table, the corresponding row exists in codebook table, which clearly = isn't true if the activity table is loaded first and the codebook table = is still empty. The system doesn't know about that dependency since it's = all implemented in the PL/pgSQL code. With a constraint like that, you = would also get an unrestorable dump if you e.g deleted a row from = codebook table after loading the activities. >=20 > Usually you would implement a schema like that using foreign keys. = That would be less code, and the system would automatically get the dump = order correct. I would recommend that over a check constraint, if = possible. >=20 > As a work-around, you can drop the constraints from the database = before upgrading, and restore them afterwards. The problem isn't really = related to upgrade per se, BTW. Running pg_dump + restore even on the = same version will give you the same error. >=20 > - Heikki --Apple-Mail=_CC6C5CE6-9D34-43D8-9AFE-8A43BB6929D7 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii Hi = Keikki,

it is clear what causes this problem, but = pg_upgradecluster is an operation which simply MUST work (if you have = several hundred of user databases, it is not an option to check all of = them for this kind of error before the cluster = upgrade).

Database upgrade is performed by the = server administrator and a simple check constraint (created by a random = database user) CANNOT break = it.


The real solution is to = change the pg_dump result from:

CREATE TABLE = activity (
    _id bigint NOT NULL,
    name = text NOT NULL,
    country bigint NOT NULL,
  =   currency bigint NOT NULL,
    CONSTRAINT = activity_country_check CHECK (codebook_check((1)::bigint, country, = false)),
    CONSTRAINT activity_currency_check CHECK = (codebook_check((2)::bigint, currency, = false))
);

<<import data = here>>


to


CREATE TABLE activity (
    _id = bigint NOT NULL,
    name text NOT NULL,
    = country bigint NOT NULL,
    currency bigint NOT = NULL
);

<<import data = here>>

ALTER TABLE activity ADD = CHECK (codebook_check((1)::bigint, country, = false));
ALTER TABLE activity ADD CHECK = (codebook_check((2)::bigint, currency, = false));



JM
=


On Sep 23, 2013, at 10:05 = AM, Heikki Linnakangas <hlinnakangas@vmware.com> = wrote:

On 21.09.2013 20:16, jan.mate@inf-it.com = wrote:
today I tried to upgrade from 9.2 to = 9.3 (pg_upgradecluster 9.2 main) and
the upgrade of one of my = databases failed with the following error: "ERROR:
new row for = relation ... violates check constraint ...".

I created an example = to reproduce this bug:

http:/= /www.inf-it.com/fixes/postgres-bugreport2/schema.sql
<= br>The problem is that when the database is dumped with pg_dump and = reloaded, the activity table is loaded first, and codebook table second. = The check constraint checks that when a row is inserted into activity = table, the corresponding row exists in codebook table, which clearly = isn't true if the activity table is loaded first and the codebook table = is still empty. The system doesn't know about that dependency since it's = all implemented in the PL/pgSQL code. With a constraint like that, you = would also get an unrestorable dump if you e.g deleted a row from = codebook table after loading the activities.

Usually you would = implement a schema like that using foreign keys. That would be less = code, and the system would automatically get the dump order correct. I = would recommend that over a check constraint, if possible.

As a = work-around, you can drop the constraints from the database before = upgrading, and restore them afterwards. The problem isn't really related = to upgrade per se, BTW. Running pg_dump + restore even on the same = version will give you the same error.

- = Heikki

= --Apple-Mail=_CC6C5CE6-9D34-43D8-9AFE-8A43BB6929D7-- --Apple-Mail=_0CB658BA-E566-4F68-B7DD-64477D749391 Content-Disposition: attachment; filename=smime.p7s Content-Type: application/pkcs7-signature; name=smime.p7s Content-Transfer-Encoding: base64 MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEHAQAAoIIMfjCCBjQw ggQcoAMCAQICAR4wDQYJKoZIhvcNAQEFBQAwfTELMAkGA1UEBhMCSUwxFjAUBgNVBAoTDVN0YXJ0 Q29tIEx0ZC4xKzApBgNVBAsTIlNlY3VyZSBEaWdpdGFsIENlcnRpZmljYXRlIFNpZ25pbmcxKTAn BgNVBAMTIFN0YXJ0Q29tIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MB4XDTA3MTAyNDIxMDE1NVoX DTE3MTAyNDIxMDE1NVowgYwxCzAJBgNVBAYTAklMMRYwFAYDVQQKEw1TdGFydENvbSBMdGQuMSsw KQYDVQQLEyJTZWN1cmUgRGlnaXRhbCBDZXJ0aWZpY2F0ZSBTaWduaW5nMTgwNgYDVQQDEy9TdGFy dENvbSBDbGFzcyAxIFByaW1hcnkgSW50ZXJtZWRpYXRlIENsaWVudCBDQTCCASIwDQYJKoZIhvcN AQEBBQADggEPADCCAQoCggEBAMcJg8zOLdgasSmkLhOrlr6KMoOMpohBllVHrdRvEg/q6r8jR+EK 75xCGhR8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