@@ -46,6 +46,7 @@ PG_FUNCTION_INFO_V1( get_base_type_pl );
4646PG_FUNCTION_INFO_V1 (get_partition_key_type );
4747PG_FUNCTION_INFO_V1 (get_tablespace_pl );
4848
49+ PG_FUNCTION_INFO_V1 (show_cache_stats_internal );
4950PG_FUNCTION_INFO_V1 (show_partition_list_internal );
5051
5152PG_FUNCTION_INFO_V1 (build_update_trigger_func_name );
@@ -72,9 +73,7 @@ PG_FUNCTION_INFO_V1( debug_capture );
7273PG_FUNCTION_INFO_V1 (get_pathman_lib_version );
7374
7475
75- /*
76- * User context for function show_partition_list_internal().
77- */
76+ /* User context for function show_partition_list_internal() */
7877typedef struct
7978{
8079Relation pathman_config ;
@@ -86,6 +85,13 @@ typedef struct
8685uint32 child_number ;/* child we're looking at */
8786}show_partition_list_cxt ;
8887
88+ /* User context for function show_pathman_cache_stats_internal() */
89+ typedef struct
90+ {
91+ MemoryContext pathman_contexts [PATHMAN_MCXT_COUNT ];
92+ int current_context ;
93+ }show_cache_stats_cxt ;
94+
8995
9096static void on_partitions_created_internal (Oid partitioned_table ,bool add_callbacks );
9197static void on_partitions_updated_internal (Oid partitioned_table ,bool add_callbacks );
@@ -265,6 +271,92 @@ get_tablespace_pl(PG_FUNCTION_ARGS)
265271 * ----------------------
266272 */
267273
274+ Datum
275+ show_cache_stats_internal (PG_FUNCTION_ARGS )
276+ {
277+ show_cache_stats_cxt * usercxt ;
278+ FuncCallContext * funccxt ;
279+
280+ /*
281+ * Initialize tuple descriptor & function call context.
282+ */
283+ if (SRF_IS_FIRSTCALL ())
284+ {
285+ TupleDesc tupdesc ;
286+ MemoryContext old_mcxt ;
287+
288+ funccxt = SRF_FIRSTCALL_INIT ();
289+
290+ old_mcxt = MemoryContextSwitchTo (funccxt -> multi_call_memory_ctx );
291+
292+ usercxt = (show_cache_stats_cxt * )palloc (sizeof (show_cache_stats_cxt ));
293+
294+ usercxt -> pathman_contexts [0 ]= TopPathmanContext ;
295+ usercxt -> pathman_contexts [1 ]= PathmanRelationCacheContext ;
296+ usercxt -> pathman_contexts [2 ]= PathmanParentCacheContext ;
297+ usercxt -> pathman_contexts [3 ]= PathmanCostraintCacheContext ;
298+
299+ usercxt -> current_context = 0 ;
300+
301+ /* Create tuple descriptor */
302+ tupdesc = CreateTemplateTupleDesc (Natts_pathman_cache_stats , false);
303+
304+ TupleDescInitEntry (tupdesc ,Anum_pathman_cs_context ,
305+ "context" ,TEXTOID ,-1 ,0 );
306+ TupleDescInitEntry (tupdesc ,Anum_pathman_cs_size ,
307+ "size" ,INT8OID ,-1 ,0 );
308+ TupleDescInitEntry (tupdesc ,Anum_pathman_cs_used ,
309+ "used" ,INT8OID ,-1 ,0 );
310+
311+ funccxt -> tuple_desc = BlessTupleDesc (tupdesc );
312+ funccxt -> user_fctx = (void * )usercxt ;
313+
314+ MemoryContextSwitchTo (old_mcxt );
315+ }
316+
317+ funccxt = SRF_PERCALL_SETUP ();
318+ usercxt = (show_cache_stats_cxt * )funccxt -> user_fctx ;
319+
320+ if (usercxt -> current_context < lengthof (usercxt -> pathman_contexts ))
321+ {
322+ MemoryContext current_mcxt ;
323+ MemoryContextCounters mcxt_stats ;
324+ HeapTuple htup ;
325+ Datum values [Natts_pathman_cache_stats ];
326+ bool isnull [Natts_pathman_cache_stats ]= {0 };
327+
328+ /* Prepare context counters */
329+ memset (& mcxt_stats ,0 ,sizeof (mcxt_stats ));
330+
331+ /* Select current memory context */
332+ current_mcxt = usercxt -> pathman_contexts [usercxt -> current_context ];
333+
334+ /* NOTE: we do not consider child contexts if it's TopPathmanContext */
335+ McxtStatsInternal (current_mcxt ,0 ,
336+ (current_mcxt != TopPathmanContext ),
337+ & mcxt_stats );
338+
339+ values [Anum_pathman_cs_context - 1 ]=
340+ CStringGetTextDatum (simpify_mcxt_name (current_mcxt ));
341+
342+ values [Anum_pathman_cs_size - 1 ]=
343+ Int64GetDatum (mcxt_stats .totalspace );
344+
345+ values [Anum_pathman_cs_used - 1 ]=
346+ Int64GetDatum (mcxt_stats .totalspace - mcxt_stats .freespace );
347+
348+ /* Switch to next context */
349+ usercxt -> current_context ++ ;
350+
351+ /* Form output tuple */
352+ htup = heap_form_tuple (funccxt -> tuple_desc ,values ,isnull );
353+
354+ SRF_RETURN_NEXT (funccxt ,HeapTupleGetDatum (htup ));
355+ }
356+
357+ SRF_RETURN_DONE (funccxt );
358+ }
359+
268360/*
269361 * List all existing partitions and their parents.
270362 */