Skip to content

Commit c28bfb3

Browse files
committed
Fix memory leak in LogStandbySnapshot().
The array allocated by GetRunningTransactionLocks() needs to be pfree'd when we're done with it. Otherwise we leak some memory during each checkpoint, if wal_level = hot_standby. This manifests as memory bloat in the checkpointer process, or in bgwriter in versions before we made the checkpointer separate. Reported and fixed by Naoya Anzai. Back-patch to 9.0 where the issue was introduced. In passing, improve comments for GetRunningTransactionLocks(), and add an Assert that we didn't overrun the palloc'd array.
1 parent bb5a217 commit c28bfb3

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

src/backend/storage/ipc/standby.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -879,16 +879,11 @@ LogStandbySnapshot(void)
879879

880880
/*
881881
* Get details of any AccessExclusiveLocks being held at the moment.
882-
*
883-
* XXX GetRunningTransactionLocks() currently holds a lock on all
884-
* partitions though it is possible to further optimise the locking. By
885-
* reference counting locks and storing the value on the ProcArray entry
886-
* for each backend we can easily tell if any locks need recording without
887-
* trying to acquire the partition locks and scanning the lock table.
888882
*/
889883
locks = GetRunningTransactionLocks(&nlocks);
890884
if (nlocks > 0)
891885
LogAccessExclusiveLocks(nlocks, locks);
886+
pfree(locks);
892887

893888
/*
894889
* Log details of all in-progress transactions. This should be the last

src/backend/storage/lmgr/lock.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2360,18 +2360,26 @@ GetLockStatusData(void)
23602360
}
23612361

23622362
/*
2363-
* Returns a list of currently held AccessExclusiveLocks, for use
2364-
* by GetRunningTransactionData().
2363+
* Returns a list of currently held AccessExclusiveLocks, for use by
2364+
* LogStandbySnapshot(). The result is a palloc'd array,
2365+
* with the number of elements returned into *nlocks.
2366+
*
2367+
* XXX This currently takes a lock on all partitions of the lock table,
2368+
* but it's possible to do better. By reference counting locks and storing
2369+
* the value in the ProcArray entry for each backend we could tell if any
2370+
* locks need recording without having to acquire the partition locks and
2371+
* scan the lock table. Whether that's worth the additional overhead
2372+
* is pretty dubious though.
23652373
*/
23662374
xl_standby_lock *
23672375
GetRunningTransactionLocks(int *nlocks)
23682376
{
2377+
xl_standby_lock *accessExclusiveLocks;
23692378
PROCLOCK *proclock;
23702379
HASH_SEQ_STATUS seqstat;
23712380
int i;
23722381
int index;
23732382
int els;
2374-
xl_standby_lock *accessExclusiveLocks;
23752383

23762384
/*
23772385
* Acquire lock on the entire shared lock data structure.
@@ -2428,6 +2436,8 @@ GetRunningTransactionLocks(int *nlocks)
24282436
}
24292437
}
24302438

2439+
Assert(index <= els);
2440+
24312441
/*
24322442
* And release locks. We do this in reverse order for two reasons: (1)
24332443
* Anyone else who needs more than one of the locks will be trying to lock

0 commit comments

Comments
 (0)