Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hq2uQ-0000jF-HV for pgsql-general@arkaria.postgresql.org; Tue, 23 Jul 2019 22:05:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hq2qh-0000rm-BM for pgsql-general@arkaria.postgresql.org; Tue, 23 Jul 2019 22:01:31 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hq2qh-0000re-0O for pgsql-general@lists.postgresql.org; Tue, 23 Jul 2019 22:01:31 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hq2qe-0001MF-Cg for pgsql-general@lists.postgresql.org; Tue, 23 Jul 2019 22:01:30 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x6NM1P7X031703; Tue, 23 Jul 2019 18:01:25 -0400 From: Tom Lane To: Alexander Voytsekhovskyy cc: pgsql-general@lists.postgresql.org Subject: Re: Query plan: SELECT vs INSERT from same select In-reply-to: References: <29965.1563916690@sss.pgh.pa.us> Comments: In-reply-to Alexander Voytsekhovskyy message dated "Wed, 24 Jul 2019 00:47:25 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <31701.1563919285.1@sss.pgh.pa.us> Date: Tue, 23 Jul 2019 18:01:25 -0400 Message-ID: <31702.1563919285@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk [ please keep the list cc'd ] Alexander Voytsekhovskyy writes: > Sorry again > here is both links: > https://explain.depesz.com/s/AEWj > https://explain.depesz.com/s/CHwF Don't think I believe that those are the same query --- there's a CTE in the second one that doesn't appear in the first, and it is eating a lot of time too. I have a vague recollection that there are cases where optimizations are possible in plain SELECT but not in data-modifying queries. So maybe this isn't pilot error but something triggered by that. We'd need to see a self-contained test case to verify that though. regards, tom lane