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 1s2ZCB-0047KE-Iu for pgsql-hackers@arkaria.postgresql.org; Thu, 02 May 2024 16:22:07 +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 1s2ZC8-002Ei5-Ox for pgsql-hackers@arkaria.postgresql.org; Thu, 02 May 2024 16:22:05 +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.94.2) (envelope-from ) id 1s2ZC7-002Ehw-Kp for pgsql-hackers@lists.postgresql.org; Thu, 02 May 2024 16:22:05 +0000 Received: from fhigh6-smtp.messagingengine.com ([103.168.172.157]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s2ZC4-001CIi-CF for pgsql-hackers@lists.postgresql.org; Thu, 02 May 2024 16:22:03 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailfhigh.nyi.internal (Postfix) with ESMTP id A28CB11400E4; Thu, 2 May 2024 12:21:58 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Thu, 02 May 2024 12:21:58 -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=1714666918; x= 1714753318; bh=gudZCgYHyjVUSC8bCzYUHwzXwn13bRZ3oHDjX/0L9cM=; b=A eWGe9SihKweTV6QVdSZPFaXfJMyMqbgZlqq5/O2usu4bevmJeRlgvrwzdmo0XuIB EbfpIz6pDcRTQyt16uqpTGPZC7Ekgwmv0nToft7LqVmGXpgr2opvNpMeks0GNkh2 ZrLOvW8bpg5rsDVdnl08R9heTZaKaUshtfJiPisZEe3uqAWNcKRL5FOHb0umqFxN NEomLV2KkQQe6akcg0CJKu73YvG+goCjqfQ8+m0zd1itT1oZeB2L03JST2ZF7eyX MtfjORCwBWx8/jr4JiE7/jjmCTVT3/KiPHP4H1BiCTXNVcjl8XKEJwxxq5eXiKqJ UYfhZ6uAEHoXFFAnbGQDA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvddukedguddtudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvvefukfggtggugfgjsehtkeertddttdejnecuhfhrohhmpeetlhhv rghrohcujfgvrhhrvghrrgcuoegrlhhvhhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorh hgqeenucggtffrrghtthgvrhhnpedvudeftdfgvedvgfffgefhveduvdeghfdugedufffh geduudettedthfdvgeffheenucffohhmrghinhepvghnthgvrhhprhhishgvuggsrdgtoh hmpdhtfihithhtvghrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghm pehmrghilhhfrhhomheprghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 2 May 2024 12:21:58 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1714666916; bh=h2JFAoSyZYrSg6QFV8hc9OwqIZP2NGfATmKaz0fAIXU=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=khcYomE8PO+B+s6UDkTsdZuwaCDaO/4SHYAksA1cpjZy1MEI5b168Mp/HKmFkZ/IC AiU4eA+txg6/7C+bsw8aV0m9FWnmub0nGUuefb1tcNL7kUgbEqPXQLII271VdE1ED6 MLd+YGDqd52z63yDGE0cIi/AIqU7VT08w90JMYiZ6Cjdzc+GCIjGjTBnu3IYGtFG44 nKZ0DePLkrzzDzvAe9yX3Ywc2rJBFX0Vr1iIrl4NUYFmUfwLqiso+Rwbax0e3FwJhO EzYn4F7cs9ercQ4MKrgtqty3GavjlyT1q61dOX5QWHlmh1rUL6cGrCyZ7d83daiuHs 8LGbY/gMHnb5w== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id 5846ACD; Thu, 2 May 2024 18:21:56 +0200 (CEST) Date: Thu, 2 May 2024 18:21:56 +0200 From: Alvaro Herrera To: Alexander Lakhin Cc: Dean Rasheed , Andrew Bille , Peter Eisentraut , Robert Haas , pgsql-hackers@lists.postgresql.org Subject: Re: cataloguing NOT NULL constraints Message-ID: <202405021621.ftmuhmfg7f2j@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <83bfef0d-3e3c-e4c7-f6c4-29db5e632883@gmail.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello Alexander On 2024-May-02, Alexander Lakhin wrote: > Could you also clarify, please, how CREATE TABLE ... LIKE is expected to > work with NOT NULL constraints? It should behave identically to 16. If in 16 you end up with a not-nullable column, then in 17 you should get a not-null constraint. > I wonder whether EXCLUDING CONSTRAINTS (ALL) should cover not-null > constraints too. What I'm seeing now, is that: > CREATE TABLE t1 (i int, CONSTRAINT nn NOT NULL i); > CREATE TABLE t2 (LIKE t1 EXCLUDING ALL); > \d+ t2 > -- ends with: > Not-null constraints: >     "nn" NOT NULL "i" In 16, this results in Table "public.t2" Column │ Type │ Collation │ Nullable │ Default │ Storage │ Compression │ Stats target │ Description ────────┼─────────┼───────────┼──────────┼─────────┼─────────┼─────────────┼──────────────┼───────────── i │ integer │ │ not null │ │ plain │ │ │ Access method: heap so the fact that we have a not-null constraint in pg17 is correct. > Or a similar case with PRIMARY KEY: > CREATE TABLE t1 (i int PRIMARY KEY); > CREATE TABLE t2 (LIKE t1 EXCLUDING CONSTRAINTS EXCLUDING INDEXES); > \d+ t2 > -- leaves: > Not-null constraints: >     "t2_i_not_null" NOT NULL "i" Here you also end up with a not-nullable column in 16, so I made it do that. Now you could argue that EXCLUDING CONSTRAINTS is explicit in saying that we don't want the constraints; but in that case why did 16 mark the columns as not-null? The answer seems to be that the standard requires this. Look at 11.3 syntax rule 9) b) iii) 4): 4) If the nullability characteristic included in LCDi is known not nullable, then let LNCi be NOT NULL; otherwise, let LNCi be the zero-length character string. where LCDi is "1) Let LCDi be the column descriptor of the i-th column of LT." and then 5) Let CDi be the LCNi LDTi LNCi Now, you could claim that the standard doesn't mention INCLUDING/EXCLUDING CONSTRAINTS, therefore since we have come up with its definition then we should make it affect not-null constraints. However, there's also this note: NOTE 520 — s, except for NOT NULL, are not included in CDi; s are effectively transformed to
s and are thereby also excluded. which is explicitly saying that not-null constraints are treated differently; in essence, with INCLUDING CONSTRAINTS we choose to affect the constraints that the standard says to ignore. Thanks for looking! -- Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/ "Learn about compilers. Then everything looks like either a compiler or a database, and now you have two problems but one of them is fun." https://twitter.com/thingskatedid/status/1456027786158776329