Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1voHrL-000Yh2-04 for pgsql-bugs@arkaria.postgresql.org; Fri, 06 Feb 2026 09:10:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1voHrJ-002p3g-0j for pgsql-bugs@arkaria.postgresql.org; Fri, 06 Feb 2026 09:10:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1voHrI-002p3Y-2z for pgsql-bugs@lists.postgresql.org; Fri, 06 Feb 2026 09:10:36 +0000 Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1voHrG-00000001Kpq-2XNs for pgsql-bugs@lists.postgresql.org; Fri, 06 Feb 2026 09:10:36 +0000 Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-47edd9024b1so4552215e9.3 for ; Fri, 06 Feb 2026 01:10:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1770369033; x=1770973833; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=XHSpsnp3m+xtNrR9q/XhSt9tlIBddeQ48a19CvwvB54=; b=o3w7CHm/8Wd7p+LcEX3dTnghTZP0M9quzSkw4VwCGDr0YNHgFOteAvHQMPE/AkIL93 fcTN+XzcPQ9vk1AHMVF0hqNimvEkJfnSdkb6c2Wx424P1AVx1vWB68WGTmum/Uw/NtuD SM6JDKlnL0ZYpNisms7mRrhV7SG16mu9uc8HPUwST2gQbQmCIXNBhd87O1vABMnF/TnZ zNBHOs1MlIDdZ3GbWoWq54vnpNWxQxaRP/BbbKxy3gbm6cYeortKqIHH3H78tREar/zL JQz2qZDM1a8m3QthCV0bm+5Xc6r1WityDT1NDH2wgu0eo+DLi0sp7wml6IQCb+Hf8CzY n/Jw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770369033; x=1770973833; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=XHSpsnp3m+xtNrR9q/XhSt9tlIBddeQ48a19CvwvB54=; b=BwXN8kAKgDmI8wzMXRF8PebeGAqETEAr0H38HormkTWLKYW4s6VhfgU6CUa68rqT0e ZRuUUxyO8K43t+H60Q7xDjpFhexIVUQfMsqmfIQ7q9HWkEtf9/ltOmZ3Tw2ZWX2lfCmj J1Dn4qJYFIv8AVUpRlQYaicb6Q2lYwvR0A1d0cFVDakzKIGFJ2KBl5wdyT+iZwgxyEit R5F3rPVNN3OCx/BNJrGIK2kPSQyrKQ9d68xCF+ZXgm1juXyFlv0wnE7Mtc2ZrZHwccby fx83peXKNzeyeLO3ZfHfUng1Oh04fGhuFrQt/khJiBGBkk7QYhQFCZlSpdMnscilTp+J 1VRQ== X-Forwarded-Encrypted: i=1; AJvYcCUH/w7E17ri2w8dqZl1Mch9lF0RV3dGkHiErlhDbioSkwk4YFbSfxMduST1BdnaapodEsS8IYII8GRv@lists.postgresql.org X-Gm-Message-State: AOJu0YxyrrYGOFHl+4lvx2DIp4f6Gj0kdCtakdygnvrKaNIAPDSsAPGU ij2vBGawIFXlDzgo5CpCz2QSGydnelxmMMs8mq7+cYoi4iSf8zgRYAT3fiOGbParZ2I= X-Gm-Gg: AZuq6aI7nFccUGXLfL08PvnQKV/QtVq3mzccsVjEeVtklo4L+MkUHVdG8LQEzyqG6an rmFQM8rg85IrMxZdCkK7Lnz5H8deWjTcUvXnBUw3DJ0gUCRvZl45FusPwnVgv0X4Bs4WogA3pR3 d8xXULOzgtTs0idgtBQ2xLW8Byj5Oq1N47zV+jaHIgsViZu7s2+2vHTlAo8JdOIRQqA2phKj+qO BFZer1/ORbiN7HYMTkoCHOZyvGBCcA75nZdW6DFQbV6b8a5ESCUpybQRUX9nphQ6ixHmQHKBE0f aGL2ge9vMEU1iXwi+vAhHg5xjapKiN/AxysBQhIV6nRDaiU9Ht+CE62QX0Fyk2rCaXy8QAQlvLe l158+QjqXblrm7ZofVbBTEQz5RA+9tSGoMnhoLMFpHbqg4zT0bpXX6aLqElyry5uZ+WOYlYDGTz 2au4ik/Xqebn09uAOrFKoD1eLzNSJ7+Yqnj4EX5y3EfimTz22WWtaK X-Received: by 2002:a05:600c:528b:b0:477:2f7c:314f with SMTP id 5b1f17b1804b1-483201e25b5mr26552365e9.10.1770369032673; Fri, 06 Feb 2026 01:10:32 -0800 (PST) Received: from laurenz.albe-K4N0CV00F97414D ([2001:871:260:5269:e994:478a:1aaa:cd34]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-436296b25d5sm4503353f8f.2.2026.02.06.01.10.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Feb 2026 01:10:32 -0800 (PST) Message-ID: <99c37bcd620778c743a413dc9a1dce53ae9f4c38.camel@cybertec.at> Subject: Re: BUG #19393: pg_upgrade fails with duplicate key violation when CHECK constraint named *_not_null exists From: Laurenz Albe To: Dilip Kumar Cc: huseyin.d3r@gmail.com, pgsql-bugs@lists.postgresql.org Date: Fri, 06 Feb 2026 10:10:31 +0100 In-Reply-To: References: <19393-6a82427485a744cf@postgresql.org> <2b3a2cd5f9b6e82fba6ac45b413291b01dd252ea.camel@cybertec.at> <61c535617992fff830961ecc09a9c20096bc1f36.camel@cybertec.at> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.58.2 (3.58.2-1.fc43) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, 2026-02-06 at 12:53 +0530, Dilip Kumar wrote: > On Thu, Feb 5, 2026 at 10:22=E2=80=AFPM Laurenz Albe wrote: > >=20 > > On Thu, 2026-02-05 at 15:58 +0100, I wrote: > > > The bug is actually not in pg_upgrade, but in CREATE TABLE.=C2=A0 The= attached patch > > > fixes the problem for me by avoiding given constraint names when gene= rating > > > the names for NOT NULL constraints. > >=20 > > ... and here is v2, including a regression test. >=20 > The fix LGTM. However I have one question, have you considered > validating the name selection logic for other constraint types as > well? I=E2=80=99m specifically thinking about AddRelationNewConstraints()= . > While I don't have a specific test case yet, is it possible for the > AddRelationNewConstraints to choose a name that is already in use when > adding a new column with a constraint? Thanks for having a look. I am not sure what you mean by "adding a new column": do you mean an ALTER TABLE that runs after the CREATE TABLE? The following works fine in v18: CREATE TABLE nulls ( y integer UNIQUE, CONSTRAINT nulls_x_not_null FOREIGN KEY (y) REFERENCES nulls (y), CONSTRAINT nulls_x_fkey CHECK (TRUE) ); ALTER TABLE nulls ADD x integer REFERENCES nulls (y) NOT NULL; Both the new foreign key and the new NOT NULL constraint get a name that doesn't conflict with the existing constraints. But I don't claim that my patch fixes all possible problems during a pg_upgrade. If you define a table like this in v13: CREATE TABLE nulls ( x integer UNIQUE NOT NULL, CONSTRAINT nulls_x_not_null FOREIGN KEY (x) REFERENCES nulls (x) ); then pg_dump --binary-upgrade will produce code like the following: CREATE TABLE laurenz.nulls ( x integer NOT NULL ); ALTER TABLE ONLY laurenz.nulls ADD CONSTRAINT nulls_x_key UNIQUE (x); ALTER TABLE ONLY laurenz.nulls ADD CONSTRAINT nulls_x_not_null FOREIGN KEY (x) REFERENCES laurenz.nu= lls(x); and the last statement will cause an error, because the constraint name wil= l conflict with the name for the NOT NULL constraint. In other words, my pat= ch only works for constraints that are dumped as part of the CREATE TABLE stat= ement, which I believe are only check constraints. But my opinion is that it is very unlikely that anybody picks a name ending in "_not_null" for a foreign key or unique constraint, while (as the bug re= port demonstrates) there may be people who define (superfluous) check constraint= s with such names. So there is still the potential for pg_upgrade failures with my patch appli= ed, but it would fix the case most likely to occur in practice. Yours, Laurenz Albe