Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 8C2A0B5DC67 for ; Sat, 17 Sep 2011 13:16:54 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 15516-03 for ; Sat, 17 Sep 2011 16:16:48 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id DF44BB5DC2A for ; Sat, 17 Sep 2011 13:16:47 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p8HGGgbj027585; Sat, 17 Sep 2011 12:16:42 -0400 (EDT) To: Guillaume Lelarge cc: Andreas , pgsql-sql@postgresql.org Subject: Re: Use select and update together In-reply-to: <1316274671.2203.2.camel@localhost.localdomain> References: <4E74B538.8080902@gmx.net> <1316274671.2203.2.camel@localhost.localdomain> Comments: In-reply-to Guillaume Lelarge message dated "Sat, 17 Sep 2011 17:51:10 +0200" Date: Sat, 17 Sep 2011 12:16:42 -0400 Message-ID: <27584.1316276202@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.404 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-0.504 X-Spam-Level: X-Archive-Number: 201109/45 X-Sequence-Number: 35845 Guillaume Lelarge writes: > On Sat, 2011-09-17 at 16:56 +0200, Andreas wrote: >> select * from ( >> update tbl set val = 1 where key in ( 1, 2, 3, 4, 5 ) returning * >> ) as x >> >> wouldn't work even in PG 9.1. >> So what data structure is coming out of an "update ... returning *" >> statement? >> It obviously doesn't work like a subquery. > The only way to make something like this work in 9.1 would be: > WITH x AS > (update tbl set val = 1 where key in ( 1, 2, 3, 4, 5 ) returning *) > SELECT * FROM x; The reason for that restriction is that WITH guarantees that the contained query is evaluated once and only once, whereas an ordinary subquery guarantees no such thing. So the effects of the UPDATE would be quite unpredictable if we allowed the former syntax. (In the specific example given it would likely work all right anyway, since there is no reason for a plain SELECT FROM to do anything except scan the subquery once. But if you did a join, say, watch out!) regards, tom lane