Skip to content

Commit df238ae

Browse files
committed
Avoid O(N^2) behavior when the standby process releases many locks.
When replaying a transaction that held many exclusive locks on the primary, a standby server's startup process would expend O(N^2) effort on manipulating the list of locks. This code was fine when written, but commit 1cff1b9 made repetitive list_delete_first() calls inefficient, as explained in its commit message. Fix by just iterating the list normally, and releasing storage only when done. (This'd be inadequate if we needed to recover from an error occurring partway through; but we don't.) Back-patch to v13 where 1cff1b9 came in. Nathan Bossart Discussion: https://postgr.es/m/CD2F0E7F-9822-45EC-A411-AE56F14DEA9F@amazon.com
1 parent 4cd72ad commit df238ae

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/backend/storage/ipc/standby.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -791,9 +791,11 @@ StandbyAcquireAccessExclusiveLock(TransactionId xid, Oid dbOid, Oid relOid)
791791
static void
792792
StandbyReleaseLockList(List *locks)
793793
{
794-
while (locks)
794+
ListCell *lc;
795+
796+
foreach(lc, locks)
795797
{
796-
xl_standby_lock *lock = (xl_standby_lock *) linitial(locks);
798+
xl_standby_lock *lock = (xl_standby_lock *) lfirst(lc);
797799
LOCKTAG locktag;
798800

799801
elog(trace_recovery(DEBUG4),
@@ -807,9 +809,9 @@ StandbyReleaseLockList(List *locks)
807809
lock->xid, lock->dbOid, lock->relOid);
808810
Assert(false);
809811
}
810-
pfree(lock);
811-
locks = list_delete_first(locks);
812812
}
813+
814+
list_free_deep(locks);
813815
}
814816

815817
static void

0 commit comments

Comments
 (0)