1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

Add pg_buffercache_evict_{relation,all} functions

In addition to the added functions, the pg_buffercache_evict() function now
shows whether the buffer was flushed.

pg_buffercache_evict_relation(): Evicts all shared buffers in a
relation at once.
pg_buffercache_evict_all(): Evicts all shared buffers at once.

Both functions provide mechanism to evict multiple shared buffers at
once. They are designed to address the inefficiency of repeatedly calling
pg_buffercache_evict() for each individual buffer, which can be time-consuming
when dealing with large shared buffer pools. (e.g., ~477ms vs. ~2576ms for
16GB of fully populated shared buffers).

These functions are intended for developer testing and debugging
purposes and are available to superusers only.

Minimal tests for the new functions are included. Also, there was no test for
pg_buffercache_evict(), test for this added too.

No new extension version is needed, as it was already increased this release
by ba2a3c2302.

Author: Nazir Bilal Yavuz <byavuz81@gmail.com>
Reviewed-by: Andres Freund <andres@anarazel.de>
Reviewed-by: Aidar Imamov <a.imamov@postgrespro.ru>
Reviewed-by: Joseph Koshakow <koshy44@gmail.com>
Discussion: https://postgr.es/m/CAN55FZ0h_YoSqqutxV6DES1RW8ig6wcA8CR9rJk358YRMxZFmw%40mail.gmail.com
This commit is contained in:
Andres Freund
2025-04-08 02:16:51 -04:00
parent d69d45a5a9
commit dcf7e1697b
8 changed files with 484 additions and 40 deletions

View File

@ -27,14 +27,24 @@
<primary>pg_buffercache_evict</primary>
</indexterm>
<indexterm>
<primary>pg_buffercache_evict_relation</primary>
</indexterm>
<indexterm>
<primary>pg_buffercache_evict_all</primary>
</indexterm>
<para>
This module provides the <function>pg_buffercache_pages()</function>
function (wrapped in the <structname>pg_buffercache</structname> view),
<function>pg_buffercache_numa_pages()</function> function (wrapped in the
<structname>pg_buffercache_numa</structname> view), the
<function>pg_buffercache_summary()</function> function, the
<function>pg_buffercache_usage_counts()</function> function and
the <function>pg_buffercache_evict()</function> function.
<function>pg_buffercache_usage_counts()</function> function, the
<function>pg_buffercache_evict()</function>, the
<function>pg_buffercache_evict_relation()</function> function and the
<function>pg_buffercache_evict_all()</function> function.
</para>
<para>
@ -76,6 +86,19 @@
function is restricted to superusers only.
</para>
<para>
The <function>pg_buffercache_evict_relation()</function> function allows all
unpinned shared buffers in the relation to be evicted from the buffer pool
given a relation identifier. Use of this function is restricted to
superusers only.
</para>
<para>
The <function>pg_buffercache_evict_all()</function> function allows all
unpinned shared buffers to be evicted in the buffer pool. Use of this
function is restricted to superusers only.
</para>
<sect2 id="pgbuffercache-pg-buffercache">
<title>The <structname>pg_buffercache</structname> View</title>
@ -452,11 +475,49 @@
<para>
The <function>pg_buffercache_evict()</function> function takes a buffer
identifier, as shown in the <structfield>bufferid</structfield> column of
the <structname>pg_buffercache</structname> view. It returns true on success,
and false if the buffer wasn't valid, if it couldn't be evicted because it
was pinned, or if it became dirty again after an attempt to write it out.
The result is immediately out of date upon return, as the buffer might
become valid again at any time due to concurrent activity. The function is
the <structname>pg_buffercache</structname> view. It returns information
about whether the buffer was evicted and flushed. The buffer_evicted
column is true on success, and false if the buffer wasn't valid, if it
couldn't be evicted because it was pinned, or if it became dirty again
after an attempt to write it out. The buffer_flushed column is true if the
buffer was flushed. This does not necessarily mean that buffer was flushed
by us, it might be flushed by someone else. The result is immediately out
of date upon return, as the buffer might become valid again at any time due
to concurrent activity. The function is intended for developer testing
only.
</para>
</sect2>
<sect2 id="pgbuffercache-pg-buffercache-evict-relation">
<title>The <structname>pg_buffercache_evict_relation</structname> Function</title>
<para>
The <function>pg_buffercache_evict_relation()</function> function is very
similar to the <function>pg_buffercache_evict()</function> function. The
difference is that the <function>pg_buffercache_evict_relation()</function>
takes a relation identifier instead of buffer identifier. It tries to
evict all buffers for all forks in that relation.
It returns the number of evicted buffers, flushed buffers and the number of
buffers that could not be evicted. Flushed buffers haven't necessarily
been flushed by us, they might have been flushed by someone else. The
result is immediately out of date upon return, as buffers might immediately
be read back in due to concurrent activity. The function is intended for
developer testing only.
</para>
</sect2>
<sect2 id="pgbuffercache-pg-buffercache-evict-all">
<title>The <structname>pg_buffercache_evict_all</structname> Function</title>
<para>
The <function>pg_buffercache_evict_all()</function> function is very
similar to the <function>pg_buffercache_evict()</function> function. The
difference is, the <function>pg_buffercache_evict_all()</function> function
does not take an argument; instead it tries to evict all buffers in the
buffer pool. It returns the number of evicted buffers, flushed buffers and
the number of buffers that could not be evicted. Flushed buffers haven't
necessarily been flushed by us, they might have been flushed by someone
else. The result is immediately out of date upon return, as buffers might
immediately be read back in due to concurrent activity. The function is
intended for developer testing only.
</para>
</sect2>