diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 963c3aed3b1b5e4ef2bcfc72a725d0c24f0ee123..83ce799e51cfc5395583bdc12fe31474a643c287 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -3,7 +3,7 @@
  *
  * Copyright 2000-2002 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.83 2002/10/15 02:24:15 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.84 2002/10/23 19:23:56 momjian Exp $
  */
 #include "postgres_fe.h"
 #include "command.h"
@@ -493,7 +493,8 @@ exec_command(const char *cmd,
 	/* help */
 	else if (strcmp(cmd, "h") == 0 || strcmp(cmd, "help") == 0)
 	{
-		helpSQL(options_string ? &options_string[strspn(options_string, " \t\n\r")] : NULL);
+		helpSQL(options_string ? &options_string[strspn(options_string, " \t\n\r")] : NULL,
+				pset.popt.topt.pager);
 		/* set pointer to end of line */
 		if (string)
 			string += strlen(string);
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index e74b8c2769fecd1fc5df3634eaab785b63c268a5..c79d61cfbca826211497a20083528b1b8a9dd0f5 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -3,7 +3,7 @@
  *
  * Copyright 2000 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.48 2002/10/15 16:44:21 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.49 2002/10/23 19:23:56 momjian Exp $
  */
 #include "postgres_fe.h"
 
@@ -515,3 +515,46 @@ SendQuery(const char *query)
 
 	return success;
 }
+
+
+/*
+ * PageOutput
+ *
+ * Tests if pager is needed and returns appropriate FILE pointer.
+ */
+FILE *
+PageOutput(int lines, bool pager)
+{
+	/* check whether we need / can / are supposed to use pager */
+	if (pager
+#ifndef WIN32
+		&&
+		isatty(fileno(stdin)) &&
+		isatty(fileno(stdout))
+#endif
+		)
+	{
+		const char *pagerprog;
+
+#ifdef TIOCGWINSZ
+		int			result;
+		struct winsize screen_size;
+
+		result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size);
+		if (result == -1 || lines > screen_size.ws_row)
+		{
+#endif
+			pagerprog = getenv("PAGER");
+			if (!pagerprog)
+				pagerprog = DEFAULT_PAGER;
+#ifndef WIN32
+			pqsignal(SIGPIPE, SIG_IGN);
+#endif
+			return popen(pagerprog, "w");
+#ifdef TIOCGWINSZ
+		}
+#endif
+	}
+
+	return stdout;
+}
diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h
index a5b0881245c0fddcca9f127bbbf260d4e695eee1..29af2f648e904e065f1001a2357b0097b16f611d 100644
--- a/src/bin/psql/common.h
+++ b/src/bin/psql/common.h
@@ -3,7 +3,7 @@
  *
  * Copyright 2000 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/common.h,v 1.19 2002/10/15 02:24:16 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/common.h,v 1.20 2002/10/23 19:23:56 momjian Exp $
  */
 #ifndef COMMON_H
 #define COMMON_H
@@ -37,6 +37,8 @@ extern PGresult *PSQLexec(const char *query, bool ignore_command_ok);
 
 extern bool SendQuery(const char *query);
 
+extern FILE *PageOutput(int lines, bool pager);
+
 /* sprompt.h */
 extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
 
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index af60be1bca90f3ee207ab0c5315311cde09f4edc..7dfb594b435b8e15ef811a8420678c2a33323121 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -3,9 +3,10 @@
  *
  * Copyright 2000 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.58 2002/10/18 22:05:36 petere Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.59 2002/10/23 19:23:56 momjian Exp $
  */
 #include "postgres_fe.h"
+#include "common.h"
 #include "print.h"
 #include "help.h"
 
@@ -161,48 +162,11 @@ struct winsize
 void
 slashUsage(bool pager)
 {
-	FILE	   *output,
-			   *pagerfd = NULL;
-
-	/* check whether we need / can / are supposed to use pager */
-	if (pager
-#ifndef WIN32
-		&&
-		isatty(fileno(stdin)) &&
-		isatty(fileno(stdout))
-#endif
-		)
-	{
-		const char *pagerprog;
+	FILE	   *output;
 
-#ifdef TIOCGWINSZ
-		int			result;
-		struct winsize screen_size;
+	output = PageOutput(50, pager);
 
-		result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size);
-		if (result == -1 || 50 > screen_size.ws_row)
-		{
-#endif
-			pagerprog = getenv("PAGER");
-			if (!pagerprog)
-				pagerprog = DEFAULT_PAGER;
-			pagerfd = popen(pagerprog, "w");
-#ifdef TIOCGWINSZ
-		}
-#endif
-	}
-
-	if (pagerfd)
-	{
-		output = pagerfd;
-#ifndef WIN32
-		pqsignal(SIGPIPE, SIG_IGN);
-#endif
-	}
-	else
-		output = stdout;
-
-	/* if you add/remove a line here, change the row test above */
+	/* if you add/remove a line here, change the row count above */
 
 	/*
 	 * if this " is the start of the string then it ought to end there to
@@ -262,9 +226,9 @@ slashUsage(bool pager)
 	fprintf(output, _(" \\z [PATTERN]   list table access privileges (same as \\dp)\n"));
 	fprintf(output, _(" \\! [COMMAND]   execute command in shell or start interactive shell\n"));
 
-	if (pagerfd)
+	if (output != stdout)
 	{
-		pclose(pagerfd);
+		pclose(output);
 #ifndef WIN32
 		pqsignal(SIGPIPE, SIG_DFL);
 #endif
@@ -278,7 +242,7 @@ slashUsage(bool pager)
  *
  */
 void
-helpSQL(const char *topic)
+helpSQL(const char *topic, bool pager)
 {
 #define VALUE_OR_NULL(a) ((a) ? (a) : "")
 
@@ -286,21 +250,31 @@ helpSQL(const char *topic)
 	{
 		int			i;
 		int			items_per_column = (QL_HELP_COUNT + 2) / 3;
+		FILE		*output;
+
+		output = PageOutput(items_per_column, pager);
 
-		puts(_("Available help:"));
+		fputs(_("Available help:\n"), output);
 
 		for (i = 0; i < items_per_column; i++)
 		{
-			printf("  %-26s%-26s",
+			fprintf(output, "  %-26s%-26s",
 				   VALUE_OR_NULL(QL_HELP[i].cmd),
 				   VALUE_OR_NULL(QL_HELP[i + items_per_column].cmd));
 			if (i + 2 * items_per_column < QL_HELP_COUNT)
-				printf("%-26s",
+				fprintf(output, "%-26s",
 				   VALUE_OR_NULL(QL_HELP[i + 2 * items_per_column].cmd));
-			fputc('\n', stdout);
+			fputc('\n', output);
+		}
+		/* Only close if we used the pager */
+		if (output != stdout)
+		{
+			pclose(output);
+#ifndef WIN32
+			pqsignal(SIGPIPE, SIG_DFL);
+#endif
 		}
 	}
-
 	else
 	{
 		int			i;
diff --git a/src/bin/psql/help.h b/src/bin/psql/help.h
index 6c655f306b379f5d87de0b8c0a322780c3306185..28a878646615644ca3a6742c7ffe443dc23d3e9c 100644
--- a/src/bin/psql/help.h
+++ b/src/bin/psql/help.h
@@ -3,7 +3,7 @@
  *
  * Copyright 2000 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/help.h,v 1.9 2002/07/15 01:56:25 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/help.h,v 1.10 2002/10/23 19:23:57 momjian Exp $
  */
 #ifndef HELP_H
 #define HELP_H
@@ -12,7 +12,7 @@ void		usage(void);
 
 void		slashUsage(bool pager);
 
-void		helpSQL(const char *topic);
+void		helpSQL(const char *topic, bool pager);
 
 void		print_copyright(void);
 
diff --git a/src/bin/psql/print.c b/src/bin/psql/print.c
index f74fedce079f43706ec887cbaafc66097969a58e..565fe809d5cac478745b5dd24c4b75989227f57d 100644
--- a/src/bin/psql/print.c
+++ b/src/bin/psql/print.c
@@ -3,9 +3,10 @@
  *
  * Copyright 2000 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/print.c,v 1.32 2002/10/03 17:09:42 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/print.c,v 1.33 2002/10/23 19:23:57 momjian Exp $
  */
 #include "postgres_fe.h"
+#include "common.h"
 #include "print.h"
 
 #include <math.h>
@@ -970,9 +971,7 @@ printTable(const char *title,
 {
 	const char *default_footer[] = {NULL};
 	unsigned short int border = opt->border;
-	FILE	   *pagerfd = NULL,
-			   *output;
-
+	FILE		*output;
 
 	if (opt->format == PRINT_NOTHING)
 		return;
@@ -983,25 +982,12 @@ printTable(const char *title,
 	if (opt->format != PRINT_HTML && border > 2)
 		border = 2;
 
-
-	/* check whether we need / can / are supposed to use pager */
-	if (fout == stdout && opt->pager
-#ifndef WIN32
-		&&
-		isatty(fileno(stdin)) &&
-		isatty(fileno(stdout))
-#endif
-		)
+	if (fout == stdout)
 	{
-		const char *pagerprog;
-
-#ifdef TIOCGWINSZ
-		unsigned int col_count = 0,
-					row_count = 0,
-					lines;
+		int col_count = 0,
+			row_count = 0,
+			lines;
 		const char *const * ptr;
-		int			result;
-		struct winsize screen_size;
 
 		/* rough estimate of columns and rows */
 		if (headers)
@@ -1020,31 +1006,11 @@ printTable(const char *title,
 		if (footers && !opt->tuples_only)
 			for (ptr = footers; *ptr; ptr++)
 				lines++;
-
-		result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size);
-		if (result == -1 || lines > screen_size.ws_row)
-		{
-#endif
-			pagerprog = getenv("PAGER");
-			if (!pagerprog)
-				pagerprog = DEFAULT_PAGER;
-			pagerfd = popen(pagerprog, "w");
-#ifdef TIOCGWINSZ
-		}
-#endif
-	}
-
-	if (pagerfd)
-	{
-		output = pagerfd;
-#ifndef WIN32
-		pqsignal(SIGPIPE, SIG_IGN);
-#endif
+		output = PageOutput(lines, opt->pager);
 	}
 	else
 		output = fout;
 
-
 	/* print the stuff */
 
 	switch (opt->format)
@@ -1077,9 +1043,10 @@ printTable(const char *title,
 			fprintf(stderr, "+ Oops, you shouldn't see this!\n");
 	}
 
-	if (pagerfd)
+	/* Only close if we used the pager */
+	if (fout == stdout && output != stdout)
 	{
-		pclose(pagerfd);
+		pclose(output);
 #ifndef WIN32
 		pqsignal(SIGPIPE, SIG_DFL);
 #endif