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 1j0TSR-0007u7-Bk for pgsql-hackers@arkaria.postgresql.org; Sat, 08 Feb 2020 16:59:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1j0TSP-0007xH-Ka for pgsql-hackers@arkaria.postgresql.org; Sat, 08 Feb 2020 16:59:49 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j0TSP-0007xA-81 for pgsql-hackers@lists.postgresql.org; Sat, 08 Feb 2020 16:59:49 +0000 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1j0TSI-0005Ap-6P for pgsql-hackers@postgresql.org; Sat, 08 Feb 2020 16:59:48 +0000 Received: by mail-wm1-x341.google.com with SMTP id t14so6001766wmi.5 for ; Sat, 08 Feb 2020 08:59:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=q22+Q40ZrthzfgubMuM+3RGtpJfuMGvrNMJlmjqcf1Q=; b=SMPKDJA2jzZfB977uUGoPv4MJElyEqgafxIT1bN9jLRzn0bdYrqIqo4MCEacjqsR64 eMI3KWr9HHvgRfWa9lcXluCIVycPBGTBu0OkcBg5MhO8ggch+/5zTVVyN23fZFEBlTvw bqLaXv7esyrH4jY1fVPqBxsZKdCZROkMzmwlwVQmC7zO7h5tC1uEZtYixFZ30FXypWm1 x7ezk614KGhuW2PEuNZGqZK7fWEUcOsTcfci61kA1ZIOF3uaWrN+uK6tb2rVT9OWzFJg 6xIzHbuDEz31+62/M5RUZUpg6KyUgczlni9ljjIvpaplCTjDVEGpsn3141RQZljeVLYu tV/Q== 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=q22+Q40ZrthzfgubMuM+3RGtpJfuMGvrNMJlmjqcf1Q=; b=HaMTzr8a73nLJVd1Wp4dDqLrSZNCk+fB9OdE1LoxijhpEdvJJMWrSiINIT98q63lMk W3jdk/Z42sn4bW+OozaQbTy6Nu6d9ArrkdPijBWJ9xeOJPS+tak2fJaFhBd97MGhgEJ8 TNwKZebz3MmXw1gnh016n7aoGrpSA5HLc8BdcKXKT8+7YlHas26ZxcYOiux/ZNAYiUJX 1rxbFHq2wjE8DEw12UWarm5YXAP/R5FLV7SMQkzM9FByFVi55Ux/uA9Jn9XdnkyuXT4K nIA/KvCzUPaqNLP6Jb+cQm11ZvWuDKxYe+4bhvWPTTMuT7a3zc0UZNq7Up7vYSCv9r5S z2Hw== X-Gm-Message-State: APjAAAV00mlXBCedeOxu9RZkdhIsQgx/186o3suhxrxq6/gOApzhUoqW Np0SVnt3YuMO+6USs5eTVDCwcw== X-Google-Smtp-Source: APXvYqxdAA2Rg4yJsZ6mf3TlyAJ8Jy1F1yrY1/VNsA0rBJjU2iLsfP0WEdazJxXFOgnKaLDok629tg== X-Received: by 2002:a1c:3803:: with SMTP id f3mr5219400wma.134.1581181180485; Sat, 08 Feb 2020 08:59:40 -0800 (PST) Received: from localhost (ip-86-49-253-92.net.upcbroadband.cz. [86.49.253.92]) by smtp.gmail.com with ESMTPSA id i2sm8071834wmb.28.2020.02.08.08.59.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Feb 2020 08:59:39 -0800 (PST) Date: Sat, 8 Feb 2020 17:59:37 +0100 From: Tomas Vondra To: Dmitry Dolgov <9erthalion6@gmail.com> 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: <20200208165937.mtnpmawawfzgm4sb@development> 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> <20200208152440.l2sllrla7utvgexr@localhost> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20200208152440.l2sllrla7utvgexr@localhost> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Sat, Feb 08, 2020 at 04:24:40PM +0100, Dmitry Dolgov wrote: >> 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. > Yeah. I'm not sure there's room for further improvements. The regressed cases were subject to the 10% cap, and with ndistinct being more than 10% of the table, we probably can find many distinct keys on each index page - we know that every ~10 rows the values change. >> 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? Not sure. I have to think about this first. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services