aboutsummaryrefslogtreecommitdiff
path: root/src/backend/storage/buffer/freelist.c
diff options
context:
space:
mode:
authorThomas Munro <tmunro@postgresql.org>2024-04-06 22:58:48 +1300
committerThomas Munro <tmunro@postgresql.org>2024-04-06 23:11:45 +1300
commit3bd8439ed628c7e9ac250b1a042d9044303c37e7 (patch)
tree5c873d096cc74591d1d1d21598af69c66918353c /src/backend/storage/buffer/freelist.c
parentf956ecd0353b2960f8322b2211142113fe2b6f67 (diff)
downloadpostgresql-3bd8439ed628c7e9ac250b1a042d9044303c37e7.tar.gz
postgresql-3bd8439ed628c7e9ac250b1a042d9044303c37e7.zip
Allow BufferAccessStrategy to limit pin count.
While pinning extra buffers to look ahead, users of strategies are in danger of using too many buffers. For some strategies, that means "escaping" from the ring, and in others it means forcing dirty data to disk very frequently with associated WAL flushing. Since external code has no insight into any of that, allow individual strategy types to expose a clamp that should be applied when deciding how many buffers to pin at once. Reviewed-by: Andres Freund <andres@anarazel.de> Reviewed-by: Melanie Plageman <melanieplageman@gmail.com> Discussion: https://postgr.es/m/CAAKRu_aJXnqsyZt6HwFLnxYEBgE17oypkxbKbT1t1geE_wvH2Q%40mail.gmail.com
Diffstat (limited to 'src/backend/storage/buffer/freelist.c')
-rw-r--r--src/backend/storage/buffer/freelist.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/backend/storage/buffer/freelist.c b/src/backend/storage/buffer/freelist.c
index 3611357fa30..de2ef1dd5e6 100644
--- a/src/backend/storage/buffer/freelist.c
+++ b/src/backend/storage/buffer/freelist.c
@@ -630,6 +630,48 @@ GetAccessStrategyBufferCount(BufferAccessStrategy strategy)
}
/*
+ * GetAccessStrategyPinLimit -- get cap of number of buffers that should be pinned
+ *
+ * When pinning extra buffers to look ahead, users of a ring-based strategy are
+ * in danger of pinning too much of the ring at once while performing look-ahead.
+ * For some strategies, that means "escaping" from the ring, and in others it
+ * means forcing dirty data to disk very frequently with associated WAL
+ * flushing. Since external code has no insight into any of that, allow
+ * individual strategy types to expose a clamp that should be applied when
+ * deciding on a maximum number of buffers to pin at once.
+ *
+ * Callers should combine this number with other relevant limits and take the
+ * minimum.
+ */
+int
+GetAccessStrategyPinLimit(BufferAccessStrategy strategy)
+{
+ if (strategy == NULL)
+ return NBuffers;
+
+ switch (strategy->btype)
+ {
+ case BAS_BULKREAD:
+
+ /*
+ * Since BAS_BULKREAD uses StrategyRejectBuffer(), dirty buffers
+ * shouldn't be a problem and the caller is free to pin up to the
+ * entire ring at once.
+ */
+ return strategy->nbuffers;
+
+ default:
+
+ /*
+ * Tell caller not to pin more than half the buffers in the ring.
+ * This is a trade-off between look ahead distance and deferring
+ * writeback and associated WAL traffic.
+ */
+ return strategy->nbuffers / 2;
+ }
+}
+
+/*
* FreeAccessStrategy -- release a BufferAccessStrategy object
*
* A simple pfree would do at the moment, but we would prefer that callers