Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p1G1w-0005Rf-SW for pgsql-hackers@arkaria.postgresql.org; Sat, 03 Dec 2022 00:05:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1p1G1v-0001zS-Kl for pgsql-hackers@arkaria.postgresql.org; Sat, 03 Dec 2022 00:05:19 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p1G1v-0001xy-6H for pgsql-hackers@lists.postgresql.org; Sat, 03 Dec 2022 00:05:19 +0000 Received: from sender4-op-o11.zoho.com ([136.143.188.11]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p1G1s-0004y5-J9 for pgsql-hackers@lists.postgresql.org; Sat, 03 Dec 2022 00:05:18 +0000 ARC-Seal: i=1; a=rsa-sha256; t=1670025910; cv=none; d=zohomail.com; s=zohoarc; b=jSughnMVicVSW66EKDhwh+oAxoEdjoHSuA9iScXBqmXblFPJYLYVf9UkUWDHSjV1jYyjQ7t2JmLGCet4LV7j6xO3pYoVHa4qMwPOiERHQI+fLt8ZKwZq9j1p2/bf2LZHLAOAhhKNPrIS2wwN2xVfB0HA2mEclHgCCtq+mkgRrMA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1670025910; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=aJ1y44xsFPeMLcSQUlbs9FGxc9AZha/UIz7KN4LDclU=; b=WZOvnXQ8JLSt9EYxF4gmzA/fUQmbVgI0wv75F+X7EO4dXNjkLKPddOTWvwM415+3HhVPaLVCJQAVPrzChFZEuYqFwu3cAwlYpxsySLgaR7Ffg37f1Sd8DAppdMO125UWntdvy7aETNDWVW3MYzkLRx3DE+Z8/G2hVD9Uy2swhw8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=highgo.ca; spf=pass smtp.mailfrom=david.zhang@highgo.ca; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1670025910; s=zoho; d=highgo.ca; i=david.zhang@highgo.ca; h=Message-ID:Date:Date:MIME-Version:Subject:Subject:To:To:Cc:Cc:References:From:From:In-Reply-To:Content-Type:Content-Transfer-Encoding:Message-Id:Reply-To; bh=aJ1y44xsFPeMLcSQUlbs9FGxc9AZha/UIz7KN4LDclU=; b=gED8q6cD8Q8bb5JinD7zA/PG+Mw2pNmtBTZ71hglgCyHN2e7mtukVZW17RIZ8BRx EUDygFK49A7XgOrkwtL8wPLQAdYWAdBqGuvUZAl7Xiv6jIoYcBIczUYK7rGsiIdBwXb ZlMpW0tCi9Fo5tTgBW2hSEXBg/6tunWPz1iHy+yk= Received: from [192.168.1.88] (d66-183-207-156.bchsia.telus.net [66.183.207.156]) by mx.zohomail.com with SMTPS id 16700259093101013.2543465186928; Fri, 2 Dec 2022 16:05:09 -0800 (PST) Message-ID: <6b69efba-5f4f-04a5-46b4-b92e38cbb69b@highgo.ca> Date: Fri, 2 Dec 2022 16:05:08 -0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.5.1 Subject: Re: Patch: Global Unique Index Content-Language: en-US To: Tom Lane , Bruce Momjian Cc: Greg Stark , Simon Riggs , Cary Huang , Pgsql Hackers References: <184879c5306.12490ea581628934.7312528450011769010@highgo.ca> <46a0f6fb-1c0b-bbd8-ef3e-366216a89501@highgo.ca> <1239139.1669763636@sss.pgh.pa.us> <1257837.1669774583@sss.pgh.pa.us> From: David Zhang In-Reply-To: <1257837.1669774583@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-ZohoMailClient: External List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2022-11-29 6:16 p.m., Tom Lane wrote: > Assuming that you are inserting into index X, and you've checked > index Y to find that it has no conflicts, what prevents another > backend from inserting a conflict into index Y just after you look? > AIUI the idea is to prevent that by continuing to hold an exclusive > lock on the whole index Y until you've completed the insertion. > Perhaps there's a better way to do that, but it's not what was > described. Another main change in patch `0004-support-global-unique-index-insert-and-update.patch`, +                search_global: +                        stack = _bt_search(iRel, insertstate.itup_key, +                                           &insertstate.buf, BT_READ, NULL); +                        xwait = _bt_check_unique_gi(iRel, &insertstate, +                                                    hRel, checkUnique, &is_unique, + &speculativeToken, heapRel); +                        if (unlikely(TransactionIdIsValid(xwait))) +                        { ... ... +                            goto search_global; +                        } Here, I am trying to use `BT_READ` to require a LW_SHARED lock on the buffer block if a match found using `itup_key` search key. The cross-partition uniqueness checking will wait if the index tuple insertion on this buffer block has not done yet, otherwise runs the uniqueness check to see if there is an ongoing transaction which may insert a conflict value. Once the ongoing insertion is done, it will go back and check again (I think it can also handle the case that a potential conflict index tuple was later marked as dead in the same transaction). Based on this change, my test results are: 1) a select-only query will not be blocked by the ongoing insertion on index X 2) insertion happening on index Y may wait for the buffer block lock when inserting a different value but it does not wait for the transaction lock held by insertion on index X. 3) when an insertion inserting a conflict value on index Y,     3.1) it waits for buffer block lock if the lock has been held by the insertion on index X.     3.2) then, it waits for transaction lock until the insertion on index X is done.