From 6497a7fd71f62d7dd279ce8b239c5abc35c5c459 Mon Sep 17 00:00:00 2001
From: Jan Wieck <JanWieck@Yahoo.com>
Date: Thu, 5 Jul 2001 15:19:40 +0000
Subject: [PATCH] Added GUC configuration options to control access statistics.

Jan
---
 src/backend/postmaster/pgstat.c               | 59 ++++++++++++++++---
 src/backend/utils/misc/guc.c                  |  9 ++-
 src/backend/utils/misc/postgresql.conf.sample | 10 ++++
 src/include/pgstat.h                          | 42 ++++++++-----
 4 files changed, 95 insertions(+), 25 deletions(-)

diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c
index 9332b7368ec..8efda5c90ff 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -14,12 +14,9 @@
  *			  thus an initdb and we might want to provide this as a
  *			  patch for 7.1.
  *
- *			- Make the functions from contrib/pgstat_tmp builtin
- *			  and create the views on initdb.
- *
  *	Copyright (c) 2001, PostgreSQL Global Development Group
  *
- *	$Id: pgstat.c,v 1.3 2001/06/30 19:01:27 petere Exp $
+ *	$Id: pgstat.c,v 1.4 2001/07/05 15:19:40 wieck Exp $
  * ----------
  */
 #include "postgres.h"
@@ -56,7 +53,11 @@
  * Global data
  * ----------
  */
-
+bool	pgstat_collect_startcollector	= true;
+bool	pgstat_collect_resetonpmstart	= true;
+bool	pgstat_collect_querystring		= false;
+bool	pgstat_collect_tuplelevel		= false;
+bool	pgstat_collect_blocklevel		= false;
 
 /* ----------
  * Local data
@@ -135,6 +136,13 @@ pgstat_init(void)
 {
 	int			alen;
 
+	/*
+	 * Force start of collector daemon if something to collect
+	 */
+	if (pgstat_collect_querystring || pgstat_collect_tuplelevel ||
+				pgstat_collect_blocklevel)
+		pgstat_collect_startcollector = true;
+
 	/*
 	 * Initialize the filenames for the status reports.
 	 */
@@ -143,6 +151,20 @@ pgstat_init(void)
 	snprintf(pgStat_fname, MAXPGPATH, 
 				PGSTAT_STAT_FILENAME, DataDir);
 
+	/*
+	 * If we don't have to start a collector or should reset the
+	 * collected statistics on postmaster start, simply remove the
+	 * file.
+	 */
+	if (!pgstat_collect_startcollector || pgstat_collect_resetonpmstart)
+		unlink(pgStat_fname);
+
+	/*
+	 * Nothing else required if collector will not get started
+	 */
+	if (!pgstat_collect_startcollector)
+		return 0;
+
 	/*
 	 * Create the UDP socket for receiving statistic messages
 	 */
@@ -211,6 +233,12 @@ pgstat_init(void)
 int
 pgstat_start(void)
 {
+	/*
+	 * Do nothing if no collector needed
+	 */
+	if (!pgstat_collect_startcollector)
+		return 0;
+
 	/*
 	 * Check that the socket at least is there
 	 */
@@ -275,6 +303,9 @@ pgstat_beterm(int pid)
 {
 	PgStat_MsgBeterm		msg;
 
+	if (!pgstat_collect_startcollector)
+		return;
+
 	msg.m_hdr.m_type		= PGSTAT_MTYPE_BETERM;
 	msg.m_hdr.m_backendid	= 0;
 	msg.m_hdr.m_procpid		= pid;
@@ -302,7 +333,7 @@ pgstat_bestart(void)
 {
 	PgStat_MsgBestart		msg;
 
-	if (pgStatSock < 0)
+	if (!pgstat_collect_startcollector || pgStatSock < 0)
 		return;
 
 	pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_BESTART);
@@ -324,7 +355,7 @@ pgstat_report_activity(char *what)
 	PgStat_MsgActivity	msg;
 	int						len;
 
-	if (pgStatSock < 0)
+	if (!pgstat_collect_querystring || pgStatSock < 0)
 		return;
 
 	len = strlen(what);
@@ -354,6 +385,10 @@ pgstat_report_tabstat(void)
 	int				n;
 	int				len;
 
+	if (!pgstat_collect_querystring && !pgstat_collect_tuplelevel &&
+			!pgstat_collect_blocklevel)
+		return;
+
 	if (pgStatSock < 0)
 		return;
 
@@ -654,7 +689,7 @@ pgstat_initstats(PgStat_Info *stats, Relation rel)
 	stats->heap_scan_counted	= FALSE;
 	stats->index_scan_counted	= FALSE;
 
-	if (pgStatSock < 0)
+	if (!pgstat_collect_startcollector || pgStatSock < 0)
 	{
 		stats->no_stats = TRUE;
 		return;
@@ -764,6 +799,10 @@ pgstat_initstats(PgStat_Info *stats, Relation rel)
 void
 pgstat_count_xact_commit(void)
 {
+	if (!pgstat_collect_querystring && !pgstat_collect_tuplelevel &&
+			!pgstat_collect_blocklevel)
+		return;
+
 	pgStatXactCommit++;
 
 	/*
@@ -791,6 +830,10 @@ pgstat_count_xact_commit(void)
 void
 pgstat_count_xact_rollback(void)
 {
+	if (!pgstat_collect_querystring && !pgstat_collect_tuplelevel &&
+			!pgstat_collect_blocklevel)
+		return;
+
 	pgStatXactRollback++;
 
 	/*
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index 96dc8399e1c..cc666047815 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -4,7 +4,7 @@
  * Support for grand unified configuration scheme, including SET
  * command, configuration file, and command line options.
  *
- * $Header: /cvsroot/pgsql/src/backend/utils/misc/guc.c,v 1.44 2001/06/30 22:03:26 petere Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/misc/guc.c,v 1.45 2001/07/05 15:19:40 wieck Exp $
  *
  * Copyright 2000 by PostgreSQL Global Development Group
  * Written by Peter Eisentraut <peter_e@gmx.net>.
@@ -36,6 +36,7 @@
 #include "storage/proc.h"
 #include "tcop/tcopprot.h"
 #include "utils/datetime.h"
+#include "pgstat.h"
 
 
 /* XXX these should be in other modules' header files */
@@ -225,6 +226,12 @@ static struct config_bool
 	{"show_btree_build_stats", PGC_SUSET, &Show_btree_build_stats, false, NULL},
 #endif
 
+	{"collect_startcollector", PGC_POSTMASTER, &pgstat_collect_startcollector, true, NULL},
+	{"collect_resetonpmstart", PGC_POSTMASTER, &pgstat_collect_resetonpmstart, true, NULL},
+	{"collect_querystring", PGC_SUSET, &pgstat_collect_querystring, false, NULL},
+	{"collect_tuplelevel", PGC_SUSET, &pgstat_collect_tuplelevel, false, NULL},
+	{"collect_blocklevel", PGC_SUSET, &pgstat_collect_blocklevel, false, NULL},
+
 	{"trace_notify", PGC_USERSET, &Trace_notify, false, NULL},
 
 #ifdef LOCK_DEBUG
diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample
index fad01e7f32e..6b25bbca6c6 100644
--- a/src/backend/utils/misc/postgresql.conf.sample
+++ b/src/backend/utils/misc/postgresql.conf.sample
@@ -149,6 +149,16 @@
 #endif
 
 
+#
+#	Access statistics collection
+#
+#collect_startcollector = true
+#collect_resetonpmstart = true
+#collect_querystring = false
+#collect_tuplelevel = false
+#collect_blocklevel = false
+
+
 #
 #	Lock Tracing
 #
diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index 5bea15c4577..51bb00dc16b 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -5,7 +5,7 @@
  *
  *	Copyright (c) 2001, PostgreSQL Global Development Group
  *
- *  $Id: pgstat.h,v 1.3 2001/06/29 23:03:02 tgl Exp $
+ *  $Id: pgstat.h,v 1.4 2001/07/05 15:19:40 wieck Exp $
  * ----------
  */
 #ifndef PGSTAT_H
@@ -321,7 +321,15 @@ typedef union	PgStat_Msg
 } PgStat_Msg;
 
 
-
+/* ----------
+ * Global variables
+ * ----------
+ */
+extern bool		pgstat_collect_startcollector;
+extern bool		pgstat_collect_resetonpmstart;
+extern bool		pgstat_collect_querystring;
+extern bool		pgstat_collect_tuplelevel;
+extern bool		pgstat_collect_blocklevel;
 
 /* ----------
  * Functions called from postmaster
@@ -350,64 +358,66 @@ extern void		pgstat_initstats(PgStat_Info *stats, Relation rel);
 
 #define pgstat_reset_heap_scan(s)										\
 	do {																\
-		if ((s)->tabentry != NULL)										\
+		if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)			\
 			(s)->heap_scan_counted = FALSE;								\
 	} while (0)
 #define pgstat_count_heap_scan(s)										\
 	do {																\
-		if ((s)->tabentry != NULL && !(s)->heap_scan_counted) {			\
+		if (pgstat_collect_tuplelevel && (s)->tabentry != NULL && 		\
+				!(s)->heap_scan_counted) {								\
 			((PgStat_TableEntry *)((s)->tabentry))->t_numscans++;		\
 			(s)->heap_scan_counted = TRUE;								\
 		}																\
 	} while (0)
 #define pgstat_count_heap_getnext(s)									\
 	do {																\
-		if ((s)->tabentry != NULL)										\
+		if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)			\
 			((PgStat_TableEntry *)((s)->tabentry))->t_tuples_returned++; \
 	} while (0)
 #define pgstat_count_heap_fetch(s)										\
 	do {																\
-		if ((s)->tabentry != NULL)										\
+		if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)			\
 			((PgStat_TableEntry *)((s)->tabentry))->t_tuples_fetched++;	\
 	} while (0)
 #define pgstat_count_heap_insert(s)										\
 	do {																\
-		if ((s)->tabentry != NULL)										\
+		if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)			\
 			((PgStat_TableEntry *)((s)->tabentry))->t_tuples_inserted++; \
 	} while (0)
 #define pgstat_count_heap_update(s)										\
 	do {																\
-		if ((s)->tabentry != NULL)										\
+		if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)			\
 			((PgStat_TableEntry *)((s)->tabentry))->t_tuples_updated++;	\
 	} while (0)
 #define pgstat_count_heap_delete(s)										\
 	do {																\
-		if ((s)->tabentry != NULL)										\
+		if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)			\
 			((PgStat_TableEntry *)((s)->tabentry))->t_tuples_deleted++;	\
 	} while (0)
 #define pgstat_reset_index_scan(s)										\
 	do {																\
-		if ((s)->tabentry != NULL)										\
+		if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)			\
 			(s)->index_scan_counted = FALSE;							\
 	} while (0)
 #define pgstat_count_index_scan(s)										\
 	do {																\
-		if ((s)->tabentry != NULL && !(s)->index_scan_counted) {		\
+		if (pgstat_collect_tuplelevel && (s)->tabentry != NULL && 		\
+				!(s)->index_scan_counted) {								\
 			((PgStat_TableEntry *)((s)->tabentry))->t_numscans++;		\
 			(s)->index_scan_counted = TRUE;								\
 		}																\
 	} while (0)
 #define pgstat_count_index_getnext(s)									\
 	do {																\
-		if ((s)->tabentry != NULL)										\
+		if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)			\
 			((PgStat_TableEntry *)((s)->tabentry))->t_tuples_returned++; \
 	} while (0)
 #define pgstat_count_buffer_read(s,r)									\
 	do {																\
-		if ((s)->tabentry != NULL)										\
+		if (pgstat_collect_blocklevel && (s)->tabentry != NULL)			\
 			((PgStat_TableEntry *)((s)->tabentry))->t_blocks_fetched++;	\
 		else {															\
-			if (!(s)->no_stats) {										\
+			if (pgstat_collect_blocklevel && !(s)->no_stats) {			\
 				pgstat_initstats((s), (r));								\
 				if ((s)->tabentry != NULL)								\
 					((PgStat_TableEntry *)((s)->tabentry))->t_blocks_fetched++; \
@@ -416,10 +426,10 @@ extern void		pgstat_initstats(PgStat_Info *stats, Relation rel);
 	} while (0)
 #define pgstat_count_buffer_hit(s,r)									\
 	do {																\
-		if ((s)->tabentry != NULL)										\
+		if (pgstat_collect_blocklevel && (s)->tabentry != NULL)			\
 			((PgStat_TableEntry *)((s)->tabentry))->t_blocks_hit++;		\
 		else {															\
-			if (!(s)->no_stats) {										\
+			if (pgstat_collect_blocklevel && !(s)->no_stats) {			\
 				pgstat_initstats((s), (r));								\
 				if ((s)->tabentry != NULL)								\
 					((PgStat_TableEntry *)((s)->tabentry))->t_blocks_hit++; \
-- 
GitLab