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 1tLa2e-00FgLU-UU for pgsql-hackers@arkaria.postgresql.org; Thu, 12 Dec 2024 03:39:08 +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 1tLa2c-006FsO-93 for pgsql-hackers@arkaria.postgresql.org; Thu, 12 Dec 2024 03:39:07 +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 1tLa2b-006Fq6-RO for pgsql-hackers@lists.postgresql.org; Thu, 12 Dec 2024 03:39:07 +0000 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tLa2Z-002OLc-Km for pgsql-hackers@lists.postgresql.org; Thu, 12 Dec 2024 03:39:06 +0000 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-21669fd5c7cso1111525ad.3 for ; Wed, 11 Dec 2024 19:39:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733974742; x=1734579542; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=wBSS2xWwVTZzvkI1kE7BzhJvTzHM7LlXHCFinuwrRU4=; b=GoNMHSb16OTNAsc9bx5TFoOyJdBfW+RfTykepDAuE4DD11Vola7oCLv1h8rJVW/e/J zsytPzFYLA23+TtpiCBiV6rutngXMujUd4jRZqdMSWdBUFYoOZP3qDD8EZQpLGzmN3Gr DA2biee1T7abet/CE1XfnSKQb7GivwoZKZ25NRuTs3KYLrS6rdRkuNujEu3QGVt8ZoA7 9gvGXI40W7igako0L3euf+Iw8V8J8uvL/uPv8nfXwfOzS7d+72JlZosvhHszkIwOPdaN NnAJcpBv+VBLDUahX1LLZvdUHNOYycwcp8Hcx2Td9R0MbdPGgcQPNJeLgnpoinA+GEPF Qmjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733974742; x=1734579542; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=wBSS2xWwVTZzvkI1kE7BzhJvTzHM7LlXHCFinuwrRU4=; b=kTY7WF4ojG+Za7ZZcnx9qQ1mYEgIVqUYCrOVvxZO/axid1uxQt9vqD3iNOWZyeim3l bZ8iI9P6Amh0xLtxppsKf62/zZogYlW9F4Lw1LEggTIjfDy2LKWr2kn/EeDgRv/3hnbM 5BRMwc4xl+lrfBZ7MZixY7vZAQQFfKMvroGiALCUVTNzP3ar3DogAMcSybhUTgJHsZYd g3/T/rRJli7/FW/sGqQOHwkP43pNx/jVuR/i4t86VvqGmeCJURy3sIZNuXlwR5QDSgSd gTm/37DBGTAdx4ea88fc7tFo/p2d61mi0Dmjh0AkHoMdJpqItruHICa5xAEcBJKHTBGD u2Uw== X-Forwarded-Encrypted: i=1; AJvYcCUkJlVlrCGa8YVsIJemIQhn1v/ovSQsPOB3MoS/IVH9ygjY0nceaARVHAUJGidcqbPyj5HguPd6PrxoVKay@lists.postgresql.org X-Gm-Message-State: AOJu0YyBRFWT32tOfrbz8MhXRCX5MD0ls/7SfBuRbo8Lx572x3cHGWC/ QS5a4fz6ql2ZdTti0m+EU3s9Vc23WL4NDFk0fQKEU3OE866tZ8PW X-Gm-Gg: ASbGncsYHoEMXicU+6T5Aon2L+J2wNRK1oF3Oz97d6USK2hSMgCy8TTqEfwh7fESg6P jCCT8ufu5Xhw2oum/ALtHV9H/sF4rIcl5D1FvBapLZBA+R8R9W9JNyqHpgNT97TUbXbf7MWScWA a3eqxNjJU3vDegwo80kSWilhwn/g8BOXLANfeWG2626uDXVZVWyvdQBVRDNzObNWSjUA+uDsLb1 h8OCplPUB5C/xJaZ87YbM5oS8rd9zx/kmbYTH8VTdcFa5Gnq6Bl6magpLF8PK3YlAbEodSbvdqc MT8Osv6Peq9AF/8R0wSzbKqYYXp00g== X-Google-Smtp-Source: AGHT+IE2OsQLuPWLqVf1ZFcLvz2K/zwf1bHt5/zko+iVADJ678NvwmH04OUd/womRkOgqRrPg/MqHg== X-Received: by 2002:a17:902:c40c:b0:216:682f:175 with SMTP id d9443c01a7336-2178aee86d4mr38148395ad.49.1733974742001; Wed, 11 Dec 2024 19:39:02 -0800 (PST) Received: from [10.10.20.106] (node-gmq.pool-125-27.dynamic.totinternet.net. [125.27.84.50]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-215f8e64457sm113856775ad.107.2024.12.11.19.38.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 11 Dec 2024 19:39:01 -0800 (PST) Message-ID: Date: Thu, 12 Dec 2024 10:38:57 +0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird 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> <480861d3-a250-4ec6-885a-75a099ecadf7@gmail.com> Content-Language: en-AU From: Andrei Lepikhov 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 12/12/24 10:09, David Rowley wrote: > On Mon, 2 Dec 2024 at 17:18, Andrei Lepikhov wrote: >> 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. > > I did end up adding another fast path there, but I felt like checking > relid > 0 wasn't as good as it could be as that would have only > short-circuited when we don't see any Vars of level 0 in the GROUP BY. > It seemed cheap enough to short-circuit when none of the relations > mentioned in the GROUP BY have multiple columns mentioned. Your solution seems much better my proposal. Thanks to apply it! > when how do you decide if the GROUP BY should become t1.a,t1.b or > t2.x,t2.y? It's not clear to me that using t1's columns is always > better than using t2's. I imagine using a mix is never better, but I'm > unsure how you'd decide which ones to use. Depends on how to calculate that 'better'. Right now, GROUP-BY employs two strategies to reduce path cost: 1) ORDER-BY statement (avoid final sorting); 2) To fit incoming subtree pathkeys (avoid grouping presorting). My idea comes close with [1], where the cost depends on the estimated number of groups in the first grouping column because cost_sort predicts the number of comparison operator calls based on statistics. In this case, the choice between (x,y) and (a,b) will depend on the ndistinct of 'x' and 'a'. In general, it was the idea to debate, more for further development than for the patch in this thread. [1] Consider the number of columns in the sort cost model https://www.postgresql.org/message-id/flat/8742aaa8-9519-4a1f-91bd-364aec65f5cf%40gmail.com -- regards, Andrei Lepikhov