Fix leak with SMgrRelations in startup process
authorMichael Paquier <michael@paquier.xyz>
Tue, 9 Sep 2025 22:23:05 +0000 (07:23 +0900)
committerMichael Paquier <michael@paquier.xyz>
Tue, 9 Sep 2025 22:23:05 +0000 (07:23 +0900)
The startup process does not process shared invalidation messages, only
sending them, and never calls AtEOXact_SMgr() which clean up any
unpinned SMgrRelations.  Hence, it is never able to free SMgrRelations
on a periodic basis, bloating its hashtable over time.

Like the checkpointer and the bgwriter, this commit takes a conservative
approach by freeing periodically SMgrRelations when replaying a
checkpoint record, either online or shutdown, so as the startup process
has a way to perform a periodic cleanup.

Issue caused by 21d9c3ee4ef7, so backpatch down to v17.

Author: Jingtang Zhang <mrdrivingduck@gmail.com>
Reviewed-by: Yuhang Qiu <iamqyh@gmail.com>
Discussion: https://postgr.es/m/28C687D4-F335-417E-B06C-6612A0BD5A10@gmail.com
Backpatch-through: 17

src/backend/access/transam/xlog.c

index 7ffb217915190496b204a8f49490ac1173c6a2c4..0baf0ac6160afb53b1cb3412a6d1406e85759562 100644 (file)
@@ -8385,6 +8385,14 @@ xlog_redo(XLogReaderState *record)
                            checkPoint.ThisTimeLineID, replayTLI)));
 
        RecoveryRestartPoint(&checkPoint, record);
+
+       /*
+        * After replaying a checkpoint record, free all smgr objects.
+        * Otherwise we would never do so for dropped relations, as the
+        * startup does not process shared invalidation messages or call
+        * AtEOXact_SMgr().
+        */
+       smgrdestroyall();
    }
    else if (info == XLOG_CHECKPOINT_ONLINE)
    {
@@ -8438,6 +8446,14 @@ xlog_redo(XLogReaderState *record)
                            checkPoint.ThisTimeLineID, replayTLI)));
 
        RecoveryRestartPoint(&checkPoint, record);
+
+       /*
+        * After replaying a checkpoint record, free all smgr objects.
+        * Otherwise we would never do so for dropped relations, as the
+        * startup does not process shared invalidation messages or call
+        * AtEOXact_SMgr().
+        */
+       smgrdestroyall();
    }
    else if (info == XLOG_OVERWRITE_CONTRECORD)
    {