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 1sAOz3-009X0u-QJ for pgsql-general@arkaria.postgresql.org; Fri, 24 May 2024 07:05:00 +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 1sAOz2-003blC-D0 for pgsql-general@arkaria.postgresql.org; Fri, 24 May 2024 07:04:56 +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 1sAOz1-003bl4-Fq for pgsql-general@lists.postgresql.org; Fri, 24 May 2024 07:04:56 +0000 Received: from outgoing6.cpt4.host-h.net ([197.189.247.39]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sAOyw-000VOk-E7 for pgsql-general@postgresql.org; Fri, 24 May 2024 07:04:53 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=exa.co.za; s=xneelo; h=Content-Transfer-Encoding:Content-Type:MIME-Version:Message-ID: Date:Subject:In-Reply-To:References:Cc:To:From:reply-to:sender:bcc; bh=S6PnvN6Ih9sbKJ/zkqNcwzg0e4Fu1M5lv4rMt5HOMwU=; b=pm+Dwn+S9Q/J9yCHORAghfwT/P VOjGcDXPmGG1w23w8+xJtWjwpNMrBMVLNw60n3I8r5XsxCNVW0BHftRGV4/l8gIV7ONaXM2QuKBzZ nxJhrSdq6bj5re0Ml4jMECBaAXIHucNz0PoDLXF8FbRcTJtimN5caOYMwuHsbnx+DZYkJLp4Qr+5J du21RBpyhsBFmUzzJdZqXrumwM1vn3g920GUczk556PblIH/fsONT0k+qGmh3DuQ4vpSi2Jq1Aco5 XFLzYNJ5aKiHClYAbinfWAARHvlq91vPBNzDfz7DUlkZvlvqPv2AsFieo91auhZ8T8kAy6xgX5m4J X9/KdkRQ==; Received: from www20.cpt1.host-h.net ([197.221.2.20]) by antispam3-cpt4.host-h.net with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1sAOyo-00GN6C-CJ; Fri, 24 May 2024 09:04:46 +0200 Received: from [155.93.228.209] (helo=DMZHOST) by www20.cpt1.host-h.net with esmtpa (Exim 4.92) (envelope-from ) id 1sAOyl-000EDS-A1; Fri, 24 May 2024 09:04:39 +0200 Received: from M6800 (M6800 [10.0.0.22]) by DMZHOST with ESMTPSA (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256) ; Fri, 24 May 2024 09:04:36 +0200 From: To: "'Tom Lane'" Cc: References: <420a01daad70$fcf2bd80$f6d83880$@exa.co.za> <561365.1716517130@sss.pgh.pa.us> In-Reply-To: <561365.1716517130@sss.pgh.pa.us> Subject: RE: Strange issue with unique index Date: Fri, 24 May 2024 09:04:35 +0200 Message-ID: <425e01daada8$a2e664d0$e8b32e70$@exa.co.za> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 16.0 Thread-Index: AQI/VJhda0vQCfLX0nyYqjXQ5SpWMQEkEtpgsNOFFEA= Content-Language: en-za X-Authenticated-Sender: smtprelay@exa.co.za X-Virus-Scanned: Clear X-Originating-IP: 197.221.2.20 X-SpamExperts-Domain: exa.co.za X-SpamExperts-Username: Authentication-Results: host-h.net; auth=pass (login) smtp.auth=@exa.co.za X-SpamExperts-Outgoing-Class: ham X-SpamExperts-Outgoing-Evidence: Combined (0.29) X-Recommended-Action: accept X-Filter-ID: Pt3MvcO5N4iKaDQ5O6lkdGlMVN6RH8bjRMzItlySaT/DKGsPjeT26WK53nfOMyAbPUtbdvnXkggZ 3YnVId/Y5jcf0yeVQAvfjHznO7+bT5xoCUr8po8rYOi4iR4Fgs3wXpWRTn3tkrFwtVscNx/s0PEK BNixaDl+r20LrCg/HK6s6hv6CgrxFcAu0asvIjgFHTXW09VphYXqoAJejID3P+KVse1sVhWabI0/ +PN3sIJwaamb6PEvxJYegyRXvOeQ4cjk/9+WHQlr0vHFUfT/VdUsBYy9/rzcLHKKAxZKeFhDsxrI TNHkeyCKHfigPec+UvbCytJ71grOBU/bldHg3yVhZ5v6kfUH1ukRPj6Sf7CJmsKPpRnNm4ODaPDt yNzc9mwSGfNaBmafifTJSMMwOH75IHefrt4A8X/Xytq/2cHcjNWYY9Jhwjc/H4SLVZROH9u8bwQR 7T6cYAlUQy2vpH/S0BGIotuwG/h4FqtFRE3jF3AHmuxI4tnczlBU/A3TOvaFIleLtlpKvviEAVoR jvoF/LVPc/CxSizXI1h1M4bXs2KsRjKrCowEavDwQuKoVsGlTwCCSjQR627sJLZx4bJqSLaNjuCT tn8dYCLjR4L+m5+adqXVtPdiKJBvcgWErgO3SBMQr286U6+kVdeM7K5yPvfmYVwWqhzjLKy9AzwB IkUL/j1Y48GvmeURQjjEdT59QmGFjNgQHfGqhmGlE+XgXpzV+pfd2zcgOvRRVO48kfSPSLF5+GHr fdaQPF/SAkf5M1BtJw8CbHRtlUh7niOKVz2JVPIYERpOUGVhJbGt85jdQ1W7xM52M4KvSDibaNLi eeJ5FbiAFf8bGl1uIOtzomtncKYxkO/KnKIVf/EZeEFATWHznKQZG2PgcuxCfIfxogdJ5DXlg79z Ihgj6MA/9xGgj7taJxWtHZ97uYqBL6o5Zpk0q7XPy5FIqPMG647lNwN4qOsSZg+fYhVZG2Fl3sSl ufDq5ij7urjLQBzcYLFtwBE1w33MuDUKAALB2hNudIch77xWAFOJDWtscqLyCc35VA7RvW/HGiGq xL0QnAWd32gCLUIzoLdn4a8Z7Qyi59xK6Xd8GdZjDkCXwA== X-Report-Abuse-To: spam@antispamquarantine.host-h.net List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk writes: >> I've run into a strange issue with a unique index that I'm struggling >> to understand. I've extracted the basic info to reproduce this below. >> ... >> This will now block until session 2 is complete. I don't understand >> why this would block. I do know it's that unique index causing the >> issue, but I need the unique index in place. >No, it's not about the unique index. It's about the foreign key constraint --- if you remove that, there is no blockage. The reason why that's happening is that the insertions of >dependent child rows acquire row locks on the FK-referenced tuple, to prevent that row from going away before the insertions commit. So when you then decide to UPDATE >the referenced row, that blocks on the other session's row lock. >You can make things a little better, at the cost of more overhead, by declaring the FK as DEFERRABLE INITIALLY DEFERRED. Thanks for the swift response. No concern using a Deferred FK here, because it's not a hight TPS area. And it does work tx. What is just interesting is that this does not happen with that unique index in place. If I run that scenario with a normal FK and without the unique index there is also no blocking happening. So it does look like PG is smart enough in the normal flow, but not with the unique index in place. In my real world table I could also work around it by making the unique index filtered to exclude this type of update (There is another flag on the table). This scenario does just bring up questions in other parts of our system, because we have a few that has this structure of parent/child with unique index on parent and updates on both levels. Regards Riaan Stander