char buf[16];
/*
- * Prepare version data before starting a critical section.
- *
- * Note that we don't have to copy this from the source database; there's
- * only one legal value.
+ * Note that we don't have to copy version data from the source database;
+ * there's only one legal value.
*/
sprintf(buf, "%s\n", PG_MAJORVERSION);
nbytes = strlen(PG_MAJORVERSION) + 1;
- /* If we are not in WAL replay then write the WAL. */
- if (!isRedo)
- {
- xl_dbase_create_wal_log_rec xlrec;
- XLogRecPtr lsn;
-
- START_CRIT_SECTION();
-
- xlrec.db_id = dbid;
- xlrec.tablespace_id = tsid;
-
- XLogBeginInsert();
- XLogRegisterData((char *) (&xlrec),
- sizeof(xl_dbase_create_wal_log_rec));
-
- lsn = XLogInsert(RM_DBASE_ID, XLOG_DBASE_CREATE_WAL_LOG);
-
- /* As always, WAL must hit the disk before the data update does. */
- XLogFlush(lsn);
- }
-
/* Create database directory. */
if (MakePGDirectory(dbpath) < 0)
{
/* Close the version file. */
CloseTransientFile(fd);
- /* Critical section done. */
+ /* If we are not in WAL replay then write the WAL. */
if (!isRedo)
+ {
+ xl_dbase_create_wal_log_rec xlrec;
+
+ START_CRIT_SECTION();
+
+ xlrec.db_id = dbid;
+ xlrec.tablespace_id = tsid;
+
+ XLogBeginInsert();
+ XLogRegisterData((char *) (&xlrec),
+ sizeof(xl_dbase_create_wal_log_rec));
+
+ (void) XLogInsert(RM_DBASE_ID, XLOG_DBASE_CREATE_WAL_LOG);
+
END_CRIT_SECTION();
+ }
}
/*
* Write the same data into the destination database's relmap file.
*
* No sinval is needed because no one can be connected to the destination
- * database yet. For the same reason, there is no need to acquire
- * RelationMappingLock.
+ * database yet.
*
* There's no point in trying to preserve files here. The new database
* isn't usable yet anyway, and won't ever be if we can't install a relmap
* file.
*/
+ LWLockAcquire(RelationMappingLock, LW_EXCLUSIVE);
write_relmap_file(&map, true, false, false, dbid, tsid, dstdbpath);
+ LWLockRelease(RelationMappingLock);
}
/*
Assert(pending_local_updates.num_mappings == 0);
/* Write the files; no WAL or sinval needed */
+ LWLockAcquire(RelationMappingLock, LW_EXCLUSIVE);
write_relmap_file(&shared_map, false, false, false,
InvalidOid, GLOBALTABLESPACE_OID, "global");
write_relmap_file(&local_map, false, false, false,
MyDatabaseId, MyDatabaseTableSpace, DatabasePath);
+ LWLockRelease(RelationMappingLock);
}
/*
char mapfilename[MAXPGPATH];
char maptempfilename[MAXPGPATH];
+ /*
+ * Even without concurrent use of this map, CheckPointRelationMap() relies
+ * on this locking. Without it, a restore of a base backup taken after
+ * this function's XLogInsert() and before its durable_rename() would not
+ * have the changes. wal_level=minimal doesn't need the lock, but this
+ * isn't performance-critical enough for such a micro-optimization.
+ */
+ Assert(LWLockHeldByMeInMode(RelationMappingLock, LW_EXCLUSIVE));
+
/*
* Fill in the overhead fields and update CRC.
*/