From: Tom Lane <tgl@sss.pgh.pa.us>
To: Aner Perez <aner.perez+pgsql-general@gmail.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Unexpected results when using GROUP BY GROUPING SETS and bind variables
Date: Wed, 09 Sep 2020 14:45:53 -0400
Message-ID: <67475.1599677153@sss.pgh.pa.us> (raw)
In-Reply-To: <CAP-7WhM6O2xsBGnpaB9C0XjQL3YzJr5YGVV13rwvhKdPab6--Q@mail.gmail.com>
References: <CAP-7WhNwyiHV=CpnMisFvASJ3HFzGEC2Lavd4tA-LQu-LTY=1w@mail.gmail.com>
<42661.1599671583@sss.pgh.pa.us>
<CAP-7WhM6O2xsBGnpaB9C0XjQL3YzJr5YGVV13rwvhKdPab6--Q@mail.gmail.com>
Aner Perez <aner.perez+pgsql-general@gmail.com> 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: tgl@sss.pgh.pa.us, aner.perez+pgsql-general@gmail.com
Subject: Re: Unexpected results when using GROUP BY GROUPING SETS and bind variables
In-Reply-To: <67475.1599677153@sss.pgh.pa.us>
References: <CAP-7WhNwyiHV=CpnMisFvASJ3HFzGEC2Lavd4tA-LQu-LTY=1w@mail.gmail.com> <42661.1599671583@sss.pgh.pa.us> <CAP-7WhM6O2xsBGnpaB9C0XjQL3YzJr5YGVV13rwvhKdPab6--Q@mail.gmail.com> <67475.1599677153@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox