]> granicus.if.org Git - postgresql/blob - src/backend/utils/cache/relmapper.c
Change TRUE/FALSE to true/false
[postgresql] / src / backend / utils / cache / relmapper.c
1 /*-------------------------------------------------------------------------
2  *
3  * relmapper.c
4  *        Catalog-to-filenode mapping
5  *
6  * For most tables, the physical file underlying the table is specified by
7  * pg_class.relfilenode.  However, that obviously won't work for pg_class
8  * itself, nor for the other "nailed" catalogs for which we have to be able
9  * to set up working Relation entries without access to pg_class.  It also
10  * does not work for shared catalogs, since there is no practical way to
11  * update other databases' pg_class entries when relocating a shared catalog.
12  * Therefore, for these special catalogs (henceforth referred to as "mapped
13  * catalogs") we rely on a separately maintained file that shows the mapping
14  * from catalog OIDs to filenode numbers.  Each database has a map file for
15  * its local mapped catalogs, and there is a separate map file for shared
16  * catalogs.  Mapped catalogs have zero in their pg_class.relfilenode entries.
17  *
18  * Relocation of a normal table is committed (ie, the new physical file becomes
19  * authoritative) when the pg_class row update commits.  For mapped catalogs,
20  * the act of updating the map file is effectively commit of the relocation.
21  * We postpone the file update till just before commit of the transaction
22  * doing the rewrite, but there is necessarily a window between.  Therefore
23  * mapped catalogs can only be relocated by operations such as VACUUM FULL
24  * and CLUSTER, which make no transactionally-significant changes: it must be
25  * safe for the new file to replace the old, even if the transaction itself
26  * aborts.  An important factor here is that the indexes and toast table of
27  * a mapped catalog must also be mapped, so that the rewrites/relocations of
28  * all these files commit in a single map file update rather than being tied
29  * to transaction commit.
30  *
31  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
32  * Portions Copyright (c) 1994, Regents of the University of California
33  *
34  *
35  * IDENTIFICATION
36  *        src/backend/utils/cache/relmapper.c
37  *
38  *-------------------------------------------------------------------------
39  */
40 #include "postgres.h"
41
42 #include <fcntl.h>
43 #include <sys/stat.h>
44 #include <unistd.h>
45
46 #include "access/xact.h"
47 #include "access/xlog.h"
48 #include "access/xloginsert.h"
49 #include "catalog/catalog.h"
50 #include "catalog/pg_tablespace.h"
51 #include "catalog/storage.h"
52 #include "miscadmin.h"
53 #include "pgstat.h"
54 #include "storage/fd.h"
55 #include "storage/lwlock.h"
56 #include "utils/inval.h"
57 #include "utils/relmapper.h"
58
59
60 /*
61  * The map file is critical data: we have no automatic method for recovering
62  * from loss or corruption of it.  We use a CRC so that we can detect
63  * corruption.  To minimize the risk of failed updates, the map file should
64  * be kept to no more than one standard-size disk sector (ie 512 bytes),
65  * and we use overwrite-in-place rather than playing renaming games.
66  * The struct layout below is designed to occupy exactly 512 bytes, which
67  * might make filesystem updates a bit more efficient.
68  *
69  * Entries in the mappings[] array are in no particular order.  We could
70  * speed searching by insisting on OID order, but it really shouldn't be
71  * worth the trouble given the intended size of the mapping sets.
72  */
73 #define RELMAPPER_FILENAME              "pg_filenode.map"
74
75 #define RELMAPPER_FILEMAGIC             0x592717        /* version ID value */
76
77 #define MAX_MAPPINGS                    62      /* 62 * 8 + 16 = 512 */
78
79 typedef struct RelMapping
80 {
81         Oid                     mapoid;                 /* OID of a catalog */
82         Oid                     mapfilenode;    /* its filenode number */
83 } RelMapping;
84
85 typedef struct RelMapFile
86 {
87         int32           magic;                  /* always RELMAPPER_FILEMAGIC */
88         int32           num_mappings;   /* number of valid RelMapping entries */
89         RelMapping      mappings[MAX_MAPPINGS];
90         pg_crc32c       crc;                    /* CRC of all above */
91         int32           pad;                    /* to make the struct size be 512 exactly */
92 } RelMapFile;
93
94 /*
95  * The currently known contents of the shared map file and our database's
96  * local map file are stored here.  These can be reloaded from disk
97  * immediately whenever we receive an update sinval message.
98  */
99 static RelMapFile shared_map;
100 static RelMapFile local_map;
101
102 /*
103  * We use the same RelMapFile data structure to track uncommitted local
104  * changes in the mappings (but note the magic and crc fields are not made
105  * valid in these variables).  Currently, map updates are not allowed within
106  * subtransactions, so one set of transaction-level changes is sufficient.
107  *
108  * The active_xxx variables contain updates that are valid in our transaction
109  * and should be honored by RelationMapOidToFilenode.  The pending_xxx
110  * variables contain updates we have been told about that aren't active yet;
111  * they will become active at the next CommandCounterIncrement.  This setup
112  * lets map updates act similarly to updates of pg_class rows, ie, they
113  * become visible only at the next CommandCounterIncrement boundary.
114  */
115 static RelMapFile active_shared_updates;
116 static RelMapFile active_local_updates;
117 static RelMapFile pending_shared_updates;
118 static RelMapFile pending_local_updates;
119
120
121 /* non-export function prototypes */
122 static void apply_map_update(RelMapFile *map, Oid relationId, Oid fileNode,
123                                  bool add_okay);
124 static void merge_map_updates(RelMapFile *map, const RelMapFile *updates,
125                                   bool add_okay);
126 static void load_relmap_file(bool shared);
127 static void write_relmap_file(bool shared, RelMapFile *newmap,
128                                   bool write_wal, bool send_sinval, bool preserve_files,
129                                   Oid dbid, Oid tsid, const char *dbpath);
130 static void perform_relmap_update(bool shared, const RelMapFile *updates);
131
132
133 /*
134  * RelationMapOidToFilenode
135  *
136  * The raison d' etre ... given a relation OID, look up its filenode.
137  *
138  * Although shared and local relation OIDs should never overlap, the caller
139  * always knows which we need --- so pass that information to avoid useless
140  * searching.
141  *
142  * Returns InvalidOid if the OID is not known (which should never happen,
143  * but the caller is in a better position to report a meaningful error).
144  */
145 Oid
146 RelationMapOidToFilenode(Oid relationId, bool shared)
147 {
148         const RelMapFile *map;
149         int32           i;
150
151         /* If there are active updates, believe those over the main maps */
152         if (shared)
153         {
154                 map = &active_shared_updates;
155                 for (i = 0; i < map->num_mappings; i++)
156                 {
157                         if (relationId == map->mappings[i].mapoid)
158                                 return map->mappings[i].mapfilenode;
159                 }
160                 map = &shared_map;
161                 for (i = 0; i < map->num_mappings; i++)
162                 {
163                         if (relationId == map->mappings[i].mapoid)
164                                 return map->mappings[i].mapfilenode;
165                 }
166         }
167         else
168         {
169                 map = &active_local_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 = &local_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
183         return InvalidOid;
184 }
185
186 /*
187  * RelationMapFilenodeToOid
188  *
189  * Do the reverse of the normal direction of mapping done in
190  * RelationMapOidToFilenode.
191  *
192  * This is not supposed to be used during normal running but rather for
193  * information purposes when looking at the filesystem or xlog.
194  *
195  * Returns InvalidOid if the OID is not known; this can easily happen if the
196  * relfilenode doesn't pertain to a mapped relation.
197  */
198 Oid
199 RelationMapFilenodeToOid(Oid filenode, bool shared)
200 {
201         const RelMapFile *map;
202         int32           i;
203
204         /* If there are active updates, believe those over the main maps */
205         if (shared)
206         {
207                 map = &active_shared_updates;
208                 for (i = 0; i < map->num_mappings; i++)
209                 {
210                         if (filenode == map->mappings[i].mapfilenode)
211                                 return map->mappings[i].mapoid;
212                 }
213                 map = &shared_map;
214                 for (i = 0; i < map->num_mappings; i++)
215                 {
216                         if (filenode == map->mappings[i].mapfilenode)
217                                 return map->mappings[i].mapoid;
218                 }
219         }
220         else
221         {
222                 map = &active_local_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 = &local_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
236         return InvalidOid;
237 }
238
239 /*
240  * RelationMapUpdateMap
241  *
242  * Install a new relfilenode mapping for the specified relation.
243  *
244  * If immediate is true (or we're bootstrapping), the mapping is activated
245  * immediately.  Otherwise it is made pending until CommandCounterIncrement.
246  */
247 void
248 RelationMapUpdateMap(Oid relationId, Oid fileNode, bool shared,
249                                          bool immediate)
250 {
251         RelMapFile *map;
252
253         if (IsBootstrapProcessingMode())
254         {
255                 /*
256                  * In bootstrap mode, the mapping gets installed in permanent map.
257                  */
258                 if (shared)
259                         map = &shared_map;
260                 else
261                         map = &local_map;
262         }
263         else
264         {
265                 /*
266                  * We don't currently support map changes within subtransactions. This
267                  * could be done with more bookkeeping infrastructure, but it doesn't
268                  * presently seem worth it.
269                  */
270                 if (GetCurrentTransactionNestLevel() > 1)
271                         elog(ERROR, "cannot change relation mapping within subtransaction");
272
273                 if (immediate)
274                 {
275                         /* Make it active, but only locally */
276                         if (shared)
277                                 map = &active_shared_updates;
278                         else
279                                 map = &active_local_updates;
280                 }
281                 else
282                 {
283                         /* Make it pending */
284                         if (shared)
285                                 map = &pending_shared_updates;
286                         else
287                                 map = &pending_local_updates;
288                 }
289         }
290         apply_map_update(map, relationId, fileNode, true);
291 }
292
293 /*
294  * apply_map_update
295  *
296  * Insert a new mapping into the given map variable, replacing any existing
297  * mapping for the same relation.
298  *
299  * In some cases the caller knows there must be an existing mapping; pass
300  * add_okay = false to draw an error if not.
301  */
302 static void
303 apply_map_update(RelMapFile *map, Oid relationId, Oid fileNode, bool add_okay)
304 {
305         int32           i;
306
307         /* Replace any existing mapping */
308         for (i = 0; i < map->num_mappings; i++)
309         {
310                 if (relationId == map->mappings[i].mapoid)
311                 {
312                         map->mappings[i].mapfilenode = fileNode;
313                         return;
314                 }
315         }
316
317         /* Nope, need to add a new mapping */
318         if (!add_okay)
319                 elog(ERROR, "attempt to apply a mapping to unmapped relation %u",
320                          relationId);
321         if (map->num_mappings >= MAX_MAPPINGS)
322                 elog(ERROR, "ran out of space in relation map");
323         map->mappings[map->num_mappings].mapoid = relationId;
324         map->mappings[map->num_mappings].mapfilenode = fileNode;
325         map->num_mappings++;
326 }
327
328 /*
329  * merge_map_updates
330  *
331  * Merge all the updates in the given pending-update map into the target map.
332  * This is just a bulk form of apply_map_update.
333  */
334 static void
335 merge_map_updates(RelMapFile *map, const RelMapFile *updates, bool add_okay)
336 {
337         int32           i;
338
339         for (i = 0; i < updates->num_mappings; i++)
340         {
341                 apply_map_update(map,
342                                                  updates->mappings[i].mapoid,
343                                                  updates->mappings[i].mapfilenode,
344                                                  add_okay);
345         }
346 }
347
348 /*
349  * RelationMapRemoveMapping
350  *
351  * Remove a relation's entry in the map.  This is only allowed for "active"
352  * (but not committed) local mappings.  We need it so we can back out the
353  * entry for the transient target file when doing VACUUM FULL/CLUSTER on
354  * a mapped relation.
355  */
356 void
357 RelationMapRemoveMapping(Oid relationId)
358 {
359         RelMapFile *map = &active_local_updates;
360         int32           i;
361
362         for (i = 0; i < map->num_mappings; i++)
363         {
364                 if (relationId == map->mappings[i].mapoid)
365                 {
366                         /* Found it, collapse it out */
367                         map->mappings[i] = map->mappings[map->num_mappings - 1];
368                         map->num_mappings--;
369                         return;
370                 }
371         }
372         elog(ERROR, "could not find temporary mapping for relation %u",
373                  relationId);
374 }
375
376 /*
377  * RelationMapInvalidate
378  *
379  * This routine is invoked for SI cache flush messages.  We must re-read
380  * the indicated map file.  However, we might receive a SI message in a
381  * process that hasn't yet, and might never, load the mapping files;
382  * for example the autovacuum launcher, which *must not* try to read
383  * a local map since it is attached to no particular database.
384  * So, re-read only if the map is valid now.
385  */
386 void
387 RelationMapInvalidate(bool shared)
388 {
389         if (shared)
390         {
391                 if (shared_map.magic == RELMAPPER_FILEMAGIC)
392                         load_relmap_file(true);
393         }
394         else
395         {
396                 if (local_map.magic == RELMAPPER_FILEMAGIC)
397                         load_relmap_file(false);
398         }
399 }
400
401 /*
402  * RelationMapInvalidateAll
403  *
404  * Reload all map files.  This is used to recover from SI message buffer
405  * overflow: we can't be sure if we missed an inval message.
406  * Again, reload only currently-valid maps.
407  */
408 void
409 RelationMapInvalidateAll(void)
410 {
411         if (shared_map.magic == RELMAPPER_FILEMAGIC)
412                 load_relmap_file(true);
413         if (local_map.magic == RELMAPPER_FILEMAGIC)
414                 load_relmap_file(false);
415 }
416
417 /*
418  * AtCCI_RelationMap
419  *
420  * Activate any "pending" relation map updates at CommandCounterIncrement time.
421  */
422 void
423 AtCCI_RelationMap(void)
424 {
425         if (pending_shared_updates.num_mappings != 0)
426         {
427                 merge_map_updates(&active_shared_updates,
428                                                   &pending_shared_updates,
429                                                   true);
430                 pending_shared_updates.num_mappings = 0;
431         }
432         if (pending_local_updates.num_mappings != 0)
433         {
434                 merge_map_updates(&active_local_updates,
435                                                   &pending_local_updates,
436                                                   true);
437                 pending_local_updates.num_mappings = 0;
438         }
439 }
440
441 /*
442  * AtEOXact_RelationMap
443  *
444  * Handle relation mapping at main-transaction commit or abort.
445  *
446  * During commit, this must be called as late as possible before the actual
447  * transaction commit, so as to minimize the window where the transaction
448  * could still roll back after committing map changes.  Although nothing
449  * critically bad happens in such a case, we still would prefer that it
450  * not happen, since we'd possibly be losing useful updates to the relations'
451  * pg_class row(s).
452  *
453  * During abort, we just have to throw away any pending map changes.
454  * Normal post-abort cleanup will take care of fixing relcache entries.
455  */
456 void
457 AtEOXact_RelationMap(bool isCommit)
458 {
459         if (isCommit)
460         {
461                 /*
462                  * We should not get here with any "pending" updates.  (We could
463                  * logically choose to treat such as committed, but in the current
464                  * code this should never happen.)
465                  */
466                 Assert(pending_shared_updates.num_mappings == 0);
467                 Assert(pending_local_updates.num_mappings == 0);
468
469                 /*
470                  * Write any active updates to the actual map files, then reset them.
471                  */
472                 if (active_shared_updates.num_mappings != 0)
473                 {
474                         perform_relmap_update(true, &active_shared_updates);
475                         active_shared_updates.num_mappings = 0;
476                 }
477                 if (active_local_updates.num_mappings != 0)
478                 {
479                         perform_relmap_update(false, &active_local_updates);
480                         active_local_updates.num_mappings = 0;
481                 }
482         }
483         else
484         {
485                 /* Abort --- drop all local and pending updates */
486                 active_shared_updates.num_mappings = 0;
487                 active_local_updates.num_mappings = 0;
488                 pending_shared_updates.num_mappings = 0;
489                 pending_local_updates.num_mappings = 0;
490         }
491 }
492
493 /*
494  * AtPrepare_RelationMap
495  *
496  * Handle relation mapping at PREPARE.
497  *
498  * Currently, we don't support preparing any transaction that changes the map.
499  */
500 void
501 AtPrepare_RelationMap(void)
502 {
503         if (active_shared_updates.num_mappings != 0 ||
504                 active_local_updates.num_mappings != 0 ||
505                 pending_shared_updates.num_mappings != 0 ||
506                 pending_local_updates.num_mappings != 0)
507                 ereport(ERROR,
508                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
509                                  errmsg("cannot PREPARE a transaction that modified relation mapping")));
510 }
511
512 /*
513  * CheckPointRelationMap
514  *
515  * This is called during a checkpoint.  It must ensure that any relation map
516  * updates that were WAL-logged before the start of the checkpoint are
517  * securely flushed to disk and will not need to be replayed later.  This
518  * seems unlikely to be a performance-critical issue, so we use a simple
519  * method: we just take and release the RelationMappingLock.  This ensures
520  * that any already-logged map update is complete, because write_relmap_file
521  * will fsync the map file before the lock is released.
522  */
523 void
524 CheckPointRelationMap(void)
525 {
526         LWLockAcquire(RelationMappingLock, LW_SHARED);
527         LWLockRelease(RelationMappingLock);
528 }
529
530 /*
531  * RelationMapFinishBootstrap
532  *
533  * Write out the initial relation mapping files at the completion of
534  * bootstrap.  All the mapped files should have been made known to us
535  * via RelationMapUpdateMap calls.
536  */
537 void
538 RelationMapFinishBootstrap(void)
539 {
540         Assert(IsBootstrapProcessingMode());
541
542         /* Shouldn't be anything "pending" ... */
543         Assert(active_shared_updates.num_mappings == 0);
544         Assert(active_local_updates.num_mappings == 0);
545         Assert(pending_shared_updates.num_mappings == 0);
546         Assert(pending_local_updates.num_mappings == 0);
547
548         /* Write the files; no WAL or sinval needed */
549         write_relmap_file(true, &shared_map, false, false, false,
550                                           InvalidOid, GLOBALTABLESPACE_OID, NULL);
551         write_relmap_file(false, &local_map, false, false, false,
552                                           MyDatabaseId, MyDatabaseTableSpace, DatabasePath);
553 }
554
555 /*
556  * RelationMapInitialize
557  *
558  * This initializes the mapper module at process startup.  We can't access the
559  * database yet, so just make sure the maps are empty.
560  */
561 void
562 RelationMapInitialize(void)
563 {
564         /* The static variables should initialize to zeroes, but let's be sure */
565         shared_map.magic = 0;           /* mark it not loaded */
566         local_map.magic = 0;
567         shared_map.num_mappings = 0;
568         local_map.num_mappings = 0;
569         active_shared_updates.num_mappings = 0;
570         active_local_updates.num_mappings = 0;
571         pending_shared_updates.num_mappings = 0;
572         pending_local_updates.num_mappings = 0;
573 }
574
575 /*
576  * RelationMapInitializePhase2
577  *
578  * This is called to prepare for access to pg_database during startup.
579  * We should be able to read the shared map file now.
580  */
581 void
582 RelationMapInitializePhase2(void)
583 {
584         /*
585          * In bootstrap mode, the map file isn't there yet, so do nothing.
586          */
587         if (IsBootstrapProcessingMode())
588                 return;
589
590         /*
591          * Load the shared map file, die on error.
592          */
593         load_relmap_file(true);
594 }
595
596 /*
597  * RelationMapInitializePhase3
598  *
599  * This is called as soon as we have determined MyDatabaseId and set up
600  * DatabasePath.  At this point we should be able to read the local map file.
601  */
602 void
603 RelationMapInitializePhase3(void)
604 {
605         /*
606          * In bootstrap mode, the map file isn't there yet, so do nothing.
607          */
608         if (IsBootstrapProcessingMode())
609                 return;
610
611         /*
612          * Load the local map file, die on error.
613          */
614         load_relmap_file(false);
615 }
616
617 /*
618  * load_relmap_file -- load data from the shared or local map file
619  *
620  * Because the map file is essential for access to core system catalogs,
621  * failure to read it is a fatal error.
622  *
623  * Note that the local case requires DatabasePath to be set up.
624  */
625 static void
626 load_relmap_file(bool shared)
627 {
628         RelMapFile *map;
629         char            mapfilename[MAXPGPATH];
630         pg_crc32c       crc;
631         int                     fd;
632
633         if (shared)
634         {
635                 snprintf(mapfilename, sizeof(mapfilename), "global/%s",
636                                  RELMAPPER_FILENAME);
637                 map = &shared_map;
638         }
639         else
640         {
641                 snprintf(mapfilename, sizeof(mapfilename), "%s/%s",
642                                  DatabasePath, RELMAPPER_FILENAME);
643                 map = &local_map;
644         }
645
646         /* Read data ... */
647         fd = OpenTransientFile(mapfilename, O_RDONLY | PG_BINARY);
648         if (fd < 0)
649                 ereport(FATAL,
650                                 (errcode_for_file_access(),
651                                  errmsg("could not open relation mapping file \"%s\": %m",
652                                                 mapfilename)));
653
654         /*
655          * Note: we could take RelationMappingLock in shared mode here, but it
656          * seems unnecessary since our read() should be atomic against any
657          * concurrent updater's write().  If the file is updated shortly after we
658          * look, the sinval signaling mechanism will make us re-read it before we
659          * are able to access any relation that's affected by the change.
660          */
661         pgstat_report_wait_start(WAIT_EVENT_RELATION_MAP_READ);
662         if (read(fd, map, sizeof(RelMapFile)) != sizeof(RelMapFile))
663                 ereport(FATAL,
664                                 (errcode_for_file_access(),
665                                  errmsg("could not read relation mapping file \"%s\": %m",
666                                                 mapfilename)));
667         pgstat_report_wait_end();
668
669         CloseTransientFile(fd);
670
671         /* check for correct magic number, etc */
672         if (map->magic != RELMAPPER_FILEMAGIC ||
673                 map->num_mappings < 0 ||
674                 map->num_mappings > MAX_MAPPINGS)
675                 ereport(FATAL,
676                                 (errmsg("relation mapping file \"%s\" contains invalid data",
677                                                 mapfilename)));
678
679         /* verify the CRC */
680         INIT_CRC32C(crc);
681         COMP_CRC32C(crc, (char *) map, offsetof(RelMapFile, crc));
682         FIN_CRC32C(crc);
683
684         if (!EQ_CRC32C(crc, map->crc))
685                 ereport(FATAL,
686                                 (errmsg("relation mapping file \"%s\" contains incorrect checksum",
687                                                 mapfilename)));
688 }
689
690 /*
691  * Write out a new shared or local map file with the given contents.
692  *
693  * The magic number and CRC are automatically updated in *newmap.  On
694  * success, we copy the data to the appropriate permanent static variable.
695  *
696  * If write_wal is true then an appropriate WAL message is emitted.
697  * (It will be false for bootstrap and WAL replay cases.)
698  *
699  * If send_sinval is true then a SI invalidation message is sent.
700  * (This should be true except in bootstrap case.)
701  *
702  * If preserve_files is true then the storage manager is warned not to
703  * delete the files listed in the map.
704  *
705  * Because this may be called during WAL replay when MyDatabaseId,
706  * DatabasePath, etc aren't valid, we require the caller to pass in suitable
707  * values.  The caller is also responsible for being sure no concurrent
708  * map update could be happening.
709  */
710 static void
711 write_relmap_file(bool shared, RelMapFile *newmap,
712                                   bool write_wal, bool send_sinval, bool preserve_files,
713                                   Oid dbid, Oid tsid, const char *dbpath)
714 {
715         int                     fd;
716         RelMapFile *realmap;
717         char            mapfilename[MAXPGPATH];
718
719         /*
720          * Fill in the overhead fields and update CRC.
721          */
722         newmap->magic = RELMAPPER_FILEMAGIC;
723         if (newmap->num_mappings < 0 || newmap->num_mappings > MAX_MAPPINGS)
724                 elog(ERROR, "attempt to write bogus relation mapping");
725
726         INIT_CRC32C(newmap->crc);
727         COMP_CRC32C(newmap->crc, (char *) newmap, offsetof(RelMapFile, crc));
728         FIN_CRC32C(newmap->crc);
729
730         /*
731          * Open the target file.  We prefer to do this before entering the
732          * critical section, so that an open() failure need not force PANIC.
733          */
734         if (shared)
735         {
736                 snprintf(mapfilename, sizeof(mapfilename), "global/%s",
737                                  RELMAPPER_FILENAME);
738                 realmap = &shared_map;
739         }
740         else
741         {
742                 snprintf(mapfilename, sizeof(mapfilename), "%s/%s",
743                                  dbpath, RELMAPPER_FILENAME);
744                 realmap = &local_map;
745         }
746
747         fd = OpenTransientFile(mapfilename, O_WRONLY | O_CREAT | PG_BINARY);
748         if (fd < 0)
749                 ereport(ERROR,
750                                 (errcode_for_file_access(),
751                                  errmsg("could not open relation mapping file \"%s\": %m",
752                                                 mapfilename)));
753
754         if (write_wal)
755         {
756                 xl_relmap_update xlrec;
757                 XLogRecPtr      lsn;
758
759                 /* now errors are fatal ... */
760                 START_CRIT_SECTION();
761
762                 xlrec.dbid = dbid;
763                 xlrec.tsid = tsid;
764                 xlrec.nbytes = sizeof(RelMapFile);
765
766                 XLogBeginInsert();
767                 XLogRegisterData((char *) (&xlrec), MinSizeOfRelmapUpdate);
768                 XLogRegisterData((char *) newmap, sizeof(RelMapFile));
769
770                 lsn = XLogInsert(RM_RELMAP_ID, XLOG_RELMAP_UPDATE);
771
772                 /* As always, WAL must hit the disk before the data update does */
773                 XLogFlush(lsn);
774         }
775
776         errno = 0;
777         pgstat_report_wait_start(WAIT_EVENT_RELATION_MAP_WRITE);
778         if (write(fd, newmap, sizeof(RelMapFile)) != sizeof(RelMapFile))
779         {
780                 /* if write didn't set errno, assume problem is no disk space */
781                 if (errno == 0)
782                         errno = ENOSPC;
783                 ereport(ERROR,
784                                 (errcode_for_file_access(),
785                                  errmsg("could not write to relation mapping file \"%s\": %m",
786                                                 mapfilename)));
787         }
788         pgstat_report_wait_end();
789
790         /*
791          * We choose to fsync the data to disk before considering the task done.
792          * It would be possible to relax this if it turns out to be a performance
793          * issue, but it would complicate checkpointing --- see notes for
794          * CheckPointRelationMap.
795          */
796         pgstat_report_wait_start(WAIT_EVENT_RELATION_MAP_SYNC);
797         if (pg_fsync(fd) != 0)
798                 ereport(ERROR,
799                                 (errcode_for_file_access(),
800                                  errmsg("could not fsync relation mapping file \"%s\": %m",
801                                                 mapfilename)));
802         pgstat_report_wait_end();
803
804         if (CloseTransientFile(fd))
805                 ereport(ERROR,
806                                 (errcode_for_file_access(),
807                                  errmsg("could not close relation mapping file \"%s\": %m",
808                                                 mapfilename)));
809
810         /*
811          * Now that the file is safely on disk, send sinval message to let other
812          * backends know to re-read it.  We must do this inside the critical
813          * section: if for some reason we fail to send the message, we have to
814          * force a database-wide PANIC.  Otherwise other backends might continue
815          * execution with stale mapping information, which would be catastrophic
816          * as soon as others began to use the now-committed data.
817          */
818         if (send_sinval)
819                 CacheInvalidateRelmap(dbid);
820
821         /*
822          * Make sure that the files listed in the map are not deleted if the outer
823          * transaction aborts.  This had better be within the critical section
824          * too: it's not likely to fail, but if it did, we'd arrive at transaction
825          * abort with the files still vulnerable.  PANICing will leave things in a
826          * good state on-disk.
827          *
828          * Note: we're cheating a little bit here by assuming that mapped files
829          * are either in pg_global or the database's default tablespace.
830          */
831         if (preserve_files)
832         {
833                 int32           i;
834
835                 for (i = 0; i < newmap->num_mappings; i++)
836                 {
837                         RelFileNode rnode;
838
839                         rnode.spcNode = tsid;
840                         rnode.dbNode = dbid;
841                         rnode.relNode = newmap->mappings[i].mapfilenode;
842                         RelationPreserveStorage(rnode, false);
843                 }
844         }
845
846         /* Success, update permanent copy */
847         memcpy(realmap, newmap, sizeof(RelMapFile));
848
849         /* Critical section done */
850         if (write_wal)
851                 END_CRIT_SECTION();
852 }
853
854 /*
855  * Merge the specified updates into the appropriate "real" map,
856  * and write out the changes.  This function must be used for committing
857  * updates during normal multiuser operation.
858  */
859 static void
860 perform_relmap_update(bool shared, const RelMapFile *updates)
861 {
862         RelMapFile      newmap;
863
864         /*
865          * Anyone updating a relation's mapping info should take exclusive lock on
866          * that rel and hold it until commit.  This ensures that there will not be
867          * concurrent updates on the same mapping value; but there could easily be
868          * concurrent updates on different values in the same file. We cover that
869          * by acquiring the RelationMappingLock, re-reading the target file to
870          * ensure it's up to date, applying the updates, and writing the data
871          * before releasing RelationMappingLock.
872          *
873          * There is only one RelationMappingLock.  In principle we could try to
874          * have one per mapping file, but it seems unlikely to be worth the
875          * trouble.
876          */
877         LWLockAcquire(RelationMappingLock, LW_EXCLUSIVE);
878
879         /* Be certain we see any other updates just made */
880         load_relmap_file(shared);
881
882         /* Prepare updated data in a local variable */
883         if (shared)
884                 memcpy(&newmap, &shared_map, sizeof(RelMapFile));
885         else
886                 memcpy(&newmap, &local_map, sizeof(RelMapFile));
887
888         /*
889          * Apply the updates to newmap.  No new mappings should appear, unless
890          * somebody is adding indexes to system catalogs.
891          */
892         merge_map_updates(&newmap, updates, allowSystemTableMods);
893
894         /* Write out the updated map and do other necessary tasks */
895         write_relmap_file(shared, &newmap, true, true, true,
896                                           (shared ? InvalidOid : MyDatabaseId),
897                                           (shared ? GLOBALTABLESPACE_OID : MyDatabaseTableSpace),
898                                           DatabasePath);
899
900         /* Now we can release the lock */
901         LWLockRelease(RelationMappingLock);
902 }
903
904 /*
905  * RELMAP resource manager's routines
906  */
907 void
908 relmap_redo(XLogReaderState *record)
909 {
910         uint8           info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
911
912         /* Backup blocks are not used in relmap records */
913         Assert(!XLogRecHasAnyBlockRefs(record));
914
915         if (info == XLOG_RELMAP_UPDATE)
916         {
917                 xl_relmap_update *xlrec = (xl_relmap_update *) XLogRecGetData(record);
918                 RelMapFile      newmap;
919                 char       *dbpath;
920
921                 if (xlrec->nbytes != sizeof(RelMapFile))
922                         elog(PANIC, "relmap_redo: wrong size %u in relmap update record",
923                                  xlrec->nbytes);
924                 memcpy(&newmap, xlrec->data, sizeof(newmap));
925
926                 /* We need to construct the pathname for this database */
927                 dbpath = GetDatabasePath(xlrec->dbid, xlrec->tsid);
928
929                 /*
930                  * Write out the new map and send sinval, but of course don't write a
931                  * new WAL entry.  There's no surrounding transaction to tell to
932                  * preserve files, either.
933                  *
934                  * There shouldn't be anyone else updating relmaps during WAL replay,
935                  * so we don't bother to take the RelationMappingLock.  We would need
936                  * to do so if load_relmap_file needed to interlock against writers.
937                  */
938                 write_relmap_file((xlrec->dbid == InvalidOid), &newmap,
939                                                   false, true, false,
940                                                   xlrec->dbid, xlrec->tsid, dbpath);
941
942                 pfree(dbpath);
943         }
944         else
945                 elog(PANIC, "relmap_redo: unknown op code %u", info);
946 }