Received: from magus.postgresql.org ([87.238.57.229]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TRkDU-0006dh-7J for pgsql-bugs@postgresql.org; Fri, 26 Oct 2012 13:44:52 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TRkDO-00052S-Na for pgsql-bugs@postgresql.org; Fri, 26 Oct 2012 13:44:51 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.5/8.14.5) with ESMTP id q9QDiiu5020678; Fri, 26 Oct 2012 09:44:44 -0400 (EDT) From: Tom Lane To: andrew@tao11.riddles.org.uk cc: pgsql-bugs@postgresql.org Subject: Re: BUG #7622: Incorrect aggregate level processing In-reply-to: References: Comments: In-reply-to andrew@tao11.riddles.org.uk message dated "Fri, 26 Oct 2012 05:20:31 -0000" Date: Fri, 26 Oct 2012 09:44:44 -0400 Message-ID: <20677.1351259084@sss.pgh.pa.us> X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201210/226 X-Sequence-Number: 34526 andrew@tao11.riddles.org.uk writes: > select (select array_agg(random()*i) from (values (1),(2)) v(a)) from > generate_series(1,3) i; > Expected output is three rows each with a 2-element array; actual output > is: > ERROR: more than one row returned by a subquery used as an expression > Looking at the explain, the aggregate is being pulled out of the subplan and > evaluated at the top query level. (This came up while doing some random data > generation, I've simplified it a bit.) This is not a bug, it's the behavior required by SQL standard. An aggregate belongs to the lowest query level supplying a variable to it (except of course when it contains no variable at all). So it's effectively an outer-reference constant for that inner select. regards, tom lane