@@ -304,6 +304,8 @@ pg_get_wal_fpi_info(PG_FUNCTION_ARGS)
304
304
XLogRecPtr start_lsn ;
305
305
XLogRecPtr end_lsn ;
306
306
XLogReaderState * xlogreader ;
307
+ MemoryContext old_cxt ;
308
+ MemoryContext tmp_cxt ;
307
309
308
310
start_lsn = PG_GETARG_LSN (0 );
309
311
end_lsn = PG_GETARG_LSN (1 );
@@ -314,14 +316,26 @@ pg_get_wal_fpi_info(PG_FUNCTION_ARGS)
314
316
315
317
xlogreader = InitXLogReaderState (start_lsn );
316
318
319
+ tmp_cxt = AllocSetContextCreate (CurrentMemoryContext ,
320
+ "pg_get_wal_fpi_info temporary cxt" ,
321
+ ALLOCSET_DEFAULT_SIZES );
322
+
317
323
while (ReadNextXLogRecord (xlogreader ) &&
318
324
xlogreader -> EndRecPtr <= end_lsn )
319
325
{
326
+ /* Use the tmp context so we can clean up after each tuple is done */
327
+ old_cxt = MemoryContextSwitchTo (tmp_cxt );
328
+
320
329
GetWALFPIInfo (fcinfo , xlogreader );
321
330
331
+ /* clean up and switch back */
332
+ MemoryContextSwitchTo (old_cxt );
333
+ MemoryContextReset (tmp_cxt );
334
+
322
335
CHECK_FOR_INTERRUPTS ();
323
336
}
324
337
338
+ MemoryContextDelete (tmp_cxt );
325
339
pfree (xlogreader -> private_data );
326
340
XLogReaderFree (xlogreader );
327
341
@@ -440,23 +454,37 @@ GetWALRecordsInfo(FunctionCallInfo fcinfo, XLogRecPtr start_lsn,
440
454
ReturnSetInfo * rsinfo = (ReturnSetInfo * ) fcinfo -> resultinfo ;
441
455
Datum values [PG_GET_WAL_RECORDS_INFO_COLS ] = {0 };
442
456
bool nulls [PG_GET_WAL_RECORDS_INFO_COLS ] = {0 };
457
+ MemoryContext old_cxt ;
458
+ MemoryContext tmp_cxt ;
443
459
444
460
InitMaterializedSRF (fcinfo , 0 );
445
461
446
462
xlogreader = InitXLogReaderState (start_lsn );
447
463
464
+ tmp_cxt = AllocSetContextCreate (CurrentMemoryContext ,
465
+ "GetWALRecordsInfo temporary cxt" ,
466
+ ALLOCSET_DEFAULT_SIZES );
467
+
448
468
while (ReadNextXLogRecord (xlogreader ) &&
449
469
xlogreader -> EndRecPtr <= end_lsn )
450
470
{
471
+ /* Use the tmp context so we can clean up after each tuple is done */
472
+ old_cxt = MemoryContextSwitchTo (tmp_cxt );
473
+
451
474
GetWALRecordInfo (xlogreader , values , nulls ,
452
475
PG_GET_WAL_RECORDS_INFO_COLS );
453
476
454
477
tuplestore_putvalues (rsinfo -> setResult , rsinfo -> setDesc ,
455
478
values , nulls );
456
479
480
+ /* clean up and switch back */
481
+ MemoryContextSwitchTo (old_cxt );
482
+ MemoryContextReset (tmp_cxt );
483
+
457
484
CHECK_FOR_INTERRUPTS ();
458
485
}
459
486
487
+ MemoryContextDelete (tmp_cxt );
460
488
pfree (xlogreader -> private_data );
461
489
XLogReaderFree (xlogreader );
462
490
@@ -560,11 +588,13 @@ GetXLogSummaryStats(XLogStats *stats, ReturnSetInfo *rsinfo,
560
588
Datum * values , bool * nulls , uint32 ncols ,
561
589
bool stats_per_record )
562
590
{
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 ;
568
598
569
599
/*
570
600
* Each row shows its percentages of the total, so make a first pass to
@@ -581,6 +611,10 @@ GetXLogSummaryStats(XLogStats *stats, ReturnSetInfo *rsinfo,
581
611
}
582
612
total_len = total_rec_len + total_fpi_len ;
583
613
614
+ tmp_cxt = AllocSetContextCreate (CurrentMemoryContext ,
615
+ "GetXLogSummaryStats temporary cxt" ,
616
+ ALLOCSET_DEFAULT_SIZES );
617
+
584
618
for (ri = 0 ; ri <= RM_MAX_ID ; ri ++ )
585
619
{
586
620
uint64 count ;
@@ -614,6 +648,8 @@ GetXLogSummaryStats(XLogStats *stats, ReturnSetInfo *rsinfo,
614
648
if (count == 0 )
615
649
continue ;
616
650
651
+ old_cxt = MemoryContextSwitchTo (tmp_cxt );
652
+
617
653
/* the upper four bits in xl_info are the rmgr's */
618
654
id = desc .rm_identify (rj << 4 );
619
655
if (id == NULL )
@@ -626,6 +662,10 @@ GetXLogSummaryStats(XLogStats *stats, ReturnSetInfo *rsinfo,
626
662
627
663
tuplestore_putvalues (rsinfo -> setResult , rsinfo -> setDesc ,
628
664
values , nulls );
665
+
666
+ /* clean up and switch back */
667
+ MemoryContextSwitchTo (old_cxt );
668
+ MemoryContextReset (tmp_cxt );
629
669
}
630
670
}
631
671
else
@@ -635,14 +675,22 @@ GetXLogSummaryStats(XLogStats *stats, ReturnSetInfo *rsinfo,
635
675
fpi_len = stats -> rmgr_stats [ri ].fpi_len ;
636
676
tot_len = rec_len + fpi_len ;
637
677
678
+ old_cxt = MemoryContextSwitchTo (tmp_cxt );
679
+
638
680
FillXLogStatsRow (desc .rm_name , count , total_count , rec_len ,
639
681
total_rec_len , fpi_len , total_fpi_len , tot_len ,
640
682
total_len , values , nulls , ncols );
641
683
642
684
tuplestore_putvalues (rsinfo -> setResult , rsinfo -> setDesc ,
643
685
values , nulls );
686
+
687
+ /* clean up and switch back */
688
+ MemoryContextSwitchTo (old_cxt );
689
+ MemoryContextReset (tmp_cxt );
644
690
}
645
691
}
692
+
693
+ MemoryContextDelete (tmp_cxt );
646
694
}
647
695
648
696
/*
0 commit comments