PostgreSQL Source Code git master
Loading...
Searching...
No Matches
rewriteheap.h File Reference
#include "access/htup.h"
#include "storage/itemptr.h"
#include "storage/relfilelocator.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 old_heap, Relation new_heap, TransactionId oldest_xmin, TransactionId freeze_xid, MultiXactId cutoff_multi)
 
void end_heap_rewrite (RewriteState state)
 
void rewrite_heap_tuple (RewriteState state, HeapTuple old_tuple, HeapTuple new_tuple)
 
bool rewrite_heap_dead_tuple (RewriteState state, HeapTuple old_tuple)
 
void CheckPointLogicalRewriteHeap (void)
 

Macro Definition Documentation

◆ LOGICAL_REWRITE_FORMAT

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

Definition at line 54 of file rewriteheap.h.

Typedef Documentation

◆ LogicalRewriteMappingData

◆ RewriteState

Definition at line 22 of file rewriteheap.h.

Function Documentation

◆ begin_heap_rewrite()

RewriteState begin_heap_rewrite ( Relation  old_heap,
Relation  new_heap,
TransactionId  oldest_xmin,
TransactionId  freeze_xid,
MultiXactId  cutoff_multi 
)
extern

Definition at line 235 of file rewriteheap.c.

237{
242
243 /*
244 * To ease cleanup, make a separate context that will contain the
245 * RewriteState struct itself plus all subsidiary data.
246 */
248 "Table rewrite",
251
252 /* Create and fill in the state struct */
254
255 state->rs_old_rel = old_heap;
256 state->rs_new_rel = new_heap;
257 state->rs_buffer = NULL;
258 /* new_heap needn't be empty, just locked */
260 state->rs_oldest_xmin = oldest_xmin;
261 state->rs_freeze_xid = freeze_xid;
262 state->rs_cutoff_multi = cutoff_multi;
263 state->rs_cxt = rw_cxt;
265
266 /* Initialize hash tables used to track update chains */
267 hash_ctl.keysize = sizeof(TidHashKey);
268 hash_ctl.entrysize = sizeof(UnresolvedTupData);
269 hash_ctl.hcxt = state->rs_cxt;
270
271 state->rs_unresolved_tups =
272 hash_create("Rewrite / Unresolved ctids",
273 128, /* arbitrary initial size */
274 &hash_ctl,
276
277 hash_ctl.entrysize = sizeof(OldToNewMappingData);
278
279 state->rs_old_new_tid_map =
280 hash_create("Rewrite / Old to new tid map",
281 128, /* arbitrary initial size */
282 &hash_ctl,
284
286
288
289 return state;
290}
#define RelationGetNumberOfBlocks(reln)
Definition bufmgr.h:307
BulkWriteState * smgr_bulk_start_rel(Relation rel, ForkNumber forknum)
Definition bulk_write.c:87
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
Definition dynahash.c:358
#define palloc0_object(type)
Definition fe_memutils.h:75
#define HASH_CONTEXT
Definition hsearch.h:102
#define HASH_ELEM
Definition hsearch.h:95
#define HASH_BLOBS
Definition hsearch.h:97
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition memutils.h:160
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
static int fb(int x)
@ MAIN_FORKNUM
Definition relpath.h:58
static void logical_begin_heap_rewrite(RewriteState state)

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, CurrentMemoryContext, fb(), HASH_BLOBS, HASH_CONTEXT, hash_create(), HASH_ELEM, logical_begin_heap_rewrite(), MAIN_FORKNUM, MemoryContextSwitchTo(), palloc0_object, RelationGetNumberOfBlocks, and smgr_bulk_start_rel().

Referenced by heapam_relation_copy_for_cluster().

◆ CheckPointLogicalRewriteHeap()

void CheckPointLogicalRewriteHeap ( void  )
extern

Definition at line 1158 of file rewriteheap.c.

1159{
1160 XLogRecPtr cutoff;
1161 XLogRecPtr redo;
1163 struct dirent *mapping_de;
1164 char path[MAXPGPATH + sizeof(PG_LOGICAL_MAPPINGS_DIR)];
1165
1166 /*
1167 * We start of with a minimum of the last redo pointer. No new decoding
1168 * slot will start before that, so that's a safe upper bound for removal.
1169 */
1170 redo = GetRedoRecPtr();
1171
1172 /* now check for the restart ptrs from existing slots */
1174
1175 /* don't start earlier than the restart lsn */
1176 if (XLogRecPtrIsValid(cutoff) && redo < cutoff)
1177 cutoff = redo;
1178
1181 {
1182 Oid dboid;
1183 Oid relid;
1184 XLogRecPtr lsn;
1187 uint32 hi,
1188 lo;
1190
1191 if (strcmp(mapping_de->d_name, ".") == 0 ||
1192 strcmp(mapping_de->d_name, "..") == 0)
1193 continue;
1194
1195 snprintf(path, sizeof(path), "%s/%s", PG_LOGICAL_MAPPINGS_DIR, mapping_de->d_name);
1196 de_type = get_dirent_type(path, mapping_de, false, DEBUG1);
1197
1199 continue;
1200
1201 /* Skip over files that cannot be ours. */
1202 if (strncmp(mapping_de->d_name, "map-", 4) != 0)
1203 continue;
1204
1206 &dboid, &relid, &hi, &lo, &rewrite_xid, &create_xid) != 6)
1207 elog(ERROR, "could not parse filename \"%s\"", mapping_de->d_name);
1208
1209 lsn = ((uint64) hi) << 32 | lo;
1210
1211 if (lsn < cutoff || !XLogRecPtrIsValid(cutoff))
1212 {
1213 elog(DEBUG1, "removing logical rewrite file \"%s\"", path);
1214 if (unlink(path) < 0)
1215 ereport(ERROR,
1217 errmsg("could not remove file \"%s\": %m", path)));
1218 }
1219 else
1220 {
1221 /* on some operating systems fsyncing a file requires O_RDWR */
1222 int fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
1223
1224 /*
1225 * The file cannot vanish due to concurrency since this function
1226 * is the only one removing logical mappings and only one
1227 * checkpoint can be in progress at a time.
1228 */
1229 if (fd < 0)
1230 ereport(ERROR,
1232 errmsg("could not open file \"%s\": %m", path)));
1233
1234 /*
1235 * We could try to avoid fsyncing files that either haven't
1236 * changed or have only been created since the checkpoint's start,
1237 * but it's currently not deemed worth the effort.
1238 */
1240 if (pg_fsync(fd) != 0)
1243 errmsg("could not fsync file \"%s\": %m", path)));
1245
1246 if (CloseTransientFile(fd) != 0)
1247 ereport(ERROR,
1249 errmsg("could not close file \"%s\": %m", path)));
1250 }
1251 }
1253
1254 /* persist directory entries to disk */
1256}
#define PG_BINARY
Definition c.h:1376
uint64_t uint64
Definition c.h:619
uint32_t uint32
Definition c.h:618
uint32 TransactionId
Definition c.h:738
int errcode_for_file_access(void)
Definition elog.c:897
#define DEBUG1
Definition elog.h:30
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
int FreeDir(DIR *dir)
Definition fd.c:3009
int CloseTransientFile(int fd)
Definition fd.c:2855
void fsync_fname(const char *fname, bool isdir)
Definition fd.c:757
int data_sync_elevel(int elevel)
Definition fd.c:3986
DIR * AllocateDir(const char *dirname)
Definition fd.c:2891
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition fd.c:2957
int pg_fsync(int fd)
Definition fd.c:390
int OpenTransientFile(const char *fileName, int fileFlags)
Definition fd.c:2678
PGFileType get_dirent_type(const char *path, const struct dirent *de, bool look_through_symlinks, int elevel)
Definition file_utils.c:547
PGFileType
Definition file_utils.h:19
@ PGFILETYPE_REG
Definition file_utils.h:22
@ PGFILETYPE_ERROR
Definition file_utils.h:20
static char * errmsg
#define MAXPGPATH
#define snprintf
Definition port.h:260
unsigned int Oid
static int fd(const char *x, int i)
#define PG_LOGICAL_MAPPINGS_DIR
#define LOGICAL_REWRITE_FORMAT
Definition rewriteheap.h:54
XLogRecPtr ReplicationSlotsComputeLogicalRestartLSN(void)
Definition slot.c:1371
Definition dirent.c:26
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition wait_event.h:69
static void pgstat_report_wait_end(void)
Definition wait_event.h:85
XLogRecPtr GetRedoRecPtr(void)
Definition xlog.c:6547
#define XLogRecPtrIsValid(r)
Definition xlogdefs.h:29
uint64 XLogRecPtr
Definition xlogdefs.h:21

References AllocateDir(), CloseTransientFile(), data_sync_elevel(), DEBUG1, elog, ereport, errcode_for_file_access(), errmsg, ERROR, fb(), fd(), FreeDir(), fsync_fname(), get_dirent_type(), GetRedoRecPtr(), LOGICAL_REWRITE_FORMAT, MAXPGPATH, OpenTransientFile(), PG_BINARY, pg_fsync(), PG_LOGICAL_MAPPINGS_DIR, PGFILETYPE_ERROR, PGFILETYPE_REG, pgstat_report_wait_end(), pgstat_report_wait_start(), ReadDir(), ReplicationSlotsComputeLogicalRestartLSN(), snprintf, and XLogRecPtrIsValid.

Referenced by CheckPointGuts().

◆ end_heap_rewrite()

void end_heap_rewrite ( RewriteState  state)
extern

Definition at line 298 of file rewriteheap.c.

299{
302
303 /*
304 * Write any remaining tuples in the UnresolvedTups table. If we have any
305 * left, they should in fact be dead, but let's err on the safe side.
306 */
307 hash_seq_init(&seq_status, state->rs_unresolved_tups);
308
310 {
311 ItemPointerSetInvalid(&unresolved->tuple->t_data->t_ctid);
313 }
314
315 /* Write the last page, if any */
316 if (state->rs_buffer)
317 {
318 smgr_bulk_write(state->rs_bulkstate, state->rs_blockno, state->rs_buffer, true);
319 state->rs_buffer = NULL;
320 }
321
322 smgr_bulk_finish(state->rs_bulkstate);
323
325
326 /* Deleting the context frees everything */
327 MemoryContextDelete(state->rs_cxt);
328}
void smgr_bulk_write(BulkWriteState *bulkstate, BlockNumber blocknum, BulkWriteBuffer buf, bool page_std)
Definition bulk_write.c:323
void smgr_bulk_finish(BulkWriteState *bulkstate)
Definition bulk_write.c:130
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition dynahash.c:1415
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition dynahash.c:1380
static void ItemPointerSetInvalid(ItemPointerData *pointer)
Definition itemptr.h:184
void MemoryContextDelete(MemoryContext context)
Definition mcxt.c:472
static void raw_heap_insert(RewriteState state, HeapTuple tup)
static void logical_end_heap_rewrite(RewriteState state)

References fb(), hash_seq_init(), hash_seq_search(), ItemPointerSetInvalid(), logical_end_heap_rewrite(), MemoryContextDelete(), raw_heap_insert(), smgr_bulk_finish(), and smgr_bulk_write().

Referenced by heapam_relation_copy_for_cluster().

◆ rewrite_heap_dead_tuple()

bool rewrite_heap_dead_tuple ( RewriteState  state,
HeapTuple  old_tuple 
)
extern

Definition at line 547 of file rewriteheap.c.

548{
549 /*
550 * If we have already seen an earlier tuple in the update chain that
551 * points to this tuple, let's forget about that earlier tuple. It's in
552 * fact dead as well, our simple xmax < OldestXmin test in
553 * HeapTupleSatisfiesVacuum just wasn't enough to detect it. It happens
554 * when xmin of a tuple is greater than xmax, which sounds
555 * counter-intuitive but is perfectly valid.
556 *
557 * We don't bother to try to detect the situation the other way round,
558 * when we encounter the dead tuple first and then the recently dead one
559 * that points to it. If that happens, we'll have some unmatched entries
560 * in the UnresolvedTups hash table at the end. That can happen anyway,
561 * because a vacuum might have removed the dead tuple in the chain before
562 * us.
563 */
566 bool found;
567
568 memset(&hashkey, 0, sizeof(hashkey));
570 hashkey.tid = old_tuple->t_self;
571
572 unresolved = hash_search(state->rs_unresolved_tups, &hashkey,
573 HASH_FIND, NULL);
574
575 if (unresolved != NULL)
576 {
577 /* Need to free the contained tuple as well as the hashtable entry */
579 hash_search(state->rs_unresolved_tups, &hashkey,
580 HASH_REMOVE, &found);
581 Assert(found);
582 return true;
583 }
584
585 return false;
586}
#define Assert(condition)
Definition c.h:945
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition dynahash.c:952
void heap_freetuple(HeapTuple htup)
Definition heaptuple.c:1384
@ HASH_FIND
Definition hsearch.h:113
@ HASH_REMOVE
Definition hsearch.h:115
static TransactionId HeapTupleHeaderGetXmin(const HeapTupleHeaderData *tup)

References Assert, fb(), HASH_FIND, HASH_REMOVE, hash_search(), heap_freetuple(), and HeapTupleHeaderGetXmin().

Referenced by heapam_relation_copy_for_cluster().

◆ rewrite_heap_tuple()

void rewrite_heap_tuple ( RewriteState  state,
HeapTuple  old_tuple,
HeapTuple  new_tuple 
)
extern

Definition at line 342 of file rewriteheap.c.

344{
346 ItemPointerData old_tid;
348 bool found;
349 bool free_new;
350
352
353 /*
354 * Copy the original tuple's visibility information into new_tuple.
355 *
356 * XXX we might later need to copy some t_infomask2 bits, too? Right now,
357 * we intentionally clear the HOT status bits.
358 */
359 memcpy(&new_tuple->t_data->t_choice.t_heap,
360 &old_tuple->t_data->t_choice.t_heap,
361 sizeof(HeapTupleFields));
362
363 new_tuple->t_data->t_infomask &= ~HEAP_XACT_MASK;
364 new_tuple->t_data->t_infomask2 &= ~HEAP2_XACT_MASK;
365 new_tuple->t_data->t_infomask |=
366 old_tuple->t_data->t_infomask & HEAP_XACT_MASK;
367
368 /*
369 * While we have our hands on the tuple, we may as well freeze any
370 * eligible xmin or xmax, so that future VACUUM effort can be saved.
371 */
373 state->rs_old_rel->rd_rel->relfrozenxid,
374 state->rs_old_rel->rd_rel->relminmxid,
375 state->rs_freeze_xid,
376 state->rs_cutoff_multi);
377
378 /*
379 * Invalid ctid means that ctid should point to the tuple itself. We'll
380 * override it later if the tuple is part of an update chain.
381 */
382 ItemPointerSetInvalid(&new_tuple->t_data->t_ctid);
383
384 /*
385 * If the tuple has been updated, check the old-to-new mapping hash table.
386 *
387 * Note that this check relies on the HeapTupleSatisfiesVacuum() in
388 * heapam_relation_copy_for_cluster() to have set hint bits.
389 */
390 if (!((old_tuple->t_data->t_infomask & HEAP_XMAX_INVALID) ||
393 !(ItemPointerEquals(&(old_tuple->t_self),
394 &(old_tuple->t_data->t_ctid))))
395 {
397
398 memset(&hashkey, 0, sizeof(hashkey));
400 hashkey.tid = old_tuple->t_data->t_ctid;
401
403 hash_search(state->rs_old_new_tid_map, &hashkey,
404 HASH_FIND, NULL);
405
406 if (mapping != NULL)
407 {
408 /*
409 * We've already copied the tuple that t_ctid points to, so we can
410 * set the ctid of this tuple to point to the new location, and
411 * insert it right away.
412 */
413 new_tuple->t_data->t_ctid = mapping->new_tid;
414
415 /* We don't need the mapping entry anymore */
416 hash_search(state->rs_old_new_tid_map, &hashkey,
417 HASH_REMOVE, &found);
418 Assert(found);
419 }
420 else
421 {
422 /*
423 * We haven't seen the tuple t_ctid points to yet. Stash this
424 * tuple into unresolved_tups to be written later.
425 */
427
428 unresolved = hash_search(state->rs_unresolved_tups, &hashkey,
429 HASH_ENTER, &found);
430 Assert(!found);
431
432 unresolved->old_tid = old_tuple->t_self;
434
435 /*
436 * We can't do anything more now, since we don't know where the
437 * tuple will be written.
438 */
440 return;
441 }
442 }
443
444 /*
445 * Now we will write the tuple, and then check to see if it is the B tuple
446 * in any new or known pair. When we resolve a known pair, we will be
447 * able to write that pair's A tuple, and then we have to check if it
448 * resolves some other pair. Hence, we need a loop here.
449 */
450 old_tid = old_tuple->t_self;
451 free_new = false;
452
453 for (;;)
454 {
455 ItemPointerData new_tid;
456
457 /* Insert the tuple and find out where it's put in new_heap */
459 new_tid = new_tuple->t_self;
460
462
463 /*
464 * If the tuple is the updated version of a row, and the prior version
465 * wouldn't be DEAD yet, then we need to either resolve the prior
466 * version (if it's waiting in rs_unresolved_tups), or make an entry
467 * in rs_old_new_tid_map (so we can resolve it when we do see it). The
468 * previous tuple's xmax would equal this one's xmin, so it's
469 * RECENTLY_DEAD if and only if the xmin is not before OldestXmin.
470 */
471 if ((new_tuple->t_data->t_infomask & HEAP_UPDATED) &&
473 state->rs_oldest_xmin))
474 {
475 /*
476 * Okay, this is B in an update pair. See if we've seen A.
477 */
479
480 memset(&hashkey, 0, sizeof(hashkey));
482 hashkey.tid = old_tid;
483
484 unresolved = hash_search(state->rs_unresolved_tups, &hashkey,
485 HASH_FIND, NULL);
486
487 if (unresolved != NULL)
488 {
489 /*
490 * We have seen and memorized the previous tuple already. Now
491 * that we know where we inserted the tuple its t_ctid points
492 * to, fix its t_ctid and insert it to the new heap.
493 */
494 if (free_new)
496 new_tuple = unresolved->tuple;
497 free_new = true;
498 old_tid = unresolved->old_tid;
499 new_tuple->t_data->t_ctid = new_tid;
500
501 /*
502 * We don't need the hash entry anymore, but don't free its
503 * tuple just yet.
504 */
505 hash_search(state->rs_unresolved_tups, &hashkey,
506 HASH_REMOVE, &found);
507 Assert(found);
508
509 /* loop back to insert the previous tuple in the chain */
510 continue;
511 }
512 else
513 {
514 /*
515 * Remember the new tid of this tuple. We'll use it to set the
516 * ctid when we find the previous tuple in the chain.
517 */
519
520 mapping = hash_search(state->rs_old_new_tid_map, &hashkey,
521 HASH_ENTER, &found);
522 Assert(!found);
523
524 mapping->new_tid = new_tid;
525 }
526 }
527
528 /* Done with this (chain of) tuples, for now */
529 if (free_new)
531 break;
532 }
533
535}
bool heap_freeze_tuple(HeapTupleHeader tuple, TransactionId relfrozenxid, TransactionId relminmxid, TransactionId FreezeLimit, TransactionId MultiXactCutoff)
Definition heapam.c:7501
bool HeapTupleHeaderIsOnlyLocked(HeapTupleHeader tuple)
HeapTuple heap_copytuple(HeapTuple tuple)
Definition heaptuple.c:698
@ HASH_ENTER
Definition hsearch.h:114
#define HEAP_XACT_MASK
static bool HeapTupleHeaderIndicatesMovedPartitions(const HeapTupleHeaderData *tup)
#define HEAP_XMAX_INVALID
static TransactionId HeapTupleHeaderGetUpdateXid(const HeapTupleHeaderData *tup)
#define HEAP_UPDATED
bool ItemPointerEquals(const ItemPointerData *pointer1, const ItemPointerData *pointer2)
Definition itemptr.c:35
static void logical_rewrite_heap_tuple(RewriteState state, ItemPointerData old_tid, HeapTuple new_tuple)
OldToNewMappingData * OldToNewMapping
static bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition transam.h:263

References Assert, fb(), HASH_ENTER, HASH_FIND, HASH_REMOVE, hash_search(), 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(), raw_heap_insert(), and TransactionIdPrecedes().

Referenced by reform_and_rewrite_tuple().