Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j0Rxu-0005dm-UJ for pgsql-hackers@arkaria.postgresql.org; Sat, 08 Feb 2020 15:24:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1j0Rxs-0000En-Br for pgsql-hackers@arkaria.postgresql.org; Sat, 08 Feb 2020 15:24:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from <9erthalion6@gmail.com>) id 1j0Rxs-0000Ee-0A for pgsql-hackers@lists.postgresql.org; Sat, 08 Feb 2020 15:24:12 +0000 Received: from mail-ed1-x543.google.com ([2a00:1450:4864:20::543]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from <9erthalion6@gmail.com>) id 1j0Rxk-0002ub-Ru for pgsql-hackers@postgresql.org; Sat, 08 Feb 2020 15:24:11 +0000 Received: by mail-ed1-x543.google.com with SMTP id f8so3005769edv.2 for ; Sat, 08 Feb 2020 07:24:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=lmUspowftlYiFbNOjraTWCOR+uBnuVPBPZDEoJ1UWes=; b=SgtyLdjQK7RkUZF3eFCMeLEI2dVnJKWjAnwV6IlUl78oqade8qwQycWMPp3PV2nYsZ hzeKxoldoVn9Kp/vRQA5C0sqd2x8IoIE04Py7SQnr/XFOU/eYKmjuP8oqmD7trTiZspM pkvvtybIxMVqNFzWBir+m67HQxfRxokQZS2H9wIjPYHk3Wl3PfDBR3g66zMHszKVfcTt f4XdCG8mlrAoFwXTlhiVVx7X4ZlDYWC8kygqjCINCUJ8nSHIlldsnaKl80IvriGyhqfy AtzAUqL+6wYMP1gVgEkFvFNX/oNYrmJ364reqecevP383xB0tVyOXKuSrwLlvRGoYXZG Ei4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=lmUspowftlYiFbNOjraTWCOR+uBnuVPBPZDEoJ1UWes=; b=UziX8gmBDAbbgu1ANXRvnimjFjbwVtQAC+cy2fsPd3rYet64b4seuUoRrQ1Eq9uLoB PtfS/9TryVITBPNcFiABIeGVRp7oYWf2OSjSf7p59FB31h+ilp4KkWyhylqq7tSuqEhd gtY7FxlqApkYZ3CFJP5FpNMtbpVE5VbQRMHVuDbsOP6mJQNRB8XrHeoY059dA22YdUo2 QPohF8yK4CjRCd+qZtT9Pk0BG33p88Ao/QrY9AZWxUzCafX28ogpIsJDMp4nmUuOV70X 8jI06Ng0uYdoU2eKHOoa/xHVYhVC+PxlzBhOfqCTdYfXdDyv1r+utdz5FHvP1kwRNga+ A2bQ== X-Gm-Message-State: APjAAAXQZIkUNqdgDfUyc4tJpPlDCEvP3tFlq+GfcZbz/LM1PFATQdLH A1bvYQY4VV0yG/KBl1f7MJY= X-Google-Smtp-Source: APXvYqzAMIMeS4fUlQFFKKkuIk65n8nYndginbY8c7Ll/XM1LIN1aZmtdnSG2I0upj2PloNdad8SEQ== X-Received: by 2002:a05:6402:1596:: with SMTP id c22mr3528914edv.268.1581175443626; Sat, 08 Feb 2020 07:24:03 -0800 (PST) Received: from localhost (dslb-094-222-004-201.094.222.pools.vodafone-ip.de. [94.222.4.201]) by smtp.gmail.com with ESMTPSA id x2sm294685edi.95.2020.02.08.07.24.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 08 Feb 2020 07:24:03 -0800 (PST) Date: Sat, 8 Feb 2020 16:24:40 +0100 From: Dmitry Dolgov <9erthalion6@gmail.com> To: Tomas Vondra Cc: Kyotaro Horiguchi , florisvannee@optiver.com, pg@bowt.ie, jesper.pedersen@redhat.com, david.rowley@2ndquadrant.com, alvherre@2ndquadrant.com, thomas.munro@gmail.com, jtc331@gmail.com, rafia.pghackers@gmail.com, jeff.janes@gmail.com, bhushan.uparkar@gmail.com, pgsql-hackers@postgresql.org, a.korotkov@postgrespro.ru Subject: Re: Index Skip Scan Message-ID: <20200208152440.l2sllrla7utvgexr@localhost> References: <20200205163730.eqvoxh6eb3do52r5@localhost> <20200206.102450.1514793516830965023.horikyota.ntt@gmail.com> <20200206105707.fuidmdsi6oapieoz@localhost> <20200206.212220.642002310227620136.horikyota.ntt@gmail.com> <20200207162543.zlj2tvcwy7jku6vu@localhost> <20200208142217.fhsiymg3szfnniih@development> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200208142217.fhsiymg3szfnniih@development> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On Sat, Feb 08, 2020 at 03:22:17PM +0100, Tomas Vondra wrote: > > I've done some testing and benchmarking of the v31 patch, looking for > regressions, costing issues etc. Essentially, I've ran a bunch of SELECT > DISTINCT queries on data sets of various size, number of distinct values > etc. The results are fairly large, so I've uploaded them to github > > https://github.com/tvondra/skip-scan-test Thanks a lot for testing! > There are a couple of regressions, where the plan with skipscan enables > is ~10x slower. But this seems to happen only in high-cardinality cases > where we misestimate the number of groups. Consider a table with two > independent columns > > CREATE TABLE t (a text, b text); > INSERT INTO t SELECT > md5((10000*random())::int::text), > md5((10000*random())::int::text) > FROM generate_series(1,1000000) s(i); > > CREATE INDEX ON t(a,b); > > ANALYZE; > > which then behaves like this: > > test=# select * from (select distinct a,b from t) foo offset 10000000; > Time: 3138.222 ms (00:03.138) > test=# set enable_indexskipscan = off; > Time: 0.312 ms > test=# select * from (select distinct a,b from t) foo offset 10000000; > Time: 199.749 ms > > So in this case the skip scan is ~15x slower than the usual plan (index > only scan + unique). The reason why this happens is pretty simple - to > estimate the number of groups we multiply the ndistinct estimates for > the two columns (which both have n_distinct = 10000), but then we cap > the estimate to 10% of the table. But when the columns are independent > with high cardinalities that under-estimates the actual value, making > the cost for skip scan much lower than it should be. The current implementation checks if we can find the next value on the same page to do a shortcut instead of tree traversal and improve such kind of situations, but I can easily imagine that it's still not enough in some extreme situations. > I don't think this is an issue the skipscan patch needs to fix, though. > Firstly, the regressed cases are a tiny minority. Secondly, we already > have a way to improve the root cause - creating extended stats with > ndistinct coefficients generally makes the problem go away. Yes, I agree. > One interesting observation however is that this regression only > happened with text columns but not with int or bigint. My assumption is > that this is due to text comparisons being much more expensive. Not sure > if there is something we could do to deal with this - reduce the number > of comparisons or something? Hm, interesting. I need to check that we do not do any unnecessary comparisons. > On Sat, Feb 08, 2020 at 02:11:59PM +0100, Tomas Vondra wrote: > > Yes, I've mentioned that already in one of the previous emails :) The > > simplest way I see to achieve what we want is to do something like in > > attached modified version with a new hasDeclaredCursor field. It's not a > > final version though, but posted just for discussion, so feel free to > > suggest any improvements or alternatives. > > IMO the proper fix for this case (moving forward, reading backwards) is > simply making it work by properly checking deleted tuples etc. Not sure > why that would be so much complex (haven't tried implementing it)? It's probably not that complex by itself, but requires changing responsibilities isolation. At the moment current implementation leaves jumping over a tree fully to _bt_skip, and heap visibility checks only to IndexOnlyNext. To check deleted tuples properly we need to either verify a corresponding heap tuple visibility inside _bt_skip (as I've mentioned in one of the previous emails, checking if an index tuple is dead is not enough), or teach the code in IndexOnlyNext to understand that _bt_skip can lead to returning the same tuple while moving forward & reading backward. Do you think it's still makes sense to go this way?