PostgreSQL Source Code  git master
hio.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * hio.c
4  * POSTGRES heap access method input/output code.
5  *
6  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/access/heap/hio.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 
16 #include "postgres.h"
17 
18 #include "access/heapam.h"
19 #include "access/hio.h"
20 #include "access/htup_details.h"
21 #include "access/visibilitymap.h"
22 #include "storage/bufmgr.h"
23 #include "storage/freespace.h"
24 #include "storage/lmgr.h"
25 #include "storage/smgr.h"
26 
27 
28 /*
29  * RelationPutHeapTuple - place tuple at specified page
30  *
31  * !!! EREPORT(ERROR) IS DISALLOWED HERE !!! Must PANIC on failure!!!
32  *
33  * Note - caller must hold BUFFER_LOCK_EXCLUSIVE on the buffer.
34  */
35 void
37  Buffer buffer,
38  HeapTuple tuple,
39  bool token)
40 {
41  Page pageHeader;
42  OffsetNumber offnum;
43 
44  /*
45  * A tuple that's being inserted speculatively should already have its
46  * token set.
47  */
48  Assert(!token || HeapTupleHeaderIsSpeculative(tuple->t_data));
49 
50  /*
51  * Do not allow tuples with invalid combinations of hint bits to be placed
52  * on a page. These combinations are detected as corruption by the
53  * contrib/amcheck logic, so if you disable one or both of these
54  * assertions, make corresponding changes there.
55  */
57  (tuple->t_data->t_infomask2 & HEAP_KEYS_UPDATED)));
59  (tuple->t_data->t_infomask & HEAP_XMAX_IS_MULTI)));
60 
61  /* Add the tuple to the page */
62  pageHeader = BufferGetPage(buffer);
63 
64  offnum = PageAddItem(pageHeader, (Item) tuple->t_data,
65  tuple->t_len, InvalidOffsetNumber, false, true);
66 
67  if (offnum == InvalidOffsetNumber)
68  elog(PANIC, "failed to add tuple to page");
69 
70  /* Update tuple->t_self to the actual position where it was stored */
71  ItemPointerSet(&(tuple->t_self), BufferGetBlockNumber(buffer), offnum);
72 
73  /*
74  * Insert the correct position into CTID of the stored tuple, too (unless
75  * this is a speculative insertion, in which case the token is held in
76  * CTID field instead)
77  */
78  if (!token)
79  {
80  ItemId itemId = PageGetItemId(pageHeader, offnum);
81  HeapTupleHeader item = (HeapTupleHeader) PageGetItem(pageHeader, itemId);
82 
83  item->t_ctid = tuple->t_self;
84  }
85 }
86 
87 /*
88  * Read in a buffer in mode, using bulk-insert strategy if bistate isn't NULL.
89  */
90 static Buffer
91 ReadBufferBI(Relation relation, BlockNumber targetBlock,
93 {
94  Buffer buffer;
95 
96  /* If not bulk-insert, exactly like ReadBuffer */
97  if (!bistate)
98  return ReadBufferExtended(relation, MAIN_FORKNUM, targetBlock,
99  mode, NULL);
100 
101  /* If we have the desired block already pinned, re-pin and return it */
102  if (bistate->current_buf != InvalidBuffer)
103  {
104  if (BufferGetBlockNumber(bistate->current_buf) == targetBlock)
105  {
106  /*
107  * Currently the LOCK variants are only used for extending
108  * relation, which should never reach this branch.
109  */
110  Assert(mode != RBM_ZERO_AND_LOCK &&
111  mode != RBM_ZERO_AND_CLEANUP_LOCK);
112 
114  return bistate->current_buf;
115  }
116  /* ... else drop the old buffer */
117  ReleaseBuffer(bistate->current_buf);
118  bistate->current_buf = InvalidBuffer;
119  }
120 
121  /* Perform a read using the buffer strategy */
122  buffer = ReadBufferExtended(relation, MAIN_FORKNUM, targetBlock,
123  mode, bistate->strategy);
124 
125  /* Save the selected block as target for future inserts */
126  IncrBufferRefCount(buffer);
127  bistate->current_buf = buffer;
128 
129  return buffer;
130 }
131 
132 /*
133  * For each heap page which is all-visible, acquire a pin on the appropriate
134  * visibility map page, if we haven't already got one.
135  *
136  * buffer2 may be InvalidBuffer, if only one buffer is involved. buffer1
137  * must not be InvalidBuffer. If both buffers are specified, block1 must
138  * be less than block2.
139  */
140 static void
141 GetVisibilityMapPins(Relation relation, Buffer buffer1, Buffer buffer2,
142  BlockNumber block1, BlockNumber block2,
143  Buffer *vmbuffer1, Buffer *vmbuffer2)
144 {
145  bool need_to_pin_buffer1;
146  bool need_to_pin_buffer2;
147 
148  Assert(BufferIsValid(buffer1));
149  Assert(buffer2 == InvalidBuffer || block1 <= block2);
150 
151  while (1)
152  {
153  /* Figure out which pins we need but don't have. */
154  need_to_pin_buffer1 = PageIsAllVisible(BufferGetPage(buffer1))
155  && !visibilitymap_pin_ok(block1, *vmbuffer1);
156  need_to_pin_buffer2 = buffer2 != InvalidBuffer
157  && PageIsAllVisible(BufferGetPage(buffer2))
158  && !visibilitymap_pin_ok(block2, *vmbuffer2);
159  if (!need_to_pin_buffer1 && !need_to_pin_buffer2)
160  return;
161 
162  /* We must unlock both buffers before doing any I/O. */
163  LockBuffer(buffer1, BUFFER_LOCK_UNLOCK);
164  if (buffer2 != InvalidBuffer && buffer2 != buffer1)
165  LockBuffer(buffer2, BUFFER_LOCK_UNLOCK);
166 
167  /* Get pins. */
168  if (need_to_pin_buffer1)
169  visibilitymap_pin(relation, block1, vmbuffer1);
170  if (need_to_pin_buffer2)
171  visibilitymap_pin(relation, block2, vmbuffer2);
172 
173  /* Relock buffers. */
175  if (buffer2 != InvalidBuffer && buffer2 != buffer1)
177 
178  /*
179  * If there are two buffers involved and we pinned just one of them,
180  * it's possible that the second one became all-visible while we were
181  * busy pinning the first one. If it looks like that's a possible
182  * scenario, we'll need to make a second pass through this loop.
183  */
184  if (buffer2 == InvalidBuffer || buffer1 == buffer2
185  || (need_to_pin_buffer1 && need_to_pin_buffer2))
186  break;
187  }
188 }
189 
190 /*
191  * Extend a relation by multiple blocks to avoid future contention on the
192  * relation extension lock. Our goal is to pre-extend the relation by an
193  * amount which ramps up as the degree of contention ramps up, but limiting
194  * the result to some sane overall value.
195  */
196 static void
198 {
199  BlockNumber blockNum,
200  firstBlock = InvalidBlockNumber;
201  int extraBlocks;
202  int lockWaiters;
203 
204  /* Use the length of the lock wait queue to judge how much to extend. */
205  lockWaiters = RelationExtensionLockWaiterCount(relation);
206  if (lockWaiters <= 0)
207  return;
208 
209  /*
210  * It might seem like multiplying the number of lock waiters by as much as
211  * 20 is too aggressive, but benchmarking revealed that smaller numbers
212  * were insufficient. 512 is just an arbitrary cap to prevent
213  * pathological results.
214  */
215  extraBlocks = Min(512, lockWaiters * 20);
216 
217  do
218  {
219  Buffer buffer;
220  Page page;
221  Size freespace;
222 
223  /*
224  * Extend by one page. This should generally match the main-line
225  * extension code in RelationGetBufferForTuple, except that we hold
226  * the relation extension lock throughout, and we don't immediately
227  * initialize the page (see below).
228  */
229  buffer = ReadBufferBI(relation, P_NEW, RBM_ZERO_AND_LOCK, bistate);
230  page = BufferGetPage(buffer);
231 
232  if (!PageIsNew(page))
233  elog(ERROR, "page %u of relation \"%s\" should be empty but is not",
234  BufferGetBlockNumber(buffer),
235  RelationGetRelationName(relation));
236 
237  /*
238  * Add the page to the FSM without initializing. If we were to
239  * initialize here, the page would potentially get flushed out to disk
240  * before we add any useful content. There's no guarantee that that'd
241  * happen before a potential crash, so we need to deal with
242  * uninitialized pages anyway, thus avoid the potential for
243  * unnecessary writes.
244  */
245 
246  /* we'll need this info below */
247  blockNum = BufferGetBlockNumber(buffer);
248  freespace = BufferGetPageSize(buffer) - SizeOfPageHeaderData;
249 
250  UnlockReleaseBuffer(buffer);
251 
252  /* Remember first block number thus added. */
253  if (firstBlock == InvalidBlockNumber)
254  firstBlock = blockNum;
255 
256  /*
257  * Immediately update the bottom level of the FSM. This has a good
258  * chance of making this page visible to other concurrently inserting
259  * backends, and we want that to happen without delay.
260  */
261  RecordPageWithFreeSpace(relation, blockNum, freespace);
262  }
263  while (--extraBlocks > 0);
264 
265  /*
266  * Updating the upper levels of the free space map is too expensive to do
267  * for every block, but it's worth doing once at the end to make sure that
268  * subsequent insertion activity sees all of those nifty free pages we
269  * just inserted.
270  */
271  FreeSpaceMapVacuumRange(relation, firstBlock, blockNum + 1);
272 }
273 
274 /*
275  * RelationGetBufferForTuple
276  *
277  * Returns pinned and exclusive-locked buffer of a page in given relation
278  * with free space >= given len.
279  *
280  * If otherBuffer is not InvalidBuffer, then it references a previously
281  * pinned buffer of another page in the same relation; on return, this
282  * buffer will also be exclusive-locked. (This case is used by heap_update;
283  * the otherBuffer contains the tuple being updated.)
284  *
285  * The reason for passing otherBuffer is that if two backends are doing
286  * concurrent heap_update operations, a deadlock could occur if they try
287  * to lock the same two buffers in opposite orders. To ensure that this
288  * can't happen, we impose the rule that buffers of a relation must be
289  * locked in increasing page number order. This is most conveniently done
290  * by having RelationGetBufferForTuple lock them both, with suitable care
291  * for ordering.
292  *
293  * NOTE: it is unlikely, but not quite impossible, for otherBuffer to be the
294  * same buffer we select for insertion of the new tuple (this could only
295  * happen if space is freed in that page after heap_update finds there's not
296  * enough there). In that case, the page will be pinned and locked only once.
297  *
298  * For the vmbuffer and vmbuffer_other arguments, we avoid deadlock by
299  * locking them only after locking the corresponding heap page, and taking
300  * no further lwlocks while they are locked.
301  *
302  * We normally use FSM to help us find free space. However,
303  * if HEAP_INSERT_SKIP_FSM is specified, we just append a new empty page to
304  * the end of the relation if the tuple won't fit on the current target page.
305  * This can save some cycles when we know the relation is new and doesn't
306  * contain useful amounts of free space.
307  *
308  * HEAP_INSERT_SKIP_FSM is also useful for non-WAL-logged additions to a
309  * relation, if the caller holds exclusive lock and is careful to invalidate
310  * relation's smgr_targblock before the first insertion --- that ensures that
311  * all insertions will occur into newly added pages and not be intermixed
312  * with tuples from other transactions. That way, a crash can't risk losing
313  * any committed data of other transactions. (See heap_insert's comments
314  * for additional constraints needed for safe usage of this behavior.)
315  *
316  * The caller can also provide a BulkInsertState object to optimize many
317  * insertions into the same relation. This keeps a pin on the current
318  * insertion target page (to save pin/unpin cycles) and also passes a
319  * BULKWRITE buffer selection strategy object to the buffer manager.
320  * Passing NULL for bistate selects the default behavior.
321  *
322  * We always try to avoid filling existing pages further than the fillfactor.
323  * This is OK since this routine is not consulted when updating a tuple and
324  * keeping it on the same page, which is the scenario fillfactor is meant
325  * to reserve space for.
326  *
327  * ereport(ERROR) is allowed here, so this routine *must* be called
328  * before any (unlogged) changes are made in buffer pool.
329  */
330 Buffer
332  Buffer otherBuffer, int options,
333  BulkInsertState bistate,
334  Buffer *vmbuffer, Buffer *vmbuffer_other)
335 {
336  bool use_fsm = !(options & HEAP_INSERT_SKIP_FSM);
337  Buffer buffer = InvalidBuffer;
338  Page page;
339  Size pageFreeSpace = 0,
340  saveFreeSpace = 0;
341  BlockNumber targetBlock,
342  otherBlock;
343  bool needLock;
344 
345  len = MAXALIGN(len); /* be conservative */
346 
347  /* Bulk insert is not supported for updates, only inserts. */
348  Assert(otherBuffer == InvalidBuffer || !bistate);
349 
350  /*
351  * If we're gonna fail for oversize tuple, do it right away
352  */
353  if (len > MaxHeapTupleSize)
354  ereport(ERROR,
355  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
356  errmsg("row is too big: size %zu, maximum size %zu",
357  len, MaxHeapTupleSize)));
358 
359  /* Compute desired extra freespace due to fillfactor option */
360  saveFreeSpace = RelationGetTargetPageFreeSpace(relation,
362 
363  if (otherBuffer != InvalidBuffer)
364  otherBlock = BufferGetBlockNumber(otherBuffer);
365  else
366  otherBlock = InvalidBlockNumber; /* just to keep compiler quiet */
367 
368  /*
369  * We first try to put the tuple on the same page we last inserted a tuple
370  * on, as cached in the BulkInsertState or relcache entry. If that
371  * doesn't work, we ask the Free Space Map to locate a suitable page.
372  * Since the FSM's info might be out of date, we have to be prepared to
373  * loop around and retry multiple times. (To insure this isn't an infinite
374  * loop, we must update the FSM with the correct amount of free space on
375  * each page that proves not to be suitable.) If the FSM has no record of
376  * a page with enough free space, we give up and extend the relation.
377  *
378  * When use_fsm is false, we either put the tuple onto the existing target
379  * page or extend the relation.
380  */
381  if (len + saveFreeSpace > MaxHeapTupleSize)
382  {
383  /* can't fit, don't bother asking FSM */
384  targetBlock = InvalidBlockNumber;
385  use_fsm = false;
386  }
387  else if (bistate && bistate->current_buf != InvalidBuffer)
388  targetBlock = BufferGetBlockNumber(bistate->current_buf);
389  else
390  targetBlock = RelationGetTargetBlock(relation);
391 
392  if (targetBlock == InvalidBlockNumber && use_fsm)
393  {
394  /*
395  * We have no cached target page, so ask the FSM for an initial
396  * target.
397  */
398  targetBlock = GetPageWithFreeSpace(relation, len + saveFreeSpace);
399 
400  /*
401  * If the FSM knows nothing of the rel, try the last page before we
402  * give up and extend. This avoids one-tuple-per-page syndrome during
403  * bootstrapping or in a recently-started system.
404  */
405  if (targetBlock == InvalidBlockNumber)
406  {
407  BlockNumber nblocks = RelationGetNumberOfBlocks(relation);
408 
409  if (nblocks > 0)
410  targetBlock = nblocks - 1;
411  }
412  }
413 
414 loop:
415  while (targetBlock != InvalidBlockNumber)
416  {
417  /*
418  * Read and exclusive-lock the target block, as well as the other
419  * block if one was given, taking suitable care with lock ordering and
420  * the possibility they are the same block.
421  *
422  * If the page-level all-visible flag is set, caller will need to
423  * clear both that and the corresponding visibility map bit. However,
424  * by the time we return, we'll have x-locked the buffer, and we don't
425  * want to do any I/O while in that state. So we check the bit here
426  * before taking the lock, and pin the page if it appears necessary.
427  * Checking without the lock creates a risk of getting the wrong
428  * answer, so we'll have to recheck after acquiring the lock.
429  */
430  if (otherBuffer == InvalidBuffer)
431  {
432  /* easy case */
433  buffer = ReadBufferBI(relation, targetBlock, RBM_NORMAL, bistate);
434  if (PageIsAllVisible(BufferGetPage(buffer)))
435  visibilitymap_pin(relation, targetBlock, vmbuffer);
436 
437  /*
438  * If the page is empty, pin vmbuffer to set all_frozen bit later.
439  */
440  if ((options & HEAP_INSERT_FROZEN) &&
441  (PageGetMaxOffsetNumber(BufferGetPage(buffer)) == 0))
442  visibilitymap_pin(relation, targetBlock, vmbuffer);
443 
445  }
446  else if (otherBlock == targetBlock)
447  {
448  /* also easy case */
449  buffer = otherBuffer;
450  if (PageIsAllVisible(BufferGetPage(buffer)))
451  visibilitymap_pin(relation, targetBlock, vmbuffer);
453  }
454  else if (otherBlock < targetBlock)
455  {
456  /* lock other buffer first */
457  buffer = ReadBuffer(relation, targetBlock);
458  if (PageIsAllVisible(BufferGetPage(buffer)))
459  visibilitymap_pin(relation, targetBlock, vmbuffer);
460  LockBuffer(otherBuffer, BUFFER_LOCK_EXCLUSIVE);
462  }
463  else
464  {
465  /* lock target buffer first */
466  buffer = ReadBuffer(relation, targetBlock);
467  if (PageIsAllVisible(BufferGetPage(buffer)))
468  visibilitymap_pin(relation, targetBlock, vmbuffer);
470  LockBuffer(otherBuffer, BUFFER_LOCK_EXCLUSIVE);
471  }
472 
473  /*
474  * We now have the target page (and the other buffer, if any) pinned
475  * and locked. However, since our initial PageIsAllVisible checks
476  * were performed before acquiring the lock, the results might now be
477  * out of date, either for the selected victim buffer, or for the
478  * other buffer passed by the caller. In that case, we'll need to
479  * give up our locks, go get the pin(s) we failed to get earlier, and
480  * re-lock. That's pretty painful, but hopefully shouldn't happen
481  * often.
482  *
483  * Note that there's a small possibility that we didn't pin the page
484  * above but still have the correct page pinned anyway, either because
485  * we've already made a previous pass through this loop, or because
486  * caller passed us the right page anyway.
487  *
488  * Note also that it's possible that by the time we get the pin and
489  * retake the buffer locks, the visibility map bit will have been
490  * cleared by some other backend anyway. In that case, we'll have
491  * done a bit of extra work for no gain, but there's no real harm
492  * done.
493  */
494  if (otherBuffer == InvalidBuffer || targetBlock <= otherBlock)
495  GetVisibilityMapPins(relation, buffer, otherBuffer,
496  targetBlock, otherBlock, vmbuffer,
497  vmbuffer_other);
498  else
499  GetVisibilityMapPins(relation, otherBuffer, buffer,
500  otherBlock, targetBlock, vmbuffer_other,
501  vmbuffer);
502 
503  /*
504  * Now we can check to see if there's enough free space here. If so,
505  * we're done.
506  */
507  page = BufferGetPage(buffer);
508 
509  /*
510  * If necessary initialize page, it'll be used soon. We could avoid
511  * dirtying the buffer here, and rely on the caller to do so whenever
512  * it puts a tuple onto the page, but there seems not much benefit in
513  * doing so.
514  */
515  if (PageIsNew(page))
516  {
517  PageInit(page, BufferGetPageSize(buffer), 0);
518  MarkBufferDirty(buffer);
519  }
520 
521  pageFreeSpace = PageGetHeapFreeSpace(page);
522  if (len + saveFreeSpace <= pageFreeSpace)
523  {
524  /* use this page as future insert target, too */
525  RelationSetTargetBlock(relation, targetBlock);
526  return buffer;
527  }
528 
529  /*
530  * Not enough space, so we must give up our page locks and pin (if
531  * any) and prepare to look elsewhere. We don't care which order we
532  * unlock the two buffers in, so this can be slightly simpler than the
533  * code above.
534  */
536  if (otherBuffer == InvalidBuffer)
537  ReleaseBuffer(buffer);
538  else if (otherBlock != targetBlock)
539  {
540  LockBuffer(otherBuffer, BUFFER_LOCK_UNLOCK);
541  ReleaseBuffer(buffer);
542  }
543 
544  /* Without FSM, always fall out of the loop and extend */
545  if (!use_fsm)
546  break;
547 
548  /*
549  * Update FSM as to condition of this page, and ask for another page
550  * to try.
551  */
552  targetBlock = RecordAndGetPageWithFreeSpace(relation,
553  targetBlock,
554  pageFreeSpace,
555  len + saveFreeSpace);
556  }
557 
558  /*
559  * Have to extend the relation.
560  *
561  * We have to use a lock to ensure no one else is extending the rel at the
562  * same time, else we will both try to initialize the same new page. We
563  * can skip locking for new or temp relations, however, since no one else
564  * could be accessing them.
565  */
566  needLock = !RELATION_IS_LOCAL(relation);
567 
568  /*
569  * If we need the lock but are not able to acquire it immediately, we'll
570  * consider extending the relation by multiple blocks at a time to manage
571  * contention on the relation extension lock. However, this only makes
572  * sense if we're using the FSM; otherwise, there's no point.
573  */
574  if (needLock)
575  {
576  if (!use_fsm)
579  {
580  /* Couldn't get the lock immediately; wait for it. */
582 
583  /*
584  * Check if some other backend has extended a block for us while
585  * we were waiting on the lock.
586  */
587  targetBlock = GetPageWithFreeSpace(relation, len + saveFreeSpace);
588 
589  /*
590  * If some other waiter has already extended the relation, we
591  * don't need to do so; just use the existing freespace.
592  */
593  if (targetBlock != InvalidBlockNumber)
594  {
596  goto loop;
597  }
598 
599  /* Time to bulk-extend. */
600  RelationAddExtraBlocks(relation, bistate);
601  }
602  }
603 
604  /*
605  * In addition to whatever extension we performed above, we always add at
606  * least one block to satisfy our own request.
607  *
608  * XXX This does an lseek - rather expensive - but at the moment it is the
609  * only way to accurately determine how many blocks are in a relation. Is
610  * it worth keeping an accurate file length in shared memory someplace,
611  * rather than relying on the kernel to do it for us?
612  */
613  buffer = ReadBufferBI(relation, P_NEW, RBM_ZERO_AND_LOCK, bistate);
614 
615  /*
616  * We need to initialize the empty new page. Double-check that it really
617  * is empty (this should never happen, but if it does we don't want to
618  * risk wiping out valid data).
619  */
620  page = BufferGetPage(buffer);
621 
622  if (!PageIsNew(page))
623  elog(ERROR, "page %u of relation \"%s\" should be empty but is not",
624  BufferGetBlockNumber(buffer),
625  RelationGetRelationName(relation));
626 
627  PageInit(page, BufferGetPageSize(buffer), 0);
628  MarkBufferDirty(buffer);
629 
630  /*
631  * The page is empty, pin vmbuffer to set all_frozen bit.
632  */
633  if (options & HEAP_INSERT_FROZEN)
634  {
636  visibilitymap_pin(relation, BufferGetBlockNumber(buffer), vmbuffer);
637  }
638 
639  /*
640  * Release the file-extension lock; it's now OK for someone else to extend
641  * the relation some more.
642  */
643  if (needLock)
645 
646  /*
647  * Lock the other buffer. It's guaranteed to be of a lower page number
648  * than the new page. To conform with the deadlock prevent rules, we ought
649  * to lock otherBuffer first, but that would give other backends a chance
650  * to put tuples on our page. To reduce the likelihood of that, attempt to
651  * lock the other buffer conditionally, that's very likely to work.
652  * Otherwise we need to lock buffers in the correct order, and retry if
653  * the space has been used in the mean time.
654  *
655  * Alternatively, we could acquire the lock on otherBuffer before
656  * extending the relation, but that'd require holding the lock while
657  * performing IO, which seems worse than an unlikely retry.
658  */
659  if (otherBuffer != InvalidBuffer)
660  {
661  Assert(otherBuffer != buffer);
662 
663  if (unlikely(!ConditionalLockBuffer(otherBuffer)))
664  {
666  LockBuffer(otherBuffer, BUFFER_LOCK_EXCLUSIVE);
668 
669  /*
670  * Because the buffer was unlocked for a while, it's possible,
671  * although unlikely, that the page was filled. If so, just retry
672  * from start.
673  */
674  if (len > PageGetHeapFreeSpace(page))
675  {
676  LockBuffer(otherBuffer, BUFFER_LOCK_UNLOCK);
677  UnlockReleaseBuffer(buffer);
678 
679  goto loop;
680  }
681  }
682  }
683 
684  if (len > PageGetHeapFreeSpace(page))
685  {
686  /* We should not get here given the test at the top */
687  elog(PANIC, "tuple is too big: size %zu", len);
688  }
689 
690  /*
691  * Remember the new page as our target for future insertions.
692  *
693  * XXX should we enter the new page into the free space map immediately,
694  * or just keep it for this backend's exclusive use in the short run
695  * (until VACUUM sees it)? Seems to depend on whether you expect the
696  * current backend to make more insertions or not, which is probably a
697  * good bet most of the time. So for now, don't add it to FSM yet.
698  */
699  RelationSetTargetBlock(relation, BufferGetBlockNumber(buffer));
700 
701  return buffer;
702 }
bool ConditionalLockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:420
static PgChecksumMode mode
Definition: pg_checksums.c:61
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:96
#define HEAP_XMAX_LOCK_ONLY
Definition: htup_details.h:196
#define PageIsAllVisible(page)
Definition: bufpage.h:385
int RelationExtensionLockWaiterCount(Relation relation)
Definition: lmgr.c:437
void RecordPageWithFreeSpace(Relation rel, BlockNumber heapBlk, Size spaceAvail)
Definition: freespace.c:181
#define HEAP_INSERT_FROZEN
Definition: heapam.h:35
void visibilitymap_pin(Relation rel, BlockNumber heapBlk, Buffer *buf)
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1483
#define ExclusiveLock
Definition: lockdefs.h:44
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
#define RELATION_IS_LOCAL(relation)
Definition: rel.h:584
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:666
#define Min(x, y)
Definition: c.h:974
Pointer Item
Definition: item.h:17
#define HeapTupleHeaderIsSpeculative(tup)
Definition: htup_details.h:429
#define InvalidBuffer
Definition: buf.h:25
int errcode(int sqlerrcode)
Definition: elog.c:704
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:416
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3700
#define P_NEW
Definition: bufmgr.h:91
#define HEAP_XMAX_COMMITTED
Definition: htup_details.h:206
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:98
#define SizeOfPageHeaderData
Definition: bufpage.h:216
#define PANIC
Definition: elog.h:55
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
#define RelationGetTargetBlock(relation)
Definition: rel.h:542
void RelationPutHeapTuple(Relation relation, Buffer buffer, HeapTuple tuple, bool token)
Definition: hio.c:36
static Buffer ReadBufferBI(Relation relation, BlockNumber targetBlock, ReadBufferMode mode, BulkInsertState bistate)
Definition: hio.c:91
uint16 OffsetNumber
Definition: off.h:24
HeapTupleHeader t_data
Definition: htup.h:68
static void GetVisibilityMapPins(Relation relation, Buffer buffer1, Buffer buffer2, BlockNumber block1, BlockNumber block2, Buffer *vmbuffer1, Buffer *vmbuffer2)
Definition: hio.c:141
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3723
#define ERROR
Definition: elog.h:45
Size PageGetHeapFreeSpace(Page page)
Definition: bufpage.c:874
ItemPointerData t_ctid
Definition: htup_details.h:160
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
#define MaxHeapTupleSize
Definition: htup_details.h:560
Buffer RelationGetBufferForTuple(Relation relation, Size len, Buffer otherBuffer, int options, BulkInsertState bistate, Buffer *vmbuffer, Buffer *vmbuffer_other)
Definition: hio.c:331
#define RelationGetRelationName(relation)
Definition: rel.h:491
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
bool ConditionalLockBuffer(Buffer buffer)
Definition: bufmgr.c:3965
#define RelationGetTargetPageFreeSpace(relation, defaultff)
Definition: rel.h:341
ReadBufferMode
Definition: bufmgr.h:37
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
void LockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:402
void UnlockRelationForExtension(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:452
#define BufferGetPageSize(buffer)
Definition: bufmgr.h:156
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3939
#define HEAP_KEYS_UPDATED
Definition: htup_details.h:278
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:211
#define HEAP_XMAX_IS_MULTI
Definition: htup_details.h:208
#define InvalidOffsetNumber
Definition: off.h:26
#define ereport(elevel,...)
Definition: elog.h:155
#define Assert(condition)
Definition: c.h:792
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:619
#define RelationSetTargetBlock(relation, targblock)
Definition: rel.h:549
size_t Size
Definition: c.h:528
#define InvalidBlockNumber
Definition: block.h:33
#define MAXALIGN(LEN)
Definition: c.h:745
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
#define HEAP_INSERT_SKIP_FSM
Definition: heapam.h:34
BufferAccessStrategy strategy
Definition: hio.h:31
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2674
static void RelationAddExtraBlocks(Relation relation, BulkInsertState bistate)
Definition: hio.c:197
#define PageIsNew(page)
Definition: bufpage.h:229
int errmsg(const char *fmt,...)
Definition: elog.c:915
#define elog(elevel,...)
Definition: elog.h:228
BlockNumber GetPageWithFreeSpace(Relation rel, Size spaceNeeded)
Definition: freespace.c:132
#define unlikely(x)
Definition: c.h:261
bool visibilitymap_pin_ok(BlockNumber heapBlk, Buffer buf)
BlockNumber RecordAndGetPageWithFreeSpace(Relation rel, BlockNumber oldPage, Size oldSpaceAvail, Size spaceNeeded)
Definition: freespace.c:149
#define HEAP_DEFAULT_FILLFACTOR
Definition: rel.h:312
int Buffer
Definition: buf.h:23
void FreeSpaceMapVacuumRange(Relation rel, BlockNumber start, BlockNumber end)
Definition: freespace.c:354
void IncrBufferRefCount(Buffer buffer)
Definition: bufmgr.c:3738
Buffer current_buf
Definition: hio.h:32
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78
#define ItemPointerSet(pointer, blockNumber, offNum)
Definition: itemptr.h:127
void PageInit(Page page, Size pageSize, Size specialSize)
Definition: bufpage.c:42