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.94.2) (envelope-from ) id 1qVr5S-001OTu-Pw for pgsql-hackers@arkaria.postgresql.org; Tue, 15 Aug 2023 10:15:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qVr5R-00D8HJ-5K for pgsql-hackers@arkaria.postgresql.org; Tue, 15 Aug 2023 10:15:41 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qVr5Q-00D8HB-RP for pgsql-hackers@lists.postgresql.org; Tue, 15 Aug 2023 10:15:41 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qVr5L-000F8G-I6 for pgsql-hackers@lists.postgresql.org; Tue, 15 Aug 2023 10:15:39 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 905DF5C0257; Tue, 15 Aug 2023 06:15:34 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Tue, 15 Aug 2023 06:15:34 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1692094534; x= 1692180934; bh=gumKnbS9n1psuTirPMo3mXuUZo7SP6kU9hbbIrnbeTM=; b=P jR4ivAv4gioqyIhl0lvqs50XcMlKealkqUkmf81PPbvWoe74BxeWfLB7pskcu/dB PVFSHpDM71Bn6r9WbpQ17ssjEj3H/1S/DjLJb1wT0xmBEqVf+BH1AOdU7cACQLDs WOLGXAI3kjVdbbSZ1zpui12kDND1EFMFuucGbjY/MgEtR87eScRDLFlQaJY6Kg6e 6Z5NzPtFZh+EIkpBLD8161fB7HAWVIGlTXMqR2V0/3+p7jpgBk0m94RcQozCTGfk W4nnbWck2wtf/fkZIdyqOH53LXjdVBPGUhD+xbALmBFJqJg0Ru+xvSHzCX80icW5 leZOivZRBolZc0zwoAOng== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedruddtjedgvdehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkgggtugfgjgesthekredttddtjeenucfhrhhomheptehlvhgr rhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh eqnecuggftrfgrthhtvghrnhepvdektdffudfftdffffehfffhjeejhffgieeuueekjeek fffgudffhfduffffueevnecuffhomhgrihhnpegvnhhtvghrphhrihhsvggusgdrtghomh enucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrlhhv hhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhg X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 15 Aug 2023 06:15:34 -0400 (EDT) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 7195FA9; Tue, 15 Aug 2023 12:15:32 +0200 (CEST) Date: Tue, 15 Aug 2023 12:15:32 +0200 From: Alvaro Herrera To: Dean Rasheed Cc: Robert Haas , Peter Eisentraut , pgsql-hackers@lists.postgresql.org Subject: Re: cataloguing NOT NULL constraints Message-ID: <20230815101532.5qffiy5sjh5bitiz@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2023-Aug-15, Dean Rasheed wrote: > I think perhaps for ALTER TABLE INHERIT, it should check that the > child has a NOT NULL constraint, and error out if not. That's the > current behaviour, and also matches other constraints types (e.g., > CHECK constraints). Yeah, I reached the same conclusion yesterday while trying it out, so that's what I implemented. I'll post later today. > More generally though, I'm worried that this is starting to get very > complicated. I wonder if there might be a different, simpler approach. > One vague idea is to have a new attribute on the column that counts > the number of constraints (local and inherited PK and NOT NULL > constraints) that make the column not null. Hmm. I grant that this is different, but I don't see that it is simpler. > Something else I noticed when reading the SQL standard is that a > user-defined CHECK (col IS NOT NULL) constraint should be recognised > by the system as also making the column not null (setting its > "nullability characteristic" to "known not nullable"). I agree with this view actually, but I've refrained from implementing it(*) because our SQL-standards people have advised against it. Insider knowledge? I don't know. I think this is a comparatively smaller consideration though, and we can adjust for it afterwards. (*) Rather: at some point I removed the implementation of that from the patch. > I'm also wondering whether creating a pg_constraint entry for *every* > not-nullable column is actually going too far. If we were to > distinguish between "defined as NOT NULL" and being not null as a > result of one or more constraints, in the way that the standard seems > to suggest, perhaps the former (likely to be much more common) could > simply be a new attribute stored on the column. I think we actually > only need to create pg_constraint entries if a constraint name or any > additional constraint properties such as NOT VALID are specified. That > would lead to far fewer new constraints, less catalog bloat, and less > noise in the \d output. There is a problem if we do this, though, which is that we cannot use the constraints for the things that we want them for -- for example, remove_useless_groupby_columns() would like to use unique constraints, not just primary keys; but it depends on the NOT NULL rows being there for invalidation reasons (namely: if the NOT NULL constraint is dropped, we need to be able to replan. Without catalog rows, we don't have a mechanism to let that happen). If we don't add all those redundant catalog rows, then this is all for naught. -- Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/ Bob [Floyd] used to say that he was planning to get a Ph.D. by the "green stamp method," namely by saving envelopes addressed to him as 'Dr. Floyd'. After collecting 500 such letters, he mused, a university somewhere in Arizona would probably grant him a degree. (Don Knuth)