Skip to content

Commit 5853662

Browse files
committed
Reduce the size of memory allocations by lazy vacuum when processing a small
table, by allocating just enough for a hardcoded number of dead tuples per page. The current estimate is 200 dead tuples per page. Per reports from Jeff Amiel, Erik Jones and Marko Kreen, and subsequent discussion. CVS: ---------------------------------------------------------------------- CVS: Enter Log. Lines beginning with `CVS:' are removed automatically CVS: CVS: Committing in . CVS: CVS: Modified Files: CVS: commands/vacuumlazy.c CVS: ----------------------------------------------------------------------
1 parent 48f7e64 commit 5853662

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/backend/commands/vacuumlazy.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
* on the number of tuples and pages we will keep track of at once.
1212
*
1313
* We are willing to use at most maintenance_work_mem memory space to keep
14-
* track of dead tuples. We initially allocate an array of TIDs of that size.
15-
* If the array threatens to overflow, we suspend the heap scan phase and
16-
* perform a pass of index cleanup and page compaction, then resume the heap
17-
* scan with an empty TID array.
14+
* track of dead tuples. We initially allocate an array of TIDs of that size,
15+
* with an upper limit that depends on table size (this limit ensures we don't
16+
* allocate a huge area uselessly for vacuuming small tables). If the array
17+
* threatens to overflow, we suspend the heap scan phase and perform a pass of
18+
* index cleanup and page compaction, then resume the heap scan with an empty
19+
* TID array.
1820
*
1921
* We can limit the storage for page free space to MaxFSMPages entries,
2022
* since that's the most the free space map will be willing to remember
@@ -36,7 +38,7 @@
3638
*
3739
*
3840
* IDENTIFICATION
39-
* $PostgreSQL: pgsql/src/backend/commands/vacuumlazy.c,v 1.99 2007/09/24 03:12:23 tgl Exp $
41+
* $PostgreSQL: pgsql/src/backend/commands/vacuumlazy.c,v 1.100 2007/09/24 03:52:55 alvherre Exp $
4042
*
4143
*-------------------------------------------------------------------------
4244
*/
@@ -68,6 +70,12 @@
6870
#define REL_TRUNCATE_MINIMUM 1000
6971
#define REL_TRUNCATE_FRACTION 16
7072

73+
/*
74+
* Guesstimation of number of dead tuples per page. This is used to
75+
* provide an upper limit to memory allocated when vacuuming small
76+
* tables.
77+
*/
78+
#define LAZY_ALLOC_TUPLES 200
7179

7280
typedef struct LVRelStats
7381
{
@@ -1001,6 +1009,11 @@ lazy_space_alloc(LVRelStats *vacrelstats, BlockNumber relblocks)
10011009
maxtuples = (maintenance_work_mem * 1024L) / sizeof(ItemPointerData);
10021010
maxtuples = Min(maxtuples, INT_MAX);
10031011
maxtuples = Min(maxtuples, MaxAllocSize / sizeof(ItemPointerData));
1012+
1013+
/* curious coding here to ensure the multiplication can't overflow */
1014+
if ((BlockNumber) (maxtuples / LAZY_ALLOC_TUPLES) > relblocks)
1015+
maxtuples = relblocks * LAZY_ALLOC_TUPLES;
1016+
10041017
/* stay sane if small maintenance_work_mem */
10051018
maxtuples = Max(maxtuples, MaxHeapTuplesPerPage);
10061019
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy