Re: EXPLAIN (no ANALYZE) taking an hour for INSERT FROM SELECT

From: Gunnlaugur Thor Briem <gunnlaugur(at)gmail(dot)com>
To: Tomas Vondra <tomas(dot)vondra(at)2ndquadrant(dot)com>
Cc: Jeff Janes <jeff(dot)janes(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Kevin Grittner <kgrittn(at)ymail(dot)com>, "pgsql-performance(at)postgresql(dot)org" <pgsql-performance(at)postgresql(dot)org>
Subject: Re: EXPLAIN (no ANALYZE) taking an hour for INSERT FROM SELECT
Date: 2015-03-11 15:54:40
Message-ID: CAPs+M8JaFmns1me5PB5TE9jsTscM0yW9ONL4JwzFHmcbQ-9p-g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Sat, Mar 7, 2015 at 3:44 PM, Tomas Vondra <tomas(dot)vondra(at)2ndquadrant(dot)com>
wrote:

> Another possibility is that this is part
> of some large batch, and autovacuum simply did not have change to do the
> work.
>

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.

This seems very likely to be the explanation here. So our workaround will
be to simply omit the WHERE clause in those cases where the full DELETE has
been issued. (And then vacuum afterwards.)

(Even better, just make the new table not temporary, and have it replace
the former table altogether. But that's for later; requires some broader
changes in our application.)

I'll report back if I *do* see the problem come up again despite this
change.

Thanks all for your help figuring this out!

Best regards,

Gulli

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Tom Lane 2015-03-11 16:15:33 Re: EXPLAIN (no ANALYZE) taking an hour for INSERT FROM SELECT
Previous Message Nicolas Paris 2015-03-10 08:53:20 Re: PG 9.3 materialized view VS Views, indexes, shared memory