Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gLtkL-00040k-7O for pgsql-general@arkaria.postgresql.org; Sun, 11 Nov 2018 17:42:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gLtkI-0000uu-BQ for pgsql-general@arkaria.postgresql.org; Sun, 11 Nov 2018 17:42:02 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gLtkH-0000un-Ve for pgsql-general@lists.postgresql.org; Sun, 11 Nov 2018 17:42:02 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gLtkA-0001TK-N2 for pgsql-general@postgresql.org; Sun, 11 Nov 2018 17:42:00 +0000 Received: from [192.168.127.1] (port=49652 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1gLtk7-000Dwu-4Y; Sun, 11 Nov 2018 17:41:51 +0000 Received: from [127.0.0.1] (port=29579 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1gLtk6-000Oan-A7; Sun, 11 Nov 2018 17:41:50 +0000 From: Andrew Gierth To: Rob Nikander Cc: Postgres General Subject: Re: query patterns for multipass aggregating In-Reply-To: <11335409-A083-4951-BC7A-3C4FD118AC04@gmail.com> (Rob Nikander's message of "Sun, 11 Nov 2018 11:20:23 -0500") Message-ID: <87d0rbwp4a.fsf@news-spur.riddles.org.uk> References: <11335409-A083-4951-BC7A-3C4FD118AC04@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Sun, 11 Nov 2018 17:41:49 +0000 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Rob" =3D=3D Rob Nikander writes: Rob> I want a query to list items, with their colors and images. Each Rob> result row is an item, so the colors and images must be aggregated Rob> into arrays or json. Rob> If there were only one other table, it=E2=80=99s a simple join and gr= oup=E2=80=A6 Rob> select items.*, array_agg(color_name) Rob> from items join colors on items.id =3D colors.item_id Rob> group by items.id=20=20=20=20 Method 1: select items.*, c.colors, i.images from items left join (select item_id, array_agg(color_name) as colors from colors group by item_id) c on c.item_id=3Ditems.id left join (select item_id, array_agg(image) as images from images group by item_id) i on i.item_id=3Ditems.id; Method 2: select items.*, c.colors, i.images from items left join lateral (select array_agg(c0.color_name) as colors from colors c0 where c0.item_id=3Ditems.id) c on true left join lateral (select array_agg(i0.image) as images from images i0 where i0.item_id=3Ditems.id) i on true; Unfortunately, the planner isn't smart enough yet to know that these two are equivalent, so they generate different sets of possible query plans. Method 1 gets plans that work well if the entire items table is being selected, since it will read the whole of the images and colors tables in one go, and it will also get plans that work well for reading a _single_ item selected by WHERE items.id=3D? because equivalence-class processing will propagate a copy of that condition down to below the grouping clauses. It will _not_ get a good plan for reading any other small subset of items (e.g. selected by other criteria); for this you need method 2, which in turn doesn't get very good plans when you fetch the whole items table. Don't be tempted to use CTEs for the subqueries in either plan; that'll only make it much worse. --=20 Andrew (irc:RhodiumToad)