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.92) (envelope-from ) id 1jIKQP-0004uF-Is for pgsql-hackers@arkaria.postgresql.org; Sat, 28 Mar 2020 22:59:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jIKQO-0006gL-Ci for pgsql-hackers@arkaria.postgresql.org; Sat, 28 Mar 2020 22:59:32 +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 1jIKQN-0006gE-Rc for pgsql-hackers@lists.postgresql.org; Sat, 28 Mar 2020 22:59:32 +0000 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jIKQI-0003vh-Ji for pgsql-hackers@lists.postgresql.org; Sat, 28 Mar 2020 22:59:30 +0000 Received: by mail-wr1-x444.google.com with SMTP id d5so16468594wrn.2 for ; Sat, 28 Mar 2020 15:59:26 -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=S+2stHz5UiAUmGSKo0DvimcHXM9bQx+2nZc3vD5nwnE=; b=RQpMhIorZbulo1/EbfRvPo3CMdoTUqt8fGA44iBvibs27b0tJadCQUssawLlwt1JXT 2aYHUmDuNERIxmq3o6VWxVB/ek6WXl1RNRm8/aIeS1FSMxNhA7NuvfHjgOEa6lgzsCzP J7BB8YFBOvVOGW0XcD2tQNyvcXLBtaTJVICZvonXSwsxJJX3bBUr74qz9k5LeqlAj1Zf lLDzgSVw8geYLzEsm5vZxYvx9q87wmbmzG+KTlKGzYTSJDDPMkgQvMz1g4QyVALRBSFq dUtzOrqkgDvG9qf7uHl8R7wKEp9+xhu/+leHq6AFrMXOMZOObUpcwQmv5t+mhGH3u1MK 7vWA== 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=S+2stHz5UiAUmGSKo0DvimcHXM9bQx+2nZc3vD5nwnE=; b=m/9UPlcV6Sj9d1MUR2iudbAa5RqcYgbJN7Zy0qFH6fmyQmCrSWyhVx7eOo+SzAFMdE fNw/UxR3FaJlJe3QoFahX7G7FomQfgYCGze5ms9owb0JPrCfI1VdFB/Xi0oP9F3a2ZjW JTra6lY7I2JlhWngiRronI/IvSJC9P/9Fsr394OB3jIdFluR7CWhq+Boe1h29j+DVUCE 1PRpMxNY6y87RSUCMU3Ljv3pTYJgI29xtLU9aNRNS/iEJLQ1dKLPUD9YCl9rivaVgZMF hm1b44FRGGf9HmhDOllGRWILYD5rGmUuH512YwoN5gmlJ3D9GurpHnYZlJeqv7QmWESL SwzQ== X-Gm-Message-State: ANhLgQ2E6CAii1UWEcufmd1+v5cX9wxTsDnADYPOPZT+YVxgtTcT5TXy YR+7b0Fqn5kkc7cAwa1CQgAMKA== X-Google-Smtp-Source: ADFU+vvwraItah4X9r2uTYPXCv8KG7u2ltNkmWXsawgRJqz40mohPH7jzODAwc0/4S4pR7mFLbzDrw== X-Received: by 2002:adf:e58b:: with SMTP id l11mr6762005wrm.284.1585436365196; Sat, 28 Mar 2020 15:59:25 -0700 (PDT) Received: from localhost (ip-86-49-253-141.net.upcbroadband.cz. [86.49.253.141]) by smtp.gmail.com with ESMTPSA id b199sm10332902wme.23.2020.03.28.15.59.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Mar 2020 15:59:24 -0700 (PDT) Date: Sat, 28 Mar 2020 23:59:22 +0100 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: <20200328225922.ljnw54bfg44vi6ib@development> References: <20200324230831.5zvjpgayh35bxedp@development> <20200328011913.btjuwwkhieav3aod@development> <20200328025830.6v6ogkseohakc32q@development> <20200328213050.byyeuozylnxv2bhf@development> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="qamiwvo5lxoq2wur" Content-Disposition: inline In-Reply-To: <20200328213050.byyeuozylnxv2bhf@development> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --qamiwvo5lxoq2wur Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline Hi, Attached is my take on simplification of the useful pathkeyes thing. It keeps the function, but it truncates query_pathkeys to only members with EC members in the relation. I think that's essentially the optimization you've proposed. I've also noticed an issue in explain output. EXPLAIN ANALYZE on a simple query gives me this: QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------- Gather Merge (cost=66.30..816060.48 rows=8333226 width=24) (actual time=6.464..19091.006 rows=10000000 loops=1) Workers Planned: 2 Workers Launched: 2 -> Incremental Sort (cost=66.28..729188.13 rows=4166613 width=24) (actual time=1.836..13401.109 rows=3333333 loops=3) Sort Key: a, b, c Presorted Key: a, b Full-sort Groups: 4156 (Methods: quicksort) Memory: 30kB (avg), 30kB (max) Presorted Groups: 4137 (Methods: quicksort) Memory: 108kB (avg), 111kB (max) Full-sort Groups: 6888 (Methods: ) Memory: 30kB (avg), 30kB (max) Presorted Groups: 6849 (Methods: ) Memory: 121kB (avg), 131kB (max) Full-sort Groups: 6869 (Methods: ) Memory: 30kB (avg), 30kB (max) Presorted Groups: 6816 (Methods: ) Memory: 128kB (avg), 132kB (max) -> Parallel Index Scan using t_a_b_idx on t (cost=0.43..382353.69 rows=4166613 width=24) (actual time=0.033..9346.679 rows=3333333 loops=3) Planning Time: 0.133 ms Execution Time: 23998.669 ms (15 rows) while with incremental sort disabled it looks like this: QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------- Gather Merge (cost=734387.50..831676.35 rows=8333226 width=24) (actual time=5597.978..14967.246 rows=10000000 loops=1) Workers Planned: 2 Workers Launched: 2 -> Sort (cost=734387.47..744804.00 rows=4166613 width=24) (actual time=5584.616..7645.711 rows=3333333 loops=3) Sort Key: a, b, c Sort Method: external merge Disk: 111216kB Worker 0: Sort Method: external merge Disk: 109552kB Worker 1: Sort Method: external merge Disk: 112112kB -> Parallel Seq Scan on t (cost=0.00..105361.13 rows=4166613 width=24) (actual time=0.011..1753.128 rows=3333333 loops=3) Planning Time: 0.048 ms Execution Time: 19682.582 ms (11 rows) So I think there's a couple of issues: 1) Missing worker identification (Worker #). 2) Missing method for workers (we have it for the leader, though). 3) I'm not sure why the lable is "Methods" instead of "Sort Method", and why it's in parenthesis. 4) Not sure having two lines for each worker is a great idea. 5) I'd probably prefer having multiple labels for avg/max memory values, instead of (avg) and (max) notes. Also, I think we use "peak" in this context instead of "max". regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --qamiwvo5lxoq2wur Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="simplify_useful_pathkeys.txt" diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c index 32bf734820..49d4990e66 100644 --- a/src/backend/optimizer/path/allpaths.c +++ b/src/backend/optimizer/path/allpaths.c @@ -2748,7 +2748,6 @@ static List * get_useful_pathkeys_for_relation(PlannerInfo *root, RelOptInfo *rel) { List *useful_pathkeys_list = NIL; - ListCell *lc; /* * Considering query_pathkeys is always worth it, because it might allow us @@ -2756,29 +2755,27 @@ get_useful_pathkeys_for_relation(PlannerInfo *root, RelOptInfo *rel) */ if (root->query_pathkeys) { - bool query_pathkeys_ok = true; + ListCell *lc; + List *pathkeys = NIL; foreach(lc, root->query_pathkeys) { PathKey *pathkey = (PathKey *) lfirst(lc); EquivalenceClass *pathkey_ec = pathkey->pk_eclass; - Expr *em_expr; /* - * We can't use incremental sort for pathkeys containing volatile - * expressions. We could walk the exppression itself, but checking - * ec_has_volatile here saves some cycles. + * We can't build Incremental Sort when pathkeys contain elements + * without an EC member not contained in the current relation, so + * just ignore anything after the first such pathkey. */ - if (pathkey_ec->ec_has_volatile || - !(em_expr = find_em_expr_for_rel(pathkey_ec, rel))) - { - query_pathkeys_ok = false; + if (!find_em_expr_for_rel(pathkey_ec, rel)) break; - } + + pathkeys = lappend(pathkeys, pathkey); } - if (query_pathkeys_ok) - useful_pathkeys_list = list_make1(list_copy(root->query_pathkeys)); + if (pathkeys) + useful_pathkeys_list = lappend(useful_pathkeys_list, pathkeys); } return useful_pathkeys_list; --qamiwvo5lxoq2wur--