pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Mohamed Wael Khobalatte <mkhobalatte@grubhub.com>
Cc: pgsql-general@postgresql.org
Subject: Re: Degraded performance during table rewrite
Date: Sat, 04 Jul 2020 15:58:56 -0400
Message-ID: <1206017.1593892736@sss.pgh.pa.us> (raw)
In-Reply-To: <CABZeWdwxXdV6s0CbM=SsREc4cHt8v8ZPRbYy9fM9k1fwh281Cg@mail.gmail.com>
References: <CABZeWdxmAafKEr7tugufWYUgAS3AnVRAwCRA8GOnRh-aTzeyCA@mail.gmail.com>
	<1115603.1593811564@sss.pgh.pa.us>
	<CABZeWdxu=04m-xEHbY5Rdsvb_s98=yKfJdGRzSWZ5PkKr8Z=6A@mail.gmail.com>
	<1154267.1593829869@sss.pgh.pa.us>
	<CABZeWdzVmHWWSfqzwmY5pSv2jU=41MRo0O=K9CfHKf94Nb3GdQ@mail.gmail.com>
	<CABZeWdwxXdV6s0CbM=SsREc4cHt8v8ZPRbYy9fM9k1fwh281Cg@mail.gmail.com>

Mohamed Wael Khobalatte <mkhobalatte@grubhub.com> writes:
> Tom, I think I understood what you meant after getting some sleep. What I
> do now is:

> - Initially load a lot of ids, say a million.
> - Then I slice that array by my preferred batch, say a 10000.
> -  Loop through these slices and run an improved query, which looks like
> this:

>         WITH del AS (
>           DELETE FROM #{old_table}
>           WHERE id >= #{first_id_in_slice} AND id <= #{last_id_in_slice}
>           RETURNING *
>         )
>         INSERT INTO #{table}
>         SELECT * FROM del
>         RETURNING id

Right, the problem occurs when you're deleting stuff that's at the end
of the id range, and have already deleted stuff that was previously the
end of the range.  If you start somewhere in the middle and work towards
the end, you dodge that problem, even for the very last delete that
*is* handling the endmost ids.

			regards, tom lane





view thread (11+ messages)  latest in thread

Message-ID: <1206017.1593892736@sss.pgh.pa.us>
Permalink:  ../1206017.1593892736@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1206017.1593892736@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, mkhobalatte@grubhub.com
  Subject: Re: Degraded performance during table rewrite
  In-Reply-To: <1206017.1593892736@sss.pgh.pa.us>
  References: <CABZeWdxmAafKEr7tugufWYUgAS3AnVRAwCRA8GOnRh-aTzeyCA@mail.gmail.com> <1115603.1593811564@sss.pgh.pa.us> <CABZeWdxu=04m-xEHbY5Rdsvb_s98=yKfJdGRzSWZ5PkKr8Z=6A@mail.gmail.com> <1154267.1593829869@sss.pgh.pa.us> <CABZeWdzVmHWWSfqzwmY5pSv2jU=41MRo0O=K9CfHKf94Nb3GdQ@mail.gmail.com> <CABZeWdwxXdV6s0CbM=SsREc4cHt8v8ZPRbYy9fM9k1fwh281Cg@mail.gmail.com> <1206017.1593892736@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox