Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g5bOy-0008GR-IT for pgsql-performance@arkaria.postgresql.org; Thu, 27 Sep 2018 18:52:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g5bOv-0007I1-Te for pgsql-performance@arkaria.postgresql.org; Thu, 27 Sep 2018 18:52:37 +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.89) (envelope-from ) id 1g5bOv-0007Hu-AQ for pgsql-performance@lists.postgresql.org; Thu, 27 Sep 2018 18:52:37 +0000 Received: from mail-io1-xd44.google.com ([2607:f8b0:4864:20::d44]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1g5bOn-00072u-As for pgsql-performance@postgresql.org; Thu, 27 Sep 2018 18:52:35 +0000 Received: by mail-io1-xd44.google.com with SMTP id q5-v6so2681698iop.3 for ; Thu, 27 Sep 2018 11:52:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=AHmr0Qo+P/QdY9/G2AtPmRA8v4NK/2EjkoKVkgtUtLE=; b=auVDv0sh8rUsiRvSQnOpXAADAjdZEpbNaGBqBV0AIAxrYIx5qharlQQWQZdKJlbbUF DtZKDC8vB8PyFHCS9Jb7doE4iGqKXnN+u7FFYUZtPrktx/KBDc4WNmk1AB2dz3LZmmUy ZVdevZVbodogYBnvHvjkl1Yj7FMWGN5WUFuSM5LL15f7FAK2Q3JKkN3MAOnAwA7nW21S oDUVobyexL9QH+Fjnf0miRdafSwrIyhJiM+nB95CYW0D2mFcYVrsQAbsjxSK+6tDzW9G d2RBGQh37ccnt1yvpBCXTQv36APPeqib91EcwfyonCFS6l5/uWyhVTWM53k56claZNFZ 9l7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=AHmr0Qo+P/QdY9/G2AtPmRA8v4NK/2EjkoKVkgtUtLE=; b=h0dyjJoOX6zztbbjYUHtVHIo1IEp3fdzjLWWNwfi6UuQ/EiSyrqb1Pf9HLb0TX1xz2 CBd/AOSS2L8HmjqRWtvHVzaivC8CTIFNHDP5NXAyfPygkptfNy9lD+BhD05box4i29cX T4i+zhCXUm4x/mORulsb9FYqiyzv6xRX0KDXJ42rpf6UFiBArZ9oumhHMFWEVjIgM7ML fgY99yIMNOylZQxfEcL7MStEo/SScjBJXEg4QhefTnsdVT1Oz49b4TO4kVBuPYIQzyB2 7qtrVtD7H2mAZKGp62owf00s1oVn8rANauEm6LURx/Vj+eS5RK38ZT0eFEA5CQJeLwKr FGYg== X-Gm-Message-State: ABuFfojRTYz+Qn7uQATiO6jTw3q31nmB+MBWkFm/M/HFCUC/OCZpMflT rToAeOAzUwX2LSX1Liuky+N28Q== X-Google-Smtp-Source: ACcGV62qt90hOaQG2FCTKR6GWAJX5MJtXPBRUQtcdDOW2zVs426arxXXU5nAwLWmEaka3zuJhvxHIw== X-Received: by 2002:a6b:2c08:: with SMTP id s8-v6mr9911629ios.217.1538074348037; Thu, 27 Sep 2018 11:52:28 -0700 (PDT) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id e16-v6sm947663iob.64.2018.09.27.11.52.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Sep 2018 11:52:27 -0700 (PDT) Received: by pryzbyj (Postfix, from userid 1000) id 0917F805397; Thu, 27 Sep 2018 13:52:24 -0500 (CDT) Date: Thu, 27 Sep 2018 13:52:24 -0500 From: Justin Pryzby To: Arjun Ranade Cc: pgsql-performance@postgresql.org Subject: Re: SELECT statement returns in 10seconds, but INSERT/CREATE TABLE AS with same SELECT takes 7 minutes Message-ID: <20180927185224.GF776@telsasoft.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Thu, Sep 27, 2018 at 01:08:05PM -0400, Arjun Ranade wrote: > When I look at the EXPLAIN ANALYZE output, it seems that it's using a > drastically different query plan for the INSERT+SELECT than SELECT by > itself. The fast, SELECT plan is using parallel query, which isn't available for INSERT+SELECT: https://www.postgresql.org/docs/current/static/when-can-parallel-query-be-used.html |Even when it is in general possible for parallel query plans to be generated, the planner will not generate them for a given query if any of the following are true: |The query writes any data or locks any database rows. Using parallel query in this case happens to mitigate the effects of the bad plan. I see Tom responded, and you got an improvement by changing join threshold. But I think you could perhaps get an better plan if the rowcount estimates were fixed. That's more important than probably anything else - changing settings is only a workaround for bad estimates. In the slow/INSERT plan, this join is returning 55000x more rows than expected (not 55k more: 55k TIMES more). 7. 26,937.132 401,503.136 ↓ 55,483.7 332,902 1 Nested Loop (cost=1,516.620..42,244.240 rows=6 width=84) (actual time=311.021..401,503.136 rows=332,902 loops=1) Join Filter: (((papa_echo.oscar_bravo)::text = (five_hotel.tango_november)::text) AND ((papa_echo.lima_tango)::text = (five_hotel.lima_mike)::text) AND ((xray_juliet1.juliet)::text = (five_hotel.papa_victor)::text)) Rows Removed by Join Filter: 351664882 Buffers: shared hit=8570619 read=6 First question is if all those conditions are independent? Or if one of those conditions also implies another, which is confusing the planner. Justin