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 1q9ZJ2-0002XD-Je for pgsql-bugs@arkaria.postgresql.org; Wed, 14 Jun 2023 22:49:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q9ZJ1-0007Ek-Cu for pgsql-bugs@arkaria.postgresql.org; Wed, 14 Jun 2023 22:49:35 +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 1q9ZJ0-0007EY-Db for pgsql-bugs@lists.postgresql.org; Wed, 14 Jun 2023 22:49:35 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1q9ZIx-002ErQ-4B for pgsql-bugs@lists.postgresql.org; Wed, 14 Jun 2023 22:49:33 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 5E9E35C01A7; Wed, 14 Jun 2023 18:49:30 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Wed, 14 Jun 2023 18:49:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; 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:sender:subject:subject:to:to; s=fm2; t= 1686782970; x=1686869370; bh=VHxQVqVdWhhHIKM0zcOhputy+qxLTFsbRLJ n31JUj6k=; b=PjNaw3uLFZ8Nkp/Z8V1as6WprpH2xQ6y6ipTNUQ8YaJ5DDoGXEy uhdI1ZaK4+ZVLW1m98BFIGC1S8jrPYWaogqjWaA/zZj4qeYXslDoxjtDw/URCuPz 5K07NGJlDGpvd7bXqdJ6v9TBiPI1yaQV40KbpyPgEJBSkjpr5s9CMGb1iti1ZhAb TxeagQ9JtzNIY4q3Q8WJDyarS/T70bjxWPPE492rkB4wqfoC49+Y2ttESJ033zxS SH/n1rSp0dlP5fu/9XOeZG11VLNNNRxb5l3zgao49weR3TlF9b02p8ECkDkDHns5 qdpla9jiMVTvfACsOMnuqcjOXqkyfnjuXwA== 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:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1686782970; x=1686869370; bh=VHxQVqVdWhhHIKM0zcOhputy+qxLTFsbRLJ n31JUj6k=; b=rHAAhRz2UOdxMuP7aWGl/wJiF89TqdilSlEMTLeI/6E7VGaoSRK xVYHJbYlAur9y3Nw2zPfhVdbXuBqW0l+koDkkEwmbj9ENMwNgPlvOZVpw7JJFMEl vYWDPQx93ZUbO5hVE5AvF/Od5P2xKGvqzKhVmYKu6ROQwEaq4idtGVn4gYSyYlxG H5DcKMFTHuYVHTzEg+LISTjBMCFqX/jw2j0WqZHcMtA81Nnk/QhpVCbNJC3f/WY4 HGB584yPrsA59hiuqvACcXQNKmtJkLbW0Ka2G1Iy9vrVpItrCkmpvIxHh9QqZtg0 kULQ/x6l9KU4bGyfMHlvVr3kSAtX9DuqXRg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrgedvuddgudeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggugfgjsehtkeertddttdejnecuhfhrohhmpeetnhgu rhgvshcuhfhrvghunhguuceorghnughrvghssegrnhgrrhgriigvlhdruggvqeenucggtf frrghtthgvrhhnpefhieetgfelveettdffjeevudejkeejudfgtdevffejledvtdefkeeg gfejhffggeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhroh hmpegrnhgurhgvshesrghnrghrrgiivghlrdguvg X-ME-Proxy: Feedback-ID: id4a34324:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 14 Jun 2023 18:49:29 -0400 (EDT) Date: Wed, 14 Jun 2023 15:49:28 -0700 From: Andres Freund To: tel@jklm.no, pgsql-bugs@lists.postgresql.org, Tom Lane , David Rowley Cc: Peter Geoghegan Subject: Re: BUG #17975: Nested Loop Index Scan returning wrong result Message-ID: <20230614224928.l255k2okc3ms3iph@awork3.anarazel.de> References: <17975-98a90c156f25c952@postgresql.org> <20230614203651.hyc4cjdzhgrg3pav@awork3.anarazel.de> <20230614211231.pudg2y2rz6az2vij@awork3.anarazel.de> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20230614211231.pudg2y2rz6az2vij@awork3.anarazel.de> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On 2023-06-14 14:12:31 -0700, Andres Freund wrote: > I think it's a problem with the uniqueness determination / missing a > qual / index selection. > > There are two rows in b with b.c_id = 13880, except that one of them has a > NULL a_id: > > => SELECT * FROM b WHERE c_id = 13880; > ┌────────┬───────┬────────┐ > │ id │ c_id │ a_id │ > ├────────┼───────┼────────┤ > │ 326048 │ 13880 │ (null) │ > │ 572151 │ 13880 │ 955968 │ > └────────┴───────┴────────┘ > (2 rows) > > . The uniqueness information comes from: > "index_a_cannot_share_c" UNIQUE, btree (c_id) WHERE a_id IS NOT NULL > > But note that we aren't using that index, we use > "index_b_c_id" btree (c_id) > > which of course also contains the a_id = NULL row. > > > We either need to force the index that we got the uniqueness information to be > used when it is partial, or add the quals from the partial unique index to all > other index scans. I suspect this is an issue going back to 9c7f5229. Indeed, 9.6 doesn't reproduce the issue (9c7f5229 was in 10). I haven't bisected it down to that, but it seems pretty likely - and if it's not that commit, it's a closely related one. It's not immediately obvious to me how to nicely fix this in a backpatchable way. An easy fix would be to not allow predicate indexes at all anymore in relation_has_unique_index_for(), but that's a pretty big cannon - fixes the issue though. Greetings, Andres Freund