PostgreSQL Source Code  git master
relmapper.c File Reference
#include "postgres.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include "access/xact.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
#include "catalog/catalog.h"
#include "catalog/pg_tablespace.h"
#include "catalog/storage.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "storage/fd.h"
#include "storage/lwlock.h"
#include "utils/inval.h"
#include "utils/relmapper.h"
Include dependency graph for relmapper.c:

Go to the source code of this file.

Data Structures

struct  RelMapping
 
struct  RelMapFile
 
struct  SerializedActiveRelMaps
 

Macros

#define RELMAPPER_FILENAME   "pg_filenode.map"
 
#define RELMAPPER_FILEMAGIC   0x592717 /* version ID value */
 
#define MAX_MAPPINGS   62 /* 62 * 8 + 16 = 512 */
 

Typedefs

typedef struct RelMapping RelMapping
 
typedef struct RelMapFile RelMapFile
 
typedef struct SerializedActiveRelMaps SerializedActiveRelMaps
 

Functions

static void apply_map_update (RelMapFile *map, Oid relationId, Oid fileNode, bool add_okay)
 
static void merge_map_updates (RelMapFile *map, const RelMapFile *updates, bool add_okay)
 
static void load_relmap_file (bool shared, bool lock_held)
 
static void read_relmap_file (RelMapFile *map, char *dbpath, bool lock_held, int elevel)
 
static void write_relmap_file (RelMapFile *newmap, bool write_wal, bool send_sinval, bool preserve_files, Oid dbid, Oid tsid, const char *dbpath)
 
static void perform_relmap_update (bool shared, const RelMapFile *updates)
 
Oid RelationMapOidToFilenode (Oid relationId, bool shared)
 
Oid RelationMapFilenodeToOid (Oid filenode, bool shared)
 
Oid RelationMapOidToFilenodeForDatabase (char *dbpath, Oid relationId)
 
void RelationMapCopy (Oid dbid, Oid tsid, char *srcdbpath, char *dstdbpath)
 
void RelationMapUpdateMap (Oid relationId, Oid fileNode, 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)
 

Variables

static RelMapFile shared_map
 
static RelMapFile local_map
 
static RelMapFile active_shared_updates
 
static RelMapFile active_local_updates
 
static RelMapFile pending_shared_updates
 
static RelMapFile pending_local_updates
 

Macro Definition Documentation

◆ MAX_MAPPINGS

#define MAX_MAPPINGS   62 /* 62 * 8 + 16 = 512 */

Definition at line 77 of file relmapper.c.

◆ RELMAPPER_FILEMAGIC

#define RELMAPPER_FILEMAGIC   0x592717 /* version ID value */

Definition at line 75 of file relmapper.c.

◆ RELMAPPER_FILENAME

#define RELMAPPER_FILENAME   "pg_filenode.map"

Definition at line 73 of file relmapper.c.

Typedef Documentation

◆ RelMapFile

typedef struct RelMapFile RelMapFile

◆ RelMapping

typedef struct RelMapping RelMapping

◆ SerializedActiveRelMaps

Function Documentation

◆ apply_map_update()

static void apply_map_update ( RelMapFile map,
Oid  relationId,
Oid  fileNode,
bool  add_okay 
)
static

Definition at line 378 of file relmapper.c.

379 {
380  int32 i;
381 
382  /* Replace any existing mapping */
383  for (i = 0; i < map->num_mappings; i++)
384  {
385  if (relationId == map->mappings[i].mapoid)
386  {
387  map->mappings[i].mapfilenode = fileNode;
388  return;
389  }
390  }
391 
392  /* Nope, need to add a new mapping */
393  if (!add_okay)
394  elog(ERROR, "attempt to apply a mapping to unmapped relation %u",
395  relationId);
396  if (map->num_mappings >= MAX_MAPPINGS)
397  elog(ERROR, "ran out of space in relation map");
398  map->mappings[map->num_mappings].mapoid = relationId;
399  map->mappings[map->num_mappings].mapfilenode = fileNode;
400  map->num_mappings++;
401 }
signed int int32
Definition: c.h:429
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
int i
Definition: isn.c:73
#define MAX_MAPPINGS
Definition: relmapper.c:77
RelMapping mappings[MAX_MAPPINGS]
Definition: relmapper.c:89
int32 num_mappings
Definition: relmapper.c:88
Oid mapoid
Definition: relmapper.c:81
Oid mapfilenode
Definition: relmapper.c:82

References elog, ERROR, i, RelMapping::mapfilenode, RelMapping::mapoid, RelMapFile::mappings, MAX_MAPPINGS, and RelMapFile::num_mappings.

Referenced by merge_map_updates(), and RelationMapUpdateMap().

◆ AtCCI_RelationMap()

void AtCCI_RelationMap ( void  )

Definition at line 498 of file relmapper.c.

499 {
501  {
504  true);
506  }
508  {
511  true);
513  }
514 }
static RelMapFile pending_local_updates
Definition: relmapper.c:131
static RelMapFile active_local_updates
Definition: relmapper.c:129
static RelMapFile pending_shared_updates
Definition: relmapper.c:130
static void merge_map_updates(RelMapFile *map, const RelMapFile *updates, bool add_okay)
Definition: relmapper.c:410
static RelMapFile active_shared_updates
Definition: relmapper.c:128

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 535 of file relmapper.c.

536 {
537  if (isCommit && !isParallelWorker)
538  {
539  /*
540  * We should not get here with any "pending" updates. (We could
541  * logically choose to treat such as committed, but in the current
542  * code this should never happen.)
543  */
546 
547  /*
548  * Write any active updates to the actual map files, then reset them.
549  */
551  {
554  }
556  {
559  }
560  }
561  else
562  {
563  /* Abort or parallel worker --- drop all local and pending updates */
564  Assert(!isParallelWorker || pending_shared_updates.num_mappings == 0);
565  Assert(!isParallelWorker || pending_local_updates.num_mappings == 0);
566 
571  }
572 }
Assert(fmt[strlen(fmt) - 1] !='\n')
static void perform_relmap_update(bool shared, const RelMapFile *updates)
Definition: relmapper.c:1006

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 582 of file relmapper.c.

583 {
588  ereport(ERROR,
589  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
590  errmsg("cannot PREPARE a transaction that modified relation mapping")));
591 }
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ereport(elevel,...)
Definition: elog.h:143

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 605 of file relmapper.c.

606 {
607  LWLockAcquire(RelationMappingLock, LW_SHARED);
608  LWLockRelease(RelationMappingLock);
609 }
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1196
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1800
@ LW_SHARED
Definition: lwlock.h:105

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

Referenced by CheckPointGuts().

◆ EstimateRelationMapSpace()

Size EstimateRelationMapSpace ( void  )

Definition at line 705 of file relmapper.c.

706 {
707  return sizeof(SerializedActiveRelMaps);
708 }
struct SerializedActiveRelMaps SerializedActiveRelMaps

Referenced by InitializeParallelDSM(), and SerializeRelationMap().

◆ load_relmap_file()

static void load_relmap_file ( bool  shared,
bool  lock_held 
)
static

Definition at line 757 of file relmapper.c.

758 {
759  if (shared)
760  read_relmap_file(&shared_map, "global", lock_held, FATAL);
761  else
763 }
#define FATAL
Definition: elog.h:35
char * DatabasePath
Definition: globals.c:97
static RelMapFile shared_map
Definition: relmapper.c:109
static void read_relmap_file(RelMapFile *map, char *dbpath, bool lock_held, int elevel)
Definition: relmapper.c:776
static RelMapFile local_map
Definition: relmapper.c:110

References DatabasePath, FATAL, local_map, read_relmap_file(), and shared_map.

Referenced by perform_relmap_update(), RelationMapInitializePhase2(), RelationMapInitializePhase3(), RelationMapInvalidate(), and RelationMapInvalidateAll().

◆ merge_map_updates()

static void merge_map_updates ( RelMapFile map,
const RelMapFile updates,
bool  add_okay 
)
static

Definition at line 410 of file relmapper.c.

411 {
412  int32 i;
413 
414  for (i = 0; i < updates->num_mappings; i++)
415  {
416  apply_map_update(map,
417  updates->mappings[i].mapoid,
418  updates->mappings[i].mapfilenode,
419  add_okay);
420  }
421 }
static void apply_map_update(RelMapFile *map, Oid relationId, Oid fileNode, bool add_okay)
Definition: relmapper.c:378

References apply_map_update(), i, RelMapping::mapfilenode, RelMapping::mapoid, RelMapFile::mappings, and RelMapFile::num_mappings.

Referenced by AtCCI_RelationMap(), and perform_relmap_update().

◆ perform_relmap_update()

static void perform_relmap_update ( bool  shared,
const RelMapFile updates 
)
static

Definition at line 1006 of file relmapper.c.

1007 {
1008  RelMapFile newmap;
1009 
1010  /*
1011  * Anyone updating a relation's mapping info should take exclusive lock on
1012  * that rel and hold it until commit. This ensures that there will not be
1013  * concurrent updates on the same mapping value; but there could easily be
1014  * concurrent updates on different values in the same file. We cover that
1015  * by acquiring the RelationMappingLock, re-reading the target file to
1016  * ensure it's up to date, applying the updates, and writing the data
1017  * before releasing RelationMappingLock.
1018  *
1019  * There is only one RelationMappingLock. In principle we could try to
1020  * have one per mapping file, but it seems unlikely to be worth the
1021  * trouble.
1022  */
1023  LWLockAcquire(RelationMappingLock, LW_EXCLUSIVE);
1024 
1025  /* Be certain we see any other updates just made */
1026  load_relmap_file(shared, true);
1027 
1028  /* Prepare updated data in a local variable */
1029  if (shared)
1030  memcpy(&newmap, &shared_map, sizeof(RelMapFile));
1031  else
1032  memcpy(&newmap, &local_map, sizeof(RelMapFile));
1033 
1034  /*
1035  * Apply the updates to newmap. No new mappings should appear, unless
1036  * somebody is adding indexes to system catalogs.
1037  */
1038  merge_map_updates(&newmap, updates, allowSystemTableMods);
1039 
1040  /* Write out the updated map and do other necessary tasks */
1041  write_relmap_file(&newmap, true, true, true,
1042  (shared ? InvalidOid : MyDatabaseId),
1043  (shared ? GLOBALTABLESPACE_OID : MyDatabaseTableSpace),
1044  (shared ? "global" : DatabasePath));
1045 
1046  /*
1047  * We successfully wrote the updated file, so it's now safe to rely on the
1048  * new values in this process, too.
1049  */
1050  if (shared)
1051  memcpy(&shared_map, &newmap, sizeof(RelMapFile));
1052  else
1053  memcpy(&local_map, &newmap, sizeof(RelMapFile));
1054 
1055  /* Now we can release the lock */
1056  LWLockRelease(RelationMappingLock);
1057 }
bool allowSystemTableMods
Definition: globals.c:124
Oid MyDatabaseTableSpace
Definition: globals.c:91
Oid MyDatabaseId
Definition: globals.c:89
@ LW_EXCLUSIVE
Definition: lwlock.h:104
#define InvalidOid
Definition: postgres_ext.h:36
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:873
static void load_relmap_file(bool shared, bool lock_held)
Definition: relmapper.c:757

References allowSystemTableMods, DatabasePath, InvalidOid, load_relmap_file(), local_map, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), merge_map_updates(), MyDatabaseId, MyDatabaseTableSpace, shared_map, and write_relmap_file().

Referenced by AtEOXact_RelationMap().

◆ read_relmap_file()

static void read_relmap_file ( RelMapFile map,
char *  dbpath,
bool  lock_held,
int  elevel 
)
static

Definition at line 776 of file relmapper.c.

777 {
778  char mapfilename[MAXPGPATH];
779  pg_crc32c crc;
780  int fd;
781  int r;
782 
783  Assert(elevel >= ERROR);
784 
785  /* Open the target file. */
786  snprintf(mapfilename, sizeof(mapfilename), "%s/%s", dbpath,
788  fd = OpenTransientFile(mapfilename, O_RDONLY | PG_BINARY);
789  if (fd < 0)
790  ereport(elevel,
792  errmsg("could not open file \"%s\": %m",
793  mapfilename)));
794 
795  /*
796  * Grab the lock to prevent the file from being updated while we read it,
797  * unless the caller is already holding the lock. If the file is updated
798  * shortly after we look, the sinval signaling mechanism will make us
799  * re-read it before we are able to access any relation that's affected by
800  * the change.
801  */
802  if (!lock_held)
803  LWLockAcquire(RelationMappingLock, LW_SHARED);
804 
805  /* Now read the data. */
807  r = read(fd, map, sizeof(RelMapFile));
808  if (r != sizeof(RelMapFile))
809  {
810  if (r < 0)
811  ereport(elevel,
813  errmsg("could not read file \"%s\": %m", mapfilename)));
814  else
815  ereport(elevel,
817  errmsg("could not read file \"%s\": read %d of %zu",
818  mapfilename, r, sizeof(RelMapFile))));
819  }
821 
822  if (!lock_held)
823  LWLockRelease(RelationMappingLock);
824 
825  if (CloseTransientFile(fd) != 0)
826  ereport(elevel,
828  errmsg("could not close file \"%s\": %m",
829  mapfilename)));
830 
831  /* check for correct magic number, etc */
832  if (map->magic != RELMAPPER_FILEMAGIC ||
833  map->num_mappings < 0 ||
834  map->num_mappings > MAX_MAPPINGS)
835  ereport(elevel,
836  (errmsg("relation mapping file \"%s\" contains invalid data",
837  mapfilename)));
838 
839  /* verify the CRC */
840  INIT_CRC32C(crc);
841  COMP_CRC32C(crc, (char *) map, offsetof(RelMapFile, crc));
842  FIN_CRC32C(crc);
843 
844  if (!EQ_CRC32C(crc, map->crc))
845  ereport(elevel,
846  (errmsg("relation mapping file \"%s\" contains incorrect checksum",
847  mapfilename)));
848 }
#define offsetof(type, field)
Definition: c.h:727
#define PG_BINARY
Definition: c.h:1268
int errcode_for_file_access(void)
Definition: elog.c:716
int CloseTransientFile(int fd)
Definition: fd.c:2688
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2511
#define read(a, b, c)
Definition: win32.h:13
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:43
#define MAXPGPATH
uint32 pg_crc32c
Definition: pg_crc32c.h:38
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:89
#define EQ_CRC32C(c1, c2)
Definition: pg_crc32c.h:42
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
#define FIN_CRC32C(crc)
Definition: pg_crc32c.h:94
return crc
#define snprintf
Definition: port.h:225
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define RELMAPPER_FILEMAGIC
Definition: relmapper.c:75
#define RELMAPPER_FILENAME
Definition: relmapper.c:73
int32 magic
Definition: relmapper.c:87
@ WAIT_EVENT_RELATION_MAP_READ
Definition: wait_event.h:195
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: wait_event.h:266
static void pgstat_report_wait_end(void)
Definition: wait_event.h:282

References Assert(), CloseTransientFile(), COMP_CRC32C, RelMapFile::crc, crc, EQ_CRC32C, ereport, errcode(), ERRCODE_DATA_CORRUPTED, errcode_for_file_access(), errmsg(), ERROR, fd(), FIN_CRC32C, INIT_CRC32C, LW_SHARED, LWLockAcquire(), LWLockRelease(), RelMapFile::magic, MAX_MAPPINGS, MAXPGPATH, RelMapFile::num_mappings, offsetof, OpenTransientFile(), PG_BINARY, pgstat_report_wait_end(), pgstat_report_wait_start(), read, RELMAPPER_FILEMAGIC, RELMAPPER_FILENAME, snprintf, and WAIT_EVENT_RELATION_MAP_READ.

Referenced by load_relmap_file(), RelationMapCopy(), and RelationMapOidToFilenodeForDatabase().

◆ RelationMapCopy()

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

Definition at line 288 of file relmapper.c.

289 {
290  RelMapFile map;
291 
292  /*
293  * Read the relmap file from the source database.
294  */
295  read_relmap_file(&map, srcdbpath, false, ERROR);
296 
297  /*
298  * Write the same data into the destination database's relmap file.
299  *
300  * No sinval is needed because no one can be connected to the destination
301  * database yet. For the same reason, there is no need to acquire
302  * RelationMappingLock.
303  *
304  * There's no point in trying to preserve files here. The new database
305  * isn't usable yet anyway, and won't ever be if we can't install a relmap
306  * file.
307  */
308  write_relmap_file(&map, true, false, false, dbid, tsid, dstdbpath);
309 }

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

Referenced by CreateDatabaseUsingWalLog().

◆ RelationMapFilenodeToOid()

Oid RelationMapFilenodeToOid ( Oid  filenode,
bool  shared 
)

Definition at line 214 of file relmapper.c.

215 {
216  const RelMapFile *map;
217  int32 i;
218 
219  /* If there are active updates, believe those over the main maps */
220  if (shared)
221  {
222  map = &active_shared_updates;
223  for (i = 0; i < map->num_mappings; i++)
224  {
225  if (filenode == map->mappings[i].mapfilenode)
226  return map->mappings[i].mapoid;
227  }
228  map = &shared_map;
229  for (i = 0; i < map->num_mappings; i++)
230  {
231  if (filenode == map->mappings[i].mapfilenode)
232  return map->mappings[i].mapoid;
233  }
234  }
235  else
236  {
237  map = &active_local_updates;
238  for (i = 0; i < map->num_mappings; i++)
239  {
240  if (filenode == map->mappings[i].mapfilenode)
241  return map->mappings[i].mapoid;
242  }
243  map = &local_map;
244  for (i = 0; i < map->num_mappings; i++)
245  {
246  if (filenode == map->mappings[i].mapfilenode)
247  return map->mappings[i].mapoid;
248  }
249  }
250 
251  return InvalidOid;
252 }

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

Referenced by RelidByRelfilenode().

◆ RelationMapFinishBootstrap()

void RelationMapFinishBootstrap ( void  )

Definition at line 619 of file relmapper.c.

620 {
622 
623  /* Shouldn't be anything "pending" ... */
628 
629  /* Write the files; no WAL or sinval needed */
630  write_relmap_file(&shared_map, false, false, false,
631  InvalidOid, GLOBALTABLESPACE_OID, "global");
632  write_relmap_file(&local_map, false, false, false,
634 }
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:406

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 643 of file relmapper.c.

644 {
645  /* The static variables should initialize to zeroes, but let's be sure */
646  shared_map.magic = 0; /* mark it not loaded */
647  local_map.magic = 0;
654 }

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 663 of file relmapper.c.

664 {
665  /*
666  * In bootstrap mode, the map file isn't there yet, so do nothing.
667  */
669  return;
670 
671  /*
672  * Load the shared map file, die on error.
673  */
674  load_relmap_file(true, false);
675 }

References IsBootstrapProcessingMode, and load_relmap_file().

Referenced by RelationCacheInitializePhase2().

◆ RelationMapInitializePhase3()

void RelationMapInitializePhase3 ( void  )

Definition at line 684 of file relmapper.c.

685 {
686  /*
687  * In bootstrap mode, the map file isn't there yet, so do nothing.
688  */
690  return;
691 
692  /*
693  * Load the local map file, die on error.
694  */
695  load_relmap_file(false, false);
696 }

References IsBootstrapProcessingMode, and load_relmap_file().

Referenced by RelationCacheInitializePhase3().

◆ RelationMapInvalidate()

void RelationMapInvalidate ( bool  shared)

Definition at line 462 of file relmapper.c.

463 {
464  if (shared)
465  {
467  load_relmap_file(true, false);
468  }
469  else
470  {
472  load_relmap_file(false, false);
473  }
474 }

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

Referenced by LocalExecuteInvalidationMessage().

◆ RelationMapInvalidateAll()

void RelationMapInvalidateAll ( void  )

Definition at line 484 of file relmapper.c.

485 {
487  load_relmap_file(true, false);
489  load_relmap_file(false, false);
490 }

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

Referenced by RelationCacheInvalidate().

◆ RelationMapOidToFilenode()

Oid RelationMapOidToFilenode ( Oid  relationId,
bool  shared 
)

Definition at line 161 of file relmapper.c.

162 {
163  const RelMapFile *map;
164  int32 i;
165 
166  /* If there are active updates, believe those over the main maps */
167  if (shared)
168  {
169  map = &active_shared_updates;
170  for (i = 0; i < map->num_mappings; i++)
171  {
172  if (relationId == map->mappings[i].mapoid)
173  return map->mappings[i].mapfilenode;
174  }
175  map = &shared_map;
176  for (i = 0; i < map->num_mappings; i++)
177  {
178  if (relationId == map->mappings[i].mapoid)
179  return map->mappings[i].mapfilenode;
180  }
181  }
182  else
183  {
184  map = &active_local_updates;
185  for (i = 0; i < map->num_mappings; i++)
186  {
187  if (relationId == map->mappings[i].mapoid)
188  return map->mappings[i].mapfilenode;
189  }
190  map = &local_map;
191  for (i = 0; i < map->num_mappings; i++)
192  {
193  if (relationId == map->mappings[i].mapoid)
194  return map->mappings[i].mapfilenode;
195  }
196  }
197 
198  return InvalidOid;
199 }

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

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

◆ RelationMapOidToFilenodeForDatabase()

Oid RelationMapOidToFilenodeForDatabase ( char *  dbpath,
Oid  relationId 
)

Definition at line 261 of file relmapper.c.

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

References ERROR, i, InvalidOid, RelMapping::mapfilenode, RelMapping::mapoid, RelMapFile::mappings, RelMapFile::num_mappings, and read_relmap_file().

Referenced by ScanSourceDatabasePgClass(), and ScanSourceDatabasePgClassTuple().

◆ RelationMapRemoveMapping()

void RelationMapRemoveMapping ( Oid  relationId)

Definition at line 432 of file relmapper.c.

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

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,
Oid  fileNode,
bool  shared,
bool  immediate 
)

Definition at line 320 of file relmapper.c.

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

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(), RelationSetNewRelfilenode(), and swap_relation_files().

◆ relmap_redo()

void relmap_redo ( XLogReaderState record)

Definition at line 1063 of file relmapper.c.

1064 {
1065  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
1066 
1067  /* Backup blocks are not used in relmap records */
1068  Assert(!XLogRecHasAnyBlockRefs(record));
1069 
1070  if (info == XLOG_RELMAP_UPDATE)
1071  {
1072  xl_relmap_update *xlrec = (xl_relmap_update *) XLogRecGetData(record);
1073  RelMapFile newmap;
1074  char *dbpath;
1075 
1076  if (xlrec->nbytes != sizeof(RelMapFile))
1077  elog(PANIC, "relmap_redo: wrong size %u in relmap update record",
1078  xlrec->nbytes);
1079  memcpy(&newmap, xlrec->data, sizeof(newmap));
1080 
1081  /* We need to construct the pathname for this database */
1082  dbpath = GetDatabasePath(xlrec->dbid, xlrec->tsid);
1083 
1084  /*
1085  * Write out the new map and send sinval, but of course don't write a
1086  * new WAL entry. There's no surrounding transaction to tell to
1087  * preserve files, either.
1088  *
1089  * There shouldn't be anyone else updating relmaps during WAL replay,
1090  * but grab the lock to interlock against load_relmap_file().
1091  *
1092  * Note that we use the same WAL record for updating the relmap of an
1093  * existing database as we do for creating a new database. In the
1094  * latter case, taking the relmap log and sending sinval messages is
1095  * unnecessary, but harmless. If we wanted to avoid it, we could add a
1096  * flag to the WAL record to indicate which operation is being
1097  * performed.
1098  */
1099  LWLockAcquire(RelationMappingLock, LW_EXCLUSIVE);
1100  write_relmap_file(&newmap, false, true, false,
1101  xlrec->dbid, xlrec->tsid, dbpath);
1102  LWLockRelease(RelationMappingLock);
1103 
1104  pfree(dbpath);
1105  }
1106  else
1107  elog(PANIC, "relmap_redo: unknown op code %u", info);
1108 }
unsigned char uint8
Definition: c.h:439
#define PANIC
Definition: elog.h:36
void pfree(void *pointer)
Definition: mcxt.c:1175
#define XLOG_RELMAP_UPDATE
Definition: relmapper.h:25
char * GetDatabasePath(Oid dbNode, Oid spcNode)
Definition: relpath.c:110
char data[FLEXIBLE_ARRAY_MEMBER]
Definition: relmapper.h:32
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:408
#define XLogRecGetData(decoder)
Definition: xlogreader.h:413
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:415
#define XLR_INFO_MASK
Definition: xlogrecord.h:62

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 
)

◆ write_relmap_file()

static void write_relmap_file ( RelMapFile newmap,
bool  write_wal,
bool  send_sinval,
bool  preserve_files,
Oid  dbid,
Oid  tsid,
const char *  dbpath 
)
static

Definition at line 873 of file relmapper.c.

875 {
876  int fd;
877  char mapfilename[MAXPGPATH];
878 
879  /*
880  * Fill in the overhead fields and update CRC.
881  */
882  newmap->magic = RELMAPPER_FILEMAGIC;
883  if (newmap->num_mappings < 0 || newmap->num_mappings > MAX_MAPPINGS)
884  elog(ERROR, "attempt to write bogus relation mapping");
885 
886  INIT_CRC32C(newmap->crc);
887  COMP_CRC32C(newmap->crc, (char *) newmap, offsetof(RelMapFile, crc));
888  FIN_CRC32C(newmap->crc);
889 
890  /*
891  * Open the target file. We prefer to do this before entering the
892  * critical section, so that an open() failure need not force PANIC.
893  */
894  snprintf(mapfilename, sizeof(mapfilename), "%s/%s",
895  dbpath, RELMAPPER_FILENAME);
896  fd = OpenTransientFile(mapfilename, O_WRONLY | O_CREAT | PG_BINARY);
897  if (fd < 0)
898  ereport(ERROR,
900  errmsg("could not open file \"%s\": %m",
901  mapfilename)));
902 
903  if (write_wal)
904  {
905  xl_relmap_update xlrec;
906  XLogRecPtr lsn;
907 
908  /* now errors are fatal ... */
910 
911  xlrec.dbid = dbid;
912  xlrec.tsid = tsid;
913  xlrec.nbytes = sizeof(RelMapFile);
914 
915  XLogBeginInsert();
916  XLogRegisterData((char *) (&xlrec), MinSizeOfRelmapUpdate);
917  XLogRegisterData((char *) newmap, sizeof(RelMapFile));
918 
919  lsn = XLogInsert(RM_RELMAP_ID, XLOG_RELMAP_UPDATE);
920 
921  /* As always, WAL must hit the disk before the data update does */
922  XLogFlush(lsn);
923  }
924 
925  errno = 0;
927  if (write(fd, newmap, sizeof(RelMapFile)) != sizeof(RelMapFile))
928  {
929  /* if write didn't set errno, assume problem is no disk space */
930  if (errno == 0)
931  errno = ENOSPC;
932  ereport(ERROR,
934  errmsg("could not write file \"%s\": %m",
935  mapfilename)));
936  }
938 
939  /*
940  * We choose to fsync the data to disk before considering the task done.
941  * It would be possible to relax this if it turns out to be a performance
942  * issue, but it would complicate checkpointing --- see notes for
943  * CheckPointRelationMap.
944  */
946  if (pg_fsync(fd) != 0)
949  errmsg("could not fsync file \"%s\": %m",
950  mapfilename)));
952 
953  if (CloseTransientFile(fd) != 0)
954  ereport(ERROR,
956  errmsg("could not close file \"%s\": %m",
957  mapfilename)));
958 
959  /*
960  * Now that the file is safely on disk, send sinval message to let other
961  * backends know to re-read it. We must do this inside the critical
962  * section: if for some reason we fail to send the message, we have to
963  * force a database-wide PANIC. Otherwise other backends might continue
964  * execution with stale mapping information, which would be catastrophic
965  * as soon as others began to use the now-committed data.
966  */
967  if (send_sinval)
968  CacheInvalidateRelmap(dbid);
969 
970  /*
971  * Make sure that the files listed in the map are not deleted if the outer
972  * transaction aborts. This had better be within the critical section
973  * too: it's not likely to fail, but if it did, we'd arrive at transaction
974  * abort with the files still vulnerable. PANICing will leave things in a
975  * good state on-disk.
976  *
977  * Note: we're cheating a little bit here by assuming that mapped files
978  * are either in pg_global or the database's default tablespace.
979  */
980  if (preserve_files)
981  {
982  int32 i;
983 
984  for (i = 0; i < newmap->num_mappings; i++)
985  {
986  RelFileNode rnode;
987 
988  rnode.spcNode = tsid;
989  rnode.dbNode = dbid;
990  rnode.relNode = newmap->mappings[i].mapfilenode;
991  RelationPreserveStorage(rnode, false);
992  }
993  }
994 
995  /* Critical section done */
996  if (write_wal)
998 }
int data_sync_elevel(int elevel)
Definition: fd.c:3826
int pg_fsync(int fd)
Definition: fd.c:359
#define write(a, b, c)
Definition: win32.h:14
void CacheInvalidateRelmap(Oid databaseId)
Definition: inval.c:1492
#define START_CRIT_SECTION()
Definition: miscadmin.h:148
#define END_CRIT_SECTION()
Definition: miscadmin.h:150
struct RelMapFile RelMapFile
#define MinSizeOfRelmapUpdate
Definition: relmapper.h:35
void RelationPreserveStorage(RelFileNode rnode, bool atCommit)
Definition: storage.c:250
pg_crc32c crc
Definition: relmapper.c:90
@ WAIT_EVENT_RELATION_MAP_WRITE
Definition: wait_event.h:197
@ WAIT_EVENT_RELATION_MAP_SYNC
Definition: wait_event.h:196
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2509
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:443
void XLogBeginInsert(void)
Definition: xloginsert.c:150
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:351

References CacheInvalidateRelmap(), CloseTransientFile(), COMP_CRC32C, RelMapFile::crc, crc, data_sync_elevel(), xl_relmap_update::dbid, RelFileNode::dbNode, elog, END_CRIT_SECTION, ereport, errcode_for_file_access(), errmsg(), ERROR, fd(), FIN_CRC32C, i, INIT_CRC32C, RelMapFile::magic, RelMapping::mapfilenode, RelMapFile::mappings, MAX_MAPPINGS, MAXPGPATH, MinSizeOfRelmapUpdate, xl_relmap_update::nbytes, RelMapFile::num_mappings, offsetof, OpenTransientFile(), PG_BINARY, pg_fsync(), pgstat_report_wait_end(), pgstat_report_wait_start(), RelationPreserveStorage(), RELMAPPER_FILEMAGIC, RELMAPPER_FILENAME, RelFileNode::relNode, snprintf, RelFileNode::spcNode, START_CRIT_SECTION, xl_relmap_update::tsid, WAIT_EVENT_RELATION_MAP_SYNC, WAIT_EVENT_RELATION_MAP_WRITE, write, XLOG_RELMAP_UPDATE, XLogBeginInsert(), XLogFlush(), XLogInsert(), and XLogRegisterData().

Referenced by perform_relmap_update(), RelationMapCopy(), RelationMapFinishBootstrap(), and relmap_redo().

Variable Documentation

◆ active_local_updates

◆ active_shared_updates

◆ local_map

◆ pending_local_updates

◆ pending_shared_updates

◆ shared_map