diff --git a/configure b/configure
index 4c46a5083781b5ddee3476d5525996a0d200a326..95db27083040b53a277282b87a0bc37aadcd6c65 100755
--- a/configure
+++ b/configure
@@ -18063,6 +18063,422 @@ _ACEOF
 
 
 
+# And one for the size of size_t (enables tweaks for > 32bit address space)
+echo "$as_me:$LINENO: checking for size_t" >&5
+echo $ECHO_N "checking for size_t... $ECHO_C" >&6
+if test "${ac_cv_type_size_t+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+$ac_includes_default
+int
+main ()
+{
+if ((size_t *) 0)
+  return 0;
+if (sizeof (size_t))
+  return 0;
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext
+if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5
+  (eval $ac_compile) 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } &&
+	 { ac_try='test -z "$ac_c_werror_flag"
+			 || test ! -s conftest.err'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; } &&
+	 { ac_try='test -s conftest.$ac_objext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  ac_cv_type_size_t=yes
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+ac_cv_type_size_t=no
+fi
+rm -f conftest.err conftest.$ac_objext conftest.$ac_ext
+fi
+echo "$as_me:$LINENO: result: $ac_cv_type_size_t" >&5
+echo "${ECHO_T}$ac_cv_type_size_t" >&6
+
+echo "$as_me:$LINENO: checking size of size_t" >&5
+echo $ECHO_N "checking size of size_t... $ECHO_C" >&6
+if test "${ac_cv_sizeof_size_t+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  if test "$ac_cv_type_size_t" = yes; then
+  # The cast to unsigned long works around a bug in the HP C Compiler
+  # version HP92453-01 B.11.11.23709.GP, which incorrectly rejects
+  # declarations like `int a3[[(sizeof (unsigned char)) >= 0]];'.
+  # This bug is HP SR number 8606223364.
+  if test "$cross_compiling" = yes; then
+  # Depending upon the size, compute the lo and hi bounds.
+cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+$ac_includes_default
+int
+main ()
+{
+static int test_array [1 - 2 * !(((long) (sizeof (size_t))) >= 0)];
+test_array [0] = 0
+
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext
+if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5
+  (eval $ac_compile) 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } &&
+	 { ac_try='test -z "$ac_c_werror_flag"
+			 || test ! -s conftest.err'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; } &&
+	 { ac_try='test -s conftest.$ac_objext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  ac_lo=0 ac_mid=0
+  while :; do
+    cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+$ac_includes_default
+int
+main ()
+{
+static int test_array [1 - 2 * !(((long) (sizeof (size_t))) <= $ac_mid)];
+test_array [0] = 0
+
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext
+if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5
+  (eval $ac_compile) 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } &&
+	 { ac_try='test -z "$ac_c_werror_flag"
+			 || test ! -s conftest.err'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; } &&
+	 { ac_try='test -s conftest.$ac_objext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  ac_hi=$ac_mid; break
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+ac_lo=`expr $ac_mid + 1`
+		    if test $ac_lo -le $ac_mid; then
+		      ac_lo= ac_hi=
+		      break
+		    fi
+		    ac_mid=`expr 2 '*' $ac_mid + 1`
+fi
+rm -f conftest.err conftest.$ac_objext conftest.$ac_ext
+  done
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+$ac_includes_default
+int
+main ()
+{
+static int test_array [1 - 2 * !(((long) (sizeof (size_t))) < 0)];
+test_array [0] = 0
+
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext
+if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5
+  (eval $ac_compile) 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } &&
+	 { ac_try='test -z "$ac_c_werror_flag"
+			 || test ! -s conftest.err'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; } &&
+	 { ac_try='test -s conftest.$ac_objext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  ac_hi=-1 ac_mid=-1
+  while :; do
+    cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+$ac_includes_default
+int
+main ()
+{
+static int test_array [1 - 2 * !(((long) (sizeof (size_t))) >= $ac_mid)];
+test_array [0] = 0
+
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext
+if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5
+  (eval $ac_compile) 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } &&
+	 { ac_try='test -z "$ac_c_werror_flag"
+			 || test ! -s conftest.err'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; } &&
+	 { ac_try='test -s conftest.$ac_objext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  ac_lo=$ac_mid; break
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+ac_hi=`expr '(' $ac_mid ')' - 1`
+		       if test $ac_mid -le $ac_hi; then
+			 ac_lo= ac_hi=
+			 break
+		       fi
+		       ac_mid=`expr 2 '*' $ac_mid`
+fi
+rm -f conftest.err conftest.$ac_objext conftest.$ac_ext
+  done
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+ac_lo= ac_hi=
+fi
+rm -f conftest.err conftest.$ac_objext conftest.$ac_ext
+fi
+rm -f conftest.err conftest.$ac_objext conftest.$ac_ext
+# Binary search between lo and hi bounds.
+while test "x$ac_lo" != "x$ac_hi"; do
+  ac_mid=`expr '(' $ac_hi - $ac_lo ')' / 2 + $ac_lo`
+  cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+$ac_includes_default
+int
+main ()
+{
+static int test_array [1 - 2 * !(((long) (sizeof (size_t))) <= $ac_mid)];
+test_array [0] = 0
+
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext
+if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5
+  (eval $ac_compile) 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } &&
+	 { ac_try='test -z "$ac_c_werror_flag"
+			 || test ! -s conftest.err'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; } &&
+	 { ac_try='test -s conftest.$ac_objext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  ac_hi=$ac_mid
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+ac_lo=`expr '(' $ac_mid ')' + 1`
+fi
+rm -f conftest.err conftest.$ac_objext conftest.$ac_ext
+done
+case $ac_lo in
+?*) ac_cv_sizeof_size_t=$ac_lo;;
+'') { { echo "$as_me:$LINENO: error: cannot compute sizeof (size_t), 77
+See \`config.log' for more details." >&5
+echo "$as_me: error: cannot compute sizeof (size_t), 77
+See \`config.log' for more details." >&2;}
+   { (exit 1); exit 1; }; } ;;
+esac
+else
+  if test "$cross_compiling" = yes; then
+  { { echo "$as_me:$LINENO: error: cannot run test program while cross compiling
+See \`config.log' for more details." >&5
+echo "$as_me: error: cannot run test program while cross compiling
+See \`config.log' for more details." >&2;}
+   { (exit 1); exit 1; }; }
+else
+  cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+$ac_includes_default
+long longval () { return (long) (sizeof (size_t)); }
+unsigned long ulongval () { return (long) (sizeof (size_t)); }
+#include <stdio.h>
+#include <stdlib.h>
+int
+main ()
+{
+
+  FILE *f = fopen ("conftest.val", "w");
+  if (! f)
+    exit (1);
+  if (((long) (sizeof (size_t))) < 0)
+    {
+      long i = longval ();
+      if (i != ((long) (sizeof (size_t))))
+	exit (1);
+      fprintf (f, "%ld\n", i);
+    }
+  else
+    {
+      unsigned long i = ulongval ();
+      if (i != ((long) (sizeof (size_t))))
+	exit (1);
+      fprintf (f, "%lu\n", i);
+    }
+  exit (ferror (f) || fclose (f) != 0);
+
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest$ac_exeext
+if { (eval echo "$as_me:$LINENO: \"$ac_link\"") >&5
+  (eval $ac_link) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } && { ac_try='./conftest$ac_exeext'
+  { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  ac_cv_sizeof_size_t=`cat conftest.val`
+else
+  echo "$as_me: program exited with status $ac_status" >&5
+echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+( exit $ac_status )
+{ { echo "$as_me:$LINENO: error: cannot compute sizeof (size_t), 77
+See \`config.log' for more details." >&5
+echo "$as_me: error: cannot compute sizeof (size_t), 77
+See \`config.log' for more details." >&2;}
+   { (exit 1); exit 1; }; }
+fi
+rm -f core *.core gmon.out bb.out conftest$ac_exeext conftest.$ac_objext conftest.$ac_ext
+fi
+fi
+rm -f conftest.val
+else
+  ac_cv_sizeof_size_t=0
+fi
+fi
+echo "$as_me:$LINENO: result: $ac_cv_sizeof_size_t" >&5
+echo "${ECHO_T}$ac_cv_sizeof_size_t" >&6
+cat >>confdefs.h <<_ACEOF
+#define SIZEOF_SIZE_T $ac_cv_sizeof_size_t
+_ACEOF
+
+
+
 # Determine memory alignment requirements for the basic C data types.
 
 echo "$as_me:$LINENO: checking for short" >&5
diff --git a/configure.in b/configure.in
index 7cd48637d0b3d24ca3b681e397b8d10c6eef73ef..91c1e1f8fe60e3fe2e67f4571f557db918c647d5 100644
--- a/configure.in
+++ b/configure.in
@@ -1,5 +1,5 @@
 dnl Process this file with autoconf to produce a configure script.
-dnl $PostgreSQL: pgsql/configure.in,v 1.419 2005/08/17 20:20:10 tgl Exp $
+dnl $PostgreSQL: pgsql/configure.in,v 1.420 2005/08/20 23:26:06 tgl Exp $
 dnl
 dnl Developers, please strive to achieve this order:
 dnl
@@ -1132,6 +1132,9 @@ fi
 # Need a #define for the size of Datum (unsigned long)
 AC_CHECK_SIZEOF([unsigned long])
 
+# And one for the size of size_t (enables tweaks for > 32bit address space)
+AC_CHECK_SIZEOF([size_t])
+
 # Determine memory alignment requirements for the basic C data types.
 
 PGAC_CHECK_ALIGNOF(short)
diff --git a/src/backend/access/transam/clog.c b/src/backend/access/transam/clog.c
index 3768570ad3d5c6ca31d5ed4cd4314cacdcf755bd..4a2e1f55927eee9f185d5c1bb56cfb5248f444ff 100644
--- a/src/backend/access/transam/clog.c
+++ b/src/backend/access/transam/clog.c
@@ -24,7 +24,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/backend/access/transam/clog.c,v 1.31 2005/06/30 00:00:50 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/access/transam/clog.c,v 1.32 2005/08/20 23:26:08 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -144,8 +144,7 @@ TransactionIdGetStatus(TransactionId xid)
 /*
  * Initialization of shared memory for CLOG
  */
-
-int
+Size
 CLOGShmemSize(void)
 {
 	return SimpleLruShmemSize();
diff --git a/src/backend/access/transam/multixact.c b/src/backend/access/transam/multixact.c
index d264d8ad57cdee540004910a85f93d3aed41a593..1adaebb6d80fa0b917b836aeddb7792a285bf226 100644
--- a/src/backend/access/transam/multixact.c
+++ b/src/backend/access/transam/multixact.c
@@ -42,7 +42,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/backend/access/transam/multixact.c,v 1.7 2005/08/20 01:29:27 ishii Exp $
+ * $PostgreSQL: pgsql/src/backend/access/transam/multixact.c,v 1.8 2005/08/20 23:26:08 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1159,13 +1159,20 @@ AtEOXact_MultiXact(void)
  * thus double memory.  Also, reserve space for the shared MultiXactState
  * struct and the per-backend MultiXactId arrays (two of those, too).
  */
-int
+Size
 MultiXactShmemSize(void)
 {
+	Size		size;
+
 #define SHARED_MULTIXACT_STATE_SIZE \
-	(sizeof(MultiXactStateData) + sizeof(MultiXactId) * 2 * MaxBackends)
+	add_size(sizeof(MultiXactStateData), \
+			 mul_size(sizeof(MultiXactId) * 2, MaxBackends))
+
+	size = SHARED_MULTIXACT_STATE_SIZE;
+	size = add_size(size, SimpleLruShmemSize());
+	size = add_size(size, SimpleLruShmemSize());
 
-	return (SimpleLruShmemSize() * 2 + SHARED_MULTIXACT_STATE_SIZE);
+	return size;
 }
 
 void
diff --git a/src/backend/access/transam/slru.c b/src/backend/access/transam/slru.c
index c7c12470209bd8ff28b52caa2dd57dfcb10b0974..67d9d3f54f34c73dd6ba057ec529f9e2f78e07bf 100644
--- a/src/backend/access/transam/slru.c
+++ b/src/backend/access/transam/slru.c
@@ -48,7 +48,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/backend/access/transam/slru.c,v 1.26 2005/07/04 04:51:44 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/access/transam/slru.c,v 1.27 2005/08/20 23:26:08 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -141,9 +141,10 @@ static int	SlruSelectLRUPage(SlruCtl ctl, int pageno);
  * Initialization of shared memory
  */
 
-int
+Size
 SimpleLruShmemSize(void)
 {
+	/* we assume NUM_SLRU_BUFFERS isn't so large as to risk overflow */
 	return BUFFERALIGN(sizeof(SlruSharedData)) + BLCKSZ * NUM_SLRU_BUFFERS;
 }
 
diff --git a/src/backend/access/transam/subtrans.c b/src/backend/access/transam/subtrans.c
index cea778d6a1ce76f7a4c88adcdf31b0067a902e50..9b450350360332362044eeabb07358148a53f5d7 100644
--- a/src/backend/access/transam/subtrans.c
+++ b/src/backend/access/transam/subtrans.c
@@ -22,7 +22,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/backend/access/transam/subtrans.c,v 1.9 2005/06/17 22:32:42 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/access/transam/subtrans.c,v 1.10 2005/08/20 23:26:08 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -161,8 +161,7 @@ SubTransGetTopmostTransaction(TransactionId xid)
 /*
  * Initialization of shared memory for SUBTRANS
  */
-
-int
+Size
 SUBTRANSShmemSize(void)
 {
 	return SimpleLruShmemSize();
diff --git a/src/backend/access/transam/twophase.c b/src/backend/access/transam/twophase.c
index 62ebf9fb40687263b0a6156efcf1665fe13f6961..f65904f037690c7a347d13c071a3e4455844c1ff 100644
--- a/src/backend/access/transam/twophase.c
+++ b/src/backend/access/transam/twophase.c
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *		$PostgreSQL: pgsql/src/backend/access/transam/twophase.c,v 1.9 2005/07/31 17:19:17 tgl Exp $
+ *		$PostgreSQL: pgsql/src/backend/access/transam/twophase.c,v 1.10 2005/08/20 23:26:10 tgl Exp $
  *
  * NOTES
  *		Each global transaction is associated with a global transaction
@@ -152,13 +152,20 @@ static void ProcessRecords(char *bufptr, TransactionId xid,
 /*
  * Initialization of shared memory
  */
-int
+Size
 TwoPhaseShmemSize(void)
 {
+	Size		size;
+
 	/* Need the fixed struct, the array of pointers, and the GTD structs */
-	return MAXALIGN(offsetof(TwoPhaseStateData, prepXacts) + 
-					sizeof(GlobalTransaction) * max_prepared_xacts) +
-		sizeof(GlobalTransactionData) * max_prepared_xacts;
+	size = offsetof(TwoPhaseStateData, prepXacts);
+	size = add_size(size, mul_size(max_prepared_xacts,
+								   sizeof(GlobalTransaction)));
+	size = MAXALIGN(size);
+	size = add_size(size, mul_size(max_prepared_xacts,
+								   sizeof(GlobalTransactionData)));
+
+	return size;
 }
 
 void
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index ae57ec0231a628e8820b31d60968a4bf851d665b..a917616cffdae35ea93a7eeba5ab40d632898a86 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.215 2005/08/11 21:11:43 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.216 2005/08/20 23:26:10 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -91,31 +91,6 @@
 #endif
 #endif
 
-/*
- * Limitation of buffer-alignment for direct io depend on OS and filesystem,
- * but BLCKSZ is assumed to be enough for it. 
- */
-#ifdef O_DIRECT
-#define ALIGNOF_XLOG_BUFFER		BLCKSZ
-#else
-#define ALIGNOF_XLOG_BUFFER		MAXIMUM_ALIGNOF
-#endif
-
-/*
- * Switch the alignment routine because ShmemAlloc() returns a max-aligned
- * buffer and ALIGNOF_XLOG_BUFFER may be greater than MAXIMUM_ALIGNOF.
- */
-#if ALIGNOF_XLOG_BUFFER <= MAXIMUM_ALIGNOF
-#define XLOG_BUFFER_ALIGN(LEN)	MAXALIGN((LEN))
-#else
-#define XLOG_BUFFER_ALIGN(LEN)	((LEN) + (ALIGNOF_XLOG_BUFFER))
-#endif
-/* assume sizeof(ptrdiff_t) == sizeof(void*) */
-#define POINTERALIGN(ALIGNVAL,PTR)	\
-	((char *)(((ptrdiff_t) (PTR) + (ALIGNVAL-1)) & ~((ptrdiff_t) (ALIGNVAL-1))))
-#define XLOG_BUFFER_POINTERALIGN(PTR)	\
-	POINTERALIGN((ALIGNOF_XLOG_BUFFER), (PTR))
-
 #ifdef OPEN_DATASYNC_FLAG
 #define DEFAULT_SYNC_METHOD_STR	"open_datasync"
 #define DEFAULT_SYNC_METHOD		SYNC_METHOD_OPEN
@@ -135,6 +110,17 @@
 #endif
 
 
+/*
+ * Limitation of buffer-alignment for direct IO depends on OS and filesystem,
+ * but BLCKSZ is assumed to be enough for it. 
+ */
+#ifdef O_DIRECT
+#define ALIGNOF_XLOG_BUFFER		BLCKSZ
+#else
+#define ALIGNOF_XLOG_BUFFER		ALIGNOF_BUFFER
+#endif
+
+
 /* File path names (all relative to $PGDATA) */
 #define BACKUP_LABEL_FILE		"backup_label"
 #define RECOVERY_COMMAND_FILE	"recovery.conf"
@@ -173,8 +159,6 @@ static int	open_sync_bit = DEFAULT_SYNC_FLAGBIT;
 
 #define XLOG_SYNC_BIT  (enableFsync ? open_sync_bit : 0)
 
-#define MinXLOGbuffers	4
-
 
 /*
  * ThisTimeLineID will be same in all backends --- it identifies current
@@ -3615,16 +3599,27 @@ UpdateControlFile(void)
 /*
  * Initialization of shared memory for XLOG
  */
-
-int
+Size
 XLOGShmemSize(void)
 {
-	if (XLOGbuffers < MinXLOGbuffers)
-		XLOGbuffers = MinXLOGbuffers;
+	Size		size;
 
-	return XLOG_BUFFER_ALIGN(sizeof(XLogCtlData) + sizeof(XLogRecPtr) * XLOGbuffers)
-		+ BLCKSZ * XLOGbuffers +
-		MAXALIGN(sizeof(ControlFileData));
+	/* XLogCtl */
+	size = sizeof(XLogCtlData);
+	/* xlblocks array */
+	size = add_size(size, mul_size(sizeof(XLogRecPtr), XLOGbuffers));
+	/* extra alignment padding for XLOG I/O buffers */
+	size = add_size(size, ALIGNOF_XLOG_BUFFER);
+	/* and the buffers themselves */
+	size = add_size(size, mul_size(BLCKSZ, XLOGbuffers));
+
+	/*
+	 * Note: we don't count ControlFileData, it comes out of the "slop
+	 * factor" added by CreateSharedMemoryAndSemaphores.  This lets us
+	 * use this routine again below to compute the actual allocation size.
+	 */
+
+	return size;
 }
 
 void
@@ -3632,17 +3627,10 @@ XLOGShmemInit(void)
 {
 	bool		foundXLog,
 				foundCFile;
-
-	/* this must agree with space requested by XLOGShmemSize() */
-	if (XLOGbuffers < MinXLOGbuffers)
-		XLOGbuffers = MinXLOGbuffers;
+	char	   *allocptr;
 
 	XLogCtl = (XLogCtlData *)
-		ShmemInitStruct("XLOG Ctl",
-						XLOG_BUFFER_ALIGN(sizeof(XLogCtlData) +
-								 sizeof(XLogRecPtr) * XLOGbuffers)
-						+ BLCKSZ * XLOGbuffers,
-						&foundXLog);
+		ShmemInitStruct("XLOG Ctl", XLOGShmemSize(), &foundXLog);
 	ControlFile = (ControlFileData *)
 		ShmemInitStruct("Control File", sizeof(ControlFileData), &foundCFile);
 
@@ -3660,17 +3648,16 @@ XLOGShmemInit(void)
 	 * a multiple of the alignment for same, so no extra alignment padding
 	 * is needed here.
 	 */
-	XLogCtl->xlblocks = (XLogRecPtr *)
-		(((char *) XLogCtl) + sizeof(XLogCtlData));
+	allocptr = ((char *) XLogCtl) + sizeof(XLogCtlData);
+	XLogCtl->xlblocks = (XLogRecPtr *) allocptr;
 	memset(XLogCtl->xlblocks, 0, sizeof(XLogRecPtr) * XLOGbuffers);
+	allocptr += sizeof(XLogRecPtr) * XLOGbuffers;
 
 	/*
-	 * Here, on the other hand, we must MAXALIGN to ensure the page
-	 * buffers have worst-case alignment.
+	 * Align the start of the page buffers to an ALIGNOF_XLOG_BUFFER boundary.
 	 */
-	XLogCtl->pages = XLOG_BUFFER_POINTERALIGN(
-		((char *) XLogCtl)
-		+ sizeof(XLogCtlData) + sizeof(XLogRecPtr) * XLOGbuffers);
+	allocptr = (char *) TYPEALIGN(ALIGNOF_XLOG_BUFFER, allocptr);
+	XLogCtl->pages = allocptr;
 	memset(XLogCtl->pages, 0, BLCKSZ * XLOGbuffers);
 
 	/*
@@ -3728,8 +3715,9 @@ BootStrapXLOG(void)
 	/* First timeline ID is always 1 */
 	ThisTimeLineID = 1;
 
- 	buffer = (char *) malloc(BLCKSZ + ALIGNOF_XLOG_BUFFER);
-	page = (XLogPageHeader) XLOG_BUFFER_POINTERALIGN(buffer);
+	/* page buffer must be aligned suitably for O_DIRECT */
+ 	buffer = (char *) palloc(BLCKSZ + ALIGNOF_XLOG_BUFFER);
+	page = (XLogPageHeader) TYPEALIGN(ALIGNOF_XLOG_BUFFER, buffer);
 	memset(page, 0, BLCKSZ);
 
 	/* Set up information for the initial checkpoint record */
@@ -3824,7 +3812,7 @@ BootStrapXLOG(void)
 	BootStrapSUBTRANS();
 	BootStrapMultiXact();
 
-	free(buffer);
+	pfree(buffer);
 }
 
 static char *
diff --git a/src/backend/commands/vacuumlazy.c b/src/backend/commands/vacuumlazy.c
index 179d35b4028df5477f7f4c4b6067a3028e960390..bfdf4b6cbb3d679125e6c6bcad7dd4cec3e01f65 100644
--- a/src/backend/commands/vacuumlazy.c
+++ b/src/backend/commands/vacuumlazy.c
@@ -31,7 +31,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/commands/vacuumlazy.c,v 1.56 2005/07/29 19:30:03 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/commands/vacuumlazy.c,v 1.57 2005/08/20 23:26:13 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -954,16 +954,16 @@ count_nondeletable_pages(Relation onerel, LVRelStats *vacrelstats)
 static void
 lazy_space_alloc(LVRelStats *vacrelstats, BlockNumber relblocks)
 {
-	int			maxtuples;
+	long		maxtuples;
 	int			maxpages;
 
-	maxtuples = (int) ((maintenance_work_mem * 1024L) / sizeof(ItemPointerData));
+	maxtuples = (maintenance_work_mem * 1024L) / sizeof(ItemPointerData);
+	maxtuples = Min(maxtuples, INT_MAX);
 	/* stay sane if small maintenance_work_mem */
-	if (maxtuples < MAX_TUPLES_PER_PAGE)
-		maxtuples = MAX_TUPLES_PER_PAGE;
+	maxtuples = Max(maxtuples, MAX_TUPLES_PER_PAGE);
 
 	vacrelstats->num_dead_tuples = 0;
-	vacrelstats->max_dead_tuples = maxtuples;
+	vacrelstats->max_dead_tuples = (int) maxtuples;
 	vacrelstats->dead_tuples = (ItemPointer)
 		palloc(maxtuples * sizeof(ItemPointerData));
 
diff --git a/src/backend/port/sysv_shmem.c b/src/backend/port/sysv_shmem.c
index a946b8d01342340d1280e4120fe2604994d44f93..23b945f08502ac3453a04a7d64bd856657804608 100644
--- a/src/backend/port/sysv_shmem.c
+++ b/src/backend/port/sysv_shmem.c
@@ -10,7 +10,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/port/sysv_shmem.c,v 1.42 2004/12/31 22:00:30 pgsql Exp $
+ *	  $PostgreSQL: pgsql/src/backend/port/sysv_shmem.c,v 1.43 2005/08/20 23:26:13 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -51,7 +51,7 @@ typedef int IpcMemoryId;		/* shared memory ID returned by shmget(2) */
 unsigned long UsedShmemSegID = 0;
 void	   *UsedShmemSegAddr = NULL;
 
-static void *InternalIpcMemoryCreate(IpcMemoryKey memKey, uint32 size);
+static void *InternalIpcMemoryCreate(IpcMemoryKey memKey, Size size);
 static void IpcMemoryDetach(int status, Datum shmaddr);
 static void IpcMemoryDelete(int status, Datum shmId);
 static PGShmemHeader *PGSharedMemoryAttach(IpcMemoryKey key,
@@ -71,7 +71,7 @@ static PGShmemHeader *PGSharedMemoryAttach(IpcMemoryKey key,
  * print out an error and abort.  Other types of errors are not recoverable.
  */
 static void *
-InternalIpcMemoryCreate(IpcMemoryKey memKey, uint32 size)
+InternalIpcMemoryCreate(IpcMemoryKey memKey, Size size)
 {
 	IpcMemoryId shmid;
 	void	   *memAddress;
@@ -99,14 +99,14 @@ InternalIpcMemoryCreate(IpcMemoryKey memKey, uint32 size)
 		 */
 		ereport(FATAL,
 				(errmsg("could not create shared memory segment: %m"),
-		errdetail("Failed system call was shmget(key=%lu, size=%u, 0%o).",
-				  (unsigned long) memKey, size,
+		errdetail("Failed system call was shmget(key=%lu, size=%lu, 0%o).",
+				  (unsigned long) memKey, (unsigned long) size,
 				  IPC_CREAT | IPC_EXCL | IPCProtection),
 				 (errno == EINVAL) ?
 				 errhint("This error usually means that PostgreSQL's request for a shared memory "
 						 "segment exceeded your kernel's SHMMAX parameter.  You can either "
 						 "reduce the request size or reconfigure the kernel with larger SHMMAX.  "
-			   "To reduce the request size (currently %u bytes), reduce "
+			   "To reduce the request size (currently %lu bytes), reduce "
 		   "PostgreSQL's shared_buffers parameter (currently %d) and/or "
 						 "its max_connections parameter (currently %d).\n"
 						 "If the request size is already small, it's possible that it is less than "
@@ -114,28 +114,28 @@ InternalIpcMemoryCreate(IpcMemoryKey memKey, uint32 size)
 						 "reconfiguring SHMMIN is called for.\n"
 						 "The PostgreSQL documentation contains more information about shared "
 						 "memory configuration.",
-						 size, NBuffers, MaxBackends) : 0,
+						 (unsigned long) size, NBuffers, MaxBackends) : 0,
 				 (errno == ENOMEM) ?
 				 errhint("This error usually means that PostgreSQL's request for a shared "
 			   "memory segment exceeded available memory or swap space. "
-			   "To reduce the request size (currently %u bytes), reduce "
+			   "To reduce the request size (currently %lu bytes), reduce "
 		   "PostgreSQL's shared_buffers parameter (currently %d) and/or "
 						 "its max_connections parameter (currently %d).\n"
 						 "The PostgreSQL documentation contains more information about shared "
 						 "memory configuration.",
-						 size, NBuffers, MaxBackends) : 0,
+						 (unsigned long) size, NBuffers, MaxBackends) : 0,
 				 (errno == ENOSPC) ?
 				 errhint("This error does *not* mean that you have run out of disk space. "
 						 "It occurs either if all available shared memory IDs have been taken, "
 						 "in which case you need to raise the SHMMNI parameter in your kernel, "
 						 "or because the system's overall limit for shared memory has been "
 			 "reached.  If you cannot increase the shared memory limit, "
-		"reduce PostgreSQL's shared memory request (currently %u bytes), "
+		"reduce PostgreSQL's shared memory request (currently %lu bytes), "
 		"by reducing its shared_buffers parameter (currently %d) and/or "
 						 "its max_connections parameter (currently %d).\n"
 						 "The PostgreSQL documentation contains more information about shared "
 						 "memory configuration.",
-						 size, NBuffers, MaxBackends) : 0));
+						 (unsigned long) size, NBuffers, MaxBackends) : 0));
 	}
 
 	/* Register on-exit routine to delete the new segment */
@@ -289,7 +289,7 @@ PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2)
  * zero will be passed.
  */
 PGShmemHeader *
-PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
+PGSharedMemoryCreate(Size size, bool makePrivate, int port)
 {
 	IpcMemoryKey NextShmemSegID;
 	void	   *memAddress;
diff --git a/src/backend/postmaster/bgwriter.c b/src/backend/postmaster/bgwriter.c
index f4158fc89a50269042f78d2bb3e6456c821985af..830cb897d98fb38f6df68c861ccfa4a90fbc90ca 100644
--- a/src/backend/postmaster/bgwriter.c
+++ b/src/backend/postmaster/bgwriter.c
@@ -37,7 +37,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/postmaster/bgwriter.c,v 1.18 2005/08/02 20:52:08 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/postmaster/bgwriter.c,v 1.19 2005/08/20 23:26:17 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -462,15 +462,19 @@ ReqShutdownHandler(SIGNAL_ARGS)
  * BgWriterShmemSize
  *		Compute space needed for bgwriter-related shared memory
  */
-int
+Size
 BgWriterShmemSize(void)
 {
+	Size		size;
+
 	/*
 	 * Currently, the size of the requests[] array is arbitrarily set
 	 * equal to NBuffers.  This may prove too large or small ...
 	 */
-	return MAXALIGN(sizeof(BgWriterShmemStruct) +
-					(NBuffers - 1) *sizeof(BgWriterRequest));
+	size = offsetof(BgWriterShmemStruct, requests);
+	size = add_size(size, mul_size(NBuffers, sizeof(BgWriterRequest)));
+
+	return size;
 }
 
 /*
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 09d1a89a7275925cd56901443b93725539115066..313ef2af31a6ad4c8fb46bfdc60557409cd439b8 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -37,7 +37,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.466 2005/08/12 19:43:31 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.467 2005/08/20 23:26:17 tgl Exp $
  *
  * NOTES
  *
@@ -816,9 +816,9 @@ PostmasterMain(int argc, char *argv[])
 	 * Initialize the child pid/HANDLE arrays for signal handling.
 	 */
 	win32_childPIDArray = (pid_t *)
-		malloc(NUM_BACKENDARRAY_ELEMS * sizeof(pid_t));
+		malloc(mul_size(NUM_BACKENDARRAY_ELEMS, sizeof(pid_t)));
 	win32_childHNDArray = (HANDLE *)
-		malloc(NUM_BACKENDARRAY_ELEMS * sizeof(HANDLE));
+		malloc(mul_size(NUM_BACKENDARRAY_ELEMS, sizeof(HANDLE)));
 	if (!win32_childPIDArray || !win32_childHNDArray)
 		ereport(FATAL,
 				(errcode(ERRCODE_OUT_OF_MEMORY),
@@ -3920,16 +3920,16 @@ restore_backend_variables(BackendParameters *param, Port *port)
 }
 
 
-size_t
+Size
 ShmemBackendArraySize(void)
 {
-	return (NUM_BACKENDARRAY_ELEMS * sizeof(Backend));
+	return mul_size(NUM_BACKENDARRAY_ELEMS, sizeof(Backend));
 }
 
 void
 ShmemBackendArrayAllocation(void)
 {
-	size_t		size = ShmemBackendArraySize();
+	Size		size = ShmemBackendArraySize();
 
 	ShmemBackendArray = (Backend *) ShmemAlloc(size);
 	/* Mark all slots as empty */
diff --git a/src/backend/storage/buffer/buf_init.c b/src/backend/storage/buffer/buf_init.c
index 5051e762d851b7beefeefe81a8d09d87ce654096..d74d356f0e6e2ecdfa9c595bcfb42f0b4a362bbb 100644
--- a/src/backend/storage/buffer/buf_init.c
+++ b/src/backend/storage/buffer/buf_init.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/storage/buffer/buf_init.c,v 1.75 2005/08/12 05:05:50 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/storage/buffer/buf_init.c,v 1.76 2005/08/20 23:26:17 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -89,7 +89,7 @@ InitBufferPool(void)
 
 	BufferBlocks = (char *)
 		ShmemInitStruct("Buffer Blocks",
-						NBuffers * BLCKSZ, &foundBufs);
+						NBuffers * (Size) BLCKSZ, &foundBufs);
 
 	if (foundDescs || foundBufs)
 	{
@@ -155,8 +155,11 @@ InitBufferPoolAccess(void)
 	/*
 	 * Allocate and zero local arrays of per-buffer info.
 	 */
-	PrivateRefCount = (int32 *) calloc(NBuffers,
-									   sizeof(*PrivateRefCount));
+	PrivateRefCount = (int32 *) calloc(NBuffers, sizeof(int32));
+	if (!PrivateRefCount)
+		ereport(FATAL,
+				(errcode(ERRCODE_OUT_OF_MEMORY),
+				 errmsg("out of memory")));
 }
 
 /*
@@ -165,19 +168,19 @@ InitBufferPoolAccess(void)
  * compute the size of shared memory for the buffer pool including
  * data pages, buffer descriptors, hash tables, etc.
  */
-int
+Size
 BufferShmemSize(void)
 {
-	int			size = 0;
+	Size		size = 0;
 
 	/* size of buffer descriptors */
-	size += MAXALIGN(NBuffers * sizeof(BufferDesc));
+	size = add_size(size, mul_size(NBuffers, sizeof(BufferDesc)));
 
 	/* size of data pages */
-	size += NBuffers * MAXALIGN(BLCKSZ);
+	size = add_size(size, mul_size(NBuffers, BLCKSZ));
 
 	/* size of stuff controlled by freelist.c */
-	size += StrategyShmemSize();
+	size = add_size(size, StrategyShmemSize());
 
 	return size;
 }
diff --git a/src/backend/storage/buffer/buf_table.c b/src/backend/storage/buffer/buf_table.c
index 5ef277edc95f6fb7df61a021a2f41ceca3e0756f..99dbbacd29867ae01b87bc2c843cfeb929957a5e 100644
--- a/src/backend/storage/buffer/buf_table.c
+++ b/src/backend/storage/buffer/buf_table.c
@@ -13,7 +13,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/storage/buffer/buf_table.c,v 1.41 2005/05/29 04:23:04 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/storage/buffer/buf_table.c,v 1.42 2005/08/20 23:26:17 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -37,7 +37,7 @@ static HTAB *SharedBufHash;
  * Estimate space needed for mapping hashtable
  *		size is the desired hash table size (possibly more than NBuffers)
  */
-int
+Size
 BufTableShmemSize(int size)
 {
 	return hash_estimate_size(size, sizeof(BufferLookupEnt));
diff --git a/src/backend/storage/buffer/freelist.c b/src/backend/storage/buffer/freelist.c
index 247a9abf591f1ae65cc4df371b9fa040290637e6..906165638b6f5df302a119bf72cd33cfbebafbb1 100644
--- a/src/backend/storage/buffer/freelist.c
+++ b/src/backend/storage/buffer/freelist.c
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/storage/buffer/freelist.c,v 1.51 2005/03/04 20:21:06 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/storage/buffer/freelist.c,v 1.52 2005/08/20 23:26:17 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -208,16 +208,16 @@ StrategyHintVacuum(bool vacuum_active)
  * Note: for somewhat historical reasons, the buffer lookup hashtable size
  * is also determined here.
  */
-int
+Size
 StrategyShmemSize(void)
 {
-	int			size = 0;
+	Size		size = 0;
 
 	/* size of lookup hash table */
-	size += BufTableShmemSize(NBuffers);
+	size = add_size(size, BufTableShmemSize(NBuffers));
 
 	/* size of the shared replacement strategy control block */
-	size += MAXALIGN(sizeof(BufferStrategyControl));
+	size = add_size(size, MAXALIGN(sizeof(BufferStrategyControl)));
 
 	return size;
 }
diff --git a/src/backend/storage/buffer/localbuf.c b/src/backend/storage/buffer/localbuf.c
index 6ac0b543ae386504af42aca730015c6a2760f68a..6dce508656297f51c1c9c5e09554786e50864c3d 100644
--- a/src/backend/storage/buffer/localbuf.c
+++ b/src/backend/storage/buffer/localbuf.c
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/storage/buffer/localbuf.c,v 1.68 2005/08/08 19:44:22 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/storage/buffer/localbuf.c,v 1.69 2005/08/20 23:26:17 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -255,15 +255,13 @@ InitLocalBuffers(void)
 	int			i;
 
 	/* Allocate and zero buffer headers and auxiliary arrays */
-	LocalBufferDescriptors = (BufferDesc *)
-		MemoryContextAllocZero(TopMemoryContext,
-							   nbufs * sizeof(BufferDesc));
-	LocalBufferBlockPointers = (Block *)
-		MemoryContextAllocZero(TopMemoryContext,
-							   nbufs * sizeof(Block));
-	LocalRefCount = (int32 *)
-		MemoryContextAllocZero(TopMemoryContext,
-							   nbufs * sizeof(int32));
+	LocalBufferDescriptors = (BufferDesc *) calloc(nbufs, sizeof(BufferDesc));
+	LocalBufferBlockPointers = (Block *) calloc(nbufs, sizeof(Block));
+	LocalRefCount = (int32 *) calloc(nbufs, sizeof(int32));
+	if (!LocalBufferDescriptors || !LocalBufferBlockPointers || !LocalRefCount)
+		ereport(FATAL,
+				(errcode(ERRCODE_OUT_OF_MEMORY),
+				 errmsg("out of memory")));
 
 	nextFreeLocalBuf = 0;
 
diff --git a/src/backend/storage/freespace/freespace.c b/src/backend/storage/freespace/freespace.c
index f8e32d23854a7002b2697686b957a5ec172345c8..11fc45ea8e718b227bf404784f410c8c053c0423 100644
--- a/src/backend/storage/freespace/freespace.c
+++ b/src/backend/storage/freespace/freespace.c
@@ -8,7 +8,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/storage/freespace/freespace.c,v 1.47 2005/08/17 03:50:59 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/storage/freespace/freespace.c,v 1.48 2005/08/20 23:26:20 tgl Exp $
  *
  *
  * NOTES:
@@ -271,11 +271,13 @@ InitFreeSpaceMap(void)
 	bool		found;
 
 	/* Create table header */
-	FreeSpaceMap = (FSMHeader *) ShmemInitStruct("Free Space Map Header", sizeof(FSMHeader), &found);
+	FreeSpaceMap = (FSMHeader *) ShmemInitStruct("Free Space Map Header",
+												 sizeof(FSMHeader),
+												 &found);
 	if (FreeSpaceMap == NULL)
 		ereport(FATAL,
 				(errcode(ERRCODE_OUT_OF_MEMORY),
-			   errmsg("insufficient shared memory for free space map")));
+				 errmsg("insufficient shared memory for free space map")));
 	if (!found)
 		MemSet(FreeSpaceMap, 0, sizeof(FSMHeader));
 
@@ -308,7 +310,7 @@ InitFreeSpaceMap(void)
 			   errmsg("max_fsm_pages must exceed max_fsm_relations * %d",
 					  CHUNKPAGES)));
 
-	FreeSpaceMap->arena = (char *) ShmemAlloc(nchunks * CHUNKBYTES);
+	FreeSpaceMap->arena = (char *) ShmemAlloc((Size) nchunks * CHUNKBYTES);
 	if (FreeSpaceMap->arena == NULL)
 		ereport(FATAL,
 				(errcode(ERRCODE_OUT_OF_MEMORY),
@@ -322,27 +324,22 @@ InitFreeSpaceMap(void)
 /*
  * Estimate amount of shmem space needed for FSM.
  */
-int
+Size
 FreeSpaceShmemSize(void)
 {
-	int			size;
+	Size		size;
 	int			nchunks;
 
 	/* table header */
 	size = MAXALIGN(sizeof(FSMHeader));
 
 	/* hash table, including the FSMRelation objects */
-	size += hash_estimate_size(MaxFSMRelations + 1, sizeof(FSMRelation));
+	size = add_size(size, hash_estimate_size(MaxFSMRelations + 1,
+											 sizeof(FSMRelation)));
 
 	/* page-storage arena */
 	nchunks = (MaxFSMPages - 1) / CHUNKPAGES + 1;
-
-	if (nchunks >= (INT_MAX / CHUNKBYTES))
-		ereport(FATAL,
-				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("max_fsm_pages is too large")));
-
-	size += MAXALIGN(nchunks * CHUNKBYTES);
+	size = add_size(size, mul_size(nchunks, CHUNKBYTES));
 
 	return size;
 }
diff --git a/src/backend/storage/ipc/ipci.c b/src/backend/storage/ipc/ipci.c
index 0761a8fdf51197491c88393eec75024e92142a42..48ef94a3ecb965faa39771296a6c7343a2bf98ce 100644
--- a/src/backend/storage/ipc/ipci.c
+++ b/src/backend/storage/ipc/ipci.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/storage/ipc/ipci.c,v 1.77 2005/06/17 22:32:45 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/storage/ipc/ipci.c,v 1.78 2005/08/20 23:26:20 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -61,36 +61,44 @@ CreateSharedMemoryAndSemaphores(bool makePrivate, int port)
 
 	if (!IsUnderPostmaster)
 	{
-		int			size;
+		Size		size;
 		int			numSemas;
 
 		/*
 		 * Size of the Postgres shared-memory block is estimated via
 		 * moderately-accurate estimates for the big hogs, plus 100K for
 		 * the stuff that's too small to bother with estimating.
+		 *
+		 * We take some care during this phase to ensure that the total
+		 * size request doesn't overflow size_t.  If this gets through,
+		 * we don't need to be so careful during the actual allocation
+		 * phase.
 		 */
-		size = hash_estimate_size(SHMEM_INDEX_SIZE, sizeof(ShmemIndexEnt));
-		size += BufferShmemSize();
-		size += LockShmemSize();
-		size += ProcGlobalShmemSize();
-		size += XLOGShmemSize();
-		size += CLOGShmemSize();
-		size += SUBTRANSShmemSize();
-		size += TwoPhaseShmemSize();
-		size += MultiXactShmemSize();
-		size += LWLockShmemSize();
-		size += ProcArrayShmemSize();
-		size += SInvalShmemSize(MaxBackends);
-		size += FreeSpaceShmemSize();
-		size += BgWriterShmemSize();
+		size = 100000;
+		size = add_size(size, hash_estimate_size(SHMEM_INDEX_SIZE,
+												 sizeof(ShmemIndexEnt)));
+		size = add_size(size, BufferShmemSize());
+		size = add_size(size, LockShmemSize());
+		size = add_size(size, ProcGlobalShmemSize());
+		size = add_size(size, XLOGShmemSize());
+		size = add_size(size, CLOGShmemSize());
+		size = add_size(size, SUBTRANSShmemSize());
+		size = add_size(size, TwoPhaseShmemSize());
+		size = add_size(size, MultiXactShmemSize());
+		size = add_size(size, LWLockShmemSize());
+		size = add_size(size, ProcArrayShmemSize());
+		size = add_size(size, SInvalShmemSize());
+		size = add_size(size, FreeSpaceShmemSize());
+		size = add_size(size, BgWriterShmemSize());
 #ifdef EXEC_BACKEND
-		size += ShmemBackendArraySize();
+		size = add_size(size, ShmemBackendArraySize());
 #endif
-		size += 100000;
+
 		/* might as well round it off to a multiple of a typical page size */
-		size += 8192 - (size % 8192);
+		size = add_size(size, 8192 - (size % 8192));
 
-		elog(DEBUG3, "invoking IpcMemoryCreate(size=%d)", size);
+		elog(DEBUG3, "invoking IpcMemoryCreate(size=%lu)",
+			 (unsigned long) size);
 
 		/*
 		 * Create the shmem segment
@@ -163,7 +171,7 @@ CreateSharedMemoryAndSemaphores(bool makePrivate, int port)
 	/*
 	 * Set up shared-inval messaging
 	 */
-	CreateSharedInvalidationState(MaxBackends);
+	CreateSharedInvalidationState();
 
 	/*
 	 * Set up free-space map
diff --git a/src/backend/storage/ipc/pmsignal.c b/src/backend/storage/ipc/pmsignal.c
index fbc5cf5666bce826b92acbcea5b59268b2299abe..a916688717c60146c7c7079f791009c89e38f0a0 100644
--- a/src/backend/storage/ipc/pmsignal.c
+++ b/src/backend/storage/ipc/pmsignal.c
@@ -8,7 +8,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/storage/ipc/pmsignal.c,v 1.18 2004/12/31 22:00:56 pgsql Exp $
+ *	  $PostgreSQL: pgsql/src/backend/storage/ipc/pmsignal.c,v 1.19 2005/08/20 23:26:20 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -48,7 +48,9 @@ PMSignalInit(void)
 	bool		found;
 
 	PMSignalFlags = (sig_atomic_t *)
-		ShmemInitStruct("PMSignalFlags", NUM_PMSIGNALS * sizeof(sig_atomic_t), &found);
+		ShmemInitStruct("PMSignalFlags",
+						NUM_PMSIGNALS * sizeof(sig_atomic_t),
+						&found);
 
 	if (!found)
 		MemSet(PMSignalFlags, 0, NUM_PMSIGNALS * sizeof(sig_atomic_t));
diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/procarray.c
index 87b1228ae6d184f7f46b6fa43d3493956ed6faab..f2950bece303aaa401fd43cb8f6e4d2a5e90b9b4 100644
--- a/src/backend/storage/ipc/procarray.c
+++ b/src/backend/storage/ipc/procarray.c
@@ -23,7 +23,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/storage/ipc/procarray.c,v 1.5 2005/08/20 01:26:36 ishii Exp $
+ *	  $PostgreSQL: pgsql/src/backend/storage/ipc/procarray.c,v 1.6 2005/08/20 23:26:20 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -81,11 +81,16 @@ static void DisplayXidCache(void);
 /*
  * Report shared-memory space needed by CreateSharedProcArray.
  */
-int
+Size
 ProcArrayShmemSize(void)
 {
-	return MAXALIGN(offsetof(ProcArrayStruct, procs) +
-					(MaxBackends + max_prepared_xacts) * sizeof(PGPROC *));
+	Size		size;
+
+	size = offsetof(ProcArrayStruct, procs);
+	size = add_size(size, mul_size(sizeof(PGPROC *),
+								   add_size(MaxBackends, max_prepared_xacts)));
+
+	return size;
 }
 
 /*
diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c
index 9af059139e7d48eeb6e815ff9bfb05445afa0ce4..76b3ef26cc4d49eabe216f44bdeb8d4c325de411 100644
--- a/src/backend/storage/ipc/shmem.c
+++ b/src/backend/storage/ipc/shmem.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/storage/ipc/shmem.c,v 1.84 2005/05/29 04:23:04 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/storage/ipc/shmem.c,v 1.85 2005/08/20 23:26:20 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -141,8 +141,8 @@ InitShmemAllocation(void *seghdr, bool init)
 void *
 ShmemAlloc(Size size)
 {
-	uint32		newStart;
-	uint32		newFree;
+	Size		newStart;
+	Size		newFree;
 	void	   *newSpace;
 
 	/* use volatile pointer to prevent code rearrangement */
@@ -415,3 +415,40 @@ ShmemInitStruct(const char *name, Size size, bool *foundPtr)
 	SpinLockRelease(ShmemIndexLock);
 	return structPtr;
 }
+
+
+/*
+ * Add two Size values, checking for overflow
+ */
+Size
+add_size(Size s1, Size s2)
+{
+	Size		result;
+
+	result = s1 + s2;
+	/* We are assuming Size is an unsigned type here... */
+	if (result < s1 || result < s2)
+		ereport(ERROR,
+				(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+				 errmsg("requested shared memory size overflows size_t")));
+	return result;
+}
+
+/*
+ * Multiply two Size values, checking for overflow
+ */
+Size
+mul_size(Size s1, Size s2)
+{
+	Size		result;
+
+	if (s1 == 0 || s2 == 0)
+		return 0;
+	result = s1 * s2;
+	/* We are assuming Size is an unsigned type here... */
+	if (result / s2 != s1)
+		ereport(ERROR,
+				(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+				 errmsg("requested shared memory size overflows size_t")));
+	return result;
+}
diff --git a/src/backend/storage/ipc/sinval.c b/src/backend/storage/ipc/sinval.c
index e771eea196e6f5d8d328842384de42dc4843d2c5..0d7b01f7966ccf7692e5e15358b8634739edd231 100644
--- a/src/backend/storage/ipc/sinval.c
+++ b/src/backend/storage/ipc/sinval.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/storage/ipc/sinval.c,v 1.76 2005/05/19 21:35:46 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/storage/ipc/sinval.c,v 1.77 2005/08/20 23:26:21 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -55,10 +55,10 @@ static void ProcessCatchupEvent(void);
 /*	should be called only by the POSTMASTER									*/
 /****************************************************************************/
 void
-CreateSharedInvalidationState(int maxBackends)
+CreateSharedInvalidationState(void)
 {
 	/* SInvalLock must be initialized already, during LWLock init */
-	SIBufferInit(maxBackends);
+	SIBufferInit();
 }
 
 /*
diff --git a/src/backend/storage/ipc/sinvaladt.c b/src/backend/storage/ipc/sinvaladt.c
index 64a9672b30881bc9d56f4da3d2409e4ea0f6a1bf..612f437322a51cacc234d3b2886747c536a002a9 100644
--- a/src/backend/storage/ipc/sinvaladt.c
+++ b/src/backend/storage/ipc/sinvaladt.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/storage/ipc/sinvaladt.c,v 1.59 2005/05/19 21:35:46 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/storage/ipc/sinvaladt.c,v 1.60 2005/08/20 23:26:21 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -32,14 +32,15 @@ static void SISetProcStateInvalid(SISeg *segP);
 /*
  * SInvalShmemSize --- return shared-memory space needed
  */
-int
-SInvalShmemSize(int maxBackends)
+Size
+SInvalShmemSize(void)
 {
-	/*
-	 * Figure space needed. Note sizeof(SISeg) includes the first
-	 * ProcState entry.
-	 */
-	return sizeof(SISeg) + sizeof(ProcState) * (maxBackends - 1);
+	Size		size;
+
+	size = offsetof(SISeg, procState);
+	size = add_size(size, mul_size(sizeof(ProcState), MaxBackends));
+
+	return size;
 }
 
 /*
@@ -47,16 +48,15 @@ SInvalShmemSize(int maxBackends)
  *		Create and initialize a new SI message buffer
  */
 void
-SIBufferInit(int maxBackends)
+SIBufferInit(void)
 {
-	int			segSize;
 	SISeg	   *segP;
 	int			i;
 	bool		found;
 
 	/* Allocate space in shared memory */
-	segSize = SInvalShmemSize(maxBackends);
-	shmInvalBuffer = segP = (SISeg *) ShmemInitStruct("shmInvalBuffer", segSize, &found);
+	shmInvalBuffer = segP = (SISeg *)
+		ShmemInitStruct("shmInvalBuffer", SInvalShmemSize(), &found);
 	if (found)
 		return;
 
@@ -64,13 +64,13 @@ SIBufferInit(int maxBackends)
 	segP->minMsgNum = 0;
 	segP->maxMsgNum = 0;
 	segP->lastBackend = 0;
-	segP->maxBackends = maxBackends;
-	segP->freeBackends = maxBackends;
+	segP->maxBackends = MaxBackends;
+	segP->freeBackends = MaxBackends;
 
 	/* The buffer[] array is initially all unused, so we need not fill it */
 
 	/* Mark all backends inactive */
-	for (i = 0; i < maxBackends; i++)
+	for (i = 0; i < segP->maxBackends; i++)
 	{
 		segP->procState[i].nextMsgNum = -1;		/* inactive */
 		segP->procState[i].resetState = false;
diff --git a/src/backend/storage/lmgr/lock.c b/src/backend/storage/lmgr/lock.c
index 22d389488d851ef03652a5cd491bb92a2da4a015..c11070a1309fa74954945d9acbcc4bbfd9077f5e 100644
--- a/src/backend/storage/lmgr/lock.c
+++ b/src/backend/storage/lmgr/lock.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/storage/lmgr/lock.c,v 1.156 2005/06/17 22:32:45 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/storage/lmgr/lock.c,v 1.157 2005/08/20 23:26:23 tgl Exp $
  *
  * NOTES
  *	  Outside modules can create a lock table and acquire/release
@@ -46,7 +46,8 @@
 /* This configuration variable is used to set the lock table size */
 int			max_locks_per_xact; /* set by guc.c */
 
-#define NLOCKENTS()	(max_locks_per_xact * (MaxBackends + max_prepared_xacts))
+#define NLOCKENTS()	\
+	mul_size(max_locks_per_xact, add_size(MaxBackends, max_prepared_xacts))
 
 
 /* Record that's written to 2PC state file when a lock is persisted */
@@ -1864,20 +1865,20 @@ next_item:
 /*
  * Estimate shared-memory space used for lock tables
  */
-int
+Size
 LockShmemSize(void)
 {
-	int			size = 0;
+	Size		size;
 	long		max_table_size = NLOCKENTS();
 
 	/* lock method headers */
-	size += MAX_LOCK_METHODS * MAXALIGN(sizeof(LockMethodData));
+	size = MAX_LOCK_METHODS * MAXALIGN(sizeof(LockMethodData));
 
 	/* lockHash table */
-	size += hash_estimate_size(max_table_size, sizeof(LOCK));
+	size = add_size(size, hash_estimate_size(max_table_size, sizeof(LOCK)));
 
 	/* proclockHash table */
-	size += hash_estimate_size(max_table_size, sizeof(PROCLOCK));
+	size = add_size(size, hash_estimate_size(max_table_size, sizeof(PROCLOCK)));
 
 	/*
 	 * Note we count only one pair of hash tables, since the userlocks
@@ -1886,7 +1887,7 @@ LockShmemSize(void)
 	 * Since the lockHash entry count above is only an estimate, add 10%
 	 * safety margin.
 	 */
-	size += size / 10;
+	size = add_size(size, size / 10);
 
 	return size;
 }
diff --git a/src/backend/storage/lmgr/lwlock.c b/src/backend/storage/lmgr/lwlock.c
index 8b7e6f945164c2302a15136d4a23dfc33d02c336..0b0bb4fb8f42506f032918ea55469fceda3a0e66 100644
--- a/src/backend/storage/lmgr/lwlock.c
+++ b/src/backend/storage/lmgr/lwlock.c
@@ -15,7 +15,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/storage/lmgr/lwlock.c,v 1.28 2005/04/28 21:47:15 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/storage/lmgr/lwlock.c,v 1.29 2005/08/20 23:26:24 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -129,17 +129,18 @@ NumLWLocks(void)
 /*
  * Compute shmem space needed for LWLocks.
  */
-int
+Size
 LWLockShmemSize(void)
 {
+	Size		size;
 	int			numLocks = NumLWLocks();
-	uint32		spaceLocks;
 
 	/* Allocate the LWLocks plus space for shared allocation counter. */
-	spaceLocks = numLocks * sizeof(LWLock) + 2 * sizeof(int);
-	spaceLocks = MAXALIGN(spaceLocks);
+	size = mul_size(numLocks, sizeof(LWLock));
+
+	size = add_size(size, 2 * sizeof(int));
 
-	return (int) spaceLocks;
+	return size;
 }
 
 
@@ -150,7 +151,7 @@ void
 CreateLWLocks(void)
 {
 	int			numLocks = NumLWLocks();
-	uint32		spaceLocks = LWLockShmemSize();
+	Size		spaceLocks = LWLockShmemSize();
 	LWLock	   *lock;
 	int			id;
 
diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c
index 90a37332f3a2eb0b396c8f8ecb738b1c37ebba97..efb09fb08f203f5bec95cadb5f23a2de503dc51c 100644
--- a/src/backend/storage/lmgr/proc.c
+++ b/src/backend/storage/lmgr/proc.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/storage/lmgr/proc.c,v 1.162 2005/08/08 03:11:55 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/storage/lmgr/proc.c,v 1.163 2005/08/20 23:26:24 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -91,15 +91,19 @@ static bool CheckStatementTimeout(void);
 /*
  * Report shared-memory space needed by InitProcGlobal.
  */
-int
+Size
 ProcGlobalShmemSize(void)
 {
-	int			size = 0;
-
-	size += MAXALIGN(sizeof(PROC_HDR)); /* ProcGlobal */
-	size += MAXALIGN(NUM_DUMMY_PROCS * sizeof(PGPROC));	/* DummyProcs */
-	size += MAXALIGN(MaxBackends * sizeof(PGPROC));		/* MyProcs */
-	size += MAXALIGN(sizeof(slock_t)); /* ProcStructLock */
+	Size		size = 0;
+
+	/* ProcGlobal */
+	size = add_size(size, sizeof(PROC_HDR));
+	/* DummyProcs */
+	size = add_size(size, mul_size(NUM_DUMMY_PROCS, sizeof(PGPROC)));
+	/* MyProcs */
+	size = add_size(size, mul_size(MaxBackends, sizeof(PGPROC)));
+	/* ProcStructLock */
+	size = add_size(size, sizeof(slock_t));
 
 	return size;
 }
diff --git a/src/backend/utils/hash/dynahash.c b/src/backend/utils/hash/dynahash.c
index 5124fe1efdba1aa298cdb51a5287ddc009add9e0..66be64a4e562864261e0d430ce647a89807477e8 100644
--- a/src/backend/utils/hash/dynahash.c
+++ b/src/backend/utils/hash/dynahash.c
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/utils/hash/dynahash.c,v 1.63 2005/06/26 23:32:33 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/utils/hash/dynahash.c,v 1.64 2005/08/20 23:26:24 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -44,6 +44,7 @@
 
 #include "postgres.h"
 
+#include "storage/shmem.h"
 #include "utils/dynahash.h"
 #include "utils/hsearch.h"
 #include "utils/memutils.h"
@@ -391,10 +392,10 @@ init_htab(HTAB *hashp, long nelem)
  * memory; therefore it does not count HTAB which is in local memory.
  * NB: assumes that all hash structure parameters have default values!
  */
-long
+Size
 hash_estimate_size(long num_entries, Size entrysize)
 {
-	long		size = 0;
+	Size		size;
 	long		nBuckets,
 				nSegments,
 				nDirEntries,
@@ -412,17 +413,20 @@ hash_estimate_size(long num_entries, Size entrysize)
 		nDirEntries <<= 1;		/* dir_alloc doubles dsize at each call */
 
 	/* fixed control info */
-	size += MAXALIGN(sizeof(HASHHDR));	/* but not HTAB, per above */
+	size = MAXALIGN(sizeof(HASHHDR));	/* but not HTAB, per above */
 	/* directory */
-	size += MAXALIGN(nDirEntries * sizeof(HASHSEGMENT));
+	size = add_size(size, mul_size(nDirEntries, sizeof(HASHSEGMENT)));
 	/* segments */
-	size += nSegments * MAXALIGN(DEF_SEGSIZE * sizeof(HASHBUCKET));
+	size = add_size(size, mul_size(nSegments,
+								   MAXALIGN(DEF_SEGSIZE * sizeof(HASHBUCKET))));
 	/* elements --- allocated in groups of up to HASHELEMENT_ALLOC_MAX */
 	elementSize = MAXALIGN(sizeof(HASHELEMENT)) + MAXALIGN(entrysize);
 	elementAllocCnt = Min(num_entries, HASHELEMENT_ALLOC_MAX);
 	elementAllocCnt = Max(elementAllocCnt, 1);
 	nElementAllocs = (num_entries - 1) / elementAllocCnt + 1;
-	size += nElementAllocs * elementAllocCnt * elementSize;
+	size = add_size(size,
+					mul_size(nElementAllocs,
+							 mul_size(elementAllocCnt, elementSize)));
 
 	return size;
 }
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index ae87d1af026dbff9d653c1cfdbb6e634cfcf3e5a..d51f8837eeec5eb512fabf68a716e1c46d3c7fc6 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.283 2005/08/19 18:58:18 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.284 2005/08/20 23:26:26 tgl Exp $
  *
  *--------------------------------------------------------------------
  */
@@ -78,6 +78,13 @@
 #define CONFIG_EXEC_PARAMS_NEW "global/config_exec_params.new"
 #endif
 
+/* upper limit for GUC variables measured in kilobytes of memory */
+#if SIZEOF_SIZE_T > 4
+#define MAX_KILOBYTES	INT_MAX
+#else
+#define MAX_KILOBYTES	(INT_MAX / 1024)
+#endif
+
 /* XXX these should appear in other modules' header files */
 extern bool Log_disconnections;
 extern DLLIMPORT bool check_function_bodies;
@@ -1027,6 +1034,10 @@ static struct config_int ConfigureNamesInt[] =
 	 * constraints here are partially unused. Similarly, the superuser
 	 * reserved number is checked to ensure it is less than the max
 	 * backends number.
+	 *
+	 * MaxBackends is limited to INT_MAX/4 because some places compute
+	 * 4*MaxBackends without any overflow check.  Likewise we have to
+	 * limit NBuffers to INT_MAX/2.
 	 */
 	{
 		{"max_connections", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
@@ -1034,7 +1045,7 @@ static struct config_int ConfigureNamesInt[] =
 			NULL
 		},
 		&MaxBackends,
-		100, 1, INT_MAX / BLCKSZ, NULL, NULL
+		100, 1, INT_MAX / 4, NULL, NULL
 	},
 
 	{
@@ -1043,7 +1054,7 @@ static struct config_int ConfigureNamesInt[] =
 			NULL
 		},
 		&ReservedBackends,
-		2, 0, INT_MAX / BLCKSZ, NULL, NULL
+		2, 0, INT_MAX / 4, NULL, NULL
 	},
 
 	{
@@ -1052,7 +1063,7 @@ static struct config_int ConfigureNamesInt[] =
 			NULL
 		},
 		&NBuffers,
-		1000, 16, INT_MAX / BLCKSZ, NULL, NULL
+		1000, 16, INT_MAX / 2, NULL, NULL
 	},
 
 	{
@@ -1061,7 +1072,7 @@ static struct config_int ConfigureNamesInt[] =
 			NULL
 		},
 		&num_temp_buffers,
-		1000, 100, INT_MAX / BLCKSZ, NULL, show_num_temp_buffers
+		1000, 100, INT_MAX / 2, NULL, show_num_temp_buffers
 	},
 
 	{
@@ -1094,7 +1105,7 @@ static struct config_int ConfigureNamesInt[] =
 						 "temporary disk files.")
 		},
 		&work_mem,
-		1024, 8 * BLCKSZ / 1024, INT_MAX / 1024, NULL, NULL
+		1024, 8 * BLCKSZ / 1024, MAX_KILOBYTES, NULL, NULL
 	},
 
 	{
@@ -1103,7 +1114,7 @@ static struct config_int ConfigureNamesInt[] =
 			gettext_noop("This includes operations such as VACUUM and CREATE INDEX.")
 		},
 		&maintenance_work_mem,
-		16384, 1024, INT_MAX / 1024, NULL, NULL
+		16384, 1024, MAX_KILOBYTES, NULL, NULL
 	},
 
 	{
@@ -1112,7 +1123,7 @@ static struct config_int ConfigureNamesInt[] =
 			NULL
 		},
 		&max_stack_depth,
-		2048, 100, INT_MAX / 1024, assign_max_stack_depth, NULL
+		2048, 100, MAX_KILOBYTES, assign_max_stack_depth, NULL
 	},
 
 	{
@@ -1193,7 +1204,7 @@ static struct config_int ConfigureNamesInt[] =
 			NULL
 		},
 		&max_prepared_xacts,
-		50, 0, 10000, NULL, NULL
+		50, 0, INT_MAX, NULL, NULL
 	},
 
 #ifdef LOCK_DEBUG
@@ -1310,7 +1321,7 @@ static struct config_int ConfigureNamesInt[] =
 			NULL
 		},
 		&XLOGbuffers,
-		8, 4, INT_MAX / BLCKSZ, NULL, NULL
+		8, 4, INT_MAX, NULL, NULL
 	},
 
 	{
diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample
index e05ca9fb887a62904dc084df113eb240ed0bd672..6e08b4cc32acfcba49b47ae37540b97cf9daa2b9 100644
--- a/src/backend/utils/misc/postgresql.conf.sample
+++ b/src/backend/utils/misc/postgresql.conf.sample
@@ -88,7 +88,7 @@
 
 #shared_buffers = 1000			# min 16 or max_connections*2, 8KB each
 #temp_buffers = 1000			# min 100, 8KB each
-#max_prepared_transactions = 50 # 0-10000
+#max_prepared_transactions = 50		# can be 0 or more
 #work_mem = 1024			# min 64, size in KB
 #maintenance_work_mem = 16384		# min 1024, size in KB
 #max_stack_depth = 2048			# min 100, size in KB
diff --git a/src/include/access/clog.h b/src/include/access/clog.h
index d03c5904f212e94c6fc8d7840e44bdd82e1ed1bd..f04eb02a58d4aaeebc36e9284a99bef17dbd36bd 100644
--- a/src/include/access/clog.h
+++ b/src/include/access/clog.h
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/access/clog.h,v 1.13 2005/06/06 17:01:24 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/access/clog.h,v 1.14 2005/08/20 23:26:29 tgl Exp $
  */
 #ifndef CLOG_H
 #define CLOG_H
@@ -31,7 +31,7 @@ typedef int XidStatus;
 extern void TransactionIdSetStatus(TransactionId xid, XidStatus status);
 extern XidStatus TransactionIdGetStatus(TransactionId xid);
 
-extern int	CLOGShmemSize(void);
+extern Size CLOGShmemSize(void);
 extern void CLOGShmemInit(void);
 extern void BootStrapCLOG(void);
 extern void StartupCLOG(void);
diff --git a/src/include/access/multixact.h b/src/include/access/multixact.h
index 29cd44a76d5d34ecd707cf334fa18d6adb89b73a..229f149406ebcc88949d014c1e264f2969639aaa 100644
--- a/src/include/access/multixact.h
+++ b/src/include/access/multixact.h
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/access/multixact.h,v 1.5 2005/08/20 01:29:16 ishii Exp $
+ * $PostgreSQL: pgsql/src/include/access/multixact.h,v 1.6 2005/08/20 23:26:29 tgl Exp $
  */
 #ifndef MULTIXACT_H
 #define MULTIXACT_H
@@ -48,7 +48,7 @@ extern int GetMultiXactIdMembers(MultiXactId multi, TransactionId **xids);
 
 extern void AtEOXact_MultiXact(void);
 
-extern int	MultiXactShmemSize(void);
+extern Size MultiXactShmemSize(void);
 extern void MultiXactShmemInit(void);
 extern void BootStrapMultiXact(void);
 extern void StartupMultiXact(void);
diff --git a/src/include/access/slru.h b/src/include/access/slru.h
index 602e2e3c1f24706126e1db52a3f2e38191869131..63e828a956317d87f7ba3bdb86d0236a3239869a 100644
--- a/src/include/access/slru.h
+++ b/src/include/access/slru.h
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/access/slru.h,v 1.12 2005/07/04 04:51:52 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/access/slru.h,v 1.13 2005/08/20 23:26:29 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -96,7 +96,7 @@ typedef SlruCtlData *SlruCtl;
 typedef struct SlruFlushData *SlruFlush;
 
 
-extern int	SimpleLruShmemSize(void);
+extern Size SimpleLruShmemSize(void);
 extern void SimpleLruInit(SlruCtl ctl, const char *name,
 			  LWLockId ctllock, const char *subdir);
 extern int	SimpleLruZeroPage(SlruCtl ctl, int pageno);
diff --git a/src/include/access/subtrans.h b/src/include/access/subtrans.h
index 2810e438d54c203c08480105f3e33ed0d4f04bb2..cdd16e90cbd6500ad7859227ca69ff9818e87e26 100644
--- a/src/include/access/subtrans.h
+++ b/src/include/access/subtrans.h
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/access/subtrans.h,v 1.6 2005/06/17 22:32:48 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/access/subtrans.h,v 1.7 2005/08/20 23:26:29 tgl Exp $
  */
 #ifndef SUBTRANS_H
 #define SUBTRANS_H
@@ -15,7 +15,7 @@ extern void SubTransSetParent(TransactionId xid, TransactionId parent);
 extern TransactionId SubTransGetParent(TransactionId xid);
 extern TransactionId SubTransGetTopmostTransaction(TransactionId xid);
 
-extern int	SUBTRANSShmemSize(void);
+extern Size SUBTRANSShmemSize(void);
 extern void SUBTRANSShmemInit(void);
 extern void BootStrapSUBTRANS(void);
 extern void StartupSUBTRANS(TransactionId oldestActiveXID);
diff --git a/src/include/access/twophase.h b/src/include/access/twophase.h
index e90b9e304db18be4528381d57ba87a2792bb7304..18fc3f9171b842841b61e8f7182d6d6e63f6aa41 100644
--- a/src/include/access/twophase.h
+++ b/src/include/access/twophase.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/access/twophase.h,v 1.4 2005/06/28 05:09:03 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/access/twophase.h,v 1.5 2005/08/20 23:26:29 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -27,7 +27,7 @@ typedef struct GlobalTransactionData *GlobalTransaction;
 /* GUC variable */
 extern int max_prepared_xacts;
 
-extern int	TwoPhaseShmemSize(void);
+extern Size TwoPhaseShmemSize(void);
 extern void TwoPhaseShmemInit(void);
 
 extern PGPROC *TwoPhaseGetDummyProc(TransactionId xid);
diff --git a/src/include/access/xlog.h b/src/include/access/xlog.h
index c16fdeeebd7491f307bf7deed9e1b3aca932f25b..b5a4667c112dc2c394ddeb4913def08089b30878 100644
--- a/src/include/access/xlog.h
+++ b/src/include/access/xlog.h
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/access/xlog.h,v 1.67 2005/07/29 19:30:08 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/access/xlog.h,v 1.68 2005/08/20 23:26:29 tgl Exp $
  */
 #ifndef XLOG_H
 #define XLOG_H
@@ -156,7 +156,7 @@ extern void xlog_redo(XLogRecPtr lsn, XLogRecord *record);
 extern void xlog_desc(char *buf, uint8 xl_info, char *rec);
 
 extern void UpdateControlFile(void);
-extern int	XLOGShmemSize(void);
+extern Size XLOGShmemSize(void);
 extern void XLOGShmemInit(void);
 extern void BootStrapXLOG(void);
 extern void StartupXLOG(void);
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 368f1935fa9f341ccf5f3aab58f156af112315a9..06aca08d90b094e6b2b0167969bc26331de7fd5d 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -604,6 +604,9 @@
    your system. */
 #undef PTHREAD_CREATE_JOINABLE
 
+/* The size of a `size_t', as computed by sizeof. */
+#undef SIZEOF_SIZE_T
+
 /* The size of a `unsigned long', as computed by sizeof. */
 #undef SIZEOF_UNSIGNED_LONG
 
diff --git a/src/include/postmaster/bgwriter.h b/src/include/postmaster/bgwriter.h
index 3cdfef1750fd296ca217daa12c0dce6ae1cc4224..c097308cc7bae566eb8dca2e8a017239e822221e 100644
--- a/src/include/postmaster/bgwriter.h
+++ b/src/include/postmaster/bgwriter.h
@@ -5,7 +5,7 @@
  *
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/include/postmaster/bgwriter.h,v 1.6 2005/06/30 00:00:52 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/postmaster/bgwriter.h,v 1.7 2005/08/20 23:26:33 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -28,7 +28,7 @@ extern void RequestCheckpoint(bool waitforit, bool warnontime);
 extern bool ForwardFsyncRequest(RelFileNode rnode, BlockNumber segno);
 extern void AbsorbFsyncRequests(void);
 
-extern int	BgWriterShmemSize(void);
+extern Size BgWriterShmemSize(void);
 extern void BgWriterShmemInit(void);
 
 #endif   /* _BGWRITER_H */
diff --git a/src/include/postmaster/postmaster.h b/src/include/postmaster/postmaster.h
index 1b11d056c71972dacad331275049c39428015f4c..0ad21cf59c0f8e79f7c78d1102502c12b903f579 100644
--- a/src/include/postmaster/postmaster.h
+++ b/src/include/postmaster/postmaster.h
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/postmaster/postmaster.h,v 1.10 2005/05/15 00:26:19 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/postmaster/postmaster.h,v 1.11 2005/08/20 23:26:33 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -42,7 +42,7 @@ extern void ClosePostmasterPorts(bool am_syslogger);
 extern pid_t postmaster_forkexec(int argc, char *argv[]);
 extern int	SubPostmasterMain(int argc, char *argv[]);
 
-extern size_t ShmemBackendArraySize(void);
+extern Size ShmemBackendArraySize(void);
 extern void ShmemBackendArrayAllocation(void);
 #endif
 
diff --git a/src/include/storage/buf_internals.h b/src/include/storage/buf_internals.h
index 2bd210dfb52e5534cce5428f7d61d8534598b583..6e083be21baf3a28151d47d927f9c45b060e6c70 100644
--- a/src/include/storage/buf_internals.h
+++ b/src/include/storage/buf_internals.h
@@ -8,7 +8,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/storage/buf_internals.h,v 1.78 2005/05/19 21:35:47 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/storage/buf_internals.h,v 1.79 2005/08/20 23:26:33 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -182,11 +182,11 @@ extern long int LocalBufferFlushCount;
 extern BufferDesc *StrategyGetBuffer(void);
 extern void StrategyFreeBuffer(BufferDesc *buf, bool at_head);
 extern int	StrategySyncStart(void);
-extern int	StrategyShmemSize(void);
+extern Size StrategyShmemSize(void);
 extern void StrategyInitialize(bool init);
 
 /* buf_table.c */
-extern int	BufTableShmemSize(int size);
+extern Size	BufTableShmemSize(int size);
 extern void InitBufTable(int size);
 extern int	BufTableLookup(BufferTag *tagPtr);
 extern int	BufTableInsert(BufferTag *tagPtr, int buf_id);
diff --git a/src/include/storage/bufmgr.h b/src/include/storage/bufmgr.h
index a88565e1596fc89ab50c0ef7744d3f29309d3a1b..63def4a2e29169893f049cb53fddc09283323dd4 100644
--- a/src/include/storage/bufmgr.h
+++ b/src/include/storage/bufmgr.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/storage/bufmgr.h,v 1.95 2005/08/12 05:05:51 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/storage/bufmgr.h,v 1.96 2005/08/20 23:26:33 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -140,7 +140,7 @@ extern void DropBuffers(Oid dbid);
 #ifdef NOT_USED
 extern void PrintPinnedBufs(void);
 #endif
-extern int	BufferShmemSize(void);
+extern Size BufferShmemSize(void);
 extern RelFileNode BufferGetFileNode(Buffer buffer);
 
 extern void SetBufferCommitInfoNeedsSave(Buffer buffer);
diff --git a/src/include/storage/freespace.h b/src/include/storage/freespace.h
index fe6cca76bcd715c803e7b8d2d9a7b28a3def9b09..1ce7b7f08ae08918001cd1e7778b4596c27e0204 100644
--- a/src/include/storage/freespace.h
+++ b/src/include/storage/freespace.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/storage/freespace.h,v 1.17 2004/12/31 22:03:42 pgsql Exp $
+ * $PostgreSQL: pgsql/src/include/storage/freespace.h,v 1.18 2005/08/20 23:26:33 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -37,7 +37,7 @@ extern int	MaxFSMPages;
  * function prototypes
  */
 extern void InitFreeSpaceMap(void);
-extern int	FreeSpaceShmemSize(void);
+extern Size FreeSpaceShmemSize(void);
 
 extern BlockNumber GetPageWithFreeSpace(RelFileNode *rel, Size spaceNeeded);
 extern BlockNumber RecordAndGetPageWithFreeSpace(RelFileNode *rel,
diff --git a/src/include/storage/lock.h b/src/include/storage/lock.h
index e434683d0e5ea5ead215a5dda299503c2025d12b..6610b1381b750cf360abdf35673f7cec602ee326 100644
--- a/src/include/storage/lock.h
+++ b/src/include/storage/lock.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/storage/lock.h,v 1.89 2005/06/17 22:32:50 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/storage/lock.h,v 1.90 2005/08/20 23:26:33 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -391,7 +391,7 @@ extern int LockCheckConflicts(LockMethod lockMethodTable,
 extern void GrantLock(LOCK *lock, PROCLOCK *proclock, LOCKMODE lockmode);
 extern void GrantAwaitedLock(void);
 extern void RemoveFromWaitQueue(PGPROC *proc);
-extern int	LockShmemSize(void);
+extern Size LockShmemSize(void);
 extern bool DeadLockCheck(PGPROC *proc);
 extern void DeadLockReport(void);
 extern void RememberSimpleDeadLock(PGPROC *proc1,
diff --git a/src/include/storage/lwlock.h b/src/include/storage/lwlock.h
index 16c369117c787dfd1f497b4d395319f9573cb6a2..98d1d40ad536d072d32a1ddaaf048fb1ae5c8f46 100644
--- a/src/include/storage/lwlock.h
+++ b/src/include/storage/lwlock.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/storage/lwlock.h,v 1.21 2005/07/27 08:05:36 neilc Exp $
+ * $PostgreSQL: pgsql/src/include/storage/lwlock.h,v 1.22 2005/08/20 23:26:34 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -73,7 +73,7 @@ extern void LWLockReleaseAll(void);
 extern bool LWLockHeldByMe(LWLockId lockid);
 
 extern int	NumLWLocks(void);
-extern int	LWLockShmemSize(void);
+extern Size LWLockShmemSize(void);
 extern void CreateLWLocks(void);
 
 #endif   /* LWLOCK_H */
diff --git a/src/include/storage/pg_shmem.h b/src/include/storage/pg_shmem.h
index ea7ecd16a97f71b0bb7167e310c3d796d4d74603..c034e19ce4b41f0decf803b2e121701652e00aa4 100644
--- a/src/include/storage/pg_shmem.h
+++ b/src/include/storage/pg_shmem.h
@@ -17,7 +17,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/storage/pg_shmem.h,v 1.14 2004/12/31 22:03:42 pgsql Exp $
+ * $PostgreSQL: pgsql/src/include/storage/pg_shmem.h,v 1.15 2005/08/20 23:26:34 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -29,8 +29,8 @@ typedef struct PGShmemHeader	/* standard header for all Postgres shmem */
 	int32		magic;			/* magic # to identify Postgres segments */
 #define PGShmemMagic  679834893
 	pid_t		creatorPID;		/* PID of creating process */
-	uint32		totalsize;		/* total size of segment */
-	uint32		freeoffset;		/* offset to first free space */
+	Size		totalsize;		/* total size of segment */
+	Size		freeoffset;		/* offset to first free space */
 #ifndef WIN32					/* Windows doesn't have useful inode#s */
 	dev_t		device;			/* device data directory is on */
 	ino_t		inode;			/* inode number of data directory */
@@ -45,8 +45,8 @@ extern void *UsedShmemSegAddr;
 extern void PGSharedMemoryReAttach(void);
 #endif
 
-extern PGShmemHeader *PGSharedMemoryCreate(uint32 size, bool makePrivate,
-					 int port);
+extern PGShmemHeader *PGSharedMemoryCreate(Size size, bool makePrivate,
+										   int port);
 extern bool PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2);
 extern void PGSharedMemoryDetach(void);
 
diff --git a/src/include/storage/proc.h b/src/include/storage/proc.h
index 63090de1cfd9a4135479a3aa3824a78db677e5d3..b9c9c5d5be63206911d7f70f58430de09dec35c4 100644
--- a/src/include/storage/proc.h
+++ b/src/include/storage/proc.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/storage/proc.h,v 1.80 2005/07/31 17:19:22 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/storage/proc.h,v 1.81 2005/08/20 23:26:34 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -122,7 +122,7 @@ extern int	StatementTimeout;
  * Function Prototypes
  */
 extern int	ProcGlobalSemas(void);
-extern int	ProcGlobalShmemSize(void);
+extern Size ProcGlobalShmemSize(void);
 extern void InitProcGlobal(void);
 extern void InitProcess(void);
 extern void InitDummyProcess(int proctype);
diff --git a/src/include/storage/procarray.h b/src/include/storage/procarray.h
index af1209d219265fd68c4837c906651fe88075e297..1c010ff5b000f656f1ce358b09f144697144cad2 100644
--- a/src/include/storage/procarray.h
+++ b/src/include/storage/procarray.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/storage/procarray.h,v 1.4 2005/08/20 01:26:29 ishii Exp $
+ * $PostgreSQL: pgsql/src/include/storage/procarray.h,v 1.5 2005/08/20 23:26:34 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -17,7 +17,7 @@
 #include "storage/lock.h"
 
 
-extern int	ProcArrayShmemSize(void);
+extern Size ProcArrayShmemSize(void);
 extern void CreateSharedProcArray(void);
 extern void ProcArrayAdd(PGPROC *proc);
 extern void ProcArrayRemove(PGPROC *proc);
diff --git a/src/include/storage/shmem.h b/src/include/storage/shmem.h
index 417b7b65e9a66e372ae2d62ca5c6fcbf1e70f158..520f8eb3adff930c30de33a0c680f47e7ed38adf 100644
--- a/src/include/storage/shmem.h
+++ b/src/include/storage/shmem.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/storage/shmem.h,v 1.44 2004/12/31 22:03:42 pgsql Exp $
+ * $PostgreSQL: pgsql/src/include/storage/shmem.h,v 1.45 2005/08/20 23:26:35 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -68,7 +68,8 @@ extern void InitShmemIndex(void);
 extern HTAB *ShmemInitHash(const char *name, long init_size, long max_size,
 			  HASHCTL *infoP, int hash_flags);
 extern void *ShmemInitStruct(const char *name, Size size, bool *foundPtr);
-
+extern Size add_size(Size s1, Size s2);
+extern Size mul_size(Size s1, Size s2);
 
 /* size constants for the shmem index table */
  /* max size of data structure string name */
diff --git a/src/include/storage/sinval.h b/src/include/storage/sinval.h
index 0f4c0f68f3f20792f500cc59f1f6a7af665000c6..2fe0cce8366c2701968e3378fdaccf4c3b3e5743 100644
--- a/src/include/storage/sinval.h
+++ b/src/include/storage/sinval.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/storage/sinval.h,v 1.41 2005/05/19 21:35:47 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/storage/sinval.h,v 1.42 2005/08/20 23:26:35 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -83,8 +83,8 @@ typedef union
 } SharedInvalidationMessage;
 
 
-extern int	SInvalShmemSize(int maxBackends);
-extern void CreateSharedInvalidationState(int maxBackends);
+extern Size SInvalShmemSize(void);
+extern void CreateSharedInvalidationState(void);
 extern void InitBackendSharedInvalidationState(void);
 
 extern void SendSharedInvalidMessage(SharedInvalidationMessage *msg);
diff --git a/src/include/storage/sinvaladt.h b/src/include/storage/sinvaladt.h
index 0be6a3865081345389ba269b0d0528758c72460d..7d1f5f468554187caa4708d6d382498f024b56e9 100644
--- a/src/include/storage/sinvaladt.h
+++ b/src/include/storage/sinvaladt.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/storage/sinvaladt.h,v 1.38 2005/05/19 21:35:47 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/storage/sinvaladt.h,v 1.39 2005/08/20 23:26:35 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -107,7 +107,7 @@ extern SISeg *shmInvalBuffer;	/* pointer to the shared inval buffer */
 /*
  * prototypes for functions in sinvaladt.c
  */
-extern void SIBufferInit(int maxBackends);
+extern void SIBufferInit(void);
 extern int	SIBackendInit(SISeg *segP);
 
 extern bool SIInsertDataEntry(SISeg *segP, SharedInvalidationMessage *data);
diff --git a/src/include/utils/hsearch.h b/src/include/utils/hsearch.h
index b4bede7666e64a68a2f87663f3e3e085607aca61..0821610b9bb58757af34286f8af3ca1ccaf87fc0 100644
--- a/src/include/utils/hsearch.h
+++ b/src/include/utils/hsearch.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/utils/hsearch.h,v 1.39 2005/06/26 23:32:34 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/utils/hsearch.h,v 1.40 2005/08/20 23:26:37 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -190,7 +190,7 @@ extern void *hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action,
 			bool *foundPtr);
 extern void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp);
 extern void *hash_seq_search(HASH_SEQ_STATUS *status);
-extern long hash_estimate_size(long num_entries, Size entrysize);
+extern Size hash_estimate_size(long num_entries, Size entrysize);
 extern long hash_select_dirsize(long num_entries);
 
 /*