From 96c102fe27de36eefb7a73c1147da2143e2e1a03 Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Fri, 18 Dec 2009 21:28:42 +0000
Subject: [PATCH] Install server-side language PL/pgSQL by default.

---
 doc/src/sgml/installation.sgml | 10 +++++-----
 src/bin/initdb/initdb.c        | 30 +++++++++++++++++++++++++++++-
 src/bin/pg_dump/pg_dump.c      | 12 ++++++++----
 src/test/regress/GNUmakefile   |  4 ++--
 4 files changed, 44 insertions(+), 12 deletions(-)

diff --git a/doc/src/sgml/installation.sgml b/doc/src/sgml/installation.sgml
index 83372275214..29caf8a51ee 100644
--- a/doc/src/sgml/installation.sgml
+++ b/doc/src/sgml/installation.sgml
@@ -1,4 +1,4 @@
-<!-- $PostgreSQL: pgsql/doc/src/sgml/installation.sgml,v 1.333 2009/12/15 22:59:53 petere Exp $ -->
+<!-- $PostgreSQL: pgsql/doc/src/sgml/installation.sgml,v 1.334 2009/12/18 21:28:42 momjian Exp $ -->
 
 <chapter id="installation">
  <title><![%standalone-include[<productname>PostgreSQL</>]]>
@@ -2266,14 +2266,14 @@ hosts=local4,bind4
      is <command>createlang</command> failing with unusual errors.
      For example, running as the owner of the PostgreSQL installation:
 <screen>
--bash-3.00$ createlang plpgsql template1
-createlang: language installation failed: ERROR:  could not load library "/opt/dbs/pgsql748/lib/plpgsql.so": A memory address is not in the address space for the process.
+-bash-3.00$ createlang plperl template1
+createlang: language installation failed: ERROR:  could not load library "/opt/dbs/pgsql748/lib/plperl.so": A memory address is not in the address space for the process.
 </screen>
     Running as a non-owner in the group posessing the PostgreSQL
     installation:
 <screen>
--bash-3.00$ createlang plpgsql template1
-createlang: language installation failed: ERROR:  could not load library "/opt/dbs/pgsql748/lib/plpgsql.so": Bad address
+-bash-3.00$ createlang plperl template1
+createlang: language installation failed: ERROR:  could not load library "/opt/dbs/pgsql748/lib/plperl.so": Bad address
 </screen>
      Another example is out of memory errors in the PostgreSQL server
      logs, with every memory allocation near or greater than 256 MB
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index d7cf1a44333..fb8a40915f4 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -42,7 +42,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  * Portions taken from FreeBSD.
  *
- * $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.179 2009/12/18 18:45:50 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.180 2009/12/18 21:28:42 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -176,6 +176,7 @@ static void setup_dictionary(void);
 static void setup_privileges(void);
 static void set_info_version(void);
 static void setup_schema(void);
+static void load_plpgsql(void);
 static void vacuum_db(void);
 static void make_template0(void);
 static void make_postgres(void);
@@ -1893,6 +1894,31 @@ setup_schema(void)
 	check_ok();
 }
 
+/*
+ * load PL/pgsql server-side language
+ */
+static void
+load_plpgsql(void)
+{
+	PG_CMD_DECL;
+
+	fputs(_("loading PL/pgSQL server-side language ... "), stdout);
+	fflush(stdout);
+
+	snprintf(cmd, sizeof(cmd),
+			 "\"%s\" %s template1 >%s",
+			 backend_exec, backend_options,
+			 DEVNULL);
+
+	PG_CMD_OPEN;
+
+	PG_CMD_PUTS("CREATE LANGUAGE plpgsql;\n");
+
+	PG_CMD_CLOSE;
+
+	check_ok();
+}
+
 /*
  * clean everything up in template1
  */
@@ -3134,6 +3160,8 @@ main(int argc, char *argv[])
 
 	setup_schema();
 
+	load_plpgsql();
+
 	vacuum_db();
 
 	make_template0();
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index bb361b33669..e1fd1c0dc4c 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -12,7 +12,7 @@
  *	by PostgreSQL
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.556 2009/12/14 00:39:11 itagaki Exp $
+ *	  $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.557 2009/12/18 21:28:42 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -32,6 +32,7 @@
 
 #include "access/attnum.h"
 #include "access/sysattr.h"
+#include "access/transam.h"
 #include "catalog/pg_cast.h"
 #include "catalog/pg_class.h"
 #include "catalog/pg_default_acl.h"
@@ -4599,8 +4600,10 @@ getProcLangs(int *numProcLangs)
 						  "(%s lanowner) AS lanowner "
 						  "FROM pg_language "
 						  "WHERE lanispl "
+						  /* do not dump initdb-installed languages */
+						  "AND oid >= %u "
 						  "ORDER BY oid",
-						  username_subquery);
+						  username_subquery, FirstNormalObjectId);
 	}
 	else if (g_fout->remoteVersion >= 80300)
 	{
@@ -4610,9 +4613,10 @@ getProcLangs(int *numProcLangs)
 						  "lanvalidator,  lanacl, "
 						  "(%s lanowner) AS lanowner "
 						  "FROM pg_language "
-						  "WHERE lanispl "
+						  "WHERE lanispl%s"
 						  "ORDER BY oid",
-						  username_subquery);
+						  username_subquery,
+						  binary_upgrade ? "\nAND lanname != 'plpgsql'" : "");
 	}
 	else if (g_fout->remoteVersion >= 80100)
 	{
diff --git a/src/test/regress/GNUmakefile b/src/test/regress/GNUmakefile
index c7e7c220784..4a47bdfe4f6 100644
--- a/src/test/regress/GNUmakefile
+++ b/src/test/regress/GNUmakefile
@@ -6,7 +6,7 @@
 # Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
 # Portions Copyright (c) 1994, Regents of the University of California
 #
-# $PostgreSQL: pgsql/src/test/regress/GNUmakefile,v 1.79 2009/10/26 21:11:22 petere Exp $
+# $PostgreSQL: pgsql/src/test/regress/GNUmakefile,v 1.80 2009/12/18 21:28:42 momjian Exp $
 #
 #-------------------------------------------------------------------------
 
@@ -138,7 +138,7 @@ tablespace-setup:
 ## Run tests
 ##
 
-pg_regress_call = ./pg_regress --inputdir=$(srcdir) --dlpath=. --multibyte=$(MULTIBYTE) --load-language=plpgsql $(NOLOCALE)
+pg_regress_call = ./pg_regress --inputdir=$(srcdir) --dlpath=. --multibyte=$(MULTIBYTE) $(NOLOCALE)
 
 check: all
 	$(pg_regress_call) --temp-install=./tmp_check --top-builddir=$(top_builddir) --schedule=$(srcdir)/parallel_schedule $(MAXCONNOPT) $(TEMP_CONF)
-- 
GitLab