PostgreSQL Source Code  git master
rewriteheap.h File Reference
#include "access/htup.h"
#include "storage/itemptr.h"
#include "storage/relfilenode.h"
#include "utils/relcache.h"
Include dependency graph for rewriteheap.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  LogicalRewriteMappingData
 

Macros

#define LOGICAL_REWRITE_FORMAT   "map-%x-%x-%X_%X-%x-%x"
 

Typedefs

typedef struct RewriteStateDataRewriteState
 
typedef struct LogicalRewriteMappingData LogicalRewriteMappingData
 

Functions

RewriteState begin_heap_rewrite (Relation OldHeap, Relation NewHeap, TransactionId OldestXmin, TransactionId FreezeXid, MultiXactId MultiXactCutoff)
 
void end_heap_rewrite (RewriteState state)
 
void rewrite_heap_tuple (RewriteState state, HeapTuple oldTuple, HeapTuple newTuple)
 
bool rewrite_heap_dead_tuple (RewriteState state, HeapTuple oldTuple)
 
void CheckPointLogicalRewriteHeap (void)
 

Macro Definition Documentation

◆ LOGICAL_REWRITE_FORMAT

#define LOGICAL_REWRITE_FORMAT   "map-%x-%x-%X_%X-%x-%x"

Typedef Documentation

◆ LogicalRewriteMappingData

◆ RewriteState

typedef struct RewriteStateData* RewriteState

Definition at line 22 of file rewriteheap.h.

Function Documentation

◆ begin_heap_rewrite()

RewriteState begin_heap_rewrite ( Relation  OldHeap,
Relation  NewHeap,
TransactionId  OldestXmin,
TransactionId  FreezeXid,
MultiXactId  MultiXactCutoff 
)

Definition at line 237 of file rewriteheap.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, CurrentMemoryContext, HASHCTL::entrysize, HASH_BLOBS, HASH_CONTEXT, hash_create(), HASH_ELEM, HASHCTL::hcxt, HASHCTL::keysize, logical_begin_heap_rewrite(), MemoryContextSwitchTo(), palloc(), palloc0(), RelationGetNumberOfBlocks, RewriteStateData::rs_blockno, RewriteStateData::rs_buffer, RewriteStateData::rs_buffer_valid, RewriteStateData::rs_cutoff_multi, RewriteStateData::rs_cxt, RewriteStateData::rs_freeze_xid, RewriteStateData::rs_new_rel, RewriteStateData::rs_old_new_tid_map, RewriteStateData::rs_old_rel, RewriteStateData::rs_oldest_xmin, and RewriteStateData::rs_unresolved_tups.

Referenced by heapam_relation_copy_for_cluster().

239 {
241  MemoryContext rw_cxt;
242  MemoryContext old_cxt;
243  HASHCTL hash_ctl;
244 
245  /*
246  * To ease cleanup, make a separate context that will contain the
247  * RewriteState struct itself plus all subsidiary data.
248  */
250  "Table rewrite",
252  old_cxt = MemoryContextSwitchTo(rw_cxt);
253 
254  /* Create and fill in the state struct */
255  state = palloc0(sizeof(RewriteStateData));
256 
257  state->rs_old_rel = old_heap;
258  state->rs_new_rel = new_heap;
259  state->rs_buffer = (Page) palloc(BLCKSZ);
260  /* new_heap needn't be empty, just locked */
261  state->rs_blockno = RelationGetNumberOfBlocks(new_heap);
262  state->rs_buffer_valid = false;
263  state->rs_oldest_xmin = oldest_xmin;
264  state->rs_freeze_xid = freeze_xid;
265  state->rs_cutoff_multi = cutoff_multi;
266  state->rs_cxt = rw_cxt;
267 
268  /* Initialize hash tables used to track update chains */
269  memset(&hash_ctl, 0, sizeof(hash_ctl));
270  hash_ctl.keysize = sizeof(TidHashKey);
271  hash_ctl.entrysize = sizeof(UnresolvedTupData);
272  hash_ctl.hcxt = state->rs_cxt;
273 
274  state->rs_unresolved_tups =
275  hash_create("Rewrite / Unresolved ctids",
276  128, /* arbitrary initial size */
277  &hash_ctl,
279 
280  hash_ctl.entrysize = sizeof(OldToNewMappingData);
281 
282  state->rs_old_new_tid_map =
283  hash_create("Rewrite / Old to new tid map",
284  128, /* arbitrary initial size */
285  &hash_ctl,
287 
288  MemoryContextSwitchTo(old_cxt);
289 
291 
292  return state;
293 }
#define AllocSetContextCreate
Definition: memutils.h:170
#define HASH_CONTEXT
Definition: hsearch.h:93
#define HASH_ELEM
Definition: hsearch.h:87
MemoryContext hcxt
Definition: hsearch.h:78
TransactionId rs_freeze_xid
Definition: rewriteheap.c:142
MultiXactId rs_cutoff_multi
Definition: rewriteheap.c:146
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Size entrysize
Definition: hsearch.h:73
Relation rs_new_rel
Definition: rewriteheap.c:135
HTAB * rs_unresolved_tups
Definition: rewriteheap.c:151
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
TransactionId rs_oldest_xmin
Definition: rewriteheap.c:140
#define HASH_BLOBS
Definition: hsearch.h:88
MemoryContext rs_cxt
Definition: rewriteheap.c:148
void * palloc0(Size size)
Definition: mcxt.c:980
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:318
Size keysize
Definition: hsearch.h:72
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:211
static void logical_begin_heap_rewrite(RewriteState state)
Definition: rewriteheap.c:794
HTAB * rs_old_new_tid_map
Definition: rewriteheap.c:152
Definition: regguts.h:298
void * palloc(Size size)
Definition: mcxt.c:949
Relation rs_old_rel
Definition: rewriteheap.c:134
BlockNumber rs_blockno
Definition: rewriteheap.c:137
Pointer Page
Definition: bufpage.h:78

◆ CheckPointLogicalRewriteHeap()

void CheckPointLogicalRewriteHeap ( void  )

Definition at line 1195 of file rewriteheap.c.

References AllocateDir(), CloseTransientFile(), dirent::d_name, data_sync_elevel(), DEBUG1, elog, ereport, errcode_for_file_access(), errmsg(), ERROR, fd(), FreeDir(), GetRedoRecPtr(), InvalidXLogRecPtr, LOGICAL_REWRITE_FORMAT, lstat, MAXPGPATH, OpenTransientFile(), PG_BINARY, pg_fsync(), pgstat_report_wait_end(), pgstat_report_wait_start(), ReadDir(), ReplicationSlotsComputeLogicalRestartLSN(), S_ISREG, snprintf, stat, and WAIT_EVENT_LOGICAL_REWRITE_CHECKPOINT_SYNC.

Referenced by CheckPointGuts().

1196 {
1197  XLogRecPtr cutoff;
1198  XLogRecPtr redo;
1199  DIR *mappings_dir;
1200  struct dirent *mapping_de;
1201  char path[MAXPGPATH + 20];
1202 
1203  /*
1204  * We start of with a minimum of the last redo pointer. No new decoding
1205  * slot will start before that, so that's a safe upper bound for removal.
1206  */
1207  redo = GetRedoRecPtr();
1208 
1209  /* now check for the restart ptrs from existing slots */
1211 
1212  /* don't start earlier than the restart lsn */
1213  if (cutoff != InvalidXLogRecPtr && redo < cutoff)
1214  cutoff = redo;
1215 
1216  mappings_dir = AllocateDir("pg_logical/mappings");
1217  while ((mapping_de = ReadDir(mappings_dir, "pg_logical/mappings")) != NULL)
1218  {
1219  struct stat statbuf;
1220  Oid dboid;
1221  Oid relid;
1222  XLogRecPtr lsn;
1223  TransactionId rewrite_xid;
1224  TransactionId create_xid;
1225  uint32 hi,
1226  lo;
1227 
1228  if (strcmp(mapping_de->d_name, ".") == 0 ||
1229  strcmp(mapping_de->d_name, "..") == 0)
1230  continue;
1231 
1232  snprintf(path, sizeof(path), "pg_logical/mappings/%s", mapping_de->d_name);
1233  if (lstat(path, &statbuf) == 0 && !S_ISREG(statbuf.st_mode))
1234  continue;
1235 
1236  /* Skip over files that cannot be ours. */
1237  if (strncmp(mapping_de->d_name, "map-", 4) != 0)
1238  continue;
1239 
1240  if (sscanf(mapping_de->d_name, LOGICAL_REWRITE_FORMAT,
1241  &dboid, &relid, &hi, &lo, &rewrite_xid, &create_xid) != 6)
1242  elog(ERROR, "could not parse filename \"%s\"", mapping_de->d_name);
1243 
1244  lsn = ((uint64) hi) << 32 | lo;
1245 
1246  if (lsn < cutoff || cutoff == InvalidXLogRecPtr)
1247  {
1248  elog(DEBUG1, "removing logical rewrite file \"%s\"", path);
1249  if (unlink(path) < 0)
1250  ereport(ERROR,
1252  errmsg("could not remove file \"%s\": %m", path)));
1253  }
1254  else
1255  {
1256  /* on some operating systems fsyncing a file requires O_RDWR */
1257  int fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
1258 
1259  /*
1260  * The file cannot vanish due to concurrency since this function
1261  * is the only one removing logical mappings and it's run while
1262  * CheckpointLock is held exclusively.
1263  */
1264  if (fd < 0)
1265  ereport(ERROR,
1267  errmsg("could not open file \"%s\": %m", path)));
1268 
1269  /*
1270  * We could try to avoid fsyncing files that either haven't
1271  * changed or have only been created since the checkpoint's start,
1272  * but it's currently not deemed worth the effort.
1273  */
1275  if (pg_fsync(fd) != 0)
1278  errmsg("could not fsync file \"%s\": %m", path)));
1280 
1281  if (CloseTransientFile(fd) != 0)
1282  ereport(ERROR,
1284  errmsg("could not close file \"%s\": %m", path)));
1285  }
1286  }
1287  FreeDir(mappings_dir);
1288 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
uint32 TransactionId
Definition: c.h:513
unsigned int Oid
Definition: postgres_ext.h:31
Definition: dirent.h:9
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1234
Definition: dirent.c:25
#define ERROR
Definition: elog.h:43
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2370
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:633
XLogRecPtr ReplicationSlotsComputeLogicalRestartLSN(void)
Definition: slot.c:804
unsigned int uint32
Definition: c.h:367
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2581
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1380
#define S_ISREG(m)
Definition: win32_port.h:299
int CloseTransientFile(int fd)
Definition: fd.c:2547
#define stat(a, b)
Definition: win32_port.h:255
int data_sync_elevel(int elevel)
Definition: fd.c:3597
#define ereport(elevel,...)
Definition: elog.h:144
uint64 XLogRecPtr
Definition: xlogdefs.h:21
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2647
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1356
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:8361
#define LOGICAL_REWRITE_FORMAT
Definition: rewriteheap.h:54
#define lstat(path, sb)
Definition: win32_port.h:244
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
int pg_fsync(int fd)
Definition: fd.c:343
char d_name[MAX_PATH]
Definition: dirent.h:14
#define snprintf
Definition: port.h:193
int FreeDir(DIR *dir)
Definition: fd.c:2699

◆ end_heap_rewrite()

void end_heap_rewrite ( RewriteState  state)

Definition at line 301 of file rewriteheap.c.

References hash_seq_init(), hash_seq_search(), ItemPointerSetInvalid, log_newpage(), logical_end_heap_rewrite(), MAIN_FORKNUM, MemoryContextDelete(), PageSetChecksumInplace(), raw_heap_insert(), RelationData::rd_node, RelationData::rd_smgr, RelationNeedsWAL, RelationOpenSmgr, RewriteStateData::rs_blockno, RewriteStateData::rs_buffer, RewriteStateData::rs_buffer_valid, RewriteStateData::rs_cxt, RewriteStateData::rs_new_rel, RewriteStateData::rs_unresolved_tups, smgrextend(), smgrimmedsync(), HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, and UnresolvedTupData::tuple.

Referenced by heapam_relation_copy_for_cluster().

302 {
303  HASH_SEQ_STATUS seq_status;
304  UnresolvedTup unresolved;
305 
306  /*
307  * Write any remaining tuples in the UnresolvedTups table. If we have any
308  * left, they should in fact be dead, but let's err on the safe side.
309  */
310  hash_seq_init(&seq_status, state->rs_unresolved_tups);
311 
312  while ((unresolved = hash_seq_search(&seq_status)) != NULL)
313  {
314  ItemPointerSetInvalid(&unresolved->tuple->t_data->t_ctid);
315  raw_heap_insert(state, unresolved->tuple);
316  }
317 
318  /* Write the last page, if any */
319  if (state->rs_buffer_valid)
320  {
321  if (RelationNeedsWAL(state->rs_new_rel))
322  log_newpage(&state->rs_new_rel->rd_node,
323  MAIN_FORKNUM,
324  state->rs_blockno,
325  state->rs_buffer,
326  true);
328 
330 
332  (char *) state->rs_buffer, true);
333  }
334 
335  /*
336  * When we WAL-logged rel pages, we must nonetheless fsync them. The
337  * reason is the same as in storage.c's RelationCopyStorage(): we're
338  * writing data that's not in shared buffers, and so a CHECKPOINT
339  * occurring during the rewriteheap operation won't have fsync'd data we
340  * wrote before the checkpoint.
341  */
342  if (RelationNeedsWAL(state->rs_new_rel))
344 
346 
347  /* Deleting the context frees everything */
348  MemoryContextDelete(state->rs_cxt);
349 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
static void logical_end_heap_rewrite(RewriteState state)
Definition: rewriteheap.c:941
struct SMgrRelationData * rd_smgr
Definition: rel.h:57
Relation rs_new_rel
Definition: rewriteheap.c:135
HeapTupleHeader t_data
Definition: htup.h:68
#define RelationOpenSmgr(relation)
Definition: rel.h:513
ItemPointerData t_ctid
Definition: htup_details.h:160
HTAB * rs_unresolved_tups
Definition: rewriteheap.c:151
MemoryContext rs_cxt
Definition: rewriteheap.c:148
RelFileNode rd_node
Definition: rel.h:55
void PageSetChecksumInplace(Page page, BlockNumber blkno)
Definition: bufpage.c:1194
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1391
#define RelationNeedsWAL(relation)
Definition: rel.h:562
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1381
void smgrextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: smgr.c:462
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:172
XLogRecPtr log_newpage(RelFileNode *rnode, ForkNumber forkNum, BlockNumber blkno, Page page, bool page_std)
Definition: xloginsert.c:977
static void raw_heap_insert(RewriteState state, HeapTuple tup)
Definition: rewriteheap.c:615
BlockNumber rs_blockno
Definition: rewriteheap.c:137
void smgrimmedsync(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:620

◆ rewrite_heap_dead_tuple()

bool rewrite_heap_dead_tuple ( RewriteState  state,
HeapTuple  oldTuple 
)

Definition at line 565 of file rewriteheap.c.

References Assert, HASH_FIND, HASH_REMOVE, hash_search(), heap_freetuple(), HeapTupleHeaderGetXmin, RewriteStateData::rs_unresolved_tups, HeapTupleData::t_data, HeapTupleData::t_self, TidHashKey::tid, UnresolvedTupData::tuple, and TidHashKey::xmin.

Referenced by heapam_relation_copy_for_cluster().

566 {
567  /*
568  * If we have already seen an earlier tuple in the update chain that
569  * points to this tuple, let's forget about that earlier tuple. It's in
570  * fact dead as well, our simple xmax < OldestXmin test in
571  * HeapTupleSatisfiesVacuum just wasn't enough to detect it. It happens
572  * when xmin of a tuple is greater than xmax, which sounds
573  * counter-intuitive but is perfectly valid.
574  *
575  * We don't bother to try to detect the situation the other way round,
576  * when we encounter the dead tuple first and then the recently dead one
577  * that points to it. If that happens, we'll have some unmatched entries
578  * in the UnresolvedTups hash table at the end. That can happen anyway,
579  * because a vacuum might have removed the dead tuple in the chain before
580  * us.
581  */
582  UnresolvedTup unresolved;
583  TidHashKey hashkey;
584  bool found;
585 
586  memset(&hashkey, 0, sizeof(hashkey));
587  hashkey.xmin = HeapTupleHeaderGetXmin(old_tuple->t_data);
588  hashkey.tid = old_tuple->t_self;
589 
590  unresolved = hash_search(state->rs_unresolved_tups, &hashkey,
591  HASH_FIND, NULL);
592 
593  if (unresolved != NULL)
594  {
595  /* Need to free the contained tuple as well as the hashtable entry */
596  heap_freetuple(unresolved->tuple);
597  hash_search(state->rs_unresolved_tups, &hashkey,
598  HASH_REMOVE, &found);
599  Assert(found);
600  return true;
601  }
602 
603  return false;
604 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:908
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
HTAB * rs_unresolved_tups
Definition: rewriteheap.c:151
ItemPointerData tid
Definition: rewriteheap.c:166
#define Assert(condition)
Definition: c.h:738
#define HeapTupleHeaderGetXmin(tup)
Definition: htup_details.h:313
TransactionId xmin
Definition: rewriteheap.c:165

◆ rewrite_heap_tuple()

void rewrite_heap_tuple ( RewriteState  state,
HeapTuple  oldTuple,
HeapTuple  newTuple 
)

Definition at line 363 of file rewriteheap.c.

References Assert, HASH_ENTER, HASH_FIND, HASH_REMOVE, hash_search(), HEAP2_XACT_MASK, heap_copytuple(), heap_freetuple(), heap_freeze_tuple(), HEAP_UPDATED, HEAP_XACT_MASK, HEAP_XMAX_INVALID, HeapTupleHeaderGetUpdateXid, HeapTupleHeaderGetXmin, HeapTupleHeaderIndicatesMovedPartitions, HeapTupleHeaderIsOnlyLocked(), ItemPointerEquals(), ItemPointerSetInvalid, logical_rewrite_heap_tuple(), MemoryContextSwitchTo(), OldToNewMappingData::new_tid, UnresolvedTupData::old_tid, raw_heap_insert(), RelationData::rd_rel, RewriteStateData::rs_cutoff_multi, RewriteStateData::rs_cxt, RewriteStateData::rs_freeze_xid, RewriteStateData::rs_old_new_tid_map, RewriteStateData::rs_old_rel, RewriteStateData::rs_oldest_xmin, RewriteStateData::rs_unresolved_tups, HeapTupleHeaderData::t_choice, HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleHeaderData::t_heap, HeapTupleHeaderData::t_infomask, HeapTupleHeaderData::t_infomask2, HeapTupleData::t_self, TidHashKey::tid, TransactionIdPrecedes(), UnresolvedTupData::tuple, and TidHashKey::xmin.

Referenced by reform_and_rewrite_tuple().

365 {
366  MemoryContext old_cxt;
367  ItemPointerData old_tid;
368  TidHashKey hashkey;
369  bool found;
370  bool free_new;
371 
372  old_cxt = MemoryContextSwitchTo(state->rs_cxt);
373 
374  /*
375  * Copy the original tuple's visibility information into new_tuple.
376  *
377  * XXX we might later need to copy some t_infomask2 bits, too? Right now,
378  * we intentionally clear the HOT status bits.
379  */
380  memcpy(&new_tuple->t_data->t_choice.t_heap,
381  &old_tuple->t_data->t_choice.t_heap,
382  sizeof(HeapTupleFields));
383 
384  new_tuple->t_data->t_infomask &= ~HEAP_XACT_MASK;
385  new_tuple->t_data->t_infomask2 &= ~HEAP2_XACT_MASK;
386  new_tuple->t_data->t_infomask |=
387  old_tuple->t_data->t_infomask & HEAP_XACT_MASK;
388 
389  /*
390  * While we have our hands on the tuple, we may as well freeze any
391  * eligible xmin or xmax, so that future VACUUM effort can be saved.
392  */
393  heap_freeze_tuple(new_tuple->t_data,
394  state->rs_old_rel->rd_rel->relfrozenxid,
395  state->rs_old_rel->rd_rel->relminmxid,
396  state->rs_freeze_xid,
397  state->rs_cutoff_multi);
398 
399  /*
400  * Invalid ctid means that ctid should point to the tuple itself. We'll
401  * override it later if the tuple is part of an update chain.
402  */
403  ItemPointerSetInvalid(&new_tuple->t_data->t_ctid);
404 
405  /*
406  * If the tuple has been updated, check the old-to-new mapping hash table.
407  */
408  if (!((old_tuple->t_data->t_infomask & HEAP_XMAX_INVALID) ||
409  HeapTupleHeaderIsOnlyLocked(old_tuple->t_data)) &&
410  !HeapTupleHeaderIndicatesMovedPartitions(old_tuple->t_data) &&
411  !(ItemPointerEquals(&(old_tuple->t_self),
412  &(old_tuple->t_data->t_ctid))))
413  {
414  OldToNewMapping mapping;
415 
416  memset(&hashkey, 0, sizeof(hashkey));
417  hashkey.xmin = HeapTupleHeaderGetUpdateXid(old_tuple->t_data);
418  hashkey.tid = old_tuple->t_data->t_ctid;
419 
420  mapping = (OldToNewMapping)
421  hash_search(state->rs_old_new_tid_map, &hashkey,
422  HASH_FIND, NULL);
423 
424  if (mapping != NULL)
425  {
426  /*
427  * We've already copied the tuple that t_ctid points to, so we can
428  * set the ctid of this tuple to point to the new location, and
429  * insert it right away.
430  */
431  new_tuple->t_data->t_ctid = mapping->new_tid;
432 
433  /* We don't need the mapping entry anymore */
434  hash_search(state->rs_old_new_tid_map, &hashkey,
435  HASH_REMOVE, &found);
436  Assert(found);
437  }
438  else
439  {
440  /*
441  * We haven't seen the tuple t_ctid points to yet. Stash this
442  * tuple into unresolved_tups to be written later.
443  */
444  UnresolvedTup unresolved;
445 
446  unresolved = hash_search(state->rs_unresolved_tups, &hashkey,
447  HASH_ENTER, &found);
448  Assert(!found);
449 
450  unresolved->old_tid = old_tuple->t_self;
451  unresolved->tuple = heap_copytuple(new_tuple);
452 
453  /*
454  * We can't do anything more now, since we don't know where the
455  * tuple will be written.
456  */
457  MemoryContextSwitchTo(old_cxt);
458  return;
459  }
460  }
461 
462  /*
463  * Now we will write the tuple, and then check to see if it is the B tuple
464  * in any new or known pair. When we resolve a known pair, we will be
465  * able to write that pair's A tuple, and then we have to check if it
466  * resolves some other pair. Hence, we need a loop here.
467  */
468  old_tid = old_tuple->t_self;
469  free_new = false;
470 
471  for (;;)
472  {
473  ItemPointerData new_tid;
474 
475  /* Insert the tuple and find out where it's put in new_heap */
476  raw_heap_insert(state, new_tuple);
477  new_tid = new_tuple->t_self;
478 
479  logical_rewrite_heap_tuple(state, old_tid, new_tuple);
480 
481  /*
482  * If the tuple is the updated version of a row, and the prior version
483  * wouldn't be DEAD yet, then we need to either resolve the prior
484  * version (if it's waiting in rs_unresolved_tups), or make an entry
485  * in rs_old_new_tid_map (so we can resolve it when we do see it). The
486  * previous tuple's xmax would equal this one's xmin, so it's
487  * RECENTLY_DEAD if and only if the xmin is not before OldestXmin.
488  */
489  if ((new_tuple->t_data->t_infomask & HEAP_UPDATED) &&
490  !TransactionIdPrecedes(HeapTupleHeaderGetXmin(new_tuple->t_data),
491  state->rs_oldest_xmin))
492  {
493  /*
494  * Okay, this is B in an update pair. See if we've seen A.
495  */
496  UnresolvedTup unresolved;
497 
498  memset(&hashkey, 0, sizeof(hashkey));
499  hashkey.xmin = HeapTupleHeaderGetXmin(new_tuple->t_data);
500  hashkey.tid = old_tid;
501 
502  unresolved = hash_search(state->rs_unresolved_tups, &hashkey,
503  HASH_FIND, NULL);
504 
505  if (unresolved != NULL)
506  {
507  /*
508  * We have seen and memorized the previous tuple already. Now
509  * that we know where we inserted the tuple its t_ctid points
510  * to, fix its t_ctid and insert it to the new heap.
511  */
512  if (free_new)
513  heap_freetuple(new_tuple);
514  new_tuple = unresolved->tuple;
515  free_new = true;
516  old_tid = unresolved->old_tid;
517  new_tuple->t_data->t_ctid = new_tid;
518 
519  /*
520  * We don't need the hash entry anymore, but don't free its
521  * tuple just yet.
522  */
523  hash_search(state->rs_unresolved_tups, &hashkey,
524  HASH_REMOVE, &found);
525  Assert(found);
526 
527  /* loop back to insert the previous tuple in the chain */
528  continue;
529  }
530  else
531  {
532  /*
533  * Remember the new tid of this tuple. We'll use it to set the
534  * ctid when we find the previous tuple in the chain.
535  */
536  OldToNewMapping mapping;
537 
538  mapping = hash_search(state->rs_old_new_tid_map, &hashkey,
539  HASH_ENTER, &found);
540  Assert(!found);
541 
542  mapping->new_tid = new_tid;
543  }
544  }
545 
546  /* Done with this (chain of) tuples, for now */
547  if (free_new)
548  heap_freetuple(new_tuple);
549  break;
550  }
551 
552  MemoryContextSwitchTo(old_cxt);
553 }
#define HeapTupleHeaderGetUpdateXid(tup)
Definition: htup_details.h:365
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:680
static void logical_rewrite_heap_tuple(RewriteState state, ItemPointerData old_tid, HeapTuple new_tuple)
Definition: rewriteheap.c:1038
TransactionId rs_freeze_xid
Definition: rewriteheap.c:142
bool HeapTupleHeaderIsOnlyLocked(HeapTupleHeader tuple)
MultiXactId rs_cutoff_multi
Definition: rewriteheap.c:146
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define HEAP2_XACT_MASK
Definition: htup_details.h:283
#define HeapTupleHeaderIndicatesMovedPartitions(tup)
Definition: htup_details.h:445
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:908
#define HEAP_UPDATED
Definition: htup_details.h:209
ItemPointerData old_tid
Definition: rewriteheap.c:175
Form_pg_class rd_rel
Definition: rel.h:109
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
ItemPointerData new_tid
Definition: rewriteheap.c:184
#define HEAP_XMAX_INVALID
Definition: htup_details.h:207
HTAB * rs_unresolved_tups
Definition: rewriteheap.c:151
bool heap_freeze_tuple(HeapTupleHeader tuple, TransactionId relfrozenxid, TransactionId relminmxid, TransactionId cutoff_xid, TransactionId cutoff_multi)
Definition: heapam.c:6347
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
TransactionId rs_oldest_xmin
Definition: rewriteheap.c:140
MemoryContext rs_cxt
Definition: rewriteheap.c:148
ItemPointerData tid
Definition: rewriteheap.c:166
#define Assert(condition)
Definition: c.h:738
HTAB * rs_old_new_tid_map
Definition: rewriteheap.c:152
#define HeapTupleHeaderGetXmin(tup)
Definition: htup_details.h:313
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:29
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:172
TransactionId xmin
Definition: rewriteheap.c:165
static void raw_heap_insert(RewriteState state, HeapTuple tup)
Definition: rewriteheap.c:615
#define HEAP_XACT_MASK
Definition: htup_details.h:218
Relation rs_old_rel
Definition: rewriteheap.c:134
OldToNewMappingData * OldToNewMapping
Definition: rewriteheap.c:187