Skip to content

Commit 510bf55

Browse files
petereCommitfest Bot
authored andcommitted
plpython: Remove support for major version conflict detection
This essentially reverts commit 866566a, which installed safeguards against loading plpython2 and plpython3 into the same process. We don't support plpython2 anymore, so this is obsolete. The Python and PL/Python initialization now happens again in _PG_init() rather than the first time a PL/Python call handler is invoked. (Often, these will be very close together.) I kept the separate PLy_initialize() function introduced by 866566a to keep _PG_init() a bit modular.
1 parent 1206df0 commit 510bf55

File tree

1 file changed

+4
-61
lines changed

1 file changed

+4
-61
lines changed

src/pl/plpython/plpy_main.c

Lines changed: 4 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ PG_FUNCTION_INFO_V1(plpython3_call_handler);
3939
PG_FUNCTION_INFO_V1(plpython3_inline_handler);
4040

4141

42+
static void PLy_initialize(void);
4243
static PLyTrigType PLy_procedure_is_trigger(Form_pg_proc procStruct);
4344
static void plpython_error_callback(void *arg);
4445
static void plpython_inline_error_callback(void *arg);
@@ -47,10 +48,6 @@ static void PLy_init_interp(void);
4748
static PLyExecutionContext *PLy_push_execution_context(bool atomic_context);
4849
static void PLy_pop_execution_context(void);
4950

50-
/* static state for Python library conflict detection */
51-
static int *plpython_version_bitmask_ptr = NULL;
52-
static int plpython_version_bitmask = 0;
53-
5451
/* initialize global variables */
5552
PyObject *PLy_interp_globals = NULL;
5653

@@ -61,62 +58,17 @@ static PLyExecutionContext *PLy_execution_contexts = NULL;
6158
void
6259
_PG_init(void)
6360
{
64-
int **bitmask_ptr;
65-
66-
/*
67-
* Set up a shared bitmask variable telling which Python version(s) are
68-
* loaded into this process's address space. If there's more than one, we
69-
* cannot call into libpython for fear of causing crashes. But postpone
70-
* the actual failure for later, so that operations like pg_restore can
71-
* load more than one plpython library so long as they don't try to do
72-
* anything much with the language.
73-
*
74-
* While we only support Python 3 these days, somebody might create an
75-
* out-of-tree version adding back support for Python 2. Conflicts with
76-
* such an extension should be detected.
77-
*/
78-
bitmask_ptr = (int **) find_rendezvous_variable("plpython_version_bitmask");
79-
if (!(*bitmask_ptr)) /* am I the first? */
80-
*bitmask_ptr = &plpython_version_bitmask;
81-
/* Retain pointer to the agreed-on shared variable ... */
82-
plpython_version_bitmask_ptr = *bitmask_ptr;
83-
/* ... and announce my presence */
84-
*plpython_version_bitmask_ptr |= (1 << PY_MAJOR_VERSION);
85-
86-
/*
87-
* This should be safe even in the presence of conflicting plpythons, and
88-
* it's necessary to do it before possibly throwing a conflict error, or
89-
* the error message won't get localized.
90-
*/
9161
pg_bindtextdomain(TEXTDOMAIN);
62+
63+
PLy_initialize();
9264
}
9365

9466
/*
95-
* Perform one-time setup of PL/Python, after checking for a conflict
96-
* with other versions of Python.
67+
* Perform one-time setup of PL/Python.
9768
*/
9869
static void
9970
PLy_initialize(void)
10071
{
101-
static bool inited = false;
102-
103-
/*
104-
* Check for multiple Python libraries before actively doing anything with
105-
* libpython. This must be repeated on each entry to PL/Python, in case a
106-
* conflicting library got loaded since we last looked.
107-
*
108-
* It is attractive to weaken this error from FATAL to ERROR, but there
109-
* would be corner cases, so it seems best to be conservative.
110-
*/
111-
if (*plpython_version_bitmask_ptr != (1 << PY_MAJOR_VERSION))
112-
ereport(FATAL,
113-
(errmsg("multiple Python libraries are present in session"),
114-
errdetail("Only one Python major version can be used in one session.")));
115-
116-
/* The rest should only be done once per session */
117-
if (inited)
118-
return;
119-
12072
PyImport_AppendInittab("plpy", PyInit_plpy);
12173
Py_Initialize();
12274
PyImport_ImportModule("plpy");
@@ -130,8 +82,6 @@ PLy_initialize(void)
13082
explicit_subtransactions = NIL;
13183

13284
PLy_execution_contexts = NULL;
133-
134-
inited = true;
13585
}
13686

13787
/*
@@ -172,9 +122,6 @@ plpython3_validator(PG_FUNCTION_ARGS)
172122
if (!check_function_bodies)
173123
PG_RETURN_VOID();
174124

175-
/* Do this only after making sure we need to do something */
176-
PLy_initialize();
177-
178125
/* Get the new function's pg_proc entry */
179126
tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
180127
if (!HeapTupleIsValid(tuple))
@@ -199,8 +146,6 @@ plpython3_call_handler(PG_FUNCTION_ARGS)
199146
PLyExecutionContext *exec_ctx;
200147
ErrorContextCallback plerrcontext;
201148

202-
PLy_initialize();
203-
204149
nonatomic = fcinfo->context &&
205150
IsA(fcinfo->context, CallContext) &&
206151
!castNode(CallContext, fcinfo->context)->atomic;
@@ -279,8 +224,6 @@ plpython3_inline_handler(PG_FUNCTION_ARGS)
279224
PLyExecutionContext *exec_ctx;
280225
ErrorContextCallback plerrcontext;
281226

282-
PLy_initialize();
283-
284227
/* Note: SPI_finish() happens in plpy_exec.c, which is dubious design */
285228
SPI_connect_ext(codeblock->atomic ? 0 : SPI_OPT_NONATOMIC);
286229

0 commit comments

Comments
 (0)