PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

Oid RelationMapOidToFilenode (Oid relationId, bool shared)
 
Oid RelationMapFilenodeToOid (Oid relationId, bool shared)
 
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)
 
void AtPrepare_RelationMap (void)
 
void CheckPointRelationMap (void)
 
void RelationMapFinishBootstrap (void)
 
void RelationMapInitialize (void)
 
void RelationMapInitializePhase2 (void)
 
void RelationMapInitializePhase3 (void)
 
void relmap_redo (XLogReaderState *record)
 
void relmap_desc (StringInfo buf, XLogReaderState *record)
 
const char * relmap_identify (uint8 info)
 

Macro Definition Documentation

#define MinSizeOfRelmapUpdate   offsetof(xl_relmap_update, data)

Definition at line 35 of file relmapper.h.

Referenced by write_relmap_file().

#define XLOG_RELMAP_UPDATE   0x00

Definition at line 25 of file relmapper.h.

Referenced by relmap_desc(), relmap_identify(), relmap_redo(), and write_relmap_file().

Typedef Documentation

Function Documentation

void AtCCI_RelationMap ( void  )

Definition at line 422 of file relmapper.c.

References merge_map_updates(), and RelMapFile::num_mappings.

Referenced by AtCCI_LocalCache().

423 {
425  {
428  true);
430  }
432  {
435  true);
437  }
438 }
static void merge_map_updates(RelMapFile *map, const RelMapFile *updates, bool add_okay)
Definition: relmapper.c:334
static RelMapFile pending_shared_updates
Definition: relmapper.c:116
static RelMapFile active_local_updates
Definition: relmapper.c:115
static RelMapFile pending_local_updates
Definition: relmapper.c:117
static RelMapFile active_shared_updates
Definition: relmapper.c:114
int32 num_mappings
Definition: relmapper.c:87
void AtEOXact_RelationMap ( bool  isCommit)

Definition at line 456 of file relmapper.c.

References Assert, RelMapFile::num_mappings, and perform_relmap_update().

Referenced by AbortTransaction(), and CommitTransaction().

457 {
458  if (isCommit)
459  {
460  /*
461  * We should not get here with any "pending" updates. (We could
462  * logically choose to treat such as committed, but in the current
463  * code this should never happen.)
464  */
467 
468  /*
469  * Write any active updates to the actual map files, then reset them.
470  */
472  {
475  }
477  {
480  }
481  }
482  else
483  {
484  /* Abort --- drop all local and pending updates */
489  }
490 }
static RelMapFile pending_shared_updates
Definition: relmapper.c:116
static void perform_relmap_update(bool shared, const RelMapFile *updates)
Definition: relmapper.c:856
static RelMapFile active_local_updates
Definition: relmapper.c:115
static RelMapFile pending_local_updates
Definition: relmapper.c:117
static RelMapFile active_shared_updates
Definition: relmapper.c:114
#define Assert(condition)
Definition: c.h:671
int32 num_mappings
Definition: relmapper.c:87
void AtPrepare_RelationMap ( void  )

Definition at line 500 of file relmapper.c.

References ereport, errcode(), errmsg(), ERROR, and RelMapFile::num_mappings.

Referenced by PrepareTransaction().

501 {
506  ereport(ERROR,
507  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
508  errmsg("cannot PREPARE a transaction that modified relation mapping")));
509 }
int errcode(int sqlerrcode)
Definition: elog.c:575
static RelMapFile pending_shared_updates
Definition: relmapper.c:116
#define ERROR
Definition: elog.h:43
static RelMapFile active_local_updates
Definition: relmapper.c:115
static RelMapFile pending_local_updates
Definition: relmapper.c:117
#define ereport(elevel, rest)
Definition: elog.h:122
static RelMapFile active_shared_updates
Definition: relmapper.c:114
int errmsg(const char *fmt,...)
Definition: elog.c:797
int32 num_mappings
Definition: relmapper.c:87
void CheckPointRelationMap ( void  )

Definition at line 523 of file relmapper.c.

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

Referenced by CheckPointGuts().

524 {
525  LWLockAcquire(RelationMappingLock, LW_SHARED);
526  LWLockRelease(RelationMappingLock);
527 }
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1714
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1110
Oid RelationMapFilenodeToOid ( Oid  relationId,
bool  shared 
)

Definition at line 198 of file relmapper.c.

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().

199 {
200  const RelMapFile *map;
201  int32 i;
202 
203  /* If there are active updates, believe those over the main maps */
204  if (shared)
205  {
206  map = &active_shared_updates;
207  for (i = 0; i < map->num_mappings; i++)
208  {
209  if (filenode == map->mappings[i].mapfilenode)
210  return map->mappings[i].mapoid;
211  }
212  map = &shared_map;
213  for (i = 0; i < map->num_mappings; i++)
214  {
215  if (filenode == map->mappings[i].mapfilenode)
216  return map->mappings[i].mapoid;
217  }
218  }
219  else
220  {
221  map = &active_local_updates;
222  for (i = 0; i < map->num_mappings; i++)
223  {
224  if (filenode == map->mappings[i].mapfilenode)
225  return map->mappings[i].mapoid;
226  }
227  map = &local_map;
228  for (i = 0; i < map->num_mappings; i++)
229  {
230  if (filenode == map->mappings[i].mapfilenode)
231  return map->mappings[i].mapoid;
232  }
233  }
234 
235  return InvalidOid;
236 }
Oid mapoid
Definition: relmapper.c:80
static RelMapFile local_map
Definition: relmapper.c:99
signed int int32
Definition: c.h:253
static RelMapFile shared_map
Definition: relmapper.c:98
static RelMapFile active_local_updates
Definition: relmapper.c:115
RelMapping mappings[MAX_MAPPINGS]
Definition: relmapper.c:88
static RelMapFile active_shared_updates
Definition: relmapper.c:114
#define InvalidOid
Definition: postgres_ext.h:36
Oid mapfilenode
Definition: relmapper.c:81
int i
int32 num_mappings
Definition: relmapper.c:87
void RelationMapFinishBootstrap ( void  )

Definition at line 537 of file relmapper.c.

References Assert, DatabasePath, GLOBALTABLESPACE_OID, InvalidOid, IsBootstrapProcessingMode, MyDatabaseId, MyDatabaseTableSpace, NULL, RelMapFile::num_mappings, and write_relmap_file().

Referenced by BootstrapModeMain().

538 {
540 
541  /* Shouldn't be anything "pending" ... */
546 
547  /* Write the files; no WAL or sinval needed */
548  write_relmap_file(true, &shared_map, false, false, false,
550  write_relmap_file(false, &local_map, false, false, false,
552 }
static RelMapFile local_map
Definition: relmapper.c:99
#define GLOBALTABLESPACE_OID
Definition: pg_tablespace.h:64
static void write_relmap_file(bool shared, RelMapFile *newmap, bool write_wal, bool send_sinval, bool preserve_files, Oid dbid, Oid tsid, const char *dbpath)
Definition: relmapper.c:709
Oid MyDatabaseTableSpace
Definition: globals.c:78
static RelMapFile pending_shared_updates
Definition: relmapper.c:116
static RelMapFile shared_map
Definition: relmapper.c:98
static RelMapFile active_local_updates
Definition: relmapper.c:115
static RelMapFile pending_local_updates
Definition: relmapper.c:117
static RelMapFile active_shared_updates
Definition: relmapper.c:114
Oid MyDatabaseId
Definition: globals.c:76
#define InvalidOid
Definition: postgres_ext.h:36
char * DatabasePath
Definition: globals.c:84
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:365
int32 num_mappings
Definition: relmapper.c:87
void RelationMapInitialize ( void  )

Definition at line 561 of file relmapper.c.

References RelMapFile::magic, and RelMapFile::num_mappings.

Referenced by RelationCacheInitialize().

562 {
563  /* The static variables should initialize to zeroes, but let's be sure */
564  shared_map.magic = 0; /* mark it not loaded */
565  local_map.magic = 0;
572 }
int32 magic
Definition: relmapper.c:86
static RelMapFile local_map
Definition: relmapper.c:99
static RelMapFile pending_shared_updates
Definition: relmapper.c:116
static RelMapFile shared_map
Definition: relmapper.c:98
static RelMapFile active_local_updates
Definition: relmapper.c:115
static RelMapFile pending_local_updates
Definition: relmapper.c:117
static RelMapFile active_shared_updates
Definition: relmapper.c:114
int32 num_mappings
Definition: relmapper.c:87
void RelationMapInitializePhase2 ( void  )

Definition at line 581 of file relmapper.c.

References IsBootstrapProcessingMode, and load_relmap_file().

Referenced by RelationCacheInitializePhase2().

582 {
583  /*
584  * In bootstrap mode, the map file isn't there yet, so do nothing.
585  */
587  return;
588 
589  /*
590  * Load the shared map file, die on error.
591  */
592  load_relmap_file(true);
593 }
static void load_relmap_file(bool shared)
Definition: relmapper.c:625
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:365
void RelationMapInitializePhase3 ( void  )

Definition at line 602 of file relmapper.c.

References IsBootstrapProcessingMode, and load_relmap_file().

Referenced by RelationCacheInitializePhase3().

603 {
604  /*
605  * In bootstrap mode, the map file isn't there yet, so do nothing.
606  */
608  return;
609 
610  /*
611  * Load the local map file, die on error.
612  */
613  load_relmap_file(false);
614 }
static void load_relmap_file(bool shared)
Definition: relmapper.c:625
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:365
void RelationMapInvalidate ( bool  shared)

Definition at line 386 of file relmapper.c.

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

Referenced by LocalExecuteInvalidationMessage().

387 {
388  if (shared)
389  {
391  load_relmap_file(true);
392  }
393  else
394  {
396  load_relmap_file(false);
397  }
398 }
int32 magic
Definition: relmapper.c:86
static RelMapFile local_map
Definition: relmapper.c:99
#define RELMAPPER_FILEMAGIC
Definition: relmapper.c:74
static RelMapFile shared_map
Definition: relmapper.c:98
static void load_relmap_file(bool shared)
Definition: relmapper.c:625
void RelationMapInvalidateAll ( void  )

Definition at line 408 of file relmapper.c.

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

Referenced by RelationCacheInvalidate().

409 {
411  load_relmap_file(true);
413  load_relmap_file(false);
414 }
int32 magic
Definition: relmapper.c:86
static RelMapFile local_map
Definition: relmapper.c:99
#define RELMAPPER_FILEMAGIC
Definition: relmapper.c:74
static RelMapFile shared_map
Definition: relmapper.c:98
static void load_relmap_file(bool shared)
Definition: relmapper.c:625
Oid RelationMapOidToFilenode ( Oid  relationId,
bool  shared 
)

Definition at line 145 of file relmapper.c.

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().

146 {
147  const RelMapFile *map;
148  int32 i;
149 
150  /* If there are active updates, believe those over the main maps */
151  if (shared)
152  {
153  map = &active_shared_updates;
154  for (i = 0; i < map->num_mappings; i++)
155  {
156  if (relationId == map->mappings[i].mapoid)
157  return map->mappings[i].mapfilenode;
158  }
159  map = &shared_map;
160  for (i = 0; i < map->num_mappings; i++)
161  {
162  if (relationId == map->mappings[i].mapoid)
163  return map->mappings[i].mapfilenode;
164  }
165  }
166  else
167  {
168  map = &active_local_updates;
169  for (i = 0; i < map->num_mappings; i++)
170  {
171  if (relationId == map->mappings[i].mapoid)
172  return map->mappings[i].mapfilenode;
173  }
174  map = &local_map;
175  for (i = 0; i < map->num_mappings; i++)
176  {
177  if (relationId == map->mappings[i].mapoid)
178  return map->mappings[i].mapfilenode;
179  }
180  }
181 
182  return InvalidOid;
183 }
Oid mapoid
Definition: relmapper.c:80
static RelMapFile local_map
Definition: relmapper.c:99
signed int int32
Definition: c.h:253
static RelMapFile shared_map
Definition: relmapper.c:98
static RelMapFile active_local_updates
Definition: relmapper.c:115
RelMapping mappings[MAX_MAPPINGS]
Definition: relmapper.c:88
static RelMapFile active_shared_updates
Definition: relmapper.c:114
#define InvalidOid
Definition: postgres_ext.h:36
Oid mapfilenode
Definition: relmapper.c:81
int i
int32 num_mappings
Definition: relmapper.c:87
void RelationMapRemoveMapping ( Oid  relationId)

Definition at line 356 of file relmapper.c.

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

Referenced by finish_heap_swap().

357 {
359  int32 i;
360 
361  for (i = 0; i < map->num_mappings; i++)
362  {
363  if (relationId == map->mappings[i].mapoid)
364  {
365  /* Found it, collapse it out */
366  map->mappings[i] = map->mappings[map->num_mappings - 1];
367  map->num_mappings--;
368  return;
369  }
370  }
371  elog(ERROR, "could not find temporary mapping for relation %u",
372  relationId);
373 }
Oid mapoid
Definition: relmapper.c:80
signed int int32
Definition: c.h:253
#define ERROR
Definition: elog.h:43
static RelMapFile active_local_updates
Definition: relmapper.c:115
RelMapping mappings[MAX_MAPPINGS]
Definition: relmapper.c:88
int i
#define elog
Definition: elog.h:219
int32 num_mappings
Definition: relmapper.c:87
void RelationMapUpdateMap ( Oid  relationId,
Oid  fileNode,
bool  shared,
bool  immediate 
)

Definition at line 247 of file relmapper.c.

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

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

249 {
250  RelMapFile *map;
251 
253  {
254  /*
255  * In bootstrap mode, the mapping gets installed in permanent map.
256  */
257  if (shared)
258  map = &shared_map;
259  else
260  map = &local_map;
261  }
262  else
263  {
264  /*
265  * We don't currently support map changes within subtransactions. This
266  * could be done with more bookkeeping infrastructure, but it doesn't
267  * presently seem worth it.
268  */
270  elog(ERROR, "cannot change relation mapping within subtransaction");
271 
272  if (immediate)
273  {
274  /* Make it active, but only locally */
275  if (shared)
276  map = &active_shared_updates;
277  else
278  map = &active_local_updates;
279  }
280  else
281  {
282  /* Make it pending */
283  if (shared)
284  map = &pending_shared_updates;
285  else
286  map = &pending_local_updates;
287  }
288  }
289  apply_map_update(map, relationId, fileNode, true);
290 }
static RelMapFile local_map
Definition: relmapper.c:99
static RelMapFile pending_shared_updates
Definition: relmapper.c:116
static RelMapFile shared_map
Definition: relmapper.c:98
#define ERROR
Definition: elog.h:43
static RelMapFile active_local_updates
Definition: relmapper.c:115
static RelMapFile pending_local_updates
Definition: relmapper.c:117
static RelMapFile active_shared_updates
Definition: relmapper.c:114
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:760
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:365
#define elog
Definition: elog.h:219
static void apply_map_update(RelMapFile *map, Oid relationId, Oid fileNode, bool add_okay)
Definition: relmapper.c:302
void relmap_desc ( StringInfo  buf,
XLogReaderState record 
)

Definition at line 20 of file relmapdesc.c.

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

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 %u",
30  xlrec->dbid, xlrec->tsid, xlrec->nbytes);
31  }
32 }
unsigned char uint8
Definition: c.h:263
#define XLogRecGetData(decoder)
Definition: xlogreader.h:202
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:198
#define XLOG_RELMAP_UPDATE
Definition: relmapper.h:25
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
const char* relmap_identify ( uint8  info)

Definition at line 35 of file relmapdesc.c.

References NULL, XLOG_RELMAP_UPDATE, and XLR_INFO_MASK.

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 }
#define XLOG_RELMAP_UPDATE
Definition: relmapper.h:25
#define NULL
Definition: c.h:226
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
void relmap_redo ( XLogReaderState record)

Definition at line 904 of file relmapper.c.

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

905 {
906  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
907 
908  /* Backup blocks are not used in relmap records */
909  Assert(!XLogRecHasAnyBlockRefs(record));
910 
911  if (info == XLOG_RELMAP_UPDATE)
912  {
913  xl_relmap_update *xlrec = (xl_relmap_update *) XLogRecGetData(record);
914  RelMapFile newmap;
915  char *dbpath;
916 
917  if (xlrec->nbytes != sizeof(RelMapFile))
918  elog(PANIC, "relmap_redo: wrong size %u in relmap update record",
919  xlrec->nbytes);
920  memcpy(&newmap, xlrec->data, sizeof(newmap));
921 
922  /* We need to construct the pathname for this database */
923  dbpath = GetDatabasePath(xlrec->dbid, xlrec->tsid);
924 
925  /*
926  * Write out the new map and send sinval, but of course don't write a
927  * new WAL entry. There's no surrounding transaction to tell to
928  * preserve files, either.
929  *
930  * There shouldn't be anyone else updating relmaps during WAL replay,
931  * so we don't bother to take the RelationMappingLock. We would need
932  * to do so if load_relmap_file needed to interlock against writers.
933  */
934  write_relmap_file((xlrec->dbid == InvalidOid), &newmap,
935  false, true, false,
936  xlrec->dbid, xlrec->tsid, dbpath);
937 
938  pfree(dbpath);
939  }
940  else
941  elog(PANIC, "relmap_redo: unknown op code %u", info);
942 }
unsigned char uint8
Definition: c.h:263
static void write_relmap_file(bool shared, RelMapFile *newmap, bool write_wal, bool send_sinval, bool preserve_files, Oid dbid, Oid tsid, const char *dbpath)
Definition: relmapper.c:709
#define PANIC
Definition: elog.h:53
void pfree(void *pointer)
Definition: mcxt.c:992
#define XLogRecGetData(decoder)
Definition: xlogreader.h:202
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:198
char * GetDatabasePath(Oid dbNode, Oid spcNode)
Definition: relpath.c:108
#define XLOG_RELMAP_UPDATE
Definition: relmapper.h:25
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:671
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:204
#define elog
Definition: elog.h:219
char data[FLEXIBLE_ARRAY_MEMBER]
Definition: relmapper.h:32