diff --git a/doc/src/sgml/ref/pg_config-ref.sgml b/doc/src/sgml/ref/pg_config-ref.sgml
index db09883902fae5783de7652a8f57eeb3e80e4fe5..daf0496290c34597e293f438005f5c9260cec3d1 100644
--- a/doc/src/sgml/ref/pg_config-ref.sgml
+++ b/doc/src/sgml/ref/pg_config-ref.sgml
@@ -1,4 +1,4 @@
-<!-- $PostgreSQL: pgsql/doc/src/sgml/ref/pg_config-ref.sgml,v 1.20 2005/06/09 18:15:05 petere Exp $ -->
+<!-- $PostgreSQL: pgsql/doc/src/sgml/ref/pg_config-ref.sgml,v 1.21 2005/08/09 22:47:03 tgl Exp $ -->
 
 <refentry id="app-pgconfig">
  <refmeta>
@@ -19,16 +19,7 @@
  <refsynopsisdiv>
   <cmdsynopsis>
    <command>pg_config</command>
-   <group choice="req" rep="repeat">
-    <arg>--bindir</arg>
-    <arg>--includedir</arg>
-    <arg>--includedir-server</arg>
-    <arg>--libdir</arg>
-    <arg>--pkglibdir</arg>
-    <arg>--pgxs</arg>
-    <arg>--configure</arg>
-    <arg>--version</arg>
-   </group>
+   <arg rep="repeat"><replaceable>option</replaceable></arg>
   </cmdsynopsis>
  </refsynopsisdiv>
 
@@ -48,7 +39,8 @@
   <title>Options</title>
 
   <para>
-   To use <application>pg_config</>, supply one or more of the following options:
+   To use <application>pg_config</>, supply one or more of the following
+   options:
    <variablelist>
     <varlistentry>
      <term><option>--bindir</option></>
@@ -124,18 +116,93 @@
      </listitem>
     </varlistentry>
 
+    <varlistentry>
+     <term><option>--cc</option></>
+     <listitem>
+      <para>
+       Print the value of the CC macro that was used for building
+       <productname>PostgreSQL</>.  This shows the C compiler used.
+      </para>
+     </listitem>
+    </varlistentry>
+
+    <varlistentry>
+     <term><option>--cppflags</option></>
+     <listitem>
+      <para>
+       Print the value of the CPPFLAGS macro that was used for building
+       <productname>PostgreSQL</>.  This shows C compiler switches needed
+       at preprocessing time (typically, <literal>-I</> switches).
+      </para>
+     </listitem>
+    </varlistentry>
+
+    <varlistentry>
+     <term><option>--cflags</option></>
+     <listitem>
+      <para>
+       Print the value of the CFLAGS macro that was used for building
+       <productname>PostgreSQL</>.  This shows C compiler switches.
+      </para>
+     </listitem>
+    </varlistentry>
+
+    <varlistentry>
+     <term><option>--cflags_sl</option></>
+     <listitem>
+      <para>
+       Print the value of the CFLAGS_SL macro that was used for building
+       <productname>PostgreSQL</>.  This shows extra C compiler switches
+       used for building shared libraries.
+      </para>
+     </listitem>
+    </varlistentry>
+
+    <varlistentry>
+     <term><option>--ldflags</option></>
+     <listitem>
+      <para>
+       Print the value of the LDFLAGS macro that was used for building
+       <productname>PostgreSQL</>.  This shows linker switches.
+      </para>
+     </listitem>
+    </varlistentry>
+
+    <varlistentry>
+     <term><option>--ldflags_sl</option></>
+     <listitem>
+      <para>
+       Print the value of the LDFLAGS_SL macro that was used for building
+       <productname>PostgreSQL</>.  This shows linker switches
+       used for building shared libraries.
+      </para>
+     </listitem>
+    </varlistentry>
+
+    <varlistentry>
+     <term><option>--libs</option></>
+     <listitem>
+      <para>
+       Print the value of the LIBS macro that was used for building
+       <productname>PostgreSQL</>.  This normally contains <literal>-l</>
+       switches for external libraries linked into <productname>PostgreSQL</>.
+      </para>
+     </listitem>
+    </varlistentry>
+
     <varlistentry>
      <term><option>--version</option></>
      <listitem>
       <para>
-       Print the version of <productname>PostgreSQL</> and exit.
+       Print the version of <productname>PostgreSQL</>.
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
 
-   If more than one option (except for <option>--version</>) is given, the
-   information is printed in that order, one item per line.
+   If more than one option is given, the information is printed in that order,
+   one item per line.  If no options are given, all available information
+   is printed, with labels.
   </para>
  </refsect1>
 
@@ -152,6 +219,13 @@
    exit status to see whether it succeeded.
   </para>
 
+  <para>
+   The options <option>--cc</option>, <option>--cppflags</option>,
+   <option>--cflags</option>, <option>--cflags_sl</option>,
+   <option>--ldflags</option>, <option>--ldflags_sl</option>,
+   and <option>--libs</option> are new in <productname>PostgreSQL</> 8.1.
+  </para>
+
   <para>
    In releases prior to <productname>PostgreSQL</> 7.1, before
    <command>pg_config</command> came to be, a method for finding the
diff --git a/src/bin/pg_config/Makefile b/src/bin/pg_config/Makefile
index 84306b795327be22e66bbcf6503f298cf686096d..e25e111f099d960dad9f18af13d4a0ab3d6e5b62 100644
--- a/src/bin/pg_config/Makefile
+++ b/src/bin/pg_config/Makefile
@@ -4,7 +4,7 @@
 #
 # Copyright (c) 1998-2005, PostgreSQL Global Development Group
 #
-# $PostgreSQL: pgsql/src/bin/pg_config/Makefile,v 1.14 2005/01/20 22:54:57 neilc Exp $
+# $PostgreSQL: pgsql/src/bin/pg_config/Makefile,v 1.15 2005/08/09 22:47:03 tgl Exp $
 #
 #-------------------------------------------------------------------------
 
@@ -15,7 +15,19 @@ include $(top_builddir)/src/Makefile.global
 
 OBJS=   pg_config.o $(WIN32RES)
 
-override CPPFLAGS := -DFRONTEND -I$(libpq_srcdir) -DVAL_CONFIGURE="\"$(configure_args)\"" $(CPPFLAGS)
+# don't include subdirectory-path-dependent -I and -L switches
+STD_CPPFLAGS := $(filter-out -I$(top_srcdir)/src/include -I$(top_builddir)/src/include,$(CPPFLAGS))
+STD_LDFLAGS := $(filter-out -L$(top_builddir)/src/port,$(LDFLAGS))
+
+override CPPFLAGS += -DFRONTEND
+override CPPFLAGS += -DVAL_CONFIGURE="\"$(configure_args)\""
+override CPPFLAGS += -DVAL_CC="\"$(CC)\""
+override CPPFLAGS += -DVAL_CPPFLAGS="\"$(STD_CPPFLAGS)\""
+override CPPFLAGS += -DVAL_CFLAGS="\"$(CFLAGS)\""
+override CPPFLAGS += -DVAL_CFLAGS_SL="\"$(CFLAGS_SL)\""
+override CPPFLAGS += -DVAL_LDFLAGS="\"$(STD_LDFLAGS)\""
+override CPPFLAGS += -DVAL_LDFLAGS_SL="\"$(LDFLAGS_SL)\""
+override CPPFLAGS += -DVAL_LIBS="\"$(LIBS)\""
 
 all: submake-libpgport pg_config
 
diff --git a/src/bin/pg_config/pg_config.c b/src/bin/pg_config/pg_config.c
index 28baf2262d09b39265610742d13018efa2d5e3bb..ae1fce770df8c1a17a180fe182b08d0c6e03082f 100644
--- a/src/bin/pg_config/pg_config.c
+++ b/src/bin/pg_config/pg_config.c
@@ -17,35 +17,267 @@
  *
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/pg_config/pg_config.c,v 1.11 2005/02/22 04:38:40 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/pg_config/pg_config.c,v 1.12 2005/08/09 22:47:03 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
 
 #include "postgres.h"
+
 #include "port.h"
-#include <stdio.h>
 
 static const char *progname;
+static char		mypath[MAXPGPATH];
+
+
+/*
+ * For each piece of information known to pg_config, we define a subroutine
+ * to print it.  This is probably overkill, but it avoids code duplication
+ * and accidentally omitting items from the "all" display.
+ */
+
+static void
+show_bindir(bool all)
+{
+	char		path[MAXPGPATH];
+	char	   *lastsep;
+
+	if (all)
+		printf("BINDIR = ");
+	/* assume we are located in the bindir */
+	strcpy(path, mypath);
+	lastsep = strrchr(path, '/');
+	if (lastsep)
+		*lastsep = '\0';
+	printf("%s\n", path);
+}
+
+static void
+show_includedir(bool all)
+{
+	char		path[MAXPGPATH];
+
+	if (all)
+		printf("INCLUDEDIR = ");
+	get_include_path(mypath, path);
+	printf("%s\n", path);
+}
+
+static void
+show_includedir_server(bool all)
+{
+	char		path[MAXPGPATH];
+
+	if (all)
+		printf("INCLUDEDIR-SERVER = ");
+	get_includeserver_path(mypath, path);
+	printf("%s\n", path);
+}
+
+static void
+show_libdir(bool all)
+{
+	char		path[MAXPGPATH];
+
+	if (all)
+		printf("LIBDIR = ");
+	get_lib_path(mypath, path);
+	printf("%s\n", path);
+}
+
+static void
+show_pkglibdir(bool all)
+{
+	char		path[MAXPGPATH];
+
+	if (all)
+		printf("PKGLIBDIR = ");
+	get_pkglib_path(mypath, path);
+	printf("%s\n", path);
+}
+
+static void
+show_pgxs(bool all)
+{
+	char		path[MAXPGPATH];
+
+	if (all)
+		printf("PGXS = ");
+	get_pkglib_path(mypath, path);
+	strncat(path, "/pgxs/src/makefiles/pgxs.mk", MAXPGPATH - 1);
+	printf("%s\n", path);
+}
+
+static void
+show_configure(bool all)
+{
+#ifdef VAL_CONFIGURE
+	if (all)
+		printf("CONFIGURE = ");
+	printf("%s\n", VAL_CONFIGURE);
+#else
+	if (!all)
+		printf("not recorded\n");
+#endif
+}
+
+static void
+show_cc(bool all)
+{
+#ifdef VAL_CC
+	if (all)
+		printf("CC = ");
+	printf("%s\n", VAL_CC);
+#else
+	if (!all)
+		printf("not recorded\n");
+#endif
+}
+
+static void
+show_cppflags(bool all)
+{
+#ifdef VAL_CPPFLAGS
+	if (all)
+		printf("CPPFLAGS = ");
+	printf("%s\n", VAL_CPPFLAGS);
+#else
+	if (!all)
+		printf("not recorded\n");
+#endif
+}
+
+static void
+show_cflags(bool all)
+{
+#ifdef VAL_CFLAGS
+	if (all)
+		printf("CFLAGS = ");
+	printf("%s\n", VAL_CFLAGS);
+#else
+	if (!all)
+		printf("not recorded\n");
+#endif
+}
+
+static void
+show_cflags_sl(bool all)
+{
+#ifdef VAL_CFLAGS_SL
+	if (all)
+		printf("CFLAGS_SL = ");
+	printf("%s\n", VAL_CFLAGS_SL);
+#else
+	if (!all)
+		printf("not recorded\n");
+#endif
+}
+
+static void
+show_ldflags(bool all)
+{
+#ifdef VAL_LDFLAGS
+	if (all)
+		printf("LDFLAGS = ");
+	printf("%s\n", VAL_LDFLAGS);
+#else
+	if (!all)
+		printf("not recorded\n");
+#endif
+}
+
+static void
+show_ldflags_sl(bool all)
+{
+#ifdef VAL_LDFLAGS_SL
+	if (all)
+		printf("LDFLAGS_SL = ");
+	printf("%s\n", VAL_LDFLAGS_SL);
+#else
+	if (!all)
+		printf("not recorded\n");
+#endif
+}
+
+static void
+show_libs(bool all)
+{
+#ifdef VAL_LIBS
+	if (all)
+		printf("LIBS = ");
+	printf("%s\n", VAL_LIBS);
+#else
+	if (!all)
+		printf("not recorded\n");
+#endif
+}
+
+static void
+show_version(bool all)
+{
+	if (all)
+		printf("VERSION = ");
+	printf("PostgreSQL " PG_VERSION "\n");
+}
+
+
+/*
+ * Table of known information items
+ *
+ * Be careful to keep this in sync with the help() display.
+ */
+typedef struct
+{
+	const char *switchname;
+	void		(*show_func) (bool all);
+} InfoItem;
+
+static const InfoItem info_items[] = {
+	{ "--bindir", show_bindir },
+	{ "--includedir", show_includedir },
+	{ "--includedir-server", show_includedir_server },
+	{ "--libdir", show_libdir },
+	{ "--pkglibdir", show_pkglibdir },
+	{ "--pgxs", show_pgxs },
+	{ "--configure", show_configure },
+	{ "--cc", show_cc },
+	{ "--cppflags", show_cppflags },
+	{ "--cflags", show_cflags },
+	{ "--cflags_sl", show_cflags_sl },
+	{ "--ldflags", show_ldflags },
+	{ "--ldflags_sl", show_ldflags_sl },
+	{ "--libs", show_libs },
+	{ "--version", show_version },
+	{ NULL, NULL }
+};
+
 
 static void
 help(void)
 {
 	printf(_("\n%s provides information about the installed version of PostgreSQL.\n\n"), progname);
 	printf(_("Usage:\n"));
-	printf(_("  %s OPTION...\n\n"), progname);
+	printf(_("  %s [ OPTION ... ]\n\n"), progname);
 	printf(_("Options:\n"));
 	printf(_("  --bindir              show location of user executables\n"));
 	printf(_("  --includedir          show location of C header files of the client\n"
-		 "                        interfaces\n"));
+			 "                        interfaces\n"));
 	printf(_("  --includedir-server   show location of C header files for the server\n"));
 	printf(_("  --libdir              show location of object code libraries\n"));
 	printf(_("  --pkglibdir           show location of dynamically loadable modules\n"));
 	printf(_("  --pgxs                show location of extension makefile\n"));
 	printf(_("  --configure           show options given to \"configure\" script when\n"
 			 "                        PostgreSQL was built\n"));
-	printf(_("  --version             show the PostgreSQL version, then exit\n"));
-	printf(_("  --help                show this help, then exit\n\n"));
+	printf(_("  --cc                  show CC value used when PostgreSQL was built\n"));
+	printf(_("  --cppflags            show CPPFLAGS value used when PostgreSQL was built\n"));
+	printf(_("  --cflags              show CFLAGS value used when PostgreSQL was built\n"));
+	printf(_("  --cflags_sl           show CFLAGS_SL value used when PostgreSQL was built\n"));
+	printf(_("  --ldflags             show LDFLAGS value used when PostgreSQL was built\n"));
+	printf(_("  --ldflags_sl          show LDFLAGS_SL value used when PostgreSQL was built\n"));
+	printf(_("  --libs                show LIBS value used when PostgreSQL was built\n"));
+	printf(_("  --version             show the PostgreSQL version\n"));
+	printf(_("  --help                show this help, then exit\n"));
+	printf(_("With no arguments, all known items are shown.\n\n"));
 	printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
 }
 
@@ -55,53 +287,36 @@ advice(void)
 	fprintf(stderr, _("\nTry \"%s --help\" for more information\n"), progname);
 }
 
+static void
+show_all(void)
+{
+	int			i;
+
+	for (i = 0; info_items[i].switchname != NULL; i++)
+	{
+		(*info_items[i].show_func) (true);
+	}
+}
 
 int
 main(int argc, char **argv)
 {
 	int			i;
+	int			j;
 	int			ret;
-	char		mypath[MAXPGPATH];
-	char		otherpath[MAXPGPATH];
 
 	set_pglocale_pgservice(argv[0], "pg_config");
 
 	progname = get_progname(argv[0]);
 
-	if (argc < 2)
-	{
-		fprintf(stderr, _("%s: argument required\n"), progname);
-		advice();
-		exit(1);
-	}
-
+	/* check for --help */
 	for (i = 1; i < argc; i++)
 	{
-		if (strcmp(argv[i], "--bindir") == 0 ||
-			strcmp(argv[i], "--includedir") == 0 ||
-			strcmp(argv[i], "--includedir-server") == 0 ||
-			strcmp(argv[i], "--libdir") == 0 ||
-			strcmp(argv[i], "--pkglibdir") == 0 ||
-			strcmp(argv[i], "--pgxs") == 0 ||
-			strcmp(argv[i], "--configure") == 0)
-		{
-			/* come back to these later */
-			continue;
-		}
-
-		if (strcmp(argv[i], "--version") == 0)
-		{
-			printf("PostgreSQL " PG_VERSION "\n");
-			exit(0);
-		}
 		if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-?") == 0)
 		{
 			help();
 			exit(0);
 		}
-		fprintf(stderr, _("%s: invalid argument: %s\n"), progname, argv[i]);
-		advice();
-		exit(1);
 	}
 
 	ret = find_my_exec(argv[0], mypath);
@@ -112,40 +327,30 @@ main(int argc, char **argv)
 		exit(1);
 	}
 
-	for (i = 1; i < argc; i++)
+	/* no arguments -> print everything */
+	if (argc < 2)
 	{
-		if (strcmp(argv[i], "--configure") == 0)
-		{
-			/* the VAL_CONFIGURE macro must be defined by the Makefile */
-			printf("%s\n", VAL_CONFIGURE);
-			continue;
-		}
+		show_all();
+		exit(0);
+	}
 
-		if (strcmp(argv[i], "--bindir") == 0)
+	for (i = 1; i < argc; i++)
+	{
+		for (j = 0; info_items[j].switchname != NULL; j++)
 		{
-			/* assume we are located in the bindir */
-			char	   *lastsep;
-
-			strcpy(otherpath, mypath);
-			lastsep = strrchr(otherpath, '/');
-			if (lastsep)
-				*lastsep = '\0';
+			if (strcmp(argv[i], info_items[j].switchname) == 0)
+			{
+				(*info_items[j].show_func) (false);
+				break;
+			}
 		}
-		else if (strcmp(argv[i], "--includedir") == 0)
-			get_include_path(mypath, otherpath);
-		else if (strcmp(argv[i], "--includedir-server") == 0)
-			get_includeserver_path(mypath, otherpath);
-		else if (strcmp(argv[i], "--libdir") == 0)
-			get_lib_path(mypath, otherpath);
-		else if (strcmp(argv[i], "--pkglibdir") == 0)
-			get_pkglib_path(mypath, otherpath);
-		else if (strcmp(argv[i], "--pgxs") == 0)
+		if (info_items[j].switchname == NULL)
 		{
-			get_pkglib_path(mypath, otherpath);
-			strncat(otherpath, "/pgxs/src/makefiles/pgxs.mk", MAXPGPATH - 1);
+			fprintf(stderr, _("%s: invalid argument: %s\n"),
+					progname, argv[i]);
+			advice();
+			exit(1);
 		}
-
-		printf("%s\n", otherpath);
 	}
 
 	return 0;
diff --git a/src/bin/pg_config/win32.mak b/src/bin/pg_config/win32.mak
index c3a8965f5ee9852a50751338b291c9e2537b73e0..0017e57851e02e0af2dae9fb9d2242d3325a8f5e 100644
--- a/src/bin/pg_config/win32.mak
+++ b/src/bin/pg_config/win32.mak
@@ -46,7 +46,7 @@ CLEAN :
 CPP_PROJ=/nologo $(OPT) /W3 /GX /D "WIN32" $(DEBUGDEF) /D "_CONSOLE" /D\
  "_MBCS" /Fp"$(INTDIR)\pg_config.pch" /YX /Fo"$(INTDIR)\\" /Fd"$(INTDIR)\\" /FD /c \
  /I ..\..\include /I ..\..\interfaces\libpq /I ..\..\include\port\win32 \
- /D "HAVE_STRDUP" /D "FRONTEND" /D VAL_CONFIGURE="\"\""
+ /D "HAVE_STRDUP" /D "FRONTEND"
 
 CPP_OBJS=$(INTDIR)/
 CPP_SBRS=.