Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * heapam_handler.c
4 : : * heap table access method code
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/access/heap/heapam_handler.c
12 : : *
13 : : *
14 : : * NOTES
15 : : * This file wires up the lower level heapam.c et al routines with the
16 : : * tableam abstraction.
17 : : *
18 : : *-------------------------------------------------------------------------
19 : : */
20 : : #include "postgres.h"
21 : :
22 : : #include "access/genam.h"
23 : : #include "access/heapam.h"
24 : : #include "access/heaptoast.h"
25 : : #include "access/multixact.h"
26 : : #include "access/rewriteheap.h"
27 : : #include "access/syncscan.h"
28 : : #include "access/tableam.h"
29 : : #include "access/tsmapi.h"
30 : : #include "access/visibilitymap.h"
31 : : #include "access/xact.h"
32 : : #include "catalog/catalog.h"
33 : : #include "catalog/index.h"
34 : : #include "catalog/storage.h"
35 : : #include "catalog/storage_xlog.h"
36 : : #include "commands/progress.h"
37 : : #include "executor/executor.h"
38 : : #include "miscadmin.h"
39 : : #include "pgstat.h"
40 : : #include "storage/bufmgr.h"
41 : : #include "storage/bufpage.h"
42 : : #include "storage/lmgr.h"
43 : : #include "storage/lock.h"
44 : : #include "storage/predicate.h"
45 : : #include "storage/procarray.h"
46 : : #include "storage/smgr.h"
47 : : #include "utils/builtins.h"
48 : : #include "utils/rel.h"
49 : : #include "utils/tuplesort.h"
50 : :
51 : : static void reform_and_rewrite_tuple(HeapTuple tuple,
52 : : Relation OldHeap, Relation NewHeap,
53 : : Datum *values, bool *isnull, RewriteState rwstate);
54 : : static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap,
55 : : Relation NewHeap, Datum *values, bool *isnull,
56 : : BulkInsertState bistate);
57 : : static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap,
58 : : Relation NewHeap, Datum *values, bool *isnull);
59 : :
60 : : static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer,
61 : : HeapTuple tuple,
62 : : OffsetNumber tupoffset);
63 : :
64 : : static BlockNumber heapam_scan_get_blocks_done(HeapScanDesc hscan);
65 : :
66 : : static bool BitmapHeapScanNextBlock(TableScanDesc scan,
67 : : bool *recheck,
68 : : uint64 *lossy_pages, uint64 *exact_pages);
69 : :
70 : :
71 : : /* ------------------------------------------------------------------------
72 : : * Slot related callbacks for heap AM
73 : : * ------------------------------------------------------------------------
74 : : */
75 : :
76 : : static const TupleTableSlotOps *
77 : 17894130 : heapam_slot_callbacks(Relation relation)
78 : : {
79 : 17894130 : return &TTSOpsBufferHeapTuple;
80 : : }
81 : :
82 : :
83 : : /* ------------------------------------------------------------------------
84 : : * Callbacks for non-modifying operations on individual tuples for heap AM
85 : : * ------------------------------------------------------------------------
86 : : */
87 : :
88 : : static bool
89 : 2857924 : heapam_fetch_row_version(Relation relation,
90 : : ItemPointer tid,
91 : : Snapshot snapshot,
92 : : TupleTableSlot *slot)
93 : : {
94 : 2857924 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
95 : : Buffer buffer;
96 : :
97 : : Assert(TTS_IS_BUFFERTUPLE(slot));
98 : :
99 : 2857924 : bslot->base.tupdata.t_self = *tid;
100 [ + + ]: 2857924 : if (heap_fetch(relation, snapshot, &bslot->base.tupdata, &buffer, false))
101 : : {
102 : : /* store in slot, transferring existing pin */
103 : 2857558 : ExecStorePinnedBufferHeapTuple(&bslot->base.tupdata, slot, buffer);
104 : 2857558 : slot->tts_tableOid = RelationGetRelid(relation);
105 : :
106 : 2857558 : return true;
107 : : }
108 : :
109 : 358 : return false;
110 : : }
111 : :
112 : : static bool
113 : 8134 : heapam_tuple_tid_valid(TableScanDesc scan, ItemPointer tid)
114 : : {
115 : 8134 : HeapScanDesc hscan = (HeapScanDesc) scan;
116 : :
117 [ + + ]: 16256 : return ItemPointerIsValid(tid) &&
118 [ + + ]: 8122 : ItemPointerGetBlockNumber(tid) < hscan->rs_nblocks;
119 : : }
120 : :
121 : : static bool
122 : 764220 : heapam_tuple_satisfies_snapshot(Relation rel, TupleTableSlot *slot,
123 : : Snapshot snapshot)
124 : : {
125 : 764220 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
126 : : bool res;
127 : :
128 : : Assert(TTS_IS_BUFFERTUPLE(slot));
129 : : Assert(BufferIsValid(bslot->buffer));
130 : :
131 : : /*
132 : : * We need buffer pin and lock to call HeapTupleSatisfiesVisibility.
133 : : * Caller should be holding pin, but not lock.
134 : : */
135 : 764220 : LockBuffer(bslot->buffer, BUFFER_LOCK_SHARE);
136 : 764220 : res = HeapTupleSatisfiesVisibility(bslot->base.tuple, snapshot,
137 : : bslot->buffer);
138 : 764220 : LockBuffer(bslot->buffer, BUFFER_LOCK_UNLOCK);
139 : :
140 : 764220 : return res;
141 : : }
142 : :
143 : :
144 : : /* ----------------------------------------------------------------------------
145 : : * Functions for manipulations of physical tuples for heap AM.
146 : : * ----------------------------------------------------------------------------
147 : : */
148 : :
149 : : static void
150 : 10842890 : heapam_tuple_insert(Relation relation, TupleTableSlot *slot, CommandId cid,
151 : : uint32 options, BulkInsertState bistate)
152 : : {
153 : 10842890 : bool shouldFree = true;
154 : 10842890 : HeapTuple tuple = ExecFetchSlotHeapTuple(slot, true, &shouldFree);
155 : :
156 : : /* Update the tuple with table oid */
157 : 10842890 : slot->tts_tableOid = RelationGetRelid(relation);
158 : 10842890 : tuple->t_tableOid = slot->tts_tableOid;
159 : :
160 : : /* Perform the insertion, and copy the resulting ItemPointer */
161 : 10842890 : heap_insert(relation, tuple, cid, options, bistate);
162 : 10842869 : ItemPointerCopy(&tuple->t_self, &slot->tts_tid);
163 : :
164 [ + + ]: 10842869 : if (shouldFree)
165 : 2961467 : pfree(tuple);
166 : 10842869 : }
167 : :
168 : : static void
169 : 2231 : heapam_tuple_insert_speculative(Relation relation, TupleTableSlot *slot,
170 : : CommandId cid, uint32 options,
171 : : BulkInsertState bistate, uint32 specToken)
172 : : {
173 : 2231 : bool shouldFree = true;
174 : 2231 : HeapTuple tuple = ExecFetchSlotHeapTuple(slot, true, &shouldFree);
175 : :
176 : : /* Update the tuple with table oid */
177 : 2231 : slot->tts_tableOid = RelationGetRelid(relation);
178 : 2231 : tuple->t_tableOid = slot->tts_tableOid;
179 : :
180 : 2231 : HeapTupleHeaderSetSpeculativeToken(tuple->t_data, specToken);
181 : 2231 : options |= HEAP_INSERT_SPECULATIVE;
182 : :
183 : : /* Perform the insertion, and copy the resulting ItemPointer */
184 : 2231 : heap_insert(relation, tuple, cid, options, bistate);
185 : 2231 : ItemPointerCopy(&tuple->t_self, &slot->tts_tid);
186 : :
187 [ + + ]: 2231 : if (shouldFree)
188 : 54 : pfree(tuple);
189 : 2231 : }
190 : :
191 : : static void
192 : 2227 : heapam_tuple_complete_speculative(Relation relation, TupleTableSlot *slot,
193 : : uint32 specToken, bool succeeded)
194 : : {
195 : : /* adjust the tuple's state accordingly */
196 [ + + ]: 2227 : if (succeeded)
197 : 2216 : heap_finish_speculative(relation, &slot->tts_tid);
198 : : else
199 : 11 : heap_abort_speculative(relation, &slot->tts_tid);
200 : 2227 : }
201 : :
202 : : static TM_Result
203 : 1109454 : heapam_tuple_delete(Relation relation, ItemPointer tid, CommandId cid,
204 : : uint32 options, Snapshot snapshot, Snapshot crosscheck,
205 : : bool wait, TM_FailureData *tmfd)
206 : : {
207 : : /*
208 : : * Currently Deleting of index tuples are handled at vacuum, in case if
209 : : * the storage itself is cleaning the dead tuples by itself, it is the
210 : : * time to call the index tuple deletion also.
211 : : */
212 : 1109454 : return heap_delete(relation, tid, cid, options, crosscheck, wait,
213 : : tmfd);
214 : : }
215 : :
216 : :
217 : : static TM_Result
218 : 2254793 : heapam_tuple_update(Relation relation, ItemPointer otid, TupleTableSlot *slot,
219 : : CommandId cid, uint32 options,
220 : : Snapshot snapshot, Snapshot crosscheck,
221 : : bool wait, TM_FailureData *tmfd,
222 : : LockTupleMode *lockmode, TU_UpdateIndexes *update_indexes)
223 : : {
224 : 2254793 : bool shouldFree = true;
225 : 2254793 : HeapTuple tuple = ExecFetchSlotHeapTuple(slot, true, &shouldFree);
226 : : TM_Result result;
227 : :
228 : : /* Update the tuple with table oid */
229 : 2254793 : slot->tts_tableOid = RelationGetRelid(relation);
230 : 2254793 : tuple->t_tableOid = slot->tts_tableOid;
231 : :
232 : 2254793 : result = heap_update(relation, otid, tuple, cid, options,
233 : : crosscheck, wait,
234 : : tmfd, lockmode, update_indexes);
235 : 2254781 : ItemPointerCopy(&tuple->t_self, &slot->tts_tid);
236 : :
237 : : /*
238 : : * Decide whether new index entries are needed for the tuple
239 : : *
240 : : * Note: heap_update returns the tid (location) of the new tuple in the
241 : : * t_self field.
242 : : *
243 : : * If the update is not HOT, we must update all indexes. If the update is
244 : : * HOT, it could be that we updated summarized columns, so we either
245 : : * update only summarized indexes, or none at all.
246 : : */
247 [ + + ]: 2254781 : if (result != TM_Ok)
248 : : {
249 : : Assert(*update_indexes == TU_None);
250 : 211 : *update_indexes = TU_None;
251 : : }
252 : 2254570 : else if (!HeapTupleIsHeapOnly(tuple))
253 : : Assert(*update_indexes == TU_All);
254 : : else
255 : : Assert((*update_indexes == TU_Summarizing) ||
256 : : (*update_indexes == TU_None));
257 : :
258 [ + + ]: 2254781 : if (shouldFree)
259 : 31993 : pfree(tuple);
260 : :
261 : 2254781 : return result;
262 : : }
263 : :
264 : : static TM_Result
265 : 571723 : heapam_tuple_lock(Relation relation, ItemPointer tid, Snapshot snapshot,
266 : : TupleTableSlot *slot, CommandId cid, LockTupleMode mode,
267 : : LockWaitPolicy wait_policy, uint8 flags,
268 : : TM_FailureData *tmfd)
269 : : {
270 : 571723 : BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
271 : : TM_Result result;
272 : : Buffer buffer;
273 : 571723 : HeapTuple tuple = &bslot->base.tupdata;
274 : : bool follow_updates;
275 : :
276 : 571723 : follow_updates = (flags & TUPLE_LOCK_FLAG_LOCK_UPDATE_IN_PROGRESS) != 0;
277 : 571723 : tmfd->traversed = false;
278 : :
279 : : Assert(TTS_IS_BUFFERTUPLE(slot));
280 : :
281 : 571921 : tuple_lock_retry:
282 : 571921 : tuple->t_self = *tid;
283 : 571921 : result = heap_lock_tuple(relation, tuple, cid, mode, wait_policy,
284 : : follow_updates, &buffer, tmfd);
285 : :
286 [ + + ]: 571908 : if (result == TM_Updated &&
287 [ + + ]: 241 : (flags & TUPLE_LOCK_FLAG_FIND_LAST_VERSION))
288 : : {
289 : : /* Should not encounter speculative tuple on recheck */
290 : : Assert(!HeapTupleHeaderIsSpeculative(tuple->t_data));
291 : :
292 : 221 : ReleaseBuffer(buffer);
293 : :
294 [ + - ]: 221 : if (!ItemPointerEquals(&tmfd->ctid, &tuple->t_self))
295 : : {
296 : : SnapshotData SnapshotDirty;
297 : : TransactionId priorXmax;
298 : :
299 : : /* it was updated, so look at the updated version */
300 : 221 : *tid = tmfd->ctid;
301 : : /* updated row should have xmin matching this xmax */
302 : 221 : priorXmax = tmfd->xmax;
303 : :
304 : : /* signal that a tuple later in the chain is getting locked */
305 : 221 : tmfd->traversed = true;
306 : :
307 : : /*
308 : : * fetch target tuple
309 : : *
310 : : * Loop here to deal with updated or busy tuples
311 : : */
312 : 221 : InitDirtySnapshot(SnapshotDirty);
313 : : for (;;)
314 : : {
315 [ + + ]: 258 : if (ItemPointerIndicatesMovedPartitions(tid))
316 [ + - ]: 11 : ereport(ERROR,
317 : : (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
318 : : errmsg("tuple to be locked was already moved to another partition due to concurrent update")));
319 : :
320 : 247 : tuple->t_self = *tid;
321 [ + + ]: 247 : if (heap_fetch(relation, &SnapshotDirty, tuple, &buffer, true))
322 : : {
323 : : /*
324 : : * If xmin isn't what we're expecting, the slot must have
325 : : * been recycled and reused for an unrelated tuple. This
326 : : * implies that the latest version of the row was deleted,
327 : : * so we need do nothing. (Should be safe to examine xmin
328 : : * without getting buffer's content lock. We assume
329 : : * reading a TransactionId to be atomic, and Xmin never
330 : : * changes in an existing tuple, except to invalid or
331 : : * frozen, and neither of those can match priorXmax.)
332 : : */
333 [ - + ]: 207 : if (!TransactionIdEquals(HeapTupleHeaderGetXmin(tuple->t_data),
334 : : priorXmax))
335 : : {
336 : 0 : ReleaseBuffer(buffer);
337 : 11 : return TM_Deleted;
338 : : }
339 : :
340 : : /* otherwise xmin should not be dirty... */
341 [ - + ]: 207 : if (TransactionIdIsValid(SnapshotDirty.xmin))
342 [ # # ]: 0 : ereport(ERROR,
343 : : (errcode(ERRCODE_DATA_CORRUPTED),
344 : : errmsg_internal("t_xmin %u is uncommitted in tuple (%u,%u) to be updated in table \"%s\"",
345 : : SnapshotDirty.xmin,
346 : : ItemPointerGetBlockNumber(&tuple->t_self),
347 : : ItemPointerGetOffsetNumber(&tuple->t_self),
348 : : RelationGetRelationName(relation))));
349 : :
350 : : /*
351 : : * If tuple is being updated by other transaction then we
352 : : * have to wait for its commit/abort, or die trying.
353 : : */
354 [ + + ]: 207 : if (TransactionIdIsValid(SnapshotDirty.xmax))
355 : : {
356 : 2 : ReleaseBuffer(buffer);
357 [ - + + - ]: 2 : switch (wait_policy)
358 : : {
359 : 0 : case LockWaitBlock:
360 : 0 : XactLockTableWait(SnapshotDirty.xmax,
361 : 0 : relation, &tuple->t_self,
362 : : XLTW_FetchUpdated);
363 : 0 : break;
364 : 1 : case LockWaitSkip:
365 [ + - ]: 1 : if (!ConditionalXactLockTableWait(SnapshotDirty.xmax, false))
366 : : /* skip instead of waiting */
367 : 1 : return TM_WouldBlock;
368 : 0 : break;
369 : 1 : case LockWaitError:
370 [ + - ]: 1 : if (!ConditionalXactLockTableWait(SnapshotDirty.xmax, log_lock_failures))
371 [ + - ]: 1 : ereport(ERROR,
372 : : (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
373 : : errmsg("could not obtain lock on row in relation \"%s\"",
374 : : RelationGetRelationName(relation))));
375 : 0 : break;
376 : : }
377 : 0 : continue; /* loop back to repeat heap_fetch */
378 : : }
379 : :
380 : : /*
381 : : * If tuple was inserted by our own transaction, we have
382 : : * to check cmin against cid: cmin >= current CID means
383 : : * our command cannot see the tuple, so we should ignore
384 : : * it. Otherwise heap_lock_tuple() will throw an error,
385 : : * and so would any later attempt to update or delete the
386 : : * tuple. (We need not check cmax because
387 : : * HeapTupleSatisfiesDirty will consider a tuple deleted
388 : : * by our transaction dead, regardless of cmax.) We just
389 : : * checked that priorXmax == xmin, so we can test that
390 : : * variable instead of doing HeapTupleHeaderGetXmin again.
391 : : */
392 [ + + + - ]: 212 : if (TransactionIdIsCurrentTransactionId(priorXmax) &&
393 : 7 : HeapTupleHeaderGetCmin(tuple->t_data) >= cid)
394 : : {
395 : 7 : tmfd->xmax = priorXmax;
396 : :
397 : : /*
398 : : * Cmin is the problematic value, so store that. See
399 : : * above.
400 : : */
401 : 7 : tmfd->cmax = HeapTupleHeaderGetCmin(tuple->t_data);
402 : 7 : ReleaseBuffer(buffer);
403 : 7 : return TM_SelfModified;
404 : : }
405 : :
406 : : /*
407 : : * This is a live tuple, so try to lock it again.
408 : : */
409 : 198 : ReleaseBuffer(buffer);
410 : 198 : goto tuple_lock_retry;
411 : : }
412 : :
413 : : /*
414 : : * If the referenced slot was actually empty, the latest
415 : : * version of the row must have been deleted, so we need do
416 : : * nothing.
417 : : */
418 [ - + ]: 40 : if (tuple->t_data == NULL)
419 : : {
420 : : Assert(!BufferIsValid(buffer));
421 : 0 : return TM_Deleted;
422 : : }
423 : :
424 : : /*
425 : : * As above, if xmin isn't what we're expecting, do nothing.
426 : : */
427 [ - + ]: 40 : if (!TransactionIdEquals(HeapTupleHeaderGetXmin(tuple->t_data),
428 : : priorXmax))
429 : : {
430 : 0 : ReleaseBuffer(buffer);
431 : 0 : return TM_Deleted;
432 : : }
433 : :
434 : : /*
435 : : * If we get here, the tuple was found but failed
436 : : * SnapshotDirty. Assuming the xmin is either a committed xact
437 : : * or our own xact (as it certainly should be if we're trying
438 : : * to modify the tuple), this must mean that the row was
439 : : * updated or deleted by either a committed xact or our own
440 : : * xact. If it was deleted, we can ignore it; if it was
441 : : * updated then chain up to the next version and repeat the
442 : : * whole process.
443 : : *
444 : : * As above, it should be safe to examine xmax and t_ctid
445 : : * without the buffer content lock, because they can't be
446 : : * changing. We'd better hold a buffer pin though.
447 : : */
448 [ + + ]: 40 : if (ItemPointerEquals(&tuple->t_self, &tuple->t_data->t_ctid))
449 : : {
450 : : /* deleted, so forget about it */
451 : 3 : ReleaseBuffer(buffer);
452 : 3 : return TM_Deleted;
453 : : }
454 : :
455 : : /* updated, so look at the updated row */
456 : 37 : *tid = tuple->t_data->t_ctid;
457 : : /* updated row should have xmin matching this xmax */
458 : 37 : priorXmax = HeapTupleHeaderGetUpdateXid(tuple->t_data);
459 : 37 : ReleaseBuffer(buffer);
460 : : /* loop back to fetch next in chain */
461 : : }
462 : : }
463 : : else
464 : : {
465 : : /* tuple was deleted, so give up */
466 : 0 : return TM_Deleted;
467 : : }
468 : : }
469 : :
470 : 571687 : slot->tts_tableOid = RelationGetRelid(relation);
471 : 571687 : tuple->t_tableOid = slot->tts_tableOid;
472 : :
473 : : /* store in slot, transferring existing pin */
474 : 571687 : ExecStorePinnedBufferHeapTuple(tuple, slot, buffer);
475 : :
476 : 571687 : return result;
477 : : }
478 : :
479 : :
480 : : /* ------------------------------------------------------------------------
481 : : * DDL related callbacks for heap AM.
482 : : * ------------------------------------------------------------------------
483 : : */
484 : :
485 : : static void
486 : 42714 : heapam_relation_set_new_filelocator(Relation rel,
487 : : const RelFileLocator *newrlocator,
488 : : char persistence,
489 : : TransactionId *freezeXid,
490 : : MultiXactId *minmulti)
491 : : {
492 : : SMgrRelation srel;
493 : :
494 : : /*
495 : : * Initialize to the minimum XID that could put tuples in the table. We
496 : : * know that no xacts older than RecentXmin are still running, so that
497 : : * will do.
498 : : */
499 : 42714 : *freezeXid = RecentXmin;
500 : :
501 : : /*
502 : : * Similarly, initialize the minimum Multixact to the first value that
503 : : * could possibly be stored in tuples in the table. Running transactions
504 : : * could reuse values from their local cache, so we are careful to
505 : : * consider all currently running multis.
506 : : *
507 : : * XXX this could be refined further, but is it worth the hassle?
508 : : */
509 : 42714 : *minmulti = GetOldestMultiXactId();
510 : :
511 : 42714 : srel = RelationCreateStorage(*newrlocator, persistence, true);
512 : :
513 : : /*
514 : : * If required, set up an init fork for an unlogged table so that it can
515 : : * be correctly reinitialized on restart.
516 : : */
517 [ + + ]: 42714 : if (persistence == RELPERSISTENCE_UNLOGGED)
518 : : {
519 : : Assert(rel->rd_rel->relkind == RELKIND_RELATION ||
520 : : rel->rd_rel->relkind == RELKIND_TOASTVALUE);
521 : 183 : smgrcreate(srel, INIT_FORKNUM, false);
522 : 183 : log_smgrcreate(newrlocator, INIT_FORKNUM);
523 : : }
524 : :
525 : 42714 : smgrclose(srel);
526 : 42714 : }
527 : :
528 : : static void
529 : 399 : heapam_relation_nontransactional_truncate(Relation rel)
530 : : {
531 : 399 : RelationTruncate(rel, 0);
532 : 399 : }
533 : :
534 : : static void
535 : 68 : heapam_relation_copy_data(Relation rel, const RelFileLocator *newrlocator)
536 : : {
537 : : SMgrRelation dstrel;
538 : :
539 : : /*
540 : : * Since we copy the file directly without looking at the shared buffers,
541 : : * we'd better first flush out any pages of the source relation that are
542 : : * in shared buffers. We assume no new changes will be made while we are
543 : : * holding exclusive lock on the rel.
544 : : */
545 : 68 : FlushRelationBuffers(rel);
546 : :
547 : : /*
548 : : * Create and copy all forks of the relation, and schedule unlinking of
549 : : * old physical files.
550 : : *
551 : : * NOTE: any conflict in relfilenumber value will be caught in
552 : : * RelationCreateStorage().
553 : : */
554 : 68 : dstrel = RelationCreateStorage(*newrlocator, rel->rd_rel->relpersistence, true);
555 : :
556 : : /* copy main fork */
557 : 68 : RelationCopyStorage(RelationGetSmgr(rel), dstrel, MAIN_FORKNUM,
558 : 68 : rel->rd_rel->relpersistence);
559 : :
560 : : /* copy those extra forks that exist */
561 : 68 : for (ForkNumber forkNum = MAIN_FORKNUM + 1;
562 [ + + ]: 272 : forkNum <= MAX_FORKNUM; forkNum++)
563 : : {
564 [ + + ]: 204 : if (smgrexists(RelationGetSmgr(rel), forkNum))
565 : : {
566 : 23 : smgrcreate(dstrel, forkNum, false);
567 : :
568 : : /*
569 : : * WAL log creation if the relation is persistent, or this is the
570 : : * init fork of an unlogged relation.
571 : : */
572 [ + + ]: 23 : if (RelationIsPermanent(rel) ||
573 [ - + - - ]: 8 : (rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED &&
574 : : forkNum == INIT_FORKNUM))
575 : 15 : log_smgrcreate(newrlocator, forkNum);
576 : 23 : RelationCopyStorage(RelationGetSmgr(rel), dstrel, forkNum,
577 : 23 : rel->rd_rel->relpersistence);
578 : : }
579 : : }
580 : :
581 : :
582 : : /* drop old relation, and close new one */
583 : 68 : RelationDropStorage(rel);
584 : 68 : smgrclose(dstrel);
585 : 68 : }
586 : :
587 : : static void
588 : 414 : heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap,
589 : : Relation OldIndex, bool use_sort,
590 : : TransactionId OldestXmin,
591 : : Snapshot snapshot,
592 : : TransactionId *xid_cutoff,
593 : : MultiXactId *multi_cutoff,
594 : : double *num_tuples,
595 : : double *tups_vacuumed,
596 : : double *tups_recently_dead)
597 : : {
598 : : RewriteState rwstate;
599 : : BulkInsertState bistate;
600 : : IndexScanDesc indexScan;
601 : : TableScanDesc tableScan;
602 : : HeapScanDesc heapScan;
603 : : bool is_system_catalog;
604 : : Tuplesortstate *tuplesort;
605 : 414 : TupleDesc oldTupDesc = RelationGetDescr(OldHeap);
606 : 414 : TupleDesc newTupDesc = RelationGetDescr(NewHeap);
607 : : TupleTableSlot *slot;
608 : : int natts;
609 : : Datum *values;
610 : : bool *isnull;
611 : : BufferHeapTupleTableSlot *hslot;
612 : 414 : BlockNumber prev_cblock = InvalidBlockNumber;
613 : 414 : bool concurrent = snapshot != NULL;
614 : :
615 : : /* Remember if it's a system catalog */
616 : 414 : is_system_catalog = IsSystemRelation(OldHeap);
617 : :
618 : : /*
619 : : * Valid smgr_targblock implies something already wrote to the relation.
620 : : * This may be harmless, but this function hasn't planned for it.
621 : : */
622 : : Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber);
623 : :
624 : : /* Preallocate values/isnull arrays */
625 : 414 : natts = newTupDesc->natts;
626 : 414 : values = palloc_array(Datum, natts);
627 : 414 : isnull = palloc_array(bool, natts);
628 : :
629 : : /*
630 : : * In non-concurrent mode, initialize the rewrite operation. This is not
631 : : * needed in concurrent mode.
632 : : */
633 [ + + ]: 414 : if (!concurrent)
634 : 405 : rwstate = begin_heap_rewrite(OldHeap, NewHeap, OldestXmin,
635 : : *xid_cutoff, *multi_cutoff);
636 : : else
637 : 9 : rwstate = NULL;
638 : :
639 : : /* In concurrent mode, prepare for bulk-insert operation. */
640 [ + + ]: 414 : if (concurrent)
641 : 9 : bistate = GetBulkInsertState();
642 : : else
643 : 405 : bistate = NULL;
644 : :
645 : : /* Set up sorting if wanted */
646 [ + + ]: 414 : if (use_sort)
647 : 85 : tuplesort = tuplesort_begin_cluster(oldTupDesc, OldIndex,
648 : : maintenance_work_mem,
649 : : NULL, TUPLESORT_NONE);
650 : : else
651 : 329 : tuplesort = NULL;
652 : :
653 : : /*
654 : : * Prepare to scan the OldHeap. To ensure we see recently-dead tuples
655 : : * that still need to be copied, we scan with SnapshotAny and use
656 : : * HeapTupleSatisfiesVacuum for the visibility test.
657 : : *
658 : : * In the CONCURRENTLY case, we do regular MVCC visibility tests, using
659 : : * the snapshot passed by the caller.
660 : : */
661 [ + + + + ]: 414 : if (OldIndex != NULL && !use_sort)
662 : : {
663 : 62 : pgstat_progress_update_param(PROGRESS_REPACK_PHASE,
664 : : PROGRESS_REPACK_PHASE_INDEX_SCAN_HEAP);
665 : :
666 : 62 : tableScan = NULL;
667 : 62 : heapScan = NULL;
668 [ + + ]: 62 : indexScan = index_beginscan(OldHeap, OldIndex,
669 : : snapshot ? snapshot : SnapshotAny,
670 : : NULL, 0, 0,
671 : : SO_NONE);
672 : 62 : index_rescan(indexScan, NULL, 0, NULL, 0);
673 : : }
674 : : else
675 : : {
676 : : /* In scan-and-sort mode and also VACUUM FULL, set phase */
677 : 352 : pgstat_progress_update_param(PROGRESS_REPACK_PHASE,
678 : : PROGRESS_REPACK_PHASE_SEQ_SCAN_HEAP);
679 : :
680 [ + + ]: 352 : tableScan = table_beginscan(OldHeap,
681 : : snapshot ? snapshot : SnapshotAny,
682 : : 0, (ScanKey) NULL,
683 : : SO_NONE);
684 : 352 : heapScan = (HeapScanDesc) tableScan;
685 : 352 : indexScan = NULL;
686 : :
687 : : /* Set total heap blocks */
688 : 352 : pgstat_progress_update_param(PROGRESS_REPACK_TOTAL_HEAP_BLKS,
689 : 352 : heapScan->rs_nblocks);
690 : : }
691 : :
692 : 414 : slot = table_slot_create(OldHeap, NULL);
693 : 414 : hslot = (BufferHeapTupleTableSlot *) slot;
694 : :
695 : : /*
696 : : * Scan through the OldHeap, either in OldIndex order or sequentially;
697 : : * copy each tuple into the NewHeap, or transiently to the tuplesort
698 : : * module. Note that we don't bother sorting dead tuples (they won't get
699 : : * to the new table anyway).
700 : : */
701 : : for (;;)
702 : 472472 : {
703 : : HeapTuple tuple;
704 : : Buffer buf;
705 : : bool isdead;
706 : :
707 [ - + ]: 472886 : CHECK_FOR_INTERRUPTS();
708 : :
709 [ + + ]: 472886 : if (indexScan != NULL)
710 : : {
711 [ + + ]: 1204 : if (!index_getnext_slot(indexScan, ForwardScanDirection, slot))
712 : 62 : break;
713 : :
714 : : /* Since we used no scan keys, should never need to recheck */
715 [ - + ]: 1142 : if (indexScan->xs_recheck)
716 [ # # ]: 0 : elog(ERROR, "CLUSTER does not support lossy index conditions");
717 : : }
718 : : else
719 : : {
720 [ + + ]: 471682 : if (!table_scan_getnextslot(tableScan, ForwardScanDirection, slot))
721 : : {
722 : : /*
723 : : * If the last pages of the scan were empty, we would go to
724 : : * the next phase while heap_blks_scanned != heap_blks_total.
725 : : * Instead, to ensure that heap_blks_scanned is equivalent to
726 : : * heap_blks_total after the table scan phase, this parameter
727 : : * is manually updated to the correct value when the table
728 : : * scan finishes.
729 : : */
730 : 352 : pgstat_progress_update_param(PROGRESS_REPACK_HEAP_BLKS_SCANNED,
731 : 352 : heapScan->rs_nblocks);
732 : 352 : break;
733 : : }
734 : :
735 : : /*
736 : : * In scan-and-sort mode and also VACUUM FULL, set heap blocks
737 : : * scanned
738 : : *
739 : : * Note that heapScan may start at an offset and wrap around, i.e.
740 : : * rs_startblock may be >0, and rs_cblock may end with a number
741 : : * below rs_startblock. To prevent showing this wraparound to the
742 : : * user, we offset rs_cblock by rs_startblock (modulo rs_nblocks).
743 : : */
744 [ + + ]: 471330 : if (prev_cblock != heapScan->rs_cblock)
745 : : {
746 : 7015 : pgstat_progress_update_param(PROGRESS_REPACK_HEAP_BLKS_SCANNED,
747 : 7015 : (heapScan->rs_cblock +
748 : 7015 : heapScan->rs_nblocks -
749 : 7015 : heapScan->rs_startblock
750 : 7015 : ) % heapScan->rs_nblocks + 1);
751 : 7015 : prev_cblock = heapScan->rs_cblock;
752 : : }
753 : : }
754 : :
755 : 472472 : tuple = ExecFetchSlotHeapTuple(slot, false, NULL);
756 : 472472 : buf = hslot->buffer;
757 : :
758 : : /*
759 : : * In concurrent mode, our table or index scan has used regular MVCC
760 : : * visibility test against a snapshot passed by caller; therefore we
761 : : * don't need another visibility test. In non-concurrent mode
762 : : * however, we must test the visibility of each tuple we read.
763 : : */
764 [ + + ]: 472472 : if (!concurrent)
765 : : {
766 : : /*
767 : : * To be able to guarantee that we can set the hint bit, acquire
768 : : * an exclusive lock on the old buffer. We need the hint bits, set
769 : : * in heapam_relation_copy_for_cluster() ->
770 : : * HeapTupleSatisfiesVacuum(), to be set, as otherwise
771 : : * reform_and_rewrite_tuple() -> rewrite_heap_tuple() will get
772 : : * confused. Specifically, rewrite_heap_tuple() checks for
773 : : * HEAP_XMAX_INVALID in the old tuple to determine whether to
774 : : * check the old-to-new mapping hash table.
775 : : *
776 : : * It'd be better if we somehow could avoid setting hint bits on
777 : : * the old page. One reason to use VACUUM FULL are very bloated
778 : : * tables - rewriting most of the old table during VACUUM FULL
779 : : * doesn't exactly help...
780 : : */
781 : 472439 : LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
782 : :
783 [ + + + + : 472439 : switch (HeapTupleSatisfiesVacuum(tuple, OldestXmin, buf))
+ - ]
784 : : {
785 : 17324 : case HEAPTUPLE_DEAD:
786 : : /* Definitely dead */
787 : 17324 : isdead = true;
788 : 17324 : break;
789 : 10770 : case HEAPTUPLE_RECENTLY_DEAD:
790 : 10770 : *tups_recently_dead += 1;
791 : : pg_fallthrough;
792 : 454994 : case HEAPTUPLE_LIVE:
793 : : /* Live or recently dead, must copy it */
794 : 454994 : isdead = false;
795 : 454994 : break;
796 : 91 : case HEAPTUPLE_INSERT_IN_PROGRESS:
797 : :
798 : : /*
799 : : * As long as we hold exclusive lock on the relation,
800 : : * normally the only way to see this is if it was inserted
801 : : * earlier in our own transaction. However, it can happen
802 : : * in system catalogs, since we tend to release write lock
803 : : * before commit there. Give a warning if neither case
804 : : * applies; but in any case we had better copy it.
805 : : */
806 [ + + ]: 91 : if (!is_system_catalog &&
807 [ - + ]: 14 : !TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetXmin(tuple->t_data)))
808 [ # # ]: 0 : elog(WARNING, "concurrent insert in progress within table \"%s\"",
809 : : RelationGetRelationName(OldHeap));
810 : : /* treat as live */
811 : 91 : isdead = false;
812 : 91 : break;
813 : 30 : case HEAPTUPLE_DELETE_IN_PROGRESS:
814 : :
815 : : /*
816 : : * Similar situation to INSERT_IN_PROGRESS case.
817 : : */
818 [ + + ]: 30 : if (!is_system_catalog &&
819 [ - + ]: 20 : !TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetUpdateXid(tuple->t_data)))
820 [ # # ]: 0 : elog(WARNING, "concurrent delete in progress within table \"%s\"",
821 : : RelationGetRelationName(OldHeap));
822 : : /* treat as recently dead */
823 : 30 : *tups_recently_dead += 1;
824 : 30 : isdead = false;
825 : 30 : break;
826 : 0 : default:
827 [ # # ]: 0 : elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
828 : : isdead = false; /* keep compiler quiet */
829 : : break;
830 : : }
831 : :
832 : 472439 : LockBuffer(buf, BUFFER_LOCK_UNLOCK);
833 : :
834 [ + + ]: 472439 : if (isdead)
835 : : {
836 : 17324 : *tups_vacuumed += 1;
837 : : /* heap rewrite module still needs to see it... */
838 [ - + ]: 17324 : if (rewrite_heap_dead_tuple(rwstate, tuple))
839 : : {
840 : : /* A previous recently-dead tuple is now known dead */
841 : 0 : *tups_vacuumed += 1;
842 : 0 : *tups_recently_dead -= 1;
843 : : }
844 : :
845 : 17324 : continue;
846 : : }
847 : : }
848 : :
849 : 455148 : *num_tuples += 1;
850 [ + + ]: 455148 : if (tuplesort != NULL)
851 : : {
852 : 363172 : tuplesort_putheaptuple(tuplesort, tuple);
853 : :
854 : : /*
855 : : * In scan-and-sort mode, report increase in number of tuples
856 : : * scanned
857 : : */
858 : 363172 : pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_SCANNED,
859 : 363172 : *num_tuples);
860 : : }
861 : : else
862 : : {
863 : 91976 : const int ct_index[] = {
864 : : PROGRESS_REPACK_HEAP_TUPLES_SCANNED,
865 : : PROGRESS_REPACK_HEAP_TUPLES_INSERTED
866 : : };
867 : : int64 ct_val[2];
868 : :
869 [ + + ]: 91976 : if (!concurrent)
870 : 91943 : reform_and_rewrite_tuple(tuple, OldHeap, NewHeap,
871 : : values, isnull, rwstate);
872 : : else
873 : 33 : heap_insert_for_repack(tuple, OldHeap, NewHeap,
874 : : values, isnull, bistate);
875 : :
876 : : /*
877 : : * In indexscan mode and also VACUUM FULL, report increase in
878 : : * number of tuples scanned and written
879 : : */
880 : 91976 : ct_val[0] = *num_tuples;
881 : 91976 : ct_val[1] = *num_tuples;
882 : 91976 : pgstat_progress_update_multi_param(2, ct_index, ct_val);
883 : : }
884 : : }
885 : :
886 [ + + ]: 414 : if (indexScan != NULL)
887 : 62 : index_endscan(indexScan);
888 [ + + ]: 414 : if (tableScan != NULL)
889 : 352 : table_endscan(tableScan);
890 [ + - ]: 414 : if (slot)
891 : 414 : ExecDropSingleTupleTableSlot(slot);
892 : :
893 : : /*
894 : : * In scan-and-sort mode, complete the sort, then read out all live tuples
895 : : * from the tuplestore and write them to the new relation.
896 : : */
897 [ + + ]: 414 : if (tuplesort != NULL)
898 : : {
899 : 85 : double n_tuples = 0;
900 : :
901 : : /* Report that we are now sorting tuples */
902 : 85 : pgstat_progress_update_param(PROGRESS_REPACK_PHASE,
903 : : PROGRESS_REPACK_PHASE_SORT_TUPLES);
904 : :
905 : 85 : tuplesort_performsort(tuplesort);
906 : :
907 : : /* Report that we are now writing new heap */
908 : 85 : pgstat_progress_update_param(PROGRESS_REPACK_PHASE,
909 : : PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP);
910 : :
911 : : for (;;)
912 : 363172 : {
913 : : HeapTuple tuple;
914 : :
915 [ - + ]: 363257 : CHECK_FOR_INTERRUPTS();
916 : :
917 : 363257 : tuple = tuplesort_getheaptuple(tuplesort, true);
918 [ + + ]: 363257 : if (tuple == NULL)
919 : 85 : break;
920 : :
921 : 363172 : n_tuples += 1;
922 [ + - ]: 363172 : if (!concurrent)
923 : 363172 : reform_and_rewrite_tuple(tuple,
924 : : OldHeap, NewHeap,
925 : : values, isnull,
926 : : rwstate);
927 : : else
928 : 0 : heap_insert_for_repack(tuple, OldHeap, NewHeap,
929 : : values, isnull, bistate);
930 : :
931 : : /* Report n_tuples */
932 : 363172 : pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED,
933 : : n_tuples);
934 : : }
935 : :
936 : 85 : tuplesort_end(tuplesort);
937 : : }
938 : :
939 : : /* Write out any remaining tuples, and fsync if needed */
940 [ + + ]: 414 : if (rwstate)
941 : 405 : end_heap_rewrite(rwstate);
942 [ + + ]: 414 : if (bistate)
943 : 9 : FreeBulkInsertState(bistate);
944 : :
945 : : /* Clean up */
946 : 414 : pfree(values);
947 : 414 : pfree(isnull);
948 : 414 : }
949 : :
950 : : /*
951 : : * Prepare to analyze the next block in the read stream. Returns false if
952 : : * the stream is exhausted and true otherwise. The scan must have been started
953 : : * with SO_TYPE_ANALYZE option.
954 : : *
955 : : * This routine holds a buffer pin and lock on the heap page. They are held
956 : : * until heapam_scan_analyze_next_tuple() returns false. That is until all the
957 : : * items of the heap page are analyzed.
958 : : */
959 : : static bool
960 : 103959 : heapam_scan_analyze_next_block(TableScanDesc scan, ReadStream *stream)
961 : : {
962 : 103959 : HeapScanDesc hscan = (HeapScanDesc) scan;
963 : :
964 : : /*
965 : : * We must maintain a pin on the target page's buffer to ensure that
966 : : * concurrent activity - e.g. HOT pruning - doesn't delete tuples out from
967 : : * under us. It comes from the stream already pinned. We also choose to
968 : : * hold sharelock on the buffer throughout --- we could release and
969 : : * re-acquire sharelock for each tuple, but since we aren't doing much
970 : : * work per tuple, the extra lock traffic is probably better avoided.
971 : : */
972 : 103959 : hscan->rs_cbuf = read_stream_next_buffer(stream, NULL);
973 [ + + ]: 103959 : if (!BufferIsValid(hscan->rs_cbuf))
974 : 10945 : return false;
975 : :
976 : 93014 : LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
977 : :
978 : 93014 : hscan->rs_cblock = BufferGetBlockNumber(hscan->rs_cbuf);
979 : 93014 : hscan->rs_cindex = FirstOffsetNumber;
980 : 93014 : return true;
981 : : }
982 : :
983 : : static bool
984 : 8032055 : heapam_scan_analyze_next_tuple(TableScanDesc scan,
985 : : double *liverows, double *deadrows,
986 : : TupleTableSlot *slot)
987 : : {
988 : 8032055 : HeapScanDesc hscan = (HeapScanDesc) scan;
989 : : Page targpage;
990 : : OffsetNumber maxoffset;
991 : : BufferHeapTupleTableSlot *hslot;
992 : :
993 : : Assert(TTS_IS_BUFFERTUPLE(slot));
994 : :
995 : 8032055 : hslot = (BufferHeapTupleTableSlot *) slot;
996 : 8032055 : targpage = BufferGetPage(hscan->rs_cbuf);
997 : 8032055 : maxoffset = PageGetMaxOffsetNumber(targpage);
998 : :
999 : : /* Inner loop over all tuples on the selected page */
1000 [ + + ]: 8362338 : for (; hscan->rs_cindex <= maxoffset; hscan->rs_cindex++)
1001 : : {
1002 : : ItemId itemid;
1003 : 8269324 : HeapTuple targtuple = &hslot->base.tupdata;
1004 : 8269324 : bool sample_it = false;
1005 : : TransactionId dead_after;
1006 : :
1007 : 8269324 : itemid = PageGetItemId(targpage, hscan->rs_cindex);
1008 : :
1009 : : /*
1010 : : * We ignore unused and redirect line pointers. DEAD line pointers
1011 : : * should be counted as dead, because we need vacuum to run to get rid
1012 : : * of them. Note that this rule agrees with the way that
1013 : : * heap_page_prune_and_freeze() counts things.
1014 : : */
1015 [ + + ]: 8269324 : if (!ItemIdIsNormal(itemid))
1016 : : {
1017 [ + + ]: 212853 : if (ItemIdIsDead(itemid))
1018 : 19719 : *deadrows += 1;
1019 : 212853 : continue;
1020 : : }
1021 : :
1022 : 8056471 : ItemPointerSet(&targtuple->t_self, hscan->rs_cblock, hscan->rs_cindex);
1023 : :
1024 : 8056471 : targtuple->t_tableOid = RelationGetRelid(scan->rs_rd);
1025 : 8056471 : targtuple->t_data = (HeapTupleHeader) PageGetItem(targpage, itemid);
1026 : 8056471 : targtuple->t_len = ItemIdGetLength(itemid);
1027 : :
1028 [ + + + + : 8056471 : switch (HeapTupleSatisfiesVacuumHorizon(targtuple,
- ]
1029 : : hscan->rs_cbuf,
1030 : : &dead_after))
1031 : : {
1032 : 7708017 : case HEAPTUPLE_LIVE:
1033 : 7708017 : sample_it = true;
1034 : 7708017 : *liverows += 1;
1035 : 7708017 : break;
1036 : :
1037 : 116071 : case HEAPTUPLE_DEAD:
1038 : : case HEAPTUPLE_RECENTLY_DEAD:
1039 : : /* Count dead and recently-dead rows */
1040 : 116071 : *deadrows += 1;
1041 : 116071 : break;
1042 : :
1043 : 231201 : case HEAPTUPLE_INSERT_IN_PROGRESS:
1044 : :
1045 : : /*
1046 : : * Insert-in-progress rows are not counted. We assume that
1047 : : * when the inserting transaction commits or aborts, it will
1048 : : * send a stats message to increment the proper count. This
1049 : : * works right only if that transaction ends after we finish
1050 : : * analyzing the table; if things happen in the other order,
1051 : : * its stats update will be overwritten by ours. However, the
1052 : : * error will be large only if the other transaction runs long
1053 : : * enough to insert many tuples, so assuming it will finish
1054 : : * after us is the safer option.
1055 : : *
1056 : : * A special case is that the inserting transaction might be
1057 : : * our own. In this case we should count and sample the row,
1058 : : * to accommodate users who load a table and analyze it in one
1059 : : * transaction. (pgstat_report_analyze has to adjust the
1060 : : * numbers we report to the cumulative stats system to make
1061 : : * this come out right.)
1062 : : */
1063 [ + + ]: 231201 : if (TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetXmin(targtuple->t_data)))
1064 : : {
1065 : 230986 : sample_it = true;
1066 : 230986 : *liverows += 1;
1067 : : }
1068 : 231201 : break;
1069 : :
1070 : 1182 : case HEAPTUPLE_DELETE_IN_PROGRESS:
1071 : :
1072 : : /*
1073 : : * We count and sample delete-in-progress rows the same as
1074 : : * live ones, so that the stats counters come out right if the
1075 : : * deleting transaction commits after us, per the same
1076 : : * reasoning given above.
1077 : : *
1078 : : * If the delete was done by our own transaction, however, we
1079 : : * must count the row as dead to make pgstat_report_analyze's
1080 : : * stats adjustments come out right. (Note: this works out
1081 : : * properly when the row was both inserted and deleted in our
1082 : : * xact.)
1083 : : *
1084 : : * The net effect of these choices is that we act as though an
1085 : : * IN_PROGRESS transaction hasn't happened yet, except if it
1086 : : * is our own transaction, which we assume has happened.
1087 : : *
1088 : : * This approach ensures that we behave sanely if we see both
1089 : : * the pre-image and post-image rows for a row being updated
1090 : : * by a concurrent transaction: we will sample the pre-image
1091 : : * but not the post-image. We also get sane results if the
1092 : : * concurrent transaction never commits.
1093 : : */
1094 [ + + ]: 1182 : if (TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetUpdateXid(targtuple->t_data)))
1095 : 1144 : *deadrows += 1;
1096 : : else
1097 : : {
1098 : 38 : sample_it = true;
1099 : 38 : *liverows += 1;
1100 : : }
1101 : 1182 : break;
1102 : :
1103 : 0 : default:
1104 [ # # ]: 0 : elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
1105 : : break;
1106 : : }
1107 : :
1108 [ + + ]: 8056471 : if (sample_it)
1109 : : {
1110 : 7939041 : ExecStoreBufferHeapTuple(targtuple, slot, hscan->rs_cbuf);
1111 : 7939041 : hscan->rs_cindex++;
1112 : :
1113 : : /* note that we leave the buffer locked here! */
1114 : 7939041 : return true;
1115 : : }
1116 : : }
1117 : :
1118 : : /* Now release the lock and pin on the page */
1119 : 93014 : UnlockReleaseBuffer(hscan->rs_cbuf);
1120 : 93014 : hscan->rs_cbuf = InvalidBuffer;
1121 : :
1122 : : /* also prevent old slot contents from having pin on page */
1123 : 93014 : ExecClearTuple(slot);
1124 : :
1125 : 93014 : return false;
1126 : : }
1127 : :
1128 : : static double
1129 : 35273 : heapam_index_build_range_scan(Relation heapRelation,
1130 : : Relation indexRelation,
1131 : : IndexInfo *indexInfo,
1132 : : bool allow_sync,
1133 : : bool anyvisible,
1134 : : bool progress,
1135 : : BlockNumber start_blockno,
1136 : : BlockNumber numblocks,
1137 : : IndexBuildCallback callback,
1138 : : void *callback_state,
1139 : : TableScanDesc scan)
1140 : : {
1141 : : HeapScanDesc hscan;
1142 : : bool is_system_catalog;
1143 : : bool checking_uniqueness;
1144 : : HeapTuple heapTuple;
1145 : : Datum values[INDEX_MAX_KEYS];
1146 : : bool isnull[INDEX_MAX_KEYS];
1147 : : double reltuples;
1148 : : ExprState *predicate;
1149 : : TupleTableSlot *slot;
1150 : : EState *estate;
1151 : : ExprContext *econtext;
1152 : : Snapshot snapshot;
1153 : 35273 : bool need_unregister_snapshot = false;
1154 : : TransactionId OldestXmin;
1155 : 35273 : BlockNumber previous_blkno = InvalidBlockNumber;
1156 : 35273 : BlockNumber root_blkno = InvalidBlockNumber;
1157 : : OffsetNumber root_offsets[MaxHeapTuplesPerPage];
1158 : :
1159 : : /*
1160 : : * sanity checks
1161 : : */
1162 : : Assert(OidIsValid(indexRelation->rd_rel->relam));
1163 : :
1164 : : /* Remember if it's a system catalog */
1165 : 35273 : is_system_catalog = IsSystemRelation(heapRelation);
1166 : :
1167 : : /* See whether we're verifying uniqueness/exclusion properties */
1168 [ + + ]: 44640 : checking_uniqueness = (indexInfo->ii_Unique ||
1169 [ + + ]: 9367 : indexInfo->ii_ExclusionOps != NULL);
1170 : :
1171 : : /*
1172 : : * "Any visible" mode is not compatible with uniqueness checks; make sure
1173 : : * only one of those is requested.
1174 : : */
1175 : : Assert(!(anyvisible && checking_uniqueness));
1176 : :
1177 : : /*
1178 : : * Need an EState for evaluation of index expressions and partial-index
1179 : : * predicates. Also a slot to hold the current tuple.
1180 : : */
1181 : 35273 : estate = CreateExecutorState();
1182 [ - + ]: 35273 : econtext = GetPerTupleExprContext(estate);
1183 : 35273 : slot = table_slot_create(heapRelation, NULL);
1184 : :
1185 : : /* Arrange for econtext's scan tuple to be the tuple under test */
1186 : 35273 : econtext->ecxt_scantuple = slot;
1187 : :
1188 : : /* Set up execution state for predicate, if any. */
1189 : 35273 : predicate = ExecPrepareQual(indexInfo->ii_Predicate, estate);
1190 : :
1191 : : /*
1192 : : * Prepare for scan of the base relation. In a normal index build, we use
1193 : : * SnapshotAny because we must retrieve all tuples and do our own time
1194 : : * qual checks (because we have to index RECENTLY_DEAD tuples). In a
1195 : : * concurrent build, or during bootstrap, we take a regular MVCC snapshot
1196 : : * and index whatever's live according to that.
1197 : : */
1198 : 35273 : OldestXmin = InvalidTransactionId;
1199 : :
1200 : : /* okay to ignore lazy VACUUMs here */
1201 [ + + + + ]: 35273 : if (!IsBootstrapProcessingMode() && !indexInfo->ii_Concurrent)
1202 : 25643 : OldestXmin = GetOldestNonRemovableTransactionId(heapRelation);
1203 : :
1204 [ + + ]: 35273 : if (!scan)
1205 : : {
1206 : : /*
1207 : : * Serial index build.
1208 : : *
1209 : : * Must begin our own heap scan in this case. We may also need to
1210 : : * register a snapshot whose lifetime is under our direct control.
1211 : : */
1212 [ + + ]: 34909 : if (!TransactionIdIsValid(OldestXmin))
1213 : : {
1214 : 9537 : snapshot = RegisterSnapshot(GetTransactionSnapshot());
1215 : 9537 : need_unregister_snapshot = true;
1216 : : }
1217 : : else
1218 : 25372 : snapshot = SnapshotAny;
1219 : :
1220 : 34909 : scan = table_beginscan_strat(heapRelation, /* relation */
1221 : : snapshot, /* snapshot */
1222 : : 0, /* number of keys */
1223 : : NULL, /* scan key */
1224 : : true, /* buffer access strategy OK */
1225 : : allow_sync); /* syncscan OK? */
1226 : : }
1227 : : else
1228 : : {
1229 : : /*
1230 : : * Parallel index build.
1231 : : *
1232 : : * Parallel case never registers/unregisters own snapshot. Snapshot
1233 : : * is taken from parallel heap scan, and is SnapshotAny or an MVCC
1234 : : * snapshot, based on same criteria as serial case.
1235 : : */
1236 : : Assert(!IsBootstrapProcessingMode());
1237 : : Assert(allow_sync);
1238 : 364 : snapshot = scan->rs_snapshot;
1239 : : }
1240 : :
1241 : 35273 : hscan = (HeapScanDesc) scan;
1242 : :
1243 : : /*
1244 : : * Must have called GetOldestNonRemovableTransactionId() if using
1245 : : * SnapshotAny. Shouldn't have for an MVCC snapshot. (It's especially
1246 : : * worth checking this for parallel builds, since ambuild routines that
1247 : : * support parallel builds must work these details out for themselves.)
1248 : : */
1249 : : Assert(snapshot == SnapshotAny || IsMVCCSnapshot(snapshot));
1250 : : Assert(snapshot == SnapshotAny ? TransactionIdIsValid(OldestXmin) :
1251 : : !TransactionIdIsValid(OldestXmin));
1252 : : Assert(snapshot == SnapshotAny || !anyvisible);
1253 : :
1254 : : /* Publish number of blocks to scan */
1255 [ + + ]: 35273 : if (progress)
1256 : : {
1257 : : BlockNumber nblocks;
1258 : :
1259 [ + + ]: 33548 : if (hscan->rs_base.rs_parallel != NULL)
1260 : : {
1261 : : ParallelBlockTableScanDesc pbscan;
1262 : :
1263 : 137 : pbscan = (ParallelBlockTableScanDesc) hscan->rs_base.rs_parallel;
1264 : 137 : nblocks = pbscan->phs_nblocks;
1265 : : }
1266 : : else
1267 : 33411 : nblocks = hscan->rs_nblocks;
1268 : :
1269 : 33548 : pgstat_progress_update_param(PROGRESS_SCAN_BLOCKS_TOTAL,
1270 : : nblocks);
1271 : : }
1272 : :
1273 : : /* set our scan endpoints */
1274 [ + + ]: 35273 : if (!allow_sync)
1275 : 1929 : heap_setscanlimits(scan, start_blockno, numblocks);
1276 : : else
1277 : : {
1278 : : /* syncscan can only be requested on whole relation */
1279 : : Assert(start_blockno == 0);
1280 : : Assert(numblocks == InvalidBlockNumber);
1281 : : }
1282 : :
1283 : 35273 : reltuples = 0;
1284 : :
1285 : : /*
1286 : : * Scan all tuples in the base relation.
1287 : : */
1288 [ + + ]: 11460675 : while ((heapTuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1289 : : {
1290 : : bool tupleIsAlive;
1291 : :
1292 [ + + ]: 11425424 : CHECK_FOR_INTERRUPTS();
1293 : :
1294 : : /* Report scan progress, if asked to. */
1295 [ + + ]: 11425424 : if (progress)
1296 : : {
1297 : 9345600 : BlockNumber blocks_done = heapam_scan_get_blocks_done(hscan);
1298 : :
1299 [ + + ]: 9345600 : if (blocks_done != previous_blkno)
1300 : : {
1301 : 118590 : pgstat_progress_update_param(PROGRESS_SCAN_BLOCKS_DONE,
1302 : : blocks_done);
1303 : 118590 : previous_blkno = blocks_done;
1304 : : }
1305 : : }
1306 : :
1307 : : /*
1308 : : * When dealing with a HOT-chain of updated tuples, we want to index
1309 : : * the values of the live tuple (if any), but index it under the TID
1310 : : * of the chain's root tuple. This approach is necessary to preserve
1311 : : * the HOT-chain structure in the heap. So we need to be able to find
1312 : : * the root item offset for every tuple that's in a HOT-chain. When
1313 : : * first reaching a new page of the relation, call
1314 : : * heap_get_root_tuples() to build a map of root item offsets on the
1315 : : * page.
1316 : : *
1317 : : * It might look unsafe to use this information across buffer
1318 : : * lock/unlock. However, we hold ShareLock on the table so no
1319 : : * ordinary insert/update/delete should occur; and we hold pin on the
1320 : : * buffer continuously while visiting the page, so no pruning
1321 : : * operation can occur either.
1322 : : *
1323 : : * In cases with only ShareUpdateExclusiveLock on the table, it's
1324 : : * possible for some HOT tuples to appear that we didn't know about
1325 : : * when we first read the page. To handle that case, we re-obtain the
1326 : : * list of root offsets when a HOT tuple points to a root item that we
1327 : : * don't know about.
1328 : : *
1329 : : * Also, although our opinions about tuple liveness could change while
1330 : : * we scan the page (due to concurrent transaction commits/aborts),
1331 : : * the chain root locations won't, so this info doesn't need to be
1332 : : * rebuilt after waiting for another transaction.
1333 : : *
1334 : : * Note the implied assumption that there is no more than one live
1335 : : * tuple per HOT-chain --- else we could create more than one index
1336 : : * entry pointing to the same root tuple.
1337 : : */
1338 [ + + ]: 11425424 : if (hscan->rs_cblock != root_blkno)
1339 : : {
1340 : 136751 : Page page = BufferGetPage(hscan->rs_cbuf);
1341 : :
1342 : 136751 : LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
1343 : 136751 : heap_get_root_tuples(page, root_offsets);
1344 : 136751 : LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
1345 : :
1346 : 136751 : root_blkno = hscan->rs_cblock;
1347 : : }
1348 : :
1349 [ + + ]: 11425424 : if (snapshot == SnapshotAny)
1350 : : {
1351 : : /* do our own time qual check */
1352 : : bool indexIt;
1353 : : TransactionId xwait;
1354 : :
1355 : 8879875 : recheck:
1356 : :
1357 : : /*
1358 : : * We could possibly get away with not locking the buffer here,
1359 : : * since caller should hold ShareLock on the relation, but let's
1360 : : * be conservative about it. (This remark is still correct even
1361 : : * with HOT-pruning: our pin on the buffer prevents pruning.)
1362 : : */
1363 : 8879875 : LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
1364 : :
1365 : : /*
1366 : : * The criteria for counting a tuple as live in this block need to
1367 : : * match what analyze.c's heapam_scan_analyze_next_tuple() does,
1368 : : * otherwise CREATE INDEX and ANALYZE may produce wildly different
1369 : : * reltuples values, e.g. when there are many recently-dead
1370 : : * tuples.
1371 : : */
1372 [ + + + + : 8879875 : switch (HeapTupleSatisfiesVacuum(heapTuple, OldestXmin,
+ - ]
1373 : : hscan->rs_cbuf))
1374 : : {
1375 : 1227 : case HEAPTUPLE_DEAD:
1376 : : /* Definitely dead, we can ignore it */
1377 : 1227 : indexIt = false;
1378 : 1227 : tupleIsAlive = false;
1379 : 1227 : break;
1380 : 6838655 : case HEAPTUPLE_LIVE:
1381 : : /* Normal case, index and unique-check it */
1382 : 6838655 : indexIt = true;
1383 : 6838655 : tupleIsAlive = true;
1384 : : /* Count it as live, too */
1385 : 6838655 : reltuples += 1;
1386 : 6838655 : break;
1387 : 154011 : case HEAPTUPLE_RECENTLY_DEAD:
1388 : :
1389 : : /*
1390 : : * If tuple is recently deleted then we must index it
1391 : : * anyway to preserve MVCC semantics. (Pre-existing
1392 : : * transactions could try to use the index after we finish
1393 : : * building it, and may need to see such tuples.)
1394 : : *
1395 : : * However, if it was HOT-updated then we must only index
1396 : : * the live tuple at the end of the HOT-chain. Since this
1397 : : * breaks semantics for pre-existing snapshots, mark the
1398 : : * index as unusable for them.
1399 : : *
1400 : : * We don't count recently-dead tuples in reltuples, even
1401 : : * if we index them; see heapam_scan_analyze_next_tuple().
1402 : : */
1403 [ + + ]: 154011 : if (HeapTupleIsHotUpdated(heapTuple))
1404 : : {
1405 : 40 : indexIt = false;
1406 : : /* mark the index as unsafe for old snapshots */
1407 : 40 : indexInfo->ii_BrokenHotChain = true;
1408 : : }
1409 : : else
1410 : 153971 : indexIt = true;
1411 : : /* In any case, exclude the tuple from unique-checking */
1412 : 154011 : tupleIsAlive = false;
1413 : 154011 : break;
1414 : 1885932 : case HEAPTUPLE_INSERT_IN_PROGRESS:
1415 : :
1416 : : /*
1417 : : * In "anyvisible" mode, this tuple is visible and we
1418 : : * don't need any further checks.
1419 : : */
1420 [ + + ]: 1885932 : if (anyvisible)
1421 : : {
1422 : 30736 : indexIt = true;
1423 : 30736 : tupleIsAlive = true;
1424 : 30736 : reltuples += 1;
1425 : 30736 : break;
1426 : : }
1427 : :
1428 : : /*
1429 : : * Since caller should hold ShareLock or better, normally
1430 : : * the only way to see this is if it was inserted earlier
1431 : : * in our own transaction. However, it can happen in
1432 : : * system catalogs, since we tend to release write lock
1433 : : * before commit there. Give a warning if neither case
1434 : : * applies.
1435 : : */
1436 : 1855196 : xwait = HeapTupleHeaderGetXmin(heapTuple->t_data);
1437 [ + + ]: 1855196 : if (!TransactionIdIsCurrentTransactionId(xwait))
1438 : : {
1439 [ - + ]: 36 : if (!is_system_catalog)
1440 [ # # ]: 0 : elog(WARNING, "concurrent insert in progress within table \"%s\"",
1441 : : RelationGetRelationName(heapRelation));
1442 : :
1443 : : /*
1444 : : * If we are performing uniqueness checks, indexing
1445 : : * such a tuple could lead to a bogus uniqueness
1446 : : * failure. In that case we wait for the inserting
1447 : : * transaction to finish and check again.
1448 : : */
1449 [ - + ]: 36 : if (checking_uniqueness)
1450 : : {
1451 : : /*
1452 : : * Must drop the lock on the buffer before we wait
1453 : : */
1454 : 0 : LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
1455 : 0 : XactLockTableWait(xwait, heapRelation,
1456 : 0 : &heapTuple->t_self,
1457 : : XLTW_InsertIndexUnique);
1458 [ # # ]: 0 : CHECK_FOR_INTERRUPTS();
1459 : 0 : goto recheck;
1460 : : }
1461 : : }
1462 : : else
1463 : : {
1464 : : /*
1465 : : * For consistency with
1466 : : * heapam_scan_analyze_next_tuple(), count
1467 : : * HEAPTUPLE_INSERT_IN_PROGRESS tuples as live only
1468 : : * when inserted by our own transaction.
1469 : : */
1470 : 1855160 : reltuples += 1;
1471 : : }
1472 : :
1473 : : /*
1474 : : * We must index such tuples, since if the index build
1475 : : * commits then they're good.
1476 : : */
1477 : 1855196 : indexIt = true;
1478 : 1855196 : tupleIsAlive = true;
1479 : 1855196 : break;
1480 : 50 : case HEAPTUPLE_DELETE_IN_PROGRESS:
1481 : :
1482 : : /*
1483 : : * As with INSERT_IN_PROGRESS case, this is unexpected
1484 : : * unless it's our own deletion or a system catalog; but
1485 : : * in anyvisible mode, this tuple is visible.
1486 : : */
1487 [ - + ]: 50 : if (anyvisible)
1488 : : {
1489 : 0 : indexIt = true;
1490 : 0 : tupleIsAlive = false;
1491 : 0 : reltuples += 1;
1492 : 0 : break;
1493 : : }
1494 : :
1495 : 50 : xwait = HeapTupleHeaderGetUpdateXid(heapTuple->t_data);
1496 [ + + ]: 50 : if (!TransactionIdIsCurrentTransactionId(xwait))
1497 : : {
1498 [ - + ]: 6 : if (!is_system_catalog)
1499 [ # # ]: 0 : elog(WARNING, "concurrent delete in progress within table \"%s\"",
1500 : : RelationGetRelationName(heapRelation));
1501 : :
1502 : : /*
1503 : : * If we are performing uniqueness checks, assuming
1504 : : * the tuple is dead could lead to missing a
1505 : : * uniqueness violation. In that case we wait for the
1506 : : * deleting transaction to finish and check again.
1507 : : *
1508 : : * Also, if it's a HOT-updated tuple, we should not
1509 : : * index it but rather the live tuple at the end of
1510 : : * the HOT-chain. However, the deleting transaction
1511 : : * could abort, possibly leaving this tuple as live
1512 : : * after all, in which case it has to be indexed. The
1513 : : * only way to know what to do is to wait for the
1514 : : * deleting transaction to finish and check again.
1515 : : */
1516 [ + - - + ]: 12 : if (checking_uniqueness ||
1517 : 6 : HeapTupleIsHotUpdated(heapTuple))
1518 : : {
1519 : : /*
1520 : : * Must drop the lock on the buffer before we wait
1521 : : */
1522 : 0 : LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
1523 : 0 : XactLockTableWait(xwait, heapRelation,
1524 : 0 : &heapTuple->t_self,
1525 : : XLTW_InsertIndexUnique);
1526 [ # # ]: 0 : CHECK_FOR_INTERRUPTS();
1527 : 0 : goto recheck;
1528 : : }
1529 : :
1530 : : /*
1531 : : * Otherwise index it but don't check for uniqueness,
1532 : : * the same as a RECENTLY_DEAD tuple.
1533 : : */
1534 : 6 : indexIt = true;
1535 : :
1536 : : /*
1537 : : * Count HEAPTUPLE_DELETE_IN_PROGRESS tuples as live,
1538 : : * if they were not deleted by the current
1539 : : * transaction. That's what
1540 : : * heapam_scan_analyze_next_tuple() does, and we want
1541 : : * the behavior to be consistent.
1542 : : */
1543 : 6 : reltuples += 1;
1544 : : }
1545 [ - + ]: 44 : else if (HeapTupleIsHotUpdated(heapTuple))
1546 : : {
1547 : : /*
1548 : : * It's a HOT-updated tuple deleted by our own xact.
1549 : : * We can assume the deletion will commit (else the
1550 : : * index contents don't matter), so treat the same as
1551 : : * RECENTLY_DEAD HOT-updated tuples.
1552 : : */
1553 : 0 : indexIt = false;
1554 : : /* mark the index as unsafe for old snapshots */
1555 : 0 : indexInfo->ii_BrokenHotChain = true;
1556 : : }
1557 : : else
1558 : : {
1559 : : /*
1560 : : * It's a regular tuple deleted by our own xact. Index
1561 : : * it, but don't check for uniqueness nor count in
1562 : : * reltuples, the same as a RECENTLY_DEAD tuple.
1563 : : */
1564 : 44 : indexIt = true;
1565 : : }
1566 : : /* In any case, exclude the tuple from unique-checking */
1567 : 50 : tupleIsAlive = false;
1568 : 50 : break;
1569 : 0 : default:
1570 [ # # ]: 0 : elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
1571 : : indexIt = tupleIsAlive = false; /* keep compiler quiet */
1572 : : break;
1573 : : }
1574 : :
1575 : 8879875 : LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
1576 : :
1577 [ + + ]: 8879875 : if (!indexIt)
1578 : 1267 : continue;
1579 : : }
1580 : : else
1581 : : {
1582 : : /* heap_getnext did the time qual check */
1583 : 2545549 : tupleIsAlive = true;
1584 : 2545549 : reltuples += 1;
1585 : : }
1586 : :
1587 : 11424157 : MemoryContextReset(econtext->ecxt_per_tuple_memory);
1588 : :
1589 : : /* Set up for predicate or expression evaluation */
1590 : 11424157 : ExecStoreBufferHeapTuple(heapTuple, slot, hscan->rs_cbuf);
1591 : :
1592 : : /*
1593 : : * In a partial index, discard tuples that don't satisfy the
1594 : : * predicate.
1595 : : */
1596 [ + + ]: 11424157 : if (predicate != NULL)
1597 : : {
1598 [ + + ]: 131212 : if (!ExecQual(predicate, econtext))
1599 : 72185 : continue;
1600 : : }
1601 : :
1602 : : /*
1603 : : * For the current heap tuple, extract all the attributes we use in
1604 : : * this index, and note which are null. This also performs evaluation
1605 : : * of any expressions needed.
1606 : : */
1607 : 11351972 : FormIndexDatum(indexInfo,
1608 : : slot,
1609 : : estate,
1610 : : values,
1611 : : isnull);
1612 : :
1613 : : /*
1614 : : * You'd think we should go ahead and build the index tuple here, but
1615 : : * some index AMs want to do further processing on the data first. So
1616 : : * pass the values[] and isnull[] arrays, instead.
1617 : : */
1618 : :
1619 [ + + ]: 11351950 : if (HeapTupleIsHeapOnly(heapTuple))
1620 : : {
1621 : : /*
1622 : : * For a heap-only tuple, pretend its TID is that of the root. See
1623 : : * src/backend/access/heap/README.HOT for discussion.
1624 : : */
1625 : : ItemPointerData tid;
1626 : : OffsetNumber offnum;
1627 : :
1628 : 3417 : offnum = ItemPointerGetOffsetNumber(&heapTuple->t_self);
1629 : :
1630 : : /*
1631 : : * If a HOT tuple points to a root that we don't know about,
1632 : : * obtain root items afresh. If that still fails, report it as
1633 : : * corruption.
1634 : : */
1635 [ - + ]: 3417 : if (root_offsets[offnum - 1] == InvalidOffsetNumber)
1636 : : {
1637 : 0 : Page page = BufferGetPage(hscan->rs_cbuf);
1638 : :
1639 : 0 : LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
1640 : 0 : heap_get_root_tuples(page, root_offsets);
1641 : 0 : LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
1642 : : }
1643 : :
1644 [ + - + - : 3417 : if (!OffsetNumberIsValid(root_offsets[offnum - 1]))
- + ]
1645 [ # # ]: 0 : ereport(ERROR,
1646 : : (errcode(ERRCODE_DATA_CORRUPTED),
1647 : : errmsg_internal("failed to find parent tuple for heap-only tuple at (%u,%u) in table \"%s\"",
1648 : : ItemPointerGetBlockNumber(&heapTuple->t_self),
1649 : : offnum,
1650 : : RelationGetRelationName(heapRelation))));
1651 : :
1652 : 3417 : ItemPointerSet(&tid, ItemPointerGetBlockNumber(&heapTuple->t_self),
1653 : 3417 : root_offsets[offnum - 1]);
1654 : :
1655 : : /* Call the AM's callback routine to process the tuple */
1656 : 3417 : callback(indexRelation, &tid, values, isnull, tupleIsAlive,
1657 : : callback_state);
1658 : : }
1659 : : else
1660 : : {
1661 : : /* Call the AM's callback routine to process the tuple */
1662 : 11348533 : callback(indexRelation, &heapTuple->t_self, values, isnull,
1663 : : tupleIsAlive, callback_state);
1664 : : }
1665 : : }
1666 : :
1667 : : /* Report scan progress one last time. */
1668 [ + + ]: 35251 : if (progress)
1669 : : {
1670 : : BlockNumber blks_done;
1671 : :
1672 [ + + ]: 33526 : if (hscan->rs_base.rs_parallel != NULL)
1673 : : {
1674 : : ParallelBlockTableScanDesc pbscan;
1675 : :
1676 : 137 : pbscan = (ParallelBlockTableScanDesc) hscan->rs_base.rs_parallel;
1677 : 137 : blks_done = pbscan->phs_nblocks;
1678 : : }
1679 : : else
1680 : 33389 : blks_done = hscan->rs_nblocks;
1681 : :
1682 : 33526 : pgstat_progress_update_param(PROGRESS_SCAN_BLOCKS_DONE,
1683 : : blks_done);
1684 : : }
1685 : :
1686 : 35251 : table_endscan(scan);
1687 : :
1688 : : /* we can now forget our snapshot, if set and registered by us */
1689 [ + + ]: 35251 : if (need_unregister_snapshot)
1690 : 9519 : UnregisterSnapshot(snapshot);
1691 : :
1692 : 35251 : ExecDropSingleTupleTableSlot(slot);
1693 : :
1694 : 35251 : FreeExecutorState(estate);
1695 : :
1696 : : /* These may have been pointing to the now-gone estate */
1697 : 35251 : indexInfo->ii_ExpressionsState = NIL;
1698 : 35251 : indexInfo->ii_PredicateState = NULL;
1699 : :
1700 : 35251 : return reltuples;
1701 : : }
1702 : :
1703 : : static void
1704 : 447 : heapam_index_validate_scan(Relation heapRelation,
1705 : : Relation indexRelation,
1706 : : IndexInfo *indexInfo,
1707 : : Snapshot snapshot,
1708 : : ValidateIndexState *state)
1709 : : {
1710 : : TableScanDesc scan;
1711 : : HeapScanDesc hscan;
1712 : : HeapTuple heapTuple;
1713 : : Datum values[INDEX_MAX_KEYS];
1714 : : bool isnull[INDEX_MAX_KEYS];
1715 : : ExprState *predicate;
1716 : : TupleTableSlot *slot;
1717 : : EState *estate;
1718 : : ExprContext *econtext;
1719 : 447 : BlockNumber root_blkno = InvalidBlockNumber;
1720 : : OffsetNumber root_offsets[MaxHeapTuplesPerPage];
1721 : : bool in_index[MaxHeapTuplesPerPage];
1722 : 447 : BlockNumber previous_blkno = InvalidBlockNumber;
1723 : :
1724 : : /* state variables for the merge */
1725 : 447 : ItemPointer indexcursor = NULL;
1726 : : ItemPointerData decoded;
1727 : 447 : bool tuplesort_empty = false;
1728 : :
1729 : : /*
1730 : : * sanity checks
1731 : : */
1732 : : Assert(OidIsValid(indexRelation->rd_rel->relam));
1733 : :
1734 : : /*
1735 : : * Need an EState for evaluation of index expressions and partial-index
1736 : : * predicates. Also a slot to hold the current tuple.
1737 : : */
1738 : 447 : estate = CreateExecutorState();
1739 [ - + ]: 447 : econtext = GetPerTupleExprContext(estate);
1740 : 447 : slot = MakeSingleTupleTableSlot(RelationGetDescr(heapRelation),
1741 : : &TTSOpsHeapTuple);
1742 : :
1743 : : /* Arrange for econtext's scan tuple to be the tuple under test */
1744 : 447 : econtext->ecxt_scantuple = slot;
1745 : :
1746 : : /* Set up execution state for predicate, if any. */
1747 : 447 : predicate = ExecPrepareQual(indexInfo->ii_Predicate, estate);
1748 : :
1749 : : /*
1750 : : * Prepare for scan of the base relation. We need just those tuples
1751 : : * satisfying the passed-in reference snapshot. We must disable syncscan
1752 : : * here, because it's critical that we read from block zero forward to
1753 : : * match the sorted TIDs.
1754 : : */
1755 : 447 : scan = table_beginscan_strat(heapRelation, /* relation */
1756 : : snapshot, /* snapshot */
1757 : : 0, /* number of keys */
1758 : : NULL, /* scan key */
1759 : : true, /* buffer access strategy OK */
1760 : : false); /* syncscan not OK */
1761 : 447 : hscan = (HeapScanDesc) scan;
1762 : :
1763 : 447 : pgstat_progress_update_param(PROGRESS_SCAN_BLOCKS_TOTAL,
1764 : 447 : hscan->rs_nblocks);
1765 : :
1766 : : /*
1767 : : * Scan all tuples matching the snapshot.
1768 : : */
1769 [ + + ]: 131593 : while ((heapTuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1770 : : {
1771 : 131146 : ItemPointer heapcursor = &heapTuple->t_self;
1772 : : ItemPointerData rootTuple;
1773 : : OffsetNumber root_offnum;
1774 : :
1775 [ - + ]: 131146 : CHECK_FOR_INTERRUPTS();
1776 : :
1777 : 131146 : state->htups += 1;
1778 : :
1779 [ + + ]: 131146 : if ((previous_blkno == InvalidBlockNumber) ||
1780 [ + + ]: 130888 : (hscan->rs_cblock != previous_blkno))
1781 : : {
1782 : 2310 : pgstat_progress_update_param(PROGRESS_SCAN_BLOCKS_DONE,
1783 : 2310 : hscan->rs_cblock);
1784 : 2310 : previous_blkno = hscan->rs_cblock;
1785 : : }
1786 : :
1787 : : /*
1788 : : * As commented in table_index_build_scan, we should index heap-only
1789 : : * tuples under the TIDs of their root tuples; so when we advance onto
1790 : : * a new heap page, build a map of root item offsets on the page.
1791 : : *
1792 : : * This complicates merging against the tuplesort output: we will
1793 : : * visit the live tuples in order by their offsets, but the root
1794 : : * offsets that we need to compare against the index contents might be
1795 : : * ordered differently. So we might have to "look back" within the
1796 : : * tuplesort output, but only within the current page. We handle that
1797 : : * by keeping a bool array in_index[] showing all the
1798 : : * already-passed-over tuplesort output TIDs of the current page. We
1799 : : * clear that array here, when advancing onto a new heap page.
1800 : : */
1801 [ + + ]: 131146 : if (hscan->rs_cblock != root_blkno)
1802 : : {
1803 : 2310 : Page page = BufferGetPage(hscan->rs_cbuf);
1804 : :
1805 : 2310 : LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
1806 : 2310 : heap_get_root_tuples(page, root_offsets);
1807 : 2310 : LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
1808 : :
1809 : 2310 : memset(in_index, 0, sizeof(in_index));
1810 : :
1811 : 2310 : root_blkno = hscan->rs_cblock;
1812 : : }
1813 : :
1814 : : /* Convert actual tuple TID to root TID */
1815 : 131146 : rootTuple = *heapcursor;
1816 : 131146 : root_offnum = ItemPointerGetOffsetNumber(heapcursor);
1817 : :
1818 [ + + ]: 131146 : if (HeapTupleIsHeapOnly(heapTuple))
1819 : : {
1820 : 9 : root_offnum = root_offsets[root_offnum - 1];
1821 [ + - + - : 9 : if (!OffsetNumberIsValid(root_offnum))
- + ]
1822 [ # # ]: 0 : ereport(ERROR,
1823 : : (errcode(ERRCODE_DATA_CORRUPTED),
1824 : : errmsg_internal("failed to find parent tuple for heap-only tuple at (%u,%u) in table \"%s\"",
1825 : : ItemPointerGetBlockNumber(heapcursor),
1826 : : ItemPointerGetOffsetNumber(heapcursor),
1827 : : RelationGetRelationName(heapRelation))));
1828 : 9 : ItemPointerSetOffsetNumber(&rootTuple, root_offnum);
1829 : : }
1830 : :
1831 : : /*
1832 : : * "merge" by skipping through the index tuples until we find or pass
1833 : : * the current root tuple.
1834 : : */
1835 [ + + + + ]: 307200 : while (!tuplesort_empty &&
1836 [ + + ]: 306890 : (!indexcursor ||
1837 : 306890 : ItemPointerCompare(indexcursor, &rootTuple) < 0))
1838 : : {
1839 : : Datum ts_val;
1840 : : bool ts_isnull;
1841 : :
1842 [ + + ]: 176054 : if (indexcursor)
1843 : : {
1844 : : /*
1845 : : * Remember index items seen earlier on the current heap page
1846 : : */
1847 [ + + ]: 175796 : if (ItemPointerGetBlockNumber(indexcursor) == root_blkno)
1848 : 173167 : in_index[ItemPointerGetOffsetNumber(indexcursor) - 1] = true;
1849 : : }
1850 : :
1851 : 176054 : tuplesort_empty = !tuplesort_getdatum(state->tuplesort, true,
1852 : : false, &ts_val, &ts_isnull,
1853 : 176054 : NULL);
1854 : : Assert(tuplesort_empty || !ts_isnull);
1855 [ + + ]: 176054 : if (!tuplesort_empty)
1856 : : {
1857 : 176029 : itemptr_decode(&decoded, DatumGetInt64(ts_val));
1858 : 176029 : indexcursor = &decoded;
1859 : : }
1860 : : else
1861 : : {
1862 : : /* Be tidy */
1863 : 25 : indexcursor = NULL;
1864 : : }
1865 : : }
1866 : :
1867 : : /*
1868 : : * If the tuplesort has overshot *and* we didn't see a match earlier,
1869 : : * then this tuple is missing from the index, so insert it.
1870 : : */
1871 [ + + + + ]: 262240 : if ((tuplesort_empty ||
1872 : 131094 : ItemPointerCompare(indexcursor, &rootTuple) > 0) &&
1873 [ + + ]: 91 : !in_index[root_offnum - 1])
1874 : : {
1875 : 86 : MemoryContextReset(econtext->ecxt_per_tuple_memory);
1876 : :
1877 : : /* Set up for predicate or expression evaluation */
1878 : 86 : ExecStoreHeapTuple(heapTuple, slot, false);
1879 : :
1880 : : /*
1881 : : * In a partial index, discard tuples that don't satisfy the
1882 : : * predicate.
1883 : : */
1884 [ + + ]: 86 : if (predicate != NULL)
1885 : : {
1886 [ + - ]: 32 : if (!ExecQual(predicate, econtext))
1887 : 32 : continue;
1888 : : }
1889 : :
1890 : : /*
1891 : : * For the current heap tuple, extract all the attributes we use
1892 : : * in this index, and note which are null. This also performs
1893 : : * evaluation of any expressions needed.
1894 : : */
1895 : 54 : FormIndexDatum(indexInfo,
1896 : : slot,
1897 : : estate,
1898 : : values,
1899 : : isnull);
1900 : :
1901 : : /*
1902 : : * You'd think we should go ahead and build the index tuple here,
1903 : : * but some index AMs want to do further processing on the data
1904 : : * first. So pass the values[] and isnull[] arrays, instead.
1905 : : */
1906 : :
1907 : : /*
1908 : : * If the tuple is already committed dead, you might think we
1909 : : * could suppress uniqueness checking, but this is no longer true
1910 : : * in the presence of HOT, because the insert is actually a proxy
1911 : : * for a uniqueness check on the whole HOT-chain. That is, the
1912 : : * tuple we have here could be dead because it was already
1913 : : * HOT-updated, and if so the updating transaction will not have
1914 : : * thought it should insert index entries. The index AM will
1915 : : * check the whole HOT-chain and correctly detect a conflict if
1916 : : * there is one.
1917 : : */
1918 : :
1919 : 54 : index_insert(indexRelation,
1920 : : values,
1921 : : isnull,
1922 : : &rootTuple,
1923 : : heapRelation,
1924 : 54 : indexInfo->ii_Unique ?
1925 : : UNIQUE_CHECK_YES : UNIQUE_CHECK_NO,
1926 : : false,
1927 : : indexInfo);
1928 : :
1929 : 54 : state->tups_inserted += 1;
1930 : : }
1931 : : }
1932 : :
1933 : 447 : table_endscan(scan);
1934 : :
1935 : 447 : ExecDropSingleTupleTableSlot(slot);
1936 : :
1937 : 447 : FreeExecutorState(estate);
1938 : :
1939 : : /* These may have been pointing to the now-gone estate */
1940 : 447 : indexInfo->ii_ExpressionsState = NIL;
1941 : 447 : indexInfo->ii_PredicateState = NULL;
1942 : 447 : }
1943 : :
1944 : : /*
1945 : : * Return the number of blocks that have been read by this scan since
1946 : : * starting. This is meant for progress reporting rather than be fully
1947 : : * accurate: in a parallel scan, workers can be concurrently reading blocks
1948 : : * further ahead than what we report.
1949 : : */
1950 : : static BlockNumber
1951 : 9345600 : heapam_scan_get_blocks_done(HeapScanDesc hscan)
1952 : : {
1953 : 9345600 : ParallelBlockTableScanDesc bpscan = NULL;
1954 : : BlockNumber startblock;
1955 : : BlockNumber blocks_done;
1956 : :
1957 [ + + ]: 9345600 : if (hscan->rs_base.rs_parallel != NULL)
1958 : : {
1959 : 1170609 : bpscan = (ParallelBlockTableScanDesc) hscan->rs_base.rs_parallel;
1960 : 1170609 : startblock = pg_atomic_read_u32(&bpscan->phs_startblock);
1961 : : }
1962 : : else
1963 : 8174991 : startblock = hscan->rs_startblock;
1964 : :
1965 : : /*
1966 : : * Might have wrapped around the end of the relation, if startblock was
1967 : : * not zero.
1968 : : */
1969 [ + + ]: 9345600 : if (hscan->rs_cblock > startblock)
1970 : 9000485 : blocks_done = hscan->rs_cblock - startblock;
1971 : : else
1972 : : {
1973 : : BlockNumber nblocks;
1974 : :
1975 [ + + ]: 345115 : nblocks = bpscan != NULL ? bpscan->phs_nblocks : hscan->rs_nblocks;
1976 : 345115 : blocks_done = nblocks - startblock +
1977 : 345115 : hscan->rs_cblock;
1978 : : }
1979 : :
1980 : 9345600 : return blocks_done;
1981 : : }
1982 : :
1983 : :
1984 : : /* ------------------------------------------------------------------------
1985 : : * Miscellaneous callbacks for the heap AM
1986 : : * ------------------------------------------------------------------------
1987 : : */
1988 : :
1989 : : /*
1990 : : * Check to see whether the table needs a TOAST table. It does only if
1991 : : * (1) there are any toastable attributes, and (2) the maximum length
1992 : : * of a tuple could exceed TOAST_TUPLE_THRESHOLD. (We don't want to
1993 : : * create a toast table for something like "f1 varchar(20)".)
1994 : : */
1995 : : static bool
1996 : 30289 : heapam_relation_needs_toast_table(Relation rel)
1997 : : {
1998 : 30289 : int32 data_length = 0;
1999 : 30289 : bool maxlength_unknown = false;
2000 : 30289 : bool has_toastable_attrs = false;
2001 : 30289 : TupleDesc tupdesc = rel->rd_att;
2002 : : int32 tuple_length;
2003 : : int i;
2004 : :
2005 [ + + ]: 117631 : for (i = 0; i < tupdesc->natts; i++)
2006 : : {
2007 : 87342 : Form_pg_attribute att = TupleDescAttr(tupdesc, i);
2008 : :
2009 [ + + ]: 87342 : if (att->attisdropped)
2010 : 753 : continue;
2011 [ + + ]: 86589 : if (att->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
2012 : 677 : continue;
2013 : 85912 : data_length = att_align_nominal(data_length, att->attalign);
2014 [ + + ]: 85912 : if (att->attlen > 0)
2015 : : {
2016 : : /* Fixed-length types are never toastable */
2017 : 64447 : data_length += att->attlen;
2018 : : }
2019 : : else
2020 : : {
2021 : 21465 : int32 maxlen = type_maximum_size(att->atttypid,
2022 : : att->atttypmod);
2023 : :
2024 [ + + ]: 21465 : if (maxlen < 0)
2025 : 19980 : maxlength_unknown = true;
2026 : : else
2027 : 1485 : data_length += maxlen;
2028 [ + + ]: 21465 : if (att->attstorage != TYPSTORAGE_PLAIN)
2029 : 20769 : has_toastable_attrs = true;
2030 : : }
2031 : : }
2032 [ + + ]: 30289 : if (!has_toastable_attrs)
2033 : 18211 : return false; /* nothing to toast? */
2034 [ + + ]: 12078 : if (maxlength_unknown)
2035 : 10993 : return true; /* any unlimited-length attrs? */
2036 : 1085 : tuple_length = MAXALIGN(SizeofHeapTupleHeader +
2037 : 1085 : BITMAPLEN(tupdesc->natts)) +
2038 : 1085 : MAXALIGN(data_length);
2039 : 1085 : return (tuple_length > TOAST_TUPLE_THRESHOLD);
2040 : : }
2041 : :
2042 : : /*
2043 : : * TOAST tables for heap relations are just heap relations.
2044 : : */
2045 : : static Oid
2046 : 11283 : heapam_relation_toast_am(Relation rel)
2047 : : {
2048 : 11283 : return rel->rd_rel->relam;
2049 : : }
2050 : :
2051 : :
2052 : : /* ------------------------------------------------------------------------
2053 : : * Planner related callbacks for the heap AM
2054 : : * ------------------------------------------------------------------------
2055 : : */
2056 : :
2057 : : #define HEAP_OVERHEAD_BYTES_PER_TUPLE \
2058 : : (MAXALIGN(SizeofHeapTupleHeader) + sizeof(ItemIdData))
2059 : : #define HEAP_USABLE_BYTES_PER_PAGE \
2060 : : (BLCKSZ - SizeOfPageHeaderData)
2061 : :
2062 : : static void
2063 : 355728 : heapam_estimate_rel_size(Relation rel, int32 *attr_widths,
2064 : : BlockNumber *pages, double *tuples,
2065 : : double *allvisfrac)
2066 : : {
2067 : 355728 : table_block_relation_estimate_size(rel, attr_widths, pages,
2068 : : tuples, allvisfrac,
2069 : : HEAP_OVERHEAD_BYTES_PER_TUPLE,
2070 : : HEAP_USABLE_BYTES_PER_PAGE);
2071 : 355728 : }
2072 : :
2073 : :
2074 : : /* ------------------------------------------------------------------------
2075 : : * Executor related callbacks for the heap AM
2076 : : * ------------------------------------------------------------------------
2077 : : */
2078 : :
2079 : : static bool
2080 : 4132571 : heapam_scan_bitmap_next_tuple(TableScanDesc scan,
2081 : : TupleTableSlot *slot,
2082 : : bool *recheck,
2083 : : uint64 *lossy_pages,
2084 : : uint64 *exact_pages)
2085 : : {
2086 : 4132571 : BitmapHeapScanDesc bscan = (BitmapHeapScanDesc) scan;
2087 : 4132571 : HeapScanDesc hscan = (HeapScanDesc) bscan;
2088 : : OffsetNumber targoffset;
2089 : : Page page;
2090 : : ItemId lp;
2091 : :
2092 : : /*
2093 : : * Out of range? If so, nothing more to look at on this page
2094 : : */
2095 [ + + ]: 4377176 : while (hscan->rs_cindex >= hscan->rs_ntuples)
2096 : : {
2097 : : /*
2098 : : * Returns false if the bitmap is exhausted and there are no further
2099 : : * blocks we need to scan.
2100 : : */
2101 [ + + ]: 259551 : if (!BitmapHeapScanNextBlock(scan, recheck, lossy_pages, exact_pages))
2102 : 14943 : return false;
2103 : : }
2104 : :
2105 : 4117625 : targoffset = hscan->rs_vistuples[hscan->rs_cindex];
2106 : 4117625 : page = BufferGetPage(hscan->rs_cbuf);
2107 : 4117625 : lp = PageGetItemId(page, targoffset);
2108 : : Assert(ItemIdIsNormal(lp));
2109 : :
2110 : 4117625 : hscan->rs_ctup.t_data = (HeapTupleHeader) PageGetItem(page, lp);
2111 : 4117625 : hscan->rs_ctup.t_len = ItemIdGetLength(lp);
2112 : 4117625 : hscan->rs_ctup.t_tableOid = scan->rs_rd->rd_id;
2113 : 4117625 : ItemPointerSet(&hscan->rs_ctup.t_self, hscan->rs_cblock, targoffset);
2114 : :
2115 [ - + - - : 4117625 : pgstat_count_heap_fetch(scan->rs_rd);
+ - - + ]
2116 : :
2117 : : /*
2118 : : * Set up the result slot to point to this tuple. Note that the slot
2119 : : * acquires a pin on the buffer.
2120 : : */
2121 : 4117625 : ExecStoreBufferHeapTuple(&hscan->rs_ctup,
2122 : : slot,
2123 : : hscan->rs_cbuf);
2124 : :
2125 : 4117625 : hscan->rs_cindex++;
2126 : :
2127 : 4117625 : return true;
2128 : : }
2129 : :
2130 : : static bool
2131 : 8589 : heapam_scan_sample_next_block(TableScanDesc scan, SampleScanState *scanstate)
2132 : : {
2133 : 8589 : HeapScanDesc hscan = (HeapScanDesc) scan;
2134 : 8589 : TsmRoutine *tsm = scanstate->tsmroutine;
2135 : : BlockNumber blockno;
2136 : :
2137 : : /* return false immediately if relation is empty */
2138 [ - + ]: 8589 : if (hscan->rs_nblocks == 0)
2139 : 0 : return false;
2140 : :
2141 : : /* release previous scan buffer, if any */
2142 [ + + ]: 8589 : if (BufferIsValid(hscan->rs_cbuf))
2143 : : {
2144 : 8475 : ReleaseBuffer(hscan->rs_cbuf);
2145 : 8475 : hscan->rs_cbuf = InvalidBuffer;
2146 : : }
2147 : :
2148 [ + + ]: 8589 : if (tsm->NextSampleBlock)
2149 : 2945 : blockno = tsm->NextSampleBlock(scanstate, hscan->rs_nblocks);
2150 : : else
2151 : : {
2152 : : /* scanning table sequentially */
2153 : :
2154 [ + + ]: 5644 : if (hscan->rs_cblock == InvalidBlockNumber)
2155 : : {
2156 : : Assert(!hscan->rs_inited);
2157 : 52 : blockno = hscan->rs_startblock;
2158 : : }
2159 : : else
2160 : : {
2161 : : Assert(hscan->rs_inited);
2162 : :
2163 : 5592 : blockno = hscan->rs_cblock + 1;
2164 : :
2165 [ + + ]: 5592 : if (blockno >= hscan->rs_nblocks)
2166 : : {
2167 : : /* wrap to beginning of rel, might not have started at 0 */
2168 : 52 : blockno = 0;
2169 : : }
2170 : :
2171 : : /*
2172 : : * Report our new scan position for synchronization purposes.
2173 : : *
2174 : : * Note: we do this before checking for end of scan so that the
2175 : : * final state of the position hint is back at the start of the
2176 : : * rel. That's not strictly necessary, but otherwise when you run
2177 : : * the same query multiple times the starting position would shift
2178 : : * a little bit backwards on every invocation, which is confusing.
2179 : : * We don't guarantee any specific ordering in general, though.
2180 : : */
2181 [ - + ]: 5592 : if (scan->rs_flags & SO_ALLOW_SYNC)
2182 : 0 : ss_report_location(scan->rs_rd, blockno);
2183 : :
2184 [ + + ]: 5592 : if (blockno == hscan->rs_startblock)
2185 : : {
2186 : 52 : blockno = InvalidBlockNumber;
2187 : : }
2188 : : }
2189 : : }
2190 : :
2191 : 8589 : hscan->rs_cblock = blockno;
2192 : :
2193 [ + + ]: 8589 : if (!BlockNumberIsValid(blockno))
2194 : : {
2195 : 110 : hscan->rs_inited = false;
2196 : 110 : return false;
2197 : : }
2198 : :
2199 : : Assert(hscan->rs_cblock < hscan->rs_nblocks);
2200 : :
2201 : : /*
2202 : : * Be sure to check for interrupts at least once per page. Checks at
2203 : : * higher code levels won't be able to stop a sample scan that encounters
2204 : : * many pages' worth of consecutive dead tuples.
2205 : : */
2206 [ - + ]: 8479 : CHECK_FOR_INTERRUPTS();
2207 : :
2208 : : /* Read page using selected strategy */
2209 : 8479 : hscan->rs_cbuf = ReadBufferExtended(hscan->rs_base.rs_rd, MAIN_FORKNUM,
2210 : : blockno, RBM_NORMAL, hscan->rs_strategy);
2211 : :
2212 : : /* in pagemode, prune the page and determine visible tuple offsets */
2213 [ + + ]: 8479 : if (hscan->rs_base.rs_flags & SO_ALLOW_PAGEMODE)
2214 : 5687 : heap_prepare_pagescan(scan);
2215 : :
2216 : 8479 : hscan->rs_inited = true;
2217 : 8479 : return true;
2218 : : }
2219 : :
2220 : : static bool
2221 : 169183 : heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate,
2222 : : TupleTableSlot *slot)
2223 : : {
2224 : 169183 : HeapScanDesc hscan = (HeapScanDesc) scan;
2225 : 169183 : TsmRoutine *tsm = scanstate->tsmroutine;
2226 : 169183 : BlockNumber blockno = hscan->rs_cblock;
2227 : 169183 : bool pagemode = (scan->rs_flags & SO_ALLOW_PAGEMODE) != 0;
2228 : :
2229 : : Page page;
2230 : : bool all_visible;
2231 : : OffsetNumber maxoffset;
2232 : :
2233 : : /*
2234 : : * When not using pagemode, we must lock the buffer during tuple
2235 : : * visibility checks.
2236 : : */
2237 [ + + ]: 169183 : if (!pagemode)
2238 : 2796 : LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
2239 : :
2240 : 169183 : page = BufferGetPage(hscan->rs_cbuf);
2241 [ + + ]: 337843 : all_visible = PageIsAllVisible(page) &&
2242 [ + - ]: 168660 : !scan->rs_snapshot->takenDuringRecovery;
2243 : 169183 : maxoffset = PageGetMaxOffsetNumber(page);
2244 : :
2245 : : for (;;)
2246 : 0 : {
2247 : : OffsetNumber tupoffset;
2248 : :
2249 [ - + ]: 169183 : CHECK_FOR_INTERRUPTS();
2250 : :
2251 : : /* Ask the tablesample method which tuples to check on this page. */
2252 : 169183 : tupoffset = tsm->NextSampleTuple(scanstate,
2253 : : blockno,
2254 : : maxoffset);
2255 : :
2256 [ + + + - : 169183 : if (OffsetNumberIsValid(tupoffset))
+ + ]
2257 : : {
2258 : : ItemId itemid;
2259 : : bool visible;
2260 : 160708 : HeapTuple tuple = &(hscan->rs_ctup);
2261 : :
2262 : : /* Skip invalid tuple pointers. */
2263 : 160708 : itemid = PageGetItemId(page, tupoffset);
2264 [ - + ]: 160708 : if (!ItemIdIsNormal(itemid))
2265 : 0 : continue;
2266 : :
2267 : 160708 : tuple->t_data = (HeapTupleHeader) PageGetItem(page, itemid);
2268 : 160708 : tuple->t_len = ItemIdGetLength(itemid);
2269 : 160708 : ItemPointerSet(&(tuple->t_self), blockno, tupoffset);
2270 : :
2271 : :
2272 [ + + ]: 160708 : if (all_visible)
2273 : 160337 : visible = true;
2274 : : else
2275 : 371 : visible = SampleHeapTupleVisible(scan, hscan->rs_cbuf,
2276 : : tuple, tupoffset);
2277 : :
2278 : : /* in pagemode, heap_prepare_pagescan did this for us */
2279 [ + + ]: 160708 : if (!pagemode)
2280 : 4 : HeapCheckForSerializableConflictOut(visible, scan->rs_rd, tuple,
2281 : : hscan->rs_cbuf, scan->rs_snapshot);
2282 : :
2283 : : /* Try next tuple from same page. */
2284 [ - + ]: 160708 : if (!visible)
2285 : 0 : continue;
2286 : :
2287 : : /* Found visible tuple, return it. */
2288 [ + + ]: 160708 : if (!pagemode)
2289 : 4 : LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
2290 : :
2291 : 160708 : ExecStoreBufferHeapTuple(tuple, slot, hscan->rs_cbuf);
2292 : :
2293 : : /* Count successfully-fetched tuples as heap fetches */
2294 [ - + - - : 160708 : pgstat_count_heap_getnext(scan->rs_rd);
+ - ]
2295 : :
2296 : 160708 : return true;
2297 : : }
2298 : : else
2299 : : {
2300 : : /*
2301 : : * If we get here, it means we've exhausted the items on this page
2302 : : * and it's time to move to the next.
2303 : : */
2304 [ + + ]: 8475 : if (!pagemode)
2305 : 2792 : LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
2306 : :
2307 : 8475 : ExecClearTuple(slot);
2308 : 8475 : return false;
2309 : : }
2310 : : }
2311 : :
2312 : : Assert(0);
2313 : : }
2314 : :
2315 : :
2316 : : /* ----------------------------------------------------------------------------
2317 : : * Helper functions for the above.
2318 : : * ----------------------------------------------------------------------------
2319 : : */
2320 : :
2321 : : /*
2322 : : * Reconstruct and rewrite the given tuple
2323 : : *
2324 : : * We cannot simply copy the tuple as-is, for several reasons:
2325 : : *
2326 : : * 1. We'd like to squeeze out the values of any dropped columns, both
2327 : : * to save space and to ensure we have no corner-case failures. (It's
2328 : : * possible for example that the new table hasn't got a TOAST table
2329 : : * and so is unable to store any large values of dropped cols.)
2330 : : *
2331 : : * 2. The tuple might not even be legal for the new table; this is
2332 : : * currently only known to happen as an after-effect of ALTER TABLE
2333 : : * SET WITHOUT OIDS.
2334 : : *
2335 : : * So, we must reconstruct the tuple from component Datums.
2336 : : */
2337 : : static void
2338 : 455115 : reform_and_rewrite_tuple(HeapTuple tuple,
2339 : : Relation OldHeap, Relation NewHeap,
2340 : : Datum *values, bool *isnull, RewriteState rwstate)
2341 : : {
2342 : : HeapTuple newtuple;
2343 : :
2344 : 455115 : newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull);
2345 : :
2346 : : /* The heap rewrite module does the rest */
2347 : 455115 : rewrite_heap_tuple(rwstate, tuple, newtuple);
2348 : :
2349 : 455115 : heap_freetuple(newtuple);
2350 : 455115 : }
2351 : :
2352 : : /*
2353 : : * Insert tuple when processing REPACK CONCURRENTLY.
2354 : : *
2355 : : * rewriteheap.c is not used in the CONCURRENTLY case because it'd be
2356 : : * difficult to do the same in the catch-up phase (as the logical
2357 : : * decoding does not provide us with sufficient visibility
2358 : : * information). Thus we must use heap_insert() both during the
2359 : : * catch-up and here.
2360 : : *
2361 : : * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical
2362 : : * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops
2363 : : * this relation, so no logical replication subscription should need the data.
2364 : : *
2365 : : * BulkInsertState is used because many tuples are inserted in the typical
2366 : : * case.
2367 : : */
2368 : : static void
2369 : 33 : heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap,
2370 : : Datum *values, bool *isnull, BulkInsertState bistate)
2371 : : {
2372 : : HeapTuple newtuple;
2373 : :
2374 : 33 : newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull);
2375 : :
2376 : 33 : heap_insert(NewHeap, newtuple, GetCurrentCommandId(true),
2377 : : HEAP_INSERT_NO_LOGICAL, bistate);
2378 : :
2379 : 33 : heap_freetuple(newtuple);
2380 : 33 : }
2381 : :
2382 : : /*
2383 : : * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack.
2384 : : *
2385 : : * Deform the given tuple, set values of dropped columns to NULL, and fill in
2386 : : * any values from attmissingval; then form a new tuple and return it. If no
2387 : : * attributes need to be changed, a copy of the original tuple is returned.
2388 : : * Caller is responsible for freeing the returned tuple.
2389 : : *
2390 : : * XXX this coding assumes that both relations have the same tupledesc.
2391 : : */
2392 : : static HeapTuple
2393 : 455148 : reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap,
2394 : : Datum *values, bool *isnull)
2395 : : {
2396 : 455148 : TupleDesc oldTupDesc = RelationGetDescr(OldHeap);
2397 : 455148 : TupleDesc newTupDesc = RelationGetDescr(NewHeap);
2398 : 455148 : bool needs_reform = false;
2399 : :
2400 : : /*
2401 : : * A short tuple might require values from attmissing val, so activate the
2402 : : * coding unconditionally in that case. The value might legitimally be
2403 : : * NULL otherwise, so this is slightly wasteful, but it probably beats
2404 : : * having to test each attribute for presence of attmissingval each time.
2405 : : */
2406 [ + + ]: 455148 : if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts)
2407 : 41 : needs_reform = true;
2408 : :
2409 : : /*
2410 : : * If the column has been dropped but a value is still present, we can
2411 : : * optimize storage now by getting rid of it.
2412 : : */
2413 [ + + ]: 455148 : if (!needs_reform)
2414 : : {
2415 [ + + ]: 3880097 : for (int i = 0; i < newTupDesc->natts; i++)
2416 : : {
2417 [ + + ]: 3425000 : if (TupleDescCompactAttr(newTupDesc, i)->attisdropped &&
2418 [ + + ]: 30 : !heap_attisnull(tuple, i + 1, newTupDesc))
2419 : : {
2420 : 10 : needs_reform = true;
2421 : 10 : break;
2422 : : }
2423 : : }
2424 : : }
2425 : :
2426 : : /* Skip work if no changes are needed */
2427 [ + + ]: 455148 : if (!needs_reform)
2428 : 455097 : return heap_copytuple(tuple);
2429 : :
2430 : 51 : heap_deform_tuple(tuple, oldTupDesc, values, isnull);
2431 : :
2432 [ + + ]: 255 : for (int i = 0; i < newTupDesc->natts; i++)
2433 : : {
2434 [ + + ]: 204 : if (TupleDescCompactAttr(newTupDesc, i)->attisdropped)
2435 : 20 : isnull[i] = true;
2436 : : }
2437 : :
2438 : 51 : return heap_form_tuple(newTupDesc, values, isnull);
2439 : : }
2440 : :
2441 : : /*
2442 : : * Check visibility of the tuple.
2443 : : */
2444 : : static bool
2445 : 371 : SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer,
2446 : : HeapTuple tuple,
2447 : : OffsetNumber tupoffset)
2448 : : {
2449 : 371 : HeapScanDesc hscan = (HeapScanDesc) scan;
2450 : :
2451 [ + + ]: 371 : if (scan->rs_flags & SO_ALLOW_PAGEMODE)
2452 : : {
2453 : 367 : uint32 start = 0,
2454 : 367 : end = hscan->rs_ntuples;
2455 : :
2456 : : /*
2457 : : * In pageatatime mode, heap_prepare_pagescan() already did visibility
2458 : : * checks, so just look at the info it left in rs_vistuples[].
2459 : : *
2460 : : * We use a binary search over the known-sorted array. Note: we could
2461 : : * save some effort if we insisted that NextSampleTuple select tuples
2462 : : * in increasing order, but it's not clear that there would be enough
2463 : : * gain to justify the restriction.
2464 : : */
2465 [ + - ]: 685 : while (start < end)
2466 : : {
2467 : 685 : uint32 mid = start + (end - start) / 2;
2468 : 685 : OffsetNumber curoffset = hscan->rs_vistuples[mid];
2469 : :
2470 [ + + ]: 685 : if (tupoffset == curoffset)
2471 : 367 : return true;
2472 [ + + ]: 318 : else if (tupoffset < curoffset)
2473 : 166 : end = mid;
2474 : : else
2475 : 152 : start = mid + 1;
2476 : : }
2477 : :
2478 : 0 : return false;
2479 : : }
2480 : : else
2481 : : {
2482 : : /* Otherwise, we have to check the tuple individually. */
2483 : 4 : return HeapTupleSatisfiesVisibility(tuple, scan->rs_snapshot,
2484 : : buffer);
2485 : : }
2486 : : }
2487 : :
2488 : : /*
2489 : : * Helper function get the next block of a bitmap heap scan. Returns true when
2490 : : * it got the next block and saved it in the scan descriptor and false when
2491 : : * the bitmap and or relation are exhausted.
2492 : : */
2493 : : static bool
2494 : 259551 : BitmapHeapScanNextBlock(TableScanDesc scan,
2495 : : bool *recheck,
2496 : : uint64 *lossy_pages, uint64 *exact_pages)
2497 : : {
2498 : 259551 : BitmapHeapScanDesc bscan = (BitmapHeapScanDesc) scan;
2499 : 259551 : HeapScanDesc hscan = (HeapScanDesc) bscan;
2500 : : BlockNumber block;
2501 : : void *per_buffer_data;
2502 : : Buffer buffer;
2503 : : Snapshot snapshot;
2504 : : int ntup;
2505 : : TBMIterateResult *tbmres;
2506 : : OffsetNumber offsets[TBM_MAX_TUPLES_PER_PAGE];
2507 : 259551 : int noffsets = -1;
2508 : :
2509 : : Assert(scan->rs_flags & SO_TYPE_BITMAPSCAN);
2510 : : Assert(hscan->rs_read_stream);
2511 : :
2512 : 259551 : hscan->rs_cindex = 0;
2513 : 259551 : hscan->rs_ntuples = 0;
2514 : :
2515 : : /* Release buffer containing previous block. */
2516 [ + + ]: 259551 : if (BufferIsValid(hscan->rs_cbuf))
2517 : : {
2518 : 244335 : ReleaseBuffer(hscan->rs_cbuf);
2519 : 244335 : hscan->rs_cbuf = InvalidBuffer;
2520 : : }
2521 : :
2522 : 259551 : hscan->rs_cbuf = read_stream_next_buffer(hscan->rs_read_stream,
2523 : : &per_buffer_data);
2524 : :
2525 [ + + ]: 259551 : if (BufferIsInvalid(hscan->rs_cbuf))
2526 : : {
2527 : : /* the bitmap is exhausted */
2528 : 14943 : return false;
2529 : : }
2530 : :
2531 : : Assert(per_buffer_data);
2532 : :
2533 : 244608 : tbmres = per_buffer_data;
2534 : :
2535 : : Assert(BlockNumberIsValid(tbmres->blockno));
2536 : : Assert(BufferGetBlockNumber(hscan->rs_cbuf) == tbmres->blockno);
2537 : :
2538 : : /* Exact pages need their tuple offsets extracted. */
2539 [ + + ]: 244608 : if (!tbmres->lossy)
2540 : 137678 : noffsets = tbm_extract_page_tuple(tbmres, offsets,
2541 : : TBM_MAX_TUPLES_PER_PAGE);
2542 : :
2543 : 244608 : *recheck = tbmres->recheck;
2544 : :
2545 : 244608 : block = hscan->rs_cblock = tbmres->blockno;
2546 : 244608 : buffer = hscan->rs_cbuf;
2547 : 244608 : snapshot = scan->rs_snapshot;
2548 : :
2549 : 244608 : ntup = 0;
2550 : :
2551 : : /*
2552 : : * Prune and repair fragmentation for the whole page, if possible.
2553 : : */
2554 : 244608 : heap_page_prune_opt(scan->rs_rd, buffer, &hscan->rs_vmbuffer,
2555 : 244608 : scan->rs_flags & SO_HINT_REL_READ_ONLY);
2556 : :
2557 : : /*
2558 : : * We must hold share lock on the buffer content while examining tuple
2559 : : * visibility. Afterwards, however, the tuples we have found to be
2560 : : * visible are guaranteed good as long as we hold the buffer pin.
2561 : : */
2562 : 244608 : LockBuffer(buffer, BUFFER_LOCK_SHARE);
2563 : :
2564 : : /*
2565 : : * We need two separate strategies for lossy and non-lossy cases.
2566 : : */
2567 [ + + ]: 244608 : if (!tbmres->lossy)
2568 : : {
2569 : : /*
2570 : : * Bitmap is non-lossy, so we just look through the offsets listed in
2571 : : * tbmres; but we have to follow any HOT chain starting at each such
2572 : : * offset.
2573 : : */
2574 : : int curslot;
2575 : :
2576 : : /* We must have extracted the tuple offsets by now */
2577 : : Assert(noffsets > -1);
2578 : :
2579 [ + + ]: 3593244 : for (curslot = 0; curslot < noffsets; curslot++)
2580 : : {
2581 : 3455569 : OffsetNumber offnum = offsets[curslot];
2582 : : ItemPointerData tid;
2583 : : HeapTupleData heapTuple;
2584 : :
2585 : 3455569 : ItemPointerSet(&tid, block, offnum);
2586 [ + + ]: 3455569 : if (heap_hot_search_buffer(&tid, scan->rs_rd, buffer, snapshot,
2587 : : &heapTuple, NULL, true))
2588 : 3303259 : hscan->rs_vistuples[ntup++] = ItemPointerGetOffsetNumber(&tid);
2589 : : }
2590 : : }
2591 : : else
2592 : : {
2593 : : /*
2594 : : * Bitmap is lossy, so we must examine each line pointer on the page.
2595 : : * But we can ignore HOT chains, since we'll check each tuple anyway.
2596 : : */
2597 : 106930 : Page page = BufferGetPage(buffer);
2598 : 106930 : OffsetNumber maxoff = PageGetMaxOffsetNumber(page);
2599 : : OffsetNumber offnum;
2600 : :
2601 [ + + ]: 923170 : for (offnum = FirstOffsetNumber; offnum <= maxoff; offnum = OffsetNumberNext(offnum))
2602 : : {
2603 : : ItemId lp;
2604 : : HeapTupleData loctup;
2605 : : bool valid;
2606 : :
2607 : 816240 : lp = PageGetItemId(page, offnum);
2608 [ - + ]: 816240 : if (!ItemIdIsNormal(lp))
2609 : 0 : continue;
2610 : 816240 : loctup.t_data = (HeapTupleHeader) PageGetItem(page, lp);
2611 : 816240 : loctup.t_len = ItemIdGetLength(lp);
2612 : 816240 : loctup.t_tableOid = scan->rs_rd->rd_id;
2613 : 816240 : ItemPointerSet(&loctup.t_self, block, offnum);
2614 : 816240 : valid = HeapTupleSatisfiesVisibility(&loctup, snapshot, buffer);
2615 [ + + ]: 816240 : if (valid)
2616 : : {
2617 : 816156 : hscan->rs_vistuples[ntup++] = offnum;
2618 : 816156 : PredicateLockTID(scan->rs_rd, &loctup.t_self, snapshot,
2619 : 816156 : HeapTupleHeaderGetXmin(loctup.t_data));
2620 : : }
2621 : 816240 : HeapCheckForSerializableConflictOut(valid, scan->rs_rd, &loctup,
2622 : : buffer, snapshot);
2623 : : }
2624 : : }
2625 : :
2626 : 244605 : LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
2627 : :
2628 : : Assert(ntup <= MaxHeapTuplesPerPage);
2629 : 244605 : hscan->rs_ntuples = ntup;
2630 : :
2631 [ + + ]: 244605 : if (tbmres->lossy)
2632 : 106930 : (*lossy_pages)++;
2633 : : else
2634 : 137675 : (*exact_pages)++;
2635 : :
2636 : : /*
2637 : : * Return true to indicate that a valid block was found and the bitmap is
2638 : : * not exhausted. If there are no visible tuples on this page,
2639 : : * hscan->rs_ntuples will be 0 and heapam_scan_bitmap_next_tuple() will
2640 : : * return false returning control to this function to advance to the next
2641 : : * block in the bitmap.
2642 : : */
2643 : 244605 : return true;
2644 : : }
2645 : :
2646 : : /* ------------------------------------------------------------------------
2647 : : * Definition of the heap table access method.
2648 : : * ------------------------------------------------------------------------
2649 : : */
2650 : :
2651 : : static const TableAmRoutine heapam_methods = {
2652 : : .type = T_TableAmRoutine,
2653 : :
2654 : : .slot_callbacks = heapam_slot_callbacks,
2655 : :
2656 : : .scan_begin = heap_beginscan,
2657 : : .scan_end = heap_endscan,
2658 : : .scan_rescan = heap_rescan,
2659 : : .scan_getnextslot = heap_getnextslot,
2660 : :
2661 : : .scan_set_tidrange = heap_set_tidrange,
2662 : : .scan_getnextslot_tidrange = heap_getnextslot_tidrange,
2663 : :
2664 : : .parallelscan_estimate = table_block_parallelscan_estimate,
2665 : : .parallelscan_initialize = table_block_parallelscan_initialize,
2666 : : .parallelscan_reinitialize = table_block_parallelscan_reinitialize,
2667 : :
2668 : : .index_fetch_begin = heapam_index_fetch_begin,
2669 : : .index_fetch_reset = heapam_index_fetch_reset,
2670 : : .index_fetch_end = heapam_index_fetch_end,
2671 : : .index_fetch_tuple = heapam_index_fetch_tuple,
2672 : :
2673 : : .tuple_insert = heapam_tuple_insert,
2674 : : .tuple_insert_speculative = heapam_tuple_insert_speculative,
2675 : : .tuple_complete_speculative = heapam_tuple_complete_speculative,
2676 : : .multi_insert = heap_multi_insert,
2677 : : .tuple_delete = heapam_tuple_delete,
2678 : : .tuple_update = heapam_tuple_update,
2679 : : .tuple_lock = heapam_tuple_lock,
2680 : :
2681 : : .tuple_fetch_row_version = heapam_fetch_row_version,
2682 : : .tuple_get_latest_tid = heap_get_latest_tid,
2683 : : .tuple_tid_valid = heapam_tuple_tid_valid,
2684 : : .tuple_satisfies_snapshot = heapam_tuple_satisfies_snapshot,
2685 : : .index_delete_tuples = heap_index_delete_tuples,
2686 : :
2687 : : .relation_set_new_filelocator = heapam_relation_set_new_filelocator,
2688 : : .relation_nontransactional_truncate = heapam_relation_nontransactional_truncate,
2689 : : .relation_copy_data = heapam_relation_copy_data,
2690 : : .relation_copy_for_cluster = heapam_relation_copy_for_cluster,
2691 : : .relation_vacuum = heap_vacuum_rel,
2692 : : .scan_analyze_next_block = heapam_scan_analyze_next_block,
2693 : : .scan_analyze_next_tuple = heapam_scan_analyze_next_tuple,
2694 : : .index_build_range_scan = heapam_index_build_range_scan,
2695 : : .index_validate_scan = heapam_index_validate_scan,
2696 : :
2697 : : .relation_size = table_block_relation_size,
2698 : : .relation_needs_toast_table = heapam_relation_needs_toast_table,
2699 : : .relation_toast_am = heapam_relation_toast_am,
2700 : : .relation_fetch_toast_slice = heap_fetch_toast_slice,
2701 : :
2702 : : .relation_estimate_size = heapam_estimate_rel_size,
2703 : :
2704 : : .scan_bitmap_next_tuple = heapam_scan_bitmap_next_tuple,
2705 : : .scan_sample_next_block = heapam_scan_sample_next_block,
2706 : : .scan_sample_next_tuple = heapam_scan_sample_next_tuple
2707 : : };
2708 : :
2709 : :
2710 : : const TableAmRoutine *
2711 : 12624670 : GetHeapamTableAmRoutine(void)
2712 : : {
2713 : 12624670 : return &heapam_methods;
2714 : : }
2715 : :
2716 : : Datum
2717 : 1461722 : heap_tableam_handler(PG_FUNCTION_ARGS)
2718 : : {
2719 : 1461722 : PG_RETURN_POINTER(&heapam_methods);
2720 : : }
|