From 6479e1ccc92c53468dc879d33fa1a93f66ae4521 Mon Sep 17 00:00:00 2001 From: Hubert Zhang Date: Mon, 26 Feb 2024 19:24:43 +0800 Subject: [PATCH v8] Add smgr hooks to extend the logic of storage management One example is that these hooks could be used by diskquota extension to detect heap table change(create/extend/truncate/unlink). Co-authored-by: Haozhou Wang Co-authored-by: Hubert Zhang Co-authored-by: Hao Wu --- src/backend/storage/smgr/smgr.c | 21 +++++++++++++++++++++ src/include/storage/smgr.h | 16 ++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/backend/storage/smgr/smgr.c b/src/backend/storage/smgr/smgr.c index f7f7fe30b6..7992da84ce 100644 --- a/src/backend/storage/smgr/smgr.c +++ b/src/backend/storage/smgr/smgr.c @@ -142,6 +142,15 @@ static dlist_head unpinned_relns; static void smgrshutdown(int code, Datum arg); static void smgrdestroy(SMgrRelation reln); +/* + * Hook for plugins to extend smgr functions. + * for example, collect statistics from smgr functions + * via recording the active relfilenode information. + */ +smgrcreate_hook_type smgrcreate_hook = NULL; +smgrextend_hook_type smgrextend_hook = NULL; +smgrtruncate_hook_type smgrtruncate_hook = NULL; +smgrdounlinkall_hook_type smgrdounlinkall_hook = NULL; /* * smgrinit(), smgrshutdown() -- Initialize or shut down storage @@ -413,6 +422,9 @@ smgrexists(SMgrRelation reln, ForkNumber forknum) void smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo) { + if (smgrcreate_hook) + (*smgrcreate_hook)(reln, forknum, isRedo); + smgrsw[reln->smgr_which].smgr_create(reln, forknum, isRedo); } @@ -471,6 +483,9 @@ smgrdounlinkall(SMgrRelation *rels, int nrels, bool isRedo) if (nrels == 0) return; + if (smgrdounlinkall_hook) + (*smgrdounlinkall_hook)(rels, nrels, isRedo); + /* * Get rid of any remaining buffers for the relations. bufmgr will just * drop them without bothering to write the contents. @@ -538,6 +553,9 @@ void smgrextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, const void *buffer, bool skipFsync) { + if (smgrextend_hook) + (*smgrextend_hook)(reln, forknum, blocknum, buffer, skipFsync); + smgrsw[reln->smgr_which].smgr_extend(reln, forknum, blocknum, buffer, skipFsync); @@ -706,6 +724,9 @@ smgrtruncate(SMgrRelation reln, ForkNumber *forknum, int nforks, BlockNumber *nb { int i; + if (smgrtruncate_hook) + (*smgrtruncate_hook)(reln, forknum, nforks, nblocks); + /* * Get rid of any buffers for the about-to-be-deleted blocks. bufmgr will * just drop them without bothering to write the contents. diff --git a/src/include/storage/smgr.h b/src/include/storage/smgr.h index 2b57addbdb..0ac1ed8f93 100644 --- a/src/include/storage/smgr.h +++ b/src/include/storage/smgr.h @@ -124,4 +124,20 @@ smgrwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, smgrwritev(reln, forknum, blocknum, &buffer, 1, skipFsync); } +/* + * Hook for plugins to extend smgr functions. + * for example, collect statistics from smgr functions + * via recording the active relfilenode information. + */ +typedef void (*smgrcreate_hook_type)(SMgrRelation reln, ForkNumber forknum, bool isRedo); +extern PGDLLIMPORT smgrcreate_hook_type smgrcreate_hook; +typedef void (*smgrextend_hook_type)(SMgrRelation reln, ForkNumber forknum, + BlockNumber blocknum, const void *buffer, bool skipFsync); +extern PGDLLIMPORT smgrextend_hook_type smgrextend_hook; +typedef void (*smgrtruncate_hook_type)(SMgrRelation reln, ForkNumber *forknum, + int nforks, BlockNumber *nblocks); +extern PGDLLIMPORT smgrtruncate_hook_type smgrtruncate_hook; +typedef void (*smgrdounlinkall_hook_type)(SMgrRelation *rels, int nrels, bool isRedo); +extern PGDLLIMPORT smgrdounlinkall_hook_type smgrdounlinkall_hook; + #endif /* SMGR_H */ -- 2.43.2