Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ed8969b

Browse files
committed
Trivial patch to double vacuum speed on tables with no indexes (prevent
second scan of table). Gregory Stark
1 parent c66939c commit ed8969b

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

src/backend/commands/vacuumlazy.c

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
* perform a pass of index cleanup and page compaction, then resume the heap
1717
* scan with an empty TID array.
1818
*
19+
* As a special exception if we're processing a table with no indexes we can
20+
* vacuum each page as we go so we don't need to allocate more space than
21+
* enough to hold as many heap tuples fit on one page.
22+
*
1923
* We can limit the storage for page free space to MaxFSMPages entries,
2024
* since that's the most the free space map will be willing to remember
2125
* anyway. If the relation has fewer than that many pages with free space,
@@ -31,7 +35,7 @@
3135
*
3236
*
3337
* IDENTIFICATION
34-
* $PostgreSQL: pgsql/src/backend/commands/vacuumlazy.c,v 1.76 2006/07/31 20:09:00 tgl Exp $
38+
* $PostgreSQL: pgsql/src/backend/commands/vacuumlazy.c,v 1.77 2006/09/04 21:40:23 momjian Exp $
3539
*
3640
*-------------------------------------------------------------------------
3741
*/
@@ -106,7 +110,7 @@ static void lazy_truncate_heap(Relation onerel, LVRelStats *vacrelstats,
106110
TransactionId OldestXmin);
107111
static BlockNumber count_nondeletable_pages(Relation onerel,
108112
LVRelStats *vacrelstats, TransactionId OldestXmin);
109-
static void lazy_space_alloc(LVRelStats *vacrelstats, BlockNumber relblocks);
113+
static void lazy_space_alloc(LVRelStats *vacrelstats, BlockNumber relblocks, unsigned nindexes);
110114
static void lazy_record_dead_tuple(LVRelStats *vacrelstats,
111115
ItemPointer itemptr);
112116
static void lazy_record_free_space(LVRelStats *vacrelstats,
@@ -206,7 +210,8 @@ lazy_vacuum_rel(Relation onerel, VacuumStmt *vacstmt)
206210
* This routine sets commit status bits, builds lists of dead tuples
207211
* and pages with free space, and calculates statistics on the number
208212
* of live tuples in the heap. When done, or when we run low on space
209-
* for dead-tuple TIDs, invoke vacuuming of indexes and heap.
213+
* for dead-tuple TIDs, or after every page if the table has no indexes
214+
* invoke vacuuming of indexes and heap.
210215
*
211216
* It also updates the minimum Xid found anywhere on the table in
212217
* vacrelstats->minxid, for later storing it in pg_class.relminxid.
@@ -247,7 +252,7 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
247252
vacrelstats->rel_pages = nblocks;
248253
vacrelstats->nonempty_pages = 0;
249254

250-
lazy_space_alloc(vacrelstats, nblocks);
255+
lazy_space_alloc(vacrelstats, nblocks, nindexes);
251256

252257
for (blkno = 0; blkno < nblocks; blkno++)
253258
{
@@ -282,8 +287,14 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
282287

283288
buf = ReadBuffer(onerel, blkno);
284289

285-
/* In this phase we only need shared access to the buffer */
286-
LockBuffer(buf, BUFFER_LOCK_SHARE);
290+
/* In this phase we only need shared access to the buffer unless we're
291+
* going to do the vacuuming now which we do if there are no indexes
292+
*/
293+
294+
if (nindexes)
295+
LockBuffer(buf, BUFFER_LOCK_SHARE);
296+
else
297+
LockBufferForCleanup(buf);
287298

288299
page = BufferGetPage(buf);
289300

@@ -450,6 +461,12 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
450461
{
451462
lazy_record_free_space(vacrelstats, blkno,
452463
PageGetFreeSpace(page));
464+
} else if (!nindexes) {
465+
/* If there are no indexes we can vacuum the page right now instead
466+
* of doing a second scan */
467+
lazy_vacuum_page(onerel, blkno, buf, 0, vacrelstats);
468+
lazy_record_free_space(vacrelstats, blkno, PageGetFreeSpace(BufferGetPage(buf)));
469+
vacrelstats->num_dead_tuples = 0;
453470
}
454471

455472
/* Remember the location of the last page with nonremovable tuples */
@@ -891,16 +908,20 @@ count_nondeletable_pages(Relation onerel, LVRelStats *vacrelstats,
891908
* See the comments at the head of this file for rationale.
892909
*/
893910
static void
894-
lazy_space_alloc(LVRelStats *vacrelstats, BlockNumber relblocks)
911+
lazy_space_alloc(LVRelStats *vacrelstats, BlockNumber relblocks, unsigned nindexes)
895912
{
896913
long maxtuples;
897914
int maxpages;
898915

899-
maxtuples = (maintenance_work_mem * 1024L) / sizeof(ItemPointerData);
900-
maxtuples = Min(maxtuples, INT_MAX);
901-
maxtuples = Min(maxtuples, MaxAllocSize / sizeof(ItemPointerData));
902-
/* stay sane if small maintenance_work_mem */
903-
maxtuples = Max(maxtuples, MaxHeapTuplesPerPage);
916+
if (nindexes) {
917+
maxtuples = (maintenance_work_mem * 1024L) / sizeof(ItemPointerData);
918+
maxtuples = Min(maxtuples, INT_MAX);
919+
maxtuples = Min(maxtuples, MaxAllocSize / sizeof(ItemPointerData));
920+
/* stay sane if small maintenance_work_mem */
921+
maxtuples = Max(maxtuples, MaxHeapTuplesPerPage);
922+
} else {
923+
maxtuples = MaxHeapTuplesPerPage;
924+
}
904925

905926
vacrelstats->num_dead_tuples = 0;
906927
vacrelstats->max_dead_tuples = (int) maxtuples;

0 commit comments

Comments
 (0)