Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kG56U-0002mw-Mn for pgsql-general@arkaria.postgresql.org; Wed, 09 Sep 2020 18:45:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kG56S-00080D-Ov for pgsql-general@arkaria.postgresql.org; Wed, 09 Sep 2020 18:45:56 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kG56S-0007zx-As for pgsql-general@lists.postgresql.org; Wed, 09 Sep 2020 18:45:56 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kG56Q-0006PZ-8i for pgsql-general@lists.postgresql.org; Wed, 09 Sep 2020 18:45:55 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 089IjrCD067476; Wed, 9 Sep 2020 14:45:53 -0400 From: Tom Lane To: Aner Perez cc: pgsql-general@lists.postgresql.org Subject: Re: Unexpected results when using GROUP BY GROUPING SETS and bind variables In-reply-to: References: <42661.1599671583@sss.pgh.pa.us> Comments: In-reply-to Aner Perez message dated "Wed, 09 Sep 2020 14:37:22 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <67474.1599677153.1@sss.pgh.pa.us> Date: Wed, 09 Sep 2020 14:45:53 -0400 Message-ID: <67475.1599677153@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Aner Perez writes: > Or if using indexed path elements in the GROUP BY is the issue and I should > put the array indexing in a subselect and do the aggregation in the outer > select. Like this: > -- Safer Subselect Version -- > SELECT level1, level2, level3, level4, sum(value) > FROM ( > SELECT path[1] as level1, path[2] as level2, path[3] as level3, path[4] > as level4, value > FROM bind_group_by > ) AS expanded > GROUP BY GROUPING SETS ( > (level1, level2, level3, level4), > (level1, level2, level3), > (level1, level2), > (level1), > () > ) > ORDER BY 1, 2, 3, 4; Yeah, that one looks a lot safer from here. There's no question about which expressions are supposed to match what. It should end up with the same plan, too. regards, tom lane