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 1gLsj2-0000kf-MA for pgsql-general@arkaria.postgresql.org; Sun, 11 Nov 2018 16:36:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gLsYg-0002q3-MM for pgsql-general@arkaria.postgresql.org; Sun, 11 Nov 2018 16:25:58 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gLsYg-0002pi-Be for pgsql-general@lists.postgresql.org; Sun, 11 Nov 2018 16:25:58 +0000 Received: from mail-qk1-x729.google.com ([2607:f8b0:4864:20::729]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gLsYd-0002VH-Qa for pgsql-general@postgresql.org; Sun, 11 Nov 2018 16:25:57 +0000 Received: by mail-qk1-x729.google.com with SMTP id o89so9624483qko.0 for ; Sun, 11 Nov 2018 08:25:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:content-transfer-encoding:mime-version:subject:message-id:date :to; bh=1Ls3mdomD2xX6xfPR24002WJNdamGS4P0QukvSQwpW4=; b=Mawh0R6Mpmbbc/rNdfgPUIiZJPlA9Nn4zUb/QCFeptZpQuZijob3CCAED0VCz7492b Yqny0POKKCvf9RKKtqnFd1A81Uet3WvPkyCVMBjTEhUEf81b7ILGevnnuBF1PSSnf3fx S8XegYJMOblZvMd8pdQNQIlsdHi58PNw9v+35v1UkmQF2hK0RcUv2KKUm0NtX6sUf0Bf kv10yWSC4GC3vlRb22bekCjsqSJqKFRuom7QV0o5rY1NJNyB7uBvJDSKGEc+uyG3J3WJ zj1CEB7gizE2CXkdKZN+R7k0MtS9r35ZnjtkkxvORaMWTbPgtYWpyOnk4y4N07MctpDm /mcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:content-transfer-encoding:mime-version :subject:message-id:date:to; bh=1Ls3mdomD2xX6xfPR24002WJNdamGS4P0QukvSQwpW4=; b=MolbAzT357L2UCEs2YgG7F+FuzPjToMLIp3w9qXzczX+OyUDinVmGHfDNb/gAZvk3+ TKUzs+nHBtkrgD/jQZnkRgTpYarNToyKSLAPS46guY3X6yy8xjBMUrzlH1L4SSc2nBEK eIFEjb4jxk1LQ7pYowEkL7ipXsgh12qwaVAY3Ihk9ABJerpinzHBrchs85sXHty8m2T+ 25ECVCe8bHSH2SQPWIigbfnH9erVcHPM3RHgdFD2f3v5MOnPrDqc7yzcTzpm227J/sTz 0sg4nBZrAOCNK6EmUf35QZeN15Q8DE1zkxCynk02yD69Aw3EQ5O9zCmDa918mAOw42j6 kQ1w== X-Gm-Message-State: AGRZ1gKFmRB0OqIoLH6x9qv1ER2d1ccauPeP5j/VPieSmfot2vD1loAU HlHnWmbcNPrzbU/1Cn8U5nTYTBuX X-Google-Smtp-Source: AJdET5elJr9wErAYLOKvz1V7m9iePyBTTb8HkE6jUM5VWHifDxNH3pdKgVQsmK+NHRgd/J77kHaPxg== X-Received: by 2002:aed:3c7d:: with SMTP id u58mr16620721qte.198.1541953225115; Sun, 11 Nov 2018 08:20:25 -0800 (PST) Received: from ?IPv6:2601:18c:cd7f:9f00:74ce:127f:ed6d:8c61? ([2601:18c:cd7f:9f00:74ce:127f:ed6d:8c61]) by smtp.gmail.com with ESMTPSA id o49sm8633745qtf.60.2018.11.11.08.20.24 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Nov 2018 08:20:24 -0800 (PST) From: Rob Nikander Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 11.5 \(3445.9.1\)) Subject: query patterns for multipass aggregating Message-Id: <11335409-A083-4951-BC7A-3C4FD118AC04@gmail.com> Date: Sun, 11 Nov 2018 11:20:23 -0500 To: Postgres General X-Mailer: Apple Mail (2.3445.9.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, I=E2=80=99ve run into this pattern a few times, and I usually get a = little confused. I=E2=80=99m wondering if there are some common = solutions or techniques. Simplified example: I have tables `items`, `colors`, and `images`. Items have many colors, = and many images. I want a query to list items, with their colors and images. Each result = row is an item, so the colors and images must be aggregated into arrays = or json.=20 If there were only one other table, it=E2=80=99s a simple join and = group=E2=80=A6 select items.*, array_agg(color_name) from items join colors on items.id =3D colors.item_id group by items.id =20 Now to get images too, my first try uses a CTE=E2=80=A6=20 // Tables: // items (id, name, foo) // colors (item_id, color_name, color_foo) // images (item_id, image_file, image_foo) with items_with_colors as ( // This is the same query as above=20 select items.*, array_agg(colors.color_name) as color_names from items join colors on items.id =3D colors.item_id group by items.id ) // Same idea repeated but now joining to images table select items.*, array_agg(images.image_file) as image_files from items_with_colors items join images on items.id =3D images.item_id group by items.id, items.name, items.foo, items.color_names; // mmm = :( One first problem is on the last line. I have to list out all the = columns. In my real situation there are many more. Listing them is only = an inconvenience, but I=E2=80=99m more worried that it is internally = comparing all the columns, when really it could compare only `items.id`. So=E2=80=A6 are there some other techniques that usually (or might) work = better? It seems like the kind of thing that could run in parallel, if I = wrote it right.=20 Rob