Skip to content

Commit 0c735c6

Browse files
committed
Don't call palloc() while holding a spinlock, either.
Fix some more violations of the "only straight-line code inside a spinlock" rule. These are hazardous not only because they risk holding the lock for an excessively long time, but because it's possible for palloc to throw elog(ERROR), leaving a stuck spinlock behind. copy_replication_slot() had two separate places that did pallocs while holding a spinlock. We can make the code simpler and safer by copying the whole ReplicationSlot struct into a local variable while holding the spinlock, and then referencing that copy. (While that's arguably more cycles than we really need to spend holding the lock, the struct isn't all that big, and this way seems far more maintainable than copying fields piecemeal. Anyway this is surely much cheaper than a palloc.) That bug goes back to v12. InvalidateObsoleteReplicationSlots() not only did a palloc while holding a spinlock, but for extra sloppiness then leaked the memory --- probably for the lifetime of the checkpointer process, though I didn't try to verify that. Fortunately that silliness is new in HEAD. pg_get_replication_slots() had a cosmetic violation of the rule, in that it only assumed it's safe to call namecpy() while holding a spinlock. Still, that's a hazard waiting to bite somebody, and there were some other cosmetic coding-rule violations in the same function, so clean it up. I back-patched this as far as v10; the code exists before that but it looks different, and this didn't seem important enough to adapt the patch further back. Discussion: https://postgr.es/m/20200602.161518.1399689010416646074.horikyota.ntt@gmail.com
1 parent a36f216 commit 0c735c6

File tree

2 files changed

+27
-41
lines changed

2 files changed

+27
-41
lines changed

src/backend/replication/slotfuncs.c

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -225,87 +225,73 @@ pg_get_replication_slots(PG_FUNCTION_ARGS)
225225
for (slotno = 0; slotno < max_replication_slots; slotno++)
226226
{
227227
ReplicationSlot *slot = &ReplicationSlotCtl->replication_slots[slotno];
228+
ReplicationSlot slot_contents;
228229
Datum values[PG_GET_REPLICATION_SLOTS_COLS];
229230
bool nulls[PG_GET_REPLICATION_SLOTS_COLS];
230-
231-
ReplicationSlotPersistency persistency;
232-
TransactionId xmin;
233-
TransactionId catalog_xmin;
234-
XLogRecPtr restart_lsn;
235-
XLogRecPtr confirmed_flush_lsn;
236-
pid_t active_pid;
237-
Oid database;
238-
NameData slot_name;
239-
NameData plugin;
240231
int i;
241232

242233
if (!slot->in_use)
243234
continue;
244235

236+
/* Copy slot contents while holding spinlock, then examine at leisure */
245237
SpinLockAcquire(&slot->mutex);
246-
247-
xmin = slot->data.xmin;
248-
catalog_xmin = slot->data.catalog_xmin;
249-
database = slot->data.database;
250-
restart_lsn = slot->data.restart_lsn;
251-
confirmed_flush_lsn = slot->data.confirmed_flush;
252-
namecpy(&slot_name, &slot->data.name);
253-
namecpy(&plugin, &slot->data.plugin);
254-
active_pid = slot->active_pid;
255-
persistency = slot->data.persistency;
256-
238+
slot_contents = *slot;
257239
SpinLockRelease(&slot->mutex);
258240

241+
memset(values, 0, sizeof(values));
259242
memset(nulls, 0, sizeof(nulls));
260243

261244
i = 0;
262-
values[i++] = NameGetDatum(&slot_name);
245+
values[i++] = NameGetDatum(&slot_contents.data.name);
263246

264-
if (database == InvalidOid)
247+
if (slot_contents.data.database == InvalidOid)
265248
nulls[i++] = true;
266249
else
267-
values[i++] = NameGetDatum(&plugin);
250+
values[i++] = NameGetDatum(&slot_contents.data.plugin);
268251

269-
if (database == InvalidOid)
252+
if (slot_contents.data.database == InvalidOid)
270253
values[i++] = CStringGetTextDatum("physical");
271254
else
272255
values[i++] = CStringGetTextDatum("logical");
273256

274-
if (database == InvalidOid)
257+
if (slot_contents.data.database == InvalidOid)
275258
nulls[i++] = true;
276259
else
277-
values[i++] = database;
260+
values[i++] = ObjectIdGetDatum(slot_contents.data.database);
278261

279-
values[i++] = BoolGetDatum(persistency == RS_TEMPORARY);
280-
values[i++] = BoolGetDatum(active_pid != 0);
262+
values[i++] = BoolGetDatum(slot_contents.data.persistency == RS_TEMPORARY);
263+
values[i++] = BoolGetDatum(slot_contents.active_pid != 0);
281264

282-
if (active_pid != 0)
283-
values[i++] = Int32GetDatum(active_pid);
265+
if (slot_contents.active_pid != 0)
266+
values[i++] = Int32GetDatum(slot_contents.active_pid);
284267
else
285268
nulls[i++] = true;
286269

287-
if (xmin != InvalidTransactionId)
288-
values[i++] = TransactionIdGetDatum(xmin);
270+
if (slot_contents.data.xmin != InvalidTransactionId)
271+
values[i++] = TransactionIdGetDatum(slot_contents.data.xmin);
289272
else
290273
nulls[i++] = true;
291274

292-
if (catalog_xmin != InvalidTransactionId)
293-
values[i++] = TransactionIdGetDatum(catalog_xmin);
275+
if (slot_contents.data.catalog_xmin != InvalidTransactionId)
276+
values[i++] = TransactionIdGetDatum(slot_contents.data.catalog_xmin);
294277
else
295278
nulls[i++] = true;
296279

297-
if (restart_lsn != InvalidXLogRecPtr)
298-
values[i++] = LSNGetDatum(restart_lsn);
280+
if (slot_contents.data.restart_lsn != InvalidXLogRecPtr)
281+
values[i++] = LSNGetDatum(slot_contents.data.restart_lsn);
299282
else
300283
nulls[i++] = true;
301284

302-
if (confirmed_flush_lsn != InvalidXLogRecPtr)
303-
values[i++] = LSNGetDatum(confirmed_flush_lsn);
285+
if (slot_contents.data.confirmed_flush != InvalidXLogRecPtr)
286+
values[i++] = LSNGetDatum(slot_contents.data.confirmed_flush);
304287
else
305288
nulls[i++] = true;
306289

290+
Assert(i == PG_GET_REPLICATION_SLOTS_COLS);
291+
307292
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
308293
}
294+
309295
LWLockRelease(ReplicationSlotControlLock);
310296

311297
tuplestore_donestoring(tupstore);

src/include/replication/slot.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ typedef struct ReplicationSlot
138138
XLogRecPtr candidate_restart_lsn;
139139
} ReplicationSlot;
140140

141-
#define SlotIsPhysical(slot) (slot->data.database == InvalidOid)
142-
#define SlotIsLogical(slot) (slot->data.database != InvalidOid)
141+
#define SlotIsPhysical(slot) ((slot)->data.database == InvalidOid)
142+
#define SlotIsLogical(slot) ((slot)->data.database != InvalidOid)
143143

144144
/*
145145
* Shared memory control area for all of replication slots.

0 commit comments

Comments
 (0)