From fb6fcbd33fbbd6d31fa2b39938e60ecb48dc4de4 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Mon, 11 Jan 2016 20:06:36 -0500
Subject: [PATCH] Use LOAD not actual code execution to pull in plpython
 library.

Commit 866566a690bb9916 is insufficient to prevent dump/reload failures
when using transform modules in a database with both plpython2 and
plpython3 installed.  The reason is that the transform extension scripts
use DO blocks as a mechanism to pull in the libpython library before
creating the transform function.  It's necessary to preload the library
because the dynamic loader won't do it for us on every platform, leading
to "unresolved symbol" failures when the transform library is loaded.
But it's *not* necessary to execute Python code, and doing so will
provoke a multiple-Pythons-are-loaded error even after the preceding
commit.

To fix, use LOAD instead of a DO block.  That requires superuser privilege,
but creation of a C function does anyway.  It also embeds knowledge of
the underlying library name for each PL language; but that's wired into
the initdb-time contents of pg_pltemplate too, so that doesn't seem like
a large problem either.  Note that CREATE TRANSFORM as such doesn't call
the language module at all.

Per a report from Paul Jones.  Back-patch to 9.5 where transform modules
were introduced.
---
 contrib/hstore_plperl/hstore_plperl--1.0.sql       | 2 +-
 contrib/hstore_plperl/hstore_plperlu--1.0.sql      | 2 +-
 contrib/hstore_plpython/hstore_plpython2u--1.0.sql | 2 +-
 contrib/hstore_plpython/hstore_plpython3u--1.0.sql | 2 +-
 contrib/hstore_plpython/hstore_plpythonu--1.0.sql  | 2 +-
 contrib/ltree_plpython/ltree_plpython2u--1.0.sql   | 2 +-
 contrib/ltree_plpython/ltree_plpython3u--1.0.sql   | 2 +-
 contrib/ltree_plpython/ltree_plpythonu--1.0.sql    | 2 +-
 8 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/contrib/hstore_plperl/hstore_plperl--1.0.sql b/contrib/hstore_plperl/hstore_plperl--1.0.sql
index ea0ad7688d7..a4fd7c22db8 100644
--- a/contrib/hstore_plperl/hstore_plperl--1.0.sql
+++ b/contrib/hstore_plperl/hstore_plperl--1.0.sql
@@ -1,5 +1,5 @@
 -- make sure the prerequisite libraries are loaded
-DO '' LANGUAGE plperl;
+LOAD 'plperl';
 SELECT NULL::hstore;
 
 
diff --git a/contrib/hstore_plperl/hstore_plperlu--1.0.sql b/contrib/hstore_plperl/hstore_plperlu--1.0.sql
index 46ad35c4879..2c2e3e3848a 100644
--- a/contrib/hstore_plperl/hstore_plperlu--1.0.sql
+++ b/contrib/hstore_plperl/hstore_plperlu--1.0.sql
@@ -1,5 +1,5 @@
 -- make sure the prerequisite libraries are loaded
-DO '' LANGUAGE plperlu;
+LOAD 'plperl';
 SELECT NULL::hstore;
 
 
diff --git a/contrib/hstore_plpython/hstore_plpython2u--1.0.sql b/contrib/hstore_plpython/hstore_plpython2u--1.0.sql
index c998de51c96..a793dc9c0cd 100644
--- a/contrib/hstore_plpython/hstore_plpython2u--1.0.sql
+++ b/contrib/hstore_plpython/hstore_plpython2u--1.0.sql
@@ -1,5 +1,5 @@
 -- make sure the prerequisite libraries are loaded
-DO '1' LANGUAGE plpython2u;
+LOAD 'plpython2';
 SELECT NULL::hstore;
 
 
diff --git a/contrib/hstore_plpython/hstore_plpython3u--1.0.sql b/contrib/hstore_plpython/hstore_plpython3u--1.0.sql
index 61d0e47793c..a85c85d4c1b 100644
--- a/contrib/hstore_plpython/hstore_plpython3u--1.0.sql
+++ b/contrib/hstore_plpython/hstore_plpython3u--1.0.sql
@@ -1,5 +1,5 @@
 -- make sure the prerequisite libraries are loaded
-DO '1' LANGUAGE plpython3u;
+LOAD 'plpython3';
 SELECT NULL::hstore;
 
 
diff --git a/contrib/hstore_plpython/hstore_plpythonu--1.0.sql b/contrib/hstore_plpython/hstore_plpythonu--1.0.sql
index 6acb97aab9e..e2e4721dca9 100644
--- a/contrib/hstore_plpython/hstore_plpythonu--1.0.sql
+++ b/contrib/hstore_plpython/hstore_plpythonu--1.0.sql
@@ -1,5 +1,5 @@
 -- make sure the prerequisite libraries are loaded
-DO '1' LANGUAGE plpythonu;
+LOAD 'plpython2';  -- change to plpython3 if that ever becomes the default
 SELECT NULL::hstore;
 
 
diff --git a/contrib/ltree_plpython/ltree_plpython2u--1.0.sql b/contrib/ltree_plpython/ltree_plpython2u--1.0.sql
index 29a12d45a28..f040bd3f561 100644
--- a/contrib/ltree_plpython/ltree_plpython2u--1.0.sql
+++ b/contrib/ltree_plpython/ltree_plpython2u--1.0.sql
@@ -1,5 +1,5 @@
 -- make sure the prerequisite libraries are loaded
-DO '1' LANGUAGE plpython2u;
+LOAD 'plpython2';
 SELECT NULL::ltree;
 
 
diff --git a/contrib/ltree_plpython/ltree_plpython3u--1.0.sql b/contrib/ltree_plpython/ltree_plpython3u--1.0.sql
index 1300a78c66f..7afe51f1483 100644
--- a/contrib/ltree_plpython/ltree_plpython3u--1.0.sql
+++ b/contrib/ltree_plpython/ltree_plpython3u--1.0.sql
@@ -1,5 +1,5 @@
 -- make sure the prerequisite libraries are loaded
-DO '1' LANGUAGE plpython3u;
+LOAD 'plpython3';
 SELECT NULL::ltree;
 
 
diff --git a/contrib/ltree_plpython/ltree_plpythonu--1.0.sql b/contrib/ltree_plpython/ltree_plpythonu--1.0.sql
index 1d1af28f4eb..50f35dd16b0 100644
--- a/contrib/ltree_plpython/ltree_plpythonu--1.0.sql
+++ b/contrib/ltree_plpython/ltree_plpythonu--1.0.sql
@@ -1,5 +1,5 @@
 -- make sure the prerequisite libraries are loaded
-DO '1' LANGUAGE plpythonu;
+LOAD 'plpython2';  -- change to plpython3 if that ever becomes the default
 SELECT NULL::ltree;
 
 
-- 
GitLab