Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U0MIB-0004Yw-1u for pgsql-sql@arkaria.postgresql.org; Wed, 30 Jan 2013 01:16:47 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1U0MI9-0008FQ-Ua for pgsql-sql@arkaria.postgresql.org; Wed, 30 Jan 2013 01:16:45 +0000 Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U0MI9-0008FL-4N for pgsql-sql@postgresql.org; Wed, 30 Jan 2013 01:16:45 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U0MI7-00024Q-Sk for pgsql-sql@postgresql.org; Wed, 30 Jan 2013 01:16:44 +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 r0U1GeNw018862; Tue, 29 Jan 2013 20:16:40 -0500 (EST) From: Tom Lane To: Kong Man cc: vyegorov@gmail.com, pgsql-sql@postgresql.org Subject: Re: Writeable CTE Not Working? In-reply-to: References: , Comments: In-reply-to Kong Man message dated "Tue, 29 Jan 2013 11:29:40 -0800" Date: Tue, 29 Jan 2013 20:16:40 -0500 Message-ID: <18861.1359508600@sss.pgh.pa.us> X-Pg-Spam-Score: -2.4 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Kong Man writes: > Hi Victor, >> I see 2 problems with this query: >> 1) CTE is just a named subquery, in your query I see no reference to >> the “upd_code” CTE. >> Therefore it is never gets called; > So, in conclusion, my misconception about CTE in general was that all CTE get called without being referenced. I think this explanation is wrong --- if you run the query with EXPLAIN ANALYZE, you can see from the rowcounts that the writable CTE *does* get run to completion, as indeed is stated to be the behavior in the fine manual. However, for a case like this where the main query isn't reading from the CTE, the CTE will get cycled to completion after the main query is done. I think what is happening is that the main query is updating all the rows in the table, and then when the CTE comes along it thinks the rows are already updated in the current command, so it doesn't replace 'em a second time. This is a consequence of the fact that the same command-counter ID is used throughout the query. My recollection is that that choice was intentional and that doing it differently would break use-cases that are less outlandish than this one. I don't recall specific examples though. Why are you trying to update the same table in two different parts of this query, anyway? The best you can really hope for with that is unspecified behavior --- we will surely not promise that one of them completes before the other starts, so in general there's no way to be sure which one would process a particular row first. regards, tom lane -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql