Skip to content

Commit c8b1c95

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 1d84751 commit c8b1c95

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
@@ -148,7 +148,9 @@ typedef struct ReplicationStateOnDisk
148148

149149
typedef struct ReplicationStateCtl
150150
{
151+
/* Tranche to use for per-origin LWLocks */
151152
int tranche_id;
153+
/* Array of length max_replication_slots */
152154
ReplicationState states[FLEXIBLE_ARRAY_MEMBER];
153155
} ReplicationStateCtl;
154156

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

170176
/*
@@ -480,7 +486,7 @@ ReplicationOriginShmemSize(void)
480486
/*
481487
* XXX: max_replication_slots is arguably the wrong thing to use, as here
482488
* we keep the replay state of *remote* transactions. But for now it seems
483-
* sufficient to reuse it, lest we introduce a separate GUC.
489+
* sufficient to reuse it, rather than introduce a separate GUC.
484490
*/
485491
if (max_replication_slots == 0)
486492
return size;
@@ -510,9 +516,9 @@ ReplicationOriginShmemInit(void)
510516
{
511517
int i;
512518

513-
replication_states_ctl->tranche_id = LWTRANCHE_REPLICATION_ORIGIN;
519+
MemSet(replication_states_ctl, 0, ReplicationOriginShmemSize());
514520

515-
MemSet(replication_states, 0, ReplicationOriginShmemSize());
521+
replication_states_ctl->tranche_id = LWTRANCHE_REPLICATION_ORIGIN;
516522

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

0 commit comments

Comments
 (0)