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 1hk6Ol-0004Ms-5d for pgsql-hackers@arkaria.postgresql.org; Sun, 07 Jul 2019 12:36:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hk6Nk-0004Cd-9D for pgsql-hackers@arkaria.postgresql.org; Sun, 07 Jul 2019 12:35:04 +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 ) id 1hk6Nj-0004CW-Th for pgsql-hackers@lists.postgresql.org; Sun, 07 Jul 2019 12:35:04 +0000 Received: from mail-wr1-x441.google.com ([2a00:1450:4864:20::441]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hk6Nc-0002mG-9n for pgsql-hackers@lists.postgresql.org; Sun, 07 Jul 2019 12:35:03 +0000 Received: by mail-wr1-x441.google.com with SMTP id u18so14190986wru.1 for ; Sun, 07 Jul 2019 05:34:55 -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:user-agent; bh=L4gDzHjvJCEjT/HsjfbActdZ2RhetG+eYehk0xyz4Es=; b=L7TW3noBBXU5HNM5VCQ4KU2BEbkB7PEf2voy8wMoQJ72WSZM9A33ko0EUUT2J67I3J tGc4GcM6iiVU2JV627Zj6ivUQjOxURqPVVXqLjOo2NGVXxJTXtmtVdWbZ9J2hTybj43U ekYbCDUHKX/2ZaMUV4CmxG5wezm84rRLIMbzzRFbZFb2XK1+yv3Sgl6B/5r371ufxh4G fIXec+bqfOK1LsKPLqi6osjhCAKo8+MJLUD+VFSB0+n4IVZ2mzUvhPwbZMseaklOyJVu OGqDNDLsJ+77G3XL8gSxXhhC8ecVMZiCLgWZnv15PXlitJjTuAglH1/Mt0+WudwcP0QX Mi3g== 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:user-agent; bh=L4gDzHjvJCEjT/HsjfbActdZ2RhetG+eYehk0xyz4Es=; b=R8RqPOjlPz5aAqTEviRwN4HO+OFhgezXte/eNt9nEJS+yooyZDE6nIJXifVwgdabWh 7QfYwf7LidqiS6+to1iZFytLU4/BrCl/BloK94wSLBgrGDg93Djx3Xx/g4l2A8aiH1rl bVtVdbDeXsCdMKz0mr1cFJXnHg5S3DYr5G8zurWziaAHDIE14AMN1zThmVqWOgaEP8MU +Ict+8Te4D8vub0vjt+8vz+IYkd77qYkPQ45LrCLUAD9RoLyakafP6TCIzr8rhr5IyCP zHPwn5q973v1yIsttdtJTkTSJNfvGRqmo9b4bRLERREg90zu/WB/LQSLt9Mkrm7VMn3m csoQ== X-Gm-Message-State: APjAAAXxX12WAXKH+4ROk8yO1pu6LfPZV5tPxWaERL4s32HtE9doNbb/ ss1zF2mXw8FIqNzjfvTLDnf9ag== X-Google-Smtp-Source: APXvYqyQkZs6XyVK/u/6JeOSGetp63VyzelNujn2oSMunoqi+uauQh4VJkAKO+I8EIC9uty8w8QplA== X-Received: by 2002:adf:db12:: with SMTP id s18mr13400157wri.335.1562502893133; Sun, 07 Jul 2019 05:34:53 -0700 (PDT) Received: from localhost (ip-86-49-253-160.net.upcbroadband.cz. [86.49.253.160]) by smtp.gmail.com with ESMTPSA id h133sm16764155wme.28.2019.07.07.05.34.51 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Sun, 07 Jul 2019 05:34:51 -0700 (PDT) Date: Sun, 7 Jul 2019 14:34:49 +0200 From: Tomas Vondra To: James Coleman Cc: Peter Geoghegan , Simon Riggs , Rafia Sabih , Shaun Thomas , Dmitry Dolgov <9erthalion6@gmail.com>, Alexander Korotkov , Tom Lane , PostgreSQL Developers Subject: Re: [PATCH] Incremental sort (was: PoC: Partial sort) Message-ID: <20190707123449.w3b2vetwv4y42evj@development> References: <20190625160235.6ctryjzvw5je7cet@development> <20190625203207.lvmmat7d2ilnr64t@development> <20190625232205.ssecrwplyb7faxbb@development> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="da2fpsfr6m73d3xi" Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20180716-1444-295967 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --da2fpsfr6m73d3xi Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline On Thu, Jul 04, 2019 at 09:29:49AM -0400, James Coleman wrote: >On Tue, Jun 25, 2019 at 7:22 PM Tomas Vondra > wrote: >> >> On Tue, Jun 25, 2019 at 04:53:40PM -0400, James Coleman wrote: >> > >> >Unrelated: if you or someone else you know that's more familiar with >> >the parallel code, I'd be interested in their looking at the patch at >> >some point, because I have a suspicion it might not be operating in >... >> So I've looked into that, and the reason seems fairly simple - when >> generating the Gather Merge paths, we only look at paths that are in >> partial_pathlist. See generate_gather_paths(). >> >> And we only have sequential + index paths in partial_pathlist, not >> incremental sort paths. >> >> IMHO we can do two things: >> >> 1) modify generate_gather_paths to also consider incremental sort for >> each sorted path, similarly to what create_ordered_paths does >> >> 2) modify build_index_paths to also generate an incremental sort path >> for each index path >> >> IMHO (1) is the right choice here, because it automatically does the >> trick for all other types of ordered paths, not just index scans. So, >> something like the attached patch, which gives me plans like this: >... >> But I'm not going to claim those are total fixes, it's the minimum I >> needed to do to make this particular type of plan work. > >Thanks for looking into this! > >I intended to apply this to my most recent version of the patch (just >sent a few minutes ago), but when I apply it I noticed that the >partition_aggregate regression tests have several of these failures: > >ERROR: could not find pathkey item to sort > >I haven't had time to look into the cause yet, so I decided to wait >until the next patch revision. > I wanted to investigate this today, but I can't reprodure it. How are you building and running the regression tests? Attached is a patch adding the incremental sort below gather merge, and also tweaking the costing. But that's mostly for and better planning decisions, I don't get any pathkey errors even with the first patch. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --da2fpsfr6m73d3xi Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="parallel-incremental-sort-v2.patch" diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c index 3efc807164..d7bf33f64d 100644 --- a/src/backend/optimizer/path/allpaths.c +++ b/src/backend/optimizer/path/allpaths.c @@ -2719,6 +2719,8 @@ generate_gather_paths(PlannerInfo *root, RelOptInfo *rel, bool override_rows) { Path *subpath = (Path *) lfirst(lc); GatherMergePath *path; + bool is_sorted; + int presorted_keys; if (subpath->pathkeys == NIL) continue; @@ -2727,6 +2729,26 @@ generate_gather_paths(PlannerInfo *root, RelOptInfo *rel, bool override_rows) path = create_gather_merge_path(root, rel, subpath, rel->reltarget, subpath->pathkeys, NULL, rowsp); add_path(rel, &path->path); + + /* consider incremental sort */ + is_sorted = pathkeys_common_contained_in(root->sort_pathkeys, + subpath->pathkeys, &presorted_keys); + + if (!is_sorted && (presorted_keys > 0)) + { + /* Also consider incremental sort. */ + subpath = (Path *) create_incremental_sort_path(root, + rel, + subpath, + root->sort_pathkeys, + presorted_keys, + -1); + + path = create_gather_merge_path(root, rel, subpath, rel->reltarget, + subpath->pathkeys, NULL, rowsp); + + add_path(rel, &path->path); + } } } diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c index 7f820e7351..c6aa17ba67 100644 --- a/src/backend/optimizer/path/costsize.c +++ b/src/backend/optimizer/path/costsize.c @@ -1875,16 +1875,8 @@ cost_incremental_sort(Path *path, limit_tuples); /* If we have a LIMIT, adjust the number of groups we'll have to return. */ - if (limit_tuples > 0 && limit_tuples < input_tuples) - { - output_tuples = limit_tuples; - output_groups = floor(output_tuples / group_tuples) + 1; - } - else - { - output_tuples = input_tuples; - output_groups = input_groups; - } + output_tuples = input_tuples; + output_groups = input_groups; /* * Startup cost of incremental sort is the startup cost of its first group --da2fpsfr6m73d3xi--