Hello PostgreSQL Hackers,

I am submitting a patch to add hooks for the functions
pg_total_relation_size and pg_indexes_size. These hooks allow for custom
behaviour to be injected into these functions, which can be useful for
extensions and other custom PostgreSQL modifications.

Patch details:

   - Adds pg_total_relation_size_hook and pg_indexes_size_hook
   - Modifies pg_total_relation_size and pg_indexes_size to call these
   hooks if they are set
   - Adds necessary type definitions and extern declarations

This feature is useful because it allows for more flexible and customizable
behaviour in relation size calculations, which can be particularly valuable
for extensions that need to account for additional storage outside of the
standard PostgreSQL mechanisms.

The patch is attached.

Thank you for considering this patch. I look forward to your feedback.

Kind regards,
Abdoulaye Ba
From 542dc052700097472180b114409603cc071c2e0d Mon Sep 17 00:00:00 2001
From: abi <abdoulayeb...@gmail.com>
Date: Thu, 8 Aug 2024 14:00:44 +0200
Subject: [PATCH 1/2] added hooks for total relation size and indexes size

---
 src/backend/utils/adt/dbsize.c | 11 ++++++++++-
 src/include/utils/rel.h        |  6 ++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c
index b2d9cc2792..c820e06ca7 100644
--- a/src/backend/utils/adt/dbsize.c
+++ b/src/backend/utils/adt/dbsize.c
@@ -31,6 +31,9 @@
 #include "utils/relmapper.h"
 #include "utils/syscache.h"
 
+Pg_total_relation_size_hook_type pg_total_relation_size_hook = NULL;
+Pg_indexes_size_hook_type pg_indexes_size_hook = NULL;
+
 /* Divide by two and round away from zero */
 #define half_rounded(x)   (((x) + ((x) < 0 ? -1 : 1)) / 2)
 
@@ -513,6 +516,9 @@ pg_indexes_size(PG_FUNCTION_ARGS)
 	if (rel == NULL)
 		PG_RETURN_NULL();
 
+	if (pg_indexes_size_hook)
+        return (*pg_indexes_size_hook)(fcinfo);
+
 	size = calculate_indexes_size(rel);
 
 	relation_close(rel, AccessShareLock);
@@ -555,11 +561,14 @@ pg_total_relation_size(PG_FUNCTION_ARGS)
 	if (rel == NULL)
 		PG_RETURN_NULL();
 
+	if (pg_total_relation_size_hook)
+        return (*pg_total_relation_size_hook)(fcinfo);
+
 	size = calculate_total_relation_size(rel);
 
 	relation_close(rel, AccessShareLock);
 
-	PG_RETURN_INT64(size);
+	PG_RETURN_INT64(size + 1);
 }
 
 /*
diff --git a/src/include/utils/rel.h b/src/include/utils/rel.h
index 8700204953..7191dfd5a6 100644
--- a/src/include/utils/rel.h
+++ b/src/include/utils/rel.h
@@ -708,4 +708,10 @@ RelationCloseSmgr(Relation relation)
 extern void RelationIncrementReferenceCount(Relation rel);
 extern void RelationDecrementReferenceCount(Relation rel);
 
+typedef Datum (*Pg_total_relation_size_hook_type)(FunctionCallInfo fcinfo);
+typedef Datum (*Pg_indexes_size_hook_type)(FunctionCallInfo fcinfo);
+
+extern PGDLLIMPORT Pg_total_relation_size_hook_type pg_total_relation_size_hook;
+extern PGDLLIMPORT Pg_indexes_size_hook_type pg_indexes_size_hook;
+
 #endif							/* REL_H */
-- 
2.34.1

From 16050065e74722421b775afdb0342955e598fedd Mon Sep 17 00:00:00 2001
From: abi <abdoulayeb...@gmail.com>
Date: Thu, 8 Aug 2024 14:06:30 +0200
Subject: [PATCH 2/2] removed the -1

---
 src/backend/utils/adt/dbsize.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c
index c820e06ca7..2e8835e302 100644
--- a/src/backend/utils/adt/dbsize.c
+++ b/src/backend/utils/adt/dbsize.c
@@ -568,7 +568,7 @@ pg_total_relation_size(PG_FUNCTION_ARGS)
 
 	relation_close(rel, AccessShareLock);
 
-	PG_RETURN_INT64(size + 1);
+	PG_RETURN_INT64(size);
 }
 
 /*
-- 
2.34.1

Reply via email to