From eda04886c1e048d695728206504ab4198462168e Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 4 Oct 2016 17:49:07 -0400 Subject: [PATCH] Avoid direct cross-module links in hstore_plperl and ltree_plpython, too. Just turning the crank on the project started in commit d51924be8. These cases turn out to be exact subsets of the boilerplate needed for hstore_plpython. Discussion: <2652.1475512158@sss.pgh.pa.us> --- contrib/hstore_plperl/Makefile | 16 +++--- contrib/hstore_plperl/hstore_plperl--1.0.sql | 5 -- contrib/hstore_plperl/hstore_plperl.c | 54 +++++++++++++++++++ contrib/hstore_plperl/hstore_plperlu--1.0.sql | 5 -- contrib/ltree_plpython/Makefile | 19 ++++--- contrib/ltree_plpython/ltree_plpython.c | 29 ++++++++++ .../ltree_plpython/ltree_plpython2u--1.0.sql | 5 -- .../ltree_plpython/ltree_plpython3u--1.0.sql | 5 -- .../ltree_plpython/ltree_plpythonu--1.0.sql | 5 -- src/tools/msvc/Mkvcbuild.pm | 3 +- 10 files changed, 102 insertions(+), 44 deletions(-) diff --git a/contrib/hstore_plperl/Makefile b/contrib/hstore_plperl/Makefile index b3b8654bc8..41d34357f9 100644 --- a/contrib/hstore_plperl/Makefile +++ b/contrib/hstore_plperl/Makefile @@ -23,20 +23,20 @@ include $(top_builddir)/src/Makefile.global include $(top_srcdir)/contrib/contrib-global.mk endif -# In configurations that forbid undefined symbols in libraries, link with each -# dependency. This does preclude pgxs builds. +# We must link libperl explicitly ifeq ($(PORTNAME), aix) rpathdir = $(pkglibdir):$(perl_archlibexp)/CORE -SHLIB_LINK += ../hstore/libhstore.exp $(perl_embed_ldflags) -endif +SHLIB_LINK += $(perl_embed_ldflags) +else ifeq ($(PORTNAME), win32) # these settings are the same as for plperl override CPPFLAGS += -DPLPERL_HAVE_UID_GID -Wno-comment -SHLIB_LINK += ../hstore/libhstore.a $(sort $(wildcard ../../src/pl/plperl/libperl*.a)) +# ... see silliness in plperl Makefile ... +SHLIB_LINK += $(sort $(wildcard ../../src/pl/plperl/libperl*.a)) +else +rpathdir = $(perl_archlibexp)/CORE +SHLIB_LINK += $(perl_embed_ldflags) endif - -ifeq ($(PORTNAME), cygwin) -SHLIB_LINK += -L../hstore -l hstore $(perl_embed_ldflags) endif # As with plperl we need to make sure that the CORE directory is included diff --git a/contrib/hstore_plperl/hstore_plperl--1.0.sql b/contrib/hstore_plperl/hstore_plperl--1.0.sql index 9a64fcb18b..af743c8733 100644 --- a/contrib/hstore_plperl/hstore_plperl--1.0.sql +++ b/contrib/hstore_plperl/hstore_plperl--1.0.sql @@ -3,11 +3,6 @@ -- complain if script is sourced in psql, rather than via CREATE EXTENSION \echo Use "CREATE EXTENSION hstore_plperl" to load this file. \quit --- make sure the prerequisite libraries are loaded -LOAD 'plperl'; -SELECT NULL::hstore; - - CREATE FUNCTION hstore_to_plperl(val internal) RETURNS internal LANGUAGE C STRICT IMMUTABLE AS 'MODULE_PATHNAME'; diff --git a/contrib/hstore_plperl/hstore_plperl.c b/contrib/hstore_plperl/hstore_plperl.c index d40a792730..480212f341 100644 --- a/contrib/hstore_plperl/hstore_plperl.c +++ b/contrib/hstore_plperl/hstore_plperl.c @@ -1,5 +1,7 @@ #include "postgres.h" + #undef _ + #include "fmgr.h" #include "plperl.h" #include "plperl_helpers.h" @@ -7,6 +9,58 @@ PG_MODULE_MAGIC; +extern void _PG_init(void); + +/* Linkage to functions in hstore module */ +typedef HStore *(*hstoreUpgrade_t) (Datum orig); +static hstoreUpgrade_t hstoreUpgrade_p; +typedef int (*hstoreUniquePairs_t) (Pairs *a, int32 l, int32 *buflen); +static hstoreUniquePairs_t hstoreUniquePairs_p; +typedef HStore *(*hstorePairs_t) (Pairs *pairs, int32 pcount, int32 buflen); +static hstorePairs_t hstorePairs_p; +typedef size_t (*hstoreCheckKeyLen_t) (size_t len); +static hstoreCheckKeyLen_t hstoreCheckKeyLen_p; +typedef size_t (*hstoreCheckValLen_t) (size_t len); +static hstoreCheckValLen_t hstoreCheckValLen_p; + + +/* + * Module initialize function: fetch function pointers for cross-module calls. + */ +void +_PG_init(void) +{ + /* Asserts verify that typedefs above match original declarations */ + AssertVariableIsOfType(&hstoreUpgrade, hstoreUpgrade_t); + hstoreUpgrade_p = (hstoreUpgrade_t) + load_external_function("$libdir/hstore", "hstoreUpgrade", + true, NULL); + AssertVariableIsOfType(&hstoreUniquePairs, hstoreUniquePairs_t); + hstoreUniquePairs_p = (hstoreUniquePairs_t) + load_external_function("$libdir/hstore", "hstoreUniquePairs", + true, NULL); + AssertVariableIsOfType(&hstorePairs, hstorePairs_t); + hstorePairs_p = (hstorePairs_t) + load_external_function("$libdir/hstore", "hstorePairs", + true, NULL); + AssertVariableIsOfType(&hstoreCheckKeyLen, hstoreCheckKeyLen_t); + hstoreCheckKeyLen_p = (hstoreCheckKeyLen_t) + load_external_function("$libdir/hstore", "hstoreCheckKeyLen", + true, NULL); + AssertVariableIsOfType(&hstoreCheckValLen, hstoreCheckValLen_t); + hstoreCheckValLen_p = (hstoreCheckValLen_t) + load_external_function("$libdir/hstore", "hstoreCheckValLen", + true, NULL); +} + + +/* These defines must be after the module init function */ +#define hstoreUpgrade hstoreUpgrade_p +#define hstoreUniquePairs hstoreUniquePairs_p +#define hstorePairs hstorePairs_p +#define hstoreCheckKeyLen hstoreCheckKeyLen_p +#define hstoreCheckValLen hstoreCheckValLen_p + PG_FUNCTION_INFO_V1(hstore_to_plperl); diff --git a/contrib/hstore_plperl/hstore_plperlu--1.0.sql b/contrib/hstore_plperl/hstore_plperlu--1.0.sql index f355284907..7c3bc86eba 100644 --- a/contrib/hstore_plperl/hstore_plperlu--1.0.sql +++ b/contrib/hstore_plperl/hstore_plperlu--1.0.sql @@ -3,11 +3,6 @@ -- complain if script is sourced in psql, rather than via CREATE EXTENSION \echo Use "CREATE EXTENSION hstore_plperlu" to load this file. \quit --- make sure the prerequisite libraries are loaded -LOAD 'plperl'; -SELECT NULL::hstore; - - CREATE FUNCTION hstore_to_plperlu(val internal) RETURNS internal LANGUAGE C STRICT IMMUTABLE AS 'MODULE_PATHNAME', 'hstore_to_plperl'; diff --git a/contrib/ltree_plpython/Makefile b/contrib/ltree_plpython/Makefile index 08186f19a1..c45b7c2b09 100644 --- a/contrib/ltree_plpython/Makefile +++ b/contrib/ltree_plpython/Makefile @@ -4,7 +4,7 @@ MODULE_big = ltree_plpython$(python_majorversion) OBJS = ltree_plpython.o $(WIN32RES) PGFILEDESC = "ltree_plpython - ltree transform for plpython" -PG_CPPFLAGS = -I$(top_srcdir)/src/pl/plpython $(python_includespec) -I$(top_srcdir)/contrib/ltree +PG_CPPFLAGS = -I$(top_srcdir)/src/pl/plpython $(python_includespec) -I$(top_srcdir)/contrib/ltree -DPLPYTHON_LIBNAME='"plpython$(python_majorversion)"' EXTENSION = ltree_plpythonu ltree_plpython2u ltree_plpython3u DATA = ltree_plpythonu--1.0.sql ltree_plpython2u--1.0.sql ltree_plpython3u--1.0.sql @@ -23,19 +23,18 @@ include $(top_builddir)/src/Makefile.global include $(top_srcdir)/contrib/contrib-global.mk endif -# In configurations that forbid undefined symbols in libraries, link with each -# dependency. This does preclude pgxs builds. +# We must link libpython explicitly ifeq ($(PORTNAME), aix) rpathdir = $(pkglibdir):$(python_libdir) -SHLIB_LINK += $(python_libspec) $(python_additional_libs) $(sort $(wildcard ../../src/pl/plpython/libplpython*.exp)) -endif +SHLIB_LINK += $(python_libspec) $(python_additional_libs) +else ifeq ($(PORTNAME), win32) -SHLIB_LINK += $(sort $(wildcard ../../src/pl/plpython/libpython*.a)) $(sort $(wildcard ../../src/pl/plpython/libplpython*.a)) +# ... see silliness in plpython Makefile ... +SHLIB_LINK += $(sort $(wildcard ../../src/pl/plpython/libpython*.a)) +else +rpathdir = $(python_libdir) +SHLIB_LINK += $(python_libspec) endif - -ifeq ($(PORTNAME), cygwin) -SHLIB_LINK += -L../ltree -lltree -L../../src/pl/plpython \ - -lplpython$(python_majorversion) $(python_libspec) endif REGRESS_OPTS += --load-extension=ltree diff --git a/contrib/ltree_plpython/ltree_plpython.c b/contrib/ltree_plpython/ltree_plpython.c index 26b7b3c275..bdd462a91b 100644 --- a/contrib/ltree_plpython/ltree_plpython.c +++ b/contrib/ltree_plpython/ltree_plpython.c @@ -1,10 +1,39 @@ #include "postgres.h" + #include "fmgr.h" #include "plpython.h" #include "ltree.h" PG_MODULE_MAGIC; +extern void _PG_init(void); + +/* Linkage to functions in plpython module */ +#if PY_MAJOR_VERSION >= 3 +typedef PyObject *(*PLyUnicode_FromStringAndSize_t) (const char *s, Py_ssize_t size); +static PLyUnicode_FromStringAndSize_t PLyUnicode_FromStringAndSize_p; +#endif + + +/* + * Module initialize function: fetch function pointers for cross-module calls. + */ +void +_PG_init(void) +{ + /* Asserts verify that typedefs above match original declarations */ +#if PY_MAJOR_VERSION >= 3 + AssertVariableIsOfType(&PLyUnicode_FromStringAndSize, PLyUnicode_FromStringAndSize_t); + PLyUnicode_FromStringAndSize_p = (PLyUnicode_FromStringAndSize_t) + load_external_function("$libdir/" PLPYTHON_LIBNAME, "PLyUnicode_FromStringAndSize", + true, NULL); +#endif +} + + +/* These defines must be after the module init function */ +#define PLyUnicode_FromStringAndSize PLyUnicode_FromStringAndSize_p + PG_FUNCTION_INFO_V1(ltree_to_plpython); diff --git a/contrib/ltree_plpython/ltree_plpython2u--1.0.sql b/contrib/ltree_plpython/ltree_plpython2u--1.0.sql index 62531371bf..5c4a703701 100644 --- a/contrib/ltree_plpython/ltree_plpython2u--1.0.sql +++ b/contrib/ltree_plpython/ltree_plpython2u--1.0.sql @@ -3,11 +3,6 @@ -- complain if script is sourced in psql, rather than via CREATE EXTENSION \echo Use "CREATE EXTENSION ltree_plpython2u" to load this file. \quit --- make sure the prerequisite libraries are loaded -LOAD 'plpython2'; -SELECT NULL::ltree; - - CREATE FUNCTION ltree_to_plpython2(val internal) RETURNS internal LANGUAGE C STRICT IMMUTABLE AS 'MODULE_PATHNAME', 'ltree_to_plpython'; diff --git a/contrib/ltree_plpython/ltree_plpython3u--1.0.sql b/contrib/ltree_plpython/ltree_plpython3u--1.0.sql index 3f21d1b721..09ada3c7e8 100644 --- a/contrib/ltree_plpython/ltree_plpython3u--1.0.sql +++ b/contrib/ltree_plpython/ltree_plpython3u--1.0.sql @@ -3,11 +3,6 @@ -- complain if script is sourced in psql, rather than via CREATE EXTENSION \echo Use "CREATE EXTENSION ltree_plpython3u" to load this file. \quit --- make sure the prerequisite libraries are loaded -LOAD 'plpython3'; -SELECT NULL::ltree; - - CREATE FUNCTION ltree_to_plpython3(val internal) RETURNS internal LANGUAGE C STRICT IMMUTABLE AS 'MODULE_PATHNAME', 'ltree_to_plpython'; diff --git a/contrib/ltree_plpython/ltree_plpythonu--1.0.sql b/contrib/ltree_plpython/ltree_plpythonu--1.0.sql index e8deadc62d..ee93edf28b 100644 --- a/contrib/ltree_plpython/ltree_plpythonu--1.0.sql +++ b/contrib/ltree_plpython/ltree_plpythonu--1.0.sql @@ -3,11 +3,6 @@ -- complain if script is sourced in psql, rather than via CREATE EXTENSION \echo Use "CREATE EXTENSION ltree_plpythonu" to load this file. \quit --- make sure the prerequisite libraries are loaded -LOAD 'plpython2'; -- change to plpython3 if that ever becomes the default -SELECT NULL::ltree; - - CREATE FUNCTION ltree_to_plpython(val internal) RETURNS internal LANGUAGE C STRICT IMMUTABLE AS 'MODULE_PATHNAME'; diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm index d2ab9e466e..de764dd4d4 100644 --- a/src/tools/msvc/Mkvcbuild.pm +++ b/src/tools/msvc/Mkvcbuild.pm @@ -480,10 +480,11 @@ sub mkvcbuild 'plpython' . $pymajorver, 'src/pl/plpython', 'hstore', 'contrib/hstore'); $hstore_plpython->AddDefine('PLPYTHON_LIBNAME="plpython' . $pymajorver . '"'); - AddTransformModule( + my $ltree_plpython = AddTransformModule( 'ltree_plpython' . $pymajorver, 'contrib/ltree_plpython', 'plpython' . $pymajorver, 'src/pl/plpython', 'ltree', 'contrib/ltree'); + $ltree_plpython->AddDefine('PLPYTHON_LIBNAME="plpython' . $pymajorver . '"'); } if ($solution->{options}->{perl})