PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
relmapper.c
Go to the documentation of this file.
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 "storage/fd.h"
54 #include "storage/lwlock.h"
55 #include "utils/inval.h"
56 #include "utils/relmapper.h"
57 
58 
59 /*
60  * The map file is critical data: we have no automatic method for recovering
61  * from loss or corruption of it. We use a CRC so that we can detect
62  * corruption. To minimize the risk of failed updates, the map file should
63  * be kept to no more than one standard-size disk sector (ie 512 bytes),
64  * and we use overwrite-in-place rather than playing renaming games.
65  * The struct layout below is designed to occupy exactly 512 bytes, which
66  * might make filesystem updates a bit more efficient.
67  *
68  * Entries in the mappings[] array are in no particular order. We could
69  * speed searching by insisting on OID order, but it really shouldn't be
70  * worth the trouble given the intended size of the mapping sets.
71  */
72 #define RELMAPPER_FILENAME "pg_filenode.map"
73 
74 #define RELMAPPER_FILEMAGIC 0x592717 /* version ID value */
75 
76 #define MAX_MAPPINGS 62 /* 62 * 8 + 16 = 512 */
77 
78 typedef struct RelMapping
79 {
80  Oid mapoid; /* OID of a catalog */
81  Oid mapfilenode; /* its filenode number */
82 } RelMapping;
83 
84 typedef struct RelMapFile
85 {
86  int32 magic; /* always RELMAPPER_FILEMAGIC */
87  int32 num_mappings; /* number of valid RelMapping entries */
89  pg_crc32c crc; /* CRC of all above */
90  int32 pad; /* to make the struct size be 512 exactly */
91 } RelMapFile;
92 
93 /*
94  * The currently known contents of the shared map file and our database's
95  * local map file are stored here. These can be reloaded from disk
96  * immediately whenever we receive an update sinval message.
97  */
100 
101 /*
102  * We use the same RelMapFile data structure to track uncommitted local
103  * changes in the mappings (but note the magic and crc fields are not made
104  * valid in these variables). Currently, map updates are not allowed within
105  * subtransactions, so one set of transaction-level changes is sufficient.
106  *
107  * The active_xxx variables contain updates that are valid in our transaction
108  * and should be honored by RelationMapOidToFilenode. The pending_xxx
109  * variables contain updates we have been told about that aren't active yet;
110  * they will become active at the next CommandCounterIncrement. This setup
111  * lets map updates act similarly to updates of pg_class rows, ie, they
112  * become visible only at the next CommandCounterIncrement boundary.
113  */
118 
119 
120 /* non-export function prototypes */
121 static void apply_map_update(RelMapFile *map, Oid relationId, Oid fileNode,
122  bool add_okay);
123 static void merge_map_updates(RelMapFile *map, const RelMapFile *updates,
124  bool add_okay);
125 static void load_relmap_file(bool shared);
126 static void write_relmap_file(bool shared, RelMapFile *newmap,
127  bool write_wal, bool send_sinval, bool preserve_files,
128  Oid dbid, Oid tsid, const char *dbpath);
129 static void perform_relmap_update(bool shared, const RelMapFile *updates);
130 
131 
132 /*
133  * RelationMapOidToFilenode
134  *
135  * The raison d' etre ... given a relation OID, look up its filenode.
136  *
137  * Although shared and local relation OIDs should never overlap, the caller
138  * always knows which we need --- so pass that information to avoid useless
139  * searching.
140  *
141  * Returns InvalidOid if the OID is not known (which should never happen,
142  * but the caller is in a better position to report a meaningful error).
143  */
144 Oid
145 RelationMapOidToFilenode(Oid relationId, bool shared)
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 }
184 
185 /*
186  * RelationMapFilenodeToOid
187  *
188  * Do the reverse of the normal direction of mapping done in
189  * RelationMapOidToFilenode.
190  *
191  * This is not supposed to be used during normal running but rather for
192  * information purposes when looking at the filesystem or xlog.
193  *
194  * Returns InvalidOid if the OID is not known; this can easily happen if the
195  * relfilenode doesn't pertain to a mapped relation.
196  */
197 Oid
198 RelationMapFilenodeToOid(Oid filenode, bool shared)
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 }
237 
238 /*
239  * RelationMapUpdateMap
240  *
241  * Install a new relfilenode mapping for the specified relation.
242  *
243  * If immediate is true (or we're bootstrapping), the mapping is activated
244  * immediately. Otherwise it is made pending until CommandCounterIncrement.
245  */
246 void
247 RelationMapUpdateMap(Oid relationId, Oid fileNode, bool shared,
248  bool immediate)
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 }
291 
292 /*
293  * apply_map_update
294  *
295  * Insert a new mapping into the given map variable, replacing any existing
296  * mapping for the same relation.
297  *
298  * In some cases the caller knows there must be an existing mapping; pass
299  * add_okay = false to draw an error if not.
300  */
301 static void
302 apply_map_update(RelMapFile *map, Oid relationId, Oid fileNode, bool add_okay)
303 {
304  int32 i;
305 
306  /* Replace any existing mapping */
307  for (i = 0; i < map->num_mappings; i++)
308  {
309  if (relationId == map->mappings[i].mapoid)
310  {
311  map->mappings[i].mapfilenode = fileNode;
312  return;
313  }
314  }
315 
316  /* Nope, need to add a new mapping */
317  if (!add_okay)
318  elog(ERROR, "attempt to apply a mapping to unmapped relation %u",
319  relationId);
320  if (map->num_mappings >= MAX_MAPPINGS)
321  elog(ERROR, "ran out of space in relation map");
322  map->mappings[map->num_mappings].mapoid = relationId;
323  map->mappings[map->num_mappings].mapfilenode = fileNode;
324  map->num_mappings++;
325 }
326 
327 /*
328  * merge_map_updates
329  *
330  * Merge all the updates in the given pending-update map into the target map.
331  * This is just a bulk form of apply_map_update.
332  */
333 static void
334 merge_map_updates(RelMapFile *map, const RelMapFile *updates, bool add_okay)
335 {
336  int32 i;
337 
338  for (i = 0; i < updates->num_mappings; i++)
339  {
340  apply_map_update(map,
341  updates->mappings[i].mapoid,
342  updates->mappings[i].mapfilenode,
343  add_okay);
344  }
345 }
346 
347 /*
348  * RelationMapRemoveMapping
349  *
350  * Remove a relation's entry in the map. This is only allowed for "active"
351  * (but not committed) local mappings. We need it so we can back out the
352  * entry for the transient target file when doing VACUUM FULL/CLUSTER on
353  * a mapped relation.
354  */
355 void
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 }
374 
375 /*
376  * RelationMapInvalidate
377  *
378  * This routine is invoked for SI cache flush messages. We must re-read
379  * the indicated map file. However, we might receive a SI message in a
380  * process that hasn't yet, and might never, load the mapping files;
381  * for example the autovacuum launcher, which *must not* try to read
382  * a local map since it is attached to no particular database.
383  * So, re-read only if the map is valid now.
384  */
385 void
387 {
388  if (shared)
389  {
390  if (shared_map.magic == RELMAPPER_FILEMAGIC)
391  load_relmap_file(true);
392  }
393  else
394  {
395  if (local_map.magic == RELMAPPER_FILEMAGIC)
396  load_relmap_file(false);
397  }
398 }
399 
400 /*
401  * RelationMapInvalidateAll
402  *
403  * Reload all map files. This is used to recover from SI message buffer
404  * overflow: we can't be sure if we missed an inval message.
405  * Again, reload only currently-valid maps.
406  */
407 void
409 {
410  if (shared_map.magic == RELMAPPER_FILEMAGIC)
411  load_relmap_file(true);
412  if (local_map.magic == RELMAPPER_FILEMAGIC)
413  load_relmap_file(false);
414 }
415 
416 /*
417  * AtCCI_RelationMap
418  *
419  * Activate any "pending" relation map updates at CommandCounterIncrement time.
420  */
421 void
423 {
424  if (pending_shared_updates.num_mappings != 0)
425  {
426  merge_map_updates(&active_shared_updates,
427  &pending_shared_updates,
428  true);
429  pending_shared_updates.num_mappings = 0;
430  }
431  if (pending_local_updates.num_mappings != 0)
432  {
433  merge_map_updates(&active_local_updates,
434  &pending_local_updates,
435  true);
436  pending_local_updates.num_mappings = 0;
437  }
438 }
439 
440 /*
441  * AtEOXact_RelationMap
442  *
443  * Handle relation mapping at main-transaction commit or abort.
444  *
445  * During commit, this must be called as late as possible before the actual
446  * transaction commit, so as to minimize the window where the transaction
447  * could still roll back after committing map changes. Although nothing
448  * critically bad happens in such a case, we still would prefer that it
449  * not happen, since we'd possibly be losing useful updates to the relations'
450  * pg_class row(s).
451  *
452  * During abort, we just have to throw away any pending map changes.
453  * Normal post-abort cleanup will take care of fixing relcache entries.
454  */
455 void
456 AtEOXact_RelationMap(bool isCommit)
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  */
465  Assert(pending_shared_updates.num_mappings == 0);
466  Assert(pending_local_updates.num_mappings == 0);
467 
468  /*
469  * Write any active updates to the actual map files, then reset them.
470  */
471  if (active_shared_updates.num_mappings != 0)
472  {
473  perform_relmap_update(true, &active_shared_updates);
474  active_shared_updates.num_mappings = 0;
475  }
476  if (active_local_updates.num_mappings != 0)
477  {
478  perform_relmap_update(false, &active_local_updates);
479  active_local_updates.num_mappings = 0;
480  }
481  }
482  else
483  {
484  /* Abort --- drop all local and pending updates */
485  active_shared_updates.num_mappings = 0;
486  active_local_updates.num_mappings = 0;
487  pending_shared_updates.num_mappings = 0;
488  pending_local_updates.num_mappings = 0;
489  }
490 }
491 
492 /*
493  * AtPrepare_RelationMap
494  *
495  * Handle relation mapping at PREPARE.
496  *
497  * Currently, we don't support preparing any transaction that changes the map.
498  */
499 void
501 {
502  if (active_shared_updates.num_mappings != 0 ||
503  active_local_updates.num_mappings != 0 ||
504  pending_shared_updates.num_mappings != 0 ||
505  pending_local_updates.num_mappings != 0)
506  ereport(ERROR,
507  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
508  errmsg("cannot PREPARE a transaction that modified relation mapping")));
509 }
510 
511 /*
512  * CheckPointRelationMap
513  *
514  * This is called during a checkpoint. It must ensure that any relation map
515  * updates that were WAL-logged before the start of the checkpoint are
516  * securely flushed to disk and will not need to be replayed later. This
517  * seems unlikely to be a performance-critical issue, so we use a simple
518  * method: we just take and release the RelationMappingLock. This ensures
519  * that any already-logged map update is complete, because write_relmap_file
520  * will fsync the map file before the lock is released.
521  */
522 void
524 {
525  LWLockAcquire(RelationMappingLock, LW_SHARED);
526  LWLockRelease(RelationMappingLock);
527 }
528 
529 /*
530  * RelationMapFinishBootstrap
531  *
532  * Write out the initial relation mapping files at the completion of
533  * bootstrap. All the mapped files should have been made known to us
534  * via RelationMapUpdateMap calls.
535  */
536 void
538 {
540 
541  /* Shouldn't be anything "pending" ... */
542  Assert(active_shared_updates.num_mappings == 0);
543  Assert(active_local_updates.num_mappings == 0);
544  Assert(pending_shared_updates.num_mappings == 0);
545  Assert(pending_local_updates.num_mappings == 0);
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 }
553 
554 /*
555  * RelationMapInitialize
556  *
557  * This initializes the mapper module at process startup. We can't access the
558  * database yet, so just make sure the maps are empty.
559  */
560 void
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;
566  shared_map.num_mappings = 0;
567  local_map.num_mappings = 0;
568  active_shared_updates.num_mappings = 0;
569  active_local_updates.num_mappings = 0;
570  pending_shared_updates.num_mappings = 0;
571  pending_local_updates.num_mappings = 0;
572 }
573 
574 /*
575  * RelationMapInitializePhase2
576  *
577  * This is called to prepare for access to pg_database during startup.
578  * We should be able to read the shared map file now.
579  */
580 void
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 }
594 
595 /*
596  * RelationMapInitializePhase3
597  *
598  * This is called as soon as we have determined MyDatabaseId and set up
599  * DatabasePath. At this point we should be able to read the local map file.
600  */
601 void
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 }
615 
616 /*
617  * load_relmap_file -- load data from the shared or local map file
618  *
619  * Because the map file is essential for access to core system catalogs,
620  * failure to read it is a fatal error.
621  *
622  * Note that the local case requires DatabasePath to be set up.
623  */
624 static void
625 load_relmap_file(bool shared)
626 {
627  RelMapFile *map;
628  char mapfilename[MAXPGPATH];
629  pg_crc32c crc;
630  int fd;
631 
632  if (shared)
633  {
634  snprintf(mapfilename, sizeof(mapfilename), "global/%s",
636  map = &shared_map;
637  }
638  else
639  {
640  snprintf(mapfilename, sizeof(mapfilename), "%s/%s",
642  map = &local_map;
643  }
644 
645  /* Read data ... */
646  fd = OpenTransientFile(mapfilename,
647  O_RDONLY | PG_BINARY, S_IRUSR | S_IWUSR);
648  if (fd < 0)
649  ereport(FATAL,
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  if (read(fd, map, sizeof(RelMapFile)) != sizeof(RelMapFile))
662  ereport(FATAL,
664  errmsg("could not read relation mapping file \"%s\": %m",
665  mapfilename)));
666 
667  CloseTransientFile(fd);
668 
669  /* check for correct magic number, etc */
670  if (map->magic != RELMAPPER_FILEMAGIC ||
671  map->num_mappings < 0 ||
672  map->num_mappings > MAX_MAPPINGS)
673  ereport(FATAL,
674  (errmsg("relation mapping file \"%s\" contains invalid data",
675  mapfilename)));
676 
677  /* verify the CRC */
678  INIT_CRC32C(crc);
679  COMP_CRC32C(crc, (char *) map, offsetof(RelMapFile, crc));
680  FIN_CRC32C(crc);
681 
682  if (!EQ_CRC32C(crc, map->crc))
683  ereport(FATAL,
684  (errmsg("relation mapping file \"%s\" contains incorrect checksum",
685  mapfilename)));
686 }
687 
688 /*
689  * Write out a new shared or local map file with the given contents.
690  *
691  * The magic number and CRC are automatically updated in *newmap. On
692  * success, we copy the data to the appropriate permanent static variable.
693  *
694  * If write_wal is TRUE then an appropriate WAL message is emitted.
695  * (It will be false for bootstrap and WAL replay cases.)
696  *
697  * If send_sinval is TRUE then a SI invalidation message is sent.
698  * (This should be true except in bootstrap case.)
699  *
700  * If preserve_files is TRUE then the storage manager is warned not to
701  * delete the files listed in the map.
702  *
703  * Because this may be called during WAL replay when MyDatabaseId,
704  * DatabasePath, etc aren't valid, we require the caller to pass in suitable
705  * values. The caller is also responsible for being sure no concurrent
706  * map update could be happening.
707  */
708 static void
709 write_relmap_file(bool shared, RelMapFile *newmap,
710  bool write_wal, bool send_sinval, bool preserve_files,
711  Oid dbid, Oid tsid, const char *dbpath)
712 {
713  int fd;
714  RelMapFile *realmap;
715  char mapfilename[MAXPGPATH];
716 
717  /*
718  * Fill in the overhead fields and update CRC.
719  */
720  newmap->magic = RELMAPPER_FILEMAGIC;
721  if (newmap->num_mappings < 0 || newmap->num_mappings > MAX_MAPPINGS)
722  elog(ERROR, "attempt to write bogus relation mapping");
723 
724  INIT_CRC32C(newmap->crc);
725  COMP_CRC32C(newmap->crc, (char *) newmap, offsetof(RelMapFile, crc));
726  FIN_CRC32C(newmap->crc);
727 
728  /*
729  * Open the target file. We prefer to do this before entering the
730  * critical section, so that an open() failure need not force PANIC.
731  */
732  if (shared)
733  {
734  snprintf(mapfilename, sizeof(mapfilename), "global/%s",
736  realmap = &shared_map;
737  }
738  else
739  {
740  snprintf(mapfilename, sizeof(mapfilename), "%s/%s",
741  dbpath, RELMAPPER_FILENAME);
742  realmap = &local_map;
743  }
744 
745  fd = OpenTransientFile(mapfilename,
746  O_WRONLY | O_CREAT | PG_BINARY,
747  S_IRUSR | S_IWUSR);
748  if (fd < 0)
749  ereport(ERROR,
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 ... */
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  if (write(fd, newmap, sizeof(RelMapFile)) != sizeof(RelMapFile))
778  {
779  /* if write didn't set errno, assume problem is no disk space */
780  if (errno == 0)
781  errno = ENOSPC;
782  ereport(ERROR,
784  errmsg("could not write to relation mapping file \"%s\": %m",
785  mapfilename)));
786  }
787 
788  /*
789  * We choose to fsync the data to disk before considering the task done.
790  * It would be possible to relax this if it turns out to be a performance
791  * issue, but it would complicate checkpointing --- see notes for
792  * CheckPointRelationMap.
793  */
794  if (pg_fsync(fd) != 0)
795  ereport(ERROR,
797  errmsg("could not fsync relation mapping file \"%s\": %m",
798  mapfilename)));
799 
800  if (CloseTransientFile(fd))
801  ereport(ERROR,
803  errmsg("could not close relation mapping file \"%s\": %m",
804  mapfilename)));
805 
806  /*
807  * Now that the file is safely on disk, send sinval message to let other
808  * backends know to re-read it. We must do this inside the critical
809  * section: if for some reason we fail to send the message, we have to
810  * force a database-wide PANIC. Otherwise other backends might continue
811  * execution with stale mapping information, which would be catastrophic
812  * as soon as others began to use the now-committed data.
813  */
814  if (send_sinval)
815  CacheInvalidateRelmap(dbid);
816 
817  /*
818  * Make sure that the files listed in the map are not deleted if the outer
819  * transaction aborts. This had better be within the critical section
820  * too: it's not likely to fail, but if it did, we'd arrive at transaction
821  * abort with the files still vulnerable. PANICing will leave things in a
822  * good state on-disk.
823  *
824  * Note: we're cheating a little bit here by assuming that mapped files
825  * are either in pg_global or the database's default tablespace.
826  */
827  if (preserve_files)
828  {
829  int32 i;
830 
831  for (i = 0; i < newmap->num_mappings; i++)
832  {
833  RelFileNode rnode;
834 
835  rnode.spcNode = tsid;
836  rnode.dbNode = dbid;
837  rnode.relNode = newmap->mappings[i].mapfilenode;
838  RelationPreserveStorage(rnode, false);
839  }
840  }
841 
842  /* Success, update permanent copy */
843  memcpy(realmap, newmap, sizeof(RelMapFile));
844 
845  /* Critical section done */
846  if (write_wal)
848 }
849 
850 /*
851  * Merge the specified updates into the appropriate "real" map,
852  * and write out the changes. This function must be used for committing
853  * updates during normal multiuser operation.
854  */
855 static void
856 perform_relmap_update(bool shared, const RelMapFile *updates)
857 {
858  RelMapFile newmap;
859 
860  /*
861  * Anyone updating a relation's mapping info should take exclusive lock on
862  * that rel and hold it until commit. This ensures that there will not be
863  * concurrent updates on the same mapping value; but there could easily be
864  * concurrent updates on different values in the same file. We cover that
865  * by acquiring the RelationMappingLock, re-reading the target file to
866  * ensure it's up to date, applying the updates, and writing the data
867  * before releasing RelationMappingLock.
868  *
869  * There is only one RelationMappingLock. In principle we could try to
870  * have one per mapping file, but it seems unlikely to be worth the
871  * trouble.
872  */
873  LWLockAcquire(RelationMappingLock, LW_EXCLUSIVE);
874 
875  /* Be certain we see any other updates just made */
876  load_relmap_file(shared);
877 
878  /* Prepare updated data in a local variable */
879  if (shared)
880  memcpy(&newmap, &shared_map, sizeof(RelMapFile));
881  else
882  memcpy(&newmap, &local_map, sizeof(RelMapFile));
883 
884  /*
885  * Apply the updates to newmap. No new mappings should appear, unless
886  * somebody is adding indexes to system catalogs.
887  */
888  merge_map_updates(&newmap, updates, allowSystemTableMods);
889 
890  /* Write out the updated map and do other necessary tasks */
891  write_relmap_file(shared, &newmap, true, true, true,
892  (shared ? InvalidOid : MyDatabaseId),
894  DatabasePath);
895 
896  /* Now we can release the lock */
897  LWLockRelease(RelationMappingLock);
898 }
899 
900 /*
901  * RELMAP resource manager's routines
902  */
903 void
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 }
Oid mapoid
Definition: relmapper.c:80
struct RelMapping RelMapping
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
int32 pad
Definition: relmapper.c:90
void RelationPreserveStorage(RelFileNode rnode, bool atCommit)
Definition: storage.c:190
void RelationMapFinishBootstrap(void)
Definition: relmapper.c:537
void RelationMapInitialize(void)
Definition: relmapper.c:561
static void merge_map_updates(RelMapFile *map, const RelMapFile *updates, bool add_okay)
Definition: relmapper.c:334
void RelationMapInitializePhase2(void)
Definition: relmapper.c:581
int32 magic
Definition: relmapper.c:86
#define write(a, b, c)
Definition: win32.h:19
static RelMapFile local_map
Definition: relmapper.c:99
uint32 pg_crc32c
Definition: pg_crc32c.h:38
void AtEOXact_RelationMap(bool isCommit)
Definition: relmapper.c:456
#define END_CRIT_SECTION()
Definition: miscadmin.h:132
#define MinSizeOfRelmapUpdate
Definition: relmapper.h:35
unsigned char uint8
Definition: c.h:263
#define GLOBALTABLESPACE_OID
Definition: pg_tablespace.h:64
void AtPrepare_RelationMap(void)
Definition: relmapper.c:500
#define START_CRIT_SECTION()
Definition: miscadmin.h:130
int errcode(int sqlerrcode)
Definition: elog.c:575
void CacheInvalidateRelmap(Oid databaseId)
Definition: inval.c:1354
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
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
unsigned int Oid
Definition: postgres_ext.h:31
#define MAX_MAPPINGS
Definition: relmapper.c:76
void RelationMapInvalidate(bool shared)
Definition: relmapper.c:386
#define PANIC
Definition: elog.h:53
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2745
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1038
signed int int32
Definition: c.h:253
Oid MyDatabaseTableSpace
Definition: globals.c:78
static RelMapFile pending_shared_updates
Definition: relmapper.c:116
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1714
#define RELMAPPER_FILEMAGIC
Definition: relmapper.c:74
void pfree(void *pointer)
Definition: mcxt.c:992
#define XLogRecGetData(decoder)
Definition: xlogreader.h:202
static RelMapFile shared_map
Definition: relmapper.c:98
#define ERROR
Definition: elog.h:43
pg_crc32c crc
Definition: relmapper.c:89
#define FATAL
Definition: elog.h:52
#define MAXPGPATH
static void perform_relmap_update(bool shared, const RelMapFile *updates)
Definition: relmapper.c:856
static RelMapFile active_local_updates
Definition: relmapper.c:115
void AtCCI_RelationMap(void)
Definition: relmapper.c:422
static RelMapFile pending_local_updates
Definition: relmapper.c:117
int OpenTransientFile(FileName fileName, int fileFlags, int fileMode)
Definition: fd.c:2093
int errcode_for_file_access(void)
Definition: elog.c:598
RelMapping mappings[MAX_MAPPINGS]
Definition: relmapper.c:88
#define EQ_CRC32C(c1, c2)
Definition: pg_crc32c.h:42
#define ereport(elevel, rest)
Definition: elog.h:122
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:198
char * GetDatabasePath(Oid dbNode, Oid spcNode)
Definition: relpath.c:108
#define RELMAPPER_FILENAME
Definition: relmapper.c:72
static RelMapFile active_shared_updates
Definition: relmapper.c:114
static void load_relmap_file(bool shared)
Definition: relmapper.c:625
int CloseTransientFile(int fd)
Definition: fd.c:2254
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:323
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:415
#define XLOG_RELMAP_UPDATE
Definition: relmapper.h:25
Oid RelationMapOidToFilenode(Oid relationId, bool shared)
Definition: relmapper.c:145
Oid MyDatabaseId
Definition: globals.c:76
void relmap_redo(XLogReaderState *record)
Definition: relmapper.c:904
bool allowSystemTableMods
Definition: globals.c:111
#define InvalidOid
Definition: postgres_ext.h:36
Oid RelationMapFilenodeToOid(Oid filenode, bool shared)
Definition: relmapper.c:198
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:760
char * DatabasePath
Definition: globals.c:84
Oid mapfilenode
Definition: relmapper.c:81
#define NULL
Definition: c.h:226
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:671
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
void RelationMapRemoveMapping(Oid relationId)
Definition: relmapper.c:356
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1110
void CheckPointRelationMap(void)
Definition: relmapper.c:523
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:365
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
void RelationMapInvalidateAll(void)
Definition: relmapper.c:408
void RelationMapInitializePhase3(void)
Definition: relmapper.c:602
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:204
int pg_fsync(int fd)
Definition: fd.c:333
#define elog
Definition: elog.h:219
struct RelMapFile RelMapFile
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:73
void XLogBeginInsert(void)
Definition: xloginsert.c:120
char data[FLEXIBLE_ARRAY_MEMBER]
Definition: relmapper.h:32
#define FIN_CRC32C(crc)
Definition: pg_crc32c.h:78
int32 num_mappings
Definition: relmapper.c:87
#define read(a, b, c)
Definition: win32.h:18
#define offsetof(type, field)
Definition: c.h:551
void RelationMapUpdateMap(Oid relationId, Oid fileNode, bool shared, bool immediate)
Definition: relmapper.c:247
static void apply_map_update(RelMapFile *map, Oid relationId, Oid fileNode, bool add_okay)
Definition: relmapper.c:302