vacuum_stats_v5.patch

text/x-patch

Filename: vacuum_stats_v5.patch
Type: text/x-patch
Part: 0
Message: Re: Displaying accumulated autovacuum cost

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: unified
Series: patch v5
File+
src/backend/commands/vacuum.c 0 0
src/backend/commands/vacuumlazy.c 0 0
src/backend/storage/buffer/bufmgr.c 0 0
src/backend/utils/init/globals.c 0 0
src/include/miscadmin.h 0 0
diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c
index f42504c..6ef85dd 100644
*** a/src/backend/commands/vacuum.c
--- b/src/backend/commands/vacuum.c
*************** vacuum(VacuumStmt *vacstmt, Oid relid, b
*** 214,219 ****
--- 214,222 ----
  
  		VacuumCostActive = (VacuumCostDelay > 0);
  		VacuumCostBalance = 0;
+ 		VacuumPageHit = 0;
+ 		VacuumPageMiss = 0;
+ 		VacuumPageDirty = 0;
  
  		/*
  		 * Loop to process each selected relation.
diff --git a/src/backend/commands/vacuumlazy.c b/src/backend/commands/vacuumlazy.c
index 38deddc..c59fceb 100644
*** a/src/backend/commands/vacuumlazy.c
--- b/src/backend/commands/vacuumlazy.c
*************** lazy_vacuum_rel(Relation onerel, VacuumS
*** 154,160 ****
  	int			nindexes;
  	BlockNumber possibly_freeable;
  	PGRUsage	ru0;
! 	TimestampTz starttime = 0;
  	bool		scan_all;
  	TransactionId freezeTableLimit;
  	BlockNumber new_rel_pages;
--- 154,163 ----
  	int			nindexes;
  	BlockNumber possibly_freeable;
  	PGRUsage	ru0;
! 	TimestampTz starttime = 0, endtime;
!  	long		secs;
!  	int			usecs;
!  	double		read_rate, write_rate;
  	bool		scan_all;
  	TransactionId freezeTableLimit;
  	BlockNumber new_rel_pages;
*************** lazy_vacuum_rel(Relation onerel, VacuumS
*** 166,173 ****
  	if (IsAutoVacuumWorkerProcess() && Log_autovacuum_min_duration >= 0)
  	{
  		pg_rusage_init(&ru0);
! 		if (Log_autovacuum_min_duration > 0)
! 			starttime = GetCurrentTimestamp();
  	}
  
  	if (vacstmt->options & VACOPT_VERBOSE)
--- 169,175 ----
  	if (IsAutoVacuumWorkerProcess() && Log_autovacuum_min_duration >= 0)
  	{
  		pg_rusage_init(&ru0);
! 		starttime = GetCurrentTimestamp();
  	}
  
  	if (vacstmt->options & VACOPT_VERBOSE)
*************** lazy_vacuum_rel(Relation onerel, VacuumS
*** 262,274 ****
  	/* and log the action if appropriate */
  	if (IsAutoVacuumWorkerProcess() && Log_autovacuum_min_duration >= 0)
  	{
  		if (Log_autovacuum_min_duration == 0 ||
! 			TimestampDifferenceExceeds(starttime, GetCurrentTimestamp(),
  									   Log_autovacuum_min_duration))
  			ereport(LOG,
  					(errmsg("automatic vacuum of table \"%s.%s.%s\": index scans: %d\n"
  							"pages: %d removed, %d remain\n"
  							"tuples: %.0f removed, %.0f remain\n"
  							"system usage: %s",
  							get_database_name(MyDatabaseId),
  							get_namespace_name(RelationGetNamespace(onerel)),
--- 264,290 ----
  	/* and log the action if appropriate */
  	if (IsAutoVacuumWorkerProcess() && Log_autovacuum_min_duration >= 0)
  	{
+ 		endtime = GetCurrentTimestamp();
  		if (Log_autovacuum_min_duration == 0 ||
! 			TimestampDifferenceExceeds(starttime, endtime,
  									   Log_autovacuum_min_duration))
+ 		{
+ 			TimestampDifference(starttime, endtime, &secs, &usecs);
+ 			read_rate = 0;
+ 			write_rate = 0;
+ 			if ((secs > 0) || (usecs > 0))
+ 			{
+ 				read_rate = (double) BLCKSZ * VacuumPageMiss / (1024 * 1024) /
+ 					(secs + usecs / 1000000.0);
+ 				write_rate = (double) BLCKSZ * VacuumPageDirty / (1024 * 1024) /
+  					(secs + usecs / 1000000.0);
+ 			}
  			ereport(LOG,
  					(errmsg("automatic vacuum of table \"%s.%s.%s\": index scans: %d\n"
  							"pages: %d removed, %d remain\n"
  							"tuples: %.0f removed, %.0f remain\n"
+ 							"buffer usage: %d hits, %d misses, %d dirtied\n"
+ 							"avg read rate: %.3f MiB/s, avg write rate: %.3f MiB/s\n"
  							"system usage: %s",
  							get_database_name(MyDatabaseId),
  							get_namespace_name(RelationGetNamespace(onerel)),
*************** lazy_vacuum_rel(Relation onerel, VacuumS
*** 277,284 ****
  							vacrelstats->pages_removed,
  							vacrelstats->rel_pages,
  							vacrelstats->tuples_deleted,
! 							new_rel_tuples,
  							pg_rusage_show(&ru0))));
  	}
  }
  
--- 293,305 ----
  							vacrelstats->pages_removed,
  							vacrelstats->rel_pages,
  							vacrelstats->tuples_deleted,
! 							vacrelstats->new_rel_tuples,
! 							VacuumPageHit,
! 							VacuumPageMiss,
! 							VacuumPageDirty,
! 							read_rate,write_rate,
  							pg_rusage_show(&ru0))));
+ 		}
  	}
  }
  
diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index e59af33..5306cb4 100644
*** a/src/backend/storage/buffer/bufmgr.c
--- b/src/backend/storage/buffer/bufmgr.c
*************** ReadBuffer_common(SMgrRelation smgr, cha
*** 340,345 ****
--- 340,346 ----
  		{
  			/* Just need to update stats before we exit */
  			*hit = true;
+ 			VacuumPageHit++;
  
  			if (VacuumCostActive)
  				VacuumCostBalance += VacuumCostPageHit;
*************** ReadBuffer_common(SMgrRelation smgr, cha
*** 471,476 ****
--- 472,478 ----
  		TerminateBufferIO(bufHdr, false, BM_VALID);
  	}
  
+ 	VacuumPageMiss++;
  	if (VacuumCostActive)
  		VacuumCostBalance += VacuumCostPageMiss;
  
*************** MarkBufferDirty(Buffer buffer)
*** 972,981 ****
  	Assert(bufHdr->refcount > 0);
  
  	/*
! 	 * If the buffer was not dirty already, do vacuum cost accounting.
  	 */
! 	if (!(bufHdr->flags & BM_DIRTY) && VacuumCostActive)
! 		VacuumCostBalance += VacuumCostPageDirty;
  
  	bufHdr->flags |= (BM_DIRTY | BM_JUST_DIRTIED);
  
--- 974,987 ----
  	Assert(bufHdr->refcount > 0);
  
  	/*
! 	 * If the buffer was not dirty already, do vacuum accounting.
  	 */
! 	if (!(bufHdr->flags & BM_DIRTY))
! 	{
! 		VacuumPageDirty++;
! 		if (VacuumCostActive)
! 			VacuumCostBalance += VacuumCostPageDirty;
! 	}
  
  	bufHdr->flags |= (BM_DIRTY | BM_JUST_DIRTIED);
  
*************** SetBufferCommitInfoNeedsSave(Buffer buff
*** 2326,2333 ****
  	{
  		LockBufHdr(bufHdr);
  		Assert(bufHdr->refcount > 0);
! 		if (!(bufHdr->flags & BM_DIRTY) && VacuumCostActive)
! 			VacuumCostBalance += VacuumCostPageDirty;
  		bufHdr->flags |= (BM_DIRTY | BM_JUST_DIRTIED);
  		UnlockBufHdr(bufHdr);
  	}
--- 2332,2343 ----
  	{
  		LockBufHdr(bufHdr);
  		Assert(bufHdr->refcount > 0);
! 		if (!(bufHdr->flags & BM_DIRTY))
! 		{
! 			VacuumPageDirty++;
! 			if (VacuumCostActive)
! 				VacuumCostBalance += VacuumCostPageDirty;
! 		}
  		bufHdr->flags |= (BM_DIRTY | BM_JUST_DIRTIED);
  		UnlockBufHdr(bufHdr);
  	}
diff --git a/src/backend/utils/init/globals.c b/src/backend/utils/init/globals.c
index c4c4154..9ce64e6 100644
*** a/src/backend/utils/init/globals.c
--- b/src/backend/utils/init/globals.c
*************** int			VacuumCostPageDirty = 20;
*** 115,120 ****
--- 115,124 ----
  int			VacuumCostLimit = 200;
  int			VacuumCostDelay = 0;
  
+ int			VacuumPageHit = 0;
+ int			VacuumPageMiss = 0;
+ int			VacuumPageDirty = 0;
+ 
  int			VacuumCostBalance = 0;		/* working state for vacuum */
  bool		VacuumCostActive = false;
  
diff --git a/src/include/miscadmin.h b/src/include/miscadmin.h
index 9d19417..4ee08fe 100644
*** a/src/include/miscadmin.h
--- b/src/include/miscadmin.h
*************** extern int	VacuumCostPageDirty;
*** 230,235 ****
--- 230,239 ----
  extern int	VacuumCostLimit;
  extern int	VacuumCostDelay;
  
+ extern int	VacuumPageHit;
+ extern int	VacuumPageMiss;
+ extern int	VacuumPageDirty;
+ 
  extern int	VacuumCostBalance;
  extern bool VacuumCostActive;