Don't ERROR, just LOG problems during replication slot checkpoints
authorAndres Freund <[email protected]>
Tue, 28 Jan 2014 21:48:36 +0000 (22:48 +0100)
committerAndres Freund <[email protected]>
Tue, 28 Jan 2014 21:59:54 +0000 (22:59 +0100)
src/backend/replication/slot.c

index 91ebc231ce37c9a80a62c0c0a2925c0e80bf2fc2..e6f2183e9b464c647d30569c2420ec0b827d3c50 100644 (file)
@@ -85,7 +85,7 @@ int                   max_replication_slots = 0;      /* the maximum number of replication slots */
 /* 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.
@@ -489,7 +489,7 @@ ReplicationSlotSave(void)
        Assert(MyReplicationSlot != NULL);
 
        sprintf(path, "pg_replslot/%s", NameStr(MyReplicationSlot->data.name));
-       SaveSlotToPath(MyReplicationSlot, path);
+       SaveSlotToPath(MyReplicationSlot, path, ERROR);
 }
 
 /*
@@ -660,7 +660,7 @@ CheckPointReplicationSlots(void)
 
                /* 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);
 }
@@ -754,7 +754,7 @@ CreateSlotOnDisk(ReplicationSlot *slot)
        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,
@@ -779,7 +779,7 @@ CreateSlotOnDisk(ReplicationSlot *slot)
  * 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];
@@ -797,6 +797,7 @@ SaveSlotToPath(ReplicationSlot *slot, const char *dir)
                SpinLockRelease(&vslot->mutex);
        }
 
+       /* and don't do anything if there's nothing to write */
        if (!was_dirty)
                return;
 
@@ -812,10 +813,13 @@ SaveSlotToPath(ReplicationSlot *slot, const char *dir)
                                                   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);
@@ -834,21 +838,27 @@ SaveSlotToPath(ReplicationSlot *slot, const char *dir)
 
        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);
@@ -856,10 +866,11 @@ SaveSlotToPath(ReplicationSlot *slot, const char *dir)
        /* 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. */