Properly re-initialize replication slot shared memory upon creation.
authorAndres Freund
Wed, 17 Aug 2016 20:15:04 +0000 (13:15 -0700)
committerAndres Freund
Wed, 17 Aug 2016 20:15:04 +0000 (13:15 -0700)
Slot creation did not clear all fields upon creation. After start the
memory is zeroed, but when a physical replication slot was created in
the shared memory of a previously existing logical slot, catalog_xmin
would not be cleared. That in turn would prevent vacuum from doing its
duties.

To fix initialize all the fields. To make similar future bugs less
likely, zero all of ReplicationSlotPersistentData, and re-order the
rest of the initialization to be in struct member order.

Analysis: Andrew Gierth
Reported-By: [email protected]
Author: Michael Paquier
Discussion: <20160705173502[email protected]>
Backpatch: 9.4, where replication slots were introduced

src/backend/replication/slot.c

index 839ead0e330d287fecc375018dca7c5b329562dd..b8761e4f88fbe902397351587f04fb68e223624b 100644 (file)
@@ -263,13 +263,22 @@ ReplicationSlotCreate(const char *name, bool db_specific,
     */
    Assert(!slot->in_use);
    Assert(!slot->active);
+
+   /* first initialize persistent data */
+   memset(&slot->data, 0, sizeof(ReplicationSlotPersistentData));
+   StrNCpy(NameStr(slot->data.name), name, NAMEDATALEN);
+   slot->data.database = db_specific ? MyDatabaseId : InvalidOid;
    slot->data.persistency = persistency;
-   slot->data.xmin = InvalidTransactionId;
+
+   /* and then data only present in shared memory */
+   slot->just_dirtied = false;
+   slot->dirty = false;
    slot->effective_xmin = InvalidTransactionId;
-   strncpy(NameStr(slot->data.name), name, NAMEDATALEN);
-   NameStr(slot->data.name)[NAMEDATALEN - 1] = '\0';
-   slot->data.database = db_specific ? MyDatabaseId : InvalidOid;
-   slot->data.restart_lsn = InvalidXLogRecPtr;
+   slot->effective_catalog_xmin = InvalidTransactionId;
+   slot->candidate_catalog_xmin = InvalidTransactionId;
+   slot->candidate_xmin_lsn = InvalidXLogRecPtr;
+   slot->candidate_restart_valid = InvalidXLogRecPtr;
+   slot->candidate_restart_lsn = InvalidXLogRecPtr;
 
    /*
     * Create the slot on disk.  We haven't actually marked the slot allocated