/* internal persistency functions */
static void RestoreSlotFromDisk(const char *name);
static void CreateSlotOnDisk(ReplicationSlot *slot);
-static void SaveSlotToPath(ReplicationSlot *slot, const char *path);
+static void SaveSlotToPath(ReplicationSlot *slot, const char *path, int elevel);
/*
* Report shared-memory space needed by ReplicationSlotShmemInit.
Assert(MyReplicationSlot != NULL);
sprintf(path, "pg_replslot/%s", NameStr(MyReplicationSlot->data.name));
- SaveSlotToPath(MyReplicationSlot, path);
+ SaveSlotToPath(MyReplicationSlot, path, ERROR);
}
/*
/* save the slot to disk, locking is handled in SaveSlotToPath() */
sprintf(path, "pg_replslot/%s", NameStr(s->data.name));
- SaveSlotToPath(s, path);
+ SaveSlotToPath(s, path, LOG);
}
LWLockRelease(ReplicationSlotAllocationLock);
}
fsync_fname(tmppath, true);
slot->dirty = true; /* signal that we really need to write */
- SaveSlotToPath(slot, tmppath);
+ SaveSlotToPath(slot, tmppath, ERROR);
if (rename(tmppath, path) != 0)
ereport(ERROR,
* Shared functionality between saving and creating a replication slot.
*/
static void
-SaveSlotToPath(ReplicationSlot *slot, const char *dir)
+SaveSlotToPath(ReplicationSlot *slot, const char *dir, int elevel)
{
char tmppath[MAXPGPATH];
char path[MAXPGPATH];
SpinLockRelease(&vslot->mutex);
}
+ /* and don't do anything if there's nothing to write */
if (!was_dirty)
return;
O_CREAT | O_EXCL | O_WRONLY | PG_BINARY,
S_IRUSR | S_IWUSR);
if (fd < 0)
- ereport(ERROR,
+ {
+ ereport(elevel,
(errcode_for_file_access(),
errmsg("could not create file \"%s\": %m",
tmppath)));
+ return;
+ }
cp.magic = SLOT_MAGIC;
INIT_CRC32(cp.checksum);
if ((write(fd, &cp, sizeof(cp))) != sizeof(cp))
{
+ int save_errno = errno;
CloseTransientFile(fd);
- ereport(ERROR,
+ errno = save_errno;
+ ereport(elevel,
(errcode_for_file_access(),
errmsg("could not write to file \"%s\": %m",
tmppath)));
+ return;
}
/* fsync the temporary file */
if (pg_fsync(fd) != 0)
{
+ int save_errno = errno;
CloseTransientFile(fd);
- ereport(ERROR,
+ errno = save_errno;
+ ereport(elevel,
(errcode_for_file_access(),
errmsg("could not fsync file \"%s\": %m",
tmppath)));
+ return;
}
CloseTransientFile(fd);
/* rename to permanent file, fsync file and directory */
if (rename(tmppath, path) != 0)
{
- ereport(ERROR,
+ ereport(elevel,
(errcode_for_file_access(),
errmsg("could not rename \"%s\" to \"%s\": %m",
tmppath, path)));
+ return;
}
/* Check CreateSlot() for the reasoning of using a crit. section. */