On Mon, Sep 5, 2016 at 11:50 AM, Claudio Freire <klaussfre...@gmail.com> wrote: > On Sun, Sep 4, 2016 at 3:46 AM, Simon Riggs <si...@2ndquadrant.com> wrote: >> On 3 September 2016 at 04:25, Claudio Freire <klaussfre...@gmail.com> wrote: >>> The patch also makes vacuum free the dead_tuples before starting >>> truncation. It didn't seem necessary to hold onto it beyond that >>> point, and it might help give the OS more cache, especially if work >>> mem is configured very high to avoid multiple index scans. >> >> How long does that part ever take? Is there any substantial gain from this? >> >> Lets discuss that as a potential second patch. > > In the test case I mentioned, it takes longer than the vacuum part itself. > > Other than freeing RAM there's no gain. I didn't measure any speed > difference while testing, but that's probably because the backward > scan doesn't benefit from the cache, but other activity on the system > might. So, depending on the workload on the server, extra available > RAM may be a significant gain on its own or not. It just didn't seem > there was a reason to keep that RAM reserved, especially after making > it a huge allocation. > > I'm fine either way. I can remove that from the patch or leave it > as-is. It just seemed like a good idea at the time.
Rebased and split versions attached
From 7b47b59d89bf3edaeb11c4ffe3660f3d5b7b86de Mon Sep 17 00:00:00 2001 From: Claudio Freire <klaussfre...@gmail.com> Date: Fri, 2 Sep 2016 23:21:01 -0300 Subject: [PATCH 1/2] Vacuum: allow using more than 1GB work mem Turns the palloc for dead_tuples into a huge allocation to allow using more than 1GB worth of dead_tuples, saving index scans on heavily bloated tables --- src/backend/commands/vacuumlazy.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/backend/commands/vacuumlazy.c b/src/backend/commands/vacuumlazy.c index 231e92d..2c98da4 100644 --- a/src/backend/commands/vacuumlazy.c +++ b/src/backend/commands/vacuumlazy.c @@ -1952,7 +1952,7 @@ lazy_space_alloc(LVRelStats *vacrelstats, BlockNumber relblocks) { maxtuples = (vac_work_mem * 1024L) / sizeof(ItemPointerData); maxtuples = Min(maxtuples, INT_MAX); - maxtuples = Min(maxtuples, MaxAllocSize / sizeof(ItemPointerData)); + maxtuples = Min(maxtuples, MaxAllocHugeSize / sizeof(ItemPointerData)); /* curious coding here to ensure the multiplication can't overflow */ if ((BlockNumber) (maxtuples / LAZY_ALLOC_TUPLES) > relblocks) @@ -1969,7 +1969,7 @@ lazy_space_alloc(LVRelStats *vacrelstats, BlockNumber relblocks) vacrelstats->num_dead_tuples = 0; vacrelstats->max_dead_tuples = (int) maxtuples; vacrelstats->dead_tuples = (ItemPointer) - palloc(maxtuples * sizeof(ItemPointerData)); + MemoryContextAllocHuge(CurrentMemoryContext, maxtuples * sizeof(ItemPointerData)); } /* -- 2.6.6
From 24fa0815d915cc31ae455ef60585f54a33dbd09c Mon Sep 17 00:00:00 2001 From: Claudio Freire <klaussfre...@gmail.com> Date: Fri, 2 Sep 2016 23:21:01 -0300 Subject: [PATCH 2/2] Vacuum: free dead_tuples sooner Frees the dead_tuples array sooner, decreasing impact of huge settings for autovacuum_work_mem or maintainance_work_mem during lazy vacuum. --- src/backend/commands/vacuumlazy.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/backend/commands/vacuumlazy.c b/src/backend/commands/vacuumlazy.c index 2c98da4..dbe2040 100644 --- a/src/backend/commands/vacuumlazy.c +++ b/src/backend/commands/vacuumlazy.c @@ -155,6 +155,7 @@ static void lazy_truncate_heap(Relation onerel, LVRelStats *vacrelstats); static BlockNumber count_nondeletable_pages(Relation onerel, LVRelStats *vacrelstats); static void lazy_space_alloc(LVRelStats *vacrelstats, BlockNumber relblocks); +static void lazy_space_dealloc(LVRelStats *vacrelstats); static void lazy_record_dead_tuple(LVRelStats *vacrelstats, ItemPointer itemptr); static bool lazy_tid_reaped(ItemPointer itemptr, void *state); @@ -1310,6 +1311,8 @@ lazy_scan_heap(Relation onerel, int options, LVRelStats *vacrelstats, for (i = 0; i < nindexes; i++) lazy_cleanup_index(Irel[i], indstats[i], vacrelstats); + lazy_space_dealloc(vacrelstats); + /* If no indexes, make log report that lazy_vacuum_heap would've made */ if (vacuumed_pages) ereport(elevel, @@ -1973,6 +1976,18 @@ lazy_space_alloc(LVRelStats *vacrelstats, BlockNumber relblocks) } /* + * lazy_space_dealloc - free lazy vacuum work mem + */ +static void +lazy_space_dealloc(LVRelStats *vacrelstats) +{ + if (vacrelstats->dead_tuples != NULL) { + pfree(vacrelstats->dead_tuples); + vacrelstats->dead_tuples = NULL; + } +} + +/* * lazy_record_dead_tuple - remember one deletable tuple */ static void -- 2.6.6
-- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers