Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 470FE1337BC8 for ; Tue, 19 Apr 2011 15:55:03 -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 98014-02 for ; Tue, 19 Apr 2011 18:54:51 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from wwwmaster.postgresql.org (wwwmaster.postgresql.org [217.196.146.204]) by mail.postgresql.org (Postfix) with ESMTP id 823171337B92 for ; Tue, 19 Apr 2011 15:54:50 -0300 (ADT) Received: from wwwmaster.postgresql.org (wwwmaster.postgresql.org [217.196.146.204]) by wwwmaster.postgresql.org (8.14.3/8.14.3) with ESMTP id p3JIsnbR074430 for ; Tue, 19 Apr 2011 18:54:49 GMT (envelope-from www@wwwmaster.postgresql.org) Received: (from www@localhost) by wwwmaster.postgresql.org (8.14.3/8.14.3/Submit) id p3JIsn6a074429; Tue, 19 Apr 2011 18:54:49 GMT (envelope-from www) Date: Tue, 19 Apr 2011 18:54:49 GMT Message-Id: <201104191854.p3JIsn6a074429@wwwmaster.postgresql.org> To: pgsql-bugs@postgresql.org Subject: BUG #5987: Rows created by WITH (INSERT ... RETURNING) are not visible to the rest of the query From: "Daniel Grace" Content-Type: text/plain; charset=utf-8 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 201104/137 X-Sequence-Number: 30056 The following bug has been logged online: Bug reference: 5987 Logged by: Daniel Grace Email address: dgrace@wingsnw.com PostgreSQL version: 9.1-alpha5 Operating system: Win7 x64 Description: Rows created by WITH (INSERT ... RETURNING) are not visible to the rest of the query Details: This may not be a bug, but I'll describe it anyways. Apologies if this is a duplicate -- "WITH" is apparently a stopword and searching the archives using it produces zero results. 9.1 adds the ability to use WITH in INSERT/UPDATE/DELETE in both directions. However, the actual effects of the query in the WITH clause are not visible to the outer query. ----- DROP TABLE IF EXISTS a; CREATE TABLE a ( t TEXT ); INSERT INTO a VALUES ('test1') RETURNING *; -- Works as expected WITH b AS (INSERT INTO a VALUES ('test2') RETURNING *) SELECT * FROM b; -- Works as expected WITH b AS (INSERT INTO a VALUES ('test3') RETURNING *) SELECT a.* FROM a INNER JOIN b USING(t); -- Does not see the newly created row. SELECT * FROM a WHERE t='test3'; -- But it was created. WITH b AS (INSERT INTO a VALUES ('test4') RETURNING *) -- Does not see the newly created row, thus the update does not happen. UPDATE a SET t='test5' FROM b WHERE a.t=b.t; SELECT * FROM a; ----- This is also true if the WITH query is a stored procedure that modifies the database and returns results, i.e. WITH b AS (SELECT * FROM create_row('test6'))... Presumably it affects UPDATE and DELETE as well, but I didn't test those cases. My actual use case is: I'm calling a function to duplicate+modify some rows. (Essentially, it does INSERT ... SELECT from the same table, but forcing the primary key to be reassigned via being a serial column and some other changes). This function returns the new rows as results (doing RETURN QUERY INSERT ... SELECT ... RETURNING *). In some situations, I want to further update the freshly created rows, so the goal was to do this: WITH newrows AS (SELECT * FROM function_that_creates_rows(...)) UPDATE basetable SET foo=overrides.bar FROM newrows LEFT JOIN (VALUES (...)) AS overrides(...) WHERE ...