Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rXUV3-00Avzs-E4 for pgsql-hackers@arkaria.postgresql.org; Tue, 06 Feb 2024 23:05:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rXUV0-0077UV-8F for pgsql-hackers@arkaria.postgresql.org; Tue, 06 Feb 2024 23:05:06 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rXUUz-0077UN-9O for pgsql-hackers@lists.postgresql.org; Tue, 06 Feb 2024 23:05:05 +0000 Received: from m15.mail.163.com ([45.254.50.219]) by magus.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rXUUs-005qAd-4Z for pgsql-hackers@lists.postgresql.org; Tue, 06 Feb 2024 23:05:01 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-ID:MIME-Version: Content-Type; bh=NteUwIQuSC71DM76Zs/UpU4klu1PtqV1iynjfxJlf8k=; b=DqPz71y2BZv9fQuOQxW9na5Znjlkmexzme/EzqJvDAxnSSzgsRpAlZ9ABrA0r4 Mbwi7uThicJas+7Em5Gkj27bYcAoHg0xAxtbsV3w1ZZY7/583aYt6ZTJGnm8ON7A e47anmlARXeHgI2jJHRY1H8ZppcuycgSxNzlCYacr+A/Q= Received: from 8235eee8a2a0 (unknown [140.205.118.203]) by gzga-smtp-mta-g0-0 (Coremail) with SMTP id _____wDn73UQu8JlVTljCw--.48312S3; Wed, 07 Feb 2024 07:04:49 +0800 (CST) References: User-agent: mu4e 1.10.7; emacs 29.1 From: Andy Fan To: Richard Guo Cc: David Rowley , pgsql-hackers@lists.postgresql.org Subject: Re: Properly pathify the union planner Date: Wed, 07 Feb 2024 06:29:28 +0800 In-reply-to: Message-ID: <87o7cti48f.fsf@163.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-CM-TRANSID: _____wDn73UQu8JlVTljCw--.48312S3 X-Coremail-Antispam: 1Uf129KBjvJXoW7Aw1rZw18CFWfWw47Jry7KFg_yoW5JryUpF W2qr4Yyr1kGan7Xrs2yF40qF1YqF4rtFyjqr4rt34vyrn8Jr18ZF4jkay5ury7Gws5Wr92 vFs7XwnxZFnxKFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zRYsj5UUUUU= X-Originating-IP: [140.205.118.203] X-CM-SenderInfo: x2klx3xlid0iqsrtqiywtou0bp/1tbiVAB8U2VOBZhnrQADsE List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=-=-= Content-Type: text/plain Hi, > * I think we should update truncate_useless_pathkeys() to account for > the ordering requested by the query's set operation; Nice catch. > I'm thinking that maybe it'd be better to move the work of sorting the > subquery's paths to the outer query level, specifically within the > build_setop_child_paths() function, just before we stick SubqueryScanPath > on top of the subquery's paths. I think this is better because: > > 1. This minimizes the impact on subquery planning and reduces the > footprint within the grouping_planner() function as much as possible. > > 2. This can help avoid the aforementioned add_path() issue because the > two involved paths will be structured as: > > cheapest_path -> subqueryscan > and > cheapest_path -> sort -> subqueryscan > > If the two paths cost fuzzily the same and add_path() decides to keep > the second one due to it having better pathkeys and pfree the first one, > it would not be a problem. This is a smart idea, it works because you create a two different subqueryscan for the cheapest_input_path. FWIW, I found we didn't create_sort_path during building a merge join path, instead it just cost the sort and add it to the cost of mergejoin path only and note this path needs a presorted data. At last during the create_mergejoin_*plan*, it create the sort_plan really. As for the mergeappend case, could we use the similar strategy? with this way, we might simpliy the code to use MergeAppend node since the caller just need to say I want to try MergeAppend with the given pathkeys without really creating the sort by themselves. (Have a quick glance of initial_cost_mergejoin and create_mergejoin_plan, looks incremental sort doesn't work with mergejoin?) > > To assist the discussion I've attached a diff file that includes all the > changes above. + */ +static int +pathkeys_useful_for_setop(PlannerInfo *root, List *pathkeys) +{ + int n_common_pathkeys; + + if (root->setop_pathkeys == NIL) + return 0; /* no special setop ordering requested */ + + if (pathkeys == NIL) + return 0; /* unordered path */ + + (void) pathkeys_count_contained_in(root->setop_pathkeys, pathkeys, + &n_common_pathkeys); + + return n_common_pathkeys; +} The two if-clauses looks unnecessary, it should be handled by pathkeys_count_contained_in already. The same issue exists in pathkeys_useful_for_ordering as well. Attached patch fix it in master. -- Best Regards Andy Fan --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v1-0001-Remove-the-unnecessary-checks-for-pathkey-routine.patch From f562dd8b80bd0b7c2d66ee9633434a1e5be82e04 Mon Sep 17 00:00:00 2001 From: "yizhi.fzh" Date: Wed, 7 Feb 2024 07:00:33 +0800 Subject: [PATCH v1 1/1] Remove the unnecessary checks for pathkey routines both pathkeys_count_contained_in and foreach can handle the NIL input, so no need the extra checks. --- src/backend/optimizer/path/pathkeys.c | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/backend/optimizer/path/pathkeys.c b/src/backend/optimizer/path/pathkeys.c index 82ff31273b..98334e5c52 100644 --- a/src/backend/optimizer/path/pathkeys.c +++ b/src/backend/optimizer/path/pathkeys.c @@ -2126,12 +2126,6 @@ pathkeys_useful_for_ordering(PlannerInfo *root, List *pathkeys) { int n_common_pathkeys; - if (root->query_pathkeys == NIL) - return 0; /* no special ordering requested */ - - if (pathkeys == NIL) - return 0; /* unordered path */ - (void) pathkeys_count_contained_in(root->query_pathkeys, pathkeys, &n_common_pathkeys); @@ -2167,10 +2161,6 @@ pathkeys_useful_for_grouping(PlannerInfo *root, List *pathkeys) if (root->group_pathkeys == NIL) return 0; - /* unordered path */ - if (pathkeys == NIL) - return 0; - /* walk the pathkeys and search for matching group key */ foreach(key, pathkeys) { -- 2.34.1 --=-=-=--