Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oOh19-0002rZ-Qq for pgsql-hackers@arkaria.postgresql.org; Thu, 18 Aug 2022 15:01:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oOh17-00022k-SK for pgsql-hackers@arkaria.postgresql.org; Thu, 18 Aug 2022 15:01:05 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oOh17-00022X-Ik for pgsql-hackers@lists.postgresql.org; Thu, 18 Aug 2022 15:01:05 +0000 Received: from mail-ed1-x536.google.com ([2a00:1450:4864:20::536]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oOh13-0002VW-GM for pgsql-hackers@lists.postgresql.org; Thu, 18 Aug 2022 15:01:05 +0000 Received: by mail-ed1-x536.google.com with SMTP id s11so2206636edd.13 for ; Thu, 18 Aug 2022 08:01:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc; bh=raCU4I6z/UWNDt7rOHzxxcxUJ6eSteHFEgwE6Lxx4AY=; b=N7kWE7Mc0c4PLfHTQapEKTfmrDkSObohd3RAWbMSMaU4gHYyUtrkpFZuo1J3bA0r4A c75NESk/dRqCzm6R1XKNyLEHnLQNMouU5QsjPjgoxKn5Fp0G1EZMizC1P8MIYoBVmqk+ bfITe0VK/RR2i1ah3olzVC9HMoe+B06tBvSSw282cjbAz4D+4KKyFpaPBwfBlueKlXrw DRUfMpxNb2JNxxvsvQ5iccyTNX49yNzyYE6lSm2redLao7qYheHgVQ/MJiez8/PVX9Xh oBxEI/4rpnOI3yh3D2cigA3r4Skgd0BuBxQxrlF/+255GXfd2jr9Cyv2w07euhRbyPl2 K/Yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc; bh=raCU4I6z/UWNDt7rOHzxxcxUJ6eSteHFEgwE6Lxx4AY=; b=Ao7oqQP/HICqPJL5UvTEwn3xPutPWx3UmcEq+drUn88y15ba2L1I5LMm2sMAU71dpd 7NqbQOZBVpmjhPnQ+DMoiuIM9L+nH7Gq737Ri9R1KegCO6NbdlXOh+bjctVoekFZX61p oEo31134xhaiXu44lCbCpHiFCYqjva5Ghg9IA8dA08A4NlqQdRMLZf5GFg3CMNEoXHZ1 bdN2MnUCsMiRehQ5EyjfxPvYY8yLMZfFc6YiT5nlA50QvCkdS9VVoNmAizJmZ8OYvLsD nZsrgVEqs+m96QpptaNXfLH7QdqkJ2UhglL0lFCNkEQ8Caml/aTw4gm6F1o74XRNI8tG d8BA== X-Gm-Message-State: ACgBeo0ZYzV85CiLlq/MdTE6zAU3Mfk2PhATeEMxPYAAHT7z68zFZCCE DX3EnhDG+vGhk7F62X+88nq4c4JvZVy34g== X-Google-Smtp-Source: AA6agR5l2OtYiCUdYoPtx9ipGKVGeF///YIskVX002/cx95+TbgNxrOha8hUFu4eAN9urnIEWthjvA== X-Received: by 2002:a05:6402:4504:b0:43b:4ec7:2ec1 with SMTP id ez4-20020a056402450400b0043b4ec72ec1mr2552991edb.7.1660834860335; Thu, 18 Aug 2022 08:01:00 -0700 (PDT) Received: from localhost.localdomain ([213.235.133.41]) by smtp.gmail.com with ESMTPSA id a12-20020a1709066d4c00b00730935f959fsm933977ejt.223.2022.08.18.08.00.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Aug 2022 08:00:59 -0700 (PDT) Message-ID: Subject: Re: cataloguing NOT NULL constraints From: Laurenz Albe To: Alvaro Herrera Cc: Pg Hackers Date: Thu, 18 Aug 2022 17:00:52 +0200 In-Reply-To: <20220818090425.37co7pfwysbervgj@alvherre.pgsql> References: <20220818090425.37co7pfwysbervgj@alvherre.pgsql> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.42.4 (3.42.4-2.fc35) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, 2022-08-18 at 11:04 +0200, Alvaro Herrera wrote: > On 2022-Aug-18, Laurenz Albe wrote: > > On Wed, 2022-08-17 at 20:12 +0200, Alvaro Herrera wrote: > > >    Using ALTER TABLE DROP CONSTRAINT works fine, and the 'attnotnull' > > >    bit is lost when the last one such constraint goes away. > > > > Wouldn't it be the correct solution to set "attnotnumm" to FALSE only > > when the last NOT NULL constraint is dropped? > > ... when the last NOT NULL or PRIMARY KEY constraint is dropped.  We > have to keep attnotnull set when a PK exists even if there's no specific > NOT NULL constraint. Of course, I forgot that. I hope that is not too hard to implement. > > > 2. If a table has a primary key, and a table is created that inherits > > >    from it, then the child has its column(s) marked attnotnull but there > > >    is no pg_constraint row for that.  This is not okay.  But what should > > >    happen? > > > > > >    1. a CHECK(col IS NOT NULL) constraint is created for each column > > >    2. a PRIMARY KEY () constraint is created > > > > I think it would be best to create a primary key constraint on the > > partition. > > Sorry, I wasn't specific enough.  This applies to legacy inheritance > only; partitioning has its own solution (as you say: the PK constraint > exists), but legacy inheritance works differently.  Creating a PK in > children tables is not feasible (because unicity cannot be maintained), > but creating a CHECK (NOT NULL) constraint is possible. > > I think a PRIMARY KEY should not be allowed to exist in an inheritance > parent, precisely because of this problem, but it seems too late to add > that restriction now.  This behavior is absurd, but longstanding: My mistake; you clearly said "inherits". Since such an inheritance child currently does not have a primary key, you can insert duplicates. So automatically adding a NUT NULL constraint on the inheritance child seems the only solution that does not break backwards compatibility. pg_upgrade would have to be able to cope with that. Forcing a primary key constraint on the inheritance child could present an upgrade problem. Even if that is probably a rare and strange case, I don't think we should risk that. Moreover, if we force a primary key on the inheritance child, using ALTER TABLE ... INHERIT might have to create a unique index on the table, which can be cumbersome if the table is large. So I think a NOT NULL constraint is the least evil. Yours, Laurenz Albe