PostgreSQL Source Code  git master
relmapper.h File Reference
#include "access/xlogreader.h"
#include "lib/stringinfo.h"
Include dependency graph for relmapper.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  xl_relmap_update
 

Macros

#define XLOG_RELMAP_UPDATE   0x00
 
#define MinSizeOfRelmapUpdate   offsetof(xl_relmap_update, data)
 

Typedefs

typedef struct xl_relmap_update xl_relmap_update
 

Functions

RelFileNumber RelationMapOidToFilenumber (Oid relationId, bool shared)
 
Oid RelationMapFilenumberToOid (RelFileNumber filenumber, bool shared)
 
RelFileNumber RelationMapOidToFilenumberForDatabase (char *dbpath, Oid relationId)
 
void RelationMapCopy (Oid dbid, Oid tsid, char *srcdbpath, char *dstdbpath)
 
void RelationMapUpdateMap (Oid relationId, RelFileNumber fileNumber, bool shared, bool immediate)
 
void RelationMapRemoveMapping (Oid relationId)
 
void RelationMapInvalidate (bool shared)
 
void RelationMapInvalidateAll (void)
 
void AtCCI_RelationMap (void)
 
void AtEOXact_RelationMap (bool isCommit, bool isParallelWorker)
 
void AtPrepare_RelationMap (void)
 
void CheckPointRelationMap (void)
 
void RelationMapFinishBootstrap (void)
 
void RelationMapInitialize (void)
 
void RelationMapInitializePhase2 (void)
 
void RelationMapInitializePhase3 (void)
 
Size EstimateRelationMapSpace (void)
 
void SerializeRelationMap (Size maxSize, char *startAddress)
 
void RestoreRelationMap (char *startAddress)
 
void relmap_redo (XLogReaderState *record)
 
void relmap_desc (StringInfo buf, XLogReaderState *record)
 
const char * relmap_identify (uint8 info)
 

Macro Definition Documentation

◆ MinSizeOfRelmapUpdate

#define MinSizeOfRelmapUpdate   offsetof(xl_relmap_update, data)

Definition at line 35 of file relmapper.h.

◆ XLOG_RELMAP_UPDATE

#define XLOG_RELMAP_UPDATE   0x00

Definition at line 25 of file relmapper.h.

Typedef Documentation

◆ xl_relmap_update

Function Documentation

◆ AtCCI_RelationMap()

void AtCCI_RelationMap ( void  )

Definition at line 504 of file relmapper.c.

505 {
507  {
510  true);
512  }
514  {
517  true);
519  }
520 }
static RelMapFile pending_local_updates
Definition: relmapper.c:135
static RelMapFile active_local_updates
Definition: relmapper.c:133
static RelMapFile pending_shared_updates
Definition: relmapper.c:134
static void merge_map_updates(RelMapFile *map, const RelMapFile *updates, bool add_okay)
Definition: relmapper.c:416
static RelMapFile active_shared_updates
Definition: relmapper.c:132
int32 num_mappings
Definition: relmapper.c:93

References active_local_updates, active_shared_updates, merge_map_updates(), RelMapFile::num_mappings, pending_local_updates, and pending_shared_updates.

Referenced by AtCCI_LocalCache().

◆ AtEOXact_RelationMap()

void AtEOXact_RelationMap ( bool  isCommit,
bool  isParallelWorker 
)

Definition at line 541 of file relmapper.c.

542 {
543  if (isCommit && !isParallelWorker)
544  {
545  /*
546  * We should not get here with any "pending" updates. (We could
547  * logically choose to treat such as committed, but in the current
548  * code this should never happen.)
549  */
552 
553  /*
554  * Write any active updates to the actual map files, then reset them.
555  */
557  {
560  }
562  {
565  }
566  }
567  else
568  {
569  /* Abort or parallel worker --- drop all local and pending updates */
570  Assert(!isParallelWorker || pending_shared_updates.num_mappings == 0);
571  Assert(!isParallelWorker || pending_local_updates.num_mappings == 0);
572 
577  }
578 }
Assert(fmt[strlen(fmt) - 1] !='\n')
static void perform_relmap_update(bool shared, const RelMapFile *updates)
Definition: relmapper.c:1028

References active_local_updates, active_shared_updates, Assert(), RelMapFile::num_mappings, pending_local_updates, pending_shared_updates, and perform_relmap_update().

Referenced by AbortTransaction(), and CommitTransaction().

◆ AtPrepare_RelationMap()

void AtPrepare_RelationMap ( void  )

Definition at line 588 of file relmapper.c.

589 {
594  ereport(ERROR,
595  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
596  errmsg("cannot PREPARE a transaction that modified relation mapping")));
597 }
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149

References active_local_updates, active_shared_updates, ereport, errcode(), errmsg(), ERROR, RelMapFile::num_mappings, pending_local_updates, and pending_shared_updates.

Referenced by PrepareTransaction().

◆ CheckPointRelationMap()

void CheckPointRelationMap ( void  )

Definition at line 611 of file relmapper.c.

612 {
613  LWLockAcquire(RelationMappingLock, LW_SHARED);
614  LWLockRelease(RelationMappingLock);
615 }
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1195
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1808
@ LW_SHARED
Definition: lwlock.h:117

References LW_SHARED, LWLockAcquire(), and LWLockRelease().

Referenced by CheckPointGuts().

◆ EstimateRelationMapSpace()

Size EstimateRelationMapSpace ( void  )

Definition at line 711 of file relmapper.c.

712 {
713  return sizeof(SerializedActiveRelMaps);
714 }
struct SerializedActiveRelMaps SerializedActiveRelMaps

Referenced by InitializeParallelDSM(), and SerializeRelationMap().

◆ RelationMapCopy()

void RelationMapCopy ( Oid  dbid,
Oid  tsid,
char *  srcdbpath,
char *  dstdbpath 
)

Definition at line 293 of file relmapper.c.

294 {
295  RelMapFile map;
296 
297  /*
298  * Read the relmap file from the source database.
299  */
300  read_relmap_file(&map, srcdbpath, false, ERROR);
301 
302  /*
303  * Write the same data into the destination database's relmap file.
304  *
305  * No sinval is needed because no one can be connected to the destination
306  * database yet. For the same reason, there is no need to acquire
307  * RelationMappingLock.
308  *
309  * There's no point in trying to preserve files here. The new database
310  * isn't usable yet anyway, and won't ever be if we can't install a relmap
311  * file.
312  */
313  write_relmap_file(&map, true, false, false, dbid, tsid, dstdbpath);
314 }
static void write_relmap_file(RelMapFile *newmap, bool write_wal, bool send_sinval, bool preserve_files, Oid dbid, Oid tsid, const char *dbpath)
Definition: relmapper.c:887
static void read_relmap_file(RelMapFile *map, char *dbpath, bool lock_held, int elevel)
Definition: relmapper.c:782

References ERROR, read_relmap_file(), and write_relmap_file().

Referenced by CreateDatabaseUsingWalLog().

◆ RelationMapFilenumberToOid()

Oid RelationMapFilenumberToOid ( RelFileNumber  filenumber,
bool  shared 
)

Definition at line 219 of file relmapper.c.

220 {
221  const RelMapFile *map;
222  int32 i;
223 
224  /* If there are active updates, believe those over the main maps */
225  if (shared)
226  {
227  map = &active_shared_updates;
228  for (i = 0; i < map->num_mappings; i++)
229  {
230  if (filenumber == map->mappings[i].mapfilenumber)
231  return map->mappings[i].mapoid;
232  }
233  map = &shared_map;
234  for (i = 0; i < map->num_mappings; i++)
235  {
236  if (filenumber == map->mappings[i].mapfilenumber)
237  return map->mappings[i].mapoid;
238  }
239  }
240  else
241  {
242  map = &active_local_updates;
243  for (i = 0; i < map->num_mappings; i++)
244  {
245  if (filenumber == map->mappings[i].mapfilenumber)
246  return map->mappings[i].mapoid;
247  }
248  map = &local_map;
249  for (i = 0; i < map->num_mappings; i++)
250  {
251  if (filenumber == map->mappings[i].mapfilenumber)
252  return map->mappings[i].mapoid;
253  }
254  }
255 
256  return InvalidOid;
257 }
signed int int32
Definition: c.h:483
int i
Definition: isn.c:73
#define InvalidOid
Definition: postgres_ext.h:36
static RelMapFile shared_map
Definition: relmapper.c:113
static RelMapFile local_map
Definition: relmapper.c:114
RelMapping mappings[MAX_MAPPINGS]
Definition: relmapper.c:94
Oid mapoid
Definition: relmapper.c:86
RelFileNumber mapfilenumber
Definition: relmapper.c:87

References active_local_updates, active_shared_updates, i, InvalidOid, local_map, RelMapping::mapfilenumber, RelMapping::mapoid, RelMapFile::mappings, RelMapFile::num_mappings, and shared_map.

Referenced by RelidByRelfilenumber().

◆ RelationMapFinishBootstrap()

void RelationMapFinishBootstrap ( void  )

Definition at line 625 of file relmapper.c.

626 {
628 
629  /* Shouldn't be anything "pending" ... */
634 
635  /* Write the files; no WAL or sinval needed */
636  write_relmap_file(&shared_map, false, false, false,
637  InvalidOid, GLOBALTABLESPACE_OID, "global");
638  write_relmap_file(&local_map, false, false, false,
640 }
Oid MyDatabaseTableSpace
Definition: globals.c:91
char * DatabasePath
Definition: globals.c:97
Oid MyDatabaseId
Definition: globals.c:89
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:414

References active_local_updates, active_shared_updates, Assert(), DatabasePath, InvalidOid, IsBootstrapProcessingMode, local_map, MyDatabaseId, MyDatabaseTableSpace, RelMapFile::num_mappings, pending_local_updates, pending_shared_updates, shared_map, and write_relmap_file().

Referenced by BootstrapModeMain().

◆ RelationMapInitialize()

void RelationMapInitialize ( void  )

Definition at line 649 of file relmapper.c.

650 {
651  /* The static variables should initialize to zeroes, but let's be sure */
652  shared_map.magic = 0; /* mark it not loaded */
653  local_map.magic = 0;
660 }
int32 magic
Definition: relmapper.c:92

References active_local_updates, active_shared_updates, local_map, RelMapFile::magic, RelMapFile::num_mappings, pending_local_updates, pending_shared_updates, and shared_map.

Referenced by RelationCacheInitialize().

◆ RelationMapInitializePhase2()

void RelationMapInitializePhase2 ( void  )

Definition at line 669 of file relmapper.c.

670 {
671  /*
672  * In bootstrap mode, the map file isn't there yet, so do nothing.
673  */
675  return;
676 
677  /*
678  * Load the shared map file, die on error.
679  */
680  load_relmap_file(true, false);
681 }
static void load_relmap_file(bool shared, bool lock_held)
Definition: relmapper.c:763

References IsBootstrapProcessingMode, and load_relmap_file().

Referenced by RelationCacheInitializePhase2().

◆ RelationMapInitializePhase3()

void RelationMapInitializePhase3 ( void  )

Definition at line 690 of file relmapper.c.

691 {
692  /*
693  * In bootstrap mode, the map file isn't there yet, so do nothing.
694  */
696  return;
697 
698  /*
699  * Load the local map file, die on error.
700  */
701  load_relmap_file(false, false);
702 }

References IsBootstrapProcessingMode, and load_relmap_file().

Referenced by RelationCacheInitializePhase3().

◆ RelationMapInvalidate()

void RelationMapInvalidate ( bool  shared)

Definition at line 468 of file relmapper.c.

469 {
470  if (shared)
471  {
473  load_relmap_file(true, false);
474  }
475  else
476  {
478  load_relmap_file(false, false);
479  }
480 }
#define RELMAPPER_FILEMAGIC
Definition: relmapper.c:74

References load_relmap_file(), local_map, RelMapFile::magic, RELMAPPER_FILEMAGIC, and shared_map.

Referenced by LocalExecuteInvalidationMessage().

◆ RelationMapInvalidateAll()

void RelationMapInvalidateAll ( void  )

Definition at line 490 of file relmapper.c.

491 {
493  load_relmap_file(true, false);
495  load_relmap_file(false, false);
496 }

References load_relmap_file(), local_map, RelMapFile::magic, RELMAPPER_FILEMAGIC, and shared_map.

Referenced by RelationCacheInvalidate().

◆ RelationMapOidToFilenumber()

RelFileNumber RelationMapOidToFilenumber ( Oid  relationId,
bool  shared 
)

Definition at line 166 of file relmapper.c.

167 {
168  const RelMapFile *map;
169  int32 i;
170 
171  /* If there are active updates, believe those over the main maps */
172  if (shared)
173  {
174  map = &active_shared_updates;
175  for (i = 0; i < map->num_mappings; i++)
176  {
177  if (relationId == map->mappings[i].mapoid)
178  return map->mappings[i].mapfilenumber;
179  }
180  map = &shared_map;
181  for (i = 0; i < map->num_mappings; i++)
182  {
183  if (relationId == map->mappings[i].mapoid)
184  return map->mappings[i].mapfilenumber;
185  }
186  }
187  else
188  {
189  map = &active_local_updates;
190  for (i = 0; i < map->num_mappings; i++)
191  {
192  if (relationId == map->mappings[i].mapoid)
193  return map->mappings[i].mapfilenumber;
194  }
195  map = &local_map;
196  for (i = 0; i < map->num_mappings; i++)
197  {
198  if (relationId == map->mappings[i].mapoid)
199  return map->mappings[i].mapfilenumber;
200  }
201  }
202 
203  return InvalidRelFileNumber;
204 }
#define InvalidRelFileNumber
Definition: relpath.h:26

References active_local_updates, active_shared_updates, i, InvalidRelFileNumber, local_map, RelMapping::mapfilenumber, RelMapping::mapoid, RelMapFile::mappings, RelMapFile::num_mappings, and shared_map.

Referenced by pg_relation_filenode(), pg_relation_filepath(), RelationInitPhysicalAddr(), and swap_relation_files().

◆ RelationMapOidToFilenumberForDatabase()

RelFileNumber RelationMapOidToFilenumberForDatabase ( char *  dbpath,
Oid  relationId 
)

Definition at line 266 of file relmapper.c.

267 {
268  RelMapFile map;
269  int i;
270 
271  /* Read the relmap file from the source database. */
272  read_relmap_file(&map, dbpath, false, ERROR);
273 
274  /* Iterate over the relmap entries to find the input relation OID. */
275  for (i = 0; i < map.num_mappings; i++)
276  {
277  if (relationId == map.mappings[i].mapoid)
278  return map.mappings[i].mapfilenumber;
279  }
280 
281  return InvalidRelFileNumber;
282 }

References ERROR, i, InvalidRelFileNumber, RelMapping::mapfilenumber, RelMapping::mapoid, RelMapFile::mappings, RelMapFile::num_mappings, and read_relmap_file().

Referenced by ScanSourceDatabasePgClass(), and ScanSourceDatabasePgClassTuple().

◆ RelationMapRemoveMapping()

void RelationMapRemoveMapping ( Oid  relationId)

Definition at line 438 of file relmapper.c.

439 {
441  int32 i;
442 
443  for (i = 0; i < map->num_mappings; i++)
444  {
445  if (relationId == map->mappings[i].mapoid)
446  {
447  /* Found it, collapse it out */
448  map->mappings[i] = map->mappings[map->num_mappings - 1];
449  map->num_mappings--;
450  return;
451  }
452  }
453  elog(ERROR, "could not find temporary mapping for relation %u",
454  relationId);
455 }

References active_local_updates, elog(), ERROR, i, RelMapping::mapoid, RelMapFile::mappings, and RelMapFile::num_mappings.

Referenced by finish_heap_swap().

◆ RelationMapUpdateMap()

void RelationMapUpdateMap ( Oid  relationId,
RelFileNumber  fileNumber,
bool  shared,
bool  immediate 
)

Definition at line 325 of file relmapper.c.

327 {
328  RelMapFile *map;
329 
331  {
332  /*
333  * In bootstrap mode, the mapping gets installed in permanent map.
334  */
335  if (shared)
336  map = &shared_map;
337  else
338  map = &local_map;
339  }
340  else
341  {
342  /*
343  * We don't currently support map changes within subtransactions, or
344  * when in parallel mode. This could be done with more bookkeeping
345  * infrastructure, but it doesn't presently seem worth it.
346  */
348  elog(ERROR, "cannot change relation mapping within subtransaction");
349 
350  if (IsInParallelMode())
351  elog(ERROR, "cannot change relation mapping in parallel mode");
352 
353  if (immediate)
354  {
355  /* Make it active, but only locally */
356  if (shared)
357  map = &active_shared_updates;
358  else
359  map = &active_local_updates;
360  }
361  else
362  {
363  /* Make it pending */
364  if (shared)
365  map = &pending_shared_updates;
366  else
367  map = &pending_local_updates;
368  }
369  }
370  apply_map_update(map, relationId, fileNumber, true);
371 }
static void apply_map_update(RelMapFile *map, Oid relationId, RelFileNumber fileNumber, bool add_okay)
Definition: relmapper.c:383
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:914
bool IsInParallelMode(void)
Definition: xact.c:1069

References active_local_updates, active_shared_updates, apply_map_update(), elog(), ERROR, GetCurrentTransactionNestLevel(), IsBootstrapProcessingMode, IsInParallelMode(), local_map, pending_local_updates, pending_shared_updates, and shared_map.

Referenced by formrdesc(), RelationBuildLocalRelation(), RelationSetNewRelfilenumber(), and swap_relation_files().

◆ relmap_desc()

void relmap_desc ( StringInfo  buf,
XLogReaderState record 
)

Definition at line 20 of file relmapdesc.c.

21 {
22  char *rec = XLogRecGetData(record);
23  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
24 
25  if (info == XLOG_RELMAP_UPDATE)
26  {
27  xl_relmap_update *xlrec = (xl_relmap_update *) rec;
28 
29  appendStringInfo(buf, "database %u tablespace %u size %d",
30  xlrec->dbid, xlrec->tsid, xlrec->nbytes);
31  }
32 }
unsigned char uint8
Definition: c.h:493
static char * buf
Definition: pg_test_fsync.c:67
#define XLOG_RELMAP_UPDATE
Definition: relmapper.h:25
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:410
#define XLogRecGetData(decoder)
Definition: xlogreader.h:415
#define XLR_INFO_MASK
Definition: xlogrecord.h:62

References appendStringInfo(), buf, xl_relmap_update::dbid, xl_relmap_update::nbytes, xl_relmap_update::tsid, XLOG_RELMAP_UPDATE, XLogRecGetData, XLogRecGetInfo, and XLR_INFO_MASK.

◆ relmap_identify()

const char* relmap_identify ( uint8  info)

Definition at line 35 of file relmapdesc.c.

36 {
37  const char *id = NULL;
38 
39  switch (info & ~XLR_INFO_MASK)
40  {
41  case XLOG_RELMAP_UPDATE:
42  id = "UPDATE";
43  break;
44  }
45 
46  return id;
47 }

References XLOG_RELMAP_UPDATE, and XLR_INFO_MASK.

◆ relmap_redo()

void relmap_redo ( XLogReaderState record)

Definition at line 1085 of file relmapper.c.

1086 {
1087  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
1088 
1089  /* Backup blocks are not used in relmap records */
1090  Assert(!XLogRecHasAnyBlockRefs(record));
1091 
1092  if (info == XLOG_RELMAP_UPDATE)
1093  {
1094  xl_relmap_update *xlrec = (xl_relmap_update *) XLogRecGetData(record);
1095  RelMapFile newmap;
1096  char *dbpath;
1097 
1098  if (xlrec->nbytes != sizeof(RelMapFile))
1099  elog(PANIC, "relmap_redo: wrong size %u in relmap update record",
1100  xlrec->nbytes);
1101  memcpy(&newmap, xlrec->data, sizeof(newmap));
1102 
1103  /* We need to construct the pathname for this database */
1104  dbpath = GetDatabasePath(xlrec->dbid, xlrec->tsid);
1105 
1106  /*
1107  * Write out the new map and send sinval, but of course don't write a
1108  * new WAL entry. There's no surrounding transaction to tell to
1109  * preserve files, either.
1110  *
1111  * There shouldn't be anyone else updating relmaps during WAL replay,
1112  * but grab the lock to interlock against load_relmap_file().
1113  *
1114  * Note that we use the same WAL record for updating the relmap of an
1115  * existing database as we do for creating a new database. In the
1116  * latter case, taking the relmap log and sending sinval messages is
1117  * unnecessary, but harmless. If we wanted to avoid it, we could add a
1118  * flag to the WAL record to indicate which operation is being
1119  * performed.
1120  */
1121  LWLockAcquire(RelationMappingLock, LW_EXCLUSIVE);
1122  write_relmap_file(&newmap, false, true, false,
1123  xlrec->dbid, xlrec->tsid, dbpath);
1124  LWLockRelease(RelationMappingLock);
1125 
1126  pfree(dbpath);
1127  }
1128  else
1129  elog(PANIC, "relmap_redo: unknown op code %u", info);
1130 }
#define PANIC
Definition: elog.h:42
@ LW_EXCLUSIVE
Definition: lwlock.h:116
void pfree(void *pointer)
Definition: mcxt.c:1456
char * GetDatabasePath(Oid dbOid, Oid spcOid)
Definition: relpath.c:110
char data[FLEXIBLE_ARRAY_MEMBER]
Definition: relmapper.h:32
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:417

References Assert(), xl_relmap_update::data, xl_relmap_update::dbid, elog(), GetDatabasePath(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), xl_relmap_update::nbytes, PANIC, pfree(), xl_relmap_update::tsid, write_relmap_file(), XLOG_RELMAP_UPDATE, XLogRecGetData, XLogRecGetInfo, XLogRecHasAnyBlockRefs, and XLR_INFO_MASK.

◆ RestoreRelationMap()

void RestoreRelationMap ( char *  startAddress)

◆ SerializeRelationMap()

void SerializeRelationMap ( Size  maxSize,
char *  startAddress 
)