PostgreSQL Source Code  git master
bufpage.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * bufpage.c
4  * POSTGRES standard buffer page code.
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/storage/page/bufpage.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include "access/htup_details.h"
18 #include "access/itup.h"
19 #include "access/xlog.h"
20 #include "storage/checksum.h"
21 #include "utils/memdebug.h"
22 #include "utils/memutils.h"
23 
24 
25 /* GUC variable */
27 
28 
29 /* ----------------------------------------------------------------
30  * Page support functions
31  * ----------------------------------------------------------------
32  */
33 
34 /*
35  * PageInit
36  * Initializes the contents of a page.
37  * Note that we don't calculate an initial checksum here; that's not done
38  * until it's time to write.
39  */
40 void
41 PageInit(Page page, Size pageSize, Size specialSize)
42 {
43  PageHeader p = (PageHeader) page;
44 
45  specialSize = MAXALIGN(specialSize);
46 
47  Assert(pageSize == BLCKSZ);
48  Assert(pageSize > specialSize + SizeOfPageHeaderData);
49 
50  /* Make sure all fields of page are zero, as well as unused space */
51  MemSet(p, 0, pageSize);
52 
53  p->pd_flags = 0;
55  p->pd_upper = pageSize - specialSize;
56  p->pd_special = pageSize - specialSize;
58  /* p->pd_prune_xid = InvalidTransactionId; done by above MemSet */
59 }
60 
61 
62 /*
63  * PageIsVerified
64  * Check that the page header and checksum (if any) appear valid.
65  *
66  * This is called when a page has just been read in from disk. The idea is
67  * to cheaply detect trashed pages before we go nuts following bogus item
68  * pointers, testing invalid transaction identifiers, etc.
69  *
70  * It turns out to be necessary to allow zeroed pages here too. Even though
71  * this routine is *not* called when deliberately adding a page to a relation,
72  * there are scenarios in which a zeroed page might be found in a table.
73  * (Example: a backend extends a relation, then crashes before it can write
74  * any WAL entry about the new page. The kernel will already have the
75  * zeroed page in the file, and it will stay that way after restart.) So we
76  * allow zeroed pages here, and are careful that the page access macros
77  * treat such a page as empty and without free space. Eventually, VACUUM
78  * will clean up such a page and make it usable.
79  */
80 bool
82 {
83  PageHeader p = (PageHeader) page;
84  size_t *pagebytes;
85  int i;
86  bool checksum_failure = false;
87  bool header_sane = false;
88  bool all_zeroes = false;
89  uint16 checksum = 0;
90 
91  /*
92  * Don't verify page data unless the page passes basic non-zero test
93  */
94  if (!PageIsNew(page))
95  {
97  {
98  checksum = pg_checksum_page((char *) page, blkno);
99 
100  if (checksum != p->pd_checksum)
101  checksum_failure = true;
102  }
103 
104  /*
105  * The following checks don't prove the header is correct, only that
106  * it looks sane enough to allow into the buffer pool. Later usage of
107  * the block can still reveal problems, which is why we offer the
108  * checksum option.
109  */
110  if ((p->pd_flags & ~PD_VALID_FLAG_BITS) == 0 &&
111  p->pd_lower <= p->pd_upper &&
112  p->pd_upper <= p->pd_special &&
113  p->pd_special <= BLCKSZ &&
114  p->pd_special == MAXALIGN(p->pd_special))
115  header_sane = true;
116 
117  if (header_sane && !checksum_failure)
118  return true;
119  }
120 
121  /*
122  * Check all-zeroes case. Luckily BLCKSZ is guaranteed to always be a
123  * multiple of size_t - and it's much faster to compare memory using the
124  * native word size.
125  */
126  StaticAssertStmt(BLCKSZ == (BLCKSZ / sizeof(size_t)) * sizeof(size_t),
127  "BLCKSZ has to be a multiple of sizeof(size_t)");
128 
129  all_zeroes = true;
130  pagebytes = (size_t *) page;
131  for (i = 0; i < (BLCKSZ / sizeof(size_t)); i++)
132  {
133  if (pagebytes[i] != 0)
134  {
135  all_zeroes = false;
136  break;
137  }
138  }
139 
140  if (all_zeroes)
141  return true;
142 
143  /*
144  * Throw a WARNING if the checksum fails, but only after we've checked for
145  * the all-zeroes case.
146  */
147  if (checksum_failure)
148  {
150  (ERRCODE_DATA_CORRUPTED,
151  errmsg("page verification failed, calculated checksum %u but expected %u",
152  checksum, p->pd_checksum)));
153 
154  if (header_sane && ignore_checksum_failure)
155  return true;
156  }
157 
158  return false;
159 }
160 
161 
162 /*
163  * PageAddItemExtended
164  *
165  * Add an item to a page. Return value is the offset at which it was
166  * inserted, or InvalidOffsetNumber if the item is not inserted for any
167  * reason. A WARNING is issued indicating the reason for the refusal.
168  *
169  * offsetNumber must be either InvalidOffsetNumber to specify finding a
170  * free item pointer, or a value between FirstOffsetNumber and one past
171  * the last existing item, to specify using that particular item pointer.
172  *
173  * If offsetNumber is valid and flag PAI_OVERWRITE is set, we just store
174  * the item at the specified offsetNumber, which must be either a
175  * currently-unused item pointer, or one past the last existing item.
176  *
177  * If offsetNumber is valid and flag PAI_OVERWRITE is not set, insert
178  * the item at the specified offsetNumber, moving existing items later
179  * in the array to make room.
180  *
181  * If offsetNumber is not valid, then assign a slot by finding the first
182  * one that is both unused and deallocated.
183  *
184  * If flag PAI_IS_HEAP is set, we enforce that there can't be more than
185  * MaxHeapTuplesPerPage line pointers on the page.
186  *
187  * !!! EREPORT(ERROR) IS DISALLOWED HERE !!!
188  */
191  Item item,
192  Size size,
193  OffsetNumber offsetNumber,
194  int flags)
195 {
196  PageHeader phdr = (PageHeader) page;
197  Size alignedSize;
198  int lower;
199  int upper;
200  ItemId itemId;
201  OffsetNumber limit;
202  bool needshuffle = false;
203 
204  /*
205  * Be wary about corrupted page pointers
206  */
207  if (phdr->pd_lower < SizeOfPageHeaderData ||
208  phdr->pd_lower > phdr->pd_upper ||
209  phdr->pd_upper > phdr->pd_special ||
210  phdr->pd_special > BLCKSZ)
211  ereport(PANIC,
212  (errcode(ERRCODE_DATA_CORRUPTED),
213  errmsg("corrupted page pointers: lower = %u, upper = %u, special = %u",
214  phdr->pd_lower, phdr->pd_upper, phdr->pd_special)));
215 
216  /*
217  * Select offsetNumber to place the new item at
218  */
220 
221  /* was offsetNumber passed in? */
222  if (OffsetNumberIsValid(offsetNumber))
223  {
224  /* yes, check it */
225  if ((flags & PAI_OVERWRITE) != 0)
226  {
227  if (offsetNumber < limit)
228  {
229  itemId = PageGetItemId(phdr, offsetNumber);
230  if (ItemIdIsUsed(itemId) || ItemIdHasStorage(itemId))
231  {
232  elog(WARNING, "will not overwrite a used ItemId");
233  return InvalidOffsetNumber;
234  }
235  }
236  }
237  else
238  {
239  if (offsetNumber < limit)
240  needshuffle = true; /* need to move existing linp's */
241  }
242  }
243  else
244  {
245  /* offsetNumber was not passed in, so find a free slot */
246  /* if no free slot, we'll put it at limit (1st open slot) */
247  if (PageHasFreeLinePointers(phdr))
248  {
249  /*
250  * Look for "recyclable" (unused) ItemId. We check for no storage
251  * as well, just to be paranoid --- unused items should never have
252  * storage.
253  */
254  for (offsetNumber = 1; offsetNumber < limit; offsetNumber++)
255  {
256  itemId = PageGetItemId(phdr, offsetNumber);
257  if (!ItemIdIsUsed(itemId) && !ItemIdHasStorage(itemId))
258  break;
259  }
260  if (offsetNumber >= limit)
261  {
262  /* the hint is wrong, so reset it */
264  }
265  }
266  else
267  {
268  /* don't bother searching if hint says there's no free slot */
269  offsetNumber = limit;
270  }
271  }
272 
273  /* Reject placing items beyond the first unused line pointer */
274  if (offsetNumber > limit)
275  {
276  elog(WARNING, "specified item offset is too large");
277  return InvalidOffsetNumber;
278  }
279 
280  /* Reject placing items beyond heap boundary, if heap */
281  if ((flags & PAI_IS_HEAP) != 0 && offsetNumber > MaxHeapTuplesPerPage)
282  {
283  elog(WARNING, "can't put more than MaxHeapTuplesPerPage items in a heap page");
284  return InvalidOffsetNumber;
285  }
286 
287  /*
288  * Compute new lower and upper pointers for page, see if it'll fit.
289  *
290  * Note: do arithmetic as signed ints, to avoid mistakes if, say,
291  * alignedSize > pd_upper.
292  */
293  if (offsetNumber == limit || needshuffle)
294  lower = phdr->pd_lower + sizeof(ItemIdData);
295  else
296  lower = phdr->pd_lower;
297 
298  alignedSize = MAXALIGN(size);
299 
300  upper = (int) phdr->pd_upper - (int) alignedSize;
301 
302  if (lower > upper)
303  return InvalidOffsetNumber;
304 
305  /*
306  * OK to insert the item. First, shuffle the existing pointers if needed.
307  */
308  itemId = PageGetItemId(phdr, offsetNumber);
309 
310  if (needshuffle)
311  memmove(itemId + 1, itemId,
312  (limit - offsetNumber) * sizeof(ItemIdData));
313 
314  /* set the item pointer */
315  ItemIdSetNormal(itemId, upper, size);
316 
317  /*
318  * Items normally contain no uninitialized bytes. Core bufpage consumers
319  * conform, but this is not a necessary coding rule; a new index AM could
320  * opt to depart from it. However, data type input functions and other
321  * C-language functions that synthesize datums should initialize all
322  * bytes; datumIsEqual() relies on this. Testing here, along with the
323  * similar check in printtup(), helps to catch such mistakes.
324  *
325  * Values of the "name" type retrieved via index-only scans may contain
326  * uninitialized bytes; see comment in btrescan(). Valgrind will report
327  * this as an error, but it is safe to ignore.
328  */
329  VALGRIND_CHECK_MEM_IS_DEFINED(item, size);
330 
331  /* copy the item's data onto the page */
332  memcpy((char *) page + upper, item, size);
333 
334  /* adjust page header */
335  phdr->pd_lower = (LocationIndex) lower;
336  phdr->pd_upper = (LocationIndex) upper;
337 
338  return offsetNumber;
339 }
340 
341 
342 /*
343  * PageGetTempPage
344  * Get a temporary page in local memory for special processing.
345  * The returned page is not initialized at all; caller must do that.
346  */
347 Page
349 {
350  Size pageSize;
351  Page temp;
352 
353  pageSize = PageGetPageSize(page);
354  temp = (Page) palloc(pageSize);
355 
356  return temp;
357 }
358 
359 /*
360  * PageGetTempPageCopy
361  * Get a temporary page in local memory for special processing.
362  * The page is initialized by copying the contents of the given page.
363  */
364 Page
366 {
367  Size pageSize;
368  Page temp;
369 
370  pageSize = PageGetPageSize(page);
371  temp = (Page) palloc(pageSize);
372 
373  memcpy(temp, page, pageSize);
374 
375  return temp;
376 }
377 
378 /*
379  * PageGetTempPageCopySpecial
380  * Get a temporary page in local memory for special processing.
381  * The page is PageInit'd with the same special-space size as the
382  * given page, and the special space is copied from the given page.
383  */
384 Page
386 {
387  Size pageSize;
388  Page temp;
389 
390  pageSize = PageGetPageSize(page);
391  temp = (Page) palloc(pageSize);
392 
393  PageInit(temp, pageSize, PageGetSpecialSize(page));
394  memcpy(PageGetSpecialPointer(temp),
395  PageGetSpecialPointer(page),
396  PageGetSpecialSize(page));
397 
398  return temp;
399 }
400 
401 /*
402  * PageRestoreTempPage
403  * Copy temporary page back to permanent page after special processing
404  * and release the temporary page.
405  */
406 void
407 PageRestoreTempPage(Page tempPage, Page oldPage)
408 {
409  Size pageSize;
410 
411  pageSize = PageGetPageSize(tempPage);
412  memcpy((char *) oldPage, (char *) tempPage, pageSize);
413 
414  pfree(tempPage);
415 }
416 
417 /*
418  * sorting support for PageRepairFragmentation and PageIndexMultiDelete
419  */
420 typedef struct itemIdSortData
421 {
422  uint16 offsetindex; /* linp array index */
423  int16 itemoff; /* page offset of item data */
424  uint16 alignedlen; /* MAXALIGN(item data len) */
427 
428 static int
429 itemoffcompare(const void *itemidp1, const void *itemidp2)
430 {
431  /* Sort in decreasing itemoff order */
432  return ((itemIdSort) itemidp2)->itemoff -
433  ((itemIdSort) itemidp1)->itemoff;
434 }
435 
436 /*
437  * After removing or marking some line pointers unused, move the tuples to
438  * remove the gaps caused by the removed items.
439  */
440 static void
441 compactify_tuples(itemIdSort itemidbase, int nitems, Page page)
442 {
443  PageHeader phdr = (PageHeader) page;
444  Offset upper;
445  int i;
446 
447  /* sort itemIdSortData array into decreasing itemoff order */
448  qsort((char *) itemidbase, nitems, sizeof(itemIdSortData),
450 
451  upper = phdr->pd_special;
452  for (i = 0; i < nitems; i++)
453  {
454  itemIdSort itemidptr = &itemidbase[i];
455  ItemId lp;
456 
457  lp = PageGetItemId(page, itemidptr->offsetindex + 1);
458  upper -= itemidptr->alignedlen;
459  memmove((char *) page + upper,
460  (char *) page + itemidptr->itemoff,
461  itemidptr->alignedlen);
462  lp->lp_off = upper;
463  }
464 
465  phdr->pd_upper = upper;
466 }
467 
468 /*
469  * PageRepairFragmentation
470  *
471  * Frees fragmented space on a page.
472  * It doesn't remove unused line pointers! Please don't change this.
473  *
474  * This routine is usable for heap pages only, but see PageIndexMultiDelete.
475  *
476  * As a side effect, the page's PD_HAS_FREE_LINES hint bit is updated.
477  */
478 void
480 {
481  Offset pd_lower = ((PageHeader) page)->pd_lower;
482  Offset pd_upper = ((PageHeader) page)->pd_upper;
483  Offset pd_special = ((PageHeader) page)->pd_special;
485  itemIdSort itemidptr;
486  ItemId lp;
487  int nline,
488  nstorage,
489  nunused;
490  int i;
491  Size totallen;
492 
493  /*
494  * It's worth the trouble to be more paranoid here than in most places,
495  * because we are about to reshuffle data in (what is usually) a shared
496  * disk buffer. If we aren't careful then corrupted pointers, lengths,
497  * etc could cause us to clobber adjacent disk buffers, spreading the data
498  * loss further. So, check everything.
499  */
500  if (pd_lower < SizeOfPageHeaderData ||
501  pd_lower > pd_upper ||
502  pd_upper > pd_special ||
503  pd_special > BLCKSZ ||
504  pd_special != MAXALIGN(pd_special))
505  ereport(ERROR,
506  (errcode(ERRCODE_DATA_CORRUPTED),
507  errmsg("corrupted page pointers: lower = %u, upper = %u, special = %u",
508  pd_lower, pd_upper, pd_special)));
509 
510  /*
511  * Run through the line pointer array and collect data about live items.
512  */
513  nline = PageGetMaxOffsetNumber(page);
514  itemidptr = itemidbase;
515  nunused = totallen = 0;
516  for (i = FirstOffsetNumber; i <= nline; i++)
517  {
518  lp = PageGetItemId(page, i);
519  if (ItemIdIsUsed(lp))
520  {
521  if (ItemIdHasStorage(lp))
522  {
523  itemidptr->offsetindex = i - 1;
524  itemidptr->itemoff = ItemIdGetOffset(lp);
525  if (unlikely(itemidptr->itemoff < (int) pd_upper ||
526  itemidptr->itemoff >= (int) pd_special))
527  ereport(ERROR,
528  (errcode(ERRCODE_DATA_CORRUPTED),
529  errmsg("corrupted item pointer: %u",
530  itemidptr->itemoff)));
531  itemidptr->alignedlen = MAXALIGN(ItemIdGetLength(lp));
532  totallen += itemidptr->alignedlen;
533  itemidptr++;
534  }
535  }
536  else
537  {
538  /* Unused entries should have lp_len = 0, but make sure */
539  ItemIdSetUnused(lp);
540  nunused++;
541  }
542  }
543 
544  nstorage = itemidptr - itemidbase;
545  if (nstorage == 0)
546  {
547  /* Page is completely empty, so just reset it quickly */
548  ((PageHeader) page)->pd_upper = pd_special;
549  }
550  else
551  {
552  /* Need to compact the page the hard way */
553  if (totallen > (Size) (pd_special - pd_lower))
554  ereport(ERROR,
555  (errcode(ERRCODE_DATA_CORRUPTED),
556  errmsg("corrupted item lengths: total %u, available space %u",
557  (unsigned int) totallen, pd_special - pd_lower)));
558 
559  compactify_tuples(itemidbase, nstorage, page);
560  }
561 
562  /* Set hint bit for PageAddItem */
563  if (nunused > 0)
565  else
567 }
568 
569 /*
570  * PageGetFreeSpace
571  * Returns the size of the free (allocatable) space on a page,
572  * reduced by the space needed for a new line pointer.
573  *
574  * Note: this should usually only be used on index pages. Use
575  * PageGetHeapFreeSpace on heap pages.
576  */
577 Size
579 {
580  int space;
581 
582  /*
583  * Use signed arithmetic here so that we behave sensibly if pd_lower >
584  * pd_upper.
585  */
586  space = (int) ((PageHeader) page)->pd_upper -
587  (int) ((PageHeader) page)->pd_lower;
588 
589  if (space < (int) sizeof(ItemIdData))
590  return 0;
591  space -= sizeof(ItemIdData);
592 
593  return (Size) space;
594 }
595 
596 /*
597  * PageGetFreeSpaceForMultipleTuples
598  * Returns the size of the free (allocatable) space on a page,
599  * reduced by the space needed for multiple new line pointers.
600  *
601  * Note: this should usually only be used on index pages. Use
602  * PageGetHeapFreeSpace on heap pages.
603  */
604 Size
606 {
607  int space;
608 
609  /*
610  * Use signed arithmetic here so that we behave sensibly if pd_lower >
611  * pd_upper.
612  */
613  space = (int) ((PageHeader) page)->pd_upper -
614  (int) ((PageHeader) page)->pd_lower;
615 
616  if (space < (int) (ntups * sizeof(ItemIdData)))
617  return 0;
618  space -= ntups * sizeof(ItemIdData);
619 
620  return (Size) space;
621 }
622 
623 /*
624  * PageGetExactFreeSpace
625  * Returns the size of the free (allocatable) space on a page,
626  * without any consideration for adding/removing line pointers.
627  */
628 Size
630 {
631  int space;
632 
633  /*
634  * Use signed arithmetic here so that we behave sensibly if pd_lower >
635  * pd_upper.
636  */
637  space = (int) ((PageHeader) page)->pd_upper -
638  (int) ((PageHeader) page)->pd_lower;
639 
640  if (space < 0)
641  return 0;
642 
643  return (Size) space;
644 }
645 
646 
647 /*
648  * PageGetHeapFreeSpace
649  * Returns the size of the free (allocatable) space on a page,
650  * reduced by the space needed for a new line pointer.
651  *
652  * The difference between this and PageGetFreeSpace is that this will return
653  * zero if there are already MaxHeapTuplesPerPage line pointers in the page
654  * and none are free. We use this to enforce that no more than
655  * MaxHeapTuplesPerPage line pointers are created on a heap page. (Although
656  * no more tuples than that could fit anyway, in the presence of redirected
657  * or dead line pointers it'd be possible to have too many line pointers.
658  * To avoid breaking code that assumes MaxHeapTuplesPerPage is a hard limit
659  * on the number of line pointers, we make this extra check.)
660  */
661 Size
663 {
664  Size space;
665 
666  space = PageGetFreeSpace(page);
667  if (space > 0)
668  {
669  OffsetNumber offnum,
670  nline;
671 
672  /*
673  * Are there already MaxHeapTuplesPerPage line pointers in the page?
674  */
675  nline = PageGetMaxOffsetNumber(page);
676  if (nline >= MaxHeapTuplesPerPage)
677  {
679  {
680  /*
681  * Since this is just a hint, we must confirm that there is
682  * indeed a free line pointer
683  */
684  for (offnum = FirstOffsetNumber; offnum <= nline; offnum = OffsetNumberNext(offnum))
685  {
686  ItemId lp = PageGetItemId(page, offnum);
687 
688  if (!ItemIdIsUsed(lp))
689  break;
690  }
691 
692  if (offnum > nline)
693  {
694  /*
695  * The hint is wrong, but we can't clear it here since we
696  * don't have the ability to mark the page dirty.
697  */
698  space = 0;
699  }
700  }
701  else
702  {
703  /*
704  * Although the hint might be wrong, PageAddItem will believe
705  * it anyway, so we must believe it too.
706  */
707  space = 0;
708  }
709  }
710  }
711  return space;
712 }
713 
714 
715 /*
716  * PageIndexTupleDelete
717  *
718  * This routine does the work of removing a tuple from an index page.
719  *
720  * Unlike heap pages, we compact out the line pointer for the removed tuple.
721  */
722 void
724 {
725  PageHeader phdr = (PageHeader) page;
726  char *addr;
727  ItemId tup;
728  Size size;
729  unsigned offset;
730  int nbytes;
731  int offidx;
732  int nline;
733 
734  /*
735  * As with PageRepairFragmentation, paranoia seems justified.
736  */
737  if (phdr->pd_lower < SizeOfPageHeaderData ||
738  phdr->pd_lower > phdr->pd_upper ||
739  phdr->pd_upper > phdr->pd_special ||
740  phdr->pd_special > BLCKSZ ||
741  phdr->pd_special != MAXALIGN(phdr->pd_special))
742  ereport(ERROR,
743  (errcode(ERRCODE_DATA_CORRUPTED),
744  errmsg("corrupted page pointers: lower = %u, upper = %u, special = %u",
745  phdr->pd_lower, phdr->pd_upper, phdr->pd_special)));
746 
747  nline = PageGetMaxOffsetNumber(page);
748  if ((int) offnum <= 0 || (int) offnum > nline)
749  elog(ERROR, "invalid index offnum: %u", offnum);
750 
751  /* change offset number to offset index */
752  offidx = offnum - 1;
753 
754  tup = PageGetItemId(page, offnum);
755  Assert(ItemIdHasStorage(tup));
756  size = ItemIdGetLength(tup);
757  offset = ItemIdGetOffset(tup);
758 
759  if (offset < phdr->pd_upper || (offset + size) > phdr->pd_special ||
760  offset != MAXALIGN(offset))
761  ereport(ERROR,
762  (errcode(ERRCODE_DATA_CORRUPTED),
763  errmsg("corrupted item pointer: offset = %u, size = %u",
764  offset, (unsigned int) size)));
765 
766  /* Amount of space to actually be deleted */
767  size = MAXALIGN(size);
768 
769  /*
770  * First, we want to get rid of the pd_linp entry for the index tuple. We
771  * copy all subsequent linp's back one slot in the array. We don't use
772  * PageGetItemId, because we are manipulating the _array_, not individual
773  * linp's.
774  */
775  nbytes = phdr->pd_lower -
776  ((char *) &phdr->pd_linp[offidx + 1] - (char *) phdr);
777 
778  if (nbytes > 0)
779  memmove((char *) &(phdr->pd_linp[offidx]),
780  (char *) &(phdr->pd_linp[offidx + 1]),
781  nbytes);
782 
783  /*
784  * Now move everything between the old upper bound (beginning of tuple
785  * space) and the beginning of the deleted tuple forward, so that space in
786  * the middle of the page is left free. If we've just deleted the tuple
787  * at the beginning of tuple space, then there's no need to do the copy.
788  */
789 
790  /* beginning of tuple space */
791  addr = (char *) page + phdr->pd_upper;
792 
793  if (offset > phdr->pd_upper)
794  memmove(addr + size, addr, offset - phdr->pd_upper);
795 
796  /* adjust free space boundary pointers */
797  phdr->pd_upper += size;
798  phdr->pd_lower -= sizeof(ItemIdData);
799 
800  /*
801  * Finally, we need to adjust the linp entries that remain.
802  *
803  * Anything that used to be before the deleted tuple's data was moved
804  * forward by the size of the deleted tuple.
805  */
806  if (!PageIsEmpty(page))
807  {
808  int i;
809 
810  nline--; /* there's one less than when we started */
811  for (i = 1; i <= nline; i++)
812  {
813  ItemId ii = PageGetItemId(phdr, i);
814 
816  if (ItemIdGetOffset(ii) <= offset)
817  ii->lp_off += size;
818  }
819  }
820 }
821 
822 
823 /*
824  * PageIndexMultiDelete
825  *
826  * This routine handles the case of deleting multiple tuples from an
827  * index page at once. It is considerably faster than a loop around
828  * PageIndexTupleDelete ... however, the caller *must* supply the array
829  * of item numbers to be deleted in item number order!
830  */
831 void
832 PageIndexMultiDelete(Page page, OffsetNumber *itemnos, int nitems)
833 {
834  PageHeader phdr = (PageHeader) page;
835  Offset pd_lower = phdr->pd_lower;
836  Offset pd_upper = phdr->pd_upper;
837  Offset pd_special = phdr->pd_special;
839  ItemIdData newitemids[MaxIndexTuplesPerPage];
840  itemIdSort itemidptr;
841  ItemId lp;
842  int nline,
843  nused;
844  Size totallen;
845  Size size;
846  unsigned offset;
847  int nextitm;
848  OffsetNumber offnum;
849 
850  Assert(nitems <= MaxIndexTuplesPerPage);
851 
852  /*
853  * If there aren't very many items to delete, then retail
854  * PageIndexTupleDelete is the best way. Delete the items in reverse
855  * order so we don't have to think about adjusting item numbers for
856  * previous deletions.
857  *
858  * TODO: tune the magic number here
859  */
860  if (nitems <= 2)
861  {
862  while (--nitems >= 0)
863  PageIndexTupleDelete(page, itemnos[nitems]);
864  return;
865  }
866 
867  /*
868  * As with PageRepairFragmentation, paranoia seems justified.
869  */
870  if (pd_lower < SizeOfPageHeaderData ||
871  pd_lower > pd_upper ||
872  pd_upper > pd_special ||
873  pd_special > BLCKSZ ||
874  pd_special != MAXALIGN(pd_special))
875  ereport(ERROR,
876  (errcode(ERRCODE_DATA_CORRUPTED),
877  errmsg("corrupted page pointers: lower = %u, upper = %u, special = %u",
878  pd_lower, pd_upper, pd_special)));
879 
880  /*
881  * Scan the item pointer array and build a list of just the ones we are
882  * going to keep. Notice we do not modify the page yet, since we are
883  * still validity-checking.
884  */
885  nline = PageGetMaxOffsetNumber(page);
886  itemidptr = itemidbase;
887  totallen = 0;
888  nused = 0;
889  nextitm = 0;
890  for (offnum = FirstOffsetNumber; offnum <= nline; offnum = OffsetNumberNext(offnum))
891  {
892  lp = PageGetItemId(page, offnum);
894  size = ItemIdGetLength(lp);
895  offset = ItemIdGetOffset(lp);
896  if (offset < pd_upper ||
897  (offset + size) > pd_special ||
898  offset != MAXALIGN(offset))
899  ereport(ERROR,
900  (errcode(ERRCODE_DATA_CORRUPTED),
901  errmsg("corrupted item pointer: offset = %u, length = %u",
902  offset, (unsigned int) size)));
903 
904  if (nextitm < nitems && offnum == itemnos[nextitm])
905  {
906  /* skip item to be deleted */
907  nextitm++;
908  }
909  else
910  {
911  itemidptr->offsetindex = nused; /* where it will go */
912  itemidptr->itemoff = offset;
913  itemidptr->alignedlen = MAXALIGN(size);
914  totallen += itemidptr->alignedlen;
915  newitemids[nused] = *lp;
916  itemidptr++;
917  nused++;
918  }
919  }
920 
921  /* this will catch invalid or out-of-order itemnos[] */
922  if (nextitm != nitems)
923  elog(ERROR, "incorrect index offsets supplied");
924 
925  if (totallen > (Size) (pd_special - pd_lower))
926  ereport(ERROR,
927  (errcode(ERRCODE_DATA_CORRUPTED),
928  errmsg("corrupted item lengths: total %u, available space %u",
929  (unsigned int) totallen, pd_special - pd_lower)));
930 
931  /*
932  * Looks good. Overwrite the line pointers with the copy, from which we've
933  * removed all the unused items.
934  */
935  memcpy(phdr->pd_linp, newitemids, nused * sizeof(ItemIdData));
936  phdr->pd_lower = SizeOfPageHeaderData + nused * sizeof(ItemIdData);
937 
938  /* and compactify the tuple data */
939  compactify_tuples(itemidbase, nused, page);
940 }
941 
942 
943 /*
944  * PageIndexTupleDeleteNoCompact
945  *
946  * Remove the specified tuple from an index page, but set its line pointer
947  * to "unused" instead of compacting it out, except that it can be removed
948  * if it's the last line pointer on the page.
949  *
950  * This is used for index AMs that require that existing TIDs of live tuples
951  * remain unchanged, and are willing to allow unused line pointers instead.
952  */
953 void
955 {
956  PageHeader phdr = (PageHeader) page;
957  char *addr;
958  ItemId tup;
959  Size size;
960  unsigned offset;
961  int nline;
962 
963  /*
964  * As with PageRepairFragmentation, paranoia seems justified.
965  */
966  if (phdr->pd_lower < SizeOfPageHeaderData ||
967  phdr->pd_lower > phdr->pd_upper ||
968  phdr->pd_upper > phdr->pd_special ||
969  phdr->pd_special > BLCKSZ ||
970  phdr->pd_special != MAXALIGN(phdr->pd_special))
971  ereport(ERROR,
972  (errcode(ERRCODE_DATA_CORRUPTED),
973  errmsg("corrupted page pointers: lower = %u, upper = %u, special = %u",
974  phdr->pd_lower, phdr->pd_upper, phdr->pd_special)));
975 
976  nline = PageGetMaxOffsetNumber(page);
977  if ((int) offnum <= 0 || (int) offnum > nline)
978  elog(ERROR, "invalid index offnum: %u", offnum);
979 
980  tup = PageGetItemId(page, offnum);
981  Assert(ItemIdHasStorage(tup));
982  size = ItemIdGetLength(tup);
983  offset = ItemIdGetOffset(tup);
984 
985  if (offset < phdr->pd_upper || (offset + size) > phdr->pd_special ||
986  offset != MAXALIGN(offset))
987  ereport(ERROR,
988  (errcode(ERRCODE_DATA_CORRUPTED),
989  errmsg("corrupted item pointer: offset = %u, size = %u",
990  offset, (unsigned int) size)));
991 
992  /* Amount of space to actually be deleted */
993  size = MAXALIGN(size);
994 
995  /*
996  * Either set the item pointer to "unused", or zap it if it's the last
997  * one. (Note: it's possible that the next-to-last one(s) are already
998  * unused, but we do not trouble to try to compact them out if so.)
999  */
1000  if ((int) offnum < nline)
1001  ItemIdSetUnused(tup);
1002  else
1003  {
1004  phdr->pd_lower -= sizeof(ItemIdData);
1005  nline--; /* there's one less than when we started */
1006  }
1007 
1008  /*
1009  * Now move everything between the old upper bound (beginning of tuple
1010  * space) and the beginning of the deleted tuple forward, so that space in
1011  * the middle of the page is left free. If we've just deleted the tuple
1012  * at the beginning of tuple space, then there's no need to do the copy.
1013  */
1014 
1015  /* beginning of tuple space */
1016  addr = (char *) page + phdr->pd_upper;
1017 
1018  if (offset > phdr->pd_upper)
1019  memmove(addr + size, addr, offset - phdr->pd_upper);
1020 
1021  /* adjust free space boundary pointer */
1022  phdr->pd_upper += size;
1023 
1024  /*
1025  * Finally, we need to adjust the linp entries that remain.
1026  *
1027  * Anything that used to be before the deleted tuple's data was moved
1028  * forward by the size of the deleted tuple.
1029  */
1030  if (!PageIsEmpty(page))
1031  {
1032  int i;
1033 
1034  for (i = 1; i <= nline; i++)
1035  {
1036  ItemId ii = PageGetItemId(phdr, i);
1037 
1038  if (ItemIdHasStorage(ii) && ItemIdGetOffset(ii) <= offset)
1039  ii->lp_off += size;
1040  }
1041  }
1042 }
1043 
1044 
1045 /*
1046  * PageIndexTupleOverwrite
1047  *
1048  * Replace a specified tuple on an index page.
1049  *
1050  * The new tuple is placed exactly where the old one had been, shifting
1051  * other tuples' data up or down as needed to keep the page compacted.
1052  * This is better than deleting and reinserting the tuple, because it
1053  * avoids any data shifting when the tuple size doesn't change; and
1054  * even when it does, we avoid moving the item pointers around.
1055  * Conceivably this could also be of use to an index AM that cares about
1056  * the physical order of tuples as well as their ItemId order.
1057  *
1058  * If there's insufficient space for the new tuple, return false. Other
1059  * errors represent data-corruption problems, so we just elog.
1060  */
1061 bool
1063  Item newtup, Size newsize)
1064 {
1065  PageHeader phdr = (PageHeader) page;
1066  ItemId tupid;
1067  int oldsize;
1068  unsigned offset;
1069  Size alignednewsize;
1070  int size_diff;
1071  int itemcount;
1072 
1073  /*
1074  * As with PageRepairFragmentation, paranoia seems justified.
1075  */
1076  if (phdr->pd_lower < SizeOfPageHeaderData ||
1077  phdr->pd_lower > phdr->pd_upper ||
1078  phdr->pd_upper > phdr->pd_special ||
1079  phdr->pd_special > BLCKSZ ||
1080  phdr->pd_special != MAXALIGN(phdr->pd_special))
1081  ereport(ERROR,
1082  (errcode(ERRCODE_DATA_CORRUPTED),
1083  errmsg("corrupted page pointers: lower = %u, upper = %u, special = %u",
1084  phdr->pd_lower, phdr->pd_upper, phdr->pd_special)));
1085 
1086  itemcount = PageGetMaxOffsetNumber(page);
1087  if ((int) offnum <= 0 || (int) offnum > itemcount)
1088  elog(ERROR, "invalid index offnum: %u", offnum);
1089 
1090  tupid = PageGetItemId(page, offnum);
1091  Assert(ItemIdHasStorage(tupid));
1092  oldsize = ItemIdGetLength(tupid);
1093  offset = ItemIdGetOffset(tupid);
1094 
1095  if (offset < phdr->pd_upper || (offset + oldsize) > phdr->pd_special ||
1096  offset != MAXALIGN(offset))
1097  ereport(ERROR,
1098  (errcode(ERRCODE_DATA_CORRUPTED),
1099  errmsg("corrupted item pointer: offset = %u, size = %u",
1100  offset, (unsigned int) oldsize)));
1101 
1102  /*
1103  * Determine actual change in space requirement, check for page overflow.
1104  */
1105  oldsize = MAXALIGN(oldsize);
1106  alignednewsize = MAXALIGN(newsize);
1107  if (alignednewsize > oldsize + (phdr->pd_upper - phdr->pd_lower))
1108  return false;
1109 
1110  /*
1111  * Relocate existing data and update line pointers, unless the new tuple
1112  * is the same size as the old (after alignment), in which case there's
1113  * nothing to do. Notice that what we have to relocate is data before the
1114  * target tuple, not data after, so it's convenient to express size_diff
1115  * as the amount by which the tuple's size is decreasing, making it the
1116  * delta to add to pd_upper and affected line pointers.
1117  */
1118  size_diff = oldsize - (int) alignednewsize;
1119  if (size_diff != 0)
1120  {
1121  char *addr = (char *) page + phdr->pd_upper;
1122  int i;
1123 
1124  /* relocate all tuple data before the target tuple */
1125  memmove(addr + size_diff, addr, offset - phdr->pd_upper);
1126 
1127  /* adjust free space boundary pointer */
1128  phdr->pd_upper += size_diff;
1129 
1130  /* adjust affected line pointers too */
1131  for (i = FirstOffsetNumber; i <= itemcount; i++)
1132  {
1133  ItemId ii = PageGetItemId(phdr, i);
1134 
1135  /* Allow items without storage; currently only BRIN needs that */
1136  if (ItemIdHasStorage(ii) && ItemIdGetOffset(ii) <= offset)
1137  ii->lp_off += size_diff;
1138  }
1139  }
1140 
1141  /* Update the item's tuple length (other fields shouldn't change) */
1142  ItemIdSetNormal(tupid, offset + size_diff, newsize);
1143 
1144  /* Copy new tuple data onto page */
1145  memcpy(PageGetItem(page, tupid), newtup, newsize);
1146 
1147  return true;
1148 }
1149 
1150 
1151 /*
1152  * Set checksum for a page in shared buffers.
1153  *
1154  * If checksums are disabled, or if the page is not initialized, just return
1155  * the input. Otherwise, we must make a copy of the page before calculating
1156  * the checksum, to prevent concurrent modifications (e.g. setting hint bits)
1157  * from making the final checksum invalid. It doesn't matter if we include or
1158  * exclude hints during the copy, as long as we write a valid page and
1159  * associated checksum.
1160  *
1161  * Returns a pointer to the block-sized data that needs to be written. Uses
1162  * statically-allocated memory, so the caller must immediately write the
1163  * returned page and not refer to it again.
1164  */
1165 char *
1167 {
1168  static char *pageCopy = NULL;
1169 
1170  /* If we don't need a checksum, just return the passed-in data */
1171  if (PageIsNew(page) || !DataChecksumsEnabled())
1172  return (char *) page;
1173 
1174  /*
1175  * We allocate the copy space once and use it over on each subsequent
1176  * call. The point of palloc'ing here, rather than having a static char
1177  * array, is first to ensure adequate alignment for the checksumming code
1178  * and second to avoid wasting space in processes that never call this.
1179  */
1180  if (pageCopy == NULL)
1181  pageCopy = MemoryContextAlloc(TopMemoryContext, BLCKSZ);
1182 
1183  memcpy(pageCopy, (char *) page, BLCKSZ);
1184  ((PageHeader) pageCopy)->pd_checksum = pg_checksum_page(pageCopy, blkno);
1185  return pageCopy;
1186 }
1187 
1188 /*
1189  * Set checksum for a page in private memory.
1190  *
1191  * This must only be used when we know that no other process can be modifying
1192  * the page buffer.
1193  */
1194 void
1196 {
1197  /* If we don't need a checksum, just return */
1198  if (PageIsNew(page) || !DataChecksumsEnabled())
1199  return;
1200 
1201  ((PageHeader) page)->pd_checksum = pg_checksum_page((char *) page, blkno);
1202 }
signed short int16
Definition: c.h:283
#define PageClearHasFreeLinePointers(page)
Definition: bufpage.h:371
#define PageIsEmpty(page)
Definition: bufpage.h:218
void PageRestoreTempPage(Page tempPage, Page oldPage)
Definition: bufpage.c:407
bool PageIsVerified(Page page, BlockNumber blkno)
Definition: bufpage.c:81
uint16 pd_flags
Definition: bufpage.h:153
#define PageSetPageSizeAndVersion(page, size, version)
Definition: bufpage.h:281
int16 itemoff
Definition: bufpage.c:423
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:43
void PageIndexTupleDelete(Page page, OffsetNumber offnum)
Definition: bufpage.c:723
bool DataChecksumsEnabled(void)
Definition: xlog.c:4731
#define VALGRIND_CHECK_MEM_IS_DEFINED(addr, size)
Definition: memdebug.h:23
#define PAI_OVERWRITE
Definition: bufpage.h:409
#define ItemIdIsUsed(itemId)
Definition: itemid.h:91
#define MaxHeapTuplesPerPage
Definition: htup_details.h:580
Pointer Item
Definition: item.h:17
char * PageSetChecksumCopy(Page page, BlockNumber blkno)
Definition: bufpage.c:1166
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:853
uint32 BlockNumber
Definition: block.h:31
Datum upper(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:74
#define SizeOfPageHeaderData
Definition: bufpage.h:212
#define PANIC
Definition: elog.h:53
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:353
#define PG_PAGE_LAYOUT_VERSION
Definition: bufpage.h:195
uint16 alignedlen
Definition: bufpage.c:424
Size PageGetFreeSpace(Page page)
Definition: bufpage.c:578
uint16 offsetindex
Definition: bufpage.c:422
uint16 OffsetNumber
Definition: off.h:24
uint16 pd_checksum
Definition: bufpage.h:152
Page PageGetTempPageCopySpecial(Page page)
Definition: bufpage.c:385
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:753
unsigned short uint16
Definition: c.h:295
#define ItemIdGetLength(itemId)
Definition: itemid.h:58
void pfree(void *pointer)
Definition: mcxt.c:949
#define ERROR
Definition: elog.h:43
Size PageGetHeapFreeSpace(Page page)
Definition: bufpage.c:662
bool PageIndexTupleOverwrite(Page page, OffsetNumber offnum, Item newtup, Size newsize)
Definition: bufpage.c:1062
#define ItemIdSetNormal(itemId, off, len)
Definition: itemid.h:139
#define memmove(d, s, c)
Definition: c.h:1045
#define FirstOffsetNumber
Definition: off.h:27
#define PageGetPageSize(page)
Definition: bufpage.h:264
struct ItemIdData ItemIdData
#define ItemIdGetOffset(itemId)
Definition: itemid.h:64
itemIdSortData * itemIdSort
Definition: bufpage.c:426
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext TopMemoryContext
Definition: mcxt.c:43
static void compactify_tuples(itemIdSort itemidbase, int nitems, Page page)
Definition: bufpage.c:441
LocationIndex pd_special
Definition: bufpage.h:156
#define WARNING
Definition: elog.h:40
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
Page PageGetTempPage(Page page)
Definition: bufpage.c:348
unsigned lp_off
Definition: itemid.h:26
static int itemoffcompare(const void *itemidp1, const void *itemidp2)
Definition: bufpage.c:429
uint16 LocationIndex
Definition: bufpage.h:83
Size PageGetFreeSpaceForMultipleTuples(Page page, int ntups)
Definition: bufpage.c:605
#define InvalidOffsetNumber
Definition: off.h:26
#define ItemIdHasStorage(itemId)
Definition: itemid.h:119
void PageIndexTupleDeleteNoCompact(Page page, OffsetNumber offnum)
Definition: bufpage.c:954
struct itemIdSortData itemIdSortData
PageHeaderData * PageHeader
Definition: bufpage.h:162
#define Assert(condition)
Definition: c.h:670
signed int Offset
Definition: c.h:423
#define PD_VALID_FLAG_BITS
Definition: bufpage.h:181
void PageIndexMultiDelete(Page page, OffsetNumber *itemnos, int nitems)
Definition: bufpage.c:832
#define OffsetNumberNext(offsetNumber)
Definition: off.h:53
size_t Size
Definition: c.h:404
#define PageGetSpecialPointer(page)
Definition: bufpage.h:322
OffsetNumber PageAddItemExtended(Page page, Item item, Size size, OffsetNumber offsetNumber, int flags)
Definition: bufpage.c:190
void PageSetChecksumInplace(Page page, BlockNumber blkno)
Definition: bufpage.c:1195
#define MAXALIGN(LEN)
Definition: c.h:623
#define PageGetSpecialSize(page)
Definition: bufpage.h:296
Size PageGetExactFreeSpace(Page page)
Definition: bufpage.c:629
bool ignore_checksum_failure
Definition: bufpage.c:26
void PageRepairFragmentation(Page page)
Definition: bufpage.c:479
#define PageIsNew(page)
Definition: bufpage.h:225
#define MaxIndexTuplesPerPage
Definition: itup.h:137
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
Page PageGetTempPageCopy(Page page)
Definition: bufpage.c:365
#define PageSetHasFreeLinePointers(page)
Definition: bufpage.h:369
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:706
#define PAI_IS_HEAP
Definition: bufpage.h:410
int i
#define OffsetNumberIsValid(offsetNumber)
Definition: off.h:40
ItemIdData pd_linp[FLEXIBLE_ARRAY_MEMBER]
Definition: bufpage.h:159
#define unlikely(x)
Definition: c.h:190
#define PageHasFreeLinePointers(page)
Definition: bufpage.h:367
#define elog
Definition: elog.h:219
#define qsort(a, b, c, d)
Definition: port.h:408
LocationIndex pd_upper
Definition: bufpage.h:155
#define ItemIdSetUnused(itemId)
Definition: itemid.h:127
uint16 pg_checksum_page(char *page, BlockNumber blkno)
#define PageGetItem(page, itemId)
Definition: bufpage.h:336
Pointer Page
Definition: bufpage.h:74
LocationIndex pd_lower
Definition: bufpage.h:154
void PageInit(Page page, Size pageSize, Size specialSize)
Definition: bufpage.c:41