PostgreSQL Source Code  git master
heapam_handler.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/heaptoast.h"
#include "access/multixact.h"
#include "access/rewriteheap.h"
#include "access/tableam.h"
#include "access/tsmapi.h"
#include "access/xact.h"
#include "catalog/catalog.h"
#include "catalog/index.h"
#include "catalog/storage.h"
#include "catalog/storage_xlog.h"
#include "commands/progress.h"
#include "executor/executor.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "storage/bufmgr.h"
#include "storage/bufpage.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/procarray.h"
#include "storage/smgr.h"
#include "utils/builtins.h"
#include "utils/rel.h"
Include dependency graph for heapam_handler.c:

Go to the source code of this file.

Macros

#define HEAP_OVERHEAD_BYTES_PER_TUPLE   (MAXALIGN(SizeofHeapTupleHeader) + sizeof(ItemIdData))
 
#define HEAP_USABLE_BYTES_PER_PAGE   (BLCKSZ - SizeOfPageHeaderData)
 

Functions

static void reform_and_rewrite_tuple (HeapTuple tuple, Relation OldHeap, Relation NewHeap, Datum *values, bool *isnull, RewriteState rwstate)
 
static bool SampleHeapTupleVisible (TableScanDesc scan, Buffer buffer, HeapTuple tuple, OffsetNumber tupoffset)
 
static BlockNumber heapam_scan_get_blocks_done (HeapScanDesc hscan)
 
static const TupleTableSlotOpsheapam_slot_callbacks (Relation relation)
 
static IndexFetchTableDataheapam_index_fetch_begin (Relation rel)
 
static void heapam_index_fetch_reset (IndexFetchTableData *scan)
 
static void heapam_index_fetch_end (IndexFetchTableData *scan)
 
static bool heapam_index_fetch_tuple (struct IndexFetchTableData *scan, ItemPointer tid, Snapshot snapshot, TupleTableSlot *slot, bool *call_again, bool *all_dead)
 
static bool heapam_fetch_row_version (Relation relation, ItemPointer tid, Snapshot snapshot, TupleTableSlot *slot)
 
static bool heapam_tuple_tid_valid (TableScanDesc scan, ItemPointer tid)
 
static bool heapam_tuple_satisfies_snapshot (Relation rel, TupleTableSlot *slot, Snapshot snapshot)
 
static void heapam_tuple_insert (Relation relation, TupleTableSlot *slot, CommandId cid, int options, BulkInsertState bistate)
 
static void heapam_tuple_insert_speculative (Relation relation, TupleTableSlot *slot, CommandId cid, int options, BulkInsertState bistate, uint32 specToken)
 
static void heapam_tuple_complete_speculative (Relation relation, TupleTableSlot *slot, uint32 specToken, bool succeeded)
 
static TM_Result heapam_tuple_delete (Relation relation, ItemPointer tid, CommandId cid, Snapshot snapshot, Snapshot crosscheck, bool wait, TM_FailureData *tmfd, bool changingPart)
 
static TM_Result heapam_tuple_update (Relation relation, ItemPointer otid, TupleTableSlot *slot, CommandId cid, Snapshot snapshot, Snapshot crosscheck, bool wait, TM_FailureData *tmfd, LockTupleMode *lockmode, bool *update_indexes)
 
static TM_Result heapam_tuple_lock (Relation relation, ItemPointer tid, Snapshot snapshot, TupleTableSlot *slot, CommandId cid, LockTupleMode mode, LockWaitPolicy wait_policy, uint8 flags, TM_FailureData *tmfd)
 
static void heapam_finish_bulk_insert (Relation relation, int options)
 
static void heapam_relation_set_new_filenode (Relation rel, const RelFileNode *newrnode, char persistence, TransactionId *freezeXid, MultiXactId *minmulti)
 
static void heapam_relation_nontransactional_truncate (Relation rel)
 
static void heapam_relation_copy_data (Relation rel, const RelFileNode *newrnode)
 
static void heapam_relation_copy_for_cluster (Relation OldHeap, Relation NewHeap, Relation OldIndex, bool use_sort, TransactionId OldestXmin, TransactionId *xid_cutoff, MultiXactId *multi_cutoff, double *num_tuples, double *tups_vacuumed, double *tups_recently_dead)
 
static bool heapam_scan_analyze_next_block (TableScanDesc scan, BlockNumber blockno, BufferAccessStrategy bstrategy)
 
static bool heapam_scan_analyze_next_tuple (TableScanDesc scan, TransactionId OldestXmin, double *liverows, double *deadrows, TupleTableSlot *slot)
 
static double heapam_index_build_range_scan (Relation heapRelation, Relation indexRelation, IndexInfo *indexInfo, bool allow_sync, bool anyvisible, bool progress, BlockNumber start_blockno, BlockNumber numblocks, IndexBuildCallback callback, void *callback_state, TableScanDesc scan)
 
static void heapam_index_validate_scan (Relation heapRelation, Relation indexRelation, IndexInfo *indexInfo, Snapshot snapshot, ValidateIndexState *state)
 
static bool heapam_relation_needs_toast_table (Relation rel)
 
static void heapam_estimate_rel_size (Relation rel, int32 *attr_widths, BlockNumber *pages, double *tuples, double *allvisfrac)
 
static bool heapam_scan_bitmap_next_block (TableScanDesc scan, TBMIterateResult *tbmres)
 
static bool heapam_scan_bitmap_next_tuple (TableScanDesc scan, TBMIterateResult *tbmres, TupleTableSlot *slot)
 
static bool heapam_scan_sample_next_block (TableScanDesc scan, SampleScanState *scanstate)
 
static bool heapam_scan_sample_next_tuple (TableScanDesc scan, SampleScanState *scanstate, TupleTableSlot *slot)
 
const TableAmRoutineGetHeapamTableAmRoutine (void)
 
Datum heap_tableam_handler (PG_FUNCTION_ARGS)
 

Variables

static const TableAmRoutine heapam_methods
 

Macro Definition Documentation

◆ HEAP_OVERHEAD_BYTES_PER_TUPLE

#define HEAP_OVERHEAD_BYTES_PER_TUPLE   (MAXALIGN(SizeofHeapTupleHeader) + sizeof(ItemIdData))

Definition at line 2046 of file heapam_handler.c.

Referenced by heapam_estimate_rel_size().

◆ HEAP_USABLE_BYTES_PER_PAGE

#define HEAP_USABLE_BYTES_PER_PAGE   (BLCKSZ - SizeOfPageHeaderData)

Definition at line 2048 of file heapam_handler.c.

Referenced by heapam_estimate_rel_size().

Function Documentation

◆ GetHeapamTableAmRoutine()

const TableAmRoutine* GetHeapamTableAmRoutine ( void  )

Definition at line 2549 of file heapam_handler.c.

References heapam_methods.

Referenced by formrdesc(), heap_getnext(), and table_scan_sample_next_tuple().

2550 {
2551  return &heapam_methods;
2552 }
static const TableAmRoutine heapam_methods

◆ heap_tableam_handler()

Datum heap_tableam_handler ( PG_FUNCTION_ARGS  )

Definition at line 2555 of file heapam_handler.c.

References PG_RETURN_POINTER.

2556 {
2558 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
static const TableAmRoutine heapam_methods

◆ heapam_estimate_rel_size()

static void heapam_estimate_rel_size ( Relation  rel,
int32 attr_widths,
BlockNumber pages,
double *  tuples,
double *  allvisfrac 
)
static

Definition at line 2052 of file heapam_handler.c.

References HEAP_OVERHEAD_BYTES_PER_TUPLE, HEAP_USABLE_BYTES_PER_PAGE, and table_block_relation_estimate_size().

Referenced by SampleHeapTupleVisible().

2055 {
2056  table_block_relation_estimate_size(rel, attr_widths, pages,
2057  tuples, allvisfrac,
2060 }
#define HEAP_OVERHEAD_BYTES_PER_TUPLE
#define HEAP_USABLE_BYTES_PER_PAGE
void table_block_relation_estimate_size(Relation rel, int32 *attr_widths, BlockNumber *pages, double *tuples, double *allvisfrac, Size overhead_bytes_per_tuple, Size usable_bytes_per_page)
Definition: tableam.c:548

◆ heapam_fetch_row_version()

static bool heapam_fetch_row_version ( Relation  relation,
ItemPointer  tid,
Snapshot  snapshot,
TupleTableSlot slot 
)
static

Definition at line 179 of file heapam_handler.c.

References Assert, BufferHeapTupleTableSlot::base, ExecStorePinnedBufferHeapTuple(), heap_fetch(), RelationGetRelid, HeapTupleData::t_self, TTS_IS_BUFFERTUPLE, TupleTableSlot::tts_tableOid, and HeapTupleTableSlot::tupdata.

Referenced by SampleHeapTupleVisible().

183 {
185  Buffer buffer;
186 
187  Assert(TTS_IS_BUFFERTUPLE(slot));
188 
189  bslot->base.tupdata.t_self = *tid;
190  if (heap_fetch(relation, snapshot, &bslot->base.tupdata, &buffer))
191  {
192  /* store in slot, transferring existing pin */
193  ExecStorePinnedBufferHeapTuple(&bslot->base.tupdata, slot, buffer);
194  slot->tts_tableOid = RelationGetRelid(relation);
195 
196  return true;
197  }
198 
199  return false;
200 }
Oid tts_tableOid
Definition: tuptable.h:131
bool heap_fetch(Relation relation, Snapshot snapshot, HeapTuple tuple, Buffer *userbuf)
Definition: heapam.c:1412
ItemPointerData t_self
Definition: htup.h:65
#define TTS_IS_BUFFERTUPLE(slot)
Definition: tuptable.h:231
#define Assert(condition)
Definition: c.h:739
TupleTableSlot * ExecStorePinnedBufferHeapTuple(HeapTuple tuple, TupleTableSlot *slot, Buffer buffer)
Definition: execTuples.c:1388
HeapTupleTableSlot base
Definition: tuptable.h:259
HeapTupleData tupdata
Definition: tuptable.h:253
int Buffer
Definition: buf.h:23
#define RelationGetRelid(relation)
Definition: rel.h:422

◆ heapam_finish_bulk_insert()

static void heapam_finish_bulk_insert ( Relation  relation,
int  options 
)
static

Definition at line 559 of file heapam_handler.c.

References HEAP_INSERT_SKIP_WAL, and heap_sync().

Referenced by SampleHeapTupleVisible().

560 {
561  /*
562  * If we skipped writing WAL, then we need to sync the heap (but not
563  * indexes since those use WAL anyway / don't go through tableam)
564  */
566  heap_sync(relation);
567 }
void heap_sync(Relation rel)
Definition: heapam.c:8938
#define HEAP_INSERT_SKIP_WAL
Definition: heapam.h:32

◆ heapam_index_build_range_scan()

static double heapam_index_build_range_scan ( Relation  heapRelation,
Relation  indexRelation,
IndexInfo indexInfo,
bool  allow_sync,
bool  anyvisible,
bool  progress,
BlockNumber  start_blockno,
BlockNumber  numblocks,
IndexBuildCallback  callback,
void *  callback_state,
TableScanDesc  scan 
)
static

Definition at line 1146 of file heapam_handler.c.

References Assert, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferGetPage, callback(), CHECK_FOR_INTERRUPTS, CreateExecutorState(), ExprContext::ecxt_per_tuple_memory, ExprContext::ecxt_scantuple, elog, ereport, errcode(), ERRCODE_DATA_CORRUPTED, errmsg_internal(), ERROR, ExecDropSingleTupleTableSlot(), ExecPrepareQual(), ExecQual(), ExecStoreBufferHeapTuple(), FormIndexDatum(), ForwardScanDirection, FreeExecutorState(), GetOldestXmin(), GetPerTupleExprContext, GetTransactionSnapshot(), heap_get_root_tuples(), heap_getnext(), heap_setscanlimits(), heapam_scan_get_blocks_done(), HEAPTUPLE_DEAD, HEAPTUPLE_DELETE_IN_PROGRESS, HEAPTUPLE_INSERT_IN_PROGRESS, HEAPTUPLE_LIVE, HEAPTUPLE_RECENTLY_DEAD, HeapTupleHeaderGetUpdateXid, HeapTupleHeaderGetXmin, HeapTupleIsHeapOnly, HeapTupleIsHotUpdated, HeapTupleSatisfiesVacuum(), IndexInfo::ii_BrokenHotChain, IndexInfo::ii_Concurrent, IndexInfo::ii_ExclusionOps, IndexInfo::ii_ExpressionsState, IndexInfo::ii_Predicate, IndexInfo::ii_PredicateState, IndexInfo::ii_Unique, INDEX_MAX_KEYS, InvalidBlockNumber, InvalidTransactionId, IsBootstrapProcessingMode, IsMVCCSnapshot, IsSystemRelation(), ItemPointerGetBlockNumber, ItemPointerGetOffsetNumber, ItemPointerSet, LockBuffer(), MaxHeapTuplesPerPage, MemoryContextReset(), NIL, OffsetNumberIsValid, OidIsValid, OldestXmin, pgstat_progress_update_param(), ParallelBlockTableScanDescData::phs_nblocks, PROCARRAY_FLAGS_VACUUM, PROGRESS_SCAN_BLOCKS_DONE, PROGRESS_SCAN_BLOCKS_TOTAL, RelationData::rd_rel, RegisterSnapshot(), RelationGetRelationName, reltuples, HeapScanDescData::rs_base, HeapScanDescData::rs_cblock, HeapScanDescData::rs_cbuf, HeapScanDescData::rs_nblocks, TableScanDescData::rs_parallel, TableScanDescData::rs_snapshot, SnapshotAny, HeapTupleData::t_data, HeapTupleData::t_self, table_beginscan_strat(), table_endscan(), table_slot_create(), TransactionIdIsCurrentTransactionId(), TransactionIdIsValid, UnregisterSnapshot(), values, WARNING, XactLockTableWait(), and XLTW_InsertIndexUnique.

Referenced by SampleHeapTupleVisible().

1157 {
1158  HeapScanDesc hscan;
1159  bool is_system_catalog;
1160  bool checking_uniqueness;
1161  HeapTuple heapTuple;
1163  bool isnull[INDEX_MAX_KEYS];
1164  double reltuples;
1165  ExprState *predicate;
1166  TupleTableSlot *slot;
1167  EState *estate;
1168  ExprContext *econtext;
1169  Snapshot snapshot;
1170  bool need_unregister_snapshot = false;
1172  BlockNumber previous_blkno = InvalidBlockNumber;
1173  BlockNumber root_blkno = InvalidBlockNumber;
1174  OffsetNumber root_offsets[MaxHeapTuplesPerPage];
1175 
1176  /*
1177  * sanity checks
1178  */
1179  Assert(OidIsValid(indexRelation->rd_rel->relam));
1180 
1181  /* Remember if it's a system catalog */
1182  is_system_catalog = IsSystemRelation(heapRelation);
1183 
1184  /* See whether we're verifying uniqueness/exclusion properties */
1185  checking_uniqueness = (indexInfo->ii_Unique ||
1186  indexInfo->ii_ExclusionOps != NULL);
1187 
1188  /*
1189  * "Any visible" mode is not compatible with uniqueness checks; make sure
1190  * only one of those is requested.
1191  */
1192  Assert(!(anyvisible && checking_uniqueness));
1193 
1194  /*
1195  * Need an EState for evaluation of index expressions and partial-index
1196  * predicates. Also a slot to hold the current tuple.
1197  */
1198  estate = CreateExecutorState();
1199  econtext = GetPerTupleExprContext(estate);
1200  slot = table_slot_create(heapRelation, NULL);
1201 
1202  /* Arrange for econtext's scan tuple to be the tuple under test */
1203  econtext->ecxt_scantuple = slot;
1204 
1205  /* Set up execution state for predicate, if any. */
1206  predicate = ExecPrepareQual(indexInfo->ii_Predicate, estate);
1207 
1208  /*
1209  * Prepare for scan of the base relation. In a normal index build, we use
1210  * SnapshotAny because we must retrieve all tuples and do our own time
1211  * qual checks (because we have to index RECENTLY_DEAD tuples). In a
1212  * concurrent build, or during bootstrap, we take a regular MVCC snapshot
1213  * and index whatever's live according to that.
1214  */
1215  OldestXmin = InvalidTransactionId;
1216 
1217  /* okay to ignore lazy VACUUMs here */
1218  if (!IsBootstrapProcessingMode() && !indexInfo->ii_Concurrent)
1219  OldestXmin = GetOldestXmin(heapRelation, PROCARRAY_FLAGS_VACUUM);
1220 
1221  if (!scan)
1222  {
1223  /*
1224  * Serial index build.
1225  *
1226  * Must begin our own heap scan in this case. We may also need to
1227  * register a snapshot whose lifetime is under our direct control.
1228  */
1229  if (!TransactionIdIsValid(OldestXmin))
1230  {
1232  need_unregister_snapshot = true;
1233  }
1234  else
1235  snapshot = SnapshotAny;
1236 
1237  scan = table_beginscan_strat(heapRelation, /* relation */
1238  snapshot, /* snapshot */
1239  0, /* number of keys */
1240  NULL, /* scan key */
1241  true, /* buffer access strategy OK */
1242  allow_sync); /* syncscan OK? */
1243  }
1244  else
1245  {
1246  /*
1247  * Parallel index build.
1248  *
1249  * Parallel case never registers/unregisters own snapshot. Snapshot
1250  * is taken from parallel heap scan, and is SnapshotAny or an MVCC
1251  * snapshot, based on same criteria as serial case.
1252  */
1254  Assert(allow_sync);
1255  snapshot = scan->rs_snapshot;
1256  }
1257 
1258  hscan = (HeapScanDesc) scan;
1259 
1260  /* Publish number of blocks to scan */
1261  if (progress)
1262  {
1263  BlockNumber nblocks;
1264 
1265  if (hscan->rs_base.rs_parallel != NULL)
1266  {
1268 
1270  nblocks = pbscan->phs_nblocks;
1271  }
1272  else
1273  nblocks = hscan->rs_nblocks;
1274 
1276  nblocks);
1277  }
1278 
1279  /*
1280  * Must call GetOldestXmin() with SnapshotAny. Should never call
1281  * GetOldestXmin() with MVCC snapshot. (It's especially worth checking
1282  * this for parallel builds, since ambuild routines that support parallel
1283  * builds must work these details out for themselves.)
1284  */
1285  Assert(snapshot == SnapshotAny || IsMVCCSnapshot(snapshot));
1286  Assert(snapshot == SnapshotAny ? TransactionIdIsValid(OldestXmin) :
1287  !TransactionIdIsValid(OldestXmin));
1288  Assert(snapshot == SnapshotAny || !anyvisible);
1289 
1290  /* set our scan endpoints */
1291  if (!allow_sync)
1292  heap_setscanlimits(scan, start_blockno, numblocks);
1293  else
1294  {
1295  /* syncscan can only be requested on whole relation */
1296  Assert(start_blockno == 0);
1297  Assert(numblocks == InvalidBlockNumber);
1298  }
1299 
1300  reltuples = 0;
1301 
1302  /*
1303  * Scan all tuples in the base relation.
1304  */
1305  while ((heapTuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1306  {
1307  bool tupleIsAlive;
1308 
1310 
1311  /* Report scan progress, if asked to. */
1312  if (progress)
1313  {
1314  BlockNumber blocks_done = heapam_scan_get_blocks_done(hscan);
1315 
1316  if (blocks_done != previous_blkno)
1317  {
1319  blocks_done);
1320  previous_blkno = blocks_done;
1321  }
1322  }
1323 
1324  /*
1325  * When dealing with a HOT-chain of updated tuples, we want to index
1326  * the values of the live tuple (if any), but index it under the TID
1327  * of the chain's root tuple. This approach is necessary to preserve
1328  * the HOT-chain structure in the heap. So we need to be able to find
1329  * the root item offset for every tuple that's in a HOT-chain. When
1330  * first reaching a new page of the relation, call
1331  * heap_get_root_tuples() to build a map of root item offsets on the
1332  * page.
1333  *
1334  * It might look unsafe to use this information across buffer
1335  * lock/unlock. However, we hold ShareLock on the table so no
1336  * ordinary insert/update/delete should occur; and we hold pin on the
1337  * buffer continuously while visiting the page, so no pruning
1338  * operation can occur either.
1339  *
1340  * Also, although our opinions about tuple liveness could change while
1341  * we scan the page (due to concurrent transaction commits/aborts),
1342  * the chain root locations won't, so this info doesn't need to be
1343  * rebuilt after waiting for another transaction.
1344  *
1345  * Note the implied assumption that there is no more than one live
1346  * tuple per HOT-chain --- else we could create more than one index
1347  * entry pointing to the same root tuple.
1348  */
1349  if (hscan->rs_cblock != root_blkno)
1350  {
1351  Page page = BufferGetPage(hscan->rs_cbuf);
1352 
1354  heap_get_root_tuples(page, root_offsets);
1356 
1357  root_blkno = hscan->rs_cblock;
1358  }
1359 
1360  if (snapshot == SnapshotAny)
1361  {
1362  /* do our own time qual check */
1363  bool indexIt;
1364  TransactionId xwait;
1365 
1366  recheck:
1367 
1368  /*
1369  * We could possibly get away with not locking the buffer here,
1370  * since caller should hold ShareLock on the relation, but let's
1371  * be conservative about it. (This remark is still correct even
1372  * with HOT-pruning: our pin on the buffer prevents pruning.)
1373  */
1375 
1376  /*
1377  * The criteria for counting a tuple as live in this block need to
1378  * match what analyze.c's heapam_scan_analyze_next_tuple() does,
1379  * otherwise CREATE INDEX and ANALYZE may produce wildly different
1380  * reltuples values, e.g. when there are many recently-dead
1381  * tuples.
1382  */
1383  switch (HeapTupleSatisfiesVacuum(heapTuple, OldestXmin,
1384  hscan->rs_cbuf))
1385  {
1386  case HEAPTUPLE_DEAD:
1387  /* Definitely dead, we can ignore it */
1388  indexIt = false;
1389  tupleIsAlive = false;
1390  break;
1391  case HEAPTUPLE_LIVE:
1392  /* Normal case, index and unique-check it */
1393  indexIt = true;
1394  tupleIsAlive = true;
1395  /* Count it as live, too */
1396  reltuples += 1;
1397  break;
1399 
1400  /*
1401  * If tuple is recently deleted then we must index it
1402  * anyway to preserve MVCC semantics. (Pre-existing
1403  * transactions could try to use the index after we finish
1404  * building it, and may need to see such tuples.)
1405  *
1406  * However, if it was HOT-updated then we must only index
1407  * the live tuple at the end of the HOT-chain. Since this
1408  * breaks semantics for pre-existing snapshots, mark the
1409  * index as unusable for them.
1410  *
1411  * We don't count recently-dead tuples in reltuples, even
1412  * if we index them; see heapam_scan_analyze_next_tuple().
1413  */
1414  if (HeapTupleIsHotUpdated(heapTuple))
1415  {
1416  indexIt = false;
1417  /* mark the index as unsafe for old snapshots */
1418  indexInfo->ii_BrokenHotChain = true;
1419  }
1420  else
1421  indexIt = true;
1422  /* In any case, exclude the tuple from unique-checking */
1423  tupleIsAlive = false;
1424  break;
1426 
1427  /*
1428  * In "anyvisible" mode, this tuple is visible and we
1429  * don't need any further checks.
1430  */
1431  if (anyvisible)
1432  {
1433  indexIt = true;
1434  tupleIsAlive = true;
1435  reltuples += 1;
1436  break;
1437  }
1438 
1439  /*
1440  * Since caller should hold ShareLock or better, normally
1441  * the only way to see this is if it was inserted earlier
1442  * in our own transaction. However, it can happen in
1443  * system catalogs, since we tend to release write lock
1444  * before commit there. Give a warning if neither case
1445  * applies.
1446  */
1447  xwait = HeapTupleHeaderGetXmin(heapTuple->t_data);
1449  {
1450  if (!is_system_catalog)
1451  elog(WARNING, "concurrent insert in progress within table \"%s\"",
1452  RelationGetRelationName(heapRelation));
1453 
1454  /*
1455  * If we are performing uniqueness checks, indexing
1456  * such a tuple could lead to a bogus uniqueness
1457  * failure. In that case we wait for the inserting
1458  * transaction to finish and check again.
1459  */
1460  if (checking_uniqueness)
1461  {
1462  /*
1463  * Must drop the lock on the buffer before we wait
1464  */
1466  XactLockTableWait(xwait, heapRelation,
1467  &heapTuple->t_self,
1470  goto recheck;
1471  }
1472  }
1473  else
1474  {
1475  /*
1476  * For consistency with
1477  * heapam_scan_analyze_next_tuple(), count
1478  * HEAPTUPLE_INSERT_IN_PROGRESS tuples as live only
1479  * when inserted by our own transaction.
1480  */
1481  reltuples += 1;
1482  }
1483 
1484  /*
1485  * We must index such tuples, since if the index build
1486  * commits then they're good.
1487  */
1488  indexIt = true;
1489  tupleIsAlive = true;
1490  break;
1492 
1493  /*
1494  * As with INSERT_IN_PROGRESS case, this is unexpected
1495  * unless it's our own deletion or a system catalog; but
1496  * in anyvisible mode, this tuple is visible.
1497  */
1498  if (anyvisible)
1499  {
1500  indexIt = true;
1501  tupleIsAlive = false;
1502  reltuples += 1;
1503  break;
1504  }
1505 
1506  xwait = HeapTupleHeaderGetUpdateXid(heapTuple->t_data);
1508  {
1509  if (!is_system_catalog)
1510  elog(WARNING, "concurrent delete in progress within table \"%s\"",
1511  RelationGetRelationName(heapRelation));
1512 
1513  /*
1514  * If we are performing uniqueness checks, assuming
1515  * the tuple is dead could lead to missing a
1516  * uniqueness violation. In that case we wait for the
1517  * deleting transaction to finish and check again.
1518  *
1519  * Also, if it's a HOT-updated tuple, we should not
1520  * index it but rather the live tuple at the end of
1521  * the HOT-chain. However, the deleting transaction
1522  * could abort, possibly leaving this tuple as live
1523  * after all, in which case it has to be indexed. The
1524  * only way to know what to do is to wait for the
1525  * deleting transaction to finish and check again.
1526  */
1527  if (checking_uniqueness ||
1528  HeapTupleIsHotUpdated(heapTuple))
1529  {
1530  /*
1531  * Must drop the lock on the buffer before we wait
1532  */
1534  XactLockTableWait(xwait, heapRelation,
1535  &heapTuple->t_self,
1538  goto recheck;
1539  }
1540 
1541  /*
1542  * Otherwise index it but don't check for uniqueness,
1543  * the same as a RECENTLY_DEAD tuple.
1544  */
1545  indexIt = true;
1546 
1547  /*
1548  * Count HEAPTUPLE_DELETE_IN_PROGRESS tuples as live,
1549  * if they were not deleted by the current
1550  * transaction. That's what
1551  * heapam_scan_analyze_next_tuple() does, and we want
1552  * the behavior to be consistent.
1553  */
1554  reltuples += 1;
1555  }
1556  else if (HeapTupleIsHotUpdated(heapTuple))
1557  {
1558  /*
1559  * It's a HOT-updated tuple deleted by our own xact.
1560  * We can assume the deletion will commit (else the
1561  * index contents don't matter), so treat the same as
1562  * RECENTLY_DEAD HOT-updated tuples.
1563  */
1564  indexIt = false;
1565  /* mark the index as unsafe for old snapshots */
1566  indexInfo->ii_BrokenHotChain = true;
1567  }
1568  else
1569  {
1570  /*
1571  * It's a regular tuple deleted by our own xact. Index
1572  * it, but don't check for uniqueness nor count in
1573  * reltuples, the same as a RECENTLY_DEAD tuple.
1574  */
1575  indexIt = true;
1576  }
1577  /* In any case, exclude the tuple from unique-checking */
1578  tupleIsAlive = false;
1579  break;
1580  default:
1581  elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
1582  indexIt = tupleIsAlive = false; /* keep compiler quiet */
1583  break;
1584  }
1585 
1587 
1588  if (!indexIt)
1589  continue;
1590  }
1591  else
1592  {
1593  /* heap_getnext did the time qual check */
1594  tupleIsAlive = true;
1595  reltuples += 1;
1596  }
1597 
1599 
1600  /* Set up for predicate or expression evaluation */
1601  ExecStoreBufferHeapTuple(heapTuple, slot, hscan->rs_cbuf);
1602 
1603  /*
1604  * In a partial index, discard tuples that don't satisfy the
1605  * predicate.
1606  */
1607  if (predicate != NULL)
1608  {
1609  if (!ExecQual(predicate, econtext))
1610  continue;
1611  }
1612 
1613  /*
1614  * For the current heap tuple, extract all the attributes we use in
1615  * this index, and note which are null. This also performs evaluation
1616  * of any expressions needed.
1617  */
1618  FormIndexDatum(indexInfo,
1619  slot,
1620  estate,
1621  values,
1622  isnull);
1623 
1624  /*
1625  * You'd think we should go ahead and build the index tuple here, but
1626  * some index AMs want to do further processing on the data first. So
1627  * pass the values[] and isnull[] arrays, instead.
1628  */
1629 
1630  if (HeapTupleIsHeapOnly(heapTuple))
1631  {
1632  /*
1633  * For a heap-only tuple, pretend its TID is that of the root. See
1634  * src/backend/access/heap/README.HOT for discussion.
1635  */
1636  ItemPointerData tid;
1637  OffsetNumber offnum;
1638 
1639  offnum = ItemPointerGetOffsetNumber(&heapTuple->t_self);
1640 
1641  if (!OffsetNumberIsValid(root_offsets[offnum - 1]))
1642  ereport(ERROR,
1644  errmsg_internal("failed to find parent tuple for heap-only tuple at (%u,%u) in table \"%s\"",
1645  ItemPointerGetBlockNumber(&heapTuple->t_self),
1646  offnum,
1647  RelationGetRelationName(heapRelation))));
1648 
1649  ItemPointerSet(&tid, ItemPointerGetBlockNumber(&heapTuple->t_self),
1650  root_offsets[offnum - 1]);
1651 
1652  /* Call the AM's callback routine to process the tuple */
1653  callback(indexRelation, &tid, values, isnull, tupleIsAlive,
1654  callback_state);
1655  }
1656  else
1657  {
1658  /* Call the AM's callback routine to process the tuple */
1659  callback(indexRelation, &heapTuple->t_self, values, isnull,
1660  tupleIsAlive, callback_state);
1661  }
1662  }
1663 
1664  /* Report scan progress one last time. */
1665  if (progress)
1666  {
1667  BlockNumber blks_done;
1668 
1669  if (hscan->rs_base.rs_parallel != NULL)
1670  {
1672 
1674  blks_done = pbscan->phs_nblocks;
1675  }
1676  else
1677  blks_done = hscan->rs_nblocks;
1678 
1680  blks_done);
1681  }
1682 
1683  table_endscan(scan);
1684 
1685  /* we can now forget our snapshot, if set and registered by us */
1686  if (need_unregister_snapshot)
1687  UnregisterSnapshot(snapshot);
1688 
1690 
1691  FreeExecutorState(estate);
1692 
1693  /* These may have been pointing to the now-gone estate */
1694  indexInfo->ii_ExpressionsState = NIL;
1695  indexInfo->ii_PredicateState = NULL;
1696 
1697  return reltuples;
1698 }
TupleTableSlot * table_slot_create(Relation relation, List **reglist)
Definition: tableam.c:77
#define HeapTupleHeaderGetUpdateXid(tup)
Definition: htup_details.h:365
void FormIndexDatum(IndexInfo *indexInfo, TupleTableSlot *slot, EState *estate, Datum *values, bool *isnull)
Definition: index.c:2567
#define NIL
Definition: pg_list.h:65
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:86
BlockNumber rs_cblock
Definition: heapam.h:58
List * ii_Predicate
Definition: execnodes.h:161
bool IsSystemRelation(Relation relation)
Definition: catalog.c:69
uint32 TransactionId
Definition: c.h:514
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:865
bool TransactionIdIsCurrentTransactionId(TransactionId xid)
Definition: xact.c:853
struct ParallelBlockTableScanDescData * ParallelBlockTableScanDesc
Definition: relscan.h:82
TableScanDescData rs_base
Definition: heapam.h:48
void pgstat_progress_update_param(int index, int64 val)
Definition: pgstat.c:3218
ExprState * ii_PredicateState
Definition: execnodes.h:162
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:231
#define MaxHeapTuplesPerPage
Definition: htup_details.h:574
int errcode(int sqlerrcode)
Definition: elog.c:608
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
uint32 BlockNumber
Definition: block.h:31
#define PROCARRAY_FLAGS_VACUUM
Definition: procarray.h:52
Form_pg_class rd_rel
Definition: rel.h:83
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:365
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:306
#define OidIsValid(objectId)
Definition: c.h:645
static TableScanDesc table_beginscan_strat(Relation rel, Snapshot snapshot, int nkeys, struct ScanKeyData *key, bool allow_strat, bool allow_sync)
Definition: tableam.h:760
struct HeapScanDescData * HeapScanDesc
Definition: heapam.h:72
uint16 OffsetNumber
Definition: off.h:24
HeapTupleHeader t_data
Definition: htup.h:68
void FreeExecutorState(EState *estate)
Definition: execUtils.c:190
#define HeapTupleIsHotUpdated(tuple)
Definition: htup_details.h:676
#define GetPerTupleExprContext(estate)
Definition: executor.h:501
List * ii_ExpressionsState
Definition: execnodes.h:160
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1290
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1224
void heap_setscanlimits(TableScanDesc sscan, BlockNumber startBlk, BlockNumber numBlks)
Definition: heapam.c:329
#define InvalidTransactionId
Definition: transam.h:31
#define RelationGetRelationName(relation)
Definition: rel.h:456
ExprState * ExecPrepareQual(List *qual, EState *estate)
Definition: execExpr.c:518
static TransactionId OldestXmin
Definition: vacuumlazy.c:145
bool ii_BrokenHotChain
Definition: execnodes.h:172
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
HTSV_Result HeapTupleSatisfiesVacuum(HeapTuple htup, TransactionId OldestXmin, Buffer buffer)
void heap_get_root_tuples(Page page, OffsetNumber *root_offsets)
Definition: pruneheap.c:745
#define ereport(elevel, rest)
Definition: elog.h:141
EState * CreateExecutorState(void)
Definition: execUtils.c:88
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:907
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:45
#define WARNING
Definition: elog.h:40
int progress
Definition: pgbench.c:235
#define PROGRESS_SCAN_BLOCKS_DONE
Definition: progress.h:103
uintptr_t Datum
Definition: postgres.h:367
BlockNumber rs_nblocks
Definition: heapam.h:51
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3602
TupleTableSlot * ExecStoreBufferHeapTuple(HeapTuple tuple, TupleTableSlot *slot, Buffer buffer)
Definition: execTuples.c:1362
#define IsMVCCSnapshot(snapshot)
Definition: snapmgr.h:97
TransactionId GetOldestXmin(Relation rel, int flags)
Definition: procarray.c:1306
Buffer rs_cbuf
Definition: heapam.h:59
int errmsg_internal(const char *fmt,...)
Definition: elog.c:909
bool ii_Unique
Definition: execnodes.h:169
void XactLockTableWait(TransactionId xid, Relation rel, ItemPointer ctid, XLTW_Oper oper)
Definition: lmgr.c:624
#define HeapTupleIsHeapOnly(tuple)
Definition: htup_details.h:685
#define Assert(condition)
Definition: c.h:739
#define HeapTupleHeaderGetXmin(tup)
Definition: htup_details.h:313
#define INDEX_MAX_KEYS
#define InvalidBlockNumber
Definition: block.h:33
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:223
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:117
#define PROGRESS_SCAN_BLOCKS_TOTAL
Definition: progress.h:102
bool ii_Concurrent
Definition: execnodes.h:171
#define SnapshotAny
Definition: snapmgr.h:69
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:831
static Datum values[MAXATTR]
Definition: bootstrap.c:167
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:374
Oid * ii_ExclusionOps
Definition: execnodes.h:163
struct SnapshotData * rs_snapshot
Definition: relscan.h:35
#define elog(elevel,...)
Definition: elog.h:228
#define OffsetNumberIsValid(offsetNumber)
Definition: off.h:39
struct ParallelTableScanDescData * rs_parallel
Definition: relscan.h:45
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:87
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98
#define TransactionIdIsValid(xid)
Definition: transam.h:41
static BlockNumber heapam_scan_get_blocks_done(HeapScanDesc hscan)
float4 reltuples
Definition: pg_class.h:63
Pointer Page
Definition: bufpage.h:78
#define ItemPointerSet(pointer, blockNumber, offNum)
Definition: itemptr.h:127

◆ heapam_index_fetch_begin()

static IndexFetchTableData* heapam_index_fetch_begin ( Relation  rel)
static

Definition at line 78 of file heapam_handler.c.

References InvalidBuffer, palloc0(), IndexFetchTableData::rel, IndexFetchHeapData::xs_base, and IndexFetchHeapData::xs_cbuf.

Referenced by SampleHeapTupleVisible().

79 {
81 
82  hscan->xs_base.rel = rel;
83  hscan->xs_cbuf = InvalidBuffer;
84 
85  return &hscan->xs_base;
86 }
#define InvalidBuffer
Definition: buf.h:25
Buffer xs_cbuf
Definition: heapam.h:81
void * palloc0(Size size)
Definition: mcxt.c:980
Relation rel
Definition: relscan.h:91
IndexFetchTableData xs_base
Definition: heapam.h:79

◆ heapam_index_fetch_end()

static void heapam_index_fetch_end ( IndexFetchTableData scan)
static

Definition at line 101 of file heapam_handler.c.

References heapam_index_fetch_reset(), and pfree().

Referenced by SampleHeapTupleVisible().

102 {
103  IndexFetchHeapData *hscan = (IndexFetchHeapData *) scan;
104 
106 
107  pfree(hscan);
108 }
static void heapam_index_fetch_reset(IndexFetchTableData *scan)
void pfree(void *pointer)
Definition: mcxt.c:1056

◆ heapam_index_fetch_reset()

static void heapam_index_fetch_reset ( IndexFetchTableData scan)
static

Definition at line 89 of file heapam_handler.c.

References BufferIsValid, InvalidBuffer, ReleaseBuffer(), and IndexFetchHeapData::xs_cbuf.

Referenced by heapam_index_fetch_end(), and SampleHeapTupleVisible().

90 {
91  IndexFetchHeapData *hscan = (IndexFetchHeapData *) scan;
92 
93  if (BufferIsValid(hscan->xs_cbuf))
94  {
95  ReleaseBuffer(hscan->xs_cbuf);
96  hscan->xs_cbuf = InvalidBuffer;
97  }
98 }
#define InvalidBuffer
Definition: buf.h:25
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3365
Buffer xs_cbuf
Definition: heapam.h:81
#define BufferIsValid(bufnum)
Definition: bufmgr.h:113

◆ heapam_index_fetch_tuple()

static bool heapam_index_fetch_tuple ( struct IndexFetchTableData scan,
ItemPointer  tid,
Snapshot  snapshot,
TupleTableSlot slot,
bool call_again,
bool all_dead 
)
static

Definition at line 111 of file heapam_handler.c.

References Assert, BufferHeapTupleTableSlot::base, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, ExecStoreBufferHeapTuple(), heap_hot_search_buffer(), heap_page_prune_opt(), IsMVCCSnapshot, ItemPointerGetBlockNumber, LockBuffer(), IndexFetchTableData::rel, RelationGetRelid, ReleaseAndReadBuffer(), HeapTupleData::t_self, TTS_IS_BUFFERTUPLE, TupleTableSlot::tts_tableOid, HeapTupleTableSlot::tupdata, IndexFetchHeapData::xs_base, and IndexFetchHeapData::xs_cbuf.

Referenced by SampleHeapTupleVisible().

116 {
117  IndexFetchHeapData *hscan = (IndexFetchHeapData *) scan;
119  bool got_heap_tuple;
120 
121  Assert(TTS_IS_BUFFERTUPLE(slot));
122 
123  /* We can skip the buffer-switching logic if we're in mid-HOT chain. */
124  if (!*call_again)
125  {
126  /* Switch to correct buffer if we don't have it already */
127  Buffer prev_buf = hscan->xs_cbuf;
128 
129  hscan->xs_cbuf = ReleaseAndReadBuffer(hscan->xs_cbuf,
130  hscan->xs_base.rel,
132 
133  /*
134  * Prune page, but only if we weren't already on this page
135  */
136  if (prev_buf != hscan->xs_cbuf)
137  heap_page_prune_opt(hscan->xs_base.rel, hscan->xs_cbuf);
138  }
139 
140  /* Obtain share-lock on the buffer so we can examine visibility */
142  got_heap_tuple = heap_hot_search_buffer(tid,
143  hscan->xs_base.rel,
144  hscan->xs_cbuf,
145  snapshot,
146  &bslot->base.tupdata,
147  all_dead,
148  !*call_again);
149  bslot->base.tupdata.t_self = *tid;
151 
152  if (got_heap_tuple)
153  {
154  /*
155  * Only in a non-MVCC snapshot can more than one member of the HOT
156  * chain be visible.
157  */
158  *call_again = !IsMVCCSnapshot(snapshot);
159 
160  slot->tts_tableOid = RelationGetRelid(scan->rel);
161  ExecStoreBufferHeapTuple(&bslot->base.tupdata, slot, hscan->xs_cbuf);
162  }
163  else
164  {
165  /* We've reached the end of the HOT chain. */
166  *call_again = false;
167  }
168 
169  return got_heap_tuple;
170 }
Oid tts_tableOid
Definition: tuptable.h:131
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:86
bool heap_hot_search_buffer(ItemPointer tid, Relation relation, Buffer buffer, Snapshot snapshot, HeapTuple heapTuple, bool *all_dead, bool first_call)
Definition: heapam.c:1526
ItemPointerData t_self
Definition: htup.h:65
Buffer xs_cbuf
Definition: heapam.h:81
#define TTS_IS_BUFFERTUPLE(slot)
Definition: tuptable.h:231
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3602
TupleTableSlot * ExecStoreBufferHeapTuple(HeapTuple tuple, TupleTableSlot *slot, Buffer buffer)
Definition: execTuples.c:1362
#define IsMVCCSnapshot(snapshot)
Definition: snapmgr.h:97
#define Assert(condition)
Definition: c.h:739
Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation, BlockNumber blockNum)
Definition: bufmgr.c:1521
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:87
HeapTupleTableSlot base
Definition: tuptable.h:259
void heap_page_prune_opt(Relation relation, Buffer buffer)
Definition: pruneheap.c:73
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98
Relation rel
Definition: relscan.h:91
HeapTupleData tupdata
Definition: tuptable.h:253
int Buffer
Definition: buf.h:23
#define RelationGetRelid(relation)
Definition: rel.h:422
IndexFetchTableData xs_base
Definition: heapam.h:79

◆ heapam_index_validate_scan()

static void heapam_index_validate_scan ( Relation  heapRelation,
Relation  indexRelation,
IndexInfo indexInfo,
Snapshot  snapshot,
ValidateIndexState state 
)
static

Definition at line 1701 of file heapam_handler.c.

References Assert, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferGetPage, CHECK_FOR_INTERRUPTS, CreateExecutorState(), DatumGetInt64, DatumGetPointer, ExprContext::ecxt_per_tuple_memory, ExprContext::ecxt_scantuple, ereport, errcode(), ERRCODE_DATA_CORRUPTED, errmsg_internal(), ERROR, ExecDropSingleTupleTableSlot(), ExecPrepareQual(), ExecQual(), ExecStoreHeapTuple(), FormIndexDatum(), ForwardScanDirection, FreeExecutorState(), GetPerTupleExprContext, heap_get_root_tuples(), heap_getnext(), HeapTupleIsHeapOnly, ValidateIndexState::htups, IndexInfo::ii_ExpressionsState, IndexInfo::ii_Predicate, IndexInfo::ii_PredicateState, IndexInfo::ii_Unique, index_insert(), INDEX_MAX_KEYS, InvalidBlockNumber, ItemPointerCompare(), ItemPointerGetBlockNumber, ItemPointerGetOffsetNumber, ItemPointerSetOffsetNumber, itemptr_decode(), LockBuffer(), MakeSingleTupleTableSlot(), MaxHeapTuplesPerPage, MemoryContextReset(), NIL, OffsetNumberIsValid, OidIsValid, pfree(), pgstat_progress_update_param(), PROGRESS_SCAN_BLOCKS_DONE, PROGRESS_SCAN_BLOCKS_TOTAL, RelationData::rd_rel, RelationGetDescr, RelationGetRelationName, HeapScanDescData::rs_cblock, HeapScanDescData::rs_cbuf, HeapScanDescData::rs_nblocks, HeapTupleData::t_self, table_beginscan_strat(), table_endscan(), TTSOpsHeapTuple, ValidateIndexState::tuplesort, tuplesort_getdatum(), ValidateIndexState::tups_inserted, UNIQUE_CHECK_NO, UNIQUE_CHECK_YES, and values.

Referenced by SampleHeapTupleVisible().

1706 {
1707  TableScanDesc scan;
1708  HeapScanDesc hscan;
1709  HeapTuple heapTuple;
1711  bool isnull[INDEX_MAX_KEYS];
1712  ExprState *predicate;
1713  TupleTableSlot *slot;
1714  EState *estate;
1715  ExprContext *econtext;
1716  BlockNumber root_blkno = InvalidBlockNumber;
1717  OffsetNumber root_offsets[MaxHeapTuplesPerPage];
1718  bool in_index[MaxHeapTuplesPerPage];
1719  BlockNumber previous_blkno = InvalidBlockNumber;
1720 
1721  /* state variables for the merge */
1722  ItemPointer indexcursor = NULL;
1723  ItemPointerData decoded;
1724  bool tuplesort_empty = false;
1725 
1726  /*
1727  * sanity checks
1728  */
1729  Assert(OidIsValid(indexRelation->rd_rel->relam));
1730 
1731  /*
1732  * Need an EState for evaluation of index expressions and partial-index
1733  * predicates. Also a slot to hold the current tuple.
1734  */
1735  estate = CreateExecutorState();
1736  econtext = GetPerTupleExprContext(estate);
1737  slot = MakeSingleTupleTableSlot(RelationGetDescr(heapRelation),
1738  &TTSOpsHeapTuple);
1739 
1740  /* Arrange for econtext's scan tuple to be the tuple under test */
1741  econtext->ecxt_scantuple = slot;
1742 
1743  /* Set up execution state for predicate, if any. */
1744  predicate = ExecPrepareQual(indexInfo->ii_Predicate, estate);
1745 
1746  /*
1747  * Prepare for scan of the base relation. We need just those tuples
1748  * satisfying the passed-in reference snapshot. We must disable syncscan
1749  * here, because it's critical that we read from block zero forward to
1750  * match the sorted TIDs.
1751  */
1752  scan = table_beginscan_strat(heapRelation, /* relation */
1753  snapshot, /* snapshot */
1754  0, /* number of keys */
1755  NULL, /* scan key */
1756  true, /* buffer access strategy OK */
1757  false); /* syncscan not OK */
1758  hscan = (HeapScanDesc) scan;
1759 
1761  hscan->rs_nblocks);
1762 
1763  /*
1764  * Scan all tuples matching the snapshot.
1765  */
1766  while ((heapTuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1767  {
1768  ItemPointer heapcursor = &heapTuple->t_self;
1769  ItemPointerData rootTuple;
1770  OffsetNumber root_offnum;
1771 
1773 
1774  state->htups += 1;
1775 
1776  if ((previous_blkno == InvalidBlockNumber) ||
1777  (hscan->rs_cblock != previous_blkno))
1778  {
1780  hscan->rs_cblock);
1781  previous_blkno = hscan->rs_cblock;
1782  }
1783 
1784  /*
1785  * As commented in table_index_build_scan, we should index heap-only
1786  * tuples under the TIDs of their root tuples; so when we advance onto
1787  * a new heap page, build a map of root item offsets on the page.
1788  *
1789  * This complicates merging against the tuplesort output: we will
1790  * visit the live tuples in order by their offsets, but the root
1791  * offsets that we need to compare against the index contents might be
1792  * ordered differently. So we might have to "look back" within the
1793  * tuplesort output, but only within the current page. We handle that
1794  * by keeping a bool array in_index[] showing all the
1795  * already-passed-over tuplesort output TIDs of the current page. We
1796  * clear that array here, when advancing onto a new heap page.
1797  */
1798  if (hscan->rs_cblock != root_blkno)
1799  {
1800  Page page = BufferGetPage(hscan->rs_cbuf);
1801 
1803  heap_get_root_tuples(page, root_offsets);
1805 
1806  memset(in_index, 0, sizeof(in_index));
1807 
1808  root_blkno = hscan->rs_cblock;
1809  }
1810 
1811  /* Convert actual tuple TID to root TID */
1812  rootTuple = *heapcursor;
1813  root_offnum = ItemPointerGetOffsetNumber(heapcursor);
1814 
1815  if (HeapTupleIsHeapOnly(heapTuple))
1816  {
1817  root_offnum = root_offsets[root_offnum - 1];
1818  if (!OffsetNumberIsValid(root_offnum))
1819  ereport(ERROR,
1821  errmsg_internal("failed to find parent tuple for heap-only tuple at (%u,%u) in table \"%s\"",
1822  ItemPointerGetBlockNumber(heapcursor),
1823  ItemPointerGetOffsetNumber(heapcursor),
1824  RelationGetRelationName(heapRelation))));
1825  ItemPointerSetOffsetNumber(&rootTuple, root_offnum);
1826  }
1827 
1828  /*
1829  * "merge" by skipping through the index tuples until we find or pass
1830  * the current root tuple.
1831  */
1832  while (!tuplesort_empty &&
1833  (!indexcursor ||
1834  ItemPointerCompare(indexcursor, &rootTuple) < 0))
1835  {
1836  Datum ts_val;
1837  bool ts_isnull;
1838 
1839  if (indexcursor)
1840  {
1841  /*
1842  * Remember index items seen earlier on the current heap page
1843  */
1844  if (ItemPointerGetBlockNumber(indexcursor) == root_blkno)
1845  in_index[ItemPointerGetOffsetNumber(indexcursor) - 1] = true;
1846  }
1847 
1848  tuplesort_empty = !tuplesort_getdatum(state->tuplesort, true,
1849  &ts_val, &ts_isnull, NULL);
1850  Assert(tuplesort_empty || !ts_isnull);
1851  if (!tuplesort_empty)
1852  {
1853  itemptr_decode(&decoded, DatumGetInt64(ts_val));
1854  indexcursor = &decoded;
1855 
1856  /* If int8 is pass-by-ref, free (encoded) TID Datum memory */
1857 #ifndef USE_FLOAT8_BYVAL
1858  pfree(DatumGetPointer(ts_val));
1859 #endif
1860  }
1861  else
1862  {
1863  /* Be tidy */
1864  indexcursor = NULL;
1865  }
1866  }
1867 
1868  /*
1869  * If the tuplesort has overshot *and* we didn't see a match earlier,
1870  * then this tuple is missing from the index, so insert it.
1871  */
1872  if ((tuplesort_empty ||
1873  ItemPointerCompare(indexcursor, &rootTuple) > 0) &&
1874  !in_index[root_offnum - 1])
1875  {
1877 
1878  /* Set up for predicate or expression evaluation */
1879  ExecStoreHeapTuple(heapTuple, slot, false);
1880 
1881  /*
1882  * In a partial index, discard tuples that don't satisfy the
1883  * predicate.
1884  */
1885  if (predicate != NULL)
1886  {
1887  if (!ExecQual(predicate, econtext))
1888  continue;
1889  }
1890 
1891  /*
1892  * For the current heap tuple, extract all the attributes we use
1893  * in this index, and note which are null. This also performs
1894  * evaluation of any expressions needed.
1895  */
1896  FormIndexDatum(indexInfo,
1897  slot,
1898  estate,
1899  values,
1900  isnull);
1901 
1902  /*
1903  * You'd think we should go ahead and build the index tuple here,
1904  * but some index AMs want to do further processing on the data
1905  * first. So pass the values[] and isnull[] arrays, instead.
1906  */
1907 
1908  /*
1909  * If the tuple is already committed dead, you might think we
1910  * could suppress uniqueness checking, but this is no longer true
1911  * in the presence of HOT, because the insert is actually a proxy
1912  * for a uniqueness check on the whole HOT-chain. That is, the
1913  * tuple we have here could be dead because it was already
1914  * HOT-updated, and if so the updating transaction will not have
1915  * thought it should insert index entries. The index AM will
1916  * check the whole HOT-chain and correctly detect a conflict if
1917  * there is one.
1918  */
1919 
1920  index_insert(indexRelation,
1921  values,
1922  isnull,
1923  &rootTuple,
1924  heapRelation,
1925  indexInfo->ii_Unique ?
1927  indexInfo);
1928 
1929  state->tups_inserted += 1;
1930  }
1931  }
1932 
1933  table_endscan(scan);
1934 
1936 
1937  FreeExecutorState(estate);
1938 
1939  /* These may have been pointing to the now-gone estate */
1940  indexInfo->ii_ExpressionsState = NIL;
1941  indexInfo->ii_PredicateState = NULL;
1942 }
int32 ItemPointerCompare(ItemPointer arg1, ItemPointer arg2)
Definition: itemptr.c:52
void FormIndexDatum(IndexInfo *indexInfo, TupleTableSlot *slot, EState *estate, Datum *values, bool *isnull)
Definition: index.c:2567
#define NIL
Definition: pg_list.h:65
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:86
bool tuplesort_getdatum(Tuplesortstate *state, bool forward, Datum *val, bool *isNull, Datum *abbrev)
Definition: tuplesort.c:2245
BlockNumber rs_cblock
Definition: heapam.h:58
List * ii_Predicate
Definition: execnodes.h:161
#define RelationGetDescr(relation)
Definition: rel.h:448
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1208
void pgstat_progress_update_param(int index, int64 val)
Definition: pgstat.c:3218
ExprState * ii_PredicateState
Definition: execnodes.h:162
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:231
#define MaxHeapTuplesPerPage
Definition: htup_details.h:574
int errcode(int sqlerrcode)
Definition: elog.c:608
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
uint32 BlockNumber
Definition: block.h:31
Form_pg_class rd_rel
Definition: rel.h:83
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:365
#define OidIsValid(objectId)
Definition: c.h:645
static TableScanDesc table_beginscan_strat(Relation rel, Snapshot snapshot, int nkeys, struct ScanKeyData *key, bool allow_strat, bool allow_sync)
Definition: tableam.h:760
struct HeapScanDescData * HeapScanDesc
Definition: heapam.h:72
uint16 OffsetNumber
Definition: off.h:24
void FreeExecutorState(EState *estate)
Definition: execUtils.c:190
#define GetPerTupleExprContext(estate)
Definition: executor.h:501
List * ii_ExpressionsState
Definition: execnodes.h:160
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define DatumGetInt64(X)
Definition: postgres.h:607
Tuplesortstate * tuplesort
Definition: index.h:35
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1290
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1224
#define RelationGetRelationName(relation)
Definition: rel.h:456
ExprState * ExecPrepareQual(List *qual, EState *estate)
Definition: execExpr.c:518
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
void heap_get_root_tuples(Page page, OffsetNumber *root_offsets)
Definition: pruneheap.c:745
#define ereport(elevel, rest)
Definition: elog.h:141
EState * CreateExecutorState(void)
Definition: execUtils.c:88
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:45
#define PROGRESS_SCAN_BLOCKS_DONE
Definition: progress.h:103
uintptr_t Datum
Definition: postgres.h:367
BlockNumber rs_nblocks
Definition: heapam.h:51
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3602
static void itemptr_decode(ItemPointer itemptr, int64 encoded)
Definition: index.h:189
Buffer rs_cbuf
Definition: heapam.h:59
int errmsg_internal(const char *fmt,...)
Definition: elog.c:909
bool ii_Unique
Definition: execnodes.h:169
#define HeapTupleIsHeapOnly(tuple)
Definition: htup_details.h:685
#define Assert(condition)
Definition: c.h:739
double tups_inserted
Definition: index.h:37
#define INDEX_MAX_KEYS
#define InvalidBlockNumber
Definition: block.h:33
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:223
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:117
#define PROGRESS_SCAN_BLOCKS_TOTAL
Definition: progress.h:102
#define DatumGetPointer(X)
Definition: postgres.h:549
double htups
Definition: index.h:37
#define ItemPointerSetOffsetNumber(pointer, offsetNumber)
Definition: itemptr.h:148
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:831
static Datum values[MAXATTR]
Definition: bootstrap.c:167
#define OffsetNumberIsValid(offsetNumber)
Definition: off.h:39
const TupleTableSlotOps TTSOpsHeapTuple
Definition: execTuples.c:84
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:87
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98
TupleTableSlot * ExecStoreHeapTuple(HeapTuple tuple, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:1322
Pointer Page
Definition: bufpage.h:78
bool index_insert(Relation indexRelation, Datum *values, bool *isnull, ItemPointer heap_t_ctid, Relation heapRelation, IndexUniqueCheck checkUnique, IndexInfo *indexInfo)
Definition: indexam.c:170

◆ heapam_relation_copy_data()

static void heapam_relation_copy_data ( Relation  rel,
const RelFileNode newrnode 
)
static

Definition at line 632 of file heapam_handler.c.

References FlushRelationBuffers(), INIT_FORKNUM, log_smgrcreate(), MAIN_FORKNUM, MAX_FORKNUM, RelationData::rd_backend, RelationData::rd_rel, RelationData::rd_smgr, RelationCopyStorage(), RelationCreateStorage(), RelationDropStorage(), RelationOpenSmgr, smgrclose(), smgrcreate(), smgrexists(), and smgropen().

Referenced by SampleHeapTupleVisible().

633 {
634  SMgrRelation dstrel;
635 
636  dstrel = smgropen(*newrnode, rel->rd_backend);
637  RelationOpenSmgr(rel);
638 
639  /*
640  * Since we copy the file directly without looking at the shared buffers,
641  * we'd better first flush out any pages of the source relation that are
642  * in shared buffers. We assume no new changes will be made while we are
643  * holding exclusive lock on the rel.
644  */
646 
647  /*
648  * Create and copy all forks of the relation, and schedule unlinking of
649  * old physical files.
650  *
651  * NOTE: any conflict in relfilenode value will be caught in
652  * RelationCreateStorage().
653  */
654  RelationCreateStorage(*newrnode, rel->rd_rel->relpersistence);
655 
656  /* copy main fork */
658  rel->rd_rel->relpersistence);
659 
660  /* copy those extra forks that exist */
661  for (ForkNumber forkNum = MAIN_FORKNUM + 1;
662  forkNum <= MAX_FORKNUM; forkNum++)
663  {
664  if (smgrexists(rel->rd_smgr, forkNum))
665  {
666  smgrcreate(dstrel, forkNum, false);
667 
668  /*
669  * WAL log creation if the relation is persistent, or this is the
670  * init fork of an unlogged relation.
671  */
672  if (rel->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT ||
673  (rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED &&
674  forkNum == INIT_FORKNUM))
675  log_smgrcreate(newrnode, forkNum);
676  RelationCopyStorage(rel->rd_smgr, dstrel, forkNum,
677  rel->rd_rel->relpersistence);
678  }
679  }
680 
681 
682  /* drop old relation, and close new one */
683  RelationDropStorage(rel);
684  smgrclose(dstrel);
685 }
void smgrclose(SMgrRelation reln)
Definition: smgr.c:256
void smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: smgr.c:333
struct SMgrRelationData * rd_smgr
Definition: rel.h:56
bool smgrexists(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:247
Form_pg_class rd_rel
Definition: rel.h:83
#define RelationOpenSmgr(relation)
Definition: rel.h:479
SMgrRelation RelationCreateStorage(RelFileNode rnode, char relpersistence)
Definition: storage.c:78
SMgrRelation smgropen(RelFileNode rnode, BackendId backend)
Definition: smgr.c:145
ForkNumber
Definition: relpath.h:40
void RelationDropStorage(Relation rel)
Definition: storage.c:147
void RelationCopyStorage(SMgrRelation src, SMgrRelation dst, ForkNumber forkNum, char relpersistence)
Definition: storage.c:336
BackendId rd_backend
Definition: rel.h:58
void FlushRelationBuffers(Relation rel)
Definition: bufmgr.c:3204
#define MAX_FORKNUM
Definition: relpath.h:55
void log_smgrcreate(const RelFileNode *rnode, ForkNumber forkNum)
Definition: storage.c:127

◆ heapam_relation_copy_for_cluster()

static void heapam_relation_copy_for_cluster ( Relation  OldHeap,
Relation  NewHeap,
Relation  OldIndex,
bool  use_sort,
TransactionId  OldestXmin,
TransactionId xid_cutoff,
MultiXactId multi_cutoff,
double *  num_tuples,
double *  tups_vacuumed,
double *  tups_recently_dead 
)
static

Definition at line 688 of file heapam_handler.c.

References Assert, begin_heap_rewrite(), buf, BufferHeapTupleTableSlot::buffer, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, CHECK_FOR_INTERRUPTS, elog, end_heap_rewrite(), ERROR, ExecDropSingleTupleTableSlot(), ExecFetchSlotHeapTuple(), ForwardScanDirection, HEAPTUPLE_DEAD, HEAPTUPLE_DELETE_IN_PROGRESS, HEAPTUPLE_INSERT_IN_PROGRESS, HEAPTUPLE_LIVE, HEAPTUPLE_RECENTLY_DEAD, HeapTupleHeaderGetUpdateXid, HeapTupleHeaderGetXmin, HeapTupleSatisfiesVacuum(), index_beginscan(), index_endscan(), index_getnext_slot(), index_rescan(), InvalidBlockNumber, IsSystemRelation(), LockBuffer(), maintenance_work_mem, TupleDescData::natts, palloc(), pfree(), pgstat_progress_update_multi_param(), pgstat_progress_update_param(), PROGRESS_CLUSTER_HEAP_BLKS_SCANNED, PROGRESS_CLUSTER_HEAP_TUPLES_SCANNED, PROGRESS_CLUSTER_HEAP_TUPLES_WRITTEN, PROGRESS_CLUSTER_INDEX_RELID, PROGRESS_CLUSTER_PHASE, PROGRESS_CLUSTER_PHASE_INDEX_SCAN_HEAP, PROGRESS_CLUSTER_PHASE_SEQ_SCAN_HEAP, PROGRESS_CLUSTER_PHASE_SORT_TUPLES, PROGRESS_CLUSTER_PHASE_WRITE_NEW_HEAP, PROGRESS_CLUSTER_TOTAL_HEAP_BLKS, reform_and_rewrite_tuple(), RelationGetDescr, RelationGetRelationName, RelationGetRelid, RelationGetTargetBlock, RelationNeedsWAL, rewrite_heap_dead_tuple(), HeapScanDescData::rs_cblock, HeapScanDescData::rs_nblocks, SnapshotAny, HeapTupleData::t_data, table_beginscan(), table_endscan(), table_scan_getnextslot(), table_slot_create(), TransactionIdIsCurrentTransactionId(), tuplesort_begin_cluster(), tuplesort_end(), tuplesort_getheaptuple(), tuplesort_performsort(), tuplesort_putheaptuple(), values, WARNING, XLogIsNeeded, and IndexScanDescData::xs_recheck.

Referenced by SampleHeapTupleVisible().

696 {
697  RewriteState rwstate;
698  IndexScanDesc indexScan;
699  TableScanDesc tableScan;
700  HeapScanDesc heapScan;
701  bool use_wal;
702  bool is_system_catalog;
703  Tuplesortstate *tuplesort;
704  TupleDesc oldTupDesc = RelationGetDescr(OldHeap);
705  TupleDesc newTupDesc = RelationGetDescr(NewHeap);
706  TupleTableSlot *slot;
707  int natts;
708  Datum *values;
709  bool *isnull;
711 
712  /* Remember if it's a system catalog */
713  is_system_catalog = IsSystemRelation(OldHeap);
714 
715  /*
716  * We need to log the copied data in WAL iff WAL archiving/streaming is
717  * enabled AND it's a WAL-logged rel.
718  */
719  use_wal = XLogIsNeeded() && RelationNeedsWAL(NewHeap);
720 
721  /* use_wal off requires smgr_targblock be initially invalid */
723 
724  /* Preallocate values/isnull arrays */
725  natts = newTupDesc->natts;
726  values = (Datum *) palloc(natts * sizeof(Datum));
727  isnull = (bool *) palloc(natts * sizeof(bool));
728 
729  /* Initialize the rewrite operation */
730  rwstate = begin_heap_rewrite(OldHeap, NewHeap, OldestXmin, *xid_cutoff,
731  *multi_cutoff, use_wal);
732 
733 
734  /* Set up sorting if wanted */
735  if (use_sort)
736  tuplesort = tuplesort_begin_cluster(oldTupDesc, OldIndex,
738  NULL, false);
739  else
740  tuplesort = NULL;
741 
742  /*
743  * Prepare to scan the OldHeap. To ensure we see recently-dead tuples
744  * that still need to be copied, we scan with SnapshotAny and use
745  * HeapTupleSatisfiesVacuum for the visibility test.
746  */
747  if (OldIndex != NULL && !use_sort)
748  {
749  const int ci_index[] = {
752  };
753  int64 ci_val[2];
754 
755  /* Set phase and OIDOldIndex to columns */
757  ci_val[1] = RelationGetRelid(OldIndex);
758  pgstat_progress_update_multi_param(2, ci_index, ci_val);
759 
760  tableScan = NULL;
761  heapScan = NULL;
762  indexScan = index_beginscan(OldHeap, OldIndex, SnapshotAny, 0, 0);
763  index_rescan(indexScan, NULL, 0, NULL, 0);
764  }
765  else
766  {
767  /* In scan-and-sort mode and also VACUUM FULL, set phase */
770 
771  tableScan = table_beginscan(OldHeap, SnapshotAny, 0, (ScanKey) NULL);
772  heapScan = (HeapScanDesc) tableScan;
773  indexScan = NULL;
774 
775  /* Set total heap blocks */
777  heapScan->rs_nblocks);
778  }
779 
780  slot = table_slot_create(OldHeap, NULL);
781  hslot = (BufferHeapTupleTableSlot *) slot;
782 
783  /*
784  * Scan through the OldHeap, either in OldIndex order or sequentially;
785  * copy each tuple into the NewHeap, or transiently to the tuplesort
786  * module. Note that we don't bother sorting dead tuples (they won't get
787  * to the new table anyway).
788  */
789  for (;;)
790  {
791  HeapTuple tuple;
792  Buffer buf;
793  bool isdead;
794 
796 
797  if (indexScan != NULL)
798  {
799  if (!index_getnext_slot(indexScan, ForwardScanDirection, slot))
800  break;
801 
802  /* Since we used no scan keys, should never need to recheck */
803  if (indexScan->xs_recheck)
804  elog(ERROR, "CLUSTER does not support lossy index conditions");
805  }
806  else
807  {
808  if (!table_scan_getnextslot(tableScan, ForwardScanDirection, slot))
809  break;
810 
811  /*
812  * In scan-and-sort mode and also VACUUM FULL, set heap blocks
813  * scanned
814  */
816  heapScan->rs_cblock + 1);
817  }
818 
819  tuple = ExecFetchSlotHeapTuple(slot, false, NULL);
820  buf = hslot->buffer;
821 
823 
824  switch (HeapTupleSatisfiesVacuum(tuple, OldestXmin, buf))
825  {
826  case HEAPTUPLE_DEAD:
827  /* Definitely dead */
828  isdead = true;
829  break;
831  *tups_recently_dead += 1;
832  /* fall through */
833  case HEAPTUPLE_LIVE:
834  /* Live or recently dead, must copy it */
835  isdead = false;
836  break;
838 
839  /*
840  * Since we hold exclusive lock on the relation, normally the
841  * only way to see this is if it was inserted earlier in our
842  * own transaction. However, it can happen in system
843  * catalogs, since we tend to release write lock before commit
844  * there. Give a warning if neither case applies; but in any
845  * case we had better copy it.
846  */
847  if (!is_system_catalog &&
849  elog(WARNING, "concurrent insert in progress within table \"%s\"",
850  RelationGetRelationName(OldHeap));
851  /* treat as live */
852  isdead = false;
853  break;
855 
856  /*
857  * Similar situation to INSERT_IN_PROGRESS case.
858  */
859  if (!is_system_catalog &&
861  elog(WARNING, "concurrent delete in progress within table \"%s\"",
862  RelationGetRelationName(OldHeap));
863  /* treat as recently dead */
864  *tups_recently_dead += 1;
865  isdead = false;
866  break;
867  default:
868  elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
869  isdead = false; /* keep compiler quiet */
870  break;
871  }
872 
874 
875  if (isdead)
876  {
877  *tups_vacuumed += 1;
878  /* heap rewrite module still needs to see it... */
879  if (rewrite_heap_dead_tuple(rwstate, tuple))
880  {
881  /* A previous recently-dead tuple is now known dead */
882  *tups_vacuumed += 1;
883  *tups_recently_dead -= 1;
884  }
885  continue;
886  }
887 
888  *num_tuples += 1;
889  if (tuplesort != NULL)
890  {
891  tuplesort_putheaptuple(tuplesort, tuple);
892 
893  /*
894  * In scan-and-sort mode, report increase in number of tuples
895  * scanned
896  */
898  *num_tuples);
899  }
900  else
901  {
902  const int ct_index[] = {
905  };
906  int64 ct_val[2];
907 
908  reform_and_rewrite_tuple(tuple, OldHeap, NewHeap,
909  values, isnull, rwstate);
910 
911  /*
912  * In indexscan mode and also VACUUM FULL, report increase in
913  * number of tuples scanned and written
914  */
915  ct_val[0] = *num_tuples;
916  ct_val[1] = *num_tuples;
917  pgstat_progress_update_multi_param(2, ct_index, ct_val);
918  }
919  }
920 
921  if (indexScan != NULL)
922  index_endscan(indexScan);
923  if (tableScan != NULL)
924  table_endscan(tableScan);
925  if (slot)
927 
928  /*
929  * In scan-and-sort mode, complete the sort, then read out all live tuples
930  * from the tuplestore and write them to the new relation.
931  */
932  if (tuplesort != NULL)
933  {
934  double n_tuples = 0;
935 
936  /* Report that we are now sorting tuples */
939 
940  tuplesort_performsort(tuplesort);
941 
942  /* Report that we are now writing new heap */
945 
946  for (;;)
947  {
948  HeapTuple tuple;
949 
951 
952  tuple = tuplesort_getheaptuple(tuplesort, true);
953  if (tuple == NULL)
954  break;
955 
956  n_tuples += 1;
958  OldHeap, NewHeap,
959  values, isnull,
960  rwstate);
961  /* Report n_tuples */
963  n_tuples);
964  }
965 
966  tuplesort_end(tuplesort);
967  }
968 
969  /* Write out any remaining tuples, and fsync if needed */
970  end_heap_rewrite(rwstate);
971 
972  /* Clean up */
973  pfree(values);
974  pfree(isnull);
975 }
TupleTableSlot * table_slot_create(Relation relation, List **reglist)
Definition: tableam.c:77
#define HeapTupleHeaderGetUpdateXid(tup)
Definition: htup_details.h:365
#define PROGRESS_CLUSTER_PHASE_SEQ_SCAN_HEAP
Definition: progress.h:48
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:86
BlockNumber rs_cblock
Definition: heapam.h:58
void tuplesort_performsort(Tuplesortstate *state)
Definition: tuplesort.c:1791
void end_heap_rewrite(RewriteState state)
Definition: rewriteheap.c:305
HeapTuple tuplesort_getheaptuple(Tuplesortstate *state, bool forward)
Definition: tuplesort.c:2196
bool IsSystemRelation(Relation relation)
Definition: catalog.c:69
#define PROGRESS_CLUSTER_HEAP_TUPLES_WRITTEN
Definition: progress.h:42
RewriteState begin_heap_rewrite(Relation old_heap, Relation new_heap, TransactionId oldest_xmin, TransactionId freeze_xid, MultiXactId cutoff_multi, bool use_wal)
Definition: rewriteheap.c:239
#define RelationGetDescr(relation)
Definition: rel.h:448
bool TransactionIdIsCurrentTransactionId(TransactionId xid)
Definition: xact.c:853
static void reform_and_rewrite_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, Datum *values, bool *isnull, RewriteState rwstate)
void pgstat_progress_update_param(int index, int64 val)
Definition: pgstat.c:3218
#define XLogIsNeeded()
Definition: xlog.h:181
Tuplesortstate * tuplesort_begin_cluster(TupleDesc tupDesc, Relation indexRel, int workMem, SortCoordinate coordinate, bool randomAccess)
Definition: tuplesort.c:880
void index_rescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int norderbys)
Definition: indexam.c:289
#define PROGRESS_CLUSTER_INDEX_RELID
Definition: progress.h:40
#define PROGRESS_CLUSTER_PHASE_WRITE_NEW_HEAP
Definition: progress.h:51
static bool table_scan_getnextslot(TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot)
Definition: tableam.h:872
#define RelationGetTargetBlock(relation)
Definition: rel.h:507
struct HeapScanDescData * HeapScanDesc
Definition: heapam.h:72
HeapTupleHeader t_data
Definition: htup.h:68
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
static TableScanDesc table_beginscan(Relation rel, Snapshot snapshot, int nkeys, struct ScanKeyData *key)
Definition: tableam.h:736
#define PROGRESS_CLUSTER_PHASE_SORT_TUPLES
Definition: progress.h:50
static char * buf
Definition: pg_test_fsync.c:67
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1224
#define PROGRESS_CLUSTER_HEAP_TUPLES_SCANNED
Definition: progress.h:41
#define RelationGetRelationName(relation)
Definition: rel.h:456
static TransactionId OldestXmin
Definition: vacuumlazy.c:145
void index_endscan(IndexScanDesc scan)
Definition: indexam.c:315
HTSV_Result HeapTupleSatisfiesVacuum(HeapTuple htup, TransactionId OldestXmin, Buffer buffer)
HeapTuple ExecFetchSlotHeapTuple(TupleTableSlot *slot, bool materialize, bool *shouldFree)
Definition: execTuples.c:1614
#define WARNING
Definition: elog.h:40
uintptr_t Datum
Definition: postgres.h:367
BlockNumber rs_nblocks
Definition: heapam.h:51
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3602
int maintenance_work_mem
Definition: globals.c:122
#define Assert(condition)
Definition: c.h:739
#define PROGRESS_CLUSTER_PHASE_INDEX_SCAN_HEAP
Definition: progress.h:49
#define HeapTupleHeaderGetXmin(tup)
Definition: htup_details.h:313
void pgstat_progress_update_multi_param(int nparam, const int *index, const int64 *val)
Definition: pgstat.c:3240
#define InvalidBlockNumber
Definition: block.h:33
bool index_getnext_slot(IndexScanDesc scan, ScanDirection direction, TupleTableSlot *slot)
Definition: indexam.c:607
#define RelationNeedsWAL(relation)
Definition: rel.h:524
#define SnapshotAny
Definition: snapmgr.h:69
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:831
static Datum values[MAXATTR]
Definition: bootstrap.c:167
bool rewrite_heap_dead_tuple(RewriteState state, HeapTuple old_tuple)
Definition: rewriteheap.c:573
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:228
void tuplesort_putheaptuple(Tuplesortstate *state, HeapTuple tup)
Definition: tuplesort.c:1457
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:87
#define PROGRESS_CLUSTER_HEAP_BLKS_SCANNED
Definition: progress.h:44
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define PROGRESS_CLUSTER_TOTAL_HEAP_BLKS
Definition: progress.h:43
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:1236
#define PROGRESS_CLUSTER_PHASE
Definition: progress.h:39
int Buffer
Definition: buf.h:23
#define RelationGetRelid(relation)
Definition: rel.h:422
IndexScanDesc index_beginscan(Relation heapRelation, Relation indexRelation, Snapshot snapshot, int nkeys, int norderbys)
Definition: indexam.c:197

◆ heapam_relation_needs_toast_table()

static bool heapam_relation_needs_toast_table ( Relation  rel)
static

Definition at line 1996 of file heapam_handler.c.

References att_align_nominal, BITMAPLEN, i, MAXALIGN, TupleDescData::natts, RelationData::rd_att, SizeofHeapTupleHeader, TOAST_TUPLE_THRESHOLD, TupleDescAttr, and type_maximum_size().

Referenced by SampleHeapTupleVisible().

1997 {
1998  int32 data_length = 0;
1999  bool maxlength_unknown = false;
2000  bool has_toastable_attrs = false;
2001  TupleDesc tupdesc = rel->rd_att;
2002  int32 tuple_length;
2003  int i;
2004 
2005  for (i = 0; i < tupdesc->natts; i++)
2006  {
2007  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
2008 
2009  if (att->attisdropped)
2010  continue;
2011  data_length = att_align_nominal(data_length, att->attalign);
2012  if (att->attlen > 0)
2013  {
2014  /* Fixed-length types are never toastable */
2015  data_length += att->attlen;
2016  }
2017  else
2018  {
2019  int32 maxlen = type_maximum_size(att->atttypid,
2020  att->atttypmod);
2021 
2022  if (maxlen < 0)
2023  maxlength_unknown = true;
2024  else
2025  data_length += maxlen;
2026  if (att->attstorage != 'p')
2027  has_toastable_attrs = true;
2028  }
2029  }
2030  if (!has_toastable_attrs)
2031  return false; /* nothing to toast? */
2032  if (maxlength_unknown)
2033  return true; /* any unlimited-length attrs? */
2034  tuple_length = MAXALIGN(SizeofHeapTupleHeader +
2035  BITMAPLEN(tupdesc->natts)) +
2036  MAXALIGN(data_length);
2037  return (tuple_length > TOAST_TUPLE_THRESHOLD);
2038 }
#define SizeofHeapTupleHeader
Definition: htup_details.h:184
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:146
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define BITMAPLEN(NATTS)
Definition: htup_details.h:547
signed int int32
Definition: c.h:347
int32 type_maximum_size(Oid type_oid, int32 typemod)
Definition: format_type.c:395
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
#define TOAST_TUPLE_THRESHOLD
Definition: heaptoast.h:48
TupleDesc rd_att
Definition: rel.h:84
#define MAXALIGN(LEN)
Definition: c.h:692
int i

◆ heapam_relation_nontransactional_truncate()

static void heapam_relation_nontransactional_truncate ( Relation  rel)
static

Definition at line 626 of file heapam_handler.c.

References RelationTruncate().

Referenced by SampleHeapTupleVisible().

627 {
628  RelationTruncate(rel, 0);
629 }
void RelationTruncate(Relation rel, BlockNumber nblocks)
Definition: storage.c:229

◆ heapam_relation_set_new_filenode()

static void heapam_relation_set_new_filenode ( Relation  rel,
const RelFileNode newrnode,
char  persistence,
TransactionId freezeXid,
MultiXactId minmulti 
)
static

Definition at line 576 of file heapam_handler.c.

References Assert, GetOldestMultiXactId(), INIT_FORKNUM, log_smgrcreate(), RelationData::rd_rel, RecentXmin, RelationCreateStorage(), smgrclose(), smgrcreate(), and smgrimmedsync().

Referenced by SampleHeapTupleVisible().

581 {
582  SMgrRelation srel;
583 
584  /*
585  * Initialize to the minimum XID that could put tuples in the table. We
586  * know that no xacts older than RecentXmin are still running, so that
587  * will do.
588  */
589  *freezeXid = RecentXmin;
590 
591  /*
592  * Similarly, initialize the minimum Multixact to the first value that
593  * could possibly be stored in tuples in the table. Running transactions
594  * could reuse values from their local cache, so we are careful to
595  * consider all currently running multis.
596  *
597  * XXX this could be refined further, but is it worth the hassle?
598  */
599  *minmulti = GetOldestMultiXactId();
600 
601  srel = RelationCreateStorage(*newrnode, persistence);
602 
603  /*
604  * If required, set up an init fork for an unlogged table so that it can
605  * be correctly reinitialized on restart. An immediate sync is required
606  * even if the page has been logged, because the write did not go through
607  * shared_buffers and therefore a concurrent checkpoint may have moved the
608  * redo pointer past our xlog record. Recovery may as well remove it
609  * while replaying, for example, XLOG_DBASE_CREATE or XLOG_TBLSPC_CREATE
610  * record. Therefore, logging is necessary even if wal_level=minimal.
611  */
612  if (persistence == RELPERSISTENCE_UNLOGGED)
613  {
614  Assert(rel->rd_rel->relkind == RELKIND_RELATION ||
615  rel->rd_rel->relkind == RELKIND_MATVIEW ||
616  rel->rd_rel->relkind == RELKIND_TOASTVALUE);
617  smgrcreate(srel, INIT_FORKNUM, false);
618  log_smgrcreate(newrnode, INIT_FORKNUM);
620  }
621 
622  smgrclose(srel);
623 }
void smgrclose(SMgrRelation reln)
Definition: smgr.c:256
void smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: smgr.c:333
TransactionId RecentXmin
Definition: snapmgr.c:167
Form_pg_class rd_rel
Definition: rel.h:83
SMgrRelation RelationCreateStorage(RelFileNode rnode, char relpersistence)
Definition: storage.c:78
MultiXactId GetOldestMultiXactId(void)
Definition: multixact.c:2493
#define Assert(condition)
Definition: c.h:739
void smgrimmedsync(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:637
void log_smgrcreate(const RelFileNode *rnode, ForkNumber forkNum)
Definition: storage.c:127

◆ heapam_scan_analyze_next_block()

static bool heapam_scan_analyze_next_block ( TableScanDesc  scan,
BlockNumber  blockno,
BufferAccessStrategy  bstrategy 
)
static

Definition at line 978 of file heapam_handler.c.

References BUFFER_LOCK_SHARE, FirstOffsetNumber, LockBuffer(), MAIN_FORKNUM, RBM_NORMAL, ReadBufferExtended(), HeapScanDescData::rs_cblock, HeapScanDescData::rs_cbuf, HeapScanDescData::rs_cindex, and TableScanDescData::rs_rd.

Referenced by SampleHeapTupleVisible().

980 {
981  HeapScanDesc hscan = (HeapScanDesc) scan;
982 
983  /*
984  * We must maintain a pin on the target page's buffer to ensure that
985  * concurrent activity - e.g. HOT pruning - doesn't delete tuples out from
986  * under us. Hence, pin the page until we are done looking at it. We
987  * also choose to hold sharelock on the buffer throughout --- we could
988  * release and re-acquire sharelock for each tuple, but since we aren't
989  * doing much work per tuple, the extra lock traffic is probably better
990  * avoided.
991  */
992  hscan->rs_cblock = blockno;
993  hscan->rs_cindex = FirstOffsetNumber;
995  blockno, RBM_NORMAL, bstrategy);
997 
998  /* in heap all blocks can contain tuples, so always return true */
999  return true;
1000 }
BlockNumber rs_cblock
Definition: heapam.h:58
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:642
struct HeapScanDescData * HeapScanDesc
Definition: heapam.h:72
#define FirstOffsetNumber
Definition: off.h:27
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3602
Buffer rs_cbuf
Definition: heapam.h:59
Relation rs_rd
Definition: relscan.h:34
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:87

◆ heapam_scan_analyze_next_tuple()

static bool heapam_scan_analyze_next_tuple ( TableScanDesc  scan,
TransactionId  OldestXmin,
double *  liverows,
double *  deadrows,
TupleTableSlot slot 
)
static

Definition at line 1003 of file heapam_handler.c.

References Assert, BufferHeapTupleTableSlot::base, BufferGetPage, elog, ERROR, ExecClearTuple(), ExecStoreBufferHeapTuple(), HEAPTUPLE_DEAD, HEAPTUPLE_DELETE_IN_PROGRESS, HEAPTUPLE_INSERT_IN_PROGRESS, HEAPTUPLE_LIVE, HEAPTUPLE_RECENTLY_DEAD, HeapTupleHeaderGetUpdateXid, HeapTupleHeaderGetXmin, HeapTupleSatisfiesVacuum(), InvalidBuffer, ItemIdGetLength, ItemIdIsDead, ItemIdIsNormal, ItemPointerSet, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, RelationGetRelid, HeapScanDescData::rs_cblock, HeapScanDescData::rs_cbuf, HeapScanDescData::rs_cindex, TableScanDescData::rs_rd, HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, TransactionIdIsCurrentTransactionId(), TTS_IS_BUFFERTUPLE, HeapTupleTableSlot::tupdata, and UnlockReleaseBuffer().

Referenced by SampleHeapTupleVisible().

1006 {
1007  HeapScanDesc hscan = (HeapScanDesc) scan;
1008  Page targpage;
1009  OffsetNumber maxoffset;
1010  BufferHeapTupleTableSlot *hslot;
1011 
1012  Assert(TTS_IS_BUFFERTUPLE(slot));
1013 
1014  hslot = (BufferHeapTupleTableSlot *) slot;
1015  targpage = BufferGetPage(hscan->rs_cbuf);
1016  maxoffset = PageGetMaxOffsetNumber(targpage);
1017 
1018  /* Inner loop over all tuples on the selected page */
1019  for (; hscan->rs_cindex <= maxoffset; hscan->rs_cindex++)
1020  {
1021  ItemId itemid;
1022  HeapTuple targtuple = &hslot->base.tupdata;
1023  bool sample_it = false;
1024 
1025  itemid = PageGetItemId(targpage, hscan->rs_cindex);
1026 
1027  /*
1028  * We ignore unused and redirect line pointers. DEAD line pointers
1029  * should be counted as dead, because we need vacuum to run to get rid
1030  * of them. Note that this rule agrees with the way that
1031  * heap_page_prune() counts things.
1032  */
1033  if (!ItemIdIsNormal(itemid))
1034  {
1035  if (ItemIdIsDead(itemid))
1036  *deadrows += 1;
1037  continue;
1038  }
1039 
1040  ItemPointerSet(&targtuple->t_self, hscan->rs_cblock, hscan->rs_cindex);
1041 
1042  targtuple->t_tableOid = RelationGetRelid(scan->rs_rd);
1043  targtuple->t_data = (HeapTupleHeader) PageGetItem(targpage, itemid);
1044  targtuple->t_len = ItemIdGetLength(itemid);
1045 
1046  switch (HeapTupleSatisfiesVacuum(targtuple, OldestXmin,
1047  hscan->rs_cbuf))
1048  {
1049  case HEAPTUPLE_LIVE:
1050  sample_it = true;
1051  *liverows += 1;
1052  break;
1053 
1054  case HEAPTUPLE_DEAD:
1056  /* Count dead and recently-dead rows */
1057  *deadrows += 1;
1058  break;
1059 
1061 
1062  /*
1063  * Insert-in-progress rows are not counted. We assume that
1064  * when the inserting transaction commits or aborts, it will
1065  * send a stats message to increment the proper count. This
1066  * works right only if that transaction ends after we finish
1067  * analyzing the table; if things happen in the other order,
1068  * its stats update will be overwritten by ours. However, the
1069  * error will be large only if the other transaction runs long
1070  * enough to insert many tuples, so assuming it will finish
1071  * after us is the safer option.
1072  *
1073  * A special case is that the inserting transaction might be
1074  * our own. In this case we should count and sample the row,
1075  * to accommodate users who load a table and analyze it in one
1076  * transaction. (pgstat_report_analyze has to adjust the
1077  * numbers we send to the stats collector to make this come
1078  * out right.)
1079  */
1081  {
1082  sample_it = true;
1083  *liverows += 1;
1084  }
1085  break;
1086 
1088 
1089  /*
1090  * We count and sample delete-in-progress rows the same as
1091  * live ones, so that the stats counters come out right if the
1092  * deleting transaction commits after us, per the same
1093  * reasoning given above.
1094  *
1095  * If the delete was done by our own transaction, however, we
1096  * must count the row as dead to make pgstat_report_analyze's
1097  * stats adjustments come out right. (Note: this works out
1098  * properly when the row was both inserted and deleted in our
1099  * xact.)
1100  *
1101  * The net effect of these choices is that we act as though an
1102  * IN_PROGRESS transaction hasn't happened yet, except if it
1103  * is our own transaction, which we assume has happened.
1104  *
1105  * This approach ensures that we behave sanely if we see both
1106  * the pre-image and post-image rows for a row being updated
1107  * by a concurrent transaction: we will sample the pre-image
1108  * but not the post-image. We also get sane results if the
1109  * concurrent transaction never commits.
1110  */
1112  *deadrows += 1;
1113  else
1114  {
1115  sample_it = true;
1116  *liverows += 1;
1117  }
1118  break;
1119 
1120  default:
1121  elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
1122  break;
1123  }
1124 
1125  if (sample_it)
1126  {
1127  ExecStoreBufferHeapTuple(targtuple, slot, hscan->rs_cbuf);
1128  hscan->rs_cindex++;
1129 
1130  /* note that we leave the buffer locked here! */
1131  return true;
1132  }
1133  }
1134 
1135  /* Now release the lock and pin on the page */
1136  UnlockReleaseBuffer(hscan->rs_cbuf);
1137  hscan->rs_cbuf = InvalidBuffer;
1138 
1139  /* also prevent old slot contents from having pin on page */
1140  ExecClearTuple(slot);
1141 
1142  return false;
1143 }
#define HeapTupleHeaderGetUpdateXid(tup)
Definition: htup_details.h:365
BlockNumber rs_cblock
Definition: heapam.h:58
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
bool TransactionIdIsCurrentTransactionId(TransactionId xid)
Definition: xact.c:853
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
#define InvalidBuffer
Definition: buf.h:25
#define ItemIdIsDead(itemId)
Definition: itemid.h:113
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
struct HeapScanDescData * HeapScanDesc
Definition: heapam.h:72
uint16 OffsetNumber
Definition: off.h:24
HeapTupleHeader t_data
Definition: htup.h:68
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3388
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
static TransactionId OldestXmin
Definition: vacuumlazy.c:145
Oid t_tableOid
Definition: htup.h:66
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
HTSV_Result HeapTupleSatisfiesVacuum(HeapTuple htup, TransactionId OldestXmin, Buffer buffer)
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
#define TTS_IS_BUFFERTUPLE(slot)
Definition: tuptable.h:231
TupleTableSlot * ExecStoreBufferHeapTuple(HeapTuple tuple, TupleTableSlot *slot, Buffer buffer)
Definition: execTuples.c:1362
Buffer rs_cbuf
Definition: heapam.h:59
#define Assert(condition)
Definition: c.h:739
#define ItemIdIsNormal(itemId)
Definition: itemid.h:99
#define HeapTupleHeaderGetXmin(tup)
Definition: htup_details.h:313
Relation rs_rd
Definition: relscan.h:34
#define elog(elevel,...)
Definition: elog.h:228
HeapTupleTableSlot base
Definition: tuptable.h:259
HeapTupleData tupdata
Definition: tuptable.h:253
#define RelationGetRelid(relation)
Definition: rel.h:422
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78
#define ItemPointerSet(pointer, blockNumber, offNum)
Definition: itemptr.h:127

◆ heapam_scan_bitmap_next_block()

static bool heapam_scan_bitmap_next_block ( TableScanDesc  scan,
TBMIterateResult tbmres 
)
static

Definition at line 2069 of file heapam_handler.c.

References Assert, TBMIterateResult::blockno, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferGetPage, CheckForSerializableConflictOut(), FirstOffsetNumber, heap_hot_search_buffer(), heap_page_prune_opt(), HeapTupleSatisfiesVisibility(), ItemIdGetLength, ItemIdIsNormal, ItemPointerGetOffsetNumber, ItemPointerSet, LockBuffer(), MaxHeapTuplesPerPage, TBMIterateResult::ntuples, OffsetNumberNext, TBMIterateResult::offsets, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, PredicateLockTuple(), RelationData::rd_id, ReleaseAndReadBuffer(), HeapScanDescData::rs_cblock, HeapScanDescData::rs_cbuf, HeapScanDescData::rs_cindex, HeapScanDescData::rs_nblocks, HeapScanDescData::rs_ntuples, TableScanDescData::rs_rd, TableScanDescData::rs_snapshot, HeapScanDescData::rs_vistuples, HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, and HeapTupleData::t_tableOid.

Referenced by SampleHeapTupleVisible().

2071 {
2072  HeapScanDesc hscan = (HeapScanDesc) scan;
2073  BlockNumber page = tbmres->blockno;
2074  Buffer buffer;
2075  Snapshot snapshot;
2076  int ntup;
2077 
2078  hscan->rs_cindex = 0;
2079  hscan->rs_ntuples = 0;
2080 
2081  /*
2082  * Ignore any claimed entries past what we think is the end of the
2083  * relation. It may have been extended after the start of our scan (we
2084  * only hold an AccessShareLock, and it could be inserts from this
2085  * backend).
2086  */
2087  if (page >= hscan->rs_nblocks)
2088  return false;
2089 
2090  /*
2091  * Acquire pin on the target heap page, trading in any pin we held before.
2092  */
2093  hscan->rs_cbuf = ReleaseAndReadBuffer(hscan->rs_cbuf,
2094  scan->rs_rd,
2095  page);
2096  hscan->rs_cblock = page;
2097  buffer = hscan->rs_cbuf;
2098  snapshot = scan->rs_snapshot;
2099 
2100  ntup = 0;
2101 
2102  /*
2103  * Prune and repair fragmentation for the whole page, if possible.
2104  */
2105  heap_page_prune_opt(scan->rs_rd, buffer);
2106 
2107  /*
2108  * We must hold share lock on the buffer content while examining tuple
2109  * visibility. Afterwards, however, the tuples we have found to be
2110  * visible are guaranteed good as long as we hold the buffer pin.
2111  */
2112  LockBuffer(buffer, BUFFER_LOCK_SHARE);
2113 
2114  /*
2115  * We need two separate strategies for lossy and non-lossy cases.
2116  */
2117  if (tbmres->ntuples >= 0)
2118  {
2119  /*
2120  * Bitmap is non-lossy, so we just look through the offsets listed in
2121  * tbmres; but we have to follow any HOT chain starting at each such
2122  * offset.
2123  */
2124  int curslot;
2125 
2126  for (curslot = 0; curslot < tbmres->ntuples; curslot++)
2127  {
2128  OffsetNumber offnum = tbmres->offsets[curslot];
2129  ItemPointerData tid;
2130  HeapTupleData heapTuple;
2131 
2132  ItemPointerSet(&tid, page, offnum);
2133  if (heap_hot_search_buffer(&tid, scan->rs_rd, buffer, snapshot,
2134  &heapTuple, NULL, true))
2135  hscan->rs_vistuples[ntup++] = ItemPointerGetOffsetNumber(&tid);
2136  }
2137  }
2138  else
2139  {
2140  /*
2141  * Bitmap is lossy, so we must examine each line pointer on the page.
2142  * But we can ignore HOT chains, since we'll check each tuple anyway.
2143  */
2144  Page dp = (Page) BufferGetPage(buffer);
2145  OffsetNumber maxoff = PageGetMaxOffsetNumber(dp);
2146  OffsetNumber offnum;
2147 
2148  for (offnum = FirstOffsetNumber; offnum <= maxoff; offnum = OffsetNumberNext(offnum))
2149  {
2150  ItemId lp;
2151  HeapTupleData loctup;
2152  bool valid;
2153 
2154  lp = PageGetItemId(dp, offnum);
2155  if (!ItemIdIsNormal(lp))
2156  continue;
2157  loctup.t_data = (HeapTupleHeader) PageGetItem((Page) dp, lp);
2158  loctup.t_len = ItemIdGetLength(lp);
2159  loctup.t_tableOid = scan->rs_rd->rd_id;
2160  ItemPointerSet(&loctup.t_self, page, offnum);
2161  valid = HeapTupleSatisfiesVisibility(&loctup, snapshot, buffer);
2162  if (valid)
2163  {
2164  hscan->rs_vistuples[ntup++] = offnum;
2165  PredicateLockTuple(scan->rs_rd, &loctup, snapshot);
2166  }
2167  CheckForSerializableConflictOut(valid, scan->rs_rd, &loctup,
2168  buffer, snapshot);
2169  }
2170  }
2171 
2172  LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
2173 
2174  Assert(ntup <= MaxHeapTuplesPerPage);
2175  hscan->rs_ntuples = ntup;
2176 
2177  return ntup > 0;
2178 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:86
BlockNumber rs_cblock
Definition: heapam.h:58
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
#define MaxHeapTuplesPerPage
Definition: htup_details.h:574
uint32 BlockNumber
Definition: block.h:31
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
void CheckForSerializableConflictOut(bool visible, Relation relation, HeapTuple tuple, Buffer buffer, Snapshot snapshot)
Definition: predicate.c:4041
struct HeapScanDescData * HeapScanDesc
Definition: heapam.h:72
uint16 OffsetNumber
Definition: off.h:24
HeapTupleHeader t_data
Definition: htup.h:68
BlockNumber blockno
Definition: tidbitmap.h:42
bool heap_hot_search_buffer(ItemPointer tid, Relation relation, Buffer buffer, Snapshot snapshot, HeapTuple heapTuple, bool *all_dead, bool first_call)
Definition: heapam.c:1526
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
OffsetNumber offsets[FLEXIBLE_ARRAY_MEMBER]
Definition: tidbitmap.h:46
#define FirstOffsetNumber
Definition: off.h:27
Oid t_tableOid
Definition: htup.h:66
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
Oid rd_id
Definition: rel.h:85
int rs_ntuples
Definition: heapam.h:69
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
BlockNumber rs_nblocks
Definition: heapam.h:51
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3602
Buffer rs_cbuf
Definition: heapam.h:59
#define Assert(condition)
Definition: c.h:739
OffsetNumber rs_vistuples[MaxHeapTuplesPerPage]
Definition: heapam.h:70
#define ItemIdIsNormal(itemId)
Definition: itemid.h:99
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation, BlockNumber blockNum)
Definition: bufmgr.c:1521
#define ItemPointerGetOffsetNumber(pointer)
Definition: itemptr.h:117
void PredicateLockTuple(Relation relation, HeapTuple tuple, Snapshot snapshot)
Definition: predicate.c:2548
Relation rs_rd
Definition: relscan.h:34
struct SnapshotData * rs_snapshot
Definition: relscan.h:35
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:87
void heap_page_prune_opt(Relation relation, Buffer buffer)
Definition: pruneheap.c:73
int Buffer
Definition: buf.h:23
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78
#define ItemPointerSet(pointer, blockNumber, offNum)
Definition: itemptr.h:127
bool HeapTupleSatisfiesVisibility(HeapTuple tup, Snapshot snapshot, Buffer buffer)

◆ heapam_scan_bitmap_next_tuple()

static bool heapam_scan_bitmap_next_tuple ( TableScanDesc  scan,
TBMIterateResult tbmres,
TupleTableSlot slot 
)
static

Definition at line 2181 of file heapam_handler.c.

References Assert, BufferGetPage, ExecStoreBufferHeapTuple(), ItemIdGetLength, ItemIdIsNormal, ItemPointerSet, PageGetItem, PageGetItemId, pgstat_count_heap_fetch, RelationData::rd_id, HeapScanDescData::rs_cblock, HeapScanDescData::rs_cbuf, HeapScanDescData::rs_cindex, HeapScanDescData::rs_ctup, HeapScanDescData::rs_ntuples, TableScanDescData::rs_rd, HeapScanDescData::rs_vistuples, HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, and HeapTupleData::t_tableOid.

Referenced by SampleHeapTupleVisible().

2184 {
2185  HeapScanDesc hscan = (HeapScanDesc) scan;
2186  OffsetNumber targoffset;
2187  Page dp;
2188  ItemId lp;
2189 
2190  /*
2191  * Out of range? If so, nothing more to look at on this page
2192  */
2193  if (hscan->rs_cindex < 0 || hscan->rs_cindex >= hscan->rs_ntuples)
2194  return false;
2195 
2196  targoffset = hscan->rs_vistuples[hscan->rs_cindex];
2197  dp = (Page) BufferGetPage(hscan->rs_cbuf);
2198  lp = PageGetItemId(dp, targoffset);
2199  Assert(ItemIdIsNormal(lp));
2200 
2201  hscan->rs_ctup.t_data = (HeapTupleHeader) PageGetItem((Page) dp, lp);
2202  hscan->rs_ctup.t_len = ItemIdGetLength(lp);
2203  hscan->rs_ctup.t_tableOid = scan->rs_rd->rd_id;
2204  ItemPointerSet(&hscan->rs_ctup.t_self, hscan->rs_cblock, targoffset);
2205 
2207 
2208  /*
2209  * Set up the result slot to point to this tuple. Note that the slot
2210  * acquires a pin on the buffer.
2211  */
2213  slot,
2214  hscan->rs_cbuf);
2215 
2216  hscan->rs_cindex++;
2217 
2218  return true;
2219 }
BlockNumber rs_cblock
Definition: heapam.h:58
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
struct HeapScanDescData * HeapScanDesc
Definition: heapam.h:72
HeapTupleData rs_ctup
Definition: heapam.h:65
uint16 OffsetNumber
Definition: off.h:24
HeapTupleHeader t_data
Definition: htup.h:68
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
ItemPointerData t_self
Definition: htup.h:65
#define pgstat_count_heap_fetch(rel)
Definition: pgstat.h:1368
uint32 t_len
Definition: htup.h:64
Oid t_tableOid
Definition: htup.h:66
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
Oid rd_id
Definition: rel.h:85
int rs_ntuples
Definition: heapam.h:69
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
TupleTableSlot * ExecStoreBufferHeapTuple(HeapTuple tuple, TupleTableSlot *slot, Buffer buffer)
Definition: execTuples.c:1362
Buffer rs_cbuf
Definition: heapam.h:59
#define Assert(condition)
Definition: c.h:739
OffsetNumber rs_vistuples[MaxHeapTuplesPerPage]
Definition: heapam.h:70
#define ItemIdIsNormal(itemId)
Definition: itemid.h:99
Relation rs_rd
Definition: relscan.h:34
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78
#define ItemPointerSet(pointer, blockNumber, offNum)
Definition: itemptr.h:127

◆ heapam_scan_get_blocks_done()

static BlockNumber heapam_scan_get_blocks_done ( HeapScanDesc  hscan)
static

Definition at line 1951 of file heapam_handler.c.

References ParallelBlockTableScanDescData::phs_nblocks, ParallelBlockTableScanDescData::phs_startblock, HeapScanDescData::rs_base, HeapScanDescData::rs_cblock, HeapScanDescData::rs_nblocks, TableScanDescData::rs_parallel, and HeapScanDescData::rs_startblock.

Referenced by heapam_index_build_range_scan().

1952 {
1953  ParallelBlockTableScanDesc bpscan = NULL;
1954  BlockNumber startblock;
1955  BlockNumber blocks_done;
1956 
1957  if (hscan->rs_base.rs_parallel != NULL)
1958  {
1960  startblock = bpscan->phs_startblock;
1961  }
1962  else
1963  startblock = hscan->rs_startblock;
1964 
1965  /*
1966  * Might have wrapped around the end of the relation, if startblock was
1967  * not zero.
1968  */
1969  if (hscan->rs_cblock > startblock)
1970  blocks_done = hscan->rs_cblock - startblock;
1971  else
1972  {
1973  BlockNumber nblocks;
1974 
1975  nblocks = bpscan != NULL ? bpscan->phs_nblocks : hscan->rs_nblocks;
1976  blocks_done = nblocks - startblock +
1977  hscan->rs_cblock;
1978  }
1979 
1980  return blocks_done;
1981 }
BlockNumber rs_cblock
Definition: heapam.h:58
struct ParallelBlockTableScanDescData * ParallelBlockTableScanDesc
Definition: relscan.h:82
TableScanDescData rs_base
Definition: heapam.h:48
uint32 BlockNumber
Definition: block.h:31
BlockNumber rs_startblock
Definition: heapam.h:52
BlockNumber rs_nblocks
Definition: heapam.h:51
struct ParallelTableScanDescData * rs_parallel
Definition: relscan.h:45

◆ heapam_scan_sample_next_block()

static bool heapam_scan_sample_next_block ( TableScanDesc  scan,
SampleScanState scanstate 
)
static

Definition at line 2222 of file heapam_handler.c.

References Assert, BlockNumberIsValid, BufferIsValid, heapgetpage(), InvalidBlockNumber, InvalidBuffer, TsmRoutine::NextSampleBlock, ReleaseBuffer(), HeapScanDescData::rs_cblock, HeapScanDescData::rs_cbuf, TableScanDescData::rs_flags, HeapScanDescData::rs_inited, HeapScanDescData::rs_nblocks, TableScanDescData::rs_rd, HeapScanDescData::rs_startblock, SO_ALLOW_SYNC, ss_report_location(), and SampleScanState::tsmroutine.

Referenced by SampleHeapTupleVisible().

2223 {
2224  HeapScanDesc hscan = (HeapScanDesc) scan;
2225  TsmRoutine *tsm = scanstate->tsmroutine;
2226  BlockNumber blockno;
2227 
2228  /* return false immediately if relation is empty */
2229  if (hscan->rs_nblocks == 0)
2230  return false;
2231 
2232  if (tsm->NextSampleBlock)
2233  {
2234  blockno = tsm->NextSampleBlock(scanstate, hscan->rs_nblocks);
2235  hscan->rs_cblock = blockno;
2236  }
2237  else
2238  {
2239  /* scanning table sequentially */
2240 
2241  if (hscan->rs_cblock == InvalidBlockNumber)
2242  {
2243  Assert(!hscan->rs_inited);
2244  blockno = hscan->rs_startblock;
2245  }
2246  else
2247  {
2248  Assert(hscan->rs_inited);
2249 
2250  blockno = hscan->rs_cblock + 1;
2251 
2252  if (blockno >= hscan->rs_nblocks)
2253  {
2254  /* wrap to beginning of rel, might not have started at 0 */
2255  blockno = 0;
2256  }
2257 
2258  /*
2259  * Report our new scan position for synchronization purposes.
2260  *
2261  * Note: we do this before checking for end of scan so that the
2262  * final state of the position hint is back at the start of the
2263  * rel. That's not strictly necessary, but otherwise when you run
2264  * the same query multiple times the starting position would shift
2265  * a little bit backwards on every invocation, which is confusing.
2266  * We don't guarantee any specific ordering in general, though.
2267  */
2268  if (scan->rs_flags & SO_ALLOW_SYNC)
2269  ss_report_location(scan->rs_rd, blockno);
2270 
2271  if (blockno == hscan->rs_startblock)
2272  {
2273  blockno = InvalidBlockNumber;
2274  }
2275  }
2276  }
2277 
2278  if (!BlockNumberIsValid(blockno))
2279  {
2280  if (BufferIsValid(hscan->rs_cbuf))
2281  ReleaseBuffer(hscan->rs_cbuf);
2282  hscan->rs_cbuf = InvalidBuffer;
2283  hscan->rs_cblock = InvalidBlockNumber;
2284  hscan->rs_inited = false;
2285 
2286  return false;
2287  }
2288 
2289  heapgetpage(scan, blockno);
2290  hscan->rs_inited = true;
2291 
2292  return true;
2293 }
BlockNumber rs_cblock
Definition: heapam.h:58
#define InvalidBuffer
Definition: buf.h:25
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3365
uint32 rs_flags
Definition: relscan.h:43
void heapgetpage(TableScanDesc sscan, BlockNumber page)
Definition: heapam.c:352
struct HeapScanDescData * HeapScanDesc
Definition: heapam.h:72
bool rs_inited
Definition: heapam.h:57
NextSampleBlock_function NextSampleBlock
Definition: tsmapi.h:73
BlockNumber rs_startblock
Definition: heapam.h:52
BlockNumber rs_nblocks
Definition: heapam.h:51
#define BlockNumberIsValid(blockNumber)
Definition: block.h:70
Buffer rs_cbuf
Definition: heapam.h:59
#define Assert(condition)
Definition: c.h:739
#define InvalidBlockNumber
Definition: block.h:33
#define BufferIsValid(bufnum)
Definition: bufmgr.h:113
struct TsmRoutine * tsmroutine
Definition: execnodes.h:1352
void ss_report_location(Relation rel, BlockNumber location)
Definition: syncscan.c:288
Relation rs_rd
Definition: relscan.h:34

◆ heapam_scan_sample_next_tuple()

static bool heapam_scan_sample_next_tuple ( TableScanDesc  scan,
SampleScanState scanstate,
TupleTableSlot slot 
)
static

Definition at line 2296 of file heapam_handler.c.

References Assert, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferGetPage, CHECK_FOR_INTERRUPTS, CheckForSerializableConflictOut(), ExecClearTuple(), ExecStoreBufferHeapTuple(), ItemIdGetLength, ItemIdIsNormal, ItemPointerSet, LockBuffer(), TsmRoutine::NextSampleTuple, OffsetNumberIsValid, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, PageIsAllVisible, pgstat_count_heap_getnext, HeapScanDescData::rs_cblock, HeapScanDescData::rs_cbuf, HeapScanDescData::rs_ctup, TableScanDescData::rs_flags, TableScanDescData::rs_rd, TableScanDescData::rs_snapshot, SampleHeapTupleVisible(), SO_ALLOW_PAGEMODE, HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, SnapshotData::takenDuringRecovery, and SampleScanState::tsmroutine.

Referenced by SampleHeapTupleVisible().

2298 {
2299  HeapScanDesc hscan = (HeapScanDesc) scan;
2300  TsmRoutine *tsm = scanstate->tsmroutine;
2301  BlockNumber blockno = hscan->rs_cblock;
2302  bool pagemode = (scan->rs_flags & SO_ALLOW_PAGEMODE) != 0;
2303 
2304  Page page;
2305  bool all_visible;
2306  OffsetNumber maxoffset;
2307 
2308  /*
2309  * When not using pagemode, we must lock the buffer during tuple
2310  * visibility checks.
2311  */
2312  if (!pagemode)
2314 
2315  page = (Page) BufferGetPage(hscan->rs_cbuf);
2316  all_visible = PageIsAllVisible(page) &&
2318  maxoffset = PageGetMaxOffsetNumber(page);
2319 
2320  for (;;)
2321  {
2322  OffsetNumber tupoffset;
2323 
2325 
2326  /* Ask the tablesample method which tuples to check on this page. */
2327  tupoffset = tsm->NextSampleTuple(scanstate,
2328  blockno,
2329  maxoffset);
2330 
2331  if (OffsetNumberIsValid(tupoffset))
2332  {
2333  ItemId itemid;
2334  bool visible;
2335  HeapTuple tuple = &(hscan->rs_ctup);
2336 
2337  /* Skip invalid tuple pointers. */
2338  itemid = PageGetItemId(page, tupoffset);
2339  if (!ItemIdIsNormal(itemid))
2340  continue;
2341 
2342  tuple->t_data = (HeapTupleHeader) PageGetItem(page, itemid);
2343  tuple->t_len = ItemIdGetLength(itemid);
2344  ItemPointerSet(&(tuple->t_self), blockno, tupoffset);
2345 
2346 
2347  if (all_visible)
2348  visible = true;
2349  else
2350  visible = SampleHeapTupleVisible(scan, hscan->rs_cbuf,
2351  tuple, tupoffset);
2352 
2353  /* in pagemode, heapgetpage did this for us */
2354  if (!pagemode)
2355  CheckForSerializableConflictOut(visible, scan->rs_rd, tuple,
2356  hscan->rs_cbuf, scan->rs_snapshot);
2357 
2358  /* Try next tuple from same page. */
2359  if (!visible)
2360  continue;
2361 
2362  /* Found visible tuple, return it. */
2363  if (!pagemode)
2365 
2366  ExecStoreBufferHeapTuple(tuple, slot, hscan->rs_cbuf);
2367 
2368  /* Count successfully-fetched tuples as heap fetches */
2370 
2371  return true;
2372  }
2373  else
2374  {
2375  /*
2376  * If we get here, it means we've exhausted the items on this page
2377  * and it's time to move to the next.
2378  */
2379  if (!pagemode)
2381 
2382  ExecClearTuple(slot);
2383  return false;
2384  }
2385  }
2386 
2387  Assert(0);
2388 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:86
BlockNumber rs_cblock
Definition: heapam.h:58
#define PageIsAllVisible(page)
Definition: bufpage.h:385
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
uint32 BlockNumber
Definition: block.h:31
uint32 rs_flags
Definition: relscan.h:43
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
void CheckForSerializableConflictOut(bool visible, Relation relation, HeapTuple tuple, Buffer buffer, Snapshot snapshot)
Definition: predicate.c:4041
struct HeapScanDescData * HeapScanDesc
Definition: heapam.h:72
HeapTupleData rs_ctup
Definition: heapam.h:65
uint16 OffsetNumber
Definition: off.h:24
HeapTupleHeader t_data
Definition: htup.h:68
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
NextSampleTuple_function NextSampleTuple
Definition: tsmapi.h:74
ItemPointerData t_self
Definition: htup.h:65
static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, OffsetNumber tupoffset)
uint32 t_len
Definition: htup.h:64
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3602
TupleTableSlot * ExecStoreBufferHeapTuple(HeapTuple tuple, TupleTableSlot *slot, Buffer buffer)
Definition: execTuples.c:1362
Buffer rs_cbuf
Definition: heapam.h:59
#define Assert(condition)
Definition: c.h:739
#define ItemIdIsNormal(itemId)
Definition: itemid.h:99
bool takenDuringRecovery
Definition: snapshot.h:184
struct TsmRoutine * tsmroutine
Definition: execnodes.h:1352
Relation rs_rd
Definition: relscan.h:34
struct SnapshotData * rs_snapshot
Definition: relscan.h:35
#define OffsetNumberIsValid(offsetNumber)
Definition: off.h:39
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:87
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define pgstat_count_heap_getnext(rel)
Definition: pgstat.h:1363
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78
#define ItemPointerSet(pointer, blockNumber, offNum)
Definition: itemptr.h:127

◆ heapam_slot_callbacks()

static const TupleTableSlotOps* heapam_slot_callbacks ( Relation  relation)
static

Definition at line 66 of file heapam_handler.c.

References TTSOpsBufferHeapTuple.

Referenced by SampleHeapTupleVisible().

67 {
68  return &TTSOpsBufferHeapTuple;
69 }
const TupleTableSlotOps TTSOpsBufferHeapTuple
Definition: execTuples.c:86

◆ heapam_tuple_complete_speculative()

static void heapam_tuple_complete_speculative ( Relation  relation,
TupleTableSlot slot,
uint32  specToken,
bool  succeeded 
)
static

Definition at line 282 of file heapam_handler.c.

References ExecFetchSlotHeapTuple(), heap_abort_speculative(), heap_finish_speculative(), pfree(), and TupleTableSlot::tts_tid.

Referenced by SampleHeapTupleVisible().

284 {
285  bool shouldFree = true;
286  HeapTuple tuple = ExecFetchSlotHeapTuple(slot, true, &shouldFree);
287 
288  /* adjust the tuple's state accordingly */
289  if (succeeded)
290  heap_finish_speculative(relation, &slot->tts_tid);
291  else
292  heap_abort_speculative(relation, &slot->tts_tid);
293 
294  if (shouldFree)
295  pfree(tuple);
296 }
void heap_abort_speculative(Relation relation, ItemPointer tid)
Definition: heapam.c:5567
void pfree(void *pointer)
Definition: mcxt.c:1056
HeapTuple ExecFetchSlotHeapTuple(TupleTableSlot *slot, bool materialize, bool *shouldFree)
Definition: execTuples.c:1614
void heap_finish_speculative(Relation relation, ItemPointer tid)
Definition: heapam.c:5476
ItemPointerData tts_tid
Definition: tuptable.h:130

◆ heapam_tuple_delete()

static TM_Result heapam_tuple_delete ( Relation  relation,
ItemPointer  tid,
CommandId  cid,
Snapshot  snapshot,
Snapshot  crosscheck,
bool  wait,
TM_FailureData tmfd,
bool  changingPart 
)
static

Definition at line 299 of file heapam_handler.c.

References heap_delete().

Referenced by SampleHeapTupleVisible().

302 {
303  /*
304  * Currently Deleting of index tuples are handled at vacuum, in case if
305  * the storage itself is cleaning the dead tuples by itself, it is the
306  * time to call the index tuple deletion also.
307  */
308  return heap_delete(relation, tid, cid, crosscheck, wait, tmfd, changingPart);
309 }
TM_Result heap_delete(Relation relation, ItemPointer tid, CommandId cid, Snapshot crosscheck, bool wait, TM_FailureData *tmfd, bool changingPart)
Definition: heapam.c:2442

◆ heapam_tuple_insert()

static void heapam_tuple_insert ( Relation  relation,
TupleTableSlot slot,
CommandId  cid,
int  options,
BulkInsertState  bistate 
)
static

Definition at line 240 of file heapam_handler.c.

References ExecFetchSlotHeapTuple(), heap_insert(), ItemPointerCopy, pfree(), RelationGetRelid, HeapTupleData::t_self, HeapTupleData::t_tableOid, TupleTableSlot::tts_tableOid, and TupleTableSlot::tts_tid.

Referenced by SampleHeapTupleVisible().

242 {
243  bool shouldFree = true;
244  HeapTuple tuple = ExecFetchSlotHeapTuple(slot, true, &shouldFree);
245 
246  /* Update the tuple with table oid */
247  slot->tts_tableOid = RelationGetRelid(relation);
248  tuple->t_tableOid = slot->tts_tableOid;
249 
250  /* Perform the insertion, and copy the resulting ItemPointer */
251  heap_insert(relation, tuple, cid, options, bistate);
252  ItemPointerCopy(&tuple->t_self, &slot->tts_tid);
253 
254  if (shouldFree)
255  pfree(tuple);
256 }
void heap_insert(Relation relation, HeapTuple tup, CommandId cid, int options, BulkInsertState bistate)
Definition: heapam.c:1868
Oid tts_tableOid
Definition: tuptable.h:131
void pfree(void *pointer)
Definition: mcxt.c:1056
ItemPointerData t_self
Definition: htup.h:65
Oid t_tableOid
Definition: htup.h:66
HeapTuple ExecFetchSlotHeapTuple(TupleTableSlot *slot, bool materialize, bool *shouldFree)
Definition: execTuples.c:1614
ItemPointerData tts_tid
Definition: tuptable.h:130
#define RelationGetRelid(relation)
Definition: rel.h:422
#define ItemPointerCopy(fromPointer, toPointer)
Definition: itemptr.h:161

◆ heapam_tuple_insert_speculative()

static void heapam_tuple_insert_speculative ( Relation  relation,
TupleTableSlot slot,
CommandId  cid,
int  options,
BulkInsertState  bistate,
uint32  specToken 
)
static

Definition at line 259 of file heapam_handler.c.

References ExecFetchSlotHeapTuple(), heap_insert(), HEAP_INSERT_SPECULATIVE, HeapTupleHeaderSetSpeculativeToken, ItemPointerCopy, pfree(), RelationGetRelid, HeapTupleData::t_data, HeapTupleData::t_self, HeapTupleData::t_tableOid, TupleTableSlot::tts_tableOid, and TupleTableSlot::tts_tid.

Referenced by SampleHeapTupleVisible().

262 {
263  bool shouldFree = true;
264  HeapTuple tuple = ExecFetchSlotHeapTuple(slot, true, &shouldFree);
265 
266  /* Update the tuple with table oid */
267  slot->tts_tableOid = RelationGetRelid(relation);
268  tuple->t_tableOid = slot->tts_tableOid;
269 
270  HeapTupleHeaderSetSpeculativeToken(tuple->t_data, specToken);
272 
273  /* Perform the insertion, and copy the resulting ItemPointer */
274  heap_insert(relation, tuple, cid, options, bistate);
275  ItemPointerCopy(&tuple->t_self, &slot->tts_tid);
276 
277  if (shouldFree)
278  pfree(tuple);
279 }
void heap_insert(Relation relation, HeapTuple tup, CommandId cid, int options, BulkInsertState bistate)
Definition: heapam.c:1868
Oid tts_tableOid
Definition: tuptable.h:131
#define HeapTupleHeaderSetSpeculativeToken(tup, token)
Definition: htup_details.h:440
#define HEAP_INSERT_SPECULATIVE
Definition: heapam.h:36
HeapTupleHeader t_data
Definition: htup.h:68
void pfree(void *pointer)
Definition: mcxt.c:1056
ItemPointerData t_self
Definition: htup.h:65
Oid t_tableOid
Definition: htup.h:66
HeapTuple ExecFetchSlotHeapTuple(TupleTableSlot *slot, bool materialize, bool *shouldFree)
Definition: execTuples.c:1614
ItemPointerData tts_tid
Definition: tuptable.h:130
#define RelationGetRelid(relation)
Definition: rel.h:422
#define ItemPointerCopy(fromPointer, toPointer)
Definition: itemptr.h:161

◆ heapam_tuple_lock()

static TM_Result heapam_tuple_lock ( Relation  relation,
ItemPointer  tid,
Snapshot  snapshot,
TupleTableSlot slot,
CommandId  cid,
LockTupleMode  mode,
LockWaitPolicy  wait_policy,
uint8  flags,
TM_FailureData tmfd 
)
static

Definition at line 347 of file heapam_handler.c.

References Assert, BufferHeapTupleTableSlot::base, BufferIsValid, TM_FailureData::cmax, ConditionalXactLockTableWait(), TM_FailureData::ctid, ereport, errcode(), ERRCODE_DATA_CORRUPTED, errmsg(), errmsg_internal(), ERROR, ExecStorePinnedBufferHeapTuple(), heap_fetch(), heap_lock_tuple(), HeapTupleHeaderGetCmin(), HeapTupleHeaderGetUpdateXid, HeapTupleHeaderGetXmin, HeapTupleHeaderIsSpeculative, InitDirtySnapshot, ItemPointerEquals(), ItemPointerIndicatesMovedPartitions, LockWaitBlock, LockWaitError, LockWaitSkip, RelationGetRelationName, RelationGetRelid, ReleaseBuffer(), HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleData::t_self, HeapTupleData::t_tableOid, TM_Deleted, TM_SelfModified, TM_Updated, TM_WouldBlock, TransactionIdEquals, TransactionIdIsCurrentTransactionId(), TransactionIdIsValid, TM_FailureData::traversed, TTS_IS_BUFFERTUPLE, TupleTableSlot::tts_tableOid, HeapTupleTableSlot::tupdata, TUPLE_LOCK_FLAG_FIND_LAST_VERSION, TUPLE_LOCK_FLAG_LOCK_UPDATE_IN_PROGRESS, XactLockTableWait(), XLTW_FetchUpdated, TM_FailureData::xmax, SnapshotData::xmax, and SnapshotData::xmin.

Referenced by SampleHeapTupleVisible().

351 {
353  TM_Result result;
354  Buffer buffer;
355  HeapTuple tuple = &bslot->base.tupdata;
356  bool follow_updates;
357 
358  follow_updates = (flags & TUPLE_LOCK_FLAG_LOCK_UPDATE_IN_PROGRESS) != 0;
359  tmfd->traversed = false;
360 
361  Assert(TTS_IS_BUFFERTUPLE(slot));
362 
363 tuple_lock_retry:
364  tuple->t_self = *tid;
365  result = heap_lock_tuple(relation, tuple, cid, mode, wait_policy,
366  follow_updates, &buffer, tmfd);
367 
368  if (result == TM_Updated &&
370  {
371  ReleaseBuffer(buffer);
372  /* Should not encounter speculative tuple on recheck */
374 
375  if (!ItemPointerEquals(&tmfd->ctid, &tuple->t_self))
376  {
377  SnapshotData SnapshotDirty;
378  TransactionId priorXmax;
379 
380  /* it was updated, so look at the updated version */
381  *tid = tmfd->ctid;
382  /* updated row should have xmin matching this xmax */
383  priorXmax = tmfd->xmax;
384 
385  /* signal that a tuple later in the chain is getting locked */
386  tmfd->traversed = true;
387 
388  /*
389  * fetch target tuple
390  *
391  * Loop here to deal with updated or busy tuples
392  */
393  InitDirtySnapshot(SnapshotDirty);
394  for (;;)
395  {
397  ereport(ERROR,
398  (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
399  errmsg("tuple to be locked was already moved to another partition due to concurrent update")));
400 
401  tuple->t_self = *tid;
402  if (heap_fetch(relation, &SnapshotDirty, tuple, &buffer))
403  {
404  /*
405  * If xmin isn't what we're expecting, the slot must have
406  * been recycled and reused for an unrelated tuple. This
407  * implies that the latest version of the row was deleted,
408  * so we need do nothing. (Should be safe to examine xmin
409  * without getting buffer's content lock. We assume
410  * reading a TransactionId to be atomic, and Xmin never
411  * changes in an existing tuple, except to invalid or
412  * frozen, and neither of those can match priorXmax.)
413  */
415  priorXmax))
416  {
417  ReleaseBuffer(buffer);
418  return TM_Deleted;
419  }
420 
421  /* otherwise xmin should not be dirty... */
422  if (TransactionIdIsValid(SnapshotDirty.xmin))
423  ereport(ERROR,
425  errmsg_internal("t_xmin is uncommitted in tuple to be updated")));
426 
427  /*
428  * If tuple is being updated by other transaction then we
429  * have to wait for its commit/abort, or die trying.
430  */
431  if (TransactionIdIsValid(SnapshotDirty.xmax))
432  {
433  ReleaseBuffer(buffer);
434  switch (wait_policy)
435  {
436  case LockWaitBlock:
437  XactLockTableWait(SnapshotDirty.xmax,
438  relation, &tuple->t_self,
440  break;
441  case LockWaitSkip:
442  if (!ConditionalXactLockTableWait(SnapshotDirty.xmax))
443  /* skip instead of waiting */
444  return TM_WouldBlock;
445  break;
446  case LockWaitError:
447  if (!ConditionalXactLockTableWait(SnapshotDirty.xmax))
448  ereport(ERROR,
449  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
450  errmsg("could not obtain lock on row in relation \"%s\"",
451  RelationGetRelationName(relation))));
452  break;
453  }
454  continue; /* loop back to repeat heap_fetch */
455  }
456 
457  /*
458  * If tuple was inserted by our own transaction, we have
459  * to check cmin against cid: cmin >= current CID means
460  * our command cannot see the tuple, so we should ignore
461  * it. Otherwise heap_lock_tuple() will throw an error,
462  * and so would any later attempt to update or delete the
463  * tuple. (We need not check cmax because
464  * HeapTupleSatisfiesDirty will consider a tuple deleted
465  * by our transaction dead, regardless of cmax.) We just
466  * checked that priorXmax == xmin, so we can test that
467  * variable instead of doing HeapTupleHeaderGetXmin again.
468  */
469  if (TransactionIdIsCurrentTransactionId(priorXmax) &&
470  HeapTupleHeaderGetCmin(tuple->t_data) >= cid)
471  {
472  tmfd->xmax = priorXmax;
473 
474  /*
475  * Cmin is the problematic value, so store that. See
476  * above.
477  */
478  tmfd->cmax = HeapTupleHeaderGetCmin(tuple->t_data);
479  ReleaseBuffer(buffer);
480  return TM_SelfModified;
481  }
482 
483  /*
484  * This is a live tuple, so try to lock it again.
485  */
486  ReleaseBuffer(buffer);
487  goto tuple_lock_retry;
488  }
489 
490  /*
491  * If the referenced slot was actually empty, the latest
492  * version of the row must have been deleted, so we need do
493  * nothing.
494  */
495  if (tuple->t_data == NULL)
496  {
497  return TM_Deleted;
498  }
499 
500  /*
501  * As above, if xmin isn't what we're expecting, do nothing.
502  */
504  priorXmax))
505  {
506  if (BufferIsValid(buffer))
507  ReleaseBuffer(buffer);
508  return TM_Deleted;
509  }
510 
511  /*
512  * If we get here, the tuple was found but failed
513  * SnapshotDirty. Assuming the xmin is either a committed xact
514  * or our own xact (as it certainly should be if we're trying
515  * to modify the tuple), this must mean that the row was
516  * updated or deleted by either a committed xact or our own
517  * xact. If it was deleted, we can ignore it; if it was
518  * updated then chain up to the next version and repeat the
519  * whole process.
520  *
521  * As above, it should be safe to examine xmax and t_ctid
522  * without the buffer content lock, because they can't be
523  * changing.
524  */
525  if (ItemPointerEquals(&tuple->t_self, &tuple->t_data->t_ctid))
526  {
527  /* deleted, so forget about it */
528  if (BufferIsValid(buffer))
529  ReleaseBuffer(buffer);
530  return TM_Deleted;
531  }
532 
533  /* updated, so look at the updated row */
534  *tid = tuple->t_data->t_ctid;
535  /* updated row should have xmin matching this xmax */
536  priorXmax = HeapTupleHeaderGetUpdateXid(tuple->t_data);
537  if (BufferIsValid(buffer))
538  ReleaseBuffer(buffer);
539  /* loop back to fetch next in chain */
540  }
541  }
542  else
543  {
544  /* tuple was deleted, so give up */
545  return TM_Deleted;
546  }
547  }
548 
549  slot->tts_tableOid = RelationGetRelid(relation);
550  tuple->t_tableOid = slot->tts_tableOid;
551 
552  /* store in slot, transferring existing pin */
553  ExecStorePinnedBufferHeapTuple(tuple, slot, buffer);
554 
555  return result;
556 }
#define HeapTupleHeaderGetUpdateXid(tup)
Definition: htup_details.h:365
Oid tts_tableOid
Definition: tuptable.h:131
ItemPointerData ctid
Definition: tableam.h:123
static PgChecksumMode mode
Definition: pg_checksums.c:61
#define InitDirtySnapshot(snapshotdata)
Definition: snapmgr.h:76
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
uint32 TransactionId
Definition: c.h:514
bool TransactionIdIsCurrentTransactionId(TransactionId xid)
Definition: xact.c:853
CommandId HeapTupleHeaderGetCmin(HeapTupleHeader tup)
Definition: combocid.c:104
bool heap_fetch(Relation relation, Snapshot snapshot, HeapTuple tuple, Buffer *userbuf)
Definition: heapam.c:1412
CommandId cmax
Definition: tableam.h:125
#define HeapTupleHeaderIsSpeculative(tup)
Definition: htup_details.h:429
int errcode(int sqlerrcode)
Definition: elog.c:608
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3365
#define TUPLE_LOCK_FLAG_LOCK_UPDATE_IN_PROGRESS
Definition: tableam.h:137
bool ConditionalXactLockTableWait(TransactionId xid)
Definition: lmgr.c:697
HeapTupleHeader t_data
Definition: htup.h:68
TransactionId xmax
Definition: tableam.h:124
#define ERROR
Definition: elog.h:43
ItemPointerData t_ctid
Definition: htup_details.h:160
ItemPointerData t_self
Definition: htup.h:65
#define RelationGetRelationName(relation)
Definition: rel.h:456
Oid t_tableOid
Definition: htup.h:66
TransactionId xmax
Definition: snapshot.h:158
TransactionId xmin
Definition: snapshot.h:157
#define ereport(elevel, rest)
Definition: elog.h:141
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:45
TM_Result
Definition: tableam.h:68
#define TTS_IS_BUFFERTUPLE(slot)
Definition: tuptable.h:231
#define ItemPointerIndicatesMovedPartitions(pointer)
Definition: itemptr.h:184
int errmsg_internal(const char *fmt,...)
Definition: elog.c:909
void XactLockTableWait(TransactionId xid, Relation rel, ItemPointer ctid, XLTW_Oper oper)
Definition: lmgr.c:624
#define Assert(condition)
Definition: c.h:739
#define HeapTupleHeaderGetXmin(tup)
Definition: htup_details.h:313
#define BufferIsValid(bufnum)
Definition: bufmgr.h:113
#define TUPLE_LOCK_FLAG_FIND_LAST_VERSION
Definition: tableam.h:139
TupleTableSlot * ExecStorePinnedBufferHeapTuple(HeapTuple tuple, TupleTableSlot *slot, Buffer buffer)
Definition: execTuples.c:1388
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:29
TM_Result heap_lock_tuple(Relation relation, HeapTuple tuple, CommandId cid, LockTupleMode mode, LockWaitPolicy wait_policy, bool follow_updates, Buffer *buffer, TM_FailureData *tmfd)
Definition: heapam.c:3973
int errmsg(const char *fmt,...)
Definition: elog.c:822
HeapTupleTableSlot base
Definition: tuptable.h:259
#define TransactionIdIsValid(xid)
Definition: transam.h:41
bool traversed
Definition: tableam.h:126
HeapTupleData tupdata
Definition: tuptable.h:253
int Buffer
Definition: buf.h:23
#define RelationGetRelid(relation)
Definition: rel.h:422

◆ heapam_tuple_satisfies_snapshot()

static bool heapam_tuple_satisfies_snapshot ( Relation  rel,
TupleTableSlot slot,
Snapshot  snapshot 
)
static

Definition at line 212 of file heapam_handler.c.

References Assert, BufferHeapTupleTableSlot::base, BufferHeapTupleTableSlot::buffer, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferIsValid, HeapTupleSatisfiesVisibility(), LockBuffer(), TTS_IS_BUFFERTUPLE, and HeapTupleTableSlot::tuple.

Referenced by SampleHeapTupleVisible().

214 {
216  bool res;
217 
218  Assert(TTS_IS_BUFFERTUPLE(slot));
219  Assert(BufferIsValid(bslot->buffer));
220 
221  /*
222  * We need buffer pin and lock to call HeapTupleSatisfiesVisibility.
223  * Caller should be holding pin, but not lock.
224  */
226  res = HeapTupleSatisfiesVisibility(bslot->base.tuple, snapshot,
227  bslot->buffer);
229 
230  return res;
231 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:86
HeapTuple tuple
Definition: tuptable.h:250
#define TTS_IS_BUFFERTUPLE(slot)
Definition: tuptable.h:231
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3602
#define Assert(condition)
Definition: c.h:739
#define BufferIsValid(bufnum)
Definition: bufmgr.h:113
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:87
HeapTupleTableSlot base
Definition: tuptable.h:259
bool HeapTupleSatisfiesVisibility(HeapTuple tup, Snapshot snapshot, Buffer buffer)

◆ heapam_tuple_tid_valid()

static bool heapam_tuple_tid_valid ( TableScanDesc  scan,
ItemPointer  tid 
)
static

Definition at line 203 of file heapam_handler.c.

References ItemPointerGetBlockNumber, ItemPointerIsValid, and HeapScanDescData::rs_nblocks.

Referenced by SampleHeapTupleVisible().

204 {
205  HeapScanDesc hscan = (HeapScanDesc) scan;
206 
207  return ItemPointerIsValid(tid) &&
209 }
#define ItemPointerIsValid(pointer)
Definition: itemptr.h:82
struct HeapScanDescData * HeapScanDesc
Definition: heapam.h:72
BlockNumber rs_nblocks
Definition: heapam.h:51
#define ItemPointerGetBlockNumber(pointer)
Definition: itemptr.h:98

◆ heapam_tuple_update()

static TM_Result heapam_tuple_update ( Relation  relation,
ItemPointer  otid,
TupleTableSlot slot,
CommandId  cid,
Snapshot  snapshot,
Snapshot  crosscheck,
bool  wait,
TM_FailureData tmfd,
LockTupleMode lockmode,
bool update_indexes 
)
static

Definition at line 313 of file heapam_handler.c.

References ExecFetchSlotHeapTuple(), heap_update(), HeapTupleIsHeapOnly, ItemPointerCopy, pfree(), RelationGetRelid, HeapTupleData::t_self, HeapTupleData::t_tableOid, TM_Ok, TupleTableSlot::tts_tableOid, and TupleTableSlot::tts_tid.

Referenced by SampleHeapTupleVisible().

317 {
318  bool shouldFree = true;
319  HeapTuple tuple = ExecFetchSlotHeapTuple(slot, true, &shouldFree);
320  TM_Result result;
321 
322  /* Update the tuple with table oid */
323  slot->tts_tableOid = RelationGetRelid(relation);
324  tuple->t_tableOid = slot->tts_tableOid;
325 
326  result = heap_update(relation, otid, tuple, cid, crosscheck, wait,
327  tmfd, lockmode);
328  ItemPointerCopy(&tuple->t_self, &slot->tts_tid);
329 
330  /*
331  * Decide whether new index entries are needed for the tuple
332  *
333  * Note: heap_update returns the tid (location) of the new tuple in the
334  * t_self field.
335  *
336  * If it's a HOT update, we mustn't insert new index entries.
337  */
338  *update_indexes = result == TM_Ok && !HeapTupleIsHeapOnly(tuple);
339 
340  if (shouldFree)
341  pfree(tuple);
342 
343  return result;
344 }
Oid tts_tableOid
Definition: tuptable.h:131
void pfree(void *pointer)
Definition: mcxt.c:1056
ItemPointerData t_self
Definition: htup.h:65
Oid t_tableOid
Definition: htup.h:66
HeapTuple ExecFetchSlotHeapTuple(TupleTableSlot *slot, bool materialize, bool *shouldFree)
Definition: execTuples.c:1614
TM_Result
Definition: tableam.h:68
#define HeapTupleIsHeapOnly(tuple)
Definition: htup_details.h:685
Definition: tableam.h:74
TM_Result heap_update(Relation relation, ItemPointer otid, HeapTuple newtup, CommandId cid, Snapshot crosscheck, bool wait, TM_FailureData *tmfd, LockTupleMode *lockmode)
Definition: heapam.c:2893
ItemPointerData tts_tid
Definition: tuptable.h:130
#define RelationGetRelid(relation)
Definition: rel.h:422
#define ItemPointerCopy(fromPointer, toPointer)
Definition: itemptr.h:161

◆ reform_and_rewrite_tuple()

static void reform_and_rewrite_tuple ( HeapTuple  tuple,
Relation  OldHeap,
Relation  NewHeap,
Datum values,
bool isnull,
RewriteState  rwstate 
)
static

Definition at line 2413 of file heapam_handler.c.

References heap_deform_tuple(), heap_form_tuple(), heap_freetuple(), i, TupleDescData::natts, RelationGetDescr, rewrite_heap_tuple(), and TupleDescAttr.

Referenced by heapam_relation_copy_for_cluster().

2416 {
2417  TupleDesc oldTupDesc = RelationGetDescr(OldHeap);
2418  TupleDesc newTupDesc = RelationGetDescr(NewHeap);
2419  HeapTuple copiedTuple;
2420  int i;
2421 
2422  heap_deform_tuple(tuple, oldTupDesc, values, isnull);
2423 
2424  /* Be sure to null out any dropped columns */
2425  for (i = 0; i < newTupDesc->natts; i++)
2426  {
2427  if (TupleDescAttr(newTupDesc, i)->attisdropped)
2428  isnull[i] = true;
2429  }
2430 
2431  copiedTuple = heap_form_tuple(newTupDesc, values, isnull);
2432 
2433  /* The heap rewrite module does the rest */
2434  rewrite_heap_tuple(rwstate, tuple, copiedTuple);
2435 
2436  heap_freetuple(copiedTuple);
2437 }
#define RelationGetDescr(relation)
Definition: rel.h:448
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:1249
static Datum values[MAXATTR]
Definition: bootstrap.c:167
int i
void rewrite_heap_tuple(RewriteState state, HeapTuple old_tuple, HeapTuple new_tuple)
Definition: rewriteheap.c:371

◆ SampleHeapTupleVisible()

static bool SampleHeapTupleVisible ( TableScanDesc  scan,
Buffer  buffer,
HeapTuple  tuple,
OffsetNumber  tupoffset 
)
static

Definition at line 2443 of file heapam_handler.c.

References heap_beginscan(), heap_compute_xid_horizon_for_tuples(), heap_endscan(), heap_get_latest_tid(), heap_getnextslot(), heap_multi_insert(), heap_rescan(), heap_vacuum_rel(), heapam_estimate_rel_size(), heapam_fetch_row_version(), heapam_finish_bulk_insert(), heapam_index_build_range_scan(), heapam_index_fetch_begin(), heapam_index_fetch_end(), heapam_index_fetch_reset(), heapam_index_fetch_tuple(), heapam_index_validate_scan(), heapam_relation_copy_data(), heapam_relation_copy_for_cluster(), heapam_relation_needs_toast_table(), heapam_relation_nontransactional_truncate(), heapam_relation_set_new_filenode(), heapam_scan_analyze_next_block(), heapam_scan_analyze_next_tuple(), heapam_scan_bitmap_next_block(), heapam_scan_bitmap_next_tuple(), heapam_scan_sample_next_block(), heapam_scan_sample_next_tuple(), heapam_slot_callbacks(), heapam_tuple_complete_speculative(), heapam_tuple_delete(), heapam_tuple_insert(), heapam_tuple_insert_speculative(), heapam_tuple_lock(), heapam_tuple_satisfies_snapshot(), heapam_tuple_tid_valid(), heapam_tuple_update(), HeapTupleSatisfiesVisibility(), TableScanDescData::rs_flags, HeapScanDescData::rs_ntuples, TableScanDescData::rs_snapshot, HeapScanDescData::rs_vistuples, SO_ALLOW_PAGEMODE, T_TableAmRoutine, table_block_parallelscan_estimate(), table_block_parallelscan_initialize(), table_block_parallelscan_reinitialize(), table_block_relation_size(), and TableAmRoutine::type.

Referenced by heapam_scan_sample_next_tuple().

2446 {
2447  HeapScanDesc hscan = (HeapScanDesc) scan;
2448 
2449  if (scan->rs_flags & SO_ALLOW_PAGEMODE)
2450  {
2451  /*
2452  * In pageatatime mode, heapgetpage() already did visibility checks,
2453  * so just look at the info it left in rs_vistuples[].
2454  *
2455  * We use a binary search over the known-sorted array. Note: we could
2456  * save some effort if we insisted that NextSampleTuple select tuples
2457  * in increasing order, but it's not clear that there would be enough
2458  * gain to justify the restriction.
2459  */
2460  int start = 0,
2461  end = hscan->rs_ntuples - 1;
2462 
2463  while (start <= end)
2464  {
2465  int mid = (start + end) / 2;
2466  OffsetNumber curoffset = hscan->rs_vistuples[mid];
2467 
2468  if (tupoffset == curoffset)
2469  return true;
2470  else if (tupoffset < curoffset)
2471  end = mid - 1;
2472  else
2473  start = mid + 1;
2474  }
2475 
2476  return false;
2477  }
2478  else
2479  {
2480  /* Otherwise, we have to check the tuple individually. */
2481  return HeapTupleSatisfiesVisibility(tuple, scan->rs_snapshot,
2482  buffer);
2483  }
2484 }
uint32 rs_flags
Definition: relscan.h:43
struct HeapScanDescData * HeapScanDesc
Definition: heapam.h:72
uint16 OffsetNumber
Definition: off.h:24
int rs_ntuples
Definition: heapam.h:69
OffsetNumber rs_vistuples[MaxHeapTuplesPerPage]
Definition: heapam.h:70
struct SnapshotData * rs_snapshot
Definition: relscan.h:35
bool HeapTupleSatisfiesVisibility(HeapTuple tup, Snapshot snapshot, Buffer buffer)

Variable Documentation

◆ heapam_methods

static const TableAmRoutine heapam_methods
static

Definition at line 57 of file heapam_handler.c.

Referenced by GetHeapamTableAmRoutine().