Skip to content

Commit 9d66ea5

Browse files
committed
Fix bogus initialization of replication origin shared memory state.
The previous coding zeroed out offsetof(ReplicationStateCtl, states) more bytes than it was entitled to, as a consequence of starting the zeroing from the wrong pointer (or, if you prefer, using the wrong calculation of how much to zero). It's unsurprising that this has not caused any reported problems, since it can be expected that the newly-allocated block is at the end of what we've used in shared memory, and we always make the shmem block substantially bigger than minimally necessary. Nonetheless, this is wrong and it could bite us someday; plus it's a dangerous model for somebody to copy. This dates back to the introduction of this code (commit 5aa2350), so back-patch to all supported branches.
1 parent 09f2752 commit 9d66ea5

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/backend/replication/logical/origin.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ typedef struct ReplicationStateOnDisk
147147

148148
typedef struct ReplicationStateCtl
149149
{
150+
/* Tranche to use for per-origin LWLocks */
150151
int tranche_id;
152+
/* Array of length max_replication_slots */
151153
ReplicationState states[FLEXIBLE_ARRAY_MEMBER];
152154
} ReplicationStateCtl;
153155

@@ -164,6 +166,10 @@ TimestampTz replorigin_session_origin_timestamp = 0;
164166
* max_replication_slots?
165167
*/
166168
static ReplicationState *replication_states;
169+
170+
/*
171+
* Actual shared memory block (replication_states[] is now part of this).
172+
*/
167173
static ReplicationStateCtl *replication_states_ctl;
168174

169175
/*
@@ -479,7 +485,7 @@ ReplicationOriginShmemSize(void)
479485
/*
480486
* XXX: max_replication_slots is arguably the wrong thing to use, as here
481487
* we keep the replay state of *remote* transactions. But for now it seems
482-
* sufficient to reuse it, lest we introduce a separate GUC.
488+
* sufficient to reuse it, rather than introduce a separate GUC.
483489
*/
484490
if (max_replication_slots == 0)
485491
return size;
@@ -509,9 +515,9 @@ ReplicationOriginShmemInit(void)
509515
{
510516
int i;
511517

512-
replication_states_ctl->tranche_id = LWTRANCHE_REPLICATION_ORIGIN;
518+
MemSet(replication_states_ctl, 0, ReplicationOriginShmemSize());
513519

514-
MemSet(replication_states, 0, ReplicationOriginShmemSize());
520+
replication_states_ctl->tranche_id = LWTRANCHE_REPLICATION_ORIGIN;
515521

516522
for (i = 0; i < max_replication_slots; i++)
517523
{

0 commit comments

Comments
 (0)