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 1jJOpQ-0001tr-3X for pgsql-hackers@arkaria.postgresql.org; Tue, 31 Mar 2020 21:53:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jJOpP-0002xt-0P for pgsql-hackers@arkaria.postgresql.org; Tue, 31 Mar 2020 21:53:47 +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 1jJOpO-0002xm-O6 for pgsql-hackers@lists.postgresql.org; Tue, 31 Mar 2020 21:53:46 +0000 Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jJOpL-0008C0-QP for pgsql-hackers@lists.postgresql.org; Tue, 31 Mar 2020 21:53:46 +0000 Received: by mail-wr1-x443.google.com with SMTP id w10so28154990wrm.4 for ; Tue, 31 Mar 2020 14:53:43 -0700 (PDT) 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=UktN9euDaeFjnX9S06DjWxrsPvJmIpT1yWjoVb45rbw=; b=v6tX5GkxSSvTo0USHZ1EIZB/onqKnUG0m+PPB1hO6vcMuvqRICi3Q/5qbrUmGEaz3B EOyYhQd7yRbDY7BUzmInKYaQ4eR7QYzZPLrXVDer5Fd21IX4j3e7sWiSz37V+rtybOIk I8T/NDbGmcJSuEUVrmupacXxlbNHOaXkJ2a2GJYXOLWdJ+mKjvob7dScbI8MDXe1Ie4w u5fx89Iys05Dpjbyx2BWtM3j1vL91wR1Nb+m8X0B+HHihKrjDV2ZOoVHmej1gt/qzuW/ neGb804VVyOJf0mN8cfrK9FJbnK1z7mFIoV6YP13ba/ybcKte9wNVpFCi+Hiw6HqyveM 0fnQ== 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=UktN9euDaeFjnX9S06DjWxrsPvJmIpT1yWjoVb45rbw=; b=VDdRQgbm0CPa1tn9+vE2qBlyR3hqijvniFF3eXxsgbdzERceFqeN1P6iHYD0KbKgIe 73nGXLPgS5gEjabPMeIBzb3FlFkWS5n61kFdZPN1y5Drpsr1jXDzn85kxHxZ0vI5dnUs DWUTEKWd5P79SXT+mkp3ehWvOtLNOuipHZYisPKtKNxEqTGl05/0L131frE62XyY5JBk 6xPdbMNhDhGu+4zqGtYWwhVHUWuPR99m2qH9c40WEMt+R/qOpwVWULUpvSW0V6aVWEFd TVKPRxt389g8ogsez+EI97wUt4iuzrvYbfqEnf1mDTMVRmOmsP6VhvumqmpVrUwM73FX dujQ== X-Gm-Message-State: ANhLgQ1fmTZbnFdY6LdJIg3Mm2mMlVg8+aqolgC41UJe5c9AdbB9hllW AcwYwrV7CnPRmyG5NeDqhjXWwA== X-Google-Smtp-Source: ADFU+vsca13yFgvTl0WfeV4RpowJTf0qS893Mw70PJruQqBIqP32Ro+4GXNdJiAwHVFtRM+EOQGTFw== X-Received: by 2002:adf:f7cb:: with SMTP id a11mr22245895wrq.79.1585691622677; Tue, 31 Mar 2020 14:53:42 -0700 (PDT) Received: from localhost (ip-86-49-253-141.net.upcbroadband.cz. [86.49.253.141]) by smtp.gmail.com with ESMTPSA id t8sm1006164wmi.43.2020.03.31.14.53.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Mar 2020 14:53:41 -0700 (PDT) Date: Tue, 31 Mar 2020 23:53:40 +0200 From: Tomas Vondra To: James Coleman Cc: Alvaro Herrera , Michael Paquier , Rafia Sabih , Peter Geoghegan , Simon Riggs , Shaun Thomas , Dmitry Dolgov <9erthalion6@gmail.com>, Alexander Korotkov , Tom Lane , PostgreSQL Developers , Andreas Karlsson Subject: Re: [PATCH] Incremental sort (was: PoC: Partial sort) Message-ID: <20200331215340.bbbrccbehmjb7dbx@development> References: <20200329031437.yvhogo25wdijwpqd@development> <20200330014442.chqnwfmelm3ejsgr@development> <20200330122426.wjdfpqiatv4ybui7@development> <20200331011407.vwz3ektatgqyi6vy@development> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Tue, Mar 31, 2020 at 02:23:15PM -0400, James Coleman wrote: >On Mon, Mar 30, 2020 at 9:14 PM Tomas Vondra > wrote: >> >> The main thing I've been working on today is benchmarking how this >> affects planning. And I'm seeing a regression that worries me a bit, >> unfortunately. >> >> The test I'm doing is pretty simple - build a small table with a bunch >> of columns: >> >> create table t (a int, b int, c int, d int, e int, f int, g int); >> >> insert into t select 100*random(), 100*random(), 100*random(), >> 100*random(), 100*random(), 100*random(), 100*random() >> from generate_series(1,100000) s(i); >> >> and then a number of indexes on subsets of up to 3 columns, as generated >> using the attached build-indexes.py script. And then run a bunch of >> explains (so no actual execution) sorting the data by at least 4 columns >> (to trigger incremental sort paths), measuring timing of the script. >> >> I did a bunch of runs on current master and v46 with incremental sort >> disabled and enabled, and the results look like this: >> >> master off on >> -------------------------- >> 34.609 37.463 37.729 >> >> which means about 8-9% regression with incremental sort. Of course, this >> is only for planning time, for execution the impact is going to be much >> smaller. But it's still a bit annoying. >> >> I've suspected this might be either due to the add_partial_path changes >> or the patch adding incremental sort to additional places, so I tested >> those parts individually and the answer is no - add_partial_path changes >> have very small impact (~1%, which might be noise). The regression comes >> mostly from the 0002 part that adds incremental sort. At least in this >> particular test - different tests might behave differently, of course. >> >> The annoying bit is that the overhead does not disappear after disabling >> incremental sort. That suggests this is not merely due to considering >> and creating higher number of paths, but due to something that happens >> before we even look at the GUC ... >> >> I think I've found one such place - if you look at compare_pathkeys, it >> has this check right before the forboth() loop: >> >> if (keys1 == keys2) >> return PATHKEYS_EQUAL; >> >> But with incremental sort we don't call pathkeys_contained_in, we call >> pathkeys_common_contained_in instead. And that does not call >> compare_pathkeys and does not have the simple equality check. Adding >> the following check seems to cut the overhead in half, which is nice: >> >> if (keys1 == keys2) >> { >> *n_common = list_length(keys1); >> return true; >> } >> >> Not sure where the rest of the regression comes from yet. > >I noticed in the other function we also optimize by checking if either >keys list is NIL, so I tried adding that, and it might have made a >minor difference, but it's hard to tell as it was under 1%. > Which other function? I don't see this optimization in compare_pathkeys, that only checks key1/key2 after the forboth loop, but that's something different. I may be wrong, but my guess would be that this won't save much, because the loop should terminate immediately. The whole point is not to loop over possibly many pathkeys when we know it's exactly the same pathkeys list (same pointer). When one of the lists is NIL the loop should terminate right away ... >I also ran perf with a slightly modified version of your test that >uses psql, and after the above changes was seeing something like a >3.5% delta between master and this patch series. Nothing obvious in >the perf report though. > Yeah, I don't think there's anything obviously more expensive. >This test is intended to be somewhat worst case, no? At what point do >we consider the trade-off worth it (given that it's not plausible to >have zero impact)? > Yes, more or less. It was definitely designed to do that - it merely plans the query (no execution), with many applicable indexes etc. It's definitely true that once the exection starts to take more time, the overhead will get negligible. Same for reducing number of indexes. And of course, for queries that can benefit from incremental sort, the speedup is likely way larger than this. In general, I think it'd be naive that we can make planner smarter with no extra overhead spent on planning, and we can never accept patches adding even tiny overhead. With that approach we'd probably end up with a trivial planner that generates just a single query plan, because that's going to be the fastest planner. A realistic approach needs to consider both the planning and execution phase, and benefits of this patch seem to be clear - if you have queries that do benefit from it. Let's try to minimize the overhead a bit more, and then we'll see. >> Also, while looking at pathkeys_common_contained_in(), I've been a bit >> puzzled why does is this correct: >> >> return (key1 == NULL); >> >> It's easy to not notice it's key1 and not keys1, so I suggest we add a >> comment 'key1 == NULL' means we've processed whole keys1 list. > >Done. > >I've included fixes for Alvaro's comments in this patch series also. > OK, thanks. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services