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 1qnZdp-00BJbT-WE for pgsql-hackers@arkaria.postgresql.org; Tue, 03 Oct 2023 07:16:26 +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 1qnZdo-001xR5-BU for pgsql-hackers@arkaria.postgresql.org; Tue, 03 Oct 2023 07:16:24 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qnZdn-001xQX-VM for pgsql-hackers@lists.postgresql.org; Tue, 03 Oct 2023 07:16:24 +0000 Received: from mail-lf1-x132.google.com ([2a00:1450:4864:20::132]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qnZdl-007vfr-1o for pgsql-hackers@lists.postgresql.org; Tue, 03 Oct 2023 07:16:23 +0000 Received: by mail-lf1-x132.google.com with SMTP id 2adb3069b0e04-50308217223so662909e87.3 for ; Tue, 03 Oct 2023 00:16:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696317379; x=1696922179; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=GGPgg0mMGhLbvFvIaILcTPlFqZu3n58TgIz+MKd2O1A=; b=Bk6Etw344C0al1QaE8aLwPxcne9N8fX6HA9FHbY4gHgX4RguKl3qxc0DXm9uzdLmvS 03m/ypkex7UzX+d6vgWoG4A54HMFGM32u0ZNYbPEmVBmjssepIzaPkXdBJIC9kFfoRp5 PAlMFCfNoBPmR1ZVqAbvZIG9Hb/8S9iNoOE7Pd1PhNfPvFh/Q2zXR3bQYA3VMjcpArlI JSwpGBJXnt8mLNkddh+vwSIucZbW5QRRDSEl2RnY/4I44BToZp8NzfJbu/Wf6DbIEy2q d8p5vQZW3xRlh8s3n87bSWHAnCPo9/aHA1AYXqiV/RCgVzW6cfNRkAfbRBFcVjwlnm9u gy4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696317379; x=1696922179; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=GGPgg0mMGhLbvFvIaILcTPlFqZu3n58TgIz+MKd2O1A=; b=Z2Fjb+3oasfEAx2zX4w/n6/cNzDFeN0da9+23sVwGAoZ/MDcLapEn0k16o5EGX6Ng2 LxaLjLuagezeGT7r7ykc18WrR6xQHFrAEdLl//xgrcmfpgJ74yIPqnHRr2R/Fjn21IYF J6pdtkBU3gboT0QPKxg17YjAmviBO4uLUElgYV1HdXPU4IrtAZctaF34q4SWCyftZsHz IEqenMtt8xJMJaoChKTRczX28M2UkKJLo/uB1b1OQnlakp3SQwhDXR6hZJMpU6pLJzbo WiGw/h9RruuJ8ZXpkUcaNJjG66Vf/sPMoHXw8d8sw2LnC/Cud39OAJ9mzCWJ7XbPYJ0x Y4lg== X-Gm-Message-State: AOJu0YzC8cZJMTlnbFK9lAggbGqvLS6VYNi5KC78m6h12YwXLAOkVr2K gSwKQWOUal+KvFJbdT7vIRf6suiVR1U8alt/VWCLN2YTq44= X-Google-Smtp-Source: AGHT+IEPW6ONnshDAHedknw/ytL4YfTZDoTGLs3u/ajCARzhnWu31W2f4FGem9eJENr/uIrtgMd2wHgFEnASC9JyZXc= X-Received: by 2002:a05:6512:33ce:b0:503:2561:adbc with SMTP id d14-20020a05651233ce00b005032561adbcmr14033246lfg.64.1696317379056; Tue, 03 Oct 2023 00:16:19 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: David Rowley Date: Tue, 3 Oct 2023 20:16:07 +1300 Message-ID: Subject: Re: pg16: XX000: could not find pathkey item to sort To: Richard Guo Cc: Justin Pryzby , pgsql-hackers@lists.postgresql.org Content-Type: multipart/mixed; boundary="000000000000ce2ee00606caaa6e" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000ce2ee00606caaa6e Content-Type: text/plain; charset="UTF-8" On Tue, 3 Oct 2023 at 09:11, David Rowley wrote: > I'm concerned that this patch will be too much overhead when creating > paths when a PathKey's EquivalenceClass has a large number of members > from partitioned tables. I just tried out the patch to see how much it affects the performance of the planner. I think we need to find a better way to strip off the pathkeys for the columns that have been aggregated. Setup: create table lp (a int, b int) partition by list (a); select 'create table lp'||x||' partition of lp for values in('||x||')' from generate_series(0,999)x; \gexec \pset pager off set enable_partitionwise_aggregate=1; Benchmark query: explain (summary on) select a,count(*) from lp group by a; Master: Planning Time: 23.945 ms Planning Time: 23.887 ms Planning Time: 23.927 ms perf top: 7.39% libc.so.6 [.] __memmove_avx_unaligned_erms 6.98% [kernel] [k] clear_page_rep 5.69% postgres [.] bms_is_subset 5.07% postgres [.] fetch_upper_rel 4.41% postgres [.] bms_equal Patched: Planning Time: 41.410 ms Planning Time: 41.474 ms Planning Time: 41.488 ms perf top: 19.02% postgres [.] bms_is_subset 6.91% postgres [.] find_ec_member_matching_expr 5.93% libc.so.6 [.] __memmove_avx_unaligned_erms 5.55% [kernel] [k] clear_page_rep 4.07% postgres [.] fetch_upper_rel 3.46% postgres [.] bms_equal > I wondered if we should instead just check > if the subpath's pathkeys match root->group_pathkeys and if they do > set the AggPath's pathkeys to list_copy_head(subpath->pathkeys, > root->num_groupby_pathkeys), that'll be much cheaper, but it just > feels a bit too much like a special case. I tried this approach (patch attached) and it does perform better than the other patch: create_agg_path_fix2.patch: Planning Time: 24.357 ms Planning Time: 24.293 ms Planning Time: 24.259 ms 7.45% libc.so.6 [.] __memmove_avx_unaligned_erms 6.90% [kernel] [k] clear_page_rep 5.56% postgres [.] bms_is_subset 5.38% postgres [.] bms_equal I wonder if the attached patch is too much of a special case fix. I guess from the lack of complaints previously that there are no other cases where we could possibly have pathkeys that belong to columns that are aggregated. I've not gone to much effort to see if I can craft a case that hits this without the ORDER BY/DISTINCT aggregate optimisation, however. David --000000000000ce2ee00606caaa6e Content-Type: application/octet-stream; name="create_agg_path_fix2.patch" Content-Disposition: attachment; filename="create_agg_path_fix2.patch" Content-Transfer-Encoding: base64 Content-ID: X-Attachment-Id: f_ln9zh9nj0 ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL29wdGltaXplci91dGlsL3BhdGhub2RlLmMgYi9zcmMv YmFja2VuZC9vcHRpbWl6ZXIvdXRpbC9wYXRobm9kZS5jCmluZGV4IDIxMWJhNjUzODkuLjI0Njlm NDM4MTkgMTAwNjQ0Ci0tLSBhL3NyYy9iYWNrZW5kL29wdGltaXplci91dGlsL3BhdGhub2RlLmMK KysrIGIvc3JjL2JhY2tlbmQvb3B0aW1pemVyL3V0aWwvcGF0aG5vZGUuYwpAQCAtMzEzMywxMCAr MzEzMywxOCBAQCBjcmVhdGVfYWdnX3BhdGgoUGxhbm5lckluZm8gKnJvb3QsCiAJcGF0aG5vZGUt PnBhdGgucGFyYWxsZWxfc2FmZSA9IHJlbC0+Y29uc2lkZXJfcGFyYWxsZWwgJiYKIAkJc3VicGF0 aC0+cGFyYWxsZWxfc2FmZTsKIAlwYXRobm9kZS0+cGF0aC5wYXJhbGxlbF93b3JrZXJzID0gc3Vi cGF0aC0+cGFyYWxsZWxfd29ya2VyczsKKwogCWlmIChhZ2dzdHJhdGVneSA9PSBBR0dfU09SVEVE KQotCQlwYXRobm9kZS0+cGF0aC5wYXRoa2V5cyA9IHN1YnBhdGgtPnBhdGhrZXlzOwkvKiBwcmVz ZXJ2ZXMgb3JkZXIgKi8KKwl7CisJCWlmIChwYXRoa2V5c19jb250YWluZWRfaW4oc3VicGF0aC0+ cGF0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