PostgreSQL Source Code  git master
rewriteheap.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * rewriteheap.c
4  * Support functions to rewrite tables.
5  *
6  * These functions provide a facility to completely rewrite a heap, while
7  * preserving visibility information and update chains.
8  *
9  * INTERFACE
10  *
11  * The caller is responsible for creating the new heap, all catalog
12  * changes, supplying the tuples to be written to the new heap, and
13  * rebuilding indexes. The caller must hold AccessExclusiveLock on the
14  * target table, because we assume no one else is writing into it.
15  *
16  * To use the facility:
17  *
18  * begin_heap_rewrite
19  * while (fetch next tuple)
20  * {
21  * if (tuple is dead)
22  * rewrite_heap_dead_tuple
23  * else
24  * {
25  * // do any transformations here if required
26  * rewrite_heap_tuple
27  * }
28  * }
29  * end_heap_rewrite
30  *
31  * The contents of the new relation shouldn't be relied on until after
32  * end_heap_rewrite is called.
33  *
34  *
35  * IMPLEMENTATION
36  *
37  * This would be a fairly trivial affair, except that we need to maintain
38  * the ctid chains that link versions of an updated tuple together.
39  * Since the newly stored tuples will have tids different from the original
40  * ones, if we just copied t_ctid fields to the new table the links would
41  * be wrong. When we are required to copy a (presumably recently-dead or
42  * delete-in-progress) tuple whose ctid doesn't point to itself, we have
43  * to substitute the correct ctid instead.
44  *
45  * For each ctid reference from A -> B, we might encounter either A first
46  * or B first. (Note that a tuple in the middle of a chain is both A and B
47  * of different pairs.)
48  *
49  * If we encounter A first, we'll store the tuple in the unresolved_tups
50  * hash table. When we later encounter B, we remove A from the hash table,
51  * fix the ctid to point to the new location of B, and insert both A and B
52  * to the new heap.
53  *
54  * If we encounter B first, we can insert B to the new heap right away.
55  * We then add an entry to the old_new_tid_map hash table showing B's
56  * original tid (in the old heap) and new tid (in the new heap).
57  * When we later encounter A, we get the new location of B from the table,
58  * and can write A immediately with the correct ctid.
59  *
60  * Entries in the hash tables can be removed as soon as the later tuple
61  * is encountered. That helps to keep the memory usage down. At the end,
62  * both tables are usually empty; we should have encountered both A and B
63  * of each pair. However, it's possible for A to be RECENTLY_DEAD and B
64  * entirely DEAD according to HeapTupleSatisfiesVacuum, because the test
65  * for deadness using OldestXmin is not exact. In such a case we might
66  * encounter B first, and skip it, and find A later. Then A would be added
67  * to unresolved_tups, and stay there until end of the rewrite. Since
68  * this case is very unusual, we don't worry about the memory usage.
69  *
70  * Using in-memory hash tables means that we use some memory for each live
71  * update chain in the table, from the time we find one end of the
72  * reference until we find the other end. That shouldn't be a problem in
73  * practice, but if you do something like an UPDATE without a where-clause
74  * on a large table, and then run CLUSTER in the same transaction, you
75  * could run out of memory. It doesn't seem worthwhile to add support for
76  * spill-to-disk, as there shouldn't be that many RECENTLY_DEAD tuples in a
77  * table under normal circumstances. Furthermore, in the typical scenario
78  * of CLUSTERing on an unchanging key column, we'll see all the versions
79  * of a given tuple together anyway, and so the peak memory usage is only
80  * proportional to the number of RECENTLY_DEAD versions of a single row, not
81  * in the whole table. Note that if we do fail halfway through a CLUSTER,
82  * the old table is still valid, so failure is not catastrophic.
83  *
84  * We can't use the normal heap_insert function to insert into the new
85  * heap, because heap_insert overwrites the visibility information.
86  * We use a special-purpose raw_heap_insert function instead, which
87  * is optimized for bulk inserting a lot of tuples, knowing that we have
88  * exclusive access to the heap. raw_heap_insert builds new pages in
89  * local storage. When a page is full, or at the end of the process,
90  * we insert it to WAL as a single record and then write it to disk
91  * directly through smgr. Note, however, that any data sent to the new
92  * heap's TOAST table will go through the normal bufmgr.
93  *
94  *
95  * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
96  * Portions Copyright (c) 1994-5, Regents of the University of California
97  *
98  * IDENTIFICATION
99  * src/backend/access/heap/rewriteheap.c
100  *
101  *-------------------------------------------------------------------------
102  */
103 #include "postgres.h"
104 
105 #include <sys/stat.h>
106 #include <unistd.h>
107 
108 #include "miscadmin.h"
109 
110 #include "access/heapam.h"
111 #include "access/heapam_xlog.h"
112 #include "access/rewriteheap.h"
113 #include "access/transam.h"
114 #include "access/tuptoaster.h"
115 #include "access/xact.h"
116 #include "access/xloginsert.h"
117 
118 #include "catalog/catalog.h"
119 
120 #include "lib/ilist.h"
121 
122 #include "pgstat.h"
123 
124 #include "replication/logical.h"
125 #include "replication/slot.h"
126 
127 #include "storage/bufmgr.h"
128 #include "storage/fd.h"
129 #include "storage/smgr.h"
130 
131 #include "utils/memutils.h"
132 #include "utils/rel.h"
133 #include "utils/tqual.h"
134 
135 #include "storage/procarray.h"
136 
137 /*
138  * State associated with a rewrite operation. This is opaque to the user
139  * of the rewrite facility.
140  */
141 typedef struct RewriteStateData
142 {
143  Relation rs_old_rel; /* source heap */
144  Relation rs_new_rel; /* destination heap */
145  Page rs_buffer; /* page currently being built */
146  BlockNumber rs_blockno; /* block where page will go */
147  bool rs_buffer_valid; /* T if any tuples in buffer */
148  bool rs_use_wal; /* must we WAL-log inserts? */
149  bool rs_logical_rewrite; /* do we need to do logical rewriting */
150  TransactionId rs_oldest_xmin; /* oldest xmin used by caller to determine
151  * tuple visibility */
152  TransactionId rs_freeze_xid; /* Xid that will be used as freeze cutoff
153  * point */
154  TransactionId rs_logical_xmin; /* Xid that will be used as cutoff point
155  * for logical rewrites */
156  MultiXactId rs_cutoff_multi; /* MultiXactId that will be used as cutoff
157  * point for multixacts */
158  MemoryContext rs_cxt; /* for hash tables and entries and tuples in
159  * them */
160  XLogRecPtr rs_begin_lsn; /* XLogInsertLsn when starting the rewrite */
161  HTAB *rs_unresolved_tups; /* unmatched A tuples */
162  HTAB *rs_old_new_tid_map; /* unmatched B tuples */
163  HTAB *rs_logical_mappings; /* logical remapping files */
164  uint32 rs_num_rewrite_mappings; /* # in memory mappings */
166 
167 /*
168  * The lookup keys for the hash tables are tuple TID and xmin (we must check
169  * both to avoid false matches from dead tuples). Beware that there is
170  * probably some padding space in this struct; it must be zeroed out for
171  * correct hashtable operation.
172  */
173 typedef struct
174 {
175  TransactionId xmin; /* tuple xmin */
176  ItemPointerData tid; /* tuple location in old heap */
177 } TidHashKey;
178 
179 /*
180  * Entry structures for the hash tables
181  */
182 typedef struct
183 {
184  TidHashKey key; /* expected xmin/old location of B tuple */
185  ItemPointerData old_tid; /* A's location in the old heap */
186  HeapTuple tuple; /* A's tuple contents */
188 
190 
191 typedef struct
192 {
193  TidHashKey key; /* actual xmin/old location of B tuple */
194  ItemPointerData new_tid; /* where we put it in the new heap */
196 
198 
199 /*
200  * In-Memory data for an xid that might need logical remapping entries
201  * to be logged.
202  */
203 typedef struct RewriteMappingFile
204 {
205  TransactionId xid; /* xid that might need to see the row */
206  int vfd; /* fd of mappings file */
207  off_t off; /* how far have we written yet */
208  uint32 num_mappings; /* number of in-memory mappings */
209  dlist_head mappings; /* list of in-memory mappings */
210  char path[MAXPGPATH]; /* path, for error messages */
212 
213 /*
214  * A single In-Memory logical rewrite mapping, hanging off
215  * RewriteMappingFile->mappings.
216  */
218 {
219  LogicalRewriteMappingData map; /* map between old and new location of the
220  * tuple */
223 
224 
225 /* prototypes for internal functions */
226 static void raw_heap_insert(RewriteState state, HeapTuple tup);
227 
228 /* internal logical remapping prototypes */
232 
233 
234 /*
235  * Begin a rewrite of a table
236  *
237  * old_heap old, locked heap relation tuples will be read from
238  * new_heap new, locked heap relation to insert tuples to
239  * oldest_xmin xid used by the caller to determine which tuples are dead
240  * freeze_xid xid before which tuples will be frozen
241  * min_multi multixact before which multis will be removed
242  * use_wal should the inserts to the new heap be WAL-logged?
243  *
244  * Returns an opaque RewriteState, allocated in current memory context,
245  * to be used in subsequent calls to the other functions.
246  */
248 begin_heap_rewrite(Relation old_heap, Relation new_heap, TransactionId oldest_xmin,
249  TransactionId freeze_xid, MultiXactId cutoff_multi,
250  bool use_wal)
251 {
253  MemoryContext rw_cxt;
254  MemoryContext old_cxt;
255  HASHCTL hash_ctl;
256 
257  /*
258  * To ease cleanup, make a separate context that will contain the
259  * RewriteState struct itself plus all subsidiary data.
260  */
262  "Table rewrite",
264  old_cxt = MemoryContextSwitchTo(rw_cxt);
265 
266  /* Create and fill in the state struct */
267  state = palloc0(sizeof(RewriteStateData));
268 
269  state->rs_old_rel = old_heap;
270  state->rs_new_rel = new_heap;
271  state->rs_buffer = (Page) palloc(BLCKSZ);
272  /* new_heap needn't be empty, just locked */
273  state->rs_blockno = RelationGetNumberOfBlocks(new_heap);
274  state->rs_buffer_valid = false;
275  state->rs_use_wal = use_wal;
276  state->rs_oldest_xmin = oldest_xmin;
277  state->rs_freeze_xid = freeze_xid;
278  state->rs_cutoff_multi = cutoff_multi;
279  state->rs_cxt = rw_cxt;
280 
281  /* Initialize hash tables used to track update chains */
282  memset(&hash_ctl, 0, sizeof(hash_ctl));
283  hash_ctl.keysize = sizeof(TidHashKey);
284  hash_ctl.entrysize = sizeof(UnresolvedTupData);
285  hash_ctl.hcxt = state->rs_cxt;
286 
287  state->rs_unresolved_tups =
288  hash_create("Rewrite / Unresolved ctids",
289  128, /* arbitrary initial size */
290  &hash_ctl,
292 
293  hash_ctl.entrysize = sizeof(OldToNewMappingData);
294 
295  state->rs_old_new_tid_map =
296  hash_create("Rewrite / Old to new tid map",
297  128, /* arbitrary initial size */
298  &hash_ctl,
300 
301  MemoryContextSwitchTo(old_cxt);
302 
304 
305  return state;
306 }
307 
308 /*
309  * End a rewrite.
310  *
311  * state and any other resources are freed.
312  */
313 void
315 {
316  HASH_SEQ_STATUS seq_status;
317  UnresolvedTup unresolved;
318 
319  /*
320  * Write any remaining tuples in the UnresolvedTups table. If we have any
321  * left, they should in fact be dead, but let's err on the safe side.
322  */
323  hash_seq_init(&seq_status, state->rs_unresolved_tups);
324 
325  while ((unresolved = hash_seq_search(&seq_status)) != NULL)
326  {
327  ItemPointerSetInvalid(&unresolved->tuple->t_data->t_ctid);
328  raw_heap_insert(state, unresolved->tuple);
329  }
330 
331  /* Write the last page, if any */
332  if (state->rs_buffer_valid)
333  {
334  if (state->rs_use_wal)
335  log_newpage(&state->rs_new_rel->rd_node,
336  MAIN_FORKNUM,
337  state->rs_blockno,
338  state->rs_buffer,
339  true);
341 
343 
345  (char *) state->rs_buffer, true);
346  }
347 
348  /*
349  * If the rel is WAL-logged, must fsync before commit. We use heap_sync
350  * to ensure that the toast table gets fsync'd too.
351  *
352  * It's obvious that we must do this when not WAL-logging. It's less
353  * obvious that we have to do it even if we did WAL-log the pages. The
354  * reason is the same as in tablecmds.c's copy_relation_data(): we're
355  * writing data that's not in shared buffers, and so a CHECKPOINT
356  * occurring during the rewriteheap operation won't have fsync'd data we
357  * wrote before the checkpoint.
358  */
359  if (RelationNeedsWAL(state->rs_new_rel))
360  heap_sync(state->rs_new_rel);
361 
363 
364  /* Deleting the context frees everything */
365  MemoryContextDelete(state->rs_cxt);
366 }
367 
368 /*
369  * Add a tuple to the new heap.
370  *
371  * Visibility information is copied from the original tuple, except that
372  * we "freeze" very-old tuples. Note that since we scribble on new_tuple,
373  * it had better be temp storage not a pointer to the original tuple.
374  *
375  * state opaque state as returned by begin_heap_rewrite
376  * old_tuple original tuple in the old heap
377  * new_tuple new, rewritten tuple to be inserted to new heap
378  */
379 void
381  HeapTuple old_tuple, HeapTuple new_tuple)
382 {
383  MemoryContext old_cxt;
384  ItemPointerData old_tid;
385  TidHashKey hashkey;
386  bool found;
387  bool free_new;
388 
389  old_cxt = MemoryContextSwitchTo(state->rs_cxt);
390 
391  /*
392  * Copy the original tuple's visibility information into new_tuple.
393  *
394  * XXX we might later need to copy some t_infomask2 bits, too? Right now,
395  * we intentionally clear the HOT status bits.
396  */
397  memcpy(&new_tuple->t_data->t_choice.t_heap,
398  &old_tuple->t_data->t_choice.t_heap,
399  sizeof(HeapTupleFields));
400 
401  new_tuple->t_data->t_infomask &= ~HEAP_XACT_MASK;
402  new_tuple->t_data->t_infomask2 &= ~HEAP2_XACT_MASK;
403  new_tuple->t_data->t_infomask |=
404  old_tuple->t_data->t_infomask & HEAP_XACT_MASK;
405 
406  /*
407  * While we have our hands on the tuple, we may as well freeze any
408  * eligible xmin or xmax, so that future VACUUM effort can be saved.
409  */
410  heap_freeze_tuple(new_tuple->t_data,
411  state->rs_old_rel->rd_rel->relfrozenxid,
412  state->rs_old_rel->rd_rel->relminmxid,
413  state->rs_freeze_xid,
414  state->rs_cutoff_multi);
415 
416  /*
417  * Invalid ctid means that ctid should point to the tuple itself. We'll
418  * override it later if the tuple is part of an update chain.
419  */
420  ItemPointerSetInvalid(&new_tuple->t_data->t_ctid);
421 
422  /*
423  * If the tuple has been updated, check the old-to-new mapping hash table.
424  */
425  if (!((old_tuple->t_data->t_infomask & HEAP_XMAX_INVALID) ||
426  HeapTupleHeaderIsOnlyLocked(old_tuple->t_data)) &&
427  !(ItemPointerEquals(&(old_tuple->t_self),
428  &(old_tuple->t_data->t_ctid))))
429  {
430  OldToNewMapping mapping;
431 
432  memset(&hashkey, 0, sizeof(hashkey));
433  hashkey.xmin = HeapTupleHeaderGetUpdateXid(old_tuple->t_data);
434  hashkey.tid = old_tuple->t_data->t_ctid;
435 
436  mapping = (OldToNewMapping)
437  hash_search(state->rs_old_new_tid_map, &hashkey,
438  HASH_FIND, NULL);
439 
440  if (mapping != NULL)
441  {
442  /*
443  * We've already copied the tuple that t_ctid points to, so we can
444  * set the ctid of this tuple to point to the new location, and
445  * insert it right away.
446  */
447  new_tuple->t_data->t_ctid = mapping->new_tid;
448 
449  /* We don't need the mapping entry anymore */
450  hash_search(state->rs_old_new_tid_map, &hashkey,
451  HASH_REMOVE, &found);
452  Assert(found);
453  }
454  else
455  {
456  /*
457  * We haven't seen the tuple t_ctid points to yet. Stash this
458  * tuple into unresolved_tups to be written later.
459  */
460  UnresolvedTup unresolved;
461 
462  unresolved = hash_search(state->rs_unresolved_tups, &hashkey,
463  HASH_ENTER, &found);
464  Assert(!found);
465 
466  unresolved->old_tid = old_tuple->t_self;
467  unresolved->tuple = heap_copytuple(new_tuple);
468 
469  /*
470  * We can't do anything more now, since we don't know where the
471  * tuple will be written.
472  */
473  MemoryContextSwitchTo(old_cxt);
474  return;
475  }
476  }
477 
478  /*
479  * Now we will write the tuple, and then check to see if it is the B tuple
480  * in any new or known pair. When we resolve a known pair, we will be
481  * able to write that pair's A tuple, and then we have to check if it
482  * resolves some other pair. Hence, we need a loop here.
483  */
484  old_tid = old_tuple->t_self;
485  free_new = false;
486 
487  for (;;)
488  {
489  ItemPointerData new_tid;
490 
491  /* Insert the tuple and find out where it's put in new_heap */
492  raw_heap_insert(state, new_tuple);
493  new_tid = new_tuple->t_self;
494 
495  logical_rewrite_heap_tuple(state, old_tid, new_tuple);
496 
497  /*
498  * If the tuple is the updated version of a row, and the prior version
499  * wouldn't be DEAD yet, then we need to either resolve the prior
500  * version (if it's waiting in rs_unresolved_tups), or make an entry
501  * in rs_old_new_tid_map (so we can resolve it when we do see it). The
502  * previous tuple's xmax would equal this one's xmin, so it's
503  * RECENTLY_DEAD if and only if the xmin is not before OldestXmin.
504  */
505  if ((new_tuple->t_data->t_infomask & HEAP_UPDATED) &&
507  state->rs_oldest_xmin))
508  {
509  /*
510  * Okay, this is B in an update pair. See if we've seen A.
511  */
512  UnresolvedTup unresolved;
513 
514  memset(&hashkey, 0, sizeof(hashkey));
515  hashkey.xmin = HeapTupleHeaderGetXmin(new_tuple->t_data);
516  hashkey.tid = old_tid;
517 
518  unresolved = hash_search(state->rs_unresolved_tups, &hashkey,
519  HASH_FIND, NULL);
520 
521  if (unresolved != NULL)
522  {
523  /*
524  * We have seen and memorized the previous tuple already. Now
525  * that we know where we inserted the tuple its t_ctid points
526  * to, fix its t_ctid and insert it to the new heap.
527  */
528  if (free_new)
529  heap_freetuple(new_tuple);
530  new_tuple = unresolved->tuple;
531  free_new = true;
532  old_tid = unresolved->old_tid;
533  new_tuple->t_data->t_ctid = new_tid;
534 
535  /*
536  * We don't need the hash entry anymore, but don't free its
537  * tuple just yet.
538  */
539  hash_search(state->rs_unresolved_tups, &hashkey,
540  HASH_REMOVE, &found);
541  Assert(found);
542 
543  /* loop back to insert the previous tuple in the chain */
544  continue;
545  }
546  else
547  {
548  /*
549  * Remember the new tid of this tuple. We'll use it to set the
550  * ctid when we find the previous tuple in the chain.
551  */
552  OldToNewMapping mapping;
553 
554  mapping = hash_search(state->rs_old_new_tid_map, &hashkey,
555  HASH_ENTER, &found);
556  Assert(!found);
557 
558  mapping->new_tid = new_tid;
559  }
560  }
561 
562  /* Done with this (chain of) tuples, for now */
563  if (free_new)
564  heap_freetuple(new_tuple);
565  break;
566  }
567 
568  MemoryContextSwitchTo(old_cxt);
569 }
570 
571 /*
572  * Register a dead tuple with an ongoing rewrite. Dead tuples are not
573  * copied to the new table, but we still make note of them so that we
574  * can release some resources earlier.
575  *
576  * Returns true if a tuple was removed from the unresolved_tups table.
577  * This indicates that that tuple, previously thought to be "recently dead",
578  * is now known really dead and won't be written to the output.
579  */
580 bool
582 {
583  /*
584  * If we have already seen an earlier tuple in the update chain that
585  * points to this tuple, let's forget about that earlier tuple. It's in
586  * fact dead as well, our simple xmax < OldestXmin test in
587  * HeapTupleSatisfiesVacuum just wasn't enough to detect it. It happens
588  * when xmin of a tuple is greater than xmax, which sounds
589  * counter-intuitive but is perfectly valid.
590  *
591  * We don't bother to try to detect the situation the other way round,
592  * when we encounter the dead tuple first and then the recently dead one
593  * that points to it. If that happens, we'll have some unmatched entries
594  * in the UnresolvedTups hash table at the end. That can happen anyway,
595  * because a vacuum might have removed the dead tuple in the chain before
596  * us.
597  */
598  UnresolvedTup unresolved;
599  TidHashKey hashkey;
600  bool found;
601 
602  memset(&hashkey, 0, sizeof(hashkey));
603  hashkey.xmin = HeapTupleHeaderGetXmin(old_tuple->t_data);
604  hashkey.tid = old_tuple->t_self;
605 
606  unresolved = hash_search(state->rs_unresolved_tups, &hashkey,
607  HASH_FIND, NULL);
608 
609  if (unresolved != NULL)
610  {
611  /* Need to free the contained tuple as well as the hashtable entry */
612  heap_freetuple(unresolved->tuple);
613  hash_search(state->rs_unresolved_tups, &hashkey,
614  HASH_REMOVE, &found);
615  Assert(found);
616  return true;
617  }
618 
619  return false;
620 }
621 
622 /*
623  * Insert a tuple to the new relation. This has to track heap_insert
624  * and its subsidiary functions!
625  *
626  * t_self of the tuple is set to the new TID of the tuple. If t_ctid of the
627  * tuple is invalid on entry, it's replaced with the new TID as well (in
628  * the inserted data only, not in the caller's copy).
629  */
630 static void
632 {
633  Page page = state->rs_buffer;
634  Size pageFreeSpace,
635  saveFreeSpace;
636  Size len;
637  OffsetNumber newoff;
638  HeapTuple heaptup;
639 
640  /*
641  * If the new tuple is too big for storage or contains already toasted
642  * out-of-line attributes from some other relation, invoke the toaster.
643  *
644  * Note: below this point, heaptup is the data we actually intend to store
645  * into the relation; tup is the caller's original untoasted data.
646  */
647  if (state->rs_new_rel->rd_rel->relkind == RELKIND_TOASTVALUE)
648  {
649  /* toast table entries should never be recursively toasted */
651  heaptup = tup;
652  }
653  else if (HeapTupleHasExternal(tup) || tup->t_len > TOAST_TUPLE_THRESHOLD)
654  heaptup = toast_insert_or_update(state->rs_new_rel, tup, NULL,
656  (state->rs_use_wal ?
657  0 : HEAP_INSERT_SKIP_WAL));
658  else
659  heaptup = tup;
660 
661  len = MAXALIGN(heaptup->t_len); /* be conservative */
662 
663  /*
664  * If we're gonna fail for oversize tuple, do it right away
665  */
666  if (len > MaxHeapTupleSize)
667  ereport(ERROR,
668  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
669  errmsg("row is too big: size %zu, maximum size %zu",
670  len, MaxHeapTupleSize)));
671 
672  /* Compute desired extra freespace due to fillfactor option */
673  saveFreeSpace = RelationGetTargetPageFreeSpace(state->rs_new_rel,
675 
676  /* Now we can check to see if there's enough free space already. */
677  if (state->rs_buffer_valid)
678  {
679  pageFreeSpace = PageGetHeapFreeSpace(page);
680 
681  if (len + saveFreeSpace > pageFreeSpace)
682  {
683  /* Doesn't fit, so write out the existing page */
684 
685  /* XLOG stuff */
686  if (state->rs_use_wal)
687  log_newpage(&state->rs_new_rel->rd_node,
688  MAIN_FORKNUM,
689  state->rs_blockno,
690  page,
691  true);
692 
693  /*
694  * Now write the page. We say isTemp = true even if it's not a
695  * temp table, because there's no need for smgr to schedule an
696  * fsync for this write; we'll do it ourselves in
697  * end_heap_rewrite.
698  */
700 
701  PageSetChecksumInplace(page, state->rs_blockno);
702 
704  state->rs_blockno, (char *) page, true);
705 
706  state->rs_blockno++;
707  state->rs_buffer_valid = false;
708  }
709  }
710 
711  if (!state->rs_buffer_valid)
712  {
713  /* Initialize a new empty page */
714  PageInit(page, BLCKSZ, 0);
715  state->rs_buffer_valid = true;
716  }
717 
718  /* And now we can insert the tuple into the page */
719  newoff = PageAddItem(page, (Item) heaptup->t_data, heaptup->t_len,
720  InvalidOffsetNumber, false, true);
721  if (newoff == InvalidOffsetNumber)
722  elog(ERROR, "failed to add tuple");
723 
724  /* Update caller's t_self to the actual position where it was stored */
725  ItemPointerSet(&(tup->t_self), state->rs_blockno, newoff);
726 
727  /*
728  * Insert the correct position into CTID of the stored tuple, too, if the
729  * caller didn't supply a valid CTID.
730  */
731  if (!ItemPointerIsValid(&tup->t_data->t_ctid))
732  {
733  ItemId newitemid;
734  HeapTupleHeader onpage_tup;
735 
736  newitemid = PageGetItemId(page, newoff);
737  onpage_tup = (HeapTupleHeader) PageGetItem(page, newitemid);
738 
739  onpage_tup->t_ctid = tup->t_self;
740  }
741 
742  /* If heaptup is a private copy, release it. */
743  if (heaptup != tup)
744  heap_freetuple(heaptup);
745 }
746 
747 /* ------------------------------------------------------------------------
748  * Logical rewrite support
749  *
750  * When doing logical decoding - which relies on using cmin/cmax of catalog
751  * tuples, via xl_heap_new_cid records - heap rewrites have to log enough
752  * information to allow the decoding backend to updates its internal mapping
753  * of (relfilenode,ctid) => (cmin, cmax) to be correct for the rewritten heap.
754  *
755  * For that, every time we find a tuple that's been modified in a catalog
756  * relation within the xmin horizon of any decoding slot, we log a mapping
757  * from the old to the new location.
758  *
759  * To deal with rewrites that abort the filename of a mapping file contains
760  * the xid of the transaction performing the rewrite, which then can be
761  * checked before being read in.
762  *
763  * For efficiency we don't immediately spill every single map mapping for a
764  * row to disk but only do so in batches when we've collected several of them
765  * in memory or when end_heap_rewrite() has been called.
766  *
767  * Crash-Safety: This module diverts from the usual patterns of doing WAL
768  * since it cannot rely on checkpoint flushing out all buffers and thus
769  * waiting for exclusive locks on buffers. Usually the XLogInsert() covering
770  * buffer modifications is performed while the buffer(s) that are being
771  * modified are exclusively locked guaranteeing that both the WAL record and
772  * the modified heap are on either side of the checkpoint. But since the
773  * mapping files we log aren't in shared_buffers that interlock doesn't work.
774  *
775  * Instead we simply write the mapping files out to disk, *before* the
776  * XLogInsert() is performed. That guarantees that either the XLogInsert() is
777  * inserted after the checkpoint's redo pointer or that the checkpoint (via
778  * LogicalRewriteHeapCheckpoint()) has flushed the (partial) mapping file to
779  * disk. That leaves the tail end that has not yet been flushed open to
780  * corruption, which is solved by including the current offset in the
781  * xl_heap_rewrite_mapping records and truncating the mapping file to it
782  * during replay. Every time a rewrite is finished all generated mapping files
783  * are synced to disk.
784  *
785  * Note that if we were only concerned about crash safety we wouldn't have to
786  * deal with WAL logging at all - an fsync() at the end of a rewrite would be
787  * sufficient for crash safety. Any mapping that hasn't been safely flushed to
788  * disk has to be by an aborted (explicitly or via a crash) transaction and is
789  * ignored by virtue of the xid in its name being subject to a
790  * TransactionDidCommit() check. But we want to support having standbys via
791  * physical replication, both for availability and to do logical decoding
792  * there.
793  * ------------------------------------------------------------------------
794  */
795 
796 /*
797  * Do preparations for logging logical mappings during a rewrite if
798  * necessary. If we detect that we don't need to log anything we'll prevent
799  * any further action by the various logical rewrite functions.
800  */
801 static void
803 {
804  HASHCTL hash_ctl;
805  TransactionId logical_xmin;
806 
807  /*
808  * We only need to persist these mappings if the rewritten table can be
809  * accessed during logical decoding, if not, we can skip doing any
810  * additional work.
811  */
812  state->rs_logical_rewrite =
814 
815  if (!state->rs_logical_rewrite)
816  return;
817 
818  ProcArrayGetReplicationSlotXmin(NULL, &logical_xmin);
819 
820  /*
821  * If there are no logical slots in progress we don't need to do anything,
822  * there cannot be any remappings for relevant rows yet. The relation's
823  * lock protects us against races.
824  */
825  if (logical_xmin == InvalidTransactionId)
826  {
827  state->rs_logical_rewrite = false;
828  return;
829  }
830 
831  state->rs_logical_xmin = logical_xmin;
833  state->rs_num_rewrite_mappings = 0;
834 
835  memset(&hash_ctl, 0, sizeof(hash_ctl));
836  hash_ctl.keysize = sizeof(TransactionId);
837  hash_ctl.entrysize = sizeof(RewriteMappingFile);
838  hash_ctl.hcxt = state->rs_cxt;
839 
840  state->rs_logical_mappings =
841  hash_create("Logical rewrite mapping",
842  128, /* arbitrary initial size */
843  &hash_ctl,
845 }
846 
847 /*
848  * Flush all logical in-memory mappings to disk, but don't fsync them yet.
849  */
850 static void
852 {
853  HASH_SEQ_STATUS seq_status;
854  RewriteMappingFile *src;
855  dlist_mutable_iter iter;
856 
857  Assert(state->rs_logical_rewrite);
858 
859  /* no logical rewrite in progress, no need to iterate over mappings */
860  if (state->rs_num_rewrite_mappings == 0)
861  return;
862 
863  elog(DEBUG1, "flushing %u logical rewrite mapping entries",
864  state->rs_num_rewrite_mappings);
865 
866  hash_seq_init(&seq_status, state->rs_logical_mappings);
867  while ((src = (RewriteMappingFile *) hash_seq_search(&seq_status)) != NULL)
868  {
869  char *waldata;
870  char *waldata_start;
872  Oid dboid;
873  uint32 len;
874  int written;
875 
876  /* this file hasn't got any new mappings */
877  if (src->num_mappings == 0)
878  continue;
879 
880  if (state->rs_old_rel->rd_rel->relisshared)
881  dboid = InvalidOid;
882  else
883  dboid = MyDatabaseId;
884 
885  xlrec.num_mappings = src->num_mappings;
886  xlrec.mapped_rel = RelationGetRelid(state->rs_old_rel);
887  xlrec.mapped_xid = src->xid;
888  xlrec.mapped_db = dboid;
889  xlrec.offset = src->off;
890  xlrec.start_lsn = state->rs_begin_lsn;
891 
892  /* write all mappings consecutively */
893  len = src->num_mappings * sizeof(LogicalRewriteMappingData);
894  waldata_start = waldata = palloc(len);
895 
896  /*
897  * collect data we need to write out, but don't modify ondisk data yet
898  */
899  dlist_foreach_modify(iter, &src->mappings)
900  {
902 
903  pmap = dlist_container(RewriteMappingDataEntry, node, iter.cur);
904 
905  memcpy(waldata, &pmap->map, sizeof(pmap->map));
906  waldata += sizeof(pmap->map);
907 
908  /* remove from the list and free */
909  dlist_delete(&pmap->node);
910  pfree(pmap);
911 
912  /* update bookkeeping */
913  state->rs_num_rewrite_mappings--;
914  src->num_mappings--;
915  }
916 
917  Assert(src->num_mappings == 0);
918  Assert(waldata == waldata_start + len);
919 
920  /*
921  * Note that we deviate from the usual WAL coding practices here,
922  * check the above "Logical rewrite support" comment for reasoning.
923  */
924  written = FileWrite(src->vfd, waldata_start, len,
926  if (written != len)
927  ereport(ERROR,
929  errmsg("could not write to file \"%s\", wrote %d of %d: %m", src->path,
930  written, len)));
931  src->off += len;
932 
933  XLogBeginInsert();
934  XLogRegisterData((char *) (&xlrec), sizeof(xlrec));
935  XLogRegisterData(waldata_start, len);
936 
937  /* write xlog record */
938  XLogInsert(RM_HEAP2_ID, XLOG_HEAP2_REWRITE);
939 
940  pfree(waldata_start);
941  }
942  Assert(state->rs_num_rewrite_mappings == 0);
943 }
944 
945 /*
946  * Logical remapping part of end_heap_rewrite().
947  */
948 static void
950 {
951  HASH_SEQ_STATUS seq_status;
952  RewriteMappingFile *src;
953 
954  /* done, no logical rewrite in progress */
955  if (!state->rs_logical_rewrite)
956  return;
957 
958  /* writeout remaining in-memory entries */
959  if (state->rs_num_rewrite_mappings > 0)
961 
962  /* Iterate over all mappings we have written and fsync the files. */
963  hash_seq_init(&seq_status, state->rs_logical_mappings);
964  while ((src = (RewriteMappingFile *) hash_seq_search(&seq_status)) != NULL)
965  {
967  ereport(ERROR,
969  errmsg("could not fsync file \"%s\": %m", src->path)));
970  FileClose(src->vfd);
971  }
972  /* memory context cleanup will deal with the rest */
973 }
974 
975 /*
976  * Log a single (old->new) mapping for 'xid'.
977  */
978 static void
981 {
982  RewriteMappingFile *src;
984  Oid relid;
985  bool found;
986 
987  relid = RelationGetRelid(state->rs_old_rel);
988 
989  /* look for existing mappings for this 'mapped' xid */
990  src = hash_search(state->rs_logical_mappings, &xid,
991  HASH_ENTER, &found);
992 
993  /*
994  * We haven't yet had the need to map anything for this xid, create
995  * per-xid data structures.
996  */
997  if (!found)
998  {
999  char path[MAXPGPATH];
1000  Oid dboid;
1001 
1002  if (state->rs_old_rel->rd_rel->relisshared)
1003  dboid = InvalidOid;
1004  else
1005  dboid = MyDatabaseId;
1006 
1007  snprintf(path, MAXPGPATH,
1008  "pg_logical/mappings/" LOGICAL_REWRITE_FORMAT,
1009  dboid, relid,
1010  (uint32) (state->rs_begin_lsn >> 32),
1011  (uint32) state->rs_begin_lsn,
1012  xid, GetCurrentTransactionId());
1013 
1014  dlist_init(&src->mappings);
1015  src->num_mappings = 0;
1016  src->off = 0;
1017  memcpy(src->path, path, sizeof(path));
1018  src->vfd = PathNameOpenFile(path,
1019  O_CREAT | O_EXCL | O_WRONLY | PG_BINARY);
1020  if (src->vfd < 0)
1021  ereport(ERROR,
1023  errmsg("could not create file \"%s\": %m", path)));
1024  }
1025 
1026  pmap = MemoryContextAlloc(state->rs_cxt,
1027  sizeof(RewriteMappingDataEntry));
1028  memcpy(&pmap->map, map, sizeof(LogicalRewriteMappingData));
1029  dlist_push_tail(&src->mappings, &pmap->node);
1030  src->num_mappings++;
1031  state->rs_num_rewrite_mappings++;
1032 
1033  /*
1034  * Write out buffer every time we've too many in-memory entries across all
1035  * mapping files.
1036  */
1037  if (state->rs_num_rewrite_mappings >= 1000 /* arbitrary number */ )
1039 }
1040 
1041 /*
1042  * Perform logical remapping for a tuple that's mapped from old_tid to
1043  * new_tuple->t_self by rewrite_heap_tuple() if necessary for the tuple.
1044  */
1045 static void
1047  HeapTuple new_tuple)
1048 {
1049  ItemPointerData new_tid = new_tuple->t_self;
1050  TransactionId cutoff = state->rs_logical_xmin;
1051  TransactionId xmin;
1052  TransactionId xmax;
1053  bool do_log_xmin = false;
1054  bool do_log_xmax = false;
1056 
1057  /* no logical rewrite in progress, we don't need to log anything */
1058  if (!state->rs_logical_rewrite)
1059  return;
1060 
1061  xmin = HeapTupleHeaderGetXmin(new_tuple->t_data);
1062  /* use *GetUpdateXid to correctly deal with multixacts */
1063  xmax = HeapTupleHeaderGetUpdateXid(new_tuple->t_data);
1064 
1065  /*
1066  * Log the mapping iff the tuple has been created recently.
1067  */
1068  if (TransactionIdIsNormal(xmin) && !TransactionIdPrecedes(xmin, cutoff))
1069  do_log_xmin = true;
1070 
1071  if (!TransactionIdIsNormal(xmax))
1072  {
1073  /*
1074  * no xmax is set, can't have any permanent ones, so this check is
1075  * sufficient
1076  */
1077  }
1078  else if (HEAP_XMAX_IS_LOCKED_ONLY(new_tuple->t_data->t_infomask))
1079  {
1080  /* only locked, we don't care */
1081  }
1082  else if (!TransactionIdPrecedes(xmax, cutoff))
1083  {
1084  /* tuple has been deleted recently, log */
1085  do_log_xmax = true;
1086  }
1087 
1088  /* if neither needs to be logged, we're done */
1089  if (!do_log_xmin && !do_log_xmax)
1090  return;
1091 
1092  /* fill out mapping information */
1093  map.old_node = state->rs_old_rel->rd_node;
1094  map.old_tid = old_tid;
1095  map.new_node = state->rs_new_rel->rd_node;
1096  map.new_tid = new_tid;
1097 
1098  /* ---
1099  * Now persist the mapping for the individual xids that are affected. We
1100  * need to log for both xmin and xmax if they aren't the same transaction
1101  * since the mapping files are per "affected" xid.
1102  * We don't muster all that much effort detecting whether xmin and xmax
1103  * are actually the same transaction, we just check whether the xid is the
1104  * same disregarding subtransactions. Logging too much is relatively
1105  * harmless and we could never do the check fully since subtransaction
1106  * data is thrown away during restarts.
1107  * ---
1108  */
1109  if (do_log_xmin)
1110  logical_rewrite_log_mapping(state, xmin, &map);
1111  /* separately log mapping for xmax unless it'd be redundant */
1112  if (do_log_xmax && !TransactionIdEquals(xmin, xmax))
1113  logical_rewrite_log_mapping(state, xmax, &map);
1114 }
1115 
1116 /*
1117  * Replay XLOG_HEAP2_REWRITE records
1118  */
1119 void
1121 {
1122  char path[MAXPGPATH];
1123  int fd;
1124  xl_heap_rewrite_mapping *xlrec;
1125  uint32 len;
1126  char *data;
1127 
1128  xlrec = (xl_heap_rewrite_mapping *) XLogRecGetData(r);
1129 
1130  snprintf(path, MAXPGPATH,
1131  "pg_logical/mappings/" LOGICAL_REWRITE_FORMAT,
1132  xlrec->mapped_db, xlrec->mapped_rel,
1133  (uint32) (xlrec->start_lsn >> 32),
1134  (uint32) xlrec->start_lsn,
1135  xlrec->mapped_xid, XLogRecGetXid(r));
1136 
1137  fd = OpenTransientFile(path,
1138  O_CREAT | O_WRONLY | PG_BINARY);
1139  if (fd < 0)
1140  ereport(ERROR,
1142  errmsg("could not create file \"%s\": %m", path)));
1143 
1144  /*
1145  * Truncate all data that's not guaranteed to have been safely fsynced (by
1146  * previous record or by the last checkpoint).
1147  */
1149  if (ftruncate(fd, xlrec->offset) != 0)
1150  ereport(ERROR,
1152  errmsg("could not truncate file \"%s\" to %u: %m",
1153  path, (uint32) xlrec->offset)));
1155 
1156  /* now seek to the position we want to write our data to */
1157  if (lseek(fd, xlrec->offset, SEEK_SET) != xlrec->offset)
1158  ereport(ERROR,
1160  errmsg("could not seek to end of file \"%s\": %m",
1161  path)));
1162 
1163  data = XLogRecGetData(r) + sizeof(*xlrec);
1164 
1165  len = xlrec->num_mappings * sizeof(LogicalRewriteMappingData);
1166 
1167  /* write out tail end of mapping file (again) */
1169  if (write(fd, data, len) != len)
1170  ereport(ERROR,
1172  errmsg("could not write to file \"%s\": %m", path)));
1174 
1175  /*
1176  * Now fsync all previously written data. We could improve things and only
1177  * do this for the last write to a file, but the required bookkeeping
1178  * doesn't seem worth the trouble.
1179  */
1181  if (pg_fsync(fd) != 0)
1182  ereport(ERROR,
1184  errmsg("could not fsync file \"%s\": %m", path)));
1186 
1187  CloseTransientFile(fd);
1188 }
1189 
1190 /* ---
1191  * Perform a checkpoint for logical rewrite mappings
1192  *
1193  * This serves two tasks:
1194  * 1) Remove all mappings not needed anymore based on the logical restart LSN
1195  * 2) Flush all remaining mappings to disk, so that replay after a checkpoint
1196  * only has to deal with the parts of a mapping that have been written out
1197  * after the checkpoint started.
1198  * ---
1199  */
1200 void
1202 {
1203  XLogRecPtr cutoff;
1204  XLogRecPtr redo;
1205  DIR *mappings_dir;
1206  struct dirent *mapping_de;
1207  char path[MAXPGPATH + 20];
1208 
1209  /*
1210  * We start of with a minimum of the last redo pointer. No new decoding
1211  * slot will start before that, so that's a safe upper bound for removal.
1212  */
1213  redo = GetRedoRecPtr();
1214 
1215  /* now check for the restart ptrs from existing slots */
1217 
1218  /* don't start earlier than the restart lsn */
1219  if (cutoff != InvalidXLogRecPtr && redo < cutoff)
1220  cutoff = redo;
1221 
1222  mappings_dir = AllocateDir("pg_logical/mappings");
1223  while ((mapping_de = ReadDir(mappings_dir, "pg_logical/mappings")) != NULL)
1224  {
1225  struct stat statbuf;
1226  Oid dboid;
1227  Oid relid;
1228  XLogRecPtr lsn;
1229  TransactionId rewrite_xid;
1230  TransactionId create_xid;
1231  uint32 hi,
1232  lo;
1233 
1234  if (strcmp(mapping_de->d_name, ".") == 0 ||
1235  strcmp(mapping_de->d_name, "..") == 0)
1236  continue;
1237 
1238  snprintf(path, sizeof(path), "pg_logical/mappings/%s", mapping_de->d_name);
1239  if (lstat(path, &statbuf) == 0 && !S_ISREG(statbuf.st_mode))
1240  continue;
1241 
1242  /* Skip over files that cannot be ours. */
1243  if (strncmp(mapping_de->d_name, "map-", 4) != 0)
1244  continue;
1245 
1246  if (sscanf(mapping_de->d_name, LOGICAL_REWRITE_FORMAT,
1247  &dboid, &relid, &hi, &lo, &rewrite_xid, &create_xid) != 6)
1248  elog(ERROR, "could not parse filename \"%s\"", mapping_de->d_name);
1249 
1250  lsn = ((uint64) hi) << 32 | lo;
1251 
1252  if (lsn < cutoff || cutoff == InvalidXLogRecPtr)
1253  {
1254  elog(DEBUG1, "removing logical rewrite file \"%s\"", path);
1255  if (unlink(path) < 0)
1256  ereport(ERROR,
1258  errmsg("could not remove file \"%s\": %m", path)));
1259  }
1260  else
1261  {
1262  int fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
1263 
1264  /*
1265  * The file cannot vanish due to concurrency since this function
1266  * is the only one removing logical mappings and it's run while
1267  * CheckpointLock is held exclusively.
1268  */
1269  if (fd < 0)
1270  ereport(ERROR,
1272  errmsg("could not open file \"%s\": %m", path)));
1273 
1274  /*
1275  * We could try to avoid fsyncing files that either haven't
1276  * changed or have only been created since the checkpoint's start,
1277  * but it's currently not deemed worth the effort.
1278  */
1280  if (pg_fsync(fd) != 0)
1281  ereport(ERROR,
1283  errmsg("could not fsync file \"%s\": %m", path)));
1285  CloseTransientFile(fd);
1286  }
1287  }
1288  FreeDir(mappings_dir);
1289 }
#define HeapTupleHeaderGetUpdateXid(tup)
Definition: htup_details.h:364
bool HeapTupleHeaderIsOnlyLocked(HeapTupleHeader tuple)
Definition: tqual.c:1596
#define ItemPointerIsValid(pointer)
Definition: itemptr.h:60
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:611
#define TOAST_TUPLE_THRESHOLD
Definition: tuptoaster.h:55
union HeapTupleHeaderData::@45 t_choice
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:198
void end_heap_rewrite(RewriteState state)
Definition: rewriteheap.c:314
#define DEBUG1
Definition: elog.h:25
dlist_node * cur
Definition: ilist.h:180
TidHashKey key
Definition: rewriteheap.c:184
void heap_xlog_logical_rewrite(XLogReaderState *r)
Definition: rewriteheap.c:1120
HeapTupleFields t_heap
Definition: htup_details.h:151
TransactionId rs_logical_xmin
Definition: rewriteheap.c:154
File PathNameOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:1358
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
#define HASH_CONTEXT
Definition: hsearch.h:93
#define HASH_ELEM
Definition: hsearch.h:87
#define dlist_foreach_modify(iter, lhead)
Definition: ilist.h:524
uint32 TransactionId
Definition: c.h:463
MemoryContext hcxt
Definition: hsearch.h:78
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:248
static void logical_rewrite_heap_tuple(RewriteState state, ItemPointerData old_tid, HeapTuple new_tuple)
Definition: rewriteheap.c:1046
static void logical_end_heap_rewrite(RewriteState state)
Definition: rewriteheap.c:949
HeapTuple toast_insert_or_update(Relation rel, HeapTuple newtup, HeapTuple oldtup, int options)
Definition: tuptoaster.c:534
#define write(a, b, c)
Definition: win32.h:14
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
struct SMgrRelationData * rd_smgr
Definition: rel.h:87
TransactionId rs_freeze_xid
Definition: rewriteheap.c:152
#define XLOG_HEAP2_REWRITE
Definition: heapam_xlog.h:53
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:317
MultiXactId rs_cutoff_multi
Definition: rewriteheap.c:156
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Pointer Item
Definition: item.h:17
Size entrysize
Definition: hsearch.h:73
Relation rs_new_rel
Definition: rewriteheap.c:144
int errcode(int sqlerrcode)
Definition: elog.c:575
#define HEAP2_XACT_MASK
Definition: htup_details.h:274
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition: bufpage.h:412
UnresolvedTupData * UnresolvedTup
Definition: rewriteheap.c:189
void heap_sync(Relation rel)
Definition: heapam.c:9232
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define HEAP_INSERT_SKIP_WAL
Definition: heapam.h:28
void CheckPointLogicalRewriteHeap(void)
Definition: rewriteheap.c:1201
uint32 BlockNumber
Definition: block.h:31
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:904
#define HEAP_UPDATED
Definition: htup_details.h:200
ItemPointerData old_tid
Definition: rewriteheap.c:185
Form_pg_class rd_rel
Definition: rel.h:114
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1373
unsigned int Oid
Definition: postgres_ext.h:31
Definition: dirent.h:9
void ProcArrayGetReplicationSlotXmin(TransactionId *xmin, TransactionId *catalog_xmin)
Definition: procarray.c:2986
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1069
uint16 OffsetNumber
Definition: off.h:24
HeapTupleHeader t_data
Definition: htup.h:67
#define RelationOpenSmgr(relation)
Definition: rel.h:469
Definition: dynahash.c:208
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
void pfree(void *pointer)
Definition: mcxt.c:936
uint32 rs_num_rewrite_mappings
Definition: rewriteheap.c:164
#define XLogRecGetData(decoder)
Definition: xlogreader.h:226
Definition: dirent.c:25
#define ERROR
Definition: elog.h:43
ItemPointerData new_tid
Definition: rewriteheap.c:194
Size PageGetHeapFreeSpace(Page page)
Definition: bufpage.c:662
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2403
#define HEAP_XMAX_INVALID
Definition: htup_details.h:198
char path[MAXPGPATH]
Definition: rewriteheap.c:210
XLogRecPtr GetXLogInsertRecPtr(void)
Definition: xlog.c:11174
ItemPointerData t_ctid
Definition: htup_details.h:155
HTAB * rs_unresolved_tups
Definition: rewriteheap.c:161
#define MAXPGPATH
ItemPointerData t_self
Definition: htup.h:65
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:197
int FileSync(File file, uint32 wait_event_info)
Definition: fd.c:2079
TransactionId xid
Definition: rewriteheap.c:205
TransactionId GetCurrentTransactionId(void)
Definition: xact.c:418
uint32 t_len
Definition: htup.h:64
static void logical_rewrite_log_mapping(RewriteState state, TransactionId xid, LogicalRewriteMappingData *map)
Definition: rewriteheap.c:979
#define MaxHeapTupleSize
Definition: htup_details.h:566
int errcode_for_file_access(void)
Definition: elog.c:598
XLogRecPtr ReplicationSlotsComputeLogicalRestartLSN(void)
Definition: slot.c:784
#define InvalidTransactionId
Definition: transam.h:31
unsigned int uint32
Definition: c.h:314
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2607
int FileWrite(File file, char *buffer, int amount, uint32 wait_event_info)
Definition: fd.c:1958
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1260
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
bool heap_freeze_tuple(HeapTupleHeader tuple, TransactionId relfrozenxid, TransactionId relminmxid, TransactionId cutoff_xid, TransactionId cutoff_multi)
Definition: heapam.c:6914
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
#define ereport(elevel, rest)
Definition: elog.h:122
XLogRecPtr rs_begin_lsn
Definition: rewriteheap.c:160
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
#define RelationGetTargetPageFreeSpace(relation, defaultff)
Definition: rel.h:316
#define S_ISREG(m)
Definition: win32_port.h:310
HTAB * rs_logical_mappings
Definition: rewriteheap.c:163
int CloseTransientFile(int fd)
Definition: fd.c:2573
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:165
TransactionId rs_oldest_xmin
Definition: rewriteheap.c:150
#define stat(a, b)
Definition: win32_port.h:266
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:323
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:415
#define HASH_BLOBS
Definition: hsearch.h:88
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:224
MemoryContext rs_cxt
Definition: rewriteheap.c:158
void * palloc0(Size size)
Definition: mcxt.c:864
#define RELKIND_TOASTVALUE
Definition: pg_class.h:163
#define RelationIsAccessibleInLogicalDecoding(relation)
Definition: rel.h:568
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:316
#define HEAP_XMAX_IS_LOCKED_ONLY(infomask)
Definition: htup_details.h:221
Oid MyDatabaseId
Definition: globals.c:77
Size keysize
Definition: hsearch.h:72
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:199
ItemPointerData tid
Definition: rewriteheap.c:176
#define InvalidOffsetNumber
Definition: off.h:26
#define InvalidOid
Definition: postgres_ext.h:36
static void dlist_init(dlist_head *head)
Definition: ilist.h:278
struct RewriteStateData RewriteStateData
TransactionId mapped_xid
Definition: heapam_xlog.h:356
TransactionId MultiXactId
Definition: c.h:473
RelFileNode rd_node
Definition: rel.h:85
void FileClose(File file)
Definition: fd.c:1749
static void logical_begin_heap_rewrite(RewriteState state)
Definition: rewriteheap.c:802
ItemPointerData new_tid
Definition: rewriteheap.h:40
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:688
HTAB * rs_old_new_tid_map
Definition: rewriteheap.c:162
Definition: regguts.h:298
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2673
#define HeapTupleHeaderGetXmin(tup)
Definition: htup_details.h:312
size_t Size
Definition: c.h:422
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1236
dlist_head mappings
Definition: rewriteheap.c:209
void PageSetChecksumInplace(Page page, BlockNumber blkno)
Definition: bufpage.c:1195
#define MAXALIGN(LEN)
Definition: c.h:641
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:8192
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1387
#define RelationNeedsWAL(relation)
Definition: rel.h:514
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:29
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1377
static void logical_heap_rewrite_flush_mappings(RewriteState state)
Definition: rewriteheap.c:851
struct RewriteMappingFile RewriteMappingFile
#define LOGICAL_REWRITE_FORMAT
Definition: rewriteheap.h:54
void smgrextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: smgr.c:600
#define lstat(path, sb)
Definition: win32_port.h:255
#define HEAP_INSERT_SKIP_FSM
Definition: heapam.h:29
bool rewrite_heap_dead_tuple(RewriteState state, HeapTuple old_tuple)
Definition: rewriteheap.c:581
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:150
#define HeapTupleHasExternal(tuple)
Definition: htup_details.h:679
void * palloc(Size size)
Definition: mcxt.c:835
int errmsg(const char *fmt,...)
Definition: elog.c:797
TransactionId xmin
Definition: rewriteheap.c:175
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:693
LogicalRewriteMappingData map
Definition: rewriteheap.c:219
XLogRecPtr log_newpage(RelFileNode *rnode, ForkNumber forkNum, BlockNumber blkno, Page page, bool page_std)
Definition: xloginsert.c:972
static void raw_heap_insert(RewriteState state, HeapTuple tup)
Definition: rewriteheap.c:631
struct LogicalRewriteMappingData LogicalRewriteMappingData
#define HEAP_XACT_MASK
Definition: htup_details.h:209
int pg_fsync(int fd)
Definition: fd.c:348
Relation rs_old_rel
Definition: rewriteheap.c:143
char d_name[MAX_PATH]
Definition: dirent.h:14
#define elog
Definition: elog.h:219
#define HEAP_DEFAULT_FILLFACTOR
Definition: rel.h:287
struct RewriteMappingDataEntry RewriteMappingDataEntry
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
BlockNumber rs_blockno
Definition: rewriteheap.c:146
void XLogBeginInsert(void)
Definition: xloginsert.c:120
void rewrite_heap_tuple(RewriteState state, HeapTuple old_tuple, HeapTuple new_tuple)
Definition: rewriteheap.c:380
#define RelationGetRelid(relation)
Definition: rel.h:425
OldToNewMappingData * OldToNewMapping
Definition: rewriteheap.c:197
int FreeDir(DIR *dir)
Definition: fd.c:2725
#define PageGetItem(page, itemId)
Definition: bufpage.h:336
Pointer Page
Definition: bufpage.h:74
#define ItemPointerSet(pointer, blockNumber, offNum)
Definition: itemptr.h:105
void PageInit(Page page, Size pageSize, Size specialSize)
Definition: bufpage.c:41
ItemPointerData old_tid
Definition: rewriteheap.h:39
#define ftruncate(a, b)
Definition: win32_port.h:60