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 1s6SeU-00C4w6-0I for pgsql-hackers@arkaria.postgresql.org; Mon, 13 May 2024 10:11:26 +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 1s6SeR-00HFhH-HB for pgsql-hackers@arkaria.postgresql.org; Mon, 13 May 2024 10:11:23 +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 1s6SeP-00HFge-7I for pgsql-hackers@lists.postgresql.org; Mon, 13 May 2024 10:11:23 +0000 Received: from wfout3-smtp.messagingengine.com ([64.147.123.146]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s6SeL-000mof-L5 for pgsql-hackers@lists.postgresql.org; Mon, 13 May 2024 10:11:20 +0000 Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailfout.west.internal (Postfix) with ESMTP id 8CDBF1C00124; Mon, 13 May 2024 06:11:14 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute7.internal (MEProxy); Mon, 13 May 2024 06:11:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eisentraut.org; h=cc:cc:content-transfer-encoding:content-type:content-type :date:date:from:from:in-reply-to:in-reply-to:message-id :mime-version:references:reply-to:subject:subject:to:to; s=fm2; t=1715595074; x=1715681474; bh=C370Px4wUbotGtUmFRc2X/vWcvKvubv/ fwbjn+umsJ8=; b=BOhd1a2TLbm7pOXpQ5u7vIZYjyONUQx/JYCRpcM1esuB+UHe qTPtcD7mr4+sZNAGxwSeNKexc/FwIgAT6PES+L7CohhLczHzZ2PjLp1ae5l8BnHP lcgCAarD8C1uStBRW5M6VMbngKadbA/3amjA4IJ8mkkUyFk2pwpC7w85h/lnhfVV txr7XPzUKK92uQbOlGp9n/Xm9yr1Nw/aWNsI0neM/wD1ZCze3P9wsFqlIX42JBBY E/1X1/ERTZgJI3XHsYUROrPChxwEd1T8HsfvkkiA5W43ghVsolBZRwmX3TESA0Xe 7k9EsRZVCefBy8Z7dCysy/M302nql719ExhbbQ== 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 :references:reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1715595074; x= 1715681474; bh=C370Px4wUbotGtUmFRc2X/vWcvKvubv/fwbjn+umsJ8=; b=D zbD7eC+f6C9ICu+n9GVlOxwWebFVZkUxYo4q+j3PXHs4slzylZUBlmArTxOuqBxf ZMCfkF5ZV8wQWVuoc7bn67lr5zehgvPZySgFeHK7HsDtRjqpn4p6PcYX0tqCDlDS d6FBFah96CS3wfkaAQvPtWpiYy3EbDv4BxnbeX53KeVSCM+Wae9Ofgf45G0Qgbvf JiTKXpv3bJcXlBivq/VlbnA9E0tGoGgX3h1HHDZnIJNU7IvvncN6uqKEuALKqUyw Bhtqc3A3utEeuiMw38z0dEMBi8uWceAGXbJu1WvbyV9Ur5jTAWXfXCBtAIFOLGGu Y1M6g/nxpOdbiB55AntDA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdeggedgvdehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepkfffgggfuffvvehfhfgjtgfgsehtkeertddtvdejnecuhfhrohhmpefrvght vghrucfgihhsvghnthhrrghuthcuoehpvghtvghrsegvihhsvghnthhrrghuthdrohhrgh eqnecuggftrfgrthhtvghrnhepjefhveehtdetgfffhffhfeefgffghffflefgieeuueek hedvhedvfeehffdvfeeunecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomhepphgvthgvrhesvghishgvnhhtrhgruhhtrdhorhhg X-ME-Proxy: Feedback-ID: ie0a040ee:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 13 May 2024 06:11:13 -0400 (EDT) Message-ID: <64c2b2ab-7ce9-475e-ac59-3bfec528bada@eisentraut.org> Date: Mon, 13 May 2024 12:11:11 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: SQL:2011 application time To: Paul Jungwirth , jian he Cc: PostgreSQL Hackers References: <85dd3ad0-fa79-46e8-a08b-c426c76825f3@eisentraut.org> <49c07eac-18ec-4aff-929c-e81b87df1092@eisentraut.org> <1edb3b79-a4bc-43b1-80aa-2e090472fe32@eisentraut.org> <47550967-260b-4180-9791-b224859fe63e@illuminatedcomputing.com> From: Peter Eisentraut Content-Language: en-US In-Reply-To: <47550967-260b-4180-9791-b224859fe63e@illuminatedcomputing.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 03.04.24 07:30, Paul Jungwirth wrote: > But is it *literally* unique? Well two identical keys, e.g. (5, > '[Jan24,Mar24)') and (5, '[Jan24,Mar24)'), do have overlapping ranges, > so the second is excluded. Normally a temporal unique index is *more* > restrictive than a standard one, since it forbids other values too (e.g. > (5, '[Jan24,Feb24)')). But sadly there is one exception: the ranges in > these keys do not overlap: (5, 'empty'), (5, 'empty'). With > ranges/multiranges, `'empty' && x` is false for all x. You can add that > key as many times as you like, despite a PK/UQ constraint: > >     postgres=# insert into t values >     ('[1,2)', 'empty', 'foo'), >     ('[1,2)', 'empty', 'bar'); >     INSERT 0 2 >     postgres=# select * from t; >       id   | valid_at | name >     -------+----------+------ >      [1,2) | empty    | foo >      [1,2) | empty    | bar >     (2 rows) > > Cases like this shouldn't actually happen for temporal tables, since > empty is not a meaningful value. An UPDATE/DELETE FOR PORTION OF would > never cause an empty. But we should still make sure they don't cause > problems. > We should give temporal primary keys an internal CHECK constraint saying > `NOT isempty(valid_at)`. The problem is analogous to NULLs in parts of a > primary key. NULLs prevent two identical keys from ever comparing as > equal. And just as a regular primary key cannot contain NULLs, so a > temporal primary key should not contain empties. > > The standard effectively prevents this with PERIODs, because a PERIOD > adds a constraint saying start < end. But our ranges enforce only start > <= end. If you say `int4range(4,4)` you get `empty`. If we constrain > primary keys as I'm suggesting, then they are literally unique, and > indisunique seems safer. > > Should we add the same CHECK constraint to temporal UNIQUE indexes? I'm > inclined toward no, just as we don't forbid NULLs in parts of a UNIQUE > key. We should try to pick what gives users more options, when possible. > Even if it is questionably meaningful, I can see use cases for allowing > empty ranges in a temporal table. For example it lets you "disable" a > row, preserving its values but marking it as never true. It looks like we missed some of these fundamental design questions early on, and it might be too late now to fix them for PG17. For example, the discussion on unique constraints misses that the question of null values in unique constraints itself is controversial and that there is now a way to change the behavior. So I imagine there is also a selection of possible behaviors you might want for empty ranges. Intuitively, I don't think empty ranges are sensible for temporal unique constraints. But anyway, it's a bit late now to be discussing this. I'm also concerned that if ranges have this fundamental incompatibility with periods, then the plan to eventually evolve this patch set to support standard periods will also have as-yet-unknown problems. Some of these issues might be design flaws in the underlying mechanisms, like range types and exclusion constraints. Like, if you're supposed to use this for scheduling but you can use empty ranges to bypass exclusion constraints, how is one supposed to use this? Yes, a check constraint using isempty() might be the right answer. But I don't see this documented anywhere. On the technical side, adding an implicit check constraint as part of a primary key constraint is quite a difficult implementation task, as I think you are discovering. I'm just reminded about how the patch for catalogued not-null constraints struggled with linking these not-null constraints to primary keys correctly. This sounds a bit similar. I'm afraid that these issues cannot be resolved in good time for this release, so we should revert this patch set for now.