Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ai1NG-0003OP-M6 for pgsql-general@arkaria.postgresql.org; Mon, 21 Mar 2016 15:04:06 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1ai1NG-00048C-7w for pgsql-general@arkaria.postgresql.org; Mon, 21 Mar 2016 15:04:06 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ai1Kz-0000gu-2F for pgsql-general@postgresql.org; Mon, 21 Mar 2016 15:01:45 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ai1Kr-0007Ms-Bq for pgsql-general@postgresql.org; Mon, 21 Mar 2016 15:01:43 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id CCED520C27 for ; Mon, 21 Mar 2016 11:01:36 -0400 (EDT) Received: from frontend1 ([10.202.2.160]) by compute1.internal (MEProxy); Mon, 21 Mar 2016 11:01:36 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=aklaver.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=NwdVQYjjGU44NxDlsnl5fI2Bw+g=; b=ohiOSJ EO1Fl+2uxTdFd58F1D+lDxDxo+gpx08CSflUQPbSsKXfHVytxUEJhy13TeOxAA57 ZaA/DsuJ0r8++ONyr067EQBLbgGoS6ye5aZE/TVuQ7UBzCVSTu5pHNBqnsyWDCZP qXZbyDASmrqnu5eHwecZn9tJHyp3Wiuv+oLKs= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=NwdVQYjjGU44NxD lsnl5fI2Bw+g=; b=keNB08kGd25ISn3BMKC8sYoarjX3Okn+EPprCdcXqjNqc/g F2F8SW839LoGAGNMgQeUqX6Mw+sweScZLLZ6KvyxaWxkOztjHE86Q4BPDgj/zWrz bNwR3Qfj59fKHVqZrqjX/mB5LS8StUf9VfsNd0W04L6fub6tlzTNM5susKhc= X-Sasl-enc: kTM+QihwS0BJDXocVRoHC4CU2yoPbGlMoCUspSurgWOi 1458572496 Received: from [192.168.1.2] (174-24-160-10.tukw.qwest.net [174.24.160.10]) by mail.messagingengine.com (Postfix) with ESMTPA id 45A45C0001F; Mon, 21 Mar 2016 11:01:36 -0400 (EDT) Subject: Re: [SQL] plan not correct? To: Bert References: <56F0079D.9060108@aklaver.com> Cc: pgsql-general From: Adrian Klaver Message-ID: <56F00CCF.9070702@aklaver.com> Date: Mon, 21 Mar 2016 08:01:35 -0700 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-general Precedence: bulk Sender: pgsql-general-owner@postgresql.org On 03/21/2016 07:54 AM, Bert wrote: Ccing list > Hello Ardian, > > The PostgreSQL version is 9.4.5 > > The reason I have the 'returning' statement in the update section is > because I only insert the data that has not been updated. I don't see > why I would need to return anything in the insert section? Well it was more about what you saw as the result of the UPDATE. It is not clear to me whether that is 'UPDATE count' or the rows from RETURNING? > > On Mon, Mar 21, 2016 at 3:39 PM, Adrian Klaver > > wrote: > > On 03/21/2016 07:03 AM, Bert wrote: > > Dear all, > > I am not sure if I am looking at a bug, or I am just doing > something wrong. > Anyhow, to me it seems that the plan for an upsert is wrong. (I > can not > find how many rows are inserted in the table) > > Regard the following setup: > # select count(1) from dlp.st_itemseat; > count > ------- > 0 > (1 row) > > # select count(1) from loaddlp.st_itemseat_insert where > loadtabletime = > '2016-03-21 14:53:28.771467'; > count > ------- > 12 > (1 row) > > # explain analyze * > > QUERY PLAN > --------------------------------------------------------------------------------------------------------------------------------------------------------- > Insert on st_itemseat (cost=26.14..41.39 rows=1 width=228) > (actual > time=1.282..1.282 rows=0 loops=1) > CTE upsert > -> Update on st_itemseat et (cost=0.15..26.11 rows=1 > width=240) > (actual time=0.066..0.066 rows=0 loops=1) > -> Nested Loop (cost=0.15..26.11 rows=1 > width=240) (actual > time=0.061..0.061 rows=0 loops=1) > -> Seq Scan on st_itemseat_insert > st_itemseat_insert_1 (cost=0.00..13.75 rows=2 width=234) (actual > time=0.031..0.040 rows=12 loops=1) > Filter: (loadtabletime = '2016-03-21 > 14:53:28.771467'::timestamp without time zone) > Rows Removed by Filter: 75 > -> Index Scan using pk_st_itemseat on > st_itemseat et > (cost=0.15..6.17 rows=1 width=14) (actual time=0.001..0.001 > rows=0 loops=12) > Index Cond: ((tick_server_id = > st_itemseat_insert_1.tick_server_id) AND (itemseat_id = > st_itemseat_insert_1.itemseat_id)) > -> Seq Scan on st_itemseat_insert (cost=0.02..15.27 rows=1 > width=228) (actual time=0.175..0.201 rows=12 loops=1) > Filter: ((loadtabletime = '2016-03-21 > 14:53:28.771467'::timestamp without time zone) AND (NOT (hashed > SubPlan 2))) > Rows Removed by Filter: 75 > SubPlan 2 > -> CTE Scan on upsert (cost=0.00..0.02 rows=1 > width=8) > (actual time=0.068..0.068 rows=0 loops=1) > Planning time: 1.022 ms > Execution time: 1.596 ms > (16 rows) > > > # * > INSERT 0 0 > > # select count(1) from dlp.st_itemseat; > count > ------- > 12 > (1 row) > > * the upsert query is added as an attachment to this mail. > > > In the query plan it seems that 0 rows are inserted; although 12 > rows > are inserted when we compare the 2 counts. > When an update happens, the rows reported in the 'update' > statement are > correct. > > > Do you get a row count or the rows? > > The reason I ask is that in the UPDATE section you have > '...returning ET.*', but not in the INSERT section. > > Not sure if it matters in this case, but the Postgres version might > provide context. > > > > Is this a bug? Or am I looking at the wrong part of the plan? I > would > like to check how many rows are actually inserted from the plan. > > wkr, > Bert > > -- > Bert Desmet > 0477/305361 > > > > > > -- > Adrian Klaver > adrian.klaver@aklaver.com > > > > > -- > Bert Desmet > 0477/305361 -- Adrian Klaver adrian.klaver@aklaver.com -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general