PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
blvacuum.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "bloom.h"
#include "commands/vacuum.h"
#include "storage/bufmgr.h"
#include "storage/indexfsm.h"
Include dependency graph for blvacuum.c:

Go to the source code of this file.

Functions

IndexBulkDeleteResultblbulkdelete (IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
 
IndexBulkDeleteResultblvacuumcleanup (IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
 

Function Documentation

◆ blbulkdelete()

IndexBulkDeleteResult * blbulkdelete ( IndexVacuumInfo info,
IndexBulkDeleteResult stats,
IndexBulkDeleteCallback  callback,
void *  callback_state 
)

Definition at line 30 of file blvacuum.c.

32{
33 Relation index = info->index;
34 BlockNumber blkno,
35 npages;
36 FreeBlockNumberArray notFullPage;
37 int countPage = 0;
39 Buffer buffer;
40 Page page;
41 BloomMetaPageData *metaData;
42 GenericXLogState *gxlogState;
43
44 if (stats == NULL)
46
48
49 /*
50 * Iterate over the pages. We don't care about concurrently added pages,
51 * they can't contain tuples to delete.
52 */
54 for (blkno = BLOOM_HEAD_BLKNO; blkno < npages; blkno++)
55 {
56 BloomTuple *itup,
57 *itupPtr,
58 *itupEnd;
59
61
62 buffer = ReadBufferExtended(index, MAIN_FORKNUM, blkno,
63 RBM_NORMAL, info->strategy);
64
66 gxlogState = GenericXLogStart(index);
67 page = GenericXLogRegisterBuffer(gxlogState, buffer, 0);
68
69 /* Ignore empty/deleted pages until blvacuumcleanup() */
70 if (PageIsNew(page) || BloomPageIsDeleted(page))
71 {
72 UnlockReleaseBuffer(buffer);
73 GenericXLogAbort(gxlogState);
74 continue;
75 }
76
77 /*
78 * Iterate over the tuples. itup points to current tuple being
79 * scanned, itupPtr points to where to save next non-deleted tuple.
80 */
81 itup = itupPtr = BloomPageGetTuple(&state, page, FirstOffsetNumber);
82 itupEnd = BloomPageGetTuple(&state, page,
84 while (itup < itupEnd)
85 {
86 /* Do we have to delete this tuple? */
87 if (callback(&itup->heapPtr, callback_state))
88 {
89 /* Yes; adjust count of tuples that will be left on page */
90 BloomPageGetOpaque(page)->maxoff--;
91 stats->tuples_removed += 1;
92 }
93 else
94 {
95 /* No; copy it to itupPtr++, but skip copy if not needed */
96 if (itupPtr != itup)
97 memmove((Pointer) itupPtr, (Pointer) itup,
98 state.sizeOfBloomTuple);
99 itupPtr = BloomPageGetNextTuple(&state, itupPtr);
100 }
101
102 itup = BloomPageGetNextTuple(&state, itup);
103 }
104
105 /* Assert that we counted correctly */
106 Assert(itupPtr == BloomPageGetTuple(&state, page,
108
109 /*
110 * Add page to new notFullPage list if we will not mark page as
111 * deleted and there is free space on it
112 */
113 if (BloomPageGetMaxOffset(page) != 0 &&
114 BloomPageGetFreeSpace(&state, page) >= state.sizeOfBloomTuple &&
115 countPage < BloomMetaBlockN)
116 notFullPage[countPage++] = blkno;
117
118 /* Did we delete something? */
119 if (itupPtr != itup)
120 {
121 /* Is it empty page now? */
122 if (BloomPageGetMaxOffset(page) == 0)
124 /* Adjust pd_lower */
125 ((PageHeader) page)->pd_lower = (Pointer) itupPtr - page;
126 /* Finish WAL-logging */
127 GenericXLogFinish(gxlogState);
128 }
129 else
130 {
131 /* Didn't change anything: abort WAL-logging */
132 GenericXLogAbort(gxlogState);
133 }
134 UnlockReleaseBuffer(buffer);
135 }
136
137 /*
138 * Update the metapage's notFullPage list with whatever we found. Our
139 * info could already be out of date at this point, but blinsert() will
140 * cope if so.
141 */
144
145 gxlogState = GenericXLogStart(index);
146 page = GenericXLogRegisterBuffer(gxlogState, buffer, 0);
147
148 metaData = BloomPageGetMeta(page);
149 memcpy(metaData->notFullPage, notFullPage, sizeof(BlockNumber) * countPage);
150 metaData->nStart = 0;
151 metaData->nEnd = countPage;
152
153 GenericXLogFinish(gxlogState);
154 UnlockReleaseBuffer(buffer);
155
156 return stats;
157}
uint32 BlockNumber
Definition: block.h:31
#define BloomPageGetOpaque(page)
Definition: bloom.h:60
#define BloomPageGetFreeSpace(state, page)
Definition: bloom.h:149
#define BloomPageGetMeta(page)
Definition: bloom.h:133
BlockNumber FreeBlockNumberArray[MAXALIGN_DOWN(BLCKSZ - SizeOfPageHeaderData - MAXALIGN(sizeof(BloomPageOpaqueData)) - MAXALIGN(sizeof(uint16) *2+sizeof(uint32)+sizeof(BloomOptions)))/sizeof(BlockNumber)]
Definition: bloom.h:115
#define BloomPageGetMaxOffset(page)
Definition: bloom.h:61
#define BloomPageGetTuple(state, page, offset)
Definition: bloom.h:71
#define BloomPageSetDeleted(page)
Definition: bloom.h:66
#define BloomPageGetNextTuple(state, tuple)
Definition: bloom.h:74
#define BloomPageIsDeleted(page)
Definition: bloom.h:64
void initBloomState(BloomState *state, Relation index)
Definition: blutils.c:161
#define BLOOM_HEAD_BLKNO
Definition: bloom.h:79
#define BloomMetaBlockN
Definition: bloom.h:131
#define BLOOM_METAPAGE_BLKNO
Definition: bloom.h:78
int Buffer
Definition: buf.h:23
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4941
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:5158
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:793
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:746
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:273
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:191
@ RBM_NORMAL
Definition: bufmgr.h:45
PageHeaderData * PageHeader
Definition: bufpage.h:173
Pointer Page
Definition: bufpage.h:81
static bool PageIsNew(Page page)
Definition: bufpage.h:233
char * Pointer
Definition: c.h:476
#define Assert(condition)
Definition: c.h:812
Page GenericXLogRegisterBuffer(GenericXLogState *state, Buffer buffer, int flags)
Definition: generic_xlog.c:299
GenericXLogState * GenericXLogStart(Relation relation)
Definition: generic_xlog.c:269
XLogRecPtr GenericXLogFinish(GenericXLogState *state)
Definition: generic_xlog.c:337
void GenericXLogAbort(GenericXLogState *state)
Definition: generic_xlog.c:444
void * palloc0(Size size)
Definition: mcxt.c:1347
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
#define FirstOffsetNumber
Definition: off.h:27
@ MAIN_FORKNUM
Definition: relpath.h:58
FreeBlockNumberArray notFullPage
Definition: bloom.h:124
uint16 nEnd
Definition: bloom.h:122
uint16 nStart
Definition: bloom.h:121
ItemPointerData heapPtr
Definition: bloom.h:159
double tuples_removed
Definition: genam.h:82
Relation index
Definition: genam.h:48
BufferAccessStrategy strategy
Definition: genam.h:55
Definition: type.h:96
Definition: regguts.h:323
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:46
void vacuum_delay_point(void)
Definition: vacuum.c:2362

References Assert, BLOOM_HEAD_BLKNO, BLOOM_METAPAGE_BLKNO, BloomMetaBlockN, BloomPageGetFreeSpace, BloomPageGetMaxOffset, BloomPageGetMeta, BloomPageGetNextTuple, BloomPageGetOpaque, BloomPageGetTuple, BloomPageIsDeleted, BloomPageSetDeleted, BUFFER_LOCK_EXCLUSIVE, callback(), FirstOffsetNumber, GenericXLogAbort(), GenericXLogFinish(), GenericXLogRegisterBuffer(), GenericXLogStart(), BloomTuple::heapPtr, IndexVacuumInfo::index, initBloomState(), LockBuffer(), MAIN_FORKNUM, BloomMetaPageData::nEnd, BloomMetaPageData::notFullPage, BloomMetaPageData::nStart, OffsetNumberNext, PageIsNew(), palloc0(), RBM_NORMAL, ReadBuffer(), ReadBufferExtended(), RelationGetNumberOfBlocks, IndexVacuumInfo::strategy, IndexBulkDeleteResult::tuples_removed, UnlockReleaseBuffer(), and vacuum_delay_point().

Referenced by blhandler().

◆ blvacuumcleanup()

IndexBulkDeleteResult * blvacuumcleanup ( IndexVacuumInfo info,
IndexBulkDeleteResult stats 
)

Definition at line 165 of file blvacuum.c.

166{
167 Relation index = info->index;
168 BlockNumber npages,
169 blkno;
170
171 if (info->analyze_only)
172 return stats;
173
174 if (stats == NULL)
176
177 /*
178 * Iterate over the pages: insert deleted pages into FSM and collect
179 * statistics.
180 */
182 stats->num_pages = npages;
183 stats->pages_free = 0;
184 stats->num_index_tuples = 0;
185 for (blkno = BLOOM_HEAD_BLKNO; blkno < npages; blkno++)
186 {
187 Buffer buffer;
188 Page page;
189
191
192 buffer = ReadBufferExtended(index, MAIN_FORKNUM, blkno,
193 RBM_NORMAL, info->strategy);
195 page = (Page) BufferGetPage(buffer);
196
197 if (PageIsNew(page) || BloomPageIsDeleted(page))
198 {
200 stats->pages_free++;
201 }
202 else
203 {
205 }
206
207 UnlockReleaseBuffer(buffer);
208 }
209
211
212 return stats;
213}
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:190
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:400
void IndexFreeSpaceMapVacuum(Relation rel)
Definition: indexfsm.c:71
void RecordFreeIndexPage(Relation rel, BlockNumber freeBlock)
Definition: indexfsm.c:52
BlockNumber pages_free
Definition: genam.h:85
BlockNumber num_pages
Definition: genam.h:79
double num_index_tuples
Definition: genam.h:81
bool analyze_only
Definition: genam.h:50

References IndexVacuumInfo::analyze_only, BLOOM_HEAD_BLKNO, BloomPageGetMaxOffset, BloomPageIsDeleted, BUFFER_LOCK_SHARE, BufferGetPage(), IndexVacuumInfo::index, IndexFreeSpaceMapVacuum(), LockBuffer(), MAIN_FORKNUM, IndexBulkDeleteResult::num_index_tuples, IndexBulkDeleteResult::num_pages, PageIsNew(), IndexBulkDeleteResult::pages_free, palloc0(), RBM_NORMAL, ReadBufferExtended(), RecordFreeIndexPage(), RelationGetNumberOfBlocks, IndexVacuumInfo::strategy, UnlockReleaseBuffer(), and vacuum_delay_point().

Referenced by blhandler().