1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
From 485c5c6c0dae58b5c6dd093809d83f09fc56f8d4 Mon Sep 17 00:00:00 2001
From: Ronan Dunklau <ronan.dunklau@dalibo.com>
Date: Mon, 4 Apr 2016 15:31:47 +0200
Subject: [PATCH] Ugly workaround for issue #136
---
sql/multicorn.sql | 14 ++++++++++++++
src/multicorn.c | 15 +++++++++++++--
2 files changed, 27 insertions(+), 2 deletions(-)
diff --git a/sql/multicorn.sql b/sql/multicorn.sql
index a01443f..7088bc5 100755
--- a/sql/multicorn.sql
+++ b/sql/multicorn.sql
@@ -1,4 +1,18 @@
-- create wrapper with validator and handler
+--
+CREATE OR REPLACE FUNCTION multicorn_check_plpython3u() RETURNS VOID AS $$
+DECLARE
+res bool := false;
+BEGIN
+ SELECT count(1) > 0 INTO res FROM pg_language WHERE lanname = 'plpython3u';
+ IF res THEN
+ DO $py$ import plpy; 2+2 $py$ language plpython3u;
+ END IF;
+END
+$$ language plpgsql VOLATILE;
+
+SELECT multicorn_check_plpython3u();
+
CREATE OR REPLACE FUNCTION multicorn_validator (text[], oid)
RETURNS bool
AS 'MODULE_PATHNAME'
diff --git a/src/multicorn.c b/src/multicorn.c
index 9cb936b..df6529d 100644
--- a/src/multicorn.c
+++ b/src/multicorn.c
@@ -24,6 +24,8 @@
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "parser/parsetree.h"
+#include "dynloader.h"
+#include "executor/spi.h"
PG_MODULE_MAGIC;
@@ -112,9 +114,18 @@ void
_PG_init()
{
HASHCTL ctl;
- MemoryContext oldctx = MemoryContextSwitchTo(CacheMemoryContext);
-
+ MemoryContext oldctx;
+#if PY_MAJOR_VERSION >= 3
+ /* Make sure that the python interpreter is initialized by plpy
+ * and not by Multicorn. */
+ SPI_connect();
+ SPI_execute("SELECT multicorn_check_plpython3u()", false, 0);
+ SPI_finish();
+#endif
+ oldctx = MemoryContextSwitchTo(CacheMemoryContext);
+ if(!Py_IsInitialized()){
Py_Initialize();
+ }
RegisterXactCallback(multicorn_xact_callback, NULL);
#if PG_VERSION_NUM >= 90300
RegisterSubXactCallback(multicorn_subxact_callback, NULL);
|