Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1booY3-0000ql-7I for pgsql-general@arkaria.postgresql.org; Tue, 27 Sep 2016 09:19:35 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1booY2-0008Ja-Q2 for pgsql-general@arkaria.postgresql.org; Tue, 27 Sep 2016 09:19:34 +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.84_2) (envelope-from ) id 1booY0-0008FI-JH; Tue, 27 Sep 2016 09:19:32 +0000 Received: from mail-wm0-x235.google.com ([2a00:1450:400c:c09::235]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1booXx-0004pc-L9; Tue, 27 Sep 2016 09:19:32 +0000 Received: by mail-wm0-x235.google.com with SMTP id d66so26435176wmf.0; Tue, 27 Sep 2016 02:19:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=9Z0zVo6/8xW5766Hak6eYUqE5CfEp+yzly/jt6mBLvE=; b=awTJnzjkSlecZZSGTaCl5QZRIm8r2YbvfNSQYFgq6N0poPf6CPzepAY7Ovjon3qa6y NZMK37EWcRLSMJxZLTmgBo+iS5qmOdzhBlV+ahzWWNQRiO8iXHOvQxbl/dNxKCUHmhAO fIhbjFg9JOTPJVCh5duUOCFzLnHOloRFcFY7HkR3OTtWtN+hqiDW1am2vw47En4ahLlI yCFS5DEVNxbd2467kYhDeBqivGOXiSvJTidXjlxbqJOwz7LaxWw2HeiTJTfmYXNQQpcF 8KMl+RoNz2T87KIl9idAfOOSla1b0o8CgHdT/eA/081r1YMhDv/yiaRMQdE6a5nVlsMv aJNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=9Z0zVo6/8xW5766Hak6eYUqE5CfEp+yzly/jt6mBLvE=; b=d8/oVaKH2qjgY9fygmZ1OsmjgiHLVUZLLd5l98CU2joW/zLYx+0eX2qNczsk6T2Sj1 SdB//3x/bGp0v3HsIZzEFgHIWDMidlAduJhhp7JdoiLFkvyo66kuN70y9Cf4YFcu6laC 9H7pPzDfmOzy293udGUK8yuLa/DDZ/dT3gDLIDIMnke2QMqTXkudIBMqVqxt9r9sBagP Y2MFLK4hdXjQJ4MbcSfXtLmluqOfXTDoENDz1CGyxJg5/WwwvC5OR5xb5c4VqpDvTuDy yH0hMHo5doJFIqiwObiSgJtZy2AVnjJGEdEHaKJ6OZoU5keqpOboe0RNBF2DUvU2MtVy LqOw== X-Gm-Message-State: AA6/9RkJMHHY9T/OKrSMQZqD0HVSwkKzrDckxAdpCJ0qQRbSFjhu7tYu2esnbPg6PzrU7q4c73DTXkvov4gWqw== X-Received: by 10.28.46.82 with SMTP id u79mr1720757wmu.57.1474967968136; Tue, 27 Sep 2016 02:19:28 -0700 (PDT) MIME-Version: 1.0 References: <1359.1474918683@sss.pgh.pa.us> In-Reply-To: <1359.1474918683@sss.pgh.pa.us> From: Tom van Tilburg Date: Tue, 27 Sep 2016 09:19:17 +0000 Message-ID: Subject: Re: inconsistent behaviour of set-returning functions in sub-query with random() To: Tom Lane Cc: pgsql-general@postgresql.org, pgsql-hackers@postgresql.org Content-Type: multipart/alternative; boundary=001a11424060cbc466053d79bd1c X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-general Precedence: bulk Sender: pgsql-general-owner@postgresql.org --001a11424060cbc466053d79bd1c Content-Type: text/plain; charset=UTF-8 Good to know and I agree that it is not an urgent case. I think this practice might be more common in the POSTGIS community where there are plenty of set-returning-functions used in this way. My use was taking a random sample of a pointcloud distrubution. I took the liberty to post your answer at stackexchange. thanks, Tom On Mon, 26 Sep 2016 at 21:38 Tom Lane wrote: > Tom van Tilburg writes: > > I'm often using the WHERE clause random() > 0.5 to pick a random subset > of > > my data. Now I noticed that when using a set-returning function in a > > sub-query, I either get the whole set or none (meaning that the WHERE > > random() > 0.5 clause is interpreted *before* the set is being > generated). > > e.g.: > > > > SELECT num FROM ( > > SELECT unnest(Array[1,2,3,4,5,6,7,8,9,10]) num) AS foo WHERE > random() > 0.5; > > Hmm, I think this is an optimizer bug. There are two legitimate behaviors > here: > > SELECT * FROM unnest(ARRAY[1,2,3,4,5,6,7,8,9,10]) WHERE random() > 0.5; > > should (and does) re-evaluate the WHERE for every row output by unnest(). > > SELECT unnest(ARRAY[1,2,3,4,5,6,7,8,9,10]) WHERE random() > 0.5; > > should evaluate WHERE only once, since that happens before expansion of the > set-returning function in the targetlist. (If you're an Oracle user and > you imagine this query as having an implicit "FROM dual", the WHERE should > be evaluated for the single row coming out of the FROM clause.) > > In the case you've got here, given the placement of the WHERE in the outer > query, you'd certainly expect it to be evaluated for each row coming out > of the inner query. But the optimizer is deciding it can push the WHERE > clause down to become a WHERE of the sub-select. That is legitimate in a > lot of cases, but not when there are SRF(s) in the sub-select's > targetlist, because that pushes the WHERE to occur before the SRF(s), > analogously to the change between the two queries I wrote. > > I'm a bit hesitant to change this in existing releases. Given the lack > of previous complaints, it seems more likely to break queries that were > behaving as-expected than to make people happy. But we could change it > in v10 and up, especially since some other corner-case changes in > SRF-in-tlist behavior are afoot. > > In the meantime, you could force it to work as you wish by inserting the > all-purpose optimization fence "OFFSET 0" in the sub-select: > > =# SELECT num FROM ( > SELECT unnest(Array[1,2,3,4,5,6,7,8,9,10]) num OFFSET 0) AS foo WHERE > random() > 0.5; > num > ----- > 1 > 4 > 7 > 9 > (4 rows) > > > regards, tom lane > --001a11424060cbc466053d79bd1c Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Good to know and I agree that it is not an urgent case.I think this practice might be more common in the POSTGIS community where= there are plenty of set-returning-functions used in this way. My use was t= aking a random sample of a pointcloud distrubution.

I to= ok the liberty to post your answer at stackexchange.

thanks,
=C2=A0Tom

On Mon, 26 Sep 2016 at 21:38 Tom Lane <tgl@sss.pgh.pa.us> wrote:
Tom van Tilburg <tom.van.tilburg@gmail.com> writes: > I'm often using the WHERE clause random() > 0.5 to pick a rando= m subset of
> my data. Now I noticed that when using a set-returning function in a > sub-query, I either get the whole set or none (meaning that the WHERE<= br> > random() > 0.5 clause is interpreted *before* the set is being gene= rated).
> e.g.:
>
> SELECT num FROM (
>=C2=A0 =C2=A0 =C2=A0SELECT unnest(Array[1,2,3,4,5,6,7,8,9,10]) num) AS = foo WHERE random() > 0.5;

Hmm, I think this is an optimizer bug.=C2=A0 There are two legitimate behav= iors
here:

=C2=A0 =C2=A0 SELECT * FROM unnest(ARRAY[1,2,3,4,5,6,7,8,9,10]) WHERE rando= m() > 0.5;

should (and does) re-evaluate the WHERE for every row output by unnest().
=C2=A0 =C2=A0 SELECT unnest(ARRAY[1,2,3,4,5,6,7,8,9,10]) WHERE random() >= ; 0.5;

should evaluate WHERE only once, since that happens before expansion of the=
set-returning function in the targetlist.=C2=A0 (If you're an Oracle us= er and
you imagine this query as having an implicit "FROM dual", the WHE= RE should
be evaluated for the single row coming out of the FROM clause.)

In the case you've got here, given the placement of the WHERE in the ou= ter
query, you'd certainly expect it to be evaluated for each row coming ou= t
of the inner query.=C2=A0 But the optimizer is deciding it can push the WHE= RE
clause down to become a WHERE of the sub-select.=C2=A0 That is legitimate i= n a
lot of cases, but not when there are SRF(s) in the sub-select's
targetlist, because that pushes the WHERE to occur before the SRF(s),
analogously to the change between the two queries I wrote.

I'm a bit hesitant to change this in existing releases.=C2=A0 Given the= lack
of previous complaints, it seems more likely to break queries that were
behaving as-expected than to make people happy.=C2=A0 But we could change i= t
in v10 and up, especially since some other corner-case changes in
SRF-in-tlist behavior are afoot.

In the meantime, you could force it to work as you wish by inserting the all-purpose optimization fence "OFFSET 0" in the sub-select:

=3D# SELECT num FROM (
=C2=A0 =C2=A0 SELECT unnest(Array[1,2,3,4,5,6,7,8,9,10]) num OFFSET 0) AS f= oo WHERE random() > 0.5;
=C2=A0num
-----
=C2=A0 =C2=A01
=C2=A0 =C2=A04
=C2=A0 =C2=A07
=C2=A0 =C2=A09
(4 rows)


=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 regards, tom lane
--001a11424060cbc466053d79bd1c--