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 1o90pe-0006Xm-EX for pgsql-bugs@arkaria.postgresql.org; Wed, 06 Jul 2022 08:56:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1o90pd-0005KE-Ax for pgsql-bugs@arkaria.postgresql.org; Wed, 06 Jul 2022 08:56:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o90pd-0005K5-1j for pgsql-bugs@lists.postgresql.org; Wed, 06 Jul 2022 08:56:25 +0000 Received: from mail-wr1-x436.google.com ([2a00:1450:4864:20::436]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1o90pa-0004oC-VP for pgsql-bugs@lists.postgresql.org; Wed, 06 Jul 2022 08:56:24 +0000 Received: by mail-wr1-x436.google.com with SMTP id cl1so21033106wrb.4 for ; Wed, 06 Jul 2022 01:56:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aiven.io; s=google; h=from:to:cc:subject:date:message-id:organization:in-reply-to :references:mime-version:content-transfer-encoding; bh=b+30NQrYmMA7aLOvlYVcxzrjbdqzHxeuaxeiuNFALLs=; b=I3UTh6c5PB1DYslTdEud7WpmSdjbdDiwiUXNqN3nD/8iGvc93GY2oqCYatBF96fRv6 UxY5qMYDt4oXMh/e+TApwXpzri+YciPu3Z9DoQjeE93hbpamNeRxkOljlUwqH/wR1rXb /YS0EB036HNx7FozEJuXl+PDGA4H8g6XBaGIU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:organization :in-reply-to:references:mime-version:content-transfer-encoding; bh=b+30NQrYmMA7aLOvlYVcxzrjbdqzHxeuaxeiuNFALLs=; b=S2K2ddXzoHKrZgk1GeYF6QRMW6x8/aBijnUhkK1AJpeYamncjjmSu2g2I3rsWL9d6b 2WtffkiTFzFD3cbwvoBTqttt/b0Lh4v5r6ep/Q/7zhP8Spajn5PNZW+3VaQYDuSGm0Yj 5z2HzRwpJWX65BqhZ3R2zf3yI9y4k7zjzG+S/R6dkObDcbQJtRf0A2v6cceJxoLZ+hIe EtGJwceRnG4vnIjDGHR0lRmp2EM8OBSpel9LUq6ynLKeGe+QauSU0BSUhnxkL3conozO qRd4XSOf3+IJjyZx40aRpCsx6LujDzMSpzEfO11Tb5adkVGTo3sQ7O9TxPX32wEekJ9w 9YjA== X-Gm-Message-State: AJIora/Sm7G55MfooOmlYrUUrb2Bic4sNjaOjmfVhUdEDtE8e6nhf3nE eD4vFL8qIyRROycslDPkDwBrCA== X-Google-Smtp-Source: AGRyM1u3d5ZguEPzkO6iv2Z6lVBdODjgHVGC9uN0xG41xEU+r86XYjHDn0AGZwXRy3SlcJZ4vnMzUQ== X-Received: by 2002:a05:6000:2a4:b0:21d:1896:763e with SMTP id l4-20020a05600002a400b0021d1896763emr36581255wry.248.1657097780916; Wed, 06 Jul 2022 01:56:20 -0700 (PDT) Received: from aivenronan.localnet ([45.13.105.93]) by smtp.gmail.com with ESMTPSA id u2-20020a5d5142000000b0021b966abc19sm34893467wrt.19.2022.07.06.01.56.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Jul 2022 01:56:20 -0700 (PDT) From: Ronan Dunklau To: Hung Nguyen Cc: pgsql-bugs@lists.postgresql.org, Tom Lane , "rjuju123@gmail.com" Subject: Re: Date: Wed, 06 Jul 2022 10:53:52 +0200 Message-ID: <2834175.e9J7NaK4W3@aivenronan> Organization: aiven In-Reply-To: <263303.1656769531@sss.pgh.pa.us> References: <263303.1656769531@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="nextPart3176046.aeNJFYEL58" Content-Transfer-Encoding: 7Bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --nextPart3176046.aeNJFYEL58 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" I've taken a look at this, and can expose a minimal test case reproducing what I believe is the same problem, see the attached test.sql file for this. The test case is a bit extreme, by setting random_page_cost so low, but the same thing can happen with default values of random_page_cost given a significantly high number of loops. Running the attached test case, I get the following: QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------- Nested Loop (cost=0.01..711.84 rows=20000 width=13) (actual time=0.240..6521.129 rows=20000 loops=1) Buffers: shared hit=445110 -> Seq Scan on t2 (cost=0.00..307.00 rows=20000 width=9) (actual time=0.007..2.297 rows=20000 loops=1) Buffers: shared hit=107 -> Bitmap Heap Scan on t1 (cost=0.01..0.02 rows=1 width=4) (actual time=0.325..0.325 rows=1 loops=20000) Recheck Cond: (id = t2.t1_id) Rows Removed by Index Recheck: 0 Heap Blocks: exact=20013 Buffers: shared hit=445003 -> Bitmap Index Scan on t1_gin_index (cost=0.00..0.01 rows=1 width=0) (actual time=0.324..0.324 rows=1 loops=20000) Index Cond: (id = t2.t1_id) Buffers: shared hit=424990 Planning: Buffers: shared hit=79 Planning Time: 0.325 ms Execution Time: 6522.759 ms If I drop the gin index, the btree index is used: QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------- Nested Loop (cost=0.29..1249.56 rows=20000 width=13) (actual time=0.018..16.570 rows=20000 loops=1) Buffers: shared hit=4607 -> Seq Scan on t2 (cost=0.00..307.00 rows=20000 width=9) (actual time=0.007..1.717 rows=20000 loops=1) Buffers: shared hit=107 -> Memoize (cost=0.29..0.31 rows=1 width=4) (actual time=0.000..0.000 rows=1 loops=20000) Cache Key: t2.t1_id Cache Mode: logical Hits: 18500 Misses: 1500 Evictions: 0 Overflows: 0 Memory Usage: 154kB Buffers: shared hit=4500 -> Index Only Scan using t1_pkey on t1 (cost=0.28..0.30 rows=1 width=4) (actual time=0.002..0.002 rows=1 loops=1500) Index Cond: (id = t2.t1_id) Heap Fetches: 1500 Buffers: shared hit=4500 Planning: Buffers: shared hit=10 Planning Time: 0.198 ms Execution Time: 17.683 ms Looking into it, it looks like we are not charging a cpu "descent" cost for the entry tree of the gin index, which we do for the btree index. In general, it does not pose a problem since IO costs are far greater than cpu costs. But when the index scan is inside a nestloop, we account for cache effect and amortize the cost of IO over the number of outer scans, which reduces its relative importance significantly. In that case, the index scan on the gin index appears much cheaper, as the constant cpu cost is not taken into account. I'm not sure how we should account for the cost of the descent, but I believe it should be more than free. Perhaps we could devise a similar strategy using the estimated numEntryPages ? -- Ronan Dunklau --nextPart3176046.aeNJFYEL58 Content-Disposition: attachment; filename="test.sql" Content-Transfer-Encoding: base64 Content-Type: application/sql; name="test.sql" QkVHSU47CkNSRUFURSBFWFRFTlNJT04gcGdfdHJnbTsKCkNSRUFURSBUQUJMRSB0MSAoCiAgaWQg dGV4dCBwcmltYXJ5IGtleQopOwoKQ1JFQVRFIFRBQkxFIHQyICgKICBpZCB0ZXh0IHByaW1hcnkg a2V5LAogIHQxX2lkIHRleHQKKTsKCkNSRUFURSBJTkRFWCB0MV9naW5faW5kZXggT04gdDEgVVNJ TkcgZ2luIChpZCBnaW5fdHJnbV9vcHMpOwoKaW5zZXJ0IGludG8gdDEgKGlkKQpzZWxlY3QgaTo6 dGV4dCBGUk9NIGdlbmVyYXRlX3NlcmllcygxLCAxNTAwKSBhcyBpOwoKaW5zZXJ0IGludG8gdDIg KGlkLCB0MV9pZCkKU0VMRUNUIGk6OnRleHQsIChpICUgMTUwMCArIDEpOjp0ZXh0IEZST00gZ2Vu ZXJhdGVfc2VyaWVzKDEsIDIwMDAwKSBpOwoKU0VUIHJhbmRvbV9wYWdlX2Nvc3QgPSAwLjAwMDAw MDAxOwpTRVQgZW5hYmxlX2hhc2hqb2luID0gb2ZmOwoKQU5BTFlaRSB0MSwgdDI7CgpleHBsYWlu IChhbmFseXplLCBidWZmZXJzKSBzZWxlY3QgKiBmcm9tIHQxIGpvaW4gdDIgb24gdDEuaWQgPSB0 Mi50MV9pZCA7CgpEUk9QIElOREVYIHQxX2dpbl9pbmRleDsKCmV4cGxhaW4gKGFuYWx5emUsIGJ1 ZmZlcnMpIHNlbGVjdCAqIGZyb20gdDEgam9pbiB0MiBvbiB0MS5pZCA9IHQyLnQxX2lkIDsKClJP TExCQUNLOwo= --nextPart3176046.aeNJFYEL58--