Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U0Nge-0001r6-CJ for pgsql-sql@arkaria.postgresql.org; Wed, 30 Jan 2013 02:46:08 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1U0Ngd-0000or-AP for pgsql-sql@arkaria.postgresql.org; Wed, 30 Jan 2013 02:46:07 +0000 Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U0Ngc-0000ol-0a for pgsql-sql@postgresql.org; Wed, 30 Jan 2013 02:46:06 +0000 Received: from dub0-omc2-s1.dub0.hotmail.com ([157.55.1.140]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U0NgY-0003N0-7h for pgsql-sql@postgresql.org; Wed, 30 Jan 2013 02:46:03 +0000 Received: from DUB116-W35 ([157.55.1.136]) by dub0-omc2-s1.dub0.hotmail.com with Microsoft SMTPSVC(6.0.3790.4675); Tue, 29 Jan 2013 18:45:56 -0800 X-EIP: [6AAV6PKGj4WRWrgfwjC1T1BytjTL7g8k] X-Originating-Email: [kong_mansatiansin@hotmail.com] Message-ID: Content-Type: multipart/alternative; boundary="_dbc9a56e-0678-41b3-8210-2732fd87f90b_" From: Kong Man To: CC: , Subject: Re: Writeable CTE Not Working? Date: Tue, 29 Jan 2013 18:45:56 -0800 Importance: Normal In-Reply-To: <18861.1359508600@sss.pgh.pa.us> References: , , <18861.1359508600@sss.pgh.pa.us> MIME-Version: 1.0 X-OriginalArrivalTime: 30 Jan 2013 02:45:56.0702 (UTC) FILETIME=[EDCEE7E0:01CDFE93] 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 --_dbc9a56e-0678-41b3-8210-2732fd87f90b_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable > I think this explanation is wrong --- if you run the query with EXPLAIN > ANALYZE=2C you can see from the rowcounts that the writable CTE *does* ge= t > run to completion=2C as indeed is stated to be the behavior in the fine > manual. >=20 > However=2C for a case like this where the main query isn't reading from > the CTE=2C 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=2C and then when the CTE comes along it thinks the > rows are already updated in the current command=2C 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. Cool. Now I understand it much better. =20 > Why are you trying to update the same table in two different parts of > this query=2C 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=2C so in general there's no way to be > sure which one would process a particular row first. It was just my misuse of writable CTE thinking it would be more efficient t= han separate statements. Best regards=2C -Kong = --_dbc9a56e-0678-41b3-8210-2732fd87f90b_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
>=3B I think this explanation is wrong --- if you run the query with EXPL= AIN
>=3B ANALYZE=2C you can see from the rowcounts that the writable C= TE *does* get
>=3B run to completion=2C as indeed is stated to be the = behavior in the fine
>=3B manual.
>=3B
>=3B However=2C for = a case like this where the main query isn't reading from
>=3B the CTE= =2C the CTE will get cycled to completion after the main query is
>=3B= done. I think what is happening is that the main query is updating all>=3B the rows in the table=2C and then when the CTE comes along it think= s the
>=3B rows are already updated in the current command=2C so it do= esn't replace
>=3B 'em a second time. This is a consequence of the fa= ct that the same
>=3B command-counter ID is used throughout the query.= My recollection is
>=3B that that choice was intentional and that do= ing it differently would
>=3B break use-cases that are less outlandish= than this one. I don't recall
>=3B specific examples though.

= Cool. =3B Now I understand it much better. =3B

>=3B Why a= re you trying to update the same table in two different parts of
>=3B = this query=2C anyway? The best you can really hope for with that is
>= =3B unspecified behavior --- we will surely not promise that one of them>=3B completes before the other starts=2C so in general there's no way t= o be
>=3B sure which one would process a particular row first.

= It was just my misuse of writable CTE thinking it would be more efficient t= han separate statements.

Best regards=2C
-Kong
=
= --_dbc9a56e-0678-41b3-8210-2732fd87f90b_--