@@ -304,6 +304,8 @@ pg_get_wal_fpi_info(PG_FUNCTION_ARGS)
304304XLogRecPtr start_lsn ;
305305XLogRecPtr end_lsn ;
306306XLogReaderState * xlogreader ;
307+ MemoryContext old_cxt ;
308+ MemoryContext tmp_cxt ;
307309
308310start_lsn = PG_GETARG_LSN (0 );
309311end_lsn = PG_GETARG_LSN (1 );
@@ -314,14 +316,26 @@ pg_get_wal_fpi_info(PG_FUNCTION_ARGS)
314316
315317xlogreader = InitXLogReaderState (start_lsn );
316318
319+ tmp_cxt = AllocSetContextCreate (CurrentMemoryContext ,
320+ "pg_get_wal_fpi_info temporary cxt" ,
321+ ALLOCSET_DEFAULT_SIZES );
322+
317323while (ReadNextXLogRecord (xlogreader )&&
318324xlogreader -> EndRecPtr <=end_lsn )
319325{
326+ /* Use the tmp context so we can clean up after each tuple is done */
327+ old_cxt = MemoryContextSwitchTo (tmp_cxt );
328+
320329GetWALFPIInfo (fcinfo ,xlogreader );
321330
331+ /* clean up and switch back */
332+ MemoryContextSwitchTo (old_cxt );
333+ MemoryContextReset (tmp_cxt );
334+
322335CHECK_FOR_INTERRUPTS ();
323336}
324337
338+ MemoryContextDelete (tmp_cxt );
325339pfree (xlogreader -> private_data );
326340XLogReaderFree (xlogreader );
327341
@@ -440,23 +454,37 @@ GetWALRecordsInfo(FunctionCallInfo fcinfo, XLogRecPtr start_lsn,
440454ReturnSetInfo * rsinfo = (ReturnSetInfo * )fcinfo -> resultinfo ;
441455Datum values [PG_GET_WAL_RECORDS_INFO_COLS ]= {0 };
442456bool nulls [PG_GET_WAL_RECORDS_INFO_COLS ]= {0 };
457+ MemoryContext old_cxt ;
458+ MemoryContext tmp_cxt ;
443459
444460InitMaterializedSRF (fcinfo ,0 );
445461
446462xlogreader = InitXLogReaderState (start_lsn );
447463
464+ tmp_cxt = AllocSetContextCreate (CurrentMemoryContext ,
465+ "GetWALRecordsInfo temporary cxt" ,
466+ ALLOCSET_DEFAULT_SIZES );
467+
448468while (ReadNextXLogRecord (xlogreader )&&
449469xlogreader -> EndRecPtr <=end_lsn )
450470{
471+ /* Use the tmp context so we can clean up after each tuple is done */
472+ old_cxt = MemoryContextSwitchTo (tmp_cxt );
473+
451474GetWALRecordInfo (xlogreader ,values ,nulls ,
452475PG_GET_WAL_RECORDS_INFO_COLS );
453476
454477tuplestore_putvalues (rsinfo -> setResult ,rsinfo -> setDesc ,
455478values ,nulls );
456479
480+ /* clean up and switch back */
481+ MemoryContextSwitchTo (old_cxt );
482+ MemoryContextReset (tmp_cxt );
483+
457484CHECK_FOR_INTERRUPTS ();
458485}
459486
487+ MemoryContextDelete (tmp_cxt );
460488pfree (xlogreader -> private_data );
461489XLogReaderFree (xlogreader );
462490
@@ -560,11 +588,13 @@ GetXLogSummaryStats(XLogStats *stats, ReturnSetInfo *rsinfo,
560588Datum * values ,bool * nulls ,uint32 ncols ,
561589bool stats_per_record )
562590{
563- uint64 total_count = 0 ;
564- uint64 total_rec_len = 0 ;
565- uint64 total_fpi_len = 0 ;
566- uint64 total_len = 0 ;
567- int ri ;
591+ MemoryContext old_cxt ;
592+ MemoryContext tmp_cxt ;
593+ uint64 total_count = 0 ;
594+ uint64 total_rec_len = 0 ;
595+ uint64 total_fpi_len = 0 ;
596+ uint64 total_len = 0 ;
597+ int ri ;
568598
569599/*
570600 * Each row shows its percentages of the total, so make a first pass to
@@ -581,6 +611,10 @@ GetXLogSummaryStats(XLogStats *stats, ReturnSetInfo *rsinfo,
581611}
582612total_len = total_rec_len + total_fpi_len ;
583613
614+ tmp_cxt = AllocSetContextCreate (CurrentMemoryContext ,
615+ "GetXLogSummaryStats temporary cxt" ,
616+ ALLOCSET_DEFAULT_SIZES );
617+
584618for (ri = 0 ;ri <=RM_MAX_ID ;ri ++ )
585619{
586620uint64 count ;
@@ -614,6 +648,8 @@ GetXLogSummaryStats(XLogStats *stats, ReturnSetInfo *rsinfo,
614648if (count == 0 )
615649continue ;
616650
651+ old_cxt = MemoryContextSwitchTo (tmp_cxt );
652+
617653/* the upper four bits in xl_info are the rmgr's */
618654id = desc .rm_identify (rj <<4 );
619655if (id == NULL )
@@ -626,6 +662,10 @@ GetXLogSummaryStats(XLogStats *stats, ReturnSetInfo *rsinfo,
626662
627663tuplestore_putvalues (rsinfo -> setResult ,rsinfo -> setDesc ,
628664values ,nulls );
665+
666+ /* clean up and switch back */
667+ MemoryContextSwitchTo (old_cxt );
668+ MemoryContextReset (tmp_cxt );
629669}
630670}
631671else
@@ -635,14 +675,22 @@ GetXLogSummaryStats(XLogStats *stats, ReturnSetInfo *rsinfo,
635675fpi_len = stats -> rmgr_stats [ri ].fpi_len ;
636676tot_len = rec_len + fpi_len ;
637677
678+ old_cxt = MemoryContextSwitchTo (tmp_cxt );
679+
638680FillXLogStatsRow (desc .rm_name ,count ,total_count ,rec_len ,
639681total_rec_len ,fpi_len ,total_fpi_len ,tot_len ,
640682total_len ,values ,nulls ,ncols );
641683
642684tuplestore_putvalues (rsinfo -> setResult ,rsinfo -> setDesc ,
643685values ,nulls );
686+
687+ /* clean up and switch back */
688+ MemoryContextSwitchTo (old_cxt );
689+ MemoryContextReset (tmp_cxt );
644690}
645691}
692+
693+ MemoryContextDelete (tmp_cxt );
646694}
647695
648696/*