Skip to content

Commit dc12e47

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 89bd9fe commit dc12e47

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
@@ -875,16 +875,11 @@ LogStandbySnapshot(void)
875875

876876
/*
877877
* Get details of any AccessExclusiveLocks being held at the moment.
878-
*
879-
* XXX GetRunningTransactionLocks() currently holds a lock on all
880-
* partitions though it is possible to further optimise the locking. By
881-
* reference counting locks and storing the value on the ProcArray entry
882-
* for each backend we can easily tell if any locks need recording without
883-
* trying to acquire the partition locks and scanning the lock table.
884878
*/
885879
locks = GetRunningTransactionLocks(&nlocks);
886880
if (nlocks > 0)
887881
LogAccessExclusiveLocks(nlocks, locks);
882+
pfree(locks);
888883

889884
/*
890885
* 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
@@ -2444,18 +2444,26 @@ GetLockStatusData(void)
24442444
}
24452445

24462446
/*
2447-
* Returns a list of currently held AccessExclusiveLocks, for use
2448-
* by GetRunningTransactionData().
2447+
* Returns a list of currently held AccessExclusiveLocks, for use by
2448+
* LogStandbySnapshot(). The result is a palloc'd array,
2449+
* with the number of elements returned into *nlocks.
2450+
*
2451+
* XXX This currently takes a lock on all partitions of the lock table,
2452+
* but it's possible to do better. By reference counting locks and storing
2453+
* the value in the ProcArray entry for each backend we could tell if any
2454+
* locks need recording without having to acquire the partition locks and
2455+
* scan the lock table. Whether that's worth the additional overhead
2456+
* is pretty dubious though.
24492457
*/
24502458
xl_standby_lock *
24512459
GetRunningTransactionLocks(int *nlocks)
24522460
{
2461+
xl_standby_lock *accessExclusiveLocks;
24532462
PROCLOCK *proclock;
24542463
HASH_SEQ_STATUS seqstat;
24552464
int i;
24562465
int index;
24572466
int els;
2458-
xl_standby_lock *accessExclusiveLocks;
24592467

24602468
/*
24612469
* Acquire lock on the entire shared lock data structure.
@@ -2512,6 +2520,8 @@ GetRunningTransactionLocks(int *nlocks)
25122520
}
25132521
}
25142522

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

0 commit comments

Comments
 (0)