PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
blvacuum.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "bloom.h"
#include "catalog/storage.h"
#include "commands/vacuum.h"
#include "miscadmin.h"
#include "postmaster/autovacuum.h"
#include "storage/bufmgr.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.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

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

Definition at line 34 of file blvacuum.c.

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

Referenced by blhandler().

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

Definition at line 169 of file blvacuum.c.

References IndexVacuumInfo::analyze_only, BLOOM_HEAD_BLKNO, BloomPageGetMaxOffset, BloomPageIsDeleted, buffer, BUFFER_LOCK_SHARE, BufferGetPage, IndexVacuumInfo::index, IndexFreeSpaceMapVacuum(), LockBuffer(), MAIN_FORKNUM, NULL, 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().

170 {
171  Relation index = info->index;
172  BlockNumber npages,
173  blkno;
174 
175  if (info->analyze_only)
176  return stats;
177 
178  if (stats == NULL)
180 
181  /*
182  * Iterate over the pages: insert deleted pages into FSM and collect
183  * statistics.
184  */
185  npages = RelationGetNumberOfBlocks(index);
186  stats->num_pages = npages;
187  stats->pages_free = 0;
188  stats->num_index_tuples = 0;
189  for (blkno = BLOOM_HEAD_BLKNO; blkno < npages; blkno++)
190  {
191  Buffer buffer;
192  Page page;
193 
195 
196  buffer = ReadBufferExtended(index, MAIN_FORKNUM, blkno,
197  RBM_NORMAL, info->strategy);
198  LockBuffer(buffer, BUFFER_LOCK_SHARE);
199  page = (Page) BufferGetPage(buffer);
200 
201  if (PageIsNew(page) || BloomPageIsDeleted(page))
202  {
203  RecordFreeIndexPage(index, blkno);
204  stats->pages_free++;
205  }
206  else
207  {
208  stats->num_index_tuples += BloomPageGetMaxOffset(page);
209  }
210 
211  UnlockReleaseBuffer(buffer);
212  }
213 
215 
216  return stats;
217 }
#define BloomPageIsDeleted(page)
Definition: bloom.h:63
bool analyze_only
Definition: genam.h:47
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:640
BufferAccessStrategy strategy
Definition: genam.h:51
Relation index
Definition: genam.h:46
uint32 BlockNumber
Definition: block.h:31
#define BLOOM_HEAD_BLKNO
Definition: bloom.h:78
Definition: type.h:89
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
BlockNumber num_pages
Definition: genam.h:73
BlockNumber pages_free
Definition: genam.h:79
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
void * palloc0(Size size)
Definition: mcxt.c:878
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:199
#define NULL
Definition: c.h:229
void IndexFreeSpaceMapVacuum(Relation rel)
Definition: indexfsm.c:71
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214
#define PageIsNew(page)
Definition: bufpage.h:225
#define BloomPageGetMaxOffset(page)
Definition: bloom.h:60
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
void vacuum_delay_point(void)
Definition: vacuum.c:1560
void RecordFreeIndexPage(Relation rel, BlockNumber freeBlock)
Definition: indexfsm.c:52
double num_index_tuples
Definition: genam.h:76
int Buffer
Definition: buf.h:23
Pointer Page
Definition: bufpage.h:74