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 1sWI3i-00EJ8U-7P for pgsql-hackers@arkaria.postgresql.org; Tue, 23 Jul 2024 16:08:14 +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 1sWI3g-00Eel4-7F for pgsql-hackers@arkaria.postgresql.org; Tue, 23 Jul 2024 16:08:12 +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 1sWI3f-00EekX-NX for pgsql-hackers@lists.postgresql.org; Tue, 23 Jul 2024 16:08:12 +0000 Received: from mail-il1-x132.google.com ([2607:f8b0:4864:20::132]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sWI3d-00156f-7q for pgsql-hackers@lists.postgresql.org; Tue, 23 Jul 2024 16:08:11 +0000 Received: by mail-il1-x132.google.com with SMTP id e9e14a558f8ab-3992195d08aso19073005ab.0 for ; Tue, 23 Jul 2024 09:08:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=illuminatedcomputing-com.20230601.gappssmtp.com; s=20230601; t=1721750887; x=1722355687; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id:from:to :cc:subject:date:message-id:reply-to; bh=i9ekOFGBzwkaLofiqcnyGrvt6YzjdR/tWkhR0cMd+kk=; b=NC6rq4yANiU0k/5EVUCL7q4W6cQ2O0R89XPYZAPjznwyc9KnOR+S8Lq1VOpJanNpWC jidXcYgFPYHifddUQtcZKZXIagXjcZjhZMuIuHlk9GlobWnZ92sVGHMeFkxGhm3uVa+B 8/VZqQeGidb/jMwiiu7yiitf/W6pdGbZ/FTfTG/O4AdanxIUj16qB0Y9LOaCFKqA1YJy czfR6AjKUyanB74W49O2OHJRAtYxwugGNv+bcvUUEqLLh49ZfJZ8diXPBM2oytUV4AjG 8Z90Q2ZWYvktfz5J7XX8fF2NyWqLkIgXuqGu5Rq3HN/t9g7fhLocvdMuHVoP1LHkpShm SOaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721750887; x=1722355687; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=i9ekOFGBzwkaLofiqcnyGrvt6YzjdR/tWkhR0cMd+kk=; b=UxvrtYGuzaSWZ+BTo8GhMlvxDR4OXlQ7oJOVjsekVTuldjMOjd1pn98sw6PVmBSLw0 miE+zoRWARfkvzWPfrysUE+V73XscLl7oL8tYI5o0160oOGmveA44yduHKx/vAkbeq9G nNI+ClllHj8uOHokJZOlYKc+Dceakzc9vldDGrD6B88l08uH8XcCvXWZy0dCgDO3PcVY 9xlduiyd87G61Dk0FllZKG0QZhiMCdjhjSnpTUxsaFBj5W31wkvUvgIxr8mA9NGOMZQr dcdeYNSlmkj26kPBRrKEiIGdohQlJc7Pr98TxW3BIvCBkx8pXo7k5lLuv2iYlKzUSdTG 5daQ== X-Forwarded-Encrypted: i=1; AJvYcCWg9EeSSi4Afa2a9rcpWBnso+J/Xkwg1uNe1qtsTpWxIYKLL3slK3/JWRVdQ2fNNYhS/sULsrdEOLwsC/3CwLth7kaN4uo95t7GvpY3fJ04SPvJ X-Gm-Message-State: AOJu0YxEeWr1K7Gs7ifSKRpd/yWE0eIOLsds0ykuNJ3DHt4ZNdxc9poN 0tSP7TwS3OGdEeAP/mYgpttXCaI+W+8apiElpioNkH+aIo6rY5cWjJS3ilm3b5w= X-Google-Smtp-Source: AGHT+IHMjpoooqRiRn2tlU7VBg9qfzrBAfJ+efvuwMqzQ4JiHBmrpUPeWtMDm6Y2ybDWCHuyGhKC8g== X-Received: by 2002:a05:6e02:12cc:b0:375:9e3f:5f6 with SMTP id e9e14a558f8ab-39a16c1dac5mr5390555ab.6.1721750887100; Tue, 23 Jul 2024 09:08:07 -0700 (PDT) Received: from [192.168.2.139] ([50.39.255.79]) by smtp.googlemail.com with ESMTPSA id d2e1a72fcca58-70d21809a7bsm4110057b3a.203.2024.07.23.09.08.06 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 23 Jul 2024 09:08:06 -0700 (PDT) Message-ID: Date: Tue, 23 Jul 2024 09:08:05 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: SQL:2011 application time From: Paul Jungwirth To: jian he Cc: Isaac Morland , Robert Haas , Jeff Davis , Peter Eisentraut , PostgreSQL Hackers References: <49c07eac-18ec-4aff-929c-e81b87df1092@eisentraut.org> <1edb3b79-a4bc-43b1-80aa-2e090472fe32@eisentraut.org> <47550967-260b-4180-9791-b224859fe63e@illuminatedcomputing.com> <64c2b2ab-7ce9-475e-ac59-3bfec528bada@eisentraut.org> <58b6687fe9de002e4c862a412719e4f918bad987.camel@j-davis.com> <30dc3561-d7ab-42be-a1c5-096643423590@illuminatedcomputing.com> Content-Language: en-US In-Reply-To: <30dc3561-d7ab-42be-a1c5-096643423590@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 7/18/24 11:39, Paul Jungwirth wrote: > So I swapped in the &&& patch, cleaned it up, and added tests. But something is wrong. After I get > one failure from an empty, I keep getting failures, even though the table is empty: > > regression=# truncate temporal_rng cascade; > NOTICE:  truncate cascades to table "temporal_fk_rng2rng" > TRUNCATE TABLE > regression=# insert into temporal_rng values ('[1,2)', '[2000-01-01,2010-01-01)'); -- ok so far > INSERT 0 1 > regression=# insert into temporal_rng values ('[1,2)', 'empty'); -- should fail and does > ERROR:  range cannot be empty > regression=# insert into temporal_rng values ('[1,2)', '[2010-01-01,2020-01-01)'); -- uh oh > ERROR:  range cannot be empty > regression=# truncate temporal_rng cascade; > NOTICE:  truncate cascades to table "temporal_fk_rng2rng" > TRUNCATE TABLE > regression=# insert into temporal_rng values ('[1,2)', '[2000-01-01,2010-01-01)'); -- ok so far > INSERT 0 1 > regression=# insert into temporal_rng values ('[1,2)', '[2010-01-01,2020-01-01)'); -- ok now > INSERT 0 1 > > It looks like the index is getting corrupted. Continuing from the above: > > regression=# create extension pageinspect; > CREATE EXTENSION > regression=# select gist_page_items(get_raw_page('temporal_rng_pk', 0), 'temporal_rng_pk'); >                               gist_page_items > ---------------------------------------------------------------------------- >  (1,"(0,1)",40,f,"(id, valid_at)=(""[1,2)"", ""[2000-01-01,2010-01-01)"")") >  (2,"(0,2)",40,f,"(id, valid_at)=(""[1,2)"", ""[2010-01-01,2020-01-01)"")") > (2 rows) > > regression=# insert into temporal_rng values ('[1,2)', 'empty'); > ERROR:  range cannot be empty > regression=# select gist_page_items(get_raw_page('temporal_rng_pk', 0), 'temporal_rng_pk'); >                               gist_page_items > ---------------------------------------------------------------------------- >  (1,"(0,1)",40,f,"(id, valid_at)=(""[1,2)"", ""[2000-01-01,2010-01-01)"")") >  (2,"(0,2)",40,f,"(id, valid_at)=(""[1,2)"", ""[2010-01-01,2020-01-01)"")") >  (3,"(0,3)",32,f,"(id, valid_at)=(""[1,2)"", empty)") > (3 rows) I realized this isn't index corruption, just MVCC. The exclusion constraint is checked after we update the index, which is why the row gets left behind. But it doesn't cause any wrong answers, and if you vacuum the table the row goes away. This also explains my confusion here: > I thought of a possible problem: this operator works great if there are already rows in the table, > but what if the *first row you insert* has an empty range? Then there is nothing to compare against, > so the operator will never be used. Right? > > Except when I test it, it still works! The first row still does a comparison because when we check the exclusion constraint, there is a comparison between the query and the key we just inserted. (When I say "query" I don't mean a SQL query, but the value used to search the index that is compared against its keys.) So I'm glad I didn't stumble on a GiST bug, but I think it means ereporting from an exclusion operator is not a workable approach. Failures leave behind invalid tuples, and future (valid) tuples can fail if we compare to those invalid tuples. Since MVCC visibility is stored in the heap, not in the index, it's not really accessible to us here. So far I don't have any ideas to rescue this idea, even though I like it a lot. So I will go back to the executor idea we discussed at pgconf.dev. One tempting alternative though is to let exclusion constraints do the not-empty check, instead of putting it in the executor. It would be an extra check we do only when the constraint has pg_constraint.conperiod. Then we don't need to add & maintain pg_class.relwithoutoverlaps, and we don't need a relcache change, and we don't need so much extra code to check existing rows when you add the constraint. It doesn't use the existing available exclusion constraint functionality, but if we're willing to extend the executor to know about WITHOUT OVERLAPS, I guess we could teach exclusion constraints about it instead. Doing the check there does seem to have better locality with the feature. So I think I will try that out as well. Yours, -- Paul ~{:-) pj@illuminatedcomputing.com