Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 4E566B5DCA1 for ; Tue, 6 Sep 2011 14:14:17 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 76400-01 for ; Tue, 6 Sep 2011 17:14:09 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from momjian.us (momjian.us [70.90.9.53]) by mail.postgresql.org (Postfix) with ESMTP id 1ED86B5DCA5 for ; Tue, 6 Sep 2011 14:14:08 -0300 (ADT) Received: (from bruce@localhost) by momjian.us (8.11.6/8.11.6) id p86HDu010756; Tue, 6 Sep 2011 13:13:56 -0400 (EDT) From: Bruce Momjian Message-Id: <201109061713.p86HDu010756@momjian.us> Subject: Re: Can't use WITH in a PERFORM query in PL/pgSQL? In-Reply-To: <29F36C7C98AB09499B1A209D48EAA615B501DA9401@mail2a.alliedtesting.com> To: depstein@alliedtesting.com Date: Tue, 6 Sep 2011 13:13:56 -0400 (EDT) CC: pgsql-bugs@postgresql.org, pgagarinov@alliedtesting.com, vshahov@alliedtesting.com X-Mailer: ELM [version 2.4ME+ PL124 (25)] MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="US-ASCII" 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/48 X-Sequence-Number: 31065 Added to TODO: Improve PERFORM handling of WITH queries or document limitation --------------------------------------------------------------------------- depstein@alliedtesting.com wrote: > Update: It has been suggested to wrap perform around a select like this: > > do > $$begin > perform( > with A as (select 1 as foo) > select foo from A > ); > end$$; > > This won't work if select returns more than one statement: > > do > $$begin > perform( > with A as (select generate_series(1,3) as foo) > select foo from A > ); > end$$; > > ERROR: more than one row returned by a subquery used as an expression > > So I still say it's broken. > > (Sorry for top-posting: I am forced to use Outlook at work...) > > From: Dmitry Epstein > Sent: Sunday, March 06, 2011 4:29 PM > To: 'pgsql-bugs@postgresql.org' > Cc: Peter Gagarinov; Vladimir Shahov > Subject: Can't use WITH in a PERFORM query in PL/pgSQL? > > PostgreSQL 9.0.1 > > It seems that PostgreSQL doesn't understand the WITH construct when used in a PERFORM query inside PL/pgSQL functions and code blocks: > > Example: > > do > $$begin > with A as (select 1 as foo) > perform foo from A; > end$$; > > syntax error at or near "perform" > > do > $$begin > with A as (select 1 as foo) > select foo from A; > end$$; > > query has no destination for result data > > The only workaround that I can think of is to use a dummy variable to capture the query result. This has to be done even when the query doesn't have a result (as when calling a function returning void). > > do > $$declare > dummy record; > begin > with A as (select 1 as foo) > select foo into dummy from A; > end$$; > > > Dmitry Epstein | Developer > > Allied Testing > T + 7 495 544 48 69 Ext 417 > M + 7 926 215 73 36 > > www.alliedtesting.com > We Deliver Quality. > -- Bruce Momjian http://momjian.us EnterpriseDB http://enterprisedb.com + It's impossible for everything to be true. +