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 1tHxtf-00B8rl-LG for pgsql-hackers@arkaria.postgresql.org; Mon, 02 Dec 2024 04:18:55 +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 1tHxtc-00DdGY-1y for pgsql-hackers@arkaria.postgresql.org; Mon, 02 Dec 2024 04:18:53 +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 1tHxtb-00DdGP-Nw for pgsql-hackers@lists.postgresql.org; Mon, 02 Dec 2024 04:18:52 +0000 Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tHxtZ-000aii-0Z for pgsql-hackers@lists.postgresql.org; Mon, 02 Dec 2024 04:18:52 +0000 Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-724f41d520cso2120559b3a.1 for ; Sun, 01 Dec 2024 20:18:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733113128; x=1733717928; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:subject:from:user-agent:mime-version:date:message-id:from:to :cc:subject:date:message-id:reply-to; bh=I5a+4P8aymaxgLZOCzkLkmWgwgVcp6+tFI744UJd7aA=; b=khPhqNs2YQkWHZgF3x1LUoAS+EKxvnh+5vt4TDebW7gad3HynfMkeURgwi5GBG/Ojk H4JmUiEdLGcSRXE/hjKXYRVZE4obex6lM7afuucF+p/F0ZxT4QjhJJJ2+xf790vZrb6V bjEpxTFywYY0lfOzJOYQ+N7gRSTPcWqhN732WzlEBgzniuY6tMw5+poT8cQX9CHo522V uTi2wiCfD/F9X4eUQTQNR8WlNJeSXPFAy25JUUuuXQN8vEX3VC8XQWsvBUM72LWsACBH dcz+CRHRqhUTGWLDICwRtwzroX04ugeytXz+FSIBWU+1cJIxCEjgm8z7PmA3gFiAfkTG qV2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733113128; x=1733717928; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:subject:from:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=I5a+4P8aymaxgLZOCzkLkmWgwgVcp6+tFI744UJd7aA=; b=TDuvDrbSBrjLHZT4d7sQUKn9l34nuEowt0Mvccc/sEqURGVNhluSLor+k2q1jAiUuj tI8U0NJB0mcdR/DfLhGMvG2ndvfQYIzYh1Hx7yJoBfgIZfb8AjIV25jF2450iDwx8muX 3zILtUkMtiywStpwWKgIkJw1UBla9iBtwCVwIUmTogo7DASgN/WDaPhneFiA1Qhf1bZ+ hWTWNe6TdTRM30muYkaf3tADMuPymw34jo1Ro4/rsLYrEDDtPxafxDb5qnRLe2/CUdkW 3XV2KHGVMm7/TzDMl8WJiCrQo18Fof7TP4diZ6op+29ogsjnm6gKf3qwvtL/NvjWbOOx yyUg== X-Forwarded-Encrypted: i=1; AJvYcCWeJP3tVQE+L+xcUuiyWB9eawhL+h82bNBFEQQqOkrBh0JiD/8OaFxCdGlwBCper6Kk8xG3P0zbgdCkqn02@lists.postgresql.org X-Gm-Message-State: AOJu0YzbzIyVxV7orGFtwvLOLArY52tUjcSjyfLWZS9i9dPkfX/YvWYg 6tPZthbFj0+rh240GBa/3F3HP0TBMmVZGsDxaKL2mCZa6iW+WFMz X-Gm-Gg: ASbGncuuXUdbMMBOk88AC6fc4/oMClLZ48aoKv84Ln9JMNGLbnNi/o6/Kt2Dbfqck71 +fb3NrFEmyvgYTer8U1TtKhZtiR8zvSX+yv6sL7azXUYdo2Bfq/0dAMrwZA3+nzXkyfnn7xufmk XXs4b66k3/yHBZ22aCdhZ2QXue8AZNIPNFEK10QgU8QB2+qD6Xo2xKoHgfC4fmuNSmUHOvufm8R vnD1LcdZQ5Px/hM6sjWw+g0AmTxHh/Kzo4l4gNXofCUCPgkAuszjYx4aAhq1Hk7/6NrAl3DTper yab+bwiUIKRiHeH2Tn+abeKvgw== X-Google-Smtp-Source: AGHT+IE2F2ZVh8c9gRXqEjxSCkaBHRcDX+IvfJ05hzusA/q6x9KkcZbaXoz42CNrlKh4AlTgh7/r+g== X-Received: by 2002:a05:6a00:4f88:b0:71e:cc7:c511 with SMTP id d2e1a72fcca58-7253014a11amr31450157b3a.23.1733113127577; Sun, 01 Dec 2024 20:18:47 -0800 (PST) Received: from [10.10.20.106] (node-otu.pool-125-26.dynamic.totinternet.net. [125.26.125.178]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7254176fc40sm7392441b3a.71.2024.12.01.20.18.45 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 01 Dec 2024 20:18:46 -0800 (PST) Message-ID: <480861d3-a250-4ec6-885a-75a099ecadf7@gmail.com> Date: Mon, 2 Dec 2024 11:18:42 +0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: Andrei Lepikhov Subject: Re: Remove useless GROUP BY columns considering unique index To: David Rowley Cc: Zhang Mingli , PostgreSQL Hackers , jian he References: <327990c8-b9b2-4b0c-bffb-462249f82de0@Spark> <1507cbb9-937f-48f5-9931-b67014511f9b@gmail.com> Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 29/11/2024 14:28, David Rowley wrote: > On Fri, 29 Nov 2024 at 19:36, Andrei Lepikhov wrote: >> 1. Thread reference in the patch comment doesn't work. >> 2. May it be possible to move remove_useless_groupby_columns in the >> second commit? It would be easier to review the differences. > > For #1, I rewrote the commit message. > > I've split into two patches for ease of review. See attached. Thanks! Patch 0001 seems OK. The differentiation of 'planmain' and 'initsplan' is not clear for me, but code movement seems correct. I should say that comment in initsplan.c: '... initialization routines' looks strange. Is this module about initialisation now? Patch 0002 looks helpful and performant. I propose to check 'relid > 0' to avoid diving into 'foreach(lc, parse->rtable)' at all if nothing has been found. It may restrict future optimisations like [1], where the upper query block can employ the 'uniqueness' of grouped clauses in selectivity estimations. But I think it is OK for now. NOTES: 1. Uniqueness is proved by a UNIQUE index. It might be a bit more sophisticated to probe not only grouping qual but also employ equivalence classes. In that case, we could process something like that: CREATE TABLE test ( x int NOT NULL, y int NOT NULL, z int NOT NULL, w int); CREATE UNIQUE INDEX ON test(y,z); SELECT t2.z FROM test t2, test t1 WHERE t1.y=t2.y GROUP BY t1.y,t2.z,t2.w; 2. The same idea could be used with DISTINCT statement. Right now we have: SELECT DISTINCT y,z,w FROM test; HashAggregate Output: y, z, w Group Key: test.y, test.z, test.w -> Seq Scan on public.test Output: x, y, z, w [1] https://www.postgresql.org/message-id/flat/50fe6779-ee2d-4256-bc64-cd661bc4029a%40gmail.com -- regards, Andrei Lepikhov