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 1sWIuR-00EO5A-DH for pgsql-hackers@arkaria.postgresql.org; Tue, 23 Jul 2024 17:02:43 +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 1sWIuP-00EytO-Hs for pgsql-hackers@arkaria.postgresql.org; Tue, 23 Jul 2024 17:02:41 +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 1sWIuP-00EytE-7Q for pgsql-hackers@lists.postgresql.org; Tue, 23 Jul 2024 17:02:41 +0000 Received: from mail-pf1-x42a.google.com ([2607:f8b0:4864:20::42a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sWIuM-001496-Re for pgsql-hackers@lists.postgresql.org; Tue, 23 Jul 2024 17:02:40 +0000 Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-70d2ae44790so1489959b3a.2 for ; Tue, 23 Jul 2024 10:02:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=illuminatedcomputing-com.20230601.gappssmtp.com; s=20230601; t=1721754157; x=1722358957; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=MS/1tKuoM2UQl5G5RmXifsnfX3C7TsqAs8UEem7vKjA=; b=aTuRbc62KVD9x6P9H+88goc1AvbymR2Ircwjrdxpy6ZRuksFPCjHGjLeNgGdOhiFrD wU1pHCaAslpFxgPc92CLVEsylp7pJOjX/dweSnEZJRJoP+0+2ESgOGDQu0pd7fVWb7v6 LPt5znNyOyuOE5CIr6oqPYlRIBRwTDqQj73q7ulkXyGrdjfZ8+Njuy5oOPI0aHKJb9C1 bON8brLxC5DMGkyqA13ULv01pe11jUTzuuA7Eg3Jek+Fuzw9p+xrhY/pMHwGFGtdxm/m 4hyRNBMXM7SwjYVy5/Tp+rUWqWm1Emao42pUmzIja+xn4m7NbsMQDdO0yCmvQWy7UgNm hzXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721754157; x=1722358957; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=MS/1tKuoM2UQl5G5RmXifsnfX3C7TsqAs8UEem7vKjA=; b=weLM2JFb9TJVwa41Qc/4BpFuNaJIWbDKed+E9E3tIhxLpUSmBDWjoiBOTEvnDtq9hj j7A5Py3Wqr0ETiVhKN5MivPN9jY7vAbBghXWoRclfElWQUeYaVNKkmoTwp+6XqIZAwRI Jw03y5KWGUhKeTjNK09JL0DTWZkqzst3OV07xrQkDRkFGYSYCFWy/OWqKP69oZwku2jV IfSgWWSOfbxkXrwirh4/A/ZfuriwDLR1dmOzxhlhPA28+iBCW8LYviBH77Y4nZ+GbQzg U2CUNFBv+E407Gy5igmRBm5vDK6sqFUIeQ1heNNzv2IT/IlxAoidZJ8D4CFwfR2k7cYa XkiQ== X-Gm-Message-State: AOJu0Yz0DowYP3UBHcnWEOQvFGayRK6Q5uoxd/Jwq1Q5tc4oSQZcm8gO /PrSWXCVPr78eclk7DnETqcMt5IXrFFajwzQ31WrcWmdeHYFy27qRrU9Qx/eENwn9JrkRUezVZF b X-Google-Smtp-Source: AGHT+IHeqGVYzVRRkGIHx9GM0PZhOj25Hj5zGWoKDckjUIPP/kTYPacL/kQj6V7ltsoVP/0ZE9lV+Q== X-Received: by 2002:a05:6a00:3cc2:b0:70d:2a4d:2edc with SMTP id d2e1a72fcca58-70d2a4d3191mr6916841b3a.20.1721754157361; Tue, 23 Jul 2024 10:02:37 -0700 (PDT) Received: from [192.168.2.139] ([50.39.255.79]) by smtp.googlemail.com with ESMTPSA id d2e1a72fcca58-70d2f485c5csm3007219b3a.88.2024.07.23.10.02.36 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 23 Jul 2024 10:02:36 -0700 (PDT) Message-ID: <376ba068-4f12-4ee0-99d8-e19defbb6d29@illuminatedcomputing.com> Date: Tue, 23 Jul 2024 10:02:36 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] GROUP BY ALL To: pgsql-hackers@lists.postgresql.org References: <932963.1721688221@sss.pgh.pa.us> Content-Language: en-US From: Paul Jungwirth In-Reply-To: <932963.1721688221@sss.pgh.pa.us> 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 7/22/24 15:43, Tom Lane wrote: > Isaac Morland writes: >> And for when this might be useful, the syntax for it already exists, >> although a spurious error message is generated: > >> odyssey=> select (uw_term).*, count(*) from uw_term group by uw_term; >> ERROR: column "uw_term.term_id" must appear in the GROUP BY clause or be >> used in an aggregate function >> LINE 1: select (uw_term).*, count(*) from uw_term group by uw_term; >> ^ > >> I'm not sure exactly what's going on here > > The SELECT entry is expanded into "uw_term.col1, uw_term.col2, > uw_term.col3, ...", and those single-column Vars don't match the > whole-row Var appearing in the GROUP BY list. I guess if we > think this is important, we could add a proof rule saying that > a per-column Var is functionally dependent on a whole-row Var > of the same relation. Odd that the point hasn't come up before > (though I guess that suggests that few people try this). I was just using this group-by-row feature last week to implement a temporal outer join in a way that would work for arbitrary tables. Here is some example SQL: https://github.com/pjungwir/temporal_ops/blob/b10d65323749faa6c47956db2e8f95441e508fce/sql/outer_join.sql#L48-L66 That does `GROUP BY a` then `SELECT (x.a).*`.[1] It is very useful for writing queries that don't want to know about the structure of the row. I noticed the same error as Isaac. I worked around the problem by wrapping it in a subquery and decomposing the row outside. It's already an obscure feature, and an easy workaround might be why you haven't heard complaints before. I wouldn't mind writing a patch for that rule when I get a chance (if no one else gets to it first.) [1] Actually I see it does `GROUP BY a, a.valid_at`, but that is surely more than I need. I think that `a.valid_at` is leftover from a previous version of the query. Yours, -- Paul ~{:-) pj@illuminatedcomputing.com