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 1qpdPy-006Pqu-I3 for pgsql-hackers@arkaria.postgresql.org; Sun, 08 Oct 2023 23:42:38 +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 1qpdPw-009Ixj-93 for pgsql-hackers@arkaria.postgresql.org; Sun, 08 Oct 2023 23:42:37 +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 1qpdPv-009Ixb-V4 for pgsql-hackers@lists.postgresql.org; Sun, 08 Oct 2023 23:42:36 +0000 Received: from mail-lf1-x12c.google.com ([2a00:1450:4864:20::12c]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qpdPp-000zfb-Qq for pgsql-hackers@lists.postgresql.org; Sun, 08 Oct 2023 23:42:36 +0000 Received: by mail-lf1-x12c.google.com with SMTP id 2adb3069b0e04-5041d6d8b10so5026517e87.2 for ; Sun, 08 Oct 2023 16:42:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696808549; x=1697413349; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=3Nd7VJCDp6vOCgkA1W4wnb96ZUCTy1TLrhOCBSWdWU0=; b=bjcNqNjtJr2OpF9Py+2ymg6JE2KjlRg//WKya0yYzcQHzTqGFBEWbh8n7Z4pC9GX74 H0VcexJ45bp1WpsphArXVygHnSM1jk7qqTKMo1e3TNcUh4IMkPUdFjKRVB0zYqBaYW65 P8Fso7tQMmozb6jJ3b3d1ltZjJnjBukBR1dm1GXMBNEIyHdleZHK8+UE/JZU/AEN+/v4 xTMrO4OCYFIR2sqrerKUuuq3/YpT/rPAnfD/6BeBznvLlohueMBc5UDoY9aiRcU+DKD1 75IgAhZezbPZSccYBvfTQWD6H6foQz0Lgf/KSZGxH/7njBmGW4m0cN2B6n31HZxJzuKj 8sVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696808549; x=1697413349; h=content-transfer-encoding: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=3Nd7VJCDp6vOCgkA1W4wnb96ZUCTy1TLrhOCBSWdWU0=; b=SNQckvNwwxEIaKYa6dP8SYJBjLKWdjNvgKFwlUGAsNyIBkVpY4C/u8PN9ASb5NynP8 YNIeX6nUZmxXFrc1Gj/2adv4g1omznShgg00utKbY43fvMfX7XVMNeyqxpRyoaPsKbge MnkH4rjwryxZQVlcLRSk088esEMJLQhPNu2ILuY5/b7sVNts3lCjSadbdCkH2MN9QZbF /f9tC8TmiSduLlqXBQMgVyH4HKGkNaNIuRoV3Me2mBif5u9fJAUe1tJAPoWkZDFKlgwf 12HQeZJYDj5p++haPoQauftIke4UCCN/wC8/ruWQrb3y5cZvJEteCo8DZ4Hp9Q+AjK/v h+tA== X-Gm-Message-State: AOJu0YzDQLHHbN8SzLuKct+ebNV+16lgrxbsoIkWCFkop9D2eR0FfJGS viNdd3JUJq5y3HSMuzQfcsu6FGI6r1r5/6axZYI= X-Google-Smtp-Source: AGHT+IF3/Z7SaFQ992ozfllYDvE56UgNh0q1FDfGYiXy7YuS8nqmoEkQGNNjTQ59CIkmqFklQRm+KHPEN74ZtTHjIPk= X-Received: by 2002:a05:6512:108e:b0:502:9fce:b6da with SMTP id j14-20020a056512108e00b005029fceb6damr14687850lfg.21.1696808548486; Sun, 08 Oct 2023 16:42:28 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: David Rowley Date: Mon, 9 Oct 2023 12:42:13 +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: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sun, 8 Oct 2023 at 23:52, Richard Guo wrote: > On Thu, Oct 5, 2023 at 2:26=E2=80=AFPM David Rowley wrote: >> >> So in short, I propose the attached fix without any regression tests >> because I feel that any regression test would just mark that there was >> a big in create_agg_path() and not really help with ensuring we don't >> end up with some similar problem in the future. > > > If the pathkeys that were added by adjust_group_pathkeys_for_groupagg() > are computable from the targetlist, it seems that we do not need to trim > them off, because prepare_sort_from_pathkeys() will add resjunk target > entries for them. But it's also no harm if we trim them off. So I > think the patch is a pretty safe fix. +1 to it. hmm, I think one of us does not understand what is going on here. I tried to explain in [1] why we *need* to strip off the pathkeys added by adjust_group_pathkeys_for_groupagg(). Given the following example: create table ab (a int,b int); explain (costs off) select a,count(distinct b) from ab group by a; QUERY PLAN ---------------------------- GroupAggregate Group Key: a -> Sort Sort Key: a, b -> Seq Scan on ab (5 rows) adjust_group_pathkeys_for_groupagg() will add the pathkey for the "b" column and that results in the Sort node sorting on {a,b}. It's simply not at all valid to have the GroupAggregate path claim that its pathkeys are also (effectively) {a,b}" as "b" does not and cannot legally exist after the aggregation takes place. We cannot put a resjunk "b" in the targetlist of the GroupAggregate either as there could be any number "b" values aggregated. Can you explain why you think we can put a resjunk "b" in the target list of the GroupAggregate in the above case? >> >> I have some concerns that the assert_pathkeys_in_target() function >> might be a little heavyweight for USE_ASSERT_CHECKING builds. So I'm >> not proposing to commit that without further discussion. > > > Yeah, it looks like some heavy to call assert_pathkeys_in_target() for > each path node. Can we run some benchmarks to see how much overhead it > would bring to USE_ASSERT_CHECKING build? I think it'll be easy to show that there is an overhead to it. It'll be in the realm of the ~41ms patched vs ~24ms unpatched that I showed in [2]. That's quite an extreme case. Maybe it's worth checking the total planning time spent in a run of the regression tests with and without the patch to see how much overhead it adds to the "average case". David [1] https://postgr.es/m/CAApHDvpJJigQRW29TppTOPYp+Aui0mtd3MpfRxyKv=3DN-tB62= jQ@mail.gmail.com [2] https://postgr.es/m/CAApHDvo7RzcQYw-gnkZr6QCijCqf8vJLkJ4XFk-KawvyAw109Q= @mail.gmail.com