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 1rvBht-00AIqF-1X for pgsql-hackers@arkaria.postgresql.org; Fri, 12 Apr 2024 07:52:22 +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 1rvBhq-00BII3-Dh for pgsql-hackers@arkaria.postgresql.org; Fri, 12 Apr 2024 07:52:18 +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 1rvBho-00BIHo-N2 for pgsql-hackers@lists.postgresql.org; Fri, 12 Apr 2024 07:52:18 +0000 Received: from fhigh7-smtp.messagingengine.com ([103.168.172.158]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rvBhj-002TXh-HX for pgsql-hackers@lists.postgresql.org; Fri, 12 Apr 2024 07:52:15 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 60A4C1140161; Fri, 12 Apr 2024 03:52:09 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Fri, 12 Apr 2024 03:52:09 -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:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1712908329; x= 1712994729; bh=Xu8Kl68AqCTBPI4QnvL1sWszhHYsel+RSWqcoMGY/zA=; b=G d93BbFpSb3DXvUNwUAX9UhLSLsNXFKMLwFtYaVjUYXToI00ksmZQl3/EatdAEZY+ NDx/VADbYRjREGl4QTIQdO3CuX7J+Krwtze7lc2aZKtSQCrgQy/ivfwZObBay1cm sZYc30Fj1X/v+C9nMcqMw4RSCha2j0iO9/eLrsFnTs0c+RH8cChrEBgjlSMOLRii oAD3SLr+Wv+PVuhrcsOsrXmdlLskQoM5co2SZEbtYEJ+RQ8fU30Z1cbpnfXTFUDD QQhXArErVQW3uimGXe3ZlDjindWzKTuPmsfU7rTN5Tc0VrDzHcj0i4IqgDQAMf2x 7MRtdfbmYszk46OAbghrQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudeitddgledtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkgggtugfgjgesthekredttddtjeenucfhrhhomheptehlvhgr rhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh eqnecuggftrfgrthhtvghrnhepvdektdffudfftdffffehfffhjeejhffgieeuueekjeek fffgudffhfduffffueevnecuffhomhgrihhnpegvnhhtvghrphhrihhsvggusgdrtghomh enucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrlhhv hhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhg X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 12 Apr 2024 03:52:08 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1712908325; bh=tvVxJcgsvrjDC7FzDVCIXSzh/5a26mE8EYiyxEqz5vQ=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=LOlgrjA1IJ6DYFXMdORI0XFLnsLtoEa0iYK0NpcweUO7FSMf1mAmQq2Jh66IqU6uc YPFFiDZnZUuS5reRdBEITtXgPU4bpiUmut+Pko6Nl30FnvnHvj4kkmSBpeAX1Qg+Lf LfTdkT4pN7XrkN9PE0fMWBGe1HSwKpjDciSed7Lg/TPNiilOWtxlR2sxfwdm3Tm7hq T4eLq6gLumzIuhwWcEF+0TxUgS4y1z+mLXCdFf+bOHIpiod3s3TxYRVwAtJyU+dnxn JRvqRKLKc5lz+ab28S6iu425jYKXmDGZj/RJSzUUbjZMncXaxqH352y+3mtRcF82tg Z1aD5GbKTDWOQ== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id 3609B329; Fri, 12 Apr 2024 09:52:05 +0200 (CEST) Date: Fri, 12 Apr 2024 09:52:05 +0200 From: Alvaro Herrera To: jian he Cc: Tender Wang , PostgreSQL Hackers Subject: Re: Can't find not null constraint, but \d+ shows that Message-ID: <202404120752.6ebv4q5zwnfw@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 2024-Apr-12, jian he wrote: > Now I am more confused... > +CREATE TABLE notnull_tbl1 (c0 int, c1 int, PRIMARY KEY (c0, c1)); > +ALTER TABLE notnull_tbl1 DROP c1; > same query, mysql make let "c0" be not null Yes, that was Postgres' old model. But the way we think of it now, is that a column is marked attnotnull when a pg_constraint entry exists to support that flag, which can be a not-null constraint, or a primary key constraint. In the old Postgres model, you're right that we would continue to have c0 as not-null, just like mysql. In the new model, that flag no longer has no reason to be there, because the backing primary key constraint has been removed, which is why we reset it. So what I was saying in the cases with replica identity and generated columns, is that there's an attnotnull flag we cannot remove, because of either of those things, but we don't have any backing constraint for it, which is an inconsistency with the view of the world that I described above. I would like to manufacture one not-null constraint at that point, or just abort the drop of the PK ... but I don't see how to do either of those things. If you want the c0 column to be still not-null after dropping the primary key, you need to SET NOT NULL: CREATE TABLE notnull_tbl1 (c0 int, c1 int, PRIMARY KEY (c0, c1)); ALTER TABLE notnull_tbl1 ALTER c0 SET NOT NULL; ALTER TABLE notnull_tbl1 DROP c1; \d+ notnull_tbl1 Table "public.notnull_tbl1" Column │ Type │ Collation │ Nullable │ Default │ Storage │ Compression │ Stats target │ Description ────────┼─────────┼───────────┼──────────┼─────────┼─────────┼─────────────┼──────────────┼───────────── c0 │ integer │ │ not null │ │ plain │ │ │ Not-null constraints: "notnull_tbl1_c0_not_null" NOT NULL "c0" Access method: heap One thing that's not quite ideal, is that the "Nullable" column doesn't make it obvious that the flag is going to be removed if you drop the PK; you have to infer that that's going to happen by noticing that there's no explicit not-null constraint listed for that column -- maybe too subtle, especially if you have a lot of columns (luckily, PKs normally don't have too many columns). This is why I suggested to change the contents of that column if the flag is sustained by the PK. Something like this, perhaps: =# CREATE TABLE notnull_tbl1 (c0 int not null, c1 int, PRIMARY KEY (c0, c1)); =# \d+ notnull_tbl1 Table "public.notnull_tbl1" Column │ Type │ Collation │ Nullable │ Default │ Storage │ Compression │ Stats target │ Description ────────┼─────────┼───────────┼─────────────┼─────────┼─────────┼─────────────┼──────────────┼───────────── c0 │ integer │ │ not null │ │ plain │ │ │ c1 │ integer │ │ primary key │ │ plain │ │ │ Indexes: "notnull_tbl1_pkey" PRIMARY KEY, btree (c0, c1) Not-null constraints: "notnull_tbl1_c0_not_null" NOT NULL "c0" Access method: heap which should make it obvious. -- Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/ "Right now the sectors on the hard disk run clockwise, but I heard a rumor that you can squeeze 0.2% more throughput by running them counterclockwise. It's worth the effort. Recommended." (Gerry Pourwelle)