Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wh3a6-0001RQ-2l for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Jul 2026 11:03:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wh3Z4-000QiK-2m for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Jul 2026 11:02:11 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wh3Z4-000QiC-1I for pgsql-hackers@lists.postgresql.org; Tue, 07 Jul 2026 11:02:11 +0000 Received: from mail-wr1-x433.google.com ([2a00:1450:4864:20::433]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wh3Z2-000000001Uk-0QmN for pgsql-hackers@lists.postgresql.org; Tue, 07 Jul 2026 11:02:10 +0000 Received: by mail-wr1-x433.google.com with SMTP id ffacd0b85a97d-476a130c138so4198610f8f.0 for ; Tue, 07 Jul 2026 04:02:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783422126; x=1784026926; darn=lists.postgresql.org; h=content-disposition:content-type:mime-version:message-id:subject:to :from:date:from:to:cc:subject:date:message-id:reply-to:content-type; bh=KVN2idzARTgVXmWBN5RozaOJpKBPrOzCbvkDuVKvqcQ=; b=hYmSs1rj8dB4/wVbvXgUUHm9HIeYJJNhlLHXc/26rHV/3/EoXv9Rk104xd97ZdIlAe V0y4WsJyP0/2+cmicjZMMl8FzEnfYbBB9rAQiv51c6R/sU8OU9m84QIVZCkt/EerDikq NHP+i0r+Gg/zd/QqUYDqzxPwPafxDt2bOemWprRH3HpKmz0DEKyPHZhfcv/Gh8OC6SV1 PZL0jdj6by1kMmLvc7xoy5cUGI8TPYNJ3lIOqJZZF1k6lSYFTxXBPJIWROafrBfvHlT9 1pyObh0EO3pD8KDcJrodgkhZoLtzMbH97nKQsPijAnmsg0Guz/P27dgGJ3niUABFkCZr iaCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783422126; x=1784026926; h=content-disposition:content-type:mime-version:message-id:subject:to :from:date:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=KVN2idzARTgVXmWBN5RozaOJpKBPrOzCbvkDuVKvqcQ=; b=QPvxF/BVKSy2FalYJTZG4PHG0n7Ptk1pPcYrUDacfGUinUIrefW0VMimkc13fN2sC4 kpO94JYeG5hyiXdFYSWI62rheFD208YQ1vfjLIsI9btyPanoGi7cV/pIM/slIE6G3Pit kjqhc5lY9UwtQahQJgBZqQS2W6avV9SmqiTJLA3JbZnnUV3jUlWqwxp1IoVF/sQ/xcuo iZCs3sP0Fs+uSOqa5xnZVRKujJyXb72d+iSVVZWa6nkaLEqGHhxGtaG8nM/JjuvURZSX FTMbZmtGW1TWNtHDFoZzpkaMam4czwrQxNWRmXaIX5vtYhl0EedaUELQ4WJEyaLQVSgR pLrg== X-Gm-Message-State: AOJu0Yzt/X3jhUGtCU+cv7cqsvpFGPRafXbJztTXg4Vm1y+noYBU2E59 0sV4ur/RwXGzByFhTUtVYATsUtKDbttdzhmJJ12AX9UKDYiN2a9XBgrpd1FETg== X-Gm-Gg: AfdE7cmtwRioociJFRX67eypvHxfXgzYI0FTH4DRCosQa7J8T2VB+N+KigIAB0xdWTa mMt5TtgxhwX7QowRhVYJHDY7P4aZ3EZtNUBVYFR1VK280BM3cm4ejUPyfSPVuIBxFV3Vfle/2vo e0DNWQIM4yYy23EBEC/B3NgBtRPjFBURO6ZGfbkgEUEMB+DF6CX72BhzwYCsof9ovM0tKTSF1nu rg2so9sXvP6ftDG5e+X73mTsa6bXMXfFL06O93N4AlcKzus44Qrq74TnGHy+dsj0Kzky6bRvLFf pO5Ul62vd4m4zeK5+8IHh8bq+XV/1FJzl84vtxFGdMg39nl7zvvWrC+qgUWucHI0lrKWl5yj1YO 3P1MBzQR01bvW41mEIWG4TvqcugdA+jVVssoMDLVC8mdJ7FBGqAYUoJHYc3JVkrW206wUu1TL+M kwH+5IrATS+aq0pKMi0y0Tr5uxRE4KhNjQk5l9iRvJ5dOj0UGQSYzgm/3wkzyFCAU1oUdnRv8L X-Received: by 2002:a05:6000:1f10:b0:475:a05c:c63e with SMTP id ffacd0b85a97d-47de66d3203mr3853246f8f.31.1783422125574; Tue, 07 Jul 2026 04:02:05 -0700 (PDT) Received: from bdtpg (ec2-15-237-197-144.eu-west-3.compute.amazonaws.com. [15.237.197.144]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-47aa0f214d2sm32136659f8f.33.2026.07.07.04.02.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jul 2026 04:02:04 -0700 (PDT) Date: Tue, 7 Jul 2026 11:02:03 +0000 From: Bertrand Drouvot To: pgsql-hackers@lists.postgresql.org Subject: Add per-backend AIO statistics Message-ID: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="Pi0/Nu3LVhi0Ij+H" Content-Disposition: inline List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Pi0/Nu3LVhi0Ij+H Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi hackers, Currently to monitor AIO we can use: 1/ pg_aios that lists all AIO handles that are currently in use. That shows what's happening right now, but not what has happened. 2/ pg_stat_get_backend_io() that shows how much IO was done, but not how it was done. There's no way to see whether IOs ran synchronously or asynchronously, whether a backend was stalling on handle exhaustion, or how completions are distributed across backends. This patch helps answering those questions by exposing cumulative per-backend AIO counters: - started: total AIO operations initiated - executed_sync: IOs executed synchronously (fallback path) - executed_async: IOs submitted asynchronously - completed_self: IO completions processed by the issuing backend - completed_other: IO completions processed on behalf of another backend - handle_waits: times waited for a free AIO handle - submitted: number of submit calls to the IO method These counters are useful for understanding and tuning AIO behavior: - executed_async / started. A ratio near zero means the backend is falling back to synchronous execution (TOAST chunk fetches, temp buffers, ...). - a non-zero handle_waits means the backend exhausted all its AIO handles. That could mean that io_max_concurrency is too low. - completed_self vs completed_other reveals cross-backend completion patterns. That helps see how IO completion work is distributed and could help interpret per backend IO statistics values. - executed_async / submitted gives the average batch size per submit call. As far as the technical implementation: This data can be retrieved with a new system function called pg_stat_get_backend_aio(), that returns one row based on the PID provided in input. pgstat_flush_backend() gains a new flag value, able to control the flush of the AIO stats. This patch relies mostly on the infrastructure provided by 9aea73fc61d4, that has introduced backend statistics. The overhead (4 functions calls and counters increments) kind of follow the same patterns as pgstat_count_backend_io_op() and I did not observe measurable regression (I did not expect to). Also that does not add that much memory per-backend: PgStat_AioCounters is 56 bytes. There is no "double" counting as a global view to show those counters does not exist. I think that's better to start with the per-backend side of it and see if we want to also add a global view. For example, completed_other identifies which backends did IOs for other backends. Also this allows correlating with pg_stat_activity and pg_stat_get_backend_io(). Examples based on Franck's blog post [1]: 1/ query the smalldocs table: postgres=# select count(*),avg(length(data)) from smalldocs; count | avg ---------+----------------------- 1024000 | 1024.0000000000000000 (1 row) postgres=# SELECT * FROM pg_stat_get_backend_aio(pg_backend_pid()); started | executed_sync | executed_async | completed_self | completed_other | handle_waits | submitted | stats_reset ---------+---------------+----------------+----------------+-----------------+--------------+-----------+------------------------------- 3125 | 46 | 3079 | 46 | 0 | 0 | 3078 | 2026-07-07 09:28:27.412136+00 We can see that the sequential scan fully benefits from AIO. 2/ query the largedocs table: postgres=# select count(*),avg(length(data)) from largedocs; count | avg -------+---------------------- 1000 | 1048576.000000000000 (1 row) postgres=# SELECT * FROM pg_stat_get_backend_aio(pg_backend_pid()); started | executed_sync | executed_async | completed_self | completed_other | handle_waits | submitted | stats_reset ---------+---------------+----------------+----------------+-----------------+--------------+-----------+------------------------------- 121154 | 121150 | 4 | 121150 | 0 | 0 | 4 | 2026-07-07 09:35:00.504872+00 We can see that the sequential scan bypasses AIO. Looking forward to your feedback. [1]: https://dev.to/franckpachot/iouring-buffered-reads-in-postgresql-19-iouring-mcn Regards, -- Bertrand Drouvot PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com --Pi0/Nu3LVhi0Ij+H Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v1-0001-Add-per-backend-AIO-statistics.patch"