Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jrTJf-0004PY-Vy for pgsql-general@arkaria.postgresql.org; Fri, 03 Jul 2020 21:33:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jrTJe-00040c-8d for pgsql-general@arkaria.postgresql.org; Fri, 03 Jul 2020 21:33:50 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jrTJd-00040U-Te for pgsql-general@lists.postgresql.org; Fri, 03 Jul 2020 21:33:50 +0000 Received: from mail-ot1-x331.google.com ([2607:f8b0:4864:20::331]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jrTJU-00032Q-Ua for pgsql-general@lists.postgresql.org; Fri, 03 Jul 2020 21:33:47 +0000 Received: by mail-ot1-x331.google.com with SMTP id 18so27552443otv.6 for ; Fri, 03 Jul 2020 14:33:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=SDexKzwVv/zngpuvxVvUptzPZUFA+GCkVvcd8Ro7pls=; b=aXfv7qiZZzMnnj+o1OUqge0ViIsICYo2+f61N0mOXDXgerVEHUgqJoG10VZV1thxPk xEyeCFTeJK6bZZhg3lS4PSok8B2Y06HIsD2AuvFoczy0IPGDeQgB1dkSPVwaex7/81c8 TePJkPq7X6zxM0TWKnlNXGpihG+LnqlBJkZoZlPatKPInzbpdM95W7ogk/rmyWBkdzP+ sz1NgcYDeoVCaZlhAA0vwoHOwkucBZv4cOekLggSnZNnSdIpVRiIicBJRrcZBhcNlUpM L17Yk8tTceKEjQvS5nHTIwKkzs5KeOfCBbpFg2moNMcYaksVLNHEmYuouPsR80vEQarN 79EA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=SDexKzwVv/zngpuvxVvUptzPZUFA+GCkVvcd8Ro7pls=; b=Ifcxc262WG+P+mravG+qnk4chchMDppu+SmHBuk2Ajo2vaqspr/4rQPAQmDh2HI4SO 9YzvdBlaR6seOF/tHtgV9ic1v9MePAQ/2DkLgGV1vcZPQTrHHqb9fpStjPtuW/SwAVTg iJMdp45u+DEOURZpoPllNqdrcNgJ52Ud9f2ShoTXpuvGSCz13QCWk070X5nG9Jiohq+c jtoMcpwiocqhW0V5spqiSrvqV7vOcCFziFUzrnlBUVIILXYeltikunyPCcD3I7f6U3rd N+trpBsstnmRXo6FPHIOmXMfuUWR+yFR+e2vno9+QriEbm49y5iAtyk2og3CSifoCIQi uv9Q== X-Gm-Message-State: AOAM531qGrj9dxiOJDGtz9KUv+56cxywn1U5eRJXQ/ncQeW/g7+SWrhs tHZsOQ98ItrbdtY28unaa1btsZzL5hNTvQ== X-Google-Smtp-Source: ABdhPJxHKWGKE4lneg0iwXU0UyKX0huC67Uwmw4nFdrP7FiVRYqfqTuR+o3/EQ5qy1M7R5BY3D/LuQ== X-Received: by 2002:a9d:222:: with SMTP id 31mr8315495otb.127.1593812018787; Fri, 03 Jul 2020 14:33:38 -0700 (PDT) Received: from [192.168.1.10] (ip70-186-126-197.no.no.cox.net. [70.186.126.197]) by smtp.googlemail.com with ESMTPSA id h68sm3697517oib.15.2020.07.03.14.33.37 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 03 Jul 2020 14:33:38 -0700 (PDT) Subject: Re: Degraded performance during table rewrite To: pgsql-general@lists.postgresql.org References: From: Ron Message-ID: <7147e502-5925-5e13-096e-757b5764f000@gmail.com> Date: Fri, 3 Jul 2020 16:33:37 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------FA01FF25246E083C9C584917" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------FA01FF25246E083C9C584917 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 7/3/20 3:24 PM, Mohamed Wael Khobalatte wrote: > Hi all, > > I am attempting to do a bunch of table rewrites to shrink a table in the > absence of pg_repack and vacuum full (both not an option). The database is > growing fast and has had significant bloat in both heaps and indexes, so > index rebuilds alone won't cut it. We found that table inheritance can be > used to achieve this rather nicely. We are running PG v9.6.18. > > We are setting up the inheritance as follows: > > BEGIN; > ALTER TABLE #{table} RENAME TO #{table}_old; > CREATE TABLE #{table} (LIKE #{table}_old INCLUDING ALL); > ALTER TABLE #{table}_old INHERIT #{table}; > ALTER SEQUENCE #{table}_id_seq OWNED BY #{table}.id; > COMMIT; > > Then, the migration itself runs as follows (each in a transaction, looping > through records and sleeping for a bit) > > WITH del AS ( >   DELETE FROM #{old_table} >   WHERE id IN ( >     SELECT id >     FROM #{old_table} >     WHERE id > #{max_deleted_id} -- This is the max deleted from the > previous batch, we grab it programmatically. >     ORDER BY id ASC >     LIMIT #{batch_size} >   ) >   RETURNING * > ) > INSERT INTO #{table} > SELECT * FROM del > RETURNING id > > For instance, the batch_size can be 10_000, and the code sleeps > programatically for 200ms (this is done in a Ruby script). > > "max_deleted_id" is passed to each run from the previous one. This > improves the inner SELECT query. > > This works very well. However, I noticed two suprising things: > > 1. The performance of the delete and insert drops by several orders of > magnitude as the script runs. For instance, in one run, it goes from 150ms > average run to 700ms per batch. > > 2. The explain itself takes a while to run on a sample batch. In one > table, the explain alone took four seconds. > > To try and reproduce this locally, I used the following dummy table: > > create table towns (id serial primary key, code text, article text, name > text, department text); > > insert into towns ( >     code, article, name, department > ) > select >     left(md5(i::text), 10), >     md5(random()::text), >     md5(random()::text), >     left(md5(random()::text), 4) > > from generate_series(1, 100000000) s(i); > > This spends 150ms per batch, which climbs to 700ms per batch. A vacuum of > the old table lowers is back to 150ms, but I don't understand why, because > we structure the query to jump over all previously dead rows. There is an > old thread in which Tom Lane mentions that the planner might itself be > walking that primary index. Is this applicable here? And is there anything > we can do besides more aggressive and continued vacuuming of the old table > (or a change in autovacuum settings)? Ideally, we want to run this > overnight without much supervision. Is the problem really about writing the new table, or reading from the old table? -- Angular momentum makes the world go 'round. --------------FA01FF25246E083C9C584917 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit On 7/3/20 3:24 PM, Mohamed Wael Khobalatte wrote:
Hi all, 

I am attempting to do a bunch of table rewrites to shrink a table in the absence of pg_repack and vacuum full (both not an option). The database is growing fast and has had significant bloat in both heaps and indexes, so index rebuilds alone won't cut it. We found that table inheritance can be used to achieve this rather nicely. We are running PG v9.6.18.

We are setting up the inheritance as follows: 

BEGIN;
ALTER TABLE #{table} RENAME TO #{table}_old;
CREATE TABLE #{table} (LIKE #{table}_old INCLUDING ALL);
ALTER TABLE #{table}_old INHERIT #{table};
ALTER SEQUENCE #{table}_id_seq OWNED BY #{table}.id;
COMMIT; 

Then, the migration itself runs as follows (each in a transaction, looping through records and sleeping for a bit)

WITH del AS (
  DELETE FROM #{old_table}
  WHERE id IN (
    SELECT id
    FROM #{old_table}
    WHERE id > #{max_deleted_id} -- This is the max deleted from the previous batch, we grab it programmatically.
    ORDER BY id ASC
    LIMIT #{batch_size}
  )
  RETURNING *
)
INSERT INTO #{table}
SELECT * FROM del
RETURNING id

For instance, the batch_size can be 10_000, and the code sleeps programatically for 200ms (this is done in a Ruby script).

"max_deleted_id" is passed to each run from the previous one. This improves the inner SELECT query.

This works very well. However, I noticed two suprising things:

1. The performance of the delete and insert drops by several orders of magnitude as the script runs. For instance, in one run, it goes from 150ms average run to 700ms per batch.

2. The explain itself takes a while to run on a sample batch. In one table, the explain alone took four seconds.

To try and reproduce this locally, I used the following dummy table:

create table towns (id serial primary key, code text, article text, name text, department text);

insert into towns (
    code, article, name, department
)
select
    left(md5(i::text), 10),
    md5(random()::text),
    md5(random()::text),
    left(md5(random()::text), 4)

from generate_series(1, 100000000) s(i);

This spends 150ms per batch, which climbs to 700ms per batch. A vacuum of the old table lowers is back to 150ms, but I don't understand why, because we structure the query to jump over all previously dead rows. There is an old thread in which Tom Lane mentions that the planner might itself be walking that primary index. Is this applicable here? And is there anything we can do besides more aggressive and continued vacuuming of the old table (or a change in autovacuum settings)? Ideally, we want to run this overnight without much supervision.  

Is the problem really about writing the new table, or reading from the old table?

--
Angular momentum makes the world go 'round.
--------------FA01FF25246E083C9C584917--