1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-30 11:03:19 +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

@ -9,17 +9,22 @@
#include "postgres.h"
#include "access/htup_details.h"
#include "access/relation.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "port/pg_numa.h"
#include "storage/buf_internals.h"
#include "storage/bufmgr.h"
#include "utils/rel.h"
#define NUM_BUFFERCACHE_PAGES_MIN_ELEM 8
#define NUM_BUFFERCACHE_PAGES_ELEM 9
#define NUM_BUFFERCACHE_SUMMARY_ELEM 5
#define NUM_BUFFERCACHE_USAGE_COUNTS_ELEM 4
#define NUM_BUFFERCACHE_EVICT_ELEM 2
#define NUM_BUFFERCACHE_EVICT_RELATION_ELEM 3
#define NUM_BUFFERCACHE_EVICT_ALL_ELEM 3
#define NUM_BUFFERCACHE_NUMA_ELEM 3
@ -93,6 +98,8 @@ PG_FUNCTION_INFO_V1(pg_buffercache_numa_pages);
PG_FUNCTION_INFO_V1(pg_buffercache_summary);
PG_FUNCTION_INFO_V1(pg_buffercache_usage_counts);
PG_FUNCTION_INFO_V1(pg_buffercache_evict);
PG_FUNCTION_INFO_V1(pg_buffercache_evict_relation);
PG_FUNCTION_INFO_V1(pg_buffercache_evict_all);
/* Only need to touch memory once per backend process lifetime */
@ -637,21 +644,131 @@ pg_buffercache_usage_counts(PG_FUNCTION_ARGS)
return (Datum) 0;
}
/*
* Helper function to check if the user has superuser privileges.
*/
static void
pg_buffercache_superuser_check(char *func_name)
{
if (!superuser())
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("must be superuser to use %s()",
func_name)));
}
/*
* Try to evict a shared buffer.
*/
Datum
pg_buffercache_evict(PG_FUNCTION_ARGS)
{
Buffer buf = PG_GETARG_INT32(0);
Datum result;
TupleDesc tupledesc;
HeapTuple tuple;
Datum values[NUM_BUFFERCACHE_EVICT_ELEM];
bool nulls[NUM_BUFFERCACHE_EVICT_ELEM] = {0};
if (!superuser())
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("must be superuser to use pg_buffercache_evict function")));
Buffer buf = PG_GETARG_INT32(0);
bool buffer_flushed;
if (get_call_result_type(fcinfo, NULL, &tupledesc) != TYPEFUNC_COMPOSITE)
elog(ERROR, "return type must be a row type");
pg_buffercache_superuser_check("pg_buffercache_evict");
if (buf < 1 || buf > NBuffers)
elog(ERROR, "bad buffer ID: %d", buf);
PG_RETURN_BOOL(EvictUnpinnedBuffer(buf));
values[0] = BoolGetDatum(EvictUnpinnedBuffer(buf, &buffer_flushed));
values[1] = BoolGetDatum(buffer_flushed);
tuple = heap_form_tuple(tupledesc, values, nulls);
result = HeapTupleGetDatum(tuple);
PG_RETURN_DATUM(result);
}
/*
* Try to evict specified relation.
*/
Datum
pg_buffercache_evict_relation(PG_FUNCTION_ARGS)
{
Datum result;
TupleDesc tupledesc;
HeapTuple tuple;
Datum values[NUM_BUFFERCACHE_EVICT_RELATION_ELEM];
bool nulls[NUM_BUFFERCACHE_EVICT_RELATION_ELEM] = {0};
Oid relOid;
Relation rel;
int32 buffers_evicted = 0;
int32 buffers_flushed = 0;
int32 buffers_skipped = 0;
if (get_call_result_type(fcinfo, NULL, &tupledesc) != TYPEFUNC_COMPOSITE)
elog(ERROR, "return type must be a row type");
pg_buffercache_superuser_check("pg_buffercache_evict_relation");
relOid = PG_GETARG_OID(0);
rel = relation_open(relOid, AccessShareLock);
if (RelationUsesLocalBuffers(rel))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("relation uses local buffers, %s() is intended to be used for shared buffers only",
"pg_buffercache_evict_relation")));
EvictRelUnpinnedBuffers(rel, &buffers_evicted, &buffers_flushed,
&buffers_skipped);
relation_close(rel, AccessShareLock);
values[0] = Int32GetDatum(buffers_evicted);
values[1] = Int32GetDatum(buffers_flushed);
values[2] = Int32GetDatum(buffers_skipped);
tuple = heap_form_tuple(tupledesc, values, nulls);
result = HeapTupleGetDatum(tuple);
PG_RETURN_DATUM(result);
}
/*
* Try to evict all shared buffers.
*/
Datum
pg_buffercache_evict_all(PG_FUNCTION_ARGS)
{
Datum result;
TupleDesc tupledesc;
HeapTuple tuple;
Datum values[NUM_BUFFERCACHE_EVICT_ALL_ELEM];
bool nulls[NUM_BUFFERCACHE_EVICT_ALL_ELEM] = {0};
int32 buffers_evicted = 0;
int32 buffers_flushed = 0;
int32 buffers_skipped = 0;
if (get_call_result_type(fcinfo, NULL, &tupledesc) != TYPEFUNC_COMPOSITE)
elog(ERROR, "return type must be a row type");
pg_buffercache_superuser_check("pg_buffercache_evict_all");
EvictAllUnpinnedBuffers(&buffers_evicted, &buffers_flushed,
&buffers_skipped);
values[0] = Int32GetDatum(buffers_evicted);
values[1] = Int32GetDatum(buffers_flushed);
values[2] = Int32GetDatum(buffers_skipped);
tuple = heap_form_tuple(tupledesc, values, nulls);
result = HeapTupleGetDatum(tuple);
PG_RETURN_DATUM(result);
}