postgres.git / summary / log / commit / refs

commit    62f868e779b3b41945fba27f65dec33595b97fd0
Author:   Fujii Masao <fujii@postgresql.org>
Date:     Thu Nov 05 09:02:22 2026 +0000
First released in: 18.7, 19.0

    Fix io_uring resource leaks across crash restarts
    
    With io_method=io_uring and restart_after_crash enabled, the postmaster
    reinitializes shared memory after a backend crash. It created new
    io_uring instances without releasing its references to the old ones,
    causing resources to accumulate on each crash restart. Eventually,
    creating new instances could fail with "Too many open files" or
    "Cannot allocate memory", preventing the server from restarting.
    
    Fix this by registering an on_shmem_exit() callback to release the
    postmaster's references to the io_uring instances during shared
    memory cleanup, before creating new ones. This prevents resources
    from accumulating across crash restarts.
    
    Backpatch to v18, where io_method=io_uring was introduced.
    
    Reported-by: Lucas DRAESCHER <git@draescher.fr>
    Author: Lucas DRAESCHER <git@draescher.fr>
    Reviewed-by: ChangAo Chen <cca5507@qq.com>
    Reviewed-by: Ilmar Yunusov <tanswis42@gmail.com>
    Reviewed-by: Laetitia Avrot <laetitia.avrot@gmail.com>
    Reviewed-by: Jakub Wartak <jakub.wartak@enterprisedb.com>
    Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
    Reviewed-by: Xuneng Zhou <xunengzhou@gmail.com>
    Discussion: https://postgr.es/m/zHN8Rb7syWYDBPUhCICxKLr_tQmFLg0T024irxVVb6RSSicZcqcA0eT73z2PQy7JmzzqnUzX0Hi3P2dPKRvmNx_jbAqSCLyqJvWjXEypyAI=@draescher.fr
    Backpatch-through: 18


src/backend/storage/aio/method_io_uring.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/backend/storage/aio/method_io_uring.c b/src/backend/storage/aio/method_io_uring.c index c0f9fc9c303..99004740652 100644 --- a/src/backend/storage/aio/method_io_uring.c +++ b/src/backend/storage/aio/method_io_uring.c @@ -37,6 +37,7 @@ #include "miscadmin.h" #include "storage/aio_internal.h" #include "storage/fd.h" +#include "storage/ipc.h" #include "storage/proc.h" #include "storage/shmem.h" #include "storage/lwlock.h" @@ -288,6 +289,25 @@ pgaio_uring_shmem_request(void *arg) ); } +/* + * on_shmem_exit() callback that releases the io_uring instances created by + * pgaio_uring_shmem_init(). Besides process exit, this is called when the + * postmaster reinitializes shared memory after a backend crash. + */ +static void +pgaio_uring_cleanup(int code, Datum arg) +{ + int TotalProcs = pgaio_uring_procs(); + + Assert(!IsUnderPostmaster); + Assert(pgaio_uring_contexts != NULL); + + for (int i = 0; i < TotalProcs; i++) + io_uring_queue_exit(&pgaio_uring_contexts[i].io_uring_ring); + + pgaio_uring_contexts = NULL; +} + static void pgaio_uring_shmem_init(void *arg) { @@ -401,6 +421,8 @@ pgaio_uring_shmem_init(void *arg) LWLockInitialize(&context->completion_lock, LWTRANCHE_AIO_URING_COMPLETION); } + + on_shmem_exit(pgaio_uring_cleanup, 0); } static void [parent: 2f2c923c3350]