@@ -50,14 +50,17 @@ file_extend_hook_type file_extend_hook = NULL;
5050file_truncate_hook_type file_truncate_hook = NULL ;
5151file_unlink_hook_type file_unlink_hook = NULL ;
5252
53+ smgr_get_impl_hook_type smgr_get_impl_hook = NULL ;
54+
5355/* Hook for plugins to get control in smgr */
5456smgr_init_hook_type smgr_init_hook = NULL ;
5557smgr_hook_type smgr_hook = NULL ;
5658smgr_shutdown_hook_type smgr_shutdown_hook = NULL ;
57-
58- static const f_smgr smgrsw [] = {
59+ #define SMGR_MAX_ID UINT8_MAX
60+ static f_smgr smgrsw [SMGR_MAX_ID + 1 ] = {
5961 /* magnetic disk */
6062 {
63+ .smgr_name = "heap" ,
6164 .smgr_init = mdinit ,
6265 .smgr_shutdown = NULL ,
6366 .smgr_open = mdopen ,
@@ -82,6 +85,7 @@ static const f_smgr smgrsw[] = {
8285 * Append-optimized relation files currently fall in this category.
8386 */
8487 {
88+ .smgr_name = "ao" ,
8589 .smgr_init = mdinit ,
8690 .smgr_shutdown = NULL ,
8791 .smgr_open = mdopen ,
@@ -115,8 +119,6 @@ static const f_smgr_ao smgrswao[] = {
115119};
116120
117121
118- static const int NSmgr = lengthof (smgrsw );
119-
120122/*
121123 * Each backend has a hashtable that stores all extant SMgrRelation objects.
122124 * In addition, "unowned" SMgrRelation objects are chained together in a list.
@@ -128,6 +130,79 @@ static dlist_head unowned_relns;
128130/* local function prototypes */
129131static void smgrshutdown (int code , Datum arg );
130132
133+ void smgr_register (const f_smgr * smgr , SMgrImpl smgr_impl )
134+ {
135+ if (!process_shared_preload_libraries_in_progress )
136+ {
137+ ereport (ERROR , (errmsg ("smgr_register not in shared_preload_libraries" )));
138+ }
139+
140+ if (smgr_impl > SMGR_MAX_ID )
141+ {
142+ elog (ERROR , "smgr_impl is out of range" );
143+ }
144+
145+ if (smgr -> smgr_name == NULL )
146+ {
147+ elog (ERROR , "smgr_name is not set" );
148+ }
149+
150+ // check if the smgr_impl is already registered, avoid conflict with existing smgr_impl
151+ if (smgrsw [smgr_impl ].smgr_name != NULL )
152+ {
153+ elog (ERROR , "smgr_impl is already registered" );
154+ }
155+
156+ smgrsw [smgr_impl ] = * smgr ;
157+ }
158+
159+ const f_smgr * smgr_get (SMgrImpl smgr_impl )
160+ {
161+ if (smgr_impl > SMGR_MAX_ID )
162+ {
163+ elog (ERROR , "smgr_impl is out of range" );
164+ }
165+
166+ if (smgrsw [smgr_impl ].smgr_name == NULL )
167+ {
168+ elog (ERROR , "smgr_impl is not registered" );
169+ }
170+
171+ return & smgrsw [smgr_impl ];
172+ }
173+
174+ /*
175+ * smgr_get_impl() is used to get the smgr id of the relation.
176+ *
177+ * FIXME: For PAX_AM_OID, Cloudberrydb reserves this value for ORCA, a
178+ * predefined value is used here to reserve the smgr id for PAX_AM_OID.
179+ * should we add a hook to get the smgr id of the relation?
180+ */
181+ SMgrImpl smgr_get_impl (const Relation rel )
182+ {
183+ SMgrImpl smgr_impl = SMGR_MD ;
184+
185+ if (RelationIsAppendOptimized (rel ))
186+ {
187+ smgr_impl = SMGR_AO ;
188+ }
189+ else if (RelationIsPax (rel ))
190+ {
191+ smgr_impl = SMGR_PAX ;
192+ }
193+ else
194+ {
195+ if (smgr_get_impl_hook )
196+ {
197+ (* smgr_get_impl_hook )(rel , & smgr_impl );
198+ Assert (smgr_impl >= SMGR_MD && smgr_impl <= SMGR_MAX_ID );
199+ Assert (smgrsw [smgr_impl ].smgr_name != NULL );
200+ }
201+ }
202+
203+
204+ return smgr_impl ;
205+ }
131206
132207/*
133208 * smgrinit(), smgrshutdown() -- Initialize or shut down storage
@@ -142,7 +217,7 @@ smgrinit(void)
142217{
143218 int i ;
144219
145- for (i = 0 ; i < NSmgr ; i ++ )
220+ for (i = 0 ; i <= SMGR_MAX_ID ; i ++ )
146221 {
147222 if (smgrsw [i ].smgr_init )
148223 smgrsw [i ].smgr_init ();
@@ -163,7 +238,7 @@ smgrshutdown(int code, Datum arg)
163238{
164239 int i ;
165240
166- for (i = 0 ; i < NSmgr ; i ++ )
241+ for (i = 0 ; i <= SMGR_MAX_ID ; i ++ )
167242 {
168243 if (smgrsw [i ].smgr_shutdown )
169244 smgrsw [i ].smgr_shutdown ();
@@ -777,3 +852,10 @@ AtEOXact_SMgr(void)
777852 smgrclose (rel );
778853 }
779854}
855+
856+ const char * smgr_get_name (SMgrImpl impl )
857+ {
858+ if (impl > SMGR_MAX_ID )
859+ return "invalid" ;
860+ return smgrsw [impl ].smgr_name ? smgrsw [impl ].smgr_name : "unknown" ;
861+ }
0 commit comments