Skip to content

Commit 81fe138

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 cc510d9 commit 81fe138

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
@@ -3353,18 +3353,26 @@ GetLockStatusData(void)
33533353
}
33543354

33553355
/*
3356-
* Returns a list of currently held AccessExclusiveLocks, for use
3357-
* by GetRunningTransactionData().
3356+
* Returns a list of currently held AccessExclusiveLocks, for use by
3357+
* LogStandbySnapshot(). The result is a palloc'd array,
3358+
* with the number of elements returned into *nlocks.
3359+
*
3360+
* XXX This currently takes a lock on all partitions of the lock table,
3361+
* but it's possible to do better. By reference counting locks and storing
3362+
* the value in the ProcArray entry for each backend we could tell if any
3363+
* locks need recording without having to acquire the partition locks and
3364+
* scan the lock table. Whether that's worth the additional overhead
3365+
* is pretty dubious though.
33583366
*/
33593367
xl_standby_lock *
33603368
GetRunningTransactionLocks(int *nlocks)
33613369
{
3370+
xl_standby_lock *accessExclusiveLocks;
33623371
PROCLOCK *proclock;
33633372
HASH_SEQ_STATUS seqstat;
33643373
int i;
33653374
int index;
33663375
int els;
3367-
xl_standby_lock *accessExclusiveLocks;
33683376

33693377
/*
33703378
* Acquire lock on the entire shared lock data structure.
@@ -3422,6 +3430,8 @@ GetRunningTransactionLocks(int *nlocks)
34223430
}
34233431
}
34243432

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

0 commit comments

Comments
 (0)