Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YVjIT-0008PL-Dw for pgsql-performance@arkaria.postgresql.org; Wed, 11 Mar 2015 16:15:49 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YVjIS-0001xW-Sn for pgsql-performance@arkaria.postgresql.org; Wed, 11 Mar 2015 16:15:48 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YVjIR-0001um-3G for pgsql-performance@postgresql.org; Wed, 11 Mar 2015 16:15:47 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YVjIK-0002su-15 for pgsql-performance@postgresql.org; Wed, 11 Mar 2015 16:15:45 +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 t2BGFX8O017468; Wed, 11 Mar 2015 12:15:33 -0400 From: Tom Lane To: Gunnlaugur Thor Briem cc: Tomas Vondra , Jeff Janes , Kevin Grittner , "pgsql-performance@postgresql.org" Subject: Re: EXPLAIN (no ANALYZE) taking an hour for INSERT FROM SELECT In-reply-to: References: <54F9E179.4000804@2ndquadrant.com> <1407502480.21180.1425691482543.JavaMail.yahoo@mail.yahoo.com> <27245.1425692294@sss.pgh.pa.us> <54FB1CDA.1080606@2ndquadrant.com> Comments: In-reply-to Gunnlaugur Thor Briem message dated "Wed, 11 Mar 2015 15:54:40 -0000" Date: Wed, 11 Mar 2015 12:15:33 -0400 Message-ID: <17467.1426090533@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org Gunnlaugur Thor Briem writes: > Yes, I think that's it: I've just realized that immediately prior to the > INSERT, in the same transaction, an unfiltered DELETE has been issued; i.e. > the whole table is being rewritten. Then the INSERT is issued ... with a > WHERE clause on non-existence in the (now empty) table. > In that case of course the WHERE clause is unnecessary, as it will always > evaluate as true (and we've locked the whole table for writes). Looks like > it is a lot worse than unnecessary, though, if it triggers this performance > snafu in EXPLAIN INSERT. Ah-hah. So what's happening is that the planner is doing an indexscan over the entire table of now-dead rows, looking vainly for an undeleted maximal row. Ouch. I wonder how hard it would be to make the indexscan give up after hitting N consecutive dead rows, for some suitable N, maybe ~1000. From the planner's viewpoint it'd be easy enough to fall back to using whatever it had in the histogram after all. But that's all happening down inside index_getnext, and I'm hesitant to stick some kind of wart into that machinery for this purpose. regards, tom lane -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance