v3-0002-Return-buffer-flushed-information-in-pg_buffercac.patch
text/x-patch
Filename: v3-0002-Return-buffer-flushed-information-in-pg_buffercac.patch
Type: text/x-patch
Part: 1
Patch
Format: format-patch
Series: patch v3-0002
Subject: Return buffer flushed information in pg_buffercache_evict function
| File | + | − |
|---|---|---|
| contrib/pg_buffercache/pg_buffercache--1.5--1.6.sql | 9 | 0 |
| contrib/pg_buffercache/pg_buffercache_pages.c | 19 | 1 |
| doc/src/sgml/pgbuffercache.sgml | 9 | 6 |
From 683dc0d8e27f99ee0897fdbd33f58a1caf56bb80 Mon Sep 17 00:00:00 2001
From: Nazir Bilal Yavuz <byavuz81@gmail.com>
Date: Mon, 24 Mar 2025 13:53:26 +0300
Subject: [PATCH v3 2/3] Return buffer flushed information in
pg_buffercache_evict function
Prior commit added ability to get buffer flushed information from
EvictUnpinnedBuffer() function. Show this information in
pg_buffercache_evict() function too.
Author: Nazir Bilal Yavuz <byavuz81@gmail.com>
Suggested-by: Joseph Koshakow <koshy44@gmail.com>
Discussion: https://postgr.es/m/CAN55FZ0h_YoSqqutxV6DES1RW8ig6wcA8CR9rJk358YRMxZFmw%40mail.gmail.com
---
doc/src/sgml/pgbuffercache.sgml | 15 ++++++++------
.../pg_buffercache--1.5--1.6.sql | 9 +++++++++
contrib/pg_buffercache/pg_buffercache_pages.c | 20 ++++++++++++++++++-
3 files changed, 37 insertions(+), 7 deletions(-)
diff --git a/doc/src/sgml/pgbuffercache.sgml b/doc/src/sgml/pgbuffercache.sgml
index d99aa979410..681c74251d4 100644
--- a/doc/src/sgml/pgbuffercache.sgml
+++ b/doc/src/sgml/pgbuffercache.sgml
@@ -391,12 +391,15 @@
<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
- intended for developer testing only.
+ the <structname>pg_buffercache</structname> view. It returns the
+ information about whether the buffer is evicted and flushed. 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. flushed column is true if the buffer is
+ flushed. This does not necessarily mean that buffer is 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>
diff --git a/contrib/pg_buffercache/pg_buffercache--1.5--1.6.sql b/contrib/pg_buffercache/pg_buffercache--1.5--1.6.sql
index 2494a0a19b1..15881f5b8fe 100644
--- a/contrib/pg_buffercache/pg_buffercache--1.5--1.6.sql
+++ b/contrib/pg_buffercache/pg_buffercache--1.5--1.6.sql
@@ -1,5 +1,14 @@
\echo Use "ALTER EXTENSION pg_buffercache UPDATE TO '1.6'" to load this file. \quit
+DROP FUNCTION pg_buffercache_evict(integer);
+CREATE OR REPLACE FUNCTION pg_buffercache_evict(
+ IN int,
+ OUT evicted boolean,
+ OUT flushed boolean)
+RETURNS record
+AS 'MODULE_PATHNAME', 'pg_buffercache_evict'
+LANGUAGE C PARALLEL SAFE VOLATILE STRICT;
+
CREATE FUNCTION pg_buffercache_evict_relation(
IN regclass,
OUT buffers_evicted int4,
diff --git a/contrib/pg_buffercache/pg_buffercache_pages.c b/contrib/pg_buffercache/pg_buffercache_pages.c
index 7d3bb50b942..77a80401525 100644
--- a/contrib/pg_buffercache/pg_buffercache_pages.c
+++ b/contrib/pg_buffercache/pg_buffercache_pages.c
@@ -22,6 +22,7 @@
#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 2
#define NUM_BUFFERCACHE_EVICT_ALL_ELEM 2
@@ -362,7 +363,17 @@ pg_buffercache_usage_counts(PG_FUNCTION_ARGS)
Datum
pg_buffercache_evict(PG_FUNCTION_ARGS)
{
+ Datum result;
+ TupleDesc tupledesc;
+ HeapTuple tuple;
+ Datum values[NUM_BUFFERCACHE_EVICT_ELEM];
+ bool nulls[NUM_BUFFERCACHE_EVICT_ELEM] = {0};
+
Buffer buf = PG_GETARG_INT32(0);
+ bool flushed;
+
+ if (get_call_result_type(fcinfo, NULL, &tupledesc) != TYPEFUNC_COMPOSITE)
+ elog(ERROR, "return type must be a row type");
if (!superuser())
ereport(ERROR,
@@ -372,7 +383,14 @@ pg_buffercache_evict(PG_FUNCTION_ARGS)
if (buf < 1 || buf > NBuffers)
elog(ERROR, "bad buffer ID: %d", buf);
- PG_RETURN_BOOL(EvictUnpinnedBuffer(buf));
+ values[0] = BoolGetDatum(EvictUnpinnedBuffer(buf, 0, &flushed));
+ values[1] = BoolGetDatum(flushed);
+
+ /* Build and return the tuple. */
+ tuple = heap_form_tuple(tupledesc, values, nulls);
+ result = HeapTupleGetDatum(tuple);
+
+ PG_RETURN_DATUM(result);
}
/*
--
2.47.2