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 1jrTCL-00043z-It for pgsql-general@arkaria.postgresql.org; Fri, 03 Jul 2020 21:26:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jrTCK-0000Hh-Gc for pgsql-general@arkaria.postgresql.org; Fri, 03 Jul 2020 21:26:16 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jrTCK-0000HF-43 for pgsql-general@lists.postgresql.org; Fri, 03 Jul 2020 21:26:16 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jrTCA-0006iM-Gc for pgsql-general@postgresql.org; Fri, 03 Jul 2020 21:26:12 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 063LQ4UC1115604; Fri, 3 Jul 2020 17:26:04 -0400 From: Tom Lane To: Mohamed Wael Khobalatte cc: pgsql-general@postgresql.org Subject: Re: Degraded performance during table rewrite In-reply-to: References: Comments: In-reply-to Mohamed Wael Khobalatte message dated "Fri, 03 Jul 2020 16:24:10 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1115602.1593811564.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 03 Jul 2020 17:26:04 -0400 Message-ID: <1115603.1593811564@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Mohamed Wael Khobalatte writes: > ... 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 > This spends 150ms per batch, which climbs to 700ms per batch. A vacuum o= f > the old table lowers is back to 150ms, but I don't understand why, becau= se > we structure the query to jump over all previously dead rows. There is a= n > old thread in which Tom Lane mentions that the planner might itself be > walking that primary index. Is this applicable here? And is there anythi= ng > we can do besides more aggressive and continued vacuuming of the old tab= le > (or a change in autovacuum settings)? Ideally, we want to run this > overnight without much supervision. Yeah, given that the slowdown seems to be in the planner, and given your further observation that v12 is better, I'd say that this is an issue with get_actual_variable_range. That's going to be invoked to try to determine the selectivity of the "WHERE id > #{max_deleted_id}" clause, if the constant is past the last value in the histogram for the id column. The improvement you see in v12 actually came in in v11, and I think I'll just quote the commit log: Author: Tom Lane Branch: master Release: REL_11_BR [3ca930fc3] 2017-09-07 19:41:51 -0400 Improve performance of get_actual_variable_range with recently-dead tu= ples. = In commit fccebe421, we hacked get_actual_variable_range() to scan the index with SnapshotDirty, so that if there are many uncommitted tuples at the end of the index range, it wouldn't laboriously scan through al= l of them looking for a live value to return. However, that didn't fix = it for the case of many recently-dead tuples at the end of the index; SnapshotDirty recognizes those as committed dead and so we're back to the same problem. = To improve the situation, invent a "SnapshotNonVacuumable" snapshot ty= pe and use that instead. The reason this helps is that, if the snapshot rejects a given index entry, we know that the indexscan will mark that index entry as killed. This means the next get_actual_variable_range(= ) scan will proceed past that entry without visiting the heap, making th= e scan a lot faster. We may end up accepting a recently-dead tuple as being the estimated extremal value, but that doesn't seem much worse t= han the compromise we made before to accept not-yet-committed extremal val= ues. = The cost of the scan is still proportional to the number of dead index entries at the end of the range, so in the interval after a mass delet= e but before VACUUM's cleaned up the mess, it's still possible for get_actual_variable_range() to take a noticeable amount of time, if yo= u've got enough such dead entries. But the constant factor is much much be= tter than before, since all we need to do with each index entry is test its "killed" bit. = We chose to back-patch commit fccebe421 at the time, but I'm hesitant = to do so here, because this form of the problem seems to affect many fewe= r people. Also, even when it happens, it's less bad than the case fixed by commit fccebe421 because we don't get the contention effects from expensive TransactionIdIsInProgress tests. = Dmitriy Sarafannikov, reviewed by Andrey Borodin = Discussion: https://postgr.es/m/05C72CF7-B5F6-4DB9-8A09-5AC897653113@y= andex.ru There are a number of possibilities for working around this in your particular situation, short of an upgrade to v11+. You could try doing a manual VACUUM between deletion steps, but that could fail to fix it if anything else is running concurrently (because the VACUUM might not think it's safe to recycle the recently-dead tuples yet). I think possibly a better approach is to try to avoid the situation wherein estimating "WHERE id > #{max_deleted_id}" requires determining the table's true endpoint id value. For that, the last id value seen in the pg_stats histogram for the id column has to be greater than the max_deleted_id value. So you might find that increasing the deletion batch size (thereby reducing max_deleted_id) does the trick; or you could increase the statistics target for that column, making the histogram larger and hence (probably) making its endpoint higher. regards, tom lane