PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 charrelmap_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  )
extern

Definition at line 505 of file relmapper.c.

506{
508 {
511 true);
513 }
515 {
518 true);
520 }
521}
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:417
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 
)
extern

Definition at line 542 of file relmapper.c.

543{
545 {
546 /*
547 * We should not get here with any "pending" updates. (We could
548 * logically choose to treat such as committed, but in the current
549 * code this should never happen.)
550 */
553
554 /*
555 * Write any active updates to the actual map files, then reset them.
556 */
558 {
561 }
563 {
566 }
567 }
568 else
569 {
570 /* Abort or parallel worker --- drop all local and pending updates */
573
578 }
579}
#define Assert(condition)
Definition c.h:945
static int fb(int x)
static void perform_relmap_update(bool shared, const RelMapFile *updates)
Definition relmapper.c:1040

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

Referenced by AbortTransaction(), and CommitTransaction().

◆ AtPrepare_RelationMap()

void AtPrepare_RelationMap ( void  )
extern

Definition at line 589 of file relmapper.c.

590{
597 errmsg("cannot PREPARE a transaction that modified relation mapping")));
598}
int errcode(int sqlerrcode)
Definition elog.c:874
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
static char * errmsg

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

Referenced by PrepareTransaction().

◆ CheckPointRelationMap()

void CheckPointRelationMap ( void  )
extern

Definition at line 612 of file relmapper.c.

613{
616}
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1177
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1794
@ LW_SHARED
Definition lwlock.h:113

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

Referenced by CheckPointGuts().

◆ EstimateRelationMapSpace()

Size EstimateRelationMapSpace ( void  )
extern

Definition at line 714 of file relmapper.c.

715{
716 return sizeof(SerializedActiveRelMaps);
717}

Referenced by InitializeParallelDSM(), and SerializeRelationMap().

◆ RelationMapCopy()

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

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.
307 *
308 * There's no point in trying to preserve files here. The new database
309 * isn't usable yet anyway, and won't ever be if we can't install a relmap
310 * file.
311 */
313 write_relmap_file(&map, true, false, false, dbid, tsid, dstdbpath);
315}
@ LW_EXCLUSIVE
Definition lwlock.h:112
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:890
static void read_relmap_file(RelMapFile *map, char *dbpath, bool lock_held, int elevel)
Definition relmapper.c:785

References ERROR, fb(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), read_relmap_file(), and write_relmap_file().

Referenced by CreateDatabaseUsingWalLog().

◆ RelationMapFilenumberToOid()

Oid RelationMapFilenumberToOid ( RelFileNumber  filenumber,
bool  shared 
)
extern

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 {
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 {
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}
int32_t int32
Definition c.h:614
int i
Definition isn.c:77
#define InvalidOid
static RelMapFile shared_map
Definition relmapper.c:113
static RelMapFile local_map
Definition relmapper.c:114
RelMapping mappings[MAX_MAPPINGS]
Definition relmapper.c:94
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  )
extern

◆ RelationMapInitialize()

void RelationMapInitialize ( void  )
extern

Definition at line 652 of file relmapper.c.

653{
654 /* The static variables should initialize to zeroes, but let's be sure */
655 shared_map.magic = 0; /* mark it not loaded */
656 local_map.magic = 0;
663}
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  )
extern

Definition at line 672 of file relmapper.c.

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

References IsBootstrapProcessingMode, and load_relmap_file().

Referenced by RelationCacheInitializePhase2().

◆ RelationMapInitializePhase3()

void RelationMapInitializePhase3 ( void  )
extern

Definition at line 693 of file relmapper.c.

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

References IsBootstrapProcessingMode, and load_relmap_file().

Referenced by RelationCacheInitializePhase3().

◆ RelationMapInvalidate()

void RelationMapInvalidate ( bool  shared)
extern

Definition at line 469 of file relmapper.c.

470{
471 if (shared)
472 {
474 load_relmap_file(true, false);
475 }
476 else
477 {
479 load_relmap_file(false, false);
480 }
481}
#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  )
extern

Definition at line 491 of file relmapper.c.

492{
494 load_relmap_file(true, false);
496 load_relmap_file(false, false);
497}

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

Referenced by RelationCacheInvalidate().

◆ RelationMapOidToFilenumber()

RelFileNumber RelationMapOidToFilenumber ( Oid  relationId,
bool  shared 
)
extern

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 {
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 {
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
204}
#define InvalidRelFileNumber
Definition relpath.h:26

References active_local_updates, active_shared_updates, fb(), 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 
)
extern

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
282}

References ERROR, fb(), 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)
extern

Definition at line 439 of file relmapper.c.

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

References active_local_updates, elog, ERROR, fb(), 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 
)
extern

Definition at line 326 of file relmapper.c.

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

References active_local_updates, active_shared_updates, apply_map_update(), elog, ERROR, fb(), 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 
)
extern

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 {
28
29 appendStringInfo(buf, "database %u tablespace %u size %d",
30 xlrec->dbid, xlrec->tsid, xlrec->nbytes);
31 }
32}
uint8_t uint8
Definition c.h:616
static char buf[DEFAULT_XLOG_SEG_SIZE]
#define XLOG_RELMAP_UPDATE
Definition relmapper.h:25
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition stringinfo.c:145
#define XLogRecGetInfo(decoder)
Definition xlogreader.h:409
#define XLogRecGetData(decoder)
Definition xlogreader.h:414

References appendStringInfo(), buf, fb(), XLOG_RELMAP_UPDATE, XLogRecGetData, and XLogRecGetInfo.

◆ relmap_identify()

const char * relmap_identify ( uint8  info)
extern

Definition at line 35 of file relmapdesc.c.

36{
37 const char *id = NULL;
38
39 switch (info & ~XLR_INFO_MASK)
40 {
42 id = "UPDATE";
43 break;
44 }
45
46 return id;
47}
#define XLR_INFO_MASK
Definition xlogrecord.h:62

References fb(), XLOG_RELMAP_UPDATE, and XLR_INFO_MASK.

◆ relmap_redo()

void relmap_redo ( XLogReaderState record)
extern

Definition at line 1097 of file relmapper.c.

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

References Assert, elog, fb(), GetDatabasePath(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), PANIC, pfree(), write_relmap_file(), XLOG_RELMAP_UPDATE, XLogRecGetData, XLogRecGetInfo, and XLogRecHasAnyBlockRefs.

◆ RestoreRelationMap()

void RestoreRelationMap ( char startAddress)
extern

Definition at line 742 of file relmapper.c.

743{
745
750 elog(ERROR, "parallel worker has existing mappings");
751
753 active_shared_updates = relmaps->active_shared_updates;
754 active_local_updates = relmaps->active_local_updates;
755}

References active_local_updates, active_shared_updates, elog, ERROR, fb(), RelMapFile::num_mappings, pending_local_updates, and pending_shared_updates.

Referenced by ParallelWorkerMain().

◆ SerializeRelationMap()

void SerializeRelationMap ( Size  maxSize,
char startAddress 
)
extern

Definition at line 725 of file relmapper.c.

726{
728
730
732 relmaps->active_shared_updates = active_shared_updates;
733 relmaps->active_local_updates = active_local_updates;
734}
Size EstimateRelationMapSpace(void)
Definition relmapper.c:714

References active_local_updates, active_shared_updates, Assert, EstimateRelationMapSpace(), and fb().

Referenced by InitializeParallelDSM().