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 1s40iD-00HN6y-7h for pgsql-hackers@arkaria.postgresql.org; Mon, 06 May 2024 15:57:09 +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 1s40iA-00EDT6-KQ for pgsql-hackers@arkaria.postgresql.org; Mon, 06 May 2024 15:57:07 +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 1s40i9-00EDSy-BF for pgsql-hackers@lists.postgresql.org; Mon, 06 May 2024 15:57:07 +0000 Received: from wfout7-smtp.messagingengine.com ([64.147.123.150]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s40i5-001k5P-Ua for pgsql-hackers@lists.postgresql.org; Mon, 06 May 2024 15:57:03 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfout.west.internal (Postfix) with ESMTP id 958D01C00161; Mon, 6 May 2024 11:56:58 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Mon, 06 May 2024 11:56:59 -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=fm3; t=1715011018; x= 1715097418; bh=Xbv4gdw60o3ddzcqeZ4dYOTd2USfj/OjSiMCcSrIH7M=; b=M fj0c0AcoHanjQXCxXNze0jk0nt7ZlPq6qgBxUAcO4UHSP7k1lWVa0PWlZUABJsX1 6KK1MLN1ihECj8blhX0GEEzUI+uGUhE1n53ZlHHVgb/4fwh20O7/nfRcP4nSHKIp cMFvtUJS5d4sy1EpFIrsMYI+pinNpzvAvbh65DaFgiNVVjY6rqNvoN1miLhI9mjj wWNhBJvPXEHTMi+VOhkiu9b01hV8MVJtrxPl8KnGRpghdCfSE7roHBoPOUXdKcey xU+vsWplZzp8uLRZ2LRck59YBvNNfdLGIqI6eoCU7fEgMJHni8UW8+VXlU8DM7U9 8qDJake7pkzfrn0QO8Y4g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvddviedgleeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkgggtugfgjgesthekredttddtjeenucfhrhhomheptehlvhgr rhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh eqnecuggftrfgrthhtvghrnhepkedthfekveduuefgueelteekgfejgeekueduudeuveeu vddufffggfetiefhvdefnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghdpvg hnthgvrhhprhhishgvuggsrdgtohhmnecuvehluhhsthgvrhfuihiivgeptdenucfrrghr rghmpehmrghilhhfrhhomheprghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 6 May 2024 11:56:57 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1715011015; bh=D1+Ezl5OpaJ9ubk1v3Uh3xNdYPuTpEl+ebHOVD88UYA=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=nEeRZIaHev7jNHiTsqKWW5nnE+D4dVgHkGjNZRJ4c4cflPxsFDkUVvvd5Kx2eWUyH 8hC1LwThSU8xZpEx1LHPIfQEDjAwgsl3qDjh38K7FpW6De0JiVpGzQZmURDQAJjclK wb6tQxGIlcXvzjV28ed7V7zy96D+BgMzgw5GsiOkyPJ27qAOvDXCh/n0qBIRxPPvq6 DAObs/zxIGSRWSXdRH0pmxc6ni8sc0ixkcDobPb4Kw8WJCQ0gYiK1aUnnoTRovnkCP 41ooQHbSGRTINdY720/VPio1c25yE0vhndPLNleXJBfsqEIAW3wmvzC59pPtsEEijb FrxtH9zJmal+g== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id F39C28D; Mon, 6 May 2024 17:56:54 +0200 (CEST) Date: Mon, 6 May 2024 17:56:54 +0200 From: Alvaro Herrera To: Justin Pryzby Cc: Robert Haas , Kyotaro Horiguchi , pgsql-hackers@lists.postgresql.org, Alexander Lakhin , Dmitry Koval Subject: Re: pg17 issues with not-null contraints Message-ID: <202405061556.2bgkainuq2cs@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <202405040920.2rg4mmjgv3ch@alvherre.pgsql> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2024-May-04, Alvaro Herrera wrote: > On 2024-May-03, Justin Pryzby wrote: > > > But if it's created with LIKE: > > postgres=# CREATE TABLE t1 (LIKE t); > > postgres=# ALTER TABLE t ATTACH PARTITION t1 DEFAULT ; > > > > ..one also sees: > > > > Not-null constraints: > > "t1_i_not_null" NOT NULL "i" > > Hmm, I think the problem here is not ATTACH; the not-null constraint is > there immediately after CREATE. I think this is all right actually, > because we derive a not-null constraint from the primary key and this is > definitely intentional. But I also think that if you do CREATE TABLE t1 > (LIKE t INCLUDING CONSTRAINTS) then you should get only the primary key > and no separate not-null constraint. That will make the table more > similar to the one being copied. I misspoke -- it's INCLUDING INDEXES that we need here, not INCLUDING CONSTRAINTS ... and it turns out we already do it that way, so with this script CREATE TABLE t (i int PRIMARY KEY) PARTITION BY RANGE (i); CREATE TABLE t1 (LIKE t INCLUDING INDEXES); ALTER TABLE t ATTACH PARTITION t1 DEFAULT ; you end up with this 55432 17devel 71313=# \d+ t Partitioned table "public.t" Column │ Type │ Collation │ Nullable │ Default │ Storage │ Compression │ Stats target │ Description ────────┼─────────┼───────────┼──────────┼─────────┼─────────┼─────────────┼──────────────┼───────────── i │ integer │ │ not null │ │ plain │ │ │ Partition key: RANGE (i) Indexes: "t_pkey" PRIMARY KEY, btree (i) Partitions: t1 DEFAULT 55432 17devel 71313=# \d+ t1 Table "public.t1" Column │ Type │ Collation │ Nullable │ Default │ Storage │ Compression │ Stats target │ Description ────────┼─────────┼───────────┼──────────┼─────────┼─────────┼─────────────┼──────────────┼───────────── i │ integer │ │ not null │ │ plain │ │ │ Partition of: t DEFAULT No partition constraint Indexes: "t1_pkey" PRIMARY KEY, btree (i) Access method: heap which I think is what you want. (Do you really want the partition to be created without the primary key already there?) Now maybe in https://www.postgresql.org/docs/devel/sql-createtable.html we need some explanation for this. Right now we have INCLUDING INDEXES Indexes, PRIMARY KEY, UNIQUE, and EXCLUDE constraints on the original table will be created on the new table. Names for the new indexes and constraints are chosen according to the default rules, regardless of how the originals were named. (This behavior avoids possible duplicate-name failures for the new indexes.) Maybe something like this before the naming considerations: When creating a table like another that has a primary key and indexes are excluded, a not-null constraint will be added to every column of the primary key. resulting in INCLUDING INDEXES Indexes, PRIMARY KEY, UNIQUE, and EXCLUDE constraints on the original table will be created on the new table. [When/If ?] indexes are excluded while creating a table like another that has a primary key, a not-null constraint will be added to every column of the primary key. Names for the new indexes and constraints are chosen according to the default rules, regardless of how the originals were named. (This behavior avoids possible duplicate-name failures for the new indexes.) What do you think? -- Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/