From 5441a6416449fc0525e62ca38938285664ae6015 Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Tue, 3 Oct 2006 21:11:55 +0000
Subject: [PATCH] The attached patch changes units of the some default values
 in postgresql.conf.

- shared_buffers = 32000kB => 32MB
- temp_buffers = 8000kB => 8MB
- wal_buffers = 8 => 64kB

The code of initdb was a bit modified to write MB-unit values.
Values greater than 8000kB are rounded out to MB.

GUC_UNIT_XBLOCKS is added for wal_buffers. It is like GUC_UNIT_BLOCKS,
but uses XLOG_BLCKSZ instead of BLCKSZ.

Also, I cleaned up the test of GUC_UNIT_* flags in preparation to
add more unit flags in less bits.

ITAGAKI Takahiro
---
 src/backend/utils/misc/guc.c                  | 104 ++++++++++++------
 src/backend/utils/misc/postgresql.conf.sample |   8 +-
 src/bin/initdb/initdb.c                       |  26 +++--
 src/include/utils/guc_tables.h                |   7 +-
 4 files changed, 99 insertions(+), 46 deletions(-)

diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index 9467e6412c..d0eb77a4ad 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -10,7 +10,7 @@
  * Written by Peter Eisentraut <peter_e@gmx.net>.
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.352 2006/09/22 21:39:57 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.353 2006/10/03 21:11:54 momjian Exp $
  *
  *--------------------------------------------------------------------
  */
@@ -1149,7 +1149,7 @@ static struct config_int ConfigureNamesInt[] =
 			GUC_UNIT_BLOCKS
 		},
 		&NBuffers,
-		1000, 16, INT_MAX / 2, NULL, NULL
+		1024, 16, INT_MAX / 2, NULL, NULL
 	},
 
 	{
@@ -1159,7 +1159,7 @@ static struct config_int ConfigureNamesInt[] =
 			GUC_UNIT_BLOCKS
 		},
 		&num_temp_buffers,
-		1000, 100, INT_MAX / 2, NULL, show_num_temp_buffers
+		1024, 100, INT_MAX / 2, NULL, show_num_temp_buffers
 	},
 
 	{
@@ -1414,7 +1414,8 @@ static struct config_int ConfigureNamesInt[] =
 	{
 		{"wal_buffers", PGC_POSTMASTER, WAL_SETTINGS,
 			gettext_noop("Sets the number of disk-page buffers in shared memory for WAL."),
-			NULL
+			NULL,
+			GUC_UNIT_XBLOCKS
 		},
 		&XLOGbuffers,
 		8, 4, INT_MAX, NULL, NULL
@@ -3606,8 +3607,18 @@ parse_int(const char *value, int *result, int flags)
 			endptr += 2;
 		}
 
-		if (used && (flags & GUC_UNIT_BLOCKS))
-			val /= (BLCKSZ/1024);
+		if (used)
+		{
+			switch (flags & GUC_UNIT_MEMORY)
+			{
+				case GUC_UNIT_BLOCKS:
+					val /= (BLCKSZ/1024);
+					break;
+				case GUC_UNIT_XBLOCKS:
+					val /= (XLOG_BLCKSZ/1024);
+					break;
+			}
+		}
 	}
 
 	if ((flags & GUC_UNIT_TIME) && endptr != value)
@@ -3647,10 +3658,18 @@ parse_int(const char *value, int *result, int flags)
 			endptr += 1;
 		}
 
-		if (used && (flags & GUC_UNIT_S))
-			val /= MS_PER_S;
-		else if (used && (flags & GUC_UNIT_MIN))
-			val /= MS_PER_MIN;
+		if (used)
+		{
+			switch (flags & GUC_UNIT_TIME)
+			{
+			case GUC_UNIT_S:
+				val /= MS_PER_S;
+				break;
+			case GUC_UNIT_MIN:
+				val /= MS_PER_MIN;
+				break;
+			}
+		}
 	}
 
 	if (endptr == value || *endptr != '\0' || errno == ERANGE
@@ -4961,23 +4980,34 @@ GetConfigOptionByNum(int varnum, const char **values, bool *noshow)
 	/* unit */
 	if (conf->vartype == PGC_INT)
 	{
-		if (conf->flags & GUC_UNIT_KB)
-			values[2] = "kB";
-		else if (conf->flags & GUC_UNIT_BLOCKS)
-		{
-			static char buf[8];
+		static char buf[8];
 
-			snprintf(buf, sizeof(buf), "%dkB", BLCKSZ/1024);
-			values[2] = buf;
+		switch (conf->flags & (GUC_UNIT_MEMORY | GUC_UNIT_TIME))
+		{
+			case GUC_UNIT_KB:
+				values[2] = "kB";
+				break;
+			case GUC_UNIT_BLOCKS:
+				snprintf(buf, sizeof(buf), "%dkB", BLCKSZ/1024);
+				values[2] = buf;
+				break;
+			case GUC_UNIT_XBLOCKS:
+				snprintf(buf, sizeof(buf), "%dkB", XLOG_BLCKSZ/1024);
+				values[2] = buf;
+				break;
+			case GUC_UNIT_MS:
+				values[2] = "ms";
+				break;
+			case GUC_UNIT_S:
+				values[2] = "s";
+				break;
+			case GUC_UNIT_MIN:
+				values[2] = "min";
+				break;
+			default:
+				values[2] = "";
+				break;
 		}
-		else if (conf->flags & GUC_UNIT_MS)
-			values[2] = "ms";
-		else if (conf->flags & GUC_UNIT_S)
-			values[2] = "s";
-		else if (conf->flags & GUC_UNIT_MIN)
-			values[2] = "min";
-		else
-			values[2] = "";
 	}
 	else
 		values[2] = NULL;
@@ -5246,8 +5276,15 @@ _ShowOption(struct config_generic * record, bool use_units)
 
 					if (use_units && result > 0 && (record->flags & GUC_UNIT_MEMORY))
 					{
-						if (record->flags & GUC_UNIT_BLOCKS)
-							result *= BLCKSZ/1024;
+						switch (record->flags & GUC_UNIT_MEMORY)
+						{
+							case GUC_UNIT_BLOCKS:
+								result *= BLCKSZ/1024;
+								break;
+							case GUC_UNIT_XBLOCKS:
+								result *= XLOG_BLCKSZ/1024;
+								break;
+						}
 
 						if (result % KB_PER_GB == 0)
 						{
@@ -5266,10 +5303,15 @@ _ShowOption(struct config_generic * record, bool use_units)
 					}
 					else if (use_units && result > 0 && (record->flags & GUC_UNIT_TIME))
 					{
-						if (record->flags & GUC_UNIT_S)
-							result = result * MS_PER_S;
-						else if (record->flags & GUC_UNIT_MIN)
-							result = result * MS_PER_MIN;
+						switch (record->flags & GUC_UNIT_TIME)
+						{
+							case GUC_UNIT_S:
+								result *= MS_PER_S;
+								break;
+							case GUC_UNIT_MIN:
+								result *= MS_PER_MIN;
+								break;
+						}
 
 						if (result % MS_PER_D == 0)
 						{
diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample
index 83e7c254dc..36312c3b2f 100644
--- a/src/backend/utils/misc/postgresql.conf.sample
+++ b/src/backend/utils/misc/postgresql.conf.sample
@@ -98,9 +98,9 @@
 
 # - Memory -
 
-#shared_buffers = 32000kB		# min 128kB or max_connections*16kB
+#shared_buffers = 32MB		# min 128kB or max_connections*16kB
 					# (change requires restart)
-#temp_buffers = 8000kB			# min 800kB
+#temp_buffers = 8MB			# min 800kB
 #max_prepared_transactions = 5		# can be 0 or more
 					# (change requires restart)
 # Note: increasing max_prepared_transactions costs ~600 bytes of shared memory
@@ -111,7 +111,7 @@
 
 # - Free Space Map -
 
-#max_fsm_pages = 1600000		# min max_fsm_relations*16, 6 bytes each
+#max_fsm_pages = 1638400		# min max_fsm_relations*16, 6 bytes each
 					# (change requires restart)
 #max_fsm_relations = 1000		# min 100, ~70 bytes each
 					# (change requires restart)
@@ -154,7 +154,7 @@
 					#   fsync_writethrough
 					#   open_sync
 #full_page_writes = on			# recover from partial page writes
-#wal_buffers = 8			# min 4, 8kB each
+#wal_buffers = 64kB			# min 4, 8kB each
 					# (change requires restart)
 #commit_delay = 0			# range 0-100000, in microseconds
 #commit_siblings = 5			# range 1-1000
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 1c5989a4ef..70f481c372 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -42,7 +42,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  * Portions taken from FreeBSD.
  *
- * $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.122 2006/09/14 23:21:47 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.123 2006/10/03 21:11:55 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1113,10 +1113,14 @@ test_config_settings(void)
 	static const int trial_conns[] = {
 		100, 50, 40, 30, 20, 10
 	};
+
+	/*
+	 * Candidate values for shared_buffers in kB. When the value is
+	 * divisible by 1024, we write it in MB-unit to configuration files.
+	 */
 	static const int trial_bufs[] = {
-		32000, 28000, 24000, 20000, 16000, 12000,
-		8000, 7200, 6400, 5600, 4800, 4000,
-		3200, 2400, 1600, 800, 400
+		32768, 28672, 24576, 20480, 16384, 12288,
+		8192, 7200, 6400, 5600, 4800, 4000,
 	};
 
 	char		cmd[MAXPGPATH];
@@ -1190,7 +1194,10 @@ test_config_settings(void)
 	n_buffers = test_buffs;
 	n_fsm_pages = FSM_FOR_BUFS(n_buffers);
 
-	printf("%dkB/%d\n", n_buffers, n_fsm_pages);
+	if (n_buffers % 1024 == 0)
+		printf("%dMB/%d\n", n_buffers/1024, n_fsm_pages);
+	else
+		printf("%dkB/%d\n", n_buffers, n_fsm_pages);
 }
 
 /*
@@ -1213,11 +1220,14 @@ setup_config(void)
 	snprintf(repltok, sizeof(repltok), "max_connections = %d", n_connections);
 	conflines = replace_token(conflines, "#max_connections = 100", repltok);
 
-	snprintf(repltok, sizeof(repltok), "shared_buffers = %dkB", n_buffers);
-	conflines = replace_token(conflines, "#shared_buffers = 32000kB", repltok);
+	if (n_buffers % 1024 == 0)
+		snprintf(repltok, sizeof(repltok), "shared_buffers = %dMB", n_buffers/1024);
+	else
+		snprintf(repltok, sizeof(repltok), "shared_buffers = %dkB", n_buffers);
+	conflines = replace_token(conflines, "#shared_buffers = 32MB", repltok);
 
 	snprintf(repltok, sizeof(repltok), "max_fsm_pages = %d", n_fsm_pages);
-	conflines = replace_token(conflines, "#max_fsm_pages = 1600000", repltok);
+	conflines = replace_token(conflines, "#max_fsm_pages = 1638400", repltok);
 
 #if DEF_PGPORT != 5432
 	snprintf(repltok, sizeof(repltok), "#port = %d", DEF_PGPORT);
diff --git a/src/include/utils/guc_tables.h b/src/include/utils/guc_tables.h
index a0ebf2c529..292d04310f 100644
--- a/src/include/utils/guc_tables.h
+++ b/src/include/utils/guc_tables.h
@@ -7,7 +7,7 @@
  *
  * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
  *
- *	  $PostgreSQL: pgsql/src/include/utils/guc_tables.h,v 1.28 2006/08/14 02:27:27 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/include/utils/guc_tables.h,v 1.29 2006/10/03 21:11:55 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -132,12 +132,13 @@ struct config_generic
 
 #define GUC_UNIT_KB				0x0400	/* value is in 1 kB */
 #define GUC_UNIT_BLOCKS			0x0800	/* value is in blocks */
-#define GUC_UNIT_MEMORY			(GUC_UNIT_KB|GUC_UNIT_BLOCKS)
+#define GUC_UNIT_XBLOCKS		0x0C00	/* value is in xlog blocks */
+#define GUC_UNIT_MEMORY			0x0C00	/* mask for KB, BLOCKS, XBLOCKS */
 
 #define GUC_UNIT_MS				0x1000	/* value is in milliseconds */
 #define GUC_UNIT_S				0x2000	/* value is in seconds */
 #define GUC_UNIT_MIN			0x4000	/* value is in minutes */
-#define GUC_UNIT_TIME			(GUC_UNIT_MS|GUC_UNIT_S|GUC_UNIT_MIN)
+#define GUC_UNIT_TIME			0x7000	/* mask for MS, S, MIN */
 
 /* bit values in status field */
 #define GUC_HAVE_TENTATIVE	0x0001		/* tentative value is defined */