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 1sRSgW-00FWBe-1K for pgsql-hackers@arkaria.postgresql.org; Wed, 10 Jul 2024 08:28:20 +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 1sRSfW-003DcD-H5 for pgsql-hackers@arkaria.postgresql.org; Wed, 10 Jul 2024 08:27:18 +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 1sRSfW-003Dc2-6i for pgsql-hackers@lists.postgresql.org; Wed, 10 Jul 2024 08:27:18 +0000 Received: from mail-yb1-xb2a.google.com ([2607:f8b0:4864:20::b2a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sRSfT-001LAQ-1K for pgsql-hackers@postgresql.org; Wed, 10 Jul 2024 08:27:16 +0000 Received: by mail-yb1-xb2a.google.com with SMTP id 3f1490d57ef6-e03c6892e31so5707988276.1 for ; Wed, 10 Jul 2024 01:27:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720600034; x=1721204834; darn=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=oOdaaFdo0vzXrTv5X502FGJ1fI93parXeb/TdGoVpv0=; b=IdUBLbYrp6omhZy0kI2O8F/BJKRde8kjSudxlwMLUnKIbae4vfmU1ayaCkxISfxtQF 5qTukKYFF9RSFF5vVoPjvfGdTUCSy4mdt05CE9UR99BvbJYqOw6CmimBAiho5dTBcCG7 l6FJaa6EmNLKWM1LzuAsDyLTgVK4Ry08MlJND1rVEBTCsYSs1J65WcrMgVcpRwI8VD6q 8X+2d/HYrIGhfTrGeHmxSOvVxe3swSsVXMyoooMEkDCdQuFQGCvKQtoDJ2u2U7zYbHsC 64beZEJ+IgdlrO70pF2MXkEDCGq5X/lzazRceoHxR9+sYQnShltHo09vaSNYrmwci+B1 mhBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720600034; x=1721204834; 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=oOdaaFdo0vzXrTv5X502FGJ1fI93parXeb/TdGoVpv0=; b=wxLNY8mB/2sOlqvpdM6UwmjELGijuylhjps1xw/XtDzmUqjxfQ5Qq0TaP/gW6LfI/B 74fFVohThDKpBbCuUrkxgip6+hr6c11zySS/xk9QBQOh9jvIhPIjCf8jUIf6+Pqj9sjY utTltNlxj4C5PvMB5ZVA3YxCVYNCtD22G3I6pN5/TF2zMkJjEHIeYWGosLlY0TL07wz6 tbMODwcS7MS1/CJ4Aq75ujxnZzZftNrl+/Q9zlNy26MikQ0JUnZbfiyaE8xt6hPPjBhn JmgLG4LTk3LNug1ab1+F6nC7ofmuj9ZeO64L4DATuo3VOa6yyZibXh1gFqQ38vSC+kdc garg== X-Forwarded-Encrypted: i=1; AJvYcCVTKMbwys38SayoM2mVuImhCWKl6RW2Hh3J6oDPp/luKE8jXp4LkzNHP/ivAuZ7P26nNEiPQHnJVoU9JECfqPVDeoWeeQewrXqzv6XM X-Gm-Message-State: AOJu0YxJMwqQvl9oDLRHFR9NQzGwQmX7fXbWwGjuXpCUEnJU2JFD2Sv/ 2tl96n8hlmiLyRXLkTfzqIgaI6vLob0hTnduT8kho5kurF3nvGfZ62uVy975ZpGJUf8YmAlfzAY /D3QOi/N4uNTDTxTqXqtA8paTPC8= X-Google-Smtp-Source: AGHT+IE+E3AtUdos8zOlaEqaiTjYr/HCGdMUcBaW5iZn6uO23qGzQXaDkg0h1PT+gzl3Sz2W/KO1Gs5vCK9hDlSaZys= X-Received: by 2002:a25:9941:0:b0:e03:6225:4055 with SMTP id 3f1490d57ef6-e041b1de30fmr4816402276.63.1720600033959; Wed, 10 Jul 2024 01:27:13 -0700 (PDT) MIME-Version: 1.0 References: <87il22cj51.fsf@163.com> In-Reply-To: From: Richard Guo Date: Wed, 10 Jul 2024 16:27:02 +0800 Message-ID: Subject: Re: Eager aggregation, take 3 To: Paul George Cc: Andy Fan , PostgreSQL-development , 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, Jul 7, 2024 at 10:45=E2=80=AFAM Paul George wrote: > Thanks for reviving this patch and for all of your work on it! Eager aggr= egation pushdown will be beneficial for my work and I'm hoping to see it la= nd. Thanks for looking at this patch! > The output of both the original query and this one match (and the plans w= ith eager aggregation and the subquery are nearly identical if you restore = the LEFT JOIN to a JOIN). I admittedly may be missing a subtlety, but does = this mean that there are conditions under which eager aggregation can be pu= shed down to the nullable side? I think it's a very risky thing to push a partial aggregation down to the nullable side of an outer join, because the NULL-extended rows produced by the outer join would not be available when we perform the partial aggregation, while with a non-eager-aggregation plan these rows are available for the top-level aggregation. This may put the rows into groups in a different way than expected, or get wrong values from the aggregate functions. I've managed to compose an example: create table t (a int, b int); insert into t select 1, 1; select t2.a, count(*) from t t1 left join t t2 on t2.b > 1 group by t2.a having t2.a is null; a | count ---+------- | 1 (1 row) This is the expected result, because after the outer join we have got a NULL-extended row. But if we somehow push down the partial aggregation to the nullable side of this outer join, we would get a wrong result. explain (costs off) select t2.a, count(*) from t t1 left join t t2 on t2.b > 1 group by t2.a having t2.a is null; QUERY PLAN ------------------------------------------- Finalize HashAggregate Group Key: t2.a -> Nested Loop Left Join Filter: (t2.a IS NULL) -> Seq Scan on t t1 -> Materialize -> Partial HashAggregate Group Key: t2.a -> Seq Scan on t t2 Filter: (b > 1) (10 rows) select t2.a, count(*) from t t1 left join t t2 on t2.b > 1 group by t2.a having t2.a is null; a | count ---+------- | 0 (1 row) I believe there are cases where pushing a partial aggregation down to the nullable side of an outer join can be safe, but I doubt that there is an easy way to identify these cases and do the push-down for them. So for now I think we'd better refrain from doing that. Thanks Richard