| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* ginvacuum.c |
| 4 |
* delete & vacuum routines for the postgres GIN |
| 5 |
* |
| 6 |
* |
| 7 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 8 |
* Portions Copyright (c) 1994, Regents of the University of California |
| 9 |
* |
| 10 |
* IDENTIFICATION |
| 11 |
* src/backend/access/gin/ginvacuum.c |
| 12 |
*------------------------------------------------------------------------- |
| 13 |
*/ |
| 14 |
|
| 15 |
#include "postgres.h" |
| 16 |
|
| 17 |
#include "access/gin_private.h" |
| 18 |
#include "access/ginxlog.h" |
| 19 |
#include "access/xloginsert.h" |
| 20 |
#include "commands/vacuum.h" |
| 21 |
#include "miscadmin.h" |
| 22 |
#include "storage/indexfsm.h" |
| 23 |
#include "storage/lmgr.h" |
| 24 |
#include "storage/predicate.h" |
| 25 |
#include "utils/memutils.h" |
| 26 |
|
| 27 |
struct GinVacuumState |
| 28 |
{ |
| 29 |
Relation index; |
| 30 |
IndexBulkDeleteResult *result; |
| 31 |
IndexBulkDeleteCallback callback; |
| 32 |
void *callback_state; |
| 33 |
GinState ginstate; |
| 34 |
BufferAccessStrategy strategy; |
| 35 |
MemoryContext tmpCxt; |
| 36 |
}; |
| 37 |
|
| 38 |
/* |
| 39 |
* Vacuums an uncompressed posting list. The size of the must can be specified |
| 40 |
* in number of items (nitems). |
| 41 |
* |
| 42 |
* If none of the items need to be removed, returns NULL. Otherwise returns |
| 43 |
* a new palloc'd array with the remaining items. The number of remaining |
| 44 |
* items is returned in *nremaining. |
| 45 |
*/ |
| 46 |
ItemPointer |
| 47 |
ginVacuumItemPointers(GinVacuumState *gvs, ItemPointerData *items, |
| 48 |
int nitem, int *nremaining) |
| 49 |
{ |
| 50 |
int i, |
| 51 |
remaining = 0; |
| 52 |
ItemPointer tmpitems = NULL; |
| 53 |
|
| 54 |
/* |
| 55 |
* Iterate over TIDs array |
| 56 |
*/ |
| 57 |
for (i = 0; i < nitem; i++) |
| 58 |
{ |
| 59 |
if (gvs->callback(items + i, gvs->callback_state)) |
| 60 |
{ |
| 61 |
gvs->result->tuples_removed += 1; |
| 62 |
if (!tmpitems) |
| 63 |
{ |
| 64 |
/* |
| 65 |
* First TID to be deleted: allocate memory to hold the |
| 66 |
* remaining items. |
| 67 |
*/ |
| 68 |
tmpitems = palloc(sizeof(ItemPointerData) * nitem); |
| 69 |
memcpy(tmpitems, items, sizeof(ItemPointerData) * i); |
| 70 |
} |
| 71 |
} |
| 72 |
else |
| 73 |
{ |
| 74 |
gvs->result->num_index_tuples += 1; |
| 75 |
if (tmpitems) |
| 76 |
tmpitems[remaining] = items[i]; |
| 77 |
remaining++; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
*nremaining = remaining; |
| 82 |
return tmpitems; |
| 83 |
} |
| 84 |
|
| 85 |
/* |
| 86 |
* Create a WAL record for vacuuming entry tree leaf page. |
| 87 |
*/ |
| 88 |
static void |
| 89 |
xlogVacuumPage(Relation index, Buffer buffer) |
| 90 |
{ |
| 91 |
Page page = BufferGetPage(buffer); |
| 92 |
XLogRecPtr recptr; |
| 93 |
|
| 94 |
/* This is only used for entry tree leaf pages. */ |
| 95 |
Assert(!GinPageIsData(page)); |
| 96 |
Assert(GinPageIsLeaf(page)); |
| 97 |
|
| 98 |
if (!RelationNeedsWAL(index)) |
| 99 |
return; |
| 100 |
|
| 101 |
/* |
| 102 |
* Always create a full image, we don't track the changes on the page at |
| 103 |
* any more fine-grained level. This could obviously be improved... |
| 104 |
*/ |
| 105 |
XLogBeginInsert(); |
| 106 |
XLogRegisterBuffer(0, buffer, REGBUF_FORCE_IMAGE | REGBUF_STANDARD); |
| 107 |
|
| 108 |
recptr = XLogInsert(RM_GIN_ID, XLOG_GIN_VACUUM_PAGE); |
| 109 |
PageSetLSN(page, recptr); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
typedef struct DataPageDeleteStack |
| 114 |
{ |
| 115 |
struct DataPageDeleteStack *child; |
| 116 |
struct DataPageDeleteStack *parent; |
| 117 |
|
| 118 |
BlockNumber blkno; /* current block number */ |
| 119 |
Buffer leftBuffer; /* pinned and locked rightest non-deleted page |
| 120 |
* on left */ |
| 121 |
bool isRoot; |
| 122 |
} DataPageDeleteStack; |
| 123 |
|
| 124 |
|
| 125 |
/* |
| 126 |
* Delete a posting tree page. |
| 127 |
*/ |
| 128 |
static void |
| 129 |
ginDeletePage(GinVacuumState *gvs, BlockNumber deleteBlkno, BlockNumber leftBlkno, |
| 130 |
BlockNumber parentBlkno, OffsetNumber myoff, bool isParentRoot) |
| 131 |
{ |
| 132 |
Buffer dBuffer; |
| 133 |
Buffer lBuffer; |
| 134 |
Buffer pBuffer; |
| 135 |
Page page, |
| 136 |
parentPage; |
| 137 |
BlockNumber rightlink; |
| 138 |
|
| 139 |
/* |
| 140 |
* This function MUST be called only if someone of parent pages hold |
| 141 |
* exclusive cleanup lock. This guarantees that no insertions currently |
| 142 |
* happen in this subtree. Caller also acquires Exclusive locks on |
| 143 |
* deletable, parent and left pages. |
| 144 |
*/ |
| 145 |
lBuffer = ReadBufferExtended(gvs->index, MAIN_FORKNUM, leftBlkno, |
| 146 |
RBM_NORMAL, gvs->strategy); |
| 147 |
dBuffer = ReadBufferExtended(gvs->index, MAIN_FORKNUM, deleteBlkno, |
| 148 |
RBM_NORMAL, gvs->strategy); |
| 149 |
pBuffer = ReadBufferExtended(gvs->index, MAIN_FORKNUM, parentBlkno, |
| 150 |
RBM_NORMAL, gvs->strategy); |
| 151 |
|
| 152 |
page = BufferGetPage(dBuffer); |
| 153 |
rightlink = GinPageGetOpaque(page)->rightlink; |
| 154 |
|
| 155 |
/* |
| 156 |
* Any insert which would have gone on the leaf block will now go to its |
| 157 |
* right sibling. |
| 158 |
*/ |
| 159 |
PredicateLockPageCombine(gvs->index, deleteBlkno, rightlink); |
| 160 |
|
| 161 |
START_CRIT_SECTION(); |
| 162 |
|
| 163 |
/* Unlink the page by changing left sibling's rightlink */ |
| 164 |
page = BufferGetPage(lBuffer); |
| 165 |
GinPageGetOpaque(page)->rightlink = rightlink; |
| 166 |
|
| 167 |
/* Delete downlink from parent */ |
| 168 |
parentPage = BufferGetPage(pBuffer); |
| 169 |
#ifdef USE_ASSERT_CHECKING |
| 170 |
do |
| 171 |
{ |
| 172 |
PostingItem *tod = GinDataPageGetPostingItem(parentPage, myoff); |
| 173 |
|
| 174 |
Assert(PostingItemGetBlockNumber(tod) == deleteBlkno); |
| 175 |
} while (0); |
| 176 |
#endif |
| 177 |
GinPageDeletePostingItem(parentPage, myoff); |
| 178 |
|
| 179 |
page = BufferGetPage(dBuffer); |
| 180 |
|
| 181 |
/* |
| 182 |
* we shouldn't change rightlink field to save workability of running |
| 183 |
* search scan |
| 184 |
*/ |
| 185 |
|
| 186 |
/* |
| 187 |
* Mark page as deleted, and remember last xid which could know its |
| 188 |
* address. |
| 189 |
*/ |
| 190 |
GinPageSetDeleted(page); |
| 191 |
GinPageSetDeleteXid(page, ReadNextTransactionId()); |
| 192 |
|
| 193 |
MarkBufferDirty(pBuffer); |
| 194 |
MarkBufferDirty(lBuffer); |
| 195 |
MarkBufferDirty(dBuffer); |
| 196 |
|
| 197 |
if (RelationNeedsWAL(gvs->index)) |
| 198 |
{ |
| 199 |
XLogRecPtr recptr; |
| 200 |
ginxlogDeletePage data; |
| 201 |
|
| 202 |
/* |
| 203 |
* We can't pass REGBUF_STANDARD for the deleted page, because we |
| 204 |
* didn't set pd_lower on pre-9.4 versions. The page might've been |
| 205 |
* binary-upgraded from an older version, and hence not have pd_lower |
| 206 |
* set correctly. Ditto for the left page, but removing the item from |
| 207 |
* the parent updated its pd_lower, so we know that's OK at this |
| 208 |
* point. |
| 209 |
*/ |
| 210 |
XLogBeginInsert(); |
| 211 |
XLogRegisterBuffer(0, dBuffer, 0); |
| 212 |
XLogRegisterBuffer(1, pBuffer, REGBUF_STANDARD); |
| 213 |
XLogRegisterBuffer(2, lBuffer, 0); |
| 214 |
|
| 215 |
data.parentOffset = myoff; |
| 216 |
data.rightLink = GinPageGetOpaque(page)->rightlink; |
| 217 |
data.deleteXid = GinPageGetDeleteXid(page); |
| 218 |
|
| 219 |
XLogRegisterData(&data, sizeof(ginxlogDeletePage)); |
| 220 |
|
| 221 |
recptr = XLogInsert(RM_GIN_ID, XLOG_GIN_DELETE_PAGE); |
| 222 |
PageSetLSN(page, recptr); |
| 223 |
PageSetLSN(parentPage, recptr); |
| 224 |
PageSetLSN(BufferGetPage(lBuffer), recptr); |
| 225 |
} |
| 226 |
|
| 227 |
ReleaseBuffer(pBuffer); |
| 228 |
ReleaseBuffer(lBuffer); |
| 229 |
ReleaseBuffer(dBuffer); |
| 230 |
|
| 231 |
END_CRIT_SECTION(); |
| 232 |
|
| 233 |
gvs->result->pages_newly_deleted++; |
| 234 |
gvs->result->pages_deleted++; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
/* |
| 239 |
* Scans posting tree and deletes empty pages. Caller must lock root page for |
| 240 |
* cleanup. During scan path from root to current page is kept exclusively |
| 241 |
* locked. Also keep left page exclusively locked, because ginDeletePage() |
| 242 |
* needs it. If we try to relock left page later, it could deadlock with |
| 243 |
* ginStepRight(). |
| 244 |
*/ |
| 245 |
static bool |
| 246 |
ginScanToDelete(GinVacuumState *gvs, BlockNumber blkno, bool isRoot, |
| 247 |
DataPageDeleteStack *parent, OffsetNumber myoff) |
| 248 |
{ |
| 249 |
DataPageDeleteStack *me; |
| 250 |
Buffer buffer; |
| 251 |
Page page; |
| 252 |
bool meDelete = false; |
| 253 |
bool isempty; |
| 254 |
|
| 255 |
if (isRoot) |
| 256 |
{ |
| 257 |
me = parent; |
| 258 |
} |
| 259 |
else |
| 260 |
{ |
| 261 |
if (!parent->child) |
| 262 |
{ |
| 263 |
me = (DataPageDeleteStack *) palloc0(sizeof(DataPageDeleteStack)); |
| 264 |
me->parent = parent; |
| 265 |
parent->child = me; |
| 266 |
me->leftBuffer = InvalidBuffer; |
| 267 |
} |
| 268 |
else |
| 269 |
me = parent->child; |
| 270 |
} |
| 271 |
|
| 272 |
buffer = ReadBufferExtended(gvs->index, MAIN_FORKNUM, blkno, |
| 273 |
RBM_NORMAL, gvs->strategy); |
| 274 |
|
| 275 |
if (!isRoot) |
| 276 |
LockBuffer(buffer, GIN_EXCLUSIVE); |
| 277 |
|
| 278 |
page = BufferGetPage(buffer); |
| 279 |
|
| 280 |
Assert(GinPageIsData(page)); |
| 281 |
|
| 282 |
if (!GinPageIsLeaf(page)) |
| 283 |
{ |
| 284 |
OffsetNumber i; |
| 285 |
|
| 286 |
me->blkno = blkno; |
| 287 |
for (i = FirstOffsetNumber; i <= GinPageGetOpaque(page)->maxoff; i++) |
| 288 |
{ |
| 289 |
PostingItem *pitem = GinDataPageGetPostingItem(page, i); |
| 290 |
|
| 291 |
if (ginScanToDelete(gvs, PostingItemGetBlockNumber(pitem), false, me, i)) |
| 292 |
i--; |
| 293 |
} |
| 294 |
|
| 295 |
if (GinPageRightMost(page) && BufferIsValid(me->child->leftBuffer)) |
| 296 |
{ |
| 297 |
UnlockReleaseBuffer(me->child->leftBuffer); |
| 298 |
me->child->leftBuffer = InvalidBuffer; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
if (GinPageIsLeaf(page)) |
| 303 |
isempty = GinDataLeafPageIsEmpty(page); |
| 304 |
else |
| 305 |
isempty = GinPageGetOpaque(page)->maxoff < FirstOffsetNumber; |
| 306 |
|
| 307 |
if (isempty) |
| 308 |
{ |
| 309 |
/* we never delete the left- or rightmost branch */ |
| 310 |
if (BufferIsValid(me->leftBuffer) && !GinPageRightMost(page)) |
| 311 |
{ |
| 312 |
Assert(!isRoot); |
| 313 |
ginDeletePage(gvs, blkno, BufferGetBlockNumber(me->leftBuffer), |
| 314 |
me->parent->blkno, myoff, me->parent->isRoot); |
| 315 |
meDelete = true; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
if (!meDelete) |
| 320 |
{ |
| 321 |
if (BufferIsValid(me->leftBuffer)) |
| 322 |
UnlockReleaseBuffer(me->leftBuffer); |
| 323 |
me->leftBuffer = buffer; |
| 324 |
} |
| 325 |
else |
| 326 |
{ |
| 327 |
if (!isRoot) |
| 328 |
LockBuffer(buffer, GIN_UNLOCK); |
| 329 |
|
| 330 |
ReleaseBuffer(buffer); |
| 331 |
} |
| 332 |
|
| 333 |
if (isRoot) |
| 334 |
ReleaseBuffer(buffer); |
| 335 |
|
| 336 |
return meDelete; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
/* |
| 341 |
* Scan through posting tree leafs, delete empty tuples. Returns true if there |
| 342 |
* is at least one empty page. |
| 343 |
*/ |
| 344 |
static bool |
| 345 |
ginVacuumPostingTreeLeaves(GinVacuumState *gvs, BlockNumber blkno) |
| 346 |
{ |
| 347 |
Buffer buffer; |
| 348 |
Page page; |
| 349 |
bool hasVoidPage = false; |
| 350 |
MemoryContext oldCxt; |
| 351 |
|
| 352 |
/* Find leftmost leaf page of posting tree and lock it in exclusive mode */ |
| 353 |
while (true) |
| 354 |
{ |
| 355 |
PostingItem *pitem; |
| 356 |
|
| 357 |
buffer = ReadBufferExtended(gvs->index, MAIN_FORKNUM, blkno, |
| 358 |
RBM_NORMAL, gvs->strategy); |
| 359 |
LockBuffer(buffer, GIN_SHARE); |
| 360 |
page = BufferGetPage(buffer); |
| 361 |
|
| 362 |
Assert(GinPageIsData(page)); |
| 363 |
|
| 364 |
if (GinPageIsLeaf(page)) |
| 365 |
{ |
| 366 |
LockBuffer(buffer, GIN_UNLOCK); |
| 367 |
LockBuffer(buffer, GIN_EXCLUSIVE); |
| 368 |
break; |
| 369 |
} |
| 370 |
|
| 371 |
Assert(PageGetMaxOffsetNumber(page) >= FirstOffsetNumber); |
| 372 |
|
| 373 |
pitem = GinDataPageGetPostingItem(page, FirstOffsetNumber); |
| 374 |
blkno = PostingItemGetBlockNumber(pitem); |
| 375 |
Assert(blkno != InvalidBlockNumber); |
| 376 |
|
| 377 |
UnlockReleaseBuffer(buffer); |
| 378 |
} |
| 379 |
|
| 380 |
/* Iterate all posting tree leaves using rightlinks and vacuum them */ |
| 381 |
while (true) |
| 382 |
{ |
| 383 |
oldCxt = MemoryContextSwitchTo(gvs->tmpCxt); |
| 384 |
ginVacuumPostingTreeLeaf(gvs->index, buffer, gvs); |
| 385 |
MemoryContextSwitchTo(oldCxt); |
| 386 |
MemoryContextReset(gvs->tmpCxt); |
| 387 |
|
| 388 |
if (GinDataLeafPageIsEmpty(page)) |
| 389 |
hasVoidPage = true; |
| 390 |
|
| 391 |
blkno = GinPageGetOpaque(page)->rightlink; |
| 392 |
|
| 393 |
UnlockReleaseBuffer(buffer); |
| 394 |
|
| 395 |
if (blkno == InvalidBlockNumber) |
| 396 |
break; |
| 397 |
|
| 398 |
/* |
| 399 |
* A safe point to delay/accept interrupts: the previous page has been |
| 400 |
* unlocked and released, so we hold no buffer content lock (nor any |
| 401 |
* other LWLock) here and CHECK_FOR_INTERRUPTS() can do its job. |
| 402 |
*/ |
| 403 |
vacuum_delay_point(false); |
| 404 |
|
| 405 |
buffer = ReadBufferExtended(gvs->index, MAIN_FORKNUM, blkno, |
| 406 |
RBM_NORMAL, gvs->strategy); |
| 407 |
LockBuffer(buffer, GIN_EXCLUSIVE); |
| 408 |
page = BufferGetPage(buffer); |
| 409 |
} |
| 410 |
|
| 411 |
return hasVoidPage; |
| 412 |
} |
| 413 |
|
| 414 |
static void |
| 415 |
ginVacuumPostingTree(GinVacuumState *gvs, BlockNumber rootBlkno) |
| 416 |
{ |
| 417 |
if (ginVacuumPostingTreeLeaves(gvs, rootBlkno)) |
| 418 |
{ |
| 419 |
/* |
| 420 |
* There is at least one empty page. So we have to rescan the tree |
| 421 |
* deleting empty pages. |
| 422 |
*/ |
| 423 |
Buffer buffer; |
| 424 |
DataPageDeleteStack root, |
| 425 |
*ptr, |
| 426 |
*tmp; |
| 427 |
|
| 428 |
buffer = ReadBufferExtended(gvs->index, MAIN_FORKNUM, rootBlkno, |
| 429 |
RBM_NORMAL, gvs->strategy); |
| 430 |
|
| 431 |
/* |
| 432 |
* Lock posting tree root for cleanup to ensure there are no |
| 433 |
* concurrent inserts. |
| 434 |
*/ |
| 435 |
LockBufferForCleanup(buffer); |
| 436 |
|
| 437 |
memset(&root, 0, sizeof(DataPageDeleteStack)); |
| 438 |
root.leftBuffer = InvalidBuffer; |
| 439 |
root.isRoot = true; |
| 440 |
|
| 441 |
ginScanToDelete(gvs, rootBlkno, true, &root, InvalidOffsetNumber); |
| 442 |
|
| 443 |
ptr = root.child; |
| 444 |
|
| 445 |
while (ptr) |
| 446 |
{ |
| 447 |
tmp = ptr->child; |
| 448 |
pfree(ptr); |
| 449 |
ptr = tmp; |
| 450 |
} |
| 451 |
|
| 452 |
UnlockReleaseBuffer(buffer); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
/* |
| 457 |
* returns modified page or NULL if page isn't modified. |
| 458 |
* Function works with original page until first change is occurred, |
| 459 |
* then page is copied into temporary one. |
| 460 |
*/ |
| 461 |
static Page |
| 462 |
ginVacuumEntryPage(GinVacuumState *gvs, Buffer buffer, BlockNumber *roots, uint32 *nroot) |
| 463 |
{ |
| 464 |
Page origpage = BufferGetPage(buffer), |
| 465 |
tmppage; |
| 466 |
OffsetNumber i, |
| 467 |
maxoff = PageGetMaxOffsetNumber(origpage); |
| 468 |
|
| 469 |
tmppage = origpage; |
| 470 |
|
| 471 |
*nroot = 0; |
| 472 |
|
| 473 |
for (i = FirstOffsetNumber; i <= maxoff; i++) |
| 474 |
{ |
| 475 |
IndexTuple itup = (IndexTuple) PageGetItem(tmppage, PageGetItemId(tmppage, i)); |
| 476 |
|
| 477 |
if (GinIsPostingTree(itup)) |
| 478 |
{ |
| 479 |
/* |
| 480 |
* store posting tree's roots for further processing, we can't |
| 481 |
* vacuum it just now due to risk of deadlocks with scans/inserts |
| 482 |
*/ |
| 483 |
roots[*nroot] = GinGetDownlink(itup); |
| 484 |
(*nroot)++; |
| 485 |
} |
| 486 |
else if (GinGetNPosting(itup) > 0) |
| 487 |
{ |
| 488 |
int nitems; |
| 489 |
ItemPointer items_orig; |
| 490 |
bool free_items_orig; |
| 491 |
ItemPointer items; |
| 492 |
|
| 493 |
/* Get list of item pointers from the tuple. */ |
| 494 |
if (GinItupIsCompressed(itup)) |
| 495 |
{ |
| 496 |
items_orig = ginPostingListDecode((GinPostingList *) GinGetPosting(itup), &nitems); |
| 497 |
free_items_orig = true; |
| 498 |
} |
| 499 |
else |
| 500 |
{ |
| 501 |
items_orig = (ItemPointer) GinGetPosting(itup); |
| 502 |
nitems = GinGetNPosting(itup); |
| 503 |
free_items_orig = false; |
| 504 |
} |
| 505 |
|
| 506 |
/* Remove any items from the list that need to be vacuumed. */ |
| 507 |
items = ginVacuumItemPointers(gvs, items_orig, nitems, &nitems); |
| 508 |
|
| 509 |
if (free_items_orig) |
| 510 |
pfree(items_orig); |
| 511 |
|
| 512 |
/* If any item pointers were removed, recreate the tuple. */ |
| 513 |
if (items) |
| 514 |
{ |
| 515 |
OffsetNumber attnum; |
| 516 |
Datum key; |
| 517 |
GinNullCategory category; |
| 518 |
GinPostingList *plist; |
| 519 |
int plistsize; |
| 520 |
|
| 521 |
if (nitems > 0) |
| 522 |
{ |
| 523 |
plist = ginCompressPostingList(items, nitems, GinMaxItemSize, NULL); |
| 524 |
plistsize = SizeOfGinPostingList(plist); |
| 525 |
} |
| 526 |
else |
| 527 |
{ |
| 528 |
plist = NULL; |
| 529 |
plistsize = 0; |
| 530 |
} |
| 531 |
|
| 532 |
/* |
| 533 |
* if we already created a temporary page, make changes in |
| 534 |
* place |
| 535 |
*/ |
| 536 |
if (tmppage == origpage) |
| 537 |
{ |
| 538 |
/* |
| 539 |
* On first difference, create a temporary copy of the |
| 540 |
* page and copy the tuple's posting list to it. |
| 541 |
*/ |
| 542 |
tmppage = PageGetTempPageCopy(origpage); |
| 543 |
|
| 544 |
/* set itup pointer to new page */ |
| 545 |
itup = (IndexTuple) PageGetItem(tmppage, PageGetItemId(tmppage, i)); |
| 546 |
} |
| 547 |
|
| 548 |
attnum = gintuple_get_attrnum(&gvs->ginstate, itup); |
| 549 |
key = gintuple_get_key(&gvs->ginstate, itup, &category); |
| 550 |
itup = GinFormTuple(&gvs->ginstate, attnum, key, category, |
| 551 |
(char *) plist, plistsize, |
| 552 |
nitems, true); |
| 553 |
if (plist) |
| 554 |
pfree(plist); |
| 555 |
PageIndexTupleDelete(tmppage, i); |
| 556 |
|
| 557 |
if (PageAddItem(tmppage, (Item) itup, IndexTupleSize(itup), i, false, false) != i) |
| 558 |
elog(ERROR, "failed to add item to index page in \"%s\"", |
| 559 |
RelationGetRelationName(gvs->index)); |
| 560 |
|
| 561 |
pfree(itup); |
| 562 |
pfree(items); |
| 563 |
} |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
return (tmppage == origpage) ? NULL : tmppage; |
| 568 |
} |
| 569 |
|
| 570 |
IndexBulkDeleteResult * |
| 571 |
ginbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, |
| 572 |
IndexBulkDeleteCallback callback, void *callback_state) |
| 573 |
{ |
| 574 |
Relation index = info->index; |
| 575 |
BlockNumber blkno = GIN_ROOT_BLKNO; |
| 576 |
GinVacuumState gvs; |
| 577 |
Buffer buffer; |
| 578 |
BlockNumber rootOfPostingTree[BLCKSZ / (sizeof(IndexTupleData) + sizeof(ItemId))]; |
| 579 |
uint32 nRoot; |
| 580 |
|
| 581 |
gvs.tmpCxt = AllocSetContextCreate(CurrentMemoryContext, |
| 582 |
"Gin vacuum temporary context", |
| 583 |
ALLOCSET_DEFAULT_SIZES); |
| 584 |
gvs.index = index; |
| 585 |
gvs.callback = callback; |
| 586 |
gvs.callback_state = callback_state; |
| 587 |
gvs.strategy = info->strategy; |
| 588 |
initGinState(&gvs.ginstate, index); |
| 589 |
|
| 590 |
/* first time through? */ |
| 591 |
if (stats == NULL) |
| 592 |
{ |
| 593 |
/* Yes, so initialize stats to zeroes */ |
| 594 |
stats = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult)); |
| 595 |
|
| 596 |
/* |
| 597 |
* and cleanup any pending inserts |
| 598 |
*/ |
| 599 |
ginInsertCleanup(&gvs.ginstate, !AmAutoVacuumWorkerProcess(), |
| 600 |
false, true, stats); |
| 601 |
} |
| 602 |
|
| 603 |
/* we'll re-count the tuples each time */ |
| 604 |
stats->num_index_tuples = 0; |
| 605 |
gvs.result = stats; |
| 606 |
|
| 607 |
buffer = ReadBufferExtended(index, MAIN_FORKNUM, blkno, |
| 608 |
RBM_NORMAL, info->strategy); |
| 609 |
|
| 610 |
/* find leaf page */ |
| 611 |
for (;;) |
| 612 |
{ |
| 613 |
Page page = BufferGetPage(buffer); |
| 614 |
IndexTuple itup; |
| 615 |
|
| 616 |
LockBuffer(buffer, GIN_SHARE); |
| 617 |
|
| 618 |
Assert(!GinPageIsData(page)); |
| 619 |
|
| 620 |
if (GinPageIsLeaf(page)) |
| 621 |
{ |
| 622 |
LockBuffer(buffer, GIN_UNLOCK); |
| 623 |
LockBuffer(buffer, GIN_EXCLUSIVE); |
| 624 |
|
| 625 |
if (blkno == GIN_ROOT_BLKNO && !GinPageIsLeaf(page)) |
| 626 |
{ |
| 627 |
LockBuffer(buffer, GIN_UNLOCK); |
| 628 |
continue; /* check it one more */ |
| 629 |
} |
| 630 |
break; |
| 631 |
} |
| 632 |
|
| 633 |
Assert(PageGetMaxOffsetNumber(page) >= FirstOffsetNumber); |
| 634 |
|
| 635 |
itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, FirstOffsetNumber)); |
| 636 |
blkno = GinGetDownlink(itup); |
| 637 |
Assert(blkno != InvalidBlockNumber); |
| 638 |
|
| 639 |
UnlockReleaseBuffer(buffer); |
| 640 |
buffer = ReadBufferExtended(index, MAIN_FORKNUM, blkno, |
| 641 |
RBM_NORMAL, info->strategy); |
| 642 |
} |
| 643 |
|
| 644 |
/* right now we found leftmost page in entry's BTree */ |
| 645 |
|
| 646 |
for (;;) |
| 647 |
{ |
| 648 |
Page page = BufferGetPage(buffer); |
| 649 |
Page resPage; |
| 650 |
uint32 i; |
| 651 |
|
| 652 |
Assert(!GinPageIsData(page)); |
| 653 |
|
| 654 |
resPage = ginVacuumEntryPage(&gvs, buffer, rootOfPostingTree, &nRoot); |
| 655 |
|
| 656 |
blkno = GinPageGetOpaque(page)->rightlink; |
| 657 |
|
| 658 |
if (resPage) |
| 659 |
{ |
| 660 |
START_CRIT_SECTION(); |
| 661 |
PageRestoreTempPage(resPage, page); |
| 662 |
MarkBufferDirty(buffer); |
| 663 |
xlogVacuumPage(gvs.index, buffer); |
| 664 |
UnlockReleaseBuffer(buffer); |
| 665 |
END_CRIT_SECTION(); |
| 666 |
} |
| 667 |
else |
| 668 |
{ |
| 669 |
UnlockReleaseBuffer(buffer); |
| 670 |
} |
| 671 |
|
| 672 |
vacuum_delay_point(false); |
| 673 |
|
| 674 |
for (i = 0; i < nRoot; i++) |
| 675 |
{ |
| 676 |
ginVacuumPostingTree(&gvs, rootOfPostingTree[i]); |
| 677 |
vacuum_delay_point(false); |
| 678 |
} |
| 679 |
|
| 680 |
if (blkno == InvalidBlockNumber) /* rightmost page */ |
| 681 |
break; |
| 682 |
|
| 683 |
buffer = ReadBufferExtended(index, MAIN_FORKNUM, blkno, |
| 684 |
RBM_NORMAL, info->strategy); |
| 685 |
LockBuffer(buffer, GIN_EXCLUSIVE); |
| 686 |
} |
| 687 |
|
| 688 |
MemoryContextDelete(gvs.tmpCxt); |
| 689 |
|
| 690 |
return gvs.result; |
| 691 |
} |
| 692 |
|
| 693 |
IndexBulkDeleteResult * |
| 694 |
ginvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats) |
| 695 |
{ |
| 696 |
Relation index = info->index; |
| 697 |
bool needLock; |
| 698 |
BlockNumber npages, |
| 699 |
blkno; |
| 700 |
BlockNumber totFreePages; |
| 701 |
GinState ginstate; |
| 702 |
GinStatsData idxStat; |
| 703 |
|
| 704 |
/* |
| 705 |
* In an autovacuum analyze, we want to clean up pending insertions. |
| 706 |
* Otherwise, an ANALYZE-only call is a no-op. |
| 707 |
*/ |
| 708 |
if (info->analyze_only) |
| 709 |
{ |
| 710 |
if (AmAutoVacuumWorkerProcess()) |
| 711 |
{ |
| 712 |
initGinState(&ginstate, index); |
| 713 |
ginInsertCleanup(&ginstate, false, true, true, stats); |
| 714 |
} |
| 715 |
return stats; |
| 716 |
} |
| 717 |
|
| 718 |
/* |
| 719 |
* Set up all-zero stats and cleanup pending inserts if ginbulkdelete |
| 720 |
* wasn't called |
| 721 |
*/ |
| 722 |
if (stats == NULL) |
| 723 |
{ |
| 724 |
stats = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult)); |
| 725 |
initGinState(&ginstate, index); |
| 726 |
ginInsertCleanup(&ginstate, !AmAutoVacuumWorkerProcess(), |
| 727 |
false, true, stats); |
| 728 |
} |
| 729 |
|
| 730 |
memset(&idxStat, 0, sizeof(idxStat)); |
| 731 |
|
| 732 |
/* |
| 733 |
* XXX we always report the heap tuple count as the number of index |
| 734 |
* entries. This is bogus if the index is partial, but it's real hard to |
| 735 |
* tell how many distinct heap entries are referenced by a GIN index. |
| 736 |
*/ |
| 737 |
stats->num_index_tuples = Max(info->num_heap_tuples, 0); |
| 738 |
stats->estimated_count = info->estimated_count; |
| 739 |
|
| 740 |
/* |
| 741 |
* Need lock unless it's local to this backend. |
| 742 |
*/ |
| 743 |
needLock = !RELATION_IS_LOCAL(index); |
| 744 |
|
| 745 |
if (needLock) |
| 746 |
LockRelationForExtension(index, ExclusiveLock); |
| 747 |
npages = RelationGetNumberOfBlocks(index); |
| 748 |
if (needLock) |
| 749 |
UnlockRelationForExtension(index, ExclusiveLock); |
| 750 |
|
| 751 |
totFreePages = 0; |
| 752 |
|
| 753 |
for (blkno = GIN_ROOT_BLKNO; blkno < npages; blkno++) |
| 754 |
{ |
| 755 |
Buffer buffer; |
| 756 |
Page page; |
| 757 |
|
| 758 |
vacuum_delay_point(false); |
| 759 |
|
| 760 |
buffer = ReadBufferExtended(index, MAIN_FORKNUM, blkno, |
| 761 |
RBM_NORMAL, info->strategy); |
| 762 |
LockBuffer(buffer, GIN_SHARE); |
| 763 |
page = (Page) BufferGetPage(buffer); |
| 764 |
|
| 765 |
if (GinPageIsRecyclable(page)) |
| 766 |
{ |
| 767 |
Assert(blkno != GIN_ROOT_BLKNO); |
| 768 |
RecordFreeIndexPage(index, blkno); |
| 769 |
totFreePages++; |
| 770 |
} |
| 771 |
else if (GinPageIsData(page)) |
| 772 |
{ |
| 773 |
idxStat.nDataPages++; |
| 774 |
} |
| 775 |
else if (!GinPageIsList(page)) |
| 776 |
{ |
| 777 |
idxStat.nEntryPages++; |
| 778 |
|
| 779 |
if (GinPageIsLeaf(page)) |
| 780 |
idxStat.nEntries += PageGetMaxOffsetNumber(page); |
| 781 |
} |
| 782 |
|
| 783 |
UnlockReleaseBuffer(buffer); |
| 784 |
} |
| 785 |
|
| 786 |
/* Update the metapage with accurate page and entry counts */ |
| 787 |
idxStat.nTotalPages = npages; |
| 788 |
ginUpdateStats(info->index, &idxStat, false); |
| 789 |
|
| 790 |
/* Finally, vacuum the FSM */ |
| 791 |
IndexFreeSpaceMapVacuum(info->index); |
| 792 |
|
| 793 |
stats->pages_free = totFreePages; |
| 794 |
|
| 795 |
if (needLock) |
| 796 |
LockRelationForExtension(index, ExclusiveLock); |
| 797 |
stats->num_pages = RelationGetNumberOfBlocks(index); |
| 798 |
if (needLock) |
| 799 |
UnlockRelationForExtension(index, ExclusiveLock); |
| 800 |
|
| 801 |
return stats; |
| 802 |
} |
| 803 |
|
| 804 |
/* |
| 805 |
* Return whether Page can safely be recycled. |
| 806 |
*/ |
| 807 |
bool |
| 808 |
GinPageIsRecyclable(Page page) |
| 809 |
{ |
| 810 |
TransactionId delete_xid; |
| 811 |
|
| 812 |
if (PageIsNew(page)) |
| 813 |
return true; |
| 814 |
|
| 815 |
if (!GinPageIsDeleted(page)) |
| 816 |
return false; |
| 817 |
|
| 818 |
delete_xid = GinPageGetDeleteXid(page); |
| 819 |
|
| 820 |
if (!TransactionIdIsValid(delete_xid)) |
| 821 |
return true; |
| 822 |
|
| 823 |
/* |
| 824 |
* If no backend still could view delete_xid as in running, all scans |
| 825 |
* concurrent with ginDeletePage() must have finished. |
| 826 |
*/ |
| 827 |
return GlobalVisCheckRemovableXid(NULL, delete_xid); |
| 828 |
} |